~ft/mcfs

ref: 1cd6fa1aa2cd7eb7fc2beebec6ae51eb25450971 mcfs/iso.c -rw-r--r-- 20.2 KiB
1cd6fa1aSigrid Haflínudóttir extract raw streams 7 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#include <u.h>
#include <libc.h>
#include <bio.h>

typedef struct Box Box;
typedef struct RunSample RunSample;
typedef struct Track Track;
typedef struct SampleToChunk SampleToChunk;

struct Box {
	vlong dsz;
	vlong dstart;
	char extended[16];
	u32int type;

	/* full box */
	u8int version;
	u32int flags;

	union {
		struct {
			u32int brand;
			u32int version;
			u32int *compat;
			int ncompat;
		}ftyp;

		struct {
			u64int creation;
			u64int modification;
			u64int duration;
			u32int timescale;
			u32int rate;
			u32int matrix[9];
			u32int nexttrack;
			u16int volume;
		}mvhd;

		struct {
			u32int trackid;
			struct {
				u32int descrindex;
				u32int duration;
				u32int size;
				u32int flags;
			}defsample;
		}trex;

		struct {
			u32int seqnumber;
		}mfhd;

		struct {
			u64int decodetime;
		}tfdt;

		struct {
			u32int trackid;
			u64int baseoffset;
			struct {
				u32int descrindex;
				u32int duration;
				u32int size;
				u32int flags;
			}defsample;
		}tfhd;

		struct {
			u32int samplecount;
			s32int dataoffset;
			u32int firstsampleflags;
			RunSample *samples;
		}trun;

		struct {
			u32int handlertype;
			u32int entrycount;
			/* FIXME entries? */
		}stsd;

		struct {
			u32int entrycount;
			/* FIXME entries */
		}stts;

		struct {
			u32int entrycount;
			/* FIXME entries */
		}stss;

		struct {
			u32int entrycount;
			SampleToChunk *entry;
		}stsc;

		struct {
			u32int entrycount;
			u64int *chunkoffset;
		}stco_co64;

		struct {
			u32int samplesize;
			u32int samplecount;
			u32int *entrysize;
		}stsz; /* FIXME need stz2 as well */

		struct {
			u64int creattime;
			u64int modtime;
			u32int trackid;
			u64int duration;
			u32int width;
			u32int height;
		}tkhd;

		struct {
			u32int handlertype;
			char *name;
		}hdlr;
	};
};

struct RunSample {
	u32int duration;
	u32int size;
	u32int flags;
	vlong timeoffset;
};

struct Track {
	u64int *chunkoffset;
	u32int numchunks;

	u32int *samplesize;
	u32int numsamples;

	SampleToChunk *stc;
	u32int numstc;

	int id;
};

struct SampleToChunk {
	u32int firstchunk;
	u32int samplesperchunk;
	u32int sdt;
};

enum {
	BoxUuid = 0x75756964u,
	BoxFtyp = 0x66747970u,
	BoxMoov = 0x6d6f6f76u,
		BoxMvhd = 0x6d766864u,
		BoxMvex = 0x6d766578u,
			BoxTrex = 0x74726578u,
		BoxTrak = 0x7472616bu,
			BoxEdts = 0x65647473u,
				BoxElst = 0x656c7374u,
			BoxTkhd = 0x746b6864u,
			BoxMdia = 0x6d646961u,
				BoxMdhd = 0x6d646864u,
				BoxHdlr = 0x68646c72u,
				BoxMinf = 0x6d696e66u,
					BoxDinf = 0x64696e66u,
					BoxStbl = 0x7374626cu,
						BoxCtts = 0x63747473u,
					BoxStsd = 0x73747364u,
					BoxStts = 0x73747473u,
					BoxStsc = 0x73747363u,
					BoxStco = 0x7374636fu,
					BoxCo64 = 0x636f3634u,
					BoxStsz = 0x7374737au,
					BoxStss = 0x73747373u,
					BoxVmhd = 0x766d6864u,
	BoxSidx = 0x73696478u,
	BoxMoof = 0x6d6f6f66u,
		BoxMfhd = 0x6d666864u,
		BoxTraf = 0x74726166u,
			BoxTfdt = 0x74666474u,
			BoxTfhd = 0x74666864u,
			BoxTrun = 0x7472756eu,
	BoxMdat = 0x6d646174u,
};

