shithub: mcfs

Download patch

ref: 07e6c91d49477e1aa398a6738629679477c42f06
parent: f6a0362e0845888acf03cac6a6fd8e98ffae42d2
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Jan 20 09:23:47 EST 2021

matroska: add support for header stripping with no lacing

--- a/matroska.c
+++ b/matroska.c
@@ -183,10 +183,10 @@
 int
 matroskarun(Biobuf *f)
 {
-	int isebml, npackets, i, skipdata;
-	vlong left, n, sz, bufsz, track, off, packetsz, x, endtracks;
+	int isebml, npackets, i, skipdata, lacing;
+	vlong left, n, sz, nsz, bufsz, track, off, packetsz, x, endtracks;
 	uvlong ts, timestamp, timestampscale;
-	uchar *buf;
+	uchar *buf, *b;
 	double duration;
 	Ebml e, te;
 	Elspec el;
@@ -200,6 +200,7 @@
 	te.tracknum = -1;
 	timestampscale = 1000000;
 	e.tracknum = -1;
+	e.comp.algo = -1;
 	duration = 0;
 	ts = 0;
 	skipdata = trackdump == Nodump;
@@ -257,10 +258,13 @@
 			if(e.tracknum == trackdump)
 				memmove(&te, &e, sizeof(e));
 			memset(&e, 0, sizeof(e));
+			e.comp.algo = -1;
 			e.timestampscale = timestampscale;
 			continue;
 		}else if(el.id == EVideo || el.id == EAudio){
 			continue;
+		}else if(el.id == EContentEncodings || el.id == EContentEncoding || el.id == EContentCompression){
+			continue;
 		}else if(el.id == EBlockGroup && !skipdata){
 			continue;
 		}else if((el.id == ESimpleBlock || el.id == EBlock) && !skipdata){
@@ -293,10 +297,34 @@
 				}
 				left -= sz;
 				sz -= 3;
+				lacing = (buf[2] >> 1) & 3;
 				npackets = buf[3]+1;
-				switch((buf[2] >> 1) & 3){ /* lacing */
+
+				if(te.comp.algo == 3 && lacing == 0){ /* header stripping, need to put bytes back */
+					if(te.comp.sz == 0)
+						te.comp.algo = -1;
+					else{
+						nsz = sz + npackets*te.comp.sz;
+						if(bufsz < nsz){
+							buf = realloc(buf, nsz);
+							bufsz = nsz;
+						}
+					}
+				}else if(te.comp.algo >= 0){
+					werrstr("unsupported: comp algo %d, lacing %d, please report a bug", te.comp.algo, lacing);
+					goto err;
+				}
+
+				b = buf+3;
+				switch(lacing){ /* lacing */
 				case 0: /* none */
-					packets[0].data = buf+3;
+					if(te.comp.algo == 3){
+						b = buf;
+						memmove(b+te.comp.sz, b+3, sz);
+						memmove(b, te.comp.data, te.comp.sz);
+						sz += te.comp.sz;
+					}
+					packets[0].data = b;
 					packets[0].sz = sz;
 					npackets = 1;
 					break;
@@ -385,6 +413,10 @@
 				getbytes(ECodecPrivate, e.codec.priv)
 			else
 				getnumber(ECodecDelay, e.codec.delay)
+			else
+				getnumber(EContentCompAlgo, e.comp.algo)
+			else
+				getbytes(EContentCompSettings, e.comp)
 			else
 				getnumber(EPixelWidth, e.video.width)
 			else
--- a/packet.h
+++ b/packet.h
@@ -22,6 +22,11 @@
 			int sz;
 		}priv;
 	}codec;
+	struct {
+		uchar *data;
+		int sz;
+		int algo;
+	}comp;
 	u32int trackuid;
 	u32int fmt;