shithub: hj264

Download patch

ref: fe02bf0c27a687383d02f0ca9e3e10eb88626b0a
parent: 6cdf4cb5e83da3d89367cd2e2d8b1813b573146d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Jul 20 05:44:52 EDT 2021

ivf: write header and flush first, then proceed with reading images

--- a/hj264.c
+++ b/hj264.c
@@ -22,6 +22,8 @@
 
 	Align = 64,
 	Maxquality = 10,
+
+	TimedenumIVF = 1000ULL,
 };
 
 typedef struct Hjob Hjob;
@@ -45,7 +47,6 @@
 	H264E_scratch_t *scratch;
 	H264E_run_param_t rp;
 	H264E_io_yuv_t ioyuv;
-	int w, h;
 	YUV yuv;
 	int fmt;
 	Biobuf out;
@@ -192,8 +193,6 @@
 	h->yuv.v = h->ioyuv.yuv[2]; h->yuv.vs = h->ioyuv.stride[2];
 	h->persist = align(p+szyuv, Align);
 	h->scratch = align(h->persist+szpersist, Align);
-	h->w = ww;
-	h->h = hh;
 
 	cp.token = h;
 	cp.run_func_in_thread = hjobsrun;
@@ -231,7 +230,7 @@
 static void
 encthread(void *p)
 {
-	u8int *data, v[20];
+	u8int *data, v[12];
 	uvlong ts;
 	Img *img;
 	Hj264 *h;
@@ -238,27 +237,6 @@
 	int sz;
 
 	h = p;
-	if(h->fmt == FmtIVF){
-		enum{
-			Timedenum = 1000ULL,
-		};
-		Bwrite(&h->out, "DKIF\x00\x00\x20\x00AVC1", 12);
-		v[0] = h->w;
-		v[1] = h->w >> 8;
-		v[2] = h->h;
-		v[3] = h->h >> 8;
-		v[4] = Timedenum;
-		v[5] = Timedenum >> 8;
-		v[6] = Timedenum >> 16;
-		v[7] = Timedenum >> 24;
-		v[8] = 1;
-		v[9] = 0;
-		v[10] = 0;
-		v[11] = 0;
-		memset(v+12, 0, 8); /* unknown duration */
-		Bwrite(&h->out, v, sizeof(v));
-	}
-
 	for(;;){
 		if((img = recvp(h->frame)) == nil)
 			break;
@@ -351,6 +329,7 @@
 	char *s, tmp[61], *f[5];
 	uvlong fstart, fend;
 	int ww, hh, in, fmt;
+	u8int v[20];
 	Img *img;
 	Hj264 *h;
 
@@ -444,6 +423,25 @@
 	*procdata() = h;
 	threadnotify(done, 1);
 	proccreate(encthread, h, mainstacksize);
+
+	if(h->fmt == FmtIVF){
+		Bwrite(&h->out, "DKIF\x00\x00\x20\x00AVC1", 12);
+		v[0] = ww;
+		v[1] = ww >> 8;
+		v[2] = hh;
+		v[3] = hh >> 8;
+		v[4] = TimedenumIVF;
+		v[5] = TimedenumIVF >> 8;
+		v[6] = TimedenumIVF >> 16;
+		v[7] = TimedenumIVF >> 24;
+		v[8] = 1;
+		v[9] = 0;
+		v[10] = 0;
+		v[11] = 0;
+		memset(v+12, 0, 8); /* unknown duration */
+		Bwrite(&h->out, v, sizeof(v));
+		Bflush(&h->out);
+	}
 
 	tstart = npe_nanosec();
 	for(nframes = 0;; nframes++){