#define bu16(x) ((x)[0]<<8 | (x)[1]<<16)
#define bu32(x) ((x)[0]<<24 | (x)[1]<<16 | (x)[2]<<8 | (x)[3])
#define bu64(x) ((u64int)(x)[0]<<56 | (u64int)(x)[1]<<48 | (u64int)(x)[2]<<40 | (u64int)(x)[3]<<32 | (x)[4]<<24 | (x)[5]<<16 | (x)[6]<<8 | (x)[7])

#define isfullbox(b) ( \
	b->type == BoxMvhd || b->type == BoxTrex || b->type == BoxMdhd || b->type == BoxHdlr || \
	b->type == BoxMfhd || b->type == BoxTfhd || b->type == BoxTfdt || b->type == BoxTrun || \
	b->type == BoxStsd || b->type == BoxStts || b->type == BoxStss || b->type == BoxTkhd || \
	b->type == BoxElst || b->type == BoxStsc || b->type == BoxStco || b->type == BoxCo64 || \
	b->type == BoxStsz || b->type == BoxCtts \
)

#define eBread(sz, e) if(Bread(f, d, (sz)) != (sz)){ werrstr(e); goto err; }

static int dflag;
static int dind;
static int trackdump = -1;
static u32int defsamplesize;

static Track track;

static int parsebox(Biobuf *f, Box *b, int *eof);

#pragma varargck type "T" u32int
static int
typefmt(Fmt *f)
{
	char t[5];
	int x;

	x = va_arg(f->args, int);
	t[0] = x>>24;
	t[1] = x>>16;
	t[2] = x>>8;
	t[3] = x;
	t[4] = 0;

	return fmtstrcpy(f, t);
}

