shithub: mcfs

Download patch

ref: c32a2aea3250224b15170c1fd3653ab43d127b0c
parent: 523b8ff86604d789c92481c9588d72c91d7ef9bb
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Mar 1 12:19:18 EST 2021

matroska: delay Block packets flushing until BlockGroup is parsed

--- a/matroska.c
+++ b/matroska.c
@@ -194,12 +194,6 @@
 	Bprint(o, "\n");
 }
 
-static void
-flush(Ebml *e, int refblock)
-{
-	USED(e); USED(refblock);
-}
-
 int
 matroskarun(Biobuf *f)
 {
@@ -227,6 +221,9 @@
 	el.id = 0;
 	refblock = 0;
 	bgend = 0;
+	npackets = 0;
+	ts = 0;
+	key = 0;
 	for(isebml = 0; left != 0;){
 		if(el.id == EBlockDuration)
 			te.blockdur *= timestampscale;
@@ -237,6 +234,16 @@
 		}
 
 		off = Boffset(f);
+
+		if(off >= bgend && !skipdata){
+			if(npackets > 0){
+				if(te.fpacket(&out, &te, packets, npackets, ts, key || refblock) != 0)
+					goto err;
+				npackets = 0;
+			}
+			refblock = 0;
+		}
+
 		n = ebmlel(f, left, &el, &sz);
 		if(n < 0){
 			werrstr("invalid ebml: %r at %#llx (size %lld)", off, sz);
@@ -249,10 +256,6 @@
 			break;
 		}
 		left -= n;
-		if(off >= bgend && !skipdata){
-			flush(&te, refblock);
-			refblock = 0;
-		}
 
 		if(el.id == EEBML){ /* EBML comes first */
 			if(isebml != 0){
@@ -303,6 +306,7 @@
 		}else if(el.id == EBlockGroup && !skipdata){
 			refblock = 0;
 			bgend = off+sz;
+			npackets = 0;
 			continue;
 		}else if((el.id == ESimpleBlock || el.id == EBlock) && !skipdata){
 			if(te.tracknum == -1)
@@ -437,8 +441,11 @@
 
 				/* ns timestamp */
 				ts = (timestamp + timecode) * timestampscale - te.codec.delay;
-				if(te.fpacket(&out, &te, packets, npackets, ts, key) != 0)
-					goto err;
+				if(el.id != EBlock){
+					if(te.fpacket(&out, &te, packets, npackets, ts, key) != 0)
+						goto err;
+					npackets = 0;
+				}
 				continue;
 			}
 		}else