shithub: hj264

Download patch

ref: 5eebf86ca323c5b9704153070e2c790e9e3a0d19
parent: 0cfccfb6710059f9131240d4a836e6aa8a62023b
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jul 14 07:04:56 EDT 2021

capture 10 seconds; reuse input buffer; produce human readable error reports

--- a/hj264.c
+++ b/hj264.c
@@ -108,6 +108,31 @@
 	}
 }
 
+#pragma varargck type "ℏ" int
+static int
+hjerror(Fmt *f)
+{
+	char *s;
+	int e;
+
+	s = nil;
+	e = va_arg(f->args, int);
+	switch(e){
+	case H264E_STATUS_SUCCESS: s = "success"; break;
+	case H264E_STATUS_BAD_ARGUMENT: s = "bad argument"; break;
+	case H264E_STATUS_BAD_PARAMETER: s = "bad parameter"; break;
+	case H264E_STATUS_BAD_FRAME_TYPE: s = "bad frame type"; break;
+	case H264E_STATUS_SIZE_NOT_MULTIPLE_16: s = "size not multiple of 16"; break;
+	case H264E_STATUS_SIZE_NOT_MULTIPLE_2: s = "size not multiple of 2"; break;
+	case H264E_STATUS_BAD_LUMA_ALIGN: s = "bad luma alignment"; break;
+	case H264E_STATUS_BAD_LUMA_STRIDE: s = "bad luma stride"; break;
+	case H264E_STATUS_BAD_CHROMA_ALIGN: s = "bad chroma alignment"; break;
+	case H264E_STATUS_BAD_CHROMA_STRIDE: s = "bad chroma stride"; break;
+	}
+
+	return s == nil ? fmtprint(f, "error %d", e) : fmtprint(f, "%s", s);
+}
+
 static void
 threadf(void *p)
 {
@@ -156,7 +181,7 @@
 	int e;
 
 	if((e = H264E_encode(h->persist, h->scratch, &h->rp, &h->yuv, data, sz)) != 0){
-		werrstr("H264E_encode: error %d", e);
+		werrstr("H264E_encode: %ℏ", e);
 		return -1;
 	}
 
@@ -173,12 +198,13 @@
 	Hj264 *h;
 
 	nthreads = clp(nthreads, 1, H264E_MAX_THREADS);
+	/* YUV logic requires alignment, allow height to be different (pad it) */
+	hh = ((hh-1) | 15) + 1;
 
 	memset(&cp, 0, sizeof(cp));
 	cp.num_layers = 1;
 	cp.gop = Gop;
 	cp.max_threads = nthreads;
-	cp.const_input_flag = 1;
 	cp.temporal_denoise_flag = denoise;
 	cp.vbv_size_bytes = kbps/1000*8/2; /* 2 seconds */
 	cp.width = ww;
@@ -185,13 +211,14 @@
 	cp.height = hh;
 
 	if((e = H264E_sizeof(&cp, &szpersist, &szscratch)) != 0){
-		werrstr("H264E_sizeof: error %d", e);
+		werrstr("H264E_sizeof: %ℏ", e);
 		return nil;
 	}
 
-	/* YUV logic requires alignment */
+	/* FIXME not padding width yet, so it still has to be multiple of 16 */
+	/* once we do that, put this line to where "hh" is aligned */
 	ww = ((ww-1) | 15) + 1;
-	hh = ((hh-1) | 15) + 1;
+
 	szyuv = ww*hh*3/2;
 	if((h = calloc(1, sizeof(*h) + Align+szyuv + Align+szpersist + Align+szscratch)) == nil)
 		return nil;
@@ -209,7 +236,7 @@
 	cp.token = h;
 	cp.run_func_in_thread = hjobsrun;
 	if((e = H264E_init(h->persist, &cp)) != 0){
-		werrstr("H264E_init: error %d", e);
+		werrstr("H264E_init: %ℏ", e);
 		return nil;
 	}
 
@@ -374,6 +401,8 @@
 	if((in = open(*argv, OREAD)) < 0)
 		sysfatal("input: %r");
 
+	fmtinstall(L'ℏ', hjerror);
+
 	memimageinit();
 	if((im = readmemimage(in)) == nil)
 		sysfatal("image: %r");
@@ -410,7 +439,7 @@
 			npe_nsleep(1000000000ULL/fps - (fend - fstart));
 
 		/* FIXME make a graceful shutdown on a note */
-		if(nanosec() - start > 4000000000ULL)
+		if(nanosec() - start > 10000000000ULL)
 			break;
 	}
 	end = nanosec();