-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
mp4.hexpat
441 lines (374 loc) · 10.7 KB
/
mp4.hexpat
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
#pragma description MPEG-4 Part 14 digital multimedia container (MP4)
#pragma endian big
#pragma MIME audio/mp4
#pragma MIME video/mp4
#pragma MIME application/mp4
import std.io;
import std.mem;
import std.string;
fn to_string(auto var) {
return str(var);
};
fn format_string(auto string) {
return string.value;
};
fn format_fixed32(auto fp32) {
return fp32.integer + fp32.fraction / 65536.0;
};
fn format_fixed16(auto fp16) {
return fp16.integer + fp16.fraction / 256.0;
};
struct FixedPoint16 {
u8 integer;
u8 fraction;
} [[format("format_fixed16")]];
struct FixedPoint32 {
u16 integer;
u16 fraction;
} [[format("format_fixed32")]];
struct string {
char value[std::mem::find_sequence_in_range(0, $, std::mem::size(), 0x00) - $];
} [[sealed, format("format_string")]];
struct BaseBox {
u64 boxSize = 0;
u64 startOffset = $;
u64 endOffset = 0;
u32 size;
char type[4];
// Calculate the size of the current box
// 1. If the size is equal to 1 -> the box size is equal to 'largeSize' attribute.
// 2. If the size is equal to 0 -> The box extends to the end of the file.
// 3. Otherwise, size is equaly to the 'size' attribute.
if (this.size == 1) {
u64 largeSize;
boxSize = largeSize;
endOffset = startOffset + boxSize;
} else if (this.size == 0) {
boxSize = std::mem::size() - startOffset;
endOffset = std::mem::size();
} else {
boxSize = size;
endOffset = startOffset + boxSize;
}
if (this.type == "uuid") {
char usertype[16];
}
};
struct FullBox : BaseBox {
u8 version;
u24 flags;
};
struct UnknownBox : BaseBox {
u8 unk[while($ != endOffset)];
};
using brand = u32 [[format("to_string")]];
struct FileTypeBox : BaseBox {
brand major_brand;
u32 minor_version;
brand compatible_brands[(endOffset - $) / sizeof(brand)];
};
struct MovieHeaderBox : FullBox {
if (this.version == 1) {
u64 creation_time;
u64 modification_time;
u32 timescale;
u64 duration;
} else { // version == 0
u32 creation_time;
u32 modification_time;
u32 timescale;
u32 duration;
}
FixedPoint32 rate;
FixedPoint16 volume;
u8 reserved[10] [[sealed]];
u32 matrix[9];
u32 preview_time;
u32 preview_duration;
u32 poster_time;
u32 selection_time;
u32 selection_duration;
u32 current_time;
u32 next_track_id;
};
struct TrackHeaderBox : FullBox {
if (this.version == 1) {
u64 creation_time;
u64 modification_time;
u32 track_id;
u32 reserved;
u64 duration;
} else { // version == 0
u32 creation_time;
u32 modification_time;
u32 track_id;
u32 reserved;
u32 duration;
}
u32 reserved_2[2] [[sealed]];
s16 layer;
s16 alternate_group;
s16 volume;
u16 reserved_3;
s32 matrix[9];
FixedPoint32 width;
FixedPoint32 height;
};
struct DataEntryBox : FullBox {
if (std::string::contains(this.type, "url")) {
string location;
} else if (std::string::contains(this.type, "urn")) {
string name;
string location;
} else {
std::error("Invalid DataEntryBox");
}
};
struct DataReferenceBox : FullBox {
u32 entry_count;
DataEntryBox data_entries[this.entry_count];
};
struct SubDataInformationBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("dref"): DataReferenceBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("DataInformationBox({})", box.type))]];
struct DataInformationBox : BaseBox {
SubDataInformationBox box[while($ < endOffset)] [[inline]];
};
struct HandlerBox : FullBox {
u32 component_type;
char handler_type[4];
u32 reserved[3];
char name[endOffset - $];
};
struct VideoMediaHeaderBox : FullBox {
u16 graphicsmode;
u16 opcolor[3];
};
struct Avc1Box : BaseBox {
u48 reserved;
u16 data_reference_index;
u16 version;
u16 revision_level;
u32 max_packet_size;
if (this.version == 0 || this.version == 1) {
u32 temporal_quality;
u32 spatial_quality;
u16 width;
u16 height;
FixedPoint32 horizontal_resolution;
FixedPoint32 vertical_resolution;
u32 data_size;
u16 frame_count;
char compressor_name[32];
u16 depth;
s16 color_table_id;
}
u8 unk[while($ != endOffset)];
};
struct Mp4aBox : BaseBox {
u48 reserved;
u16 data_reference_index;
u16 version;
u16 revision_level;
u32 max_packet_size;
if (this.version == 0) {
u16 num_audio_channels;
u16 sample_size;
u16 compression_id;
u16 packet_size;
FixedPoint32 sample_rate;
}
u8 unk[while($ != endOffset)];
};
struct SubSampleDescriptionBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("mp4a"): Mp4aBox box [[inline]];
("avc1"): Avc1Box box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("SubSampleDescriptionBox({})", box.type))]];
struct SampleDescriptionBox : FullBox {
u32 entry_count;
SubSampleDescriptionBox box[while($ < endOffset)] [[inline]];
};
struct SampleTimeToSampleEntry {
u32 sample_count;
u32 sample_delta;
};
struct SampleTimeToSampleBox: FullBox {
u32 entry_count;
SampleTimeToSampleEntry entry_list[this.entry_count];
u8 unk[while($ != endOffset)];
};
struct SampleToChunkEntry {
u32 first_chunk;
u32 samples_per_chunk;
u32 sample_description_index;
};
struct SampleToChunkBox: FullBox {
u32 entry_count;
SampleToChunkEntry entry_list[this.entry_count];
u8 unk[while($ != endOffset)];
};
struct ChunkOffsetBox: FullBox {
u32 entry_count;
u32 chunk_offset[this.entry_count];
u8 unk[while($ != endOffset)];
};
struct SyncSampleBox: FullBox {
u32 entry_count;
u32 sample_number[this.entry_count];
u8 unk[while($ != endOffset)];
};
struct CompositionOffsetEntryV0 {
u32 sample_count;
u32 sample_offset;
};
struct CompositionOffsetEntryV1 {
u32 sample_count;
s32 sample_offset;
};
struct CompositionOffsetBox: FullBox {
u32 entry_count;
if(this.version == 0) {
CompositionOffsetEntryV0 entry_list[this.entry_count];
}
else if (this.version == 1) {
CompositionOffsetEntryV1 entry_list[this.entry_count];
}
};
struct SampleSizeBox: FullBox {
u32 sample_size;
u32 sample_count;
if(this.sample_size==0) {
u32 entry_size[this.sample_count];
}
};
struct SubSampleBoxTable {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("stsd"): SampleDescriptionBox box [[inline]];
("stts"): SampleTimeToSampleBox box [[inline]];
("stsc"): SampleToChunkBox box [[inline]];
("stco"): ChunkOffsetBox box [[inline]];
("stss"): SyncSampleBox box [[inline]];
("ctts"): CompositionOffsetBox box [[inline]];
("stsz"): SampleSizeBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("SubSampleBoxTable({})", box.type))]];
struct SampleBoxTable : BaseBox {
SubSampleBoxTable box[while($ < endOffset)] [[inline]];
};
struct SubMediaInformationBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("vmhd"): VideoMediaHeaderBox box [[inline]];
("hdlr"): HandlerBox box [[inline]];
("dinf"): DataInformationBox box [[inline]];
("stbl"): SampleBoxTable box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("MediaInformationBox({})", box.type))]];
struct MediaInformationBox : BaseBox {
SubMediaInformationBox box[while($ < endOffset)] [[inline]];
};
struct MediaHeaderBox : FullBox {
if (this.version == 1) {
u64 creation_time;
u64 modification_time;
u32 timescale;
u64 duration;
} else { // version==0
u32 creation_time;
u32 modification_time;
u32 timescale;
u32 duration;
}
u16 language [[comment("ISO-639-2/T language code")]];
u16 quality;
};
struct SubMediaBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("mdhd"): MediaHeaderBox box [[inline]];
("hdlr"): HandlerBox box [[inline]];
("minf"): MediaInformationBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("MediaBox({})", box.type))]];
struct MediaBox : BaseBox {
SubMediaBox box[while($ < endOffset)] [[inline]];
};
struct EditListEntry64 {
u64 segment_duration;
s64 media_time;
s16 media_rate_integer;
s16 media_rate_fraction;
};
struct EditListEntry32 {
u32 segment_duration;
s32 media_time;
s16 media_rate_integer;
s16 media_rate_fraction;
};
struct EditListBox : FullBox {
u32 entry_count;
if (this.version == 1) {
EditListEntry64 entry_list[this.entry_count];
} else { // version 0
EditListEntry32 entry_list[this.entry_count];
}
};
struct SubEditBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("elst"): EditListBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("EditBox({})", box.type))]];
struct EditBox : BaseBox {
SubEditBox box[while($ < endOffset)] [[inline]];
};
struct SubTrackBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("mdia"): MediaBox box [[inline]];
("edts"): EditBox box [[inline]];
("tkhd"): TrackHeaderBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("TrackBox({})", box.type))]];
struct TrackBox : BaseBox {
SubTrackBox box[while($ < endOffset)] [[inline]];
};
struct SubMovieBox {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("mvhd"): MovieHeaderBox box [[inline]];
("trak"): TrackBox box [[inline]];
(_): UnknownBox box [[inline]];
// TODO: Add "iods" box
}
} [[name(std::format("MovieBox({})", box.type))]];
struct MovieBox : BaseBox {
SubMovieBox box[while($ < endOffset)] [[inline]];
};
struct MediaDataBox : BaseBox {
u8 data[while($ < endOffset)] [[sealed]];
};
struct Box {
u32 type = std::mem::read_unsigned($ + 4, 4, std::mem::Endian::Big);
match (str(type)) {
("ftyp"): FileTypeBox box [[inline]];
("moov"): MovieBox box [[inline]];
("mdat"): MediaDataBox box [[inline]];
(_): UnknownBox box [[inline]];
}
} [[name(std::format("Box({})", box.type))]];
Box mp4[while(!std::mem::eof())] @ 0x0;