~ft/mcfs

ref: 49dc43fd64bb77001494be6721380895f1bba971 mcfs/extra/mkvdump.c -rw-r--r-- 3.3 KiB
49dc43fd — Sigrid Solveig Haflínudóttir matroska: handle av1 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "../ebml.c"

#define min(a,b) ((a)<=(b)?(a):(b))

static vlong stack[128];

static void
usage(void)
{
	fprint(2, "usage: %s [-d] [-h hxsz] [file.mkv]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	int n, sti, i, hxsz, lacing, fd, debug;
	vlong sz, off, x;
	uchar *b, t[3];
	Biobuf in, out;
	Elspec s;
	double d;
	Tm tm;

	debug = 0;
	hxsz = 32;
	ARGBEGIN{
	case 'd':
		debug++;
		break;
	case 'h':
		if((hxsz = atoi(EARGF(usage()))) > 0)
			break;
	default:
		usage();
	}ARGEND

	fd = 0;
	if(argc == 1){
		if((fd = open(*argv, OREAD)) < 0)
			sysfatal("%r");
	}else if(argc != 0)
		usage();

	if((b = malloc(hxsz)) == nil)
		sysfatal("memory");

	quotefmtinstall();
	fmtinstall('H', encodefmt);
	tmfmtinstall();
	Binit(&in, fd, OREAD);
	Binit(&out, 1, OWRITE);

	sti = 0;
	for(;;){
		off = Boffset(&in);
		if((n = ebmlel(&in, 0x7fffffffffffffffLL, &s, &sz)) < 0){
			werrstr("invalid ebml: %r at %#llx (size %lld)", off, sz);
			goto err;
		}
		if(s.type < 0)
			continue;
		if(n == 0) /* eof */
			break;
		while(sti > 0 && off >= stack[sti-1])
			sti--;
		if(s.name){
			for(i = 0; i < sti; i++)
				Bputc(&out, '\t');
			Bprint(&out, "%s ", s.name);
			if(debug)
				Bprint(&out, " (id %#llx) ", s.id);
			if(s.type == Emaster && sti < nelem(stack))
				stack[sti++] = off+n+sz;
		}else{
			Bprint(&out, "%#llx ", s.id);
		}
		if(debug)
			Bprint(&out, "(%lld bytes) ", sz);

		switch(s.type){
		case Ebinary:
			if(s.id == ESimpleBlock || s.id == EBlock){
				if((n = ebmluint(&in, sz, &x)) < 0){
					werrstr("block: %r");
					goto err;
				}
				sz -= n;
				if(Bread(&in, t, 3) != 3){
					werrstr("block: %r");
					goto err;
				}
				sz -= 3;
				lacing = (t[2] >> 1) & 3;
				Bprint(&out, "(track %lld, timecode %d) ", x, (s16int)(t[0]<<8 | t[1]));
				if(s.id == ESimpleBlock){
					if(t[2] & 0x80)
						Bprint(&out, "(key) ");
					if(t[2] & 0x01)
						Bprint(&out, "(discard) ");
				}
				if(t[2] & 0x08)
					Bprint(&out, "(hidden) ");
				if(lacing != 0)
					Bprint(&out, "(lacing %d) ", lacing);
			}
			n = min(hxsz, sz);
			if(Bread(&in, b, n) != n){
				werrstr("binary: %r");
				goto err;
			}
			Bprint(&out, "[%.*H%s] (%lld bytes)", n, b, sz > n ? "..." : "", sz);
			sz -= n;
			break;
		case Efloat:
			if(ebmlfloat(&in, sz, &d) < 0)
				goto err;
			Bprint(&out, "%g", d);
			sz = 0;
			break;
		case Eunsigned:
			if(ebmlrawuint(&in, sz, &x) < 0)
				goto err;
			Bprint(&out, "%llud", (uvlong)x);
			sz = 0;
			break;
		case Esigned:
			if(ebmlrawsint(&in, sz, &x) < 0)
				goto err;
			Bprint(&out, "%lld", x);
			sz = 0;
			break;
		case Eunicode:
		case Eascii:
			n = min(hxsz, sz);
			if(Bread(&in, b, n) != n){
				werrstr("string: %r");
				goto err;
			}
			Bprint(&out, "%#.*q%s", n, (char*)b, sz > n ? "..." : "");
			sz -= n;
			break;
		case Etimestamp:
			if(ebmlrawsint(&in, sz, &x) < 0)
				goto err;
			x /= 1000000000LL;
			x += 978307200LL;
			Bprint(&out, "%τ", tmfmt(tmtime(&tm, x, nil), nil));
			sz = 0;
			break;
		case Emaster:
			break;
		default:
			Bprint(&out, "???");
		}

		if(s.id == ETrackType)
			Bprint(&out, " (%s)", ebmltracktype(x));

		if(Bputc(&out, '\n') < 0)
			break;
		if(s.type != Emaster && sz > 0 && Bseek(&in, sz, 1) < 0)
			goto err;
			
	}
	Bterm(&out);
	Bterm(&in);

	exits(nil);
err:
	fprint(2, "%s: %r\n", fd == 0 ? "stdin" : *argv);
	exits("error");
}