shithub: hj264

Download patch

ref: 17578ac2a4ded4303459881acd0d149188692292
parent: cad6611cd489fd7214576800cd5e461a51a7e60a
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Sat Jul 30 18:49:26 EDT 2022

exit cleanly; print average fps when debug is enabled

--- a/hj264.c
+++ b/hj264.c
@@ -137,12 +137,10 @@
 {
 	int e;
 
-	if((e = H264E_encode(h->persist, h->scratch, &h->rp, &h->ioyuv, data, sz)) != 0){
+	if((e = H264E_encode(h->persist, h->scratch, &h->rp, &h->ioyuv, data, sz)) != 0)
 		werrstr("H264E_encode: %ℏ", e);
-		return -1;
-	}
 
-	return 0;
+	return e;
 }
 
 static Hj264 *
@@ -205,7 +203,7 @@
 		t->id = i;
 		t->job = chancreate(sizeof(void*), 0);
 		t->done = chancreate(sizeof(void*), 0);
-		procrfork(threadf, t, mainstacksize, RFCFDG|RFCENVG);
+		proccreate(threadf, t, mainstacksize);
 		recvp(t->done);
 	}
 
@@ -213,7 +211,7 @@
 }
 
 static void
-hj264free(Hj264 *h)
+hj264close(Hj264 *h)
 {
 	int i;
 
@@ -222,9 +220,20 @@
 		chanclose(h->threads[i].job);
 	}
 
-	free(h);
+	Bflush(&h->out);
 }
 
+static int
+done(void*, char*)
+{
+	Hj264 *h;
+
+	h = *threaddata();
+	Bflush(&h->out);
+
+	return 1;
+}
+
 static void
 encthread(void *p)
 {
@@ -234,12 +243,15 @@
 	Hj264 *h;
 	int sz;
 
-	threadsetname("hj264/encthread");
-
 	h = p;
 	prev = nil;
+
+	threadsetname("hj264/encthread");
+	threadnotify(done, 1);
+	*threaddata() = h;
+
 	for(;;){
-		if((img = recvp(h->frame)) == nil)
+		if(recv(h->frame, &img) < 0)
 			break;
 		if(opt && prev != nil && memcmp(img->bgrx, prev->bgrx, img->w*img->h*4) == 0){
 			free(img);
@@ -316,25 +328,12 @@
 	threadexitsall("usage");
 }
 
-static int
-done(void *, char *)
-{
-	Hj264 *h;
-
-	if((h = *procdata()) != nil){
-		chanclose(h->frame);
-		recvp(h->done);
-	}
-
-	return 1;
-}
-
 int
 main(int argc, char **argv)
 {
 	int nthreads, fps, kbps, denoise, quality, qp, gop;
 	char *s, tmp[61], *f[5];
-	uvlong fstart, fend;
+	uvlong fstart, fend, f₀;
 	int ww, hh, in, fmt;
 	u8int v[20];
 	Img *img;
@@ -428,9 +427,7 @@
 		h->rp.qp_max = 50;
 		h->rp.desired_frame_bytes = kbps*1000/8/fps;
 	}
-	*procdata() = h;
-	atnotify(done, 1);
-	procrfork(encthread, h, mainstacksize, RFCENVG);
+	proccreate(encthread, h, mainstacksize);
 
 	if(h->fmt == FmtIVF){
 		Bwrite(&h->out, "DKIF\x00\x00\x20\x00AVC1", 12);
@@ -452,7 +449,8 @@
 			return -1;
 	}
 
-	tstart = nsec() - npe_nanosec();
+	f₀ = npe_nanosec();
+	tstart = nsec() - f₀;
 	for(nframes = 0;; nframes++){
 		fstart = npe_nanosec();
 		if((img = imgread(in, ww, hh)) == nil)
@@ -461,13 +459,14 @@
 			break;
 		fend = npe_nanosec();
 
+		if(debug && nframes > 0 && (nframes % fps) == 0)
+			fprint(2, "avg fps: %llud\n", nframes/((fend - f₀)/Nsec));
+
 		if(Nsec/fps > (fend - fstart))
 			npe_nsleep(Nsec/fps - (fend - fstart));
 	}
 
 	chanclose(h->frame);
-	recvp(h->done);
-	hj264free(h);
 
-	return done(nil, nil);
+	return 0;
 }