shithub: hj264

Download patch

ref: c6ffefd38f9b69b560e543571e98549c3facd611
parent: bf2a0ea2464a89431888f01af4f5b5c0bbe62105
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jul 14 11:02:48 EDT 2021

remove 10s timer, use npe_nanosec, shut down better on Del

--- a/hj264.c
+++ b/hj264.c
@@ -8,6 +8,7 @@
 #include <tos.h>
 
 void npe_nsleep(uvlong ns);
+uvlong npe_nanosec(void);
 
 #define max(a,b) ((a)>(b)?(a):(b))
 #define min(a,b) ((a)<(b)?(a):(b))
@@ -268,36 +269,6 @@
 	free(h);
 }
 
-static uvlong
-nanosec(void)
-{
-	static uvlong fasthz, xstart;
-	uvlong x, div;
-
-	if(fasthz == ~0ULL)
-		return nsec() - xstart;
-
-	if(fasthz == 0){
-		if(_tos->cyclefreq){
-			cycles(&xstart);
-			fasthz = _tos->cyclefreq;
-		} else {
-			xstart = nsec();
-			fasthz = ~0ULL;
-			fprint(2, "cyclefreq not available, falling back to nsec()\n");
-			fprint(2, "you might want to disable aux/timesync\n");
-			return 0;
-		}
-	}
-	cycles(&x);
-	x -= xstart;
-
-	/* this is ugly */
-	for(div = 1000000000ULL; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
-
-	return x / (fasthz / div);
-}
-
 static void
 encthread(void *p)
 {
@@ -354,13 +325,29 @@
 	threadexitsall("usage");
 }
 
+static uvlong nframes, tstart;
+
+static int
+done(void *, char *msg)
+{
+	uvlong s;
+	Hj264 *h;
+
+	if((s = (npe_nanosec() - tstart)/1000000000ULL) != 0)
+		fprint(2, "%llud fps\n", nframes / s);
+	h = *threaddata();
+	Bflush(&h->out);
+	threadexitsall(msg);
+	return 1;
+}
+
 int
 main(int argc, char **argv)
 {
 	int nthreads, fps, kbps, denoise, quality, qp;
-	uvlong start, end, fstart, fend;
 	char *s, tmp[61], *f[5];
-	int ww, hh, in, nframes;
+	uvlong fstart, fend;
+	int ww, hh, in;
 	Img *img;
 	Hj264 *h;
 
@@ -434,31 +421,25 @@
 		h->rp.desired_frame_bytes = kbps*1000/8/fps;
 	}
 	proccreate(encthread, h, mainstacksize);
+	*threaddata() = h;
+	threadnotify(done, 1);
 
-	start = nanosec();
+	tstart = npe_nanosec();
 	for(nframes = 0;; nframes++){
-		fstart = nanosec();
+		fstart = npe_nanosec();
 		if((img = imgread(in, ww, hh)) == nil)
 			break;
 		if(sendp(h->frame, img) != 1)
 			break;
-		fend = nanosec();
+		fend = npe_nanosec();
 
 		if(1000000000ULL/fps > (fend - fstart))
 			npe_nsleep(1000000000ULL/fps - (fend - fstart));
-
-		/* FIXME make a graceful shutdown on a note */
-		if(nanosec() - start > 10000000000ULL)
-			break;
 	}
-	end = nanosec();
-	fprint(2, "%d fps\n", (int)(nframes / ((end - start)/1000000000ULL)));
 
 	chanclose(h->frame);
 	recvp(h->done);
 	hj264free(h);
 
-	threadexitsall(nil);
-
-	return 0;
+	return done(nil, nil);
 }