shithub: mcfs

Download patch

ref: 72ed7dd00d9460a3711822cafaad3d0a87254db9
parent: 5dc44497e08df77ed4e7a12981a6a527bf70424e
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Dec 14 06:33:33 EST 2020

ebml: support for "-t audio" and "-t video" options

--- a/ebml.c
+++ b/ebml.c
@@ -4,6 +4,18 @@
 #include <ctype.h>
 #include "common.h"
 
+enum
+{
+	Trackvideo = 1,
+	Trackaudio = 2,
+	Trackcomplex = 3,
+	Tracklogo = 16,
+	Tracksubtitles = 17,
+	Trackbuttons = 18,
+	Trackcontrol = 32,
+	Trackmetadata = 33,
+};
+
 static Packet packets[256];
 
 int
@@ -136,13 +148,13 @@
 	char *s;
 	int n;
 
-	if(e->tracktype == 17){
+	if(e->tracktype == Tracksubtitles){
 		if(strcmp(e->codec.name, "S_TEXT/UTF8") == 0)
 			return "srt";
-	}else if(e->tracktype == 2){
+	}else if(e->tracktype == Trackaudio){
 		if(strcmp(e->codec.name, "A_MPEG/L3") == 0)
 			return "mp3";
-	}else if(e->tracktype == 1){
+	}else if(e->tracktype == Trackvideo){
 		if(strcmp(e->codec.name, "V_MPEG4/ISO/AVC") == 0)
 			return "h264";
 	}
@@ -164,7 +176,7 @@
 	e->duration = duration;
 	e->duration *= e->timestampscale;
 	c = format(e);
-	if(e->tracktype == 1){
+	if(e->tracktype == Trackvideo){
 		if(strcmp(c, "vp9") == 0)
 			e->fmt = FmtVp09;
 		else if(strcmp(c, "vp8") == 0)
@@ -175,7 +187,7 @@
 			goto err;
 		e->fpacket = ivfpacket;
 		return 0;
-	}else if(e->tracktype == 2){
+	}else if(e->tracktype == Trackaudio){
 		if(strcmp(c, "vorbis") == 0){
 			e->fmt = FmtVorbis;
 			e->fpacket = oggpacket;
@@ -191,7 +203,7 @@
 			e->fpacket = mp3packet;
 			return 0;
 		}
-	}else if(e->tracktype == 17){
+	}else if(e->tracktype == Tracksubtitles){
 		if(strcmp(c, "srt") == 0){
 			e->fmt = FmtSrt;
 			e->fpacket = srtpacket;
@@ -209,14 +221,14 @@
 tracktype(Ebml *e)
 {
 	static char *types[] = {
-		[1] = "video",
-		[2] = "audio",
-		[3] = "complex",
-		[16] = "logo",
-		[17] = "subtitle",
-		[18] = "buttons",
-		[32] = "control",
-		[33] = "metadata",
+		[Trackvideo] = "video",
+		[Trackaudio] = "audio",
+		[Trackcomplex] = "complex",
+		[Tracklogo] = "logo",
+		[Tracksubtitles] = "subtitle",
+		[Trackbuttons] = "buttons",
+		[Trackcontrol] = "control",
+		[Trackmetadata] = "metadata",
 	};
 	char *t;
 
@@ -239,10 +251,10 @@
 		Bprint(o, "codec name: %s\n", e->codec.name);
 		Bprint(o, "codec priv data: %d bytes [%.*H]\n", e->codec.priv.sz, e->codec.priv.sz, e->codec.priv.data);
 		Bprint(o, "codec delay: %zdns\n", e->codec.delay);
-		if(e->tracktype == 1){
+		if(e->tracktype == Trackvideo){
 			Bprint(o, "video: %dx%d [%d,%d,%d,%d]\n", e->video.width, e->video.height, e->video.crop.left, e->video.crop.top, e->video.crop.right, e->video.crop.bottom);
 			Bprint(o, "display: %dx%d unit=%d aspectmode=%d\n", e->video.display.width, e->video.display.height, e->video.display.unit, e->video.display.aspectmode);
-		}else if(e->tracktype == 2){
+		}else if(e->tracktype == Trackaudio){
 			Bprint(o, "audio: samplerate=%g, channels=%d, bps=%d\n", e->audio.samplerate, e->audio.channels, e->audio.bps);
 		}
 		Bprint(o, "seek pre-roll: %zd\n", e->seekpreroll);
@@ -323,8 +335,15 @@
 			if(trackdump != Nodump) /* skip it entirely if no dump required */
 				continue;
 		}else if(id == 0xae){ /* track entry */
-			if(e.tracknum > 0 && trackdump == Nodump)
-				trackinfo(&out, &e);
+			fprint(2, "track %d | type %d | want %d\n", e.tracknum, e.tracktype, trackdump);
+			if(e.tracknum > 0){
+				if(trackdump == Nodump)
+					trackinfo(&out, &e);
+				else if(trackdump == Dumpvideo && e.tracktype == Trackvideo)
+					trackdump = e.tracknum;
+				else if(trackdump == Dumpaudio && e.tracktype == Trackaudio)
+					trackdump = e.tracknum;
+			}
 			if(e.tracknum == trackdump)
 				memmove(&te, &e, sizeof(e));
 			memset(&e, 0, sizeof(e));
@@ -343,6 +362,10 @@
 			}
 			left -= n;
 			sz -= n;
+			if(trackdump == Dumpvideo && e.tracktype == Trackvideo)
+				trackdump = te.tracknum;
+			else if(trackdump == Dumpaudio && e.tracktype == Trackaudio)
+				trackdump = te.tracknum;
 			if(track == trackdump && track == te.tracknum){
 				if(te.fpacket == nil && initctx(&te, duration) != 0){
 					werrstr("packet: %r");