shithub: aacenc

Download patch

ref: e52ee3dad9644d9c7a07f55d4d7e3bdc68894a5a
parent: 223d5ee4d92c52b61863739dd3bf633806643f81
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Aug 11 12:24:30 EDT 2021

remove PES logic; flush on a note; write DSE with initial nsec value to help with syncing to video

--- a/frontend/aacenc.c
+++ b/frontend/aacenc.c
@@ -24,51 +24,12 @@
 #include <bio.h>
 #include <faac.h>
 
-enum {
-	Taudio = 0xc0,
-	Tvideo = 0xe0,
-};
+static Biobuf out;
 
 static int
-peswrite(Biobuf *o, uvlong ns, int type, u8int *data, int sz)
+done(void*, char*)
 {
-	u8int p[] = {
-		0x00, 0x00, 0x01, /* start code */
-		type, /* stream id 0 */
-		0, 0, /* packet length */
-		0x00, /* a bunch of nothing */
-		0x80, /* PTS included */
-		5, /* PTS is 5 bytes */
-		0, 0, 0, 0, 0, /* PTS value */
-		0, 0, 0, 0, 0, /* DTS value */
-	};
-	int plen, psz, dsz;
-	uvlong pts;
-
-	pts = ns / 99999ULL;
-	p[9] = 1<<5 | (pts>>30)<<1 | 1;
-	p[10] = pts>>22;
-	p[11] = pts>>14 | 1;
-	p[12] = pts>>7;
-	p[13] = pts<<1 | 1;
-	psz = sizeof(p)-5;
-	if((type & 0xf0) == Tvideo){
-		psz += 5;
-		p[7] |= 0x40; /* DTS is included */
-		p[8] += 5;
-		memmove(p+14, p+9, 5);
-	}
-
-	for(; sz > 0; sz -= dsz, data += dsz){
-		dsz = 0xffff - (psz-6); /* maximum */
-		dsz = sz <= dsz ? sz : dsz;
-		plen = psz-6 + dsz;
-		p[4] = plen >> 8;
-		p[5] = plen;
-		if(Bwrite(o, p, psz) < 0 || Bwrite(o, data, dsz) < 0)
-			return -1;
-	}
-
+	Bflush(&out);
 	return 0;
 }
 
@@ -82,15 +43,14 @@
 void
 main(int argc, char **argv)
 {
-	int nch, srate, type, brate, sz, n, r, q, pes, frsz;
+	int nch, srate, type, brate, sz, n, r, q, i;
 	ulong insamples, outsz, insz;
 	faacEncConfigurationPtr fmt;
-	uvlong mul, div, nfr;
+	u8int *obuf, ph[7+1+4+8];
 	faacEncHandle e;
 	s16int *pcm;
-	u8int *obuf;
-	Biobuf out;
 	Biobuf in;
+	uvlong ns;
 	char *s;
 
 	brate = 0;
@@ -97,7 +57,6 @@
 	srate = 44100;
 	nch = 2;
 	type = LOW;
-	pes = 0;
 	q = 0;
 	ARGBEGIN{
 	case 'B':
@@ -112,9 +71,6 @@
 		if((srate = atoi(EARGF(usage()))) < 1)
 			sysfatal("invalid samplerate %d", srate);
 		break;
-	case 'p':
-		pes = 1;
-		break;
 	case 'q':
 		if((q = atoi(EARGF(usage()))) < 1)
 			sysfatal("invalid quantization quality %d", q);
@@ -139,14 +95,6 @@
 	if(Binit(&in, 0, OREAD) != 0 || Binit(&out, 1, OWRITE) != 0)
 		sysfatal("io init failed");
 
-	for(mul = 1000000000ULL, div = srate;;){
-		if((mul % 10) == 0 && (div % 10) == 0){
-			mul /= 10;
-			div /= 10;
-		}else
-			break;
-	}
-
 	setfcr(getfcr() & ~(FPINVAL|FPOVFL));
 
 	if((e = faacEncOpen(srate, nch, &insamples, &outsz)) == nil)
@@ -169,8 +117,10 @@
 	if(!faacEncSetConfiguration(e, fmt))
 		sysfatal("invalid encoder configuration");
 
-	frsz = insamples / nch;
-	for(nfr = 0;;){
+	atnotify(done, 1);
+
+	ns = nsec();
+	for(;;){
 		for(n = 0; n == 0 || (n & (sizeof(*pcm)-1)) != 0; n += r){
 			if((r = Bread(&in, pcm+n, insz-n)) <= 0)
 				break;
@@ -179,12 +129,23 @@
 			break;
 		if((sz = faacEncEncode(e, pcm, n/sizeof(*pcm), obuf, outsz)) < 0)
 			sysfatal("faacEncEncode");
-		if(pes){
-			if(peswrite(&out, nfr*mul/div, Taudio, obuf, sz) < 0)
-				break;
-			n = n / sizeof(*pcm) / nch;
-			nfr += n < frsz ? frsz : 0;
-		}else if(Bwrite(&out, obuf, sz) < 0)
+		if(ns != 0){
+			memmove(ph, obuf, 7);
+			/* set frame size */
+			ph[3] &= ~3;
+			ph[4] = sizeof(ph)>>3;
+			ph[5] = (ph[5]&~0xe0) | sizeof(ph)<<4;
+			ph[7] = 0x04; /* DSE */
+			ph[8] = 'n';
+			ph[9] = 's';
+			ph[10] = 'e';
+			ph[11] = 'c';
+			for(i = 0; i < 8; i++, ns >>= 8)
+				ph[12+i] = ns;
+			ns = 0;
+			Bwrite(&out, ph, sizeof(ph));
+		}
+		if(Bwrite(&out, obuf, sz) < 0)
 			break;
 	}
 	Bflush(&out);