-
Notifications
You must be signed in to change notification settings - Fork 8
/
MP4.bt
586 lines (549 loc) · 18 KB
/
MP4.bt
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
//------------------------------------------------
//--- 010 Editor v8.0 Binary Template
//
// File: MP4.bt
// Authors: Alexey Lyashko, Andrew Molyneux, Marian Denes, SweetScape
// Version: 3.1
// Purpose: Defines a template for parsing MP4 and MOV video files.
// Category: Video
// File Mask: *.mp4,*.mov,*.m4v
// ID Bytes: [+4] 66 74 79 70, [+4] 6D 6F 6F 76, [+4] 6D 64 61 74
// History:
// 3.1 2018-06-17 A. Lyashko: Added support for sample table atoms.
// 3.0 2017-10-03 A Molyneux: Total rewrite, more complete and robust.
// 2.2 2016-01-29 SweetScape: Updated header for repository submission.
// 2.1 2015-06-04 M Denes: Public release.
//------------------------------------------------
BigEndian();
struct fp16;
struct fp32;
struct fp32uvw;
struct matrix;
struct fourcc;
struct boxheader;
struct compatible_brand;
struct mp4box;
struct mp4time;
struct mp4lang;
struct qtopcolor;
struct elst_entry;
struct tkhd_flags;
struct vmhd_flags;
// QuickTime language codes from https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap4/qtff4.html#//apple_ref/doc/uid/TP40000939-CH206-34353
enum qtlang
{
qtlEnglish = 0,
qtlFrench = 1,
qtlGerman = 2,
qtlItalian = 3,
qtlDutch = 4,
qtlSwedish = 5,
qtlSpanish = 6,
qtlDanish = 7,
qtlPortuguese = 8,
qtlNorwegian = 9,
qtlHebrew = 10,
qtlJapanese = 11,
qtlArabic = 12,
qtlFinnish = 13,
qtlGreek = 14,
qtlIcelandic = 15,
qtlMaltese = 16,
qtlTurkish = 17,
qtlCroatian = 18,
qtlChineseTraditional = 19,
qtlUrdu = 20,
qtlHindi = 21,
qtlThai = 22,
qtlKorean = 23,
qtlLithuanian = 24,
qtlPolish = 25,
qtlHungarian = 26,
qtlEstonian = 27,
qtlLettish = 28,
qtlLatvian = 28,
qtlSaami = 29,
qtlSami = 29,
qtlFaroese = 30,
qtlFarsi = 31,
qtlRussian = 32,
qtlChineseSimplified = 33,
qtlFlemish = 34,
qtlIrish = 35,
qtlAlbanian = 36,
qtlRomanian = 37,
qtlCzech = 38,
qtlSlovak = 39,
qtlSlovenian = 40,
qtlYiddish = 41,
qtlSerbian = 42,
qtlMacedonian = 43,
qtlBulgarian = 44,
qtlUkrainian = 45,
qtlBelarusian = 46,
qtlUzbek = 47,
qtlKazakh = 48,
qtlAzerbaijani = 49,
qtlAzerbaijanAr = 50,
qtlArmenian = 51,
qtlGeorgian = 52,
qtlMoldavian = 53,
qtlKirghiz = 54,
qtlTajiki = 55,
qtlTurkmen = 56,
qtlMongolian = 57,
qtlMongolianCyr = 58,
qtlPashto = 59,
qtlKurdish = 60,
qtlKashmiri = 61,
qtlSindhi = 62,
qtlTibetan = 63,
qtlNepali = 64,
qtlSanskrit = 65,
qtlMarathi = 66,
qtlBengali = 67,
qtlAssamese = 68,
qtlGujarati = 69,
qtlPunjabi = 70,
qtlOriya = 71,
qtlMalayalam = 72,
qtlKannada = 73,
qtlTamil = 74,
qtlTelugu = 75,
qtlSinhala = 76,
qtlBurmese = 77,
qtlKhmer = 78,
qtlLao = 79,
qtlVietnamese = 80,
qtlIndonesian = 81,
qtlTagalog = 82,
qtlMalayRoman = 83,
qtlMalayArabic = 84,
qtlAmharic = 85,
qtlGalla = 87,
qtlOromo = 87,
qtlSomali = 88,
qtlSwahili = 89,
qtlKinyarwanda = 90,
qtlRundi = 91,
qtlNyanja = 92,
qtlMalagasy = 93,
qtlEsperanto = 94,
qtlWelsh = 128,
qtlBasque = 129,
qtlCatalan = 130,
qtlLatin = 131,
qtlQuechua = 132,
qtlGuarani = 133,
qtlAymara = 134,
qtlTatar = 135,
qtlUighur = 136,
qtlDzongkha = 137,
qtlJavaneseRom = 138,
qtlUnspecified = 32767
};
// QuickTime graphics modes from https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap4/qtff4.html#//apple_ref/doc/uid/TP40000939-CH206-18741
enum <uint16> qtgfxmode
{
qtgCopy = 0x000,
qtgDitherCopy = 0x040,
qtgBlend = 0x020,
qtgTransparent = 0x024,
qtgStraightAlpha = 0x100,
qtgPremulWhiteAlpha = 0x101,
qtgPremulBlackAlpha = 0x102,
qtgStraightAlphaBlend = 0x104,
qtgComposition = 0x103
};
uint64 ContentSize(boxheader& bh)
{
return (exists(bh.size64) ? bh.size64 : bh.size) - sizeof(bh);
}
string ReadFP16(fp16& f)
{
local string buf;
SPrintf(buf, "%lf", (double)f.value / (double)0x100);
return buf;
}
string ReadFP32(fp32& f)
{
local string buf;
SPrintf(buf, "%lf", (double)f.value / (double)0x10000);
return buf;
}
string ReadFP32uvw(fp32uvw& f)
{
local string buf;
SPrintf(buf, "%lf", (double)f.value / (double)0x40000000);
return buf;
}
wstring ReadBox(mp4box& box)
{
return box.hdr.type.value;
}
string BoxComment(mp4box& box)
{
switch (box.hdr.type.value)
{
case "dinf": return "Data Information Box";
case "dref": return "Data Reference Box";
case "edts": return "Edit Box";
case "elst": return "Edit List Box";
case "free": return "Free Space Box";
case "ftyp": return "File Type Box";
case "hdlr": return "Handler Reference Box";
case "mdat": return "Media Data Box";
case "mdhd": return "Media Header Box";
case "mdia": return "Media Box";
case "minf": return "Media Information Box";
case "moov": return "Movie Box";
case "mvhd": return "Movie Header Box";
case "stbl": return "Sample Table Box";
case "tkhd": return "Track Header Box";
case "trak": return "Track Box";
case "udta": return "User Data Box";
case "vmhd": return "Video Media Information Header Box";
// Added the following to support stbl child atoms
case "stss": return "Sync Sample Box";
case "stsz": return "Sample Size Box";
case "stco": return "Chunk Offset Box";
case "co64": return "Chunk Large Offset Box";
case "stsc": return "Sample To Chunk Box";
case "stts": return "Time To Sample Box";
case "stsd": return "Sample Description Box";
default: return "Unknown box type";
}
}
wstring ReadFourCC(fourcc& f)
{
return f.value;
}
wstring ReadBoxHeader(boxheader& bh)
{
local string s;
SPrintf(s, "[size=%Lu]", ContentSize(bh));
return bh.type.value + " " + s;
}
string ReadCompatibleBrand(compatible_brand& b)
{
return b.brand.value;
}
string ReadTime(mp4time& t)
{
// Magic number 2082844800 is the difference in seconds between the
// classic Mac OS epoch (midnight on January 1st 1904) and the POSIX
// epoch (midnight on January 1st 1970).
local quad value = (quad)t.value - 2082844800;
return TimeTToString(value, "yyyy-MM-dd hh:mm:ss");
}
string ReadLang(mp4lang& lang)
{
if (lang.value < 0x0400 || lang.value == qtlUnspecified)
{
local qtlang v = lang.value;
return EnumToString(v);
}
local char iso[4];
iso[0] = ((lang.value & 0x7c00) >> 10) + 0x60;
iso[1] = ((lang.value & 0x03e0) >> 5) + 0x60;
iso[2] = (lang.value & 0x001f) + 0x60;
iso[3] = '\0';
return iso;
}
string ReadOpcolor(qtopcolor& c)
{
local string result;
SPrintf(result, "r=%d, g=%d, b=%d", c.red, c.green, c.blue);
return result;
}
string PipeConcat(string s1, string s2)
{
if (Strlen(s1) > 0)
{
return s1 + " | " + s2;
}
return s2;
}
string ReadTkhdFlags(tkhd_flags& f)
{
local string result;
if (f.track_enabled)
{
result = "Track enabled";
}
if (f.track_in_movie)
{
result = PipeConcat(result, "Track in movie");
}
if (f.track_in_preview)
{
result = PipeConcat(result, "Track in preview");
}
if (f.track_in_poster)
{
result = PipeConcat(result, "Track in poster");
}
return result;
}
string ReadVmhdFlags(vmhd_flags& f)
{
local string result;
if (f.no_lean_ahead)
{
result = "No lean ahead";
}
return result;
}
typedef struct
{
int16 value;
} fp16 <read=ReadFP16, open=suppress>;
typedef struct
{
int32 value;
} fp32 <read=ReadFP32, open=suppress>;
typedef struct
{
int32 value;
} fp32uvw <read=ReadFP32uvw, open=suppress>;
typedef struct
{
fp32 a <name="a">;
fp32 b <name="b">;
fp32uvw u <name="u">;
fp32 c <name="c">;
fp32 d <name="d">;
fp32uvw v <name="v">;
fp32 x <name="x">;
fp32 y <name="y">;
fp32uvw w <name="w">;
} matrix <open=suppress>;
typedef struct
{
byte value[4] <open=suppress>;
} fourcc <read=ReadFourCC, open=suppress>;
typedef struct
{
uint32 size;
fourcc type;
if (size == 1)
{
uint64 size64;
}
} boxheader <read=ReadBoxHeader, open=suppress>;
typedef struct
{
fourcc brand;
} compatible_brand <read=ReadCompatibleBrand, open=suppress>;
typedef struct
{
uint32 value;
} mp4time <read=ReadTime, open=suppress>;
typedef struct
{
uint16 value;
} mp4lang <read=ReadLang, open=suppress>;
typedef struct
{
uint16 red;
uint16 green;
uint16 blue;
} qtopcolor <read=ReadOpcolor, open=suppress>;
typedef struct
{
uint32 duration <name="Duration", comment="Duration of this edit segment in movie time scale units (specified in mvhd box)">;
uint32 media_time <name="Media Time", comment="Start time of this edit segment in media time scale units (specified in mdhd box)">;
fp32 media_rate <name="Media Rate", comment="Relative rate at which to play this edit segment">;
} elst_entry;
typedef struct
{
byte dummy[2] <open=suppress>;
byte dummy2 : 4;
byte track_in_poster : 1 <name="Track in poster">;
byte track_in_preview : 1 <name="Track in preview">;
byte track_in_movie : 1 <name="Track in movie">;
byte track_enabled : 1 <name="Track enabled">;
} tkhd_flags <read=ReadTkhdFlags, open=suppress>;
typedef struct
{
byte dummy[2];
byte dummy2 : 7;
byte no_lean_ahead : 1;
} vmhd_flags <read=ReadVmhdFlags, open=suppress>;
// The following three structs are used with sample table atoms
typedef struct
{
uint32 first_chunk;
uint32 samples_per_chunk;
uint32 sample_description_index;
}str_stsc;
typedef struct
{
uint32 sample_count;
uint32 sample_delta;
}str_stts;
typedef struct
{
uint32 contentSize;
byte content[contentSize - 4];
}str_stsd;
typedef struct
{
boxheader hdr;
local uint64 contentsize = ContentSize(hdr);
switch (hdr.type.value)
{
// These box types are all just containers for other boxes
case "dinf":
case "edts":
case "mdia":
case "minf":
case "moov":
case "stbl":
case "trak":
case "udta":
local quad startOffset = FTell();
while (FTell() < startOffset + contentsize)
{
mp4box box <name="Box">;
}
break;
// Added stsd, stts, stsc, stco, co64, stsz and stss
case "stsd":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
str_stsd description[entry_count];
break;
case "stts":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
str_stts entry[entry_count];
break;
case "stsc":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
str_stsc entry[entry_count];
break;
case "stco":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
uint32 chunk_offset[entry_count];
break;
case "co64":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
uint64 chunk_offset[entry_count];
break;
case "stsz":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 sample_size;
uint32 sample_count;
if(sample_size == 0)
{
uint32 entry_size[sample_count];
}
break;
case "stss":
byte version <name="Version">;
byte flag[3] <open=suppress, name="Flags">;
uint32 entry_count;
uint32 sample_number[entry_count];
break;
case "elst":
byte version <name="Version">;
byte flags[3] <open=suppress, name="Flags">;
uint32 num_entries <name="Number of Entries">;
elst_entry entries[num_entries] <open=suppress, name="Edit List Table">;
break;
case "ftyp":
fourcc major_brand <name="Major Brand">;
uint32 minor_version <format=hex, name="Minor Version">;
while (FTell() < startof(major_brand) + contentsize)
{
compatible_brand brand <name="Compatible Brand">;
}
break;
case "hdlr":
byte version <name="Version">;
byte flags[3] <open=suppress, name="Flags">;
fourcc type <name="Component Type">;
fourcc subtype <name="Component Subtype">;
uint32 mfr <name="Component Manufacturer">;
uint32 flags_mask <name="Component Flags Mask">;
// TODO: Apparently the rest of this box is a "counted string" giving the component name,
// but I haven't yet found good documentation on how it's structured
byte rest[contentsize - (FTell() - startof(version))] <open=suppress>;
break;
case "mdhd":
byte version <name="Version">;
byte flags[3] <open=suppress, name="Flags">;
mp4time create_time <name="Creation Time">;
mp4time modify_time <name="Modification Time">;
uint32 time_scale <name="Time Scale", comment="Number of media time units per second">;
uint32 duration <name="Duration", comment="Duration of the media in media time scale units">;
mp4lang language <name="Language">;
uint16 quality <name="Quality">;
break;
case "mvhd":
byte version <name="Version">;
byte flags[3] <open=suppress, name="Flags">;
mp4time create_time <name="Creation Time">;
mp4time modify_time <name="Modification Time">;
uint32 time_scale <name="Time Scale", comment="Number of movie time units per second">;
uint32 duration <name="Duration", comment="Duration of the movie in movie time scale units">;
fp32 preferred_rate <name="Preferred Rate">;
fp16 preferred_volume <name="Preferred Volume">;
byte reserved[10] <name="Reserved">;
matrix matrix_structure <name="Matrix Structure">;
uint32 preview_time <name="Preview Time", comment="Time value in the movie at which the preview begins">;
uint32 preview_duration <name="Preview Duration", comment="Duration of the movie preview in movie time scale units">;
uint32 poster_time <name="Poster Time", comment="Time value in the movie of the movie poster">;
uint32 selection_time <name="Selection Time", comment="Start time of the current selection">;
uint32 selection_duration <name="Selection Duration", comment="Duration of the current selection in movie time scale units">;
uint32 current_time <name="Current Time", comment="Time value of current time position within the movie">;
uint32 next_track_id <name="Next Track ID", comment="The value to use for the track ID number of the next track added">;
break;
case "smhd":
byte version <name="Version">;
byte flags[3] <open=suppress, name="Flags">;
fp16 balance <name="Balance">;
uint16 reserved <name="Reserved">;
break;
case "tkhd":
byte version <name="Version">;
tkhd_flags flags <name="Flags">;
mp4time create_time <name="Creation Time">;
mp4time modify_time <name="Modification Time">;
uint32 track_id <name="Track ID">;
uint32 reserved <name="Reserved">;
uint32 duration <name="Duration", comment="Duration of the media in movie time scale units (specified in mvhd box)">;
uint64 reserved2 <name="Reserved">;
uint16 layer <name="Layer">;
uint16 alt_group <name="Alternate Group">;
fp16 volume <name="Volume">;
uint16 reserved3 <name="Reserved">;
matrix matrix_structure <name="Matrix Structure">;
fp32 width <name="Track Width">;
fp32 height <name="Track Height">;
break;
case "vmhd":
byte version <name="Version">;
vmhd_flags flags <name="Flags">;
qtgfxmode graphics_mode <name="Graphics Mode">;
qtopcolor opcolor <name="Opcolor", open=suppress>;
break;
default:
FSkip(contentsize);
break;
}
} mp4box <read=ReadBox, comment=BoxComment>;
while (FTell() < FileSize())
{
mp4box box <name="Box">;
}