shithub: hj264

Download patch

ref: 15ebc8dd890b8e843ef20bb54736215c6e0e4925
parent: a3dc6a1a11abd625563c2caa0303c8dde6fcf2f7
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Jul 13 11:03:45 EDT 2021

limit fps (in a dumb way)

--- a/hj264.c
+++ b/hj264.c
@@ -7,6 +7,8 @@
 #include <memdraw.h>
 #include <tos.h>
 
+void npe_nsleep(uvlong ns);
+
 #define max(a,b) ((a)>(b)?(a):(b))
 #define min(a,b) ((a)<(b)?(a):(b))
 #define clp(v,a,b) min((b), max((v),(a)))
@@ -303,8 +305,8 @@
 main(int argc, char **argv)
 {
 	int nthreads, fps, kbps, denoise, quality, qp;
+	uvlong start, end, fstart, fend;
 	int ww, hh, in, nframes;
-	uvlong start, end;
 	Memimage *im;
 	Hj264 *h;
 	char *s;
@@ -355,6 +357,7 @@
 	h = nil;
 	start = nanosec();
 	for(nframes = 0;; nframes++){
+		fstart = nanosec();
 		seek(in, 0, 0);
 		if((im = readmemimage(in)) == nil)
 			break;
@@ -367,6 +370,8 @@
 			if(Binit(&h->out, 1, OWRITE) < 0)
 				sysfatal("Binit failed: %r");
 			h->frame = chancreate(sizeof(void*), fps);
+
+			/* FIXME how about changing these on the fly? */
 			h->rp.encode_speed = Maxquality - quality;
 			h->rp.qp_min = h->rp.qp_max = qp;
 			if(kbps > 0){
@@ -377,8 +382,12 @@
 			proccreate(encthread, h, mainstacksize);
 		}
 
-		sendp(h->frame, im);
+		if(sendp(h->frame, im) != 1)
+			break;
+		fend = nanosec();
 
+		if(1000000000ULL/fps > (fend - fstart))
+			npe_nsleep(1000000000ULL/fps - (fend - fstart));
 		if(nanosec() - start > 4000000000ULL)
 			break;
 	}