static void
printbox(Box *b)
{
	int i;
	char ind[16] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
	uvlong u;

	if(dflag == 0)
		return;

	fprint(2, "%.*s%T\n", dind, ind, b->type);

	/* full box */
	if(isfullbox(b)){
		fprint(2, "\t%.*sversion\t%d\n", dind, ind, b->version);
		fprint(2, "\t%.*sflags\t0x%ux\n", dind, ind, b->flags);
	}

	if(b->type == BoxFtyp){
		fprint(2, "\t%.*sbrand\t%T\n", dind, ind, b->ftyp.brand);
		fprint(2, "\t%.*sversion\t%d\n", dind, ind, b->ftyp.version);
		fprint(2, "\t%.*scompat", dind, ind);
		for(i = 0; i < b->ftyp.ncompat; i++)
			fprint(2, "\t%.*s%T", dind, ind, b->ftyp.compat[i]);
		fprint(2, "\n");
	}else if(b->type == BoxMvhd){
		fprint(2, "\t%.*screation\t%zd\n", dind, ind, b->mvhd.creation);
		fprint(2, "\t%.*smodification\t%zd\n", dind, ind, b->mvhd.modification);
		fprint(2, "\t%.*stimescale\t%ud\n", dind, ind, b->mvhd.timescale);
		fprint(2, "\t%.*sduration\t%zd\n", dind, ind, b->mvhd.duration);
		fprint(2, "\t%.*srate\t0x%ux\n", dind, ind, b->mvhd.rate);
		fprint(2, "\t%.*svolume\t0x%ux\n", dind, ind, b->mvhd.volume);
		fprint(2, "\t%.*snexttrack\t0x%ux\n", dind, ind, b->mvhd.nexttrack);
		fprint(2, "\t%.*smatrix\t0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux\n", dind, ind,
			b->mvhd.matrix[0],
			b->mvhd.matrix[1],
			b->mvhd.matrix[2],
			b->mvhd.matrix[3],
			b->mvhd.matrix[4],
			b->mvhd.matrix[5],
			b->mvhd.matrix[6],
			b->mvhd.matrix[7],
			b->mvhd.matrix[8]
		);
	}else if(b->type == BoxTrex){
		fprint(2, "\t%.*strackid\t0x%ux\n", dind, ind, b->trex.trackid);
		fprint(2, "\t%.*sdefsample.\n", dind, ind);
		fprint(2, "\t\t%.*s.descrindex\t0x%ux\n", dind, ind, b->trex.defsample.descrindex);
		fprint(2, "\t\t%.*s.duration\t%ud\n", dind, ind, b->trex.defsample.duration);
		fprint(2, "\t\t%.*s.size\t0x%ux\n", dind, ind, b->trex.defsample.size);
		fprint(2, "\t\t%.*s.flags\t0x%ux\n", dind, ind, b->trex.defsample.flags);
	}else if(b->type == BoxMfhd){
		fprint(2, "\t%.*sseqnumber\t%ud\n", dind, ind, b->mfhd.seqnumber);
	}else if(b->type == BoxTfhd){
		fprint(2, "\t%.*strackid\t0x%ux\n", dind, ind, b->tfhd.trackid);
		if(b->flags & 1)
			fprint(2, "\t%.*sbaseoffset\t%zd\n", dind, ind, b->tfhd.baseoffset);
		fprint(2, "\t%.*sdefsample.\n", dind, ind);
		if(b->flags & 2)
			fprint(2, "\t\t%.*s.descrindex\t0x%ux\n", dind, ind, b->tfhd.defsample.descrindex);
		if(b->flags & 8)
			fprint(2, "\t\t%.*s.duration\t%ud\n", dind, ind, b->tfhd.defsample.duration);
		if(b->flags & 16)
			fprint(2, "\t\t%.*s.size\t0x%ux\n", dind, ind, b->tfhd.defsample.size);
		if(b->flags & 32)
			fprint(2, "\t\t%.*s.flags\t0x%ux\n", dind, ind, b->tfhd.defsample.flags);
		if(b->flags & 0x10000)
			fprint(2, "\t%.*sduration is empty\n", dind, ind);
		if(b->flags & 0x20000)
			fprint(2, "\t%.*sdefault base is moof\n", dind, ind);
	}else if(b->type == BoxTfdt){
		fprint(2, "\t%.*sdecodetime\t%zd\n", dind, ind, b->tfdt.decodetime);
	}else if(b->type == BoxTrun){
		fprint(2, "\t%.*ssamplecount\t%ud\n", dind, ind, b->trun.samplecount);
		if(b->flags & 1)
			fprint(2, "\t%.*sdataoffset\t%d\n", dind, ind, b->trun.dataoffset);
		if(b->flags & 2)
			fprint(2, "\t%.*sfirstsampleflags\t0x%ux\n", dind, ind, b->trun.firstsampleflags);
		for(u = 0; u < b->trun.samplecount; u++){
			fprint(2, "\t%.*ssamples[%zd]\n", dind, ind, u);
			if(b->flags & 0x100)
				fprint(2, "\t\t%.*s.duration\t%ud\n", dind, ind, b->trun.samples[u].duration);
			if(b->flags & 0x200)
				fprint(2, "\t\t%.*s.size\t%ud\n", dind, ind, b->trun.samples[u].size);
			if(b->flags & 0x400)
				fprint(2, "\t\t%.*s.flags\t0x%ux\n", dind, ind, b->trun.samples[u].flags);
			if(b->flags & 0x800)
				fprint(2, "\t\t%.*s.timeoffset\t%zd\n", dind, ind, b->trun.samples[u].timeoffset);
		}
	}else if(b->type == BoxStsd){
		fprint(2, "\t%.*shandler_type\t%08x\n", dind, ind, b->stsd.handlertype);
		fprint(2, "\t%.*sentry_count\t%ud\n", dind, ind, b->stsd.entrycount);
	}else if(b->type == BoxStts){
		fprint(2, "\t%.*sentry_count\t%ud\n", dind, ind, b->stts.entrycount);
	}else if(b->type == BoxStss){
		fprint(2, "\t%.*sentry_count\t%ud\n", dind, ind, b->stss.entrycount);
	}else if(b->type == BoxStsc){
		fprint(2, "\t%.*sentry_count\t%ud\n", dind, ind, b->stss.entrycount);
		for(u = 0; u < b->stsc.entrycount; u++){
			fprint(2, "\t%.*sentry[%zd]\n", dind, ind, u);
			fprint(2, "\t\t%.*sfirst_chunk\t%ud\n", dind, ind, b->stsc.entry[u].firstchunk);
			fprint(2, "\t\t%.*ssamples_per_chunk\t%ud\n", dind, ind, b->stsc.entry[u].samplesperchunk);
			fprint(2, "\t\t%.*ssample_description_table\t%ud\n", dind, ind, b->stsc.entry[u].sdt);
		}
	}else if(b->type == BoxStsz){
		fprint(2, "\t%.*ssample_size\t%ud\n", dind, ind, b->stsz.samplesize);
		fprint(2, "\t%.*ssample_count\t%ud\n", dind, ind, b->stsz.samplecount);
		if(b->stsz.samplesize == 0){
			for(u = 0; u < b->stsz.samplecount; u++)
				fprint(2, "\t%.*sentrysize[%zd]\t%ud\n", dind, ind, u, b->stsz.entrysize[u]);
		}
	}else if(b->type == BoxTkhd){
		fprint(2, "\t%.*screation_time\t%zd\n", dind, ind, b->tkhd.creattime);
		fprint(2, "\t%.*smodification_timetime\t%zd\n", dind, ind, b->tkhd.modtime);
		fprint(2, "\t%.*strack_id\t%ud\n", dind, ind, b->tkhd.trackid);
		fprint(2, "\t%.*sduration\t%zd\n", dind, ind, b->tkhd.duration);
		fprint(2, "\t%.*swidth\t%ud\n", dind, ind, b->tkhd.width);
		fprint(2, "\t%.*sheight\t%ud\n", dind, ind, b->tkhd.height);
	}else if(b->type == BoxHdlr){
		fprint(2, "\t%.*shandler_type\t%c%c%c%c\n", dind, ind, b->hdlr.handlertype>>24, b->hdlr.handlertype>>16&0xff, b->hdlr.handlertype>>8&0xff, b->hdlr.handlertype&0xff);
		fprint(2, "\t%.*sname\t%s\n", dind, ind, b->hdlr.name);
	}else if(b->type == BoxStco || b->type == BoxCo64){
		fprint(2, "\t%.*sentry_count\t%ud\n", dind, ind, b->stss.entrycount);
		for(u = 0; u < b->stco_co64.entrycount; u++)
			fprint(2, "\t%.*schunkoffset[%zd]\t%zd\n", dind, ind, u, b->stco_co64.chunkoffset[u]);
	}else{
		fprint(2, "\t%.*sstart\t%zd\n", dind, ind, b->dstart);
		fprint(2, "\t%.*ssize\t%zd\n", dind, ind, b->dsz);
	}
}

static void
dumptrack(Biobuf *f)
{
	SampleToChunk *stc;
	u32int si, ch, lastch;
	u32int samplelast, sample, rawsz;
	vlong o;
	u8int *raw;
	Biobuf out;

	Binit(&out, 1, OWRITE);
	raw = nil;
	rawsz = 0;
	sample = samplelast = 0;
	stc = track.stc;
	for(si = 0; si < track.numstc; si++, stc++){
		lastch = si+1 < track.numstc ? stc[1].firstchunk : track.numchunks;
		for(ch = stc->firstchunk-1; ch < lastch && ch < track.numchunks; ch++){
			o = track.chunkoffset[ch];
			if(Bseek(f, o, 0) != o)
				sysfatal("chunk %ud: %r", ch);
			for(; sample < samplelast+stc->samplesperchunk && sample < track.numsamples; sample++){
				if(track.samplesize[sample] == 0)
					break;
				if(rawsz < track.samplesize[sample]){
					rawsz = track.samplesize[sample] * 2;
					raw = realloc(raw, rawsz);
				}
				if(Bread(f, raw, track.samplesize[sample]) != track.samplesize[sample])
					sysfatal("chunk %ud sample %ud size %ud: %r", ch, sample, track.samplesize[sample]);
				if(Bwrite(&out, raw, track.samplesize[sample]) != track.samplesize[sample])
					exits(nil);
			}
			samplelast = sample;
		}
	}
	fprint(2, "%ud samples\n", sample);
	Bterm(&out);
	free(raw);
};

static int
parseboxdata(Biobuf *f, Box *b)
{
	u8int d[128], *p;
	Box inner;
	int i, n, eof;
	s32int s32i;
	u64int u;

	if(b->type == BoxFtyp){
		eBread(8, "brand and version");
		b->ftyp.brand = bu32(d);
		b->ftyp.version = bu32(d+4);
		if(b->dsz % 4 != 0){
			werrstr("compatible_brands size");
			goto err;
		}
		b->ftyp.ncompat = (b->dsz - 8) / 4;
		b->ftyp.compat = calloc(b->ftyp.ncompat, 4);
		for(i = 0; i < b->ftyp.ncompat; i++){
			eBread(4, "compatible_brands");
			b->ftyp.compat[i] = bu32(d);
		}
		printbox(b);
	}else if(b->type == BoxMoov || b->type == BoxMvex || b->type == BoxTrak ||
		b->type == BoxMdia || b->type == BoxMinf || b->type == BoxStbl ||
		b->type == BoxMoof || b->type == BoxTraf || b->type == BoxEdts){
		printbox(b);
		dind++;
		for(;;){
			memset(&inner, 0, sizeof(inner));
			if(parsebox(f, &inner, &eof) != 0)
				goto err;
			Bseek(f, inner.dstart+inner.dsz, 0);
			if(inner.dstart+inner.dsz >= b->dstart+b->dsz)
				break;
		}
		if(b->type == BoxTrak){
			if(track.id == trackdump){
				u = Boffset(f);
				dumptrack(f);
				Bseek(f, u, 0);
			}
			free(track.chunkoffset);
			free(track.samplesize);
			free(track.stc);
			memset(&track, 0, sizeof(track));
		}
		dind--;
	}else if(b->type == BoxMvhd){
		n = b->version == 0 ? 96 : 108;
		eBread(n, "short read");
		p = d;

		b->mvhd.creation = bu32(p); p += 4;
		if(b->version == 1){
			b->mvhd.creation = b->mvhd.creation<<32 | bu32(p); p += 4;
		}

		b->mvhd.modification = bu32(p); p += 4;
		if(b->version == 1){
			b->mvhd.modification = b->mvhd.modification<<32 | bu32(p); p += 4;
		}

		b->mvhd.timescale = bu32(p); p += 4;

		b->mvhd.duration = bu32(p); p += 4;
		if(b->version == 1){
			b->mvhd.duration = b->mvhd.duration<<32 | bu32(p); p += 4;
		}

		b->mvhd.rate = bu32(p); p += 4;
		b->mvhd.volume = bu16(p); p += 2;
		p += 2;
		p += 4*2;
		for(i = 0; i < 9; i++){
			b->mvhd.matrix[i] = bu32(p); p += 4;
		}
		p += 4*6;
		b->mvhd.nexttrack = bu32(p);

		printbox(b);
	}else if(b->type == BoxTrex){
		eBread(20, "short read");
		b->trex.trackid = bu32(d);
		b->trex.defsample.descrindex = bu32(d+4);
		b->trex.defsample.duration = bu32(d+8);
		b->trex.defsample.size = bu32(d+12);
		b->trex.defsample.flags = bu32(d+16);
		defsamplesize = b->trex.defsample.size;
		printbox(b);
	}else if(b->type == BoxMfhd){
		eBread(4, "short read");
		b->mfhd.seqnumber = bu32(d);
		printbox(b);
	}else if(b->type == BoxTfhd){
		eBread(4, "track_id");
		b->tfhd.trackid = bu32(d);
		if(b->flags & 1){
			eBread(8, "base_data_offset");
			b->tfhd.baseoffset = bu64(d);
		}
		if(b->flags & 2){
			eBread(4, "sample_description_index");
			b->tfhd.defsample.descrindex = bu32(d);
		}
		if(b->flags & 8){
			eBread(4, "default_sample_duration");
			b->tfhd.defsample.duration = bu32(d);
		}
		if(b->flags & 16){
			eBread(4, "default_sample_size");
			b->tfhd.defsample.size = bu32(d);
			defsamplesize = b->tfhd.defsample.size;
		}
		if(b->flags & 32){
			eBread(4, "default_sample_flags");
			b->tfhd.defsample.flags = bu32(d);
		}
		printbox(b);
	}else if(b->type == BoxTfdt){
		if(b->version == 1){
			eBread(8, "base_media_decode_time");
			b->tfdt.decodetime = bu64(d);
		}else{
			eBread(4, "base_media_decode_time");
			b->tfdt.decodetime = bu32(d);
		}
		printbox(b);
	}else if(b->type == BoxTrun){
		eBread(4, "sample_count");
		b->trun.samplecount = bu32(d);
		if(b->flags & 1){
			eBread(4, "data_offset");
			b->trun.dataoffset = bu32(d);
		}
		if(b->flags & 4){
			eBread(4, "first_sample_flags");
			b->trun.firstsampleflags = bu32(d);
		}
		/* FIXME free those */
		b->trun.samples = calloc(b->trun.samplecount, sizeof(RunSample));
		for(u = 0; u < b->trun.samplecount; u++){
			if(b->flags & 0x100){
				eBread(4, "sample_duration");
				b->trun.samples[u].duration = bu32(d);
			}
			if(b->flags & 0x200){
				eBread(4, "sample_size");
				b->trun.samples[u].size = bu32(d);
			}
			if(b->flags & 0x400){
				eBread(4, "sample_flags");
				b->trun.samples[u].flags = bu32(d);
			}
			if(b->flags & 0x800){
				eBread(4, "sample_composition_time_offset");
				s32i = bu32(d);
				if(b->version == 0)
					b->trun.samples[u].timeoffset = bu32(d);
				else
					b->trun.samples[u].timeoffset = s32i;
			}
		}
		printbox(b);
	}else if(b->type == BoxStsd){
		eBread(4, "handler_type");
		b->stsd.handlertype = bu32(d);
		eBread(4, "entry_count");
		b->stsd.entrycount = bu32(d);
		/* FIXME not reading actual entries here */
		printbox(b);
	}else if(b->type == BoxStts){
		eBread(4, "entry_count");
		b->stts.entrycount = bu32(d);
		/* FIXME not reading actual entries here */
		printbox(b);
	}else if(b->type == BoxStss){
		eBread(4, "entry_count");
		b->stss.entrycount = bu32(d);
		/* FIXME not reading actual entries here */
		printbox(b);
	}else if(b->type == BoxStsc){
		eBread(4, "entry_count");
		b->stsc.entrycount = bu32(d);
		b->stsc.entry = calloc(b->stsc.entrycount, sizeof(SampleToChunk));
		for(u = 0; u < b->stsc.entrycount; u++){
			eBread(4, "first_chunk");
			b->stsc.entry[u].firstchunk = bu32(d);
			eBread(4, "samples_per_chunk");
			b->stsc.entry[u].samplesperchunk = bu32(d);
			eBread(4, "sample_description_table");
			b->stsc.entry[u].sdt = bu32(d);
		}
		track.numstc = b->stsc.entrycount;
		track.stc = b->stsc.entry;
		printbox(b);
	}else if(b->type == BoxStco || b->type == BoxCo64){
		eBread(4, "entry_count");
		b->stco_co64.entrycount = bu32(d);
		b->stco_co64.chunkoffset = calloc(b->stco_co64.entrycount, sizeof(u64int));
		for(u = 0; u < b->stco_co64.entrycount; u++){
			eBread(b->type == BoxStco ? 4 : 8, "chunk_offset");
			b->stco_co64.chunkoffset[u] = b->type == BoxStco ? bu32(d) : bu64(d);
		}
		track.numchunks = b->stco_co64.entrycount;
		track.chunkoffset = b->stco_co64.chunkoffset;
		printbox(b);
	}else if(b->type == BoxStsz){
		eBread(4, "sample_size");
		b->stsz.samplesize = bu32(d);
		eBread(4, "sample_count");
		b->stsz.samplecount = bu32(d);
		if(b->stsz.samplesize == 0){
			b->stsz.entrysize = calloc(b->stsz.samplecount, sizeof(u32int));
			for(u = 0; u < b->stsz.samplecount; u++){
				eBread(4, "chunk_offset");
				b->stsz.entrysize[u] = bu32(d);
			}
			track.numsamples = b->stsz.samplecount;
			track.samplesize = b->stsz.entrysize;
		}
		printbox(b);
	}else if(b->type == BoxTkhd){
		if(b->version == 1){
			eBread(8, "creation_time");
			b->tkhd.creattime = bu64(d);
			eBread(8, "modification_time");
			b->tkhd.modtime = bu64(d);
			eBread(8, "track_id"); /* skipping 4 reserved as well */
			b->tkhd.trackid = bu32(d);
			track.id = b->tkhd.trackid;
			eBread(8, "duration");
			b->tkhd.duration = bu64(d);
		}else if(b->version == 0){
			eBread(4, "creation_time");
			b->tkhd.creattime = bu32(d);
			eBread(4, "modification_time");
			b->tkhd.modtime = bu32(d);
			eBread(8, "track_id"); /* skipping 4 reserved as well */
			b->tkhd.trackid = bu32(d);
			track.id = b->tkhd.trackid;
			eBread(4, "duration");
			b->tkhd.duration = bu32(d);
		}else{
			werrstr("uknown version %d", b->version);
			goto err;
		}
		eBread(8+2+2+2+2, "reserved, layer, alternate_group, volume, reserved");
		eBread(9*4, "matrix");
		eBread(4, "width");
		b->tkhd.width = bu32(d)>>16; /* FIXME fixed-point 16.16 */
		eBread(4, "height");
		b->tkhd.height = bu32(d)>>16; /* FIXME fixed-point 16.16 */
		printbox(b);
	}else if(b->type == BoxHdlr){
		eBread(4, "pre_defined");
		eBread(4, "handler_type");
		b->hdlr.handlertype = bu32(d);
		eBread(3*4, "reserved");
		for(u = 0; u < sizeof(d)-1; u++){
			if(Bread(f, d+u, 1) != 1){
				werrstr("name");
				goto err;
			}
			if(d[u] == 0)
				break;
		}
		d[u] = 0;
		b->hdlr.name = strdup((char*)d);
		printbox(b);
	}else{
		printbox(b);
	}

	return 0;
err:
	werrstr("%T: %r", b->type);
	return -1;
}

static int
parsebox(Biobuf *f, Box *b, int *eof)
{
	vlong sz, start;
	u8int d[8];
	int r;

	*eof = 0;
	b->dstart = start = Boffset(f);
	if((r = Bread(f, d, 8)) != 8){
		if(r == 0)
			*eof = 1;
		else
			werrstr("size and type");
		goto err;
	}
	b->dstart += 8;
	b->dsz = sz = d[0]<<24 | d[1]<<16 | d[2]<<8 | d[3];
	b->type = d[4]<<24 | d[5]<<16 | d[6]<<8 | d[7];

	if(sz == 1){
		if(Bread(f, d, 8) != 8){
			werrstr("largesize");
			goto err;
		}
		b->dstart += 8;
		b->dsz = (vlong)d[0]<<56 | (vlong)d[1]<<48 | (vlong)d[2]<<40 | (vlong)d[3]<<32 | d[4]<<24 | d[5]<<16 | d[6]<<8 | d[7];
	}else if(sz == 0){
		b->dsz = (vlong)1<<63 - 1;
	}

	if(b->type == BoxUuid){
		if(Bread(f, b->extended, 16) != 16){
			werrstr("extended_type");
			goto err;
		}
		b->dstart += 16;
	}else if(isfullbox(b)){
		if(Bread(f, d, 4) != 4){
			werrstr("full box");
			goto err;
		}
		b->version = d[0];
		b->flags = d[1]<<16 | d[2]<<8 | d[3];
		b->dstart += 4;
	}

	b->dsz -= b->dstart - start;

	if(parseboxdata(f, b) == 0)
		return 0;

err:
	werrstr("parsebox: %r");
	return -1;
}

static void
usage(void)
{
	fprint(2, "usage: iso [-d] [-t TRACK] FILE\n");
	exits("usage");
}

int
main(int argc, char **argv)
{
	Biobuf *f;
	Box b;
	int eof;

	dflag = 0;
	ARGBEGIN{
	case 'd':
		dflag = 1;
		break;
	case 't':
		trackdump = atoi(EARGF(usage()));
		break;
	}ARGEND

	fmtinstall('T', typefmt);

	for(; *argv; argv++){
		if((f = Bopen(*argv, OREAD)) == nil)
			sysfatal("%s: %r", *argv);

		for(;;){
			dind = 0;
			memset(&b, 0, sizeof(b));
			if(parsebox(f, &b, &eof) != 0){
				if(eof)
					break;
				sysfatal("%s: %r", *argv);
			}
			Bseek(f, b.dstart+b.dsz, 0);
		}

		Bterm(f);
	}

	return 0;
}