mp4-atom 0.10.1

A MP4/ISOBMFF atom decoder and encoder
Documentation
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
use crate::*;

#[test]
fn hevc() {
    const ENCODED: &[u8] = include_bytes!("hevc.mp4");

    let buf = &mut std::io::Cursor::new(ENCODED);
    let ftyp = Ftyp::decode(buf).expect("failed to decode ftyp");

    assert_eq!(
        ftyp,
        Ftyp {
            major_brand: b"iso6".into(),
            minor_version: 512,
            compatible_brands: vec![b"iso6".into(), b"cmfc".into(), b"mp41".into()],
        }
    );

    let moov = Moov::decode(buf).expect("failed to decode moov");
    assert_eq!(
        moov,
        Moov {
            mvhd: Mvhd {
                creation_time: 0,
                modification_time: 0,
                timescale: 1000,
                duration: 0,
                rate: 1.into(),
                volume: 1.into(),
                matrix: Matrix {
                    a: 65536,
                    b: 0,
                    u: 0,
                    c: 0,
                    d: 65536,
                    v: 0,
                    x: 0,
                    y: 0,
                    w: 1073741824
                },
                next_track_id: 2
            },
            meta: None,
            mvex: Some(Mvex {
                mehd: None,
                trex: vec![Trex {
                    track_id: 1,
                    default_sample_description_index: 1,
                    default_sample_duration: 0,
                    default_sample_size: 0,
                    default_sample_flags: 0
                }]
            }),
            trak: vec![Trak {
                tkhd: Tkhd {
                    creation_time: 0,
                    modification_time: 0,
                    track_id: 1,
                    duration: 0,
                    layer: 0,
                    alternate_group: 0,
                    enabled: true,
                    volume: 0.into(),
                    matrix: Matrix {
                        a: 65536,
                        b: 0,
                        u: 0,
                        c: 0,
                        d: 65536,
                        v: 0,
                        x: 0,
                        y: 0,
                        w: 1073741824
                    },
                    width: 1920.into(),
                    height: 1080.into()
                },
                edts: None,
                meta: None,
                mdia: Mdia {
                    mdhd: Mdhd {
                        creation_time: 0,
                        modification_time: 0,
                        timescale: 15360,
                        duration: 0,
                        language: "und".into()
                    },
                    hdlr: Hdlr {
                        handler: b"vide".into(),
                        name: "VideoHandler".into()
                    },
                    minf: Minf {
                        vmhd: Some(Vmhd {
                            graphics_mode: 0,
                            op_color: RgbColor {
                                red: 0,
                                green: 0,
                                blue: 0
                            }
                        }),
                        dinf: Dinf {
                            dref: Dref {
                                urls: vec![Url {
                                    location: "".to_string()
                                }]
                            }
                        },
                        stbl: Stbl {
                            stsd: Stsd {
                                codecs: vec![Hev1 {
                                    visual: Visual {
                                        data_reference_index: 1,
                                        width: 1920,
                                        height: 1080,
                                        horizresolution: 72.into(),
                                        vertresolution: 72.into(),
                                        frame_count: 1,
                                        compressor: "".into(),
                                        depth: 24
                                    },
                                    hvcc: Hvcc {
                                        configuration_version: 1,
                                        general_profile_space: 0,
                                        general_tier_flag: true,
                                        general_profile_idc: 1,
                                        general_profile_compatibility_flags: [96, 0, 0, 0],
                                        general_constraint_indicator_flags: [144, 0, 0, 0, 0, 0],
                                        general_level_idc: 120,
                                        min_spatial_segmentation_idc: 0,
                                        parallelism_type: 0,
                                        chroma_format_idc: 1,
                                        bit_depth_luma_minus8: 0,
                                        bit_depth_chroma_minus8: 0,
                                        avg_frame_rate: 0,
                                        constant_frame_rate: 0,
                                        num_temporal_layers: 1,
                                        temporal_id_nested: true,
                                        length_size_minus_one: 3,
                                        arrays: vec![
                                            HvcCArray {
                                                completeness: false,
                                                nal_unit_type: 32,
                                                nalus: vec![vec![
                                                    64, 1, 12, 1, 255, 255, 33, 96, 0, 0, 3, 0,
                                                    144, 0, 0, 3, 0, 0, 3, 0, 120, 149, 152, 9
                                                ]]
                                            },
                                            HvcCArray {
                                                completeness: false,
                                                nal_unit_type: 33,
                                                nalus: vec![vec![
                                                    66, 1, 1, 33, 96, 0, 0, 3, 0, 144, 0, 0, 3, 0,
                                                    0, 3, 0, 120, 160, 3, 192, 128, 16, 229, 150,
                                                    86, 105, 36, 202, 240, 16, 16, 0, 0, 3, 0, 16,
                                                    0, 0, 3, 1, 224, 128
                                                ]]
                                            },
                                            HvcCArray {
                                                completeness: false,
                                                nal_unit_type: 34,
                                                nalus: vec![vec![68, 1, 193, 114, 180, 98, 64]]
                                            },
                                            HvcCArray {
                                                completeness: false,
                                                nal_unit_type: 39,
                                                // It looks scary, but it's just the x265 CLI flags.
                                                nalus: vec![vec![
                                                    78, 1, 5, 255, 255, 255, 255, 255, 255, 255,
                                                    115, 44, 162, 222, 9, 181, 23, 71, 219, 187,
                                                    85, 164, 254, 127, 194, 252, 78, 120, 50, 54,
                                                    53, 32, 40, 98, 117, 105, 108, 100, 32, 49, 53,
                                                    49, 41, 32, 45, 32, 50, 46, 54, 43, 52, 57, 45,
                                                    55, 50, 49, 57, 51, 55, 54, 100, 101, 52, 50,
                                                    97, 58, 91, 87, 105, 110, 100, 111, 119, 115,
                                                    93, 91, 71, 67, 67, 32, 55, 46, 51, 46, 48, 93,
                                                    91, 54, 52, 32, 98, 105, 116, 93, 32, 56, 98,
                                                    105, 116, 43, 49, 48, 98, 105, 116, 32, 45, 32,
                                                    72, 46, 50, 54, 53, 47, 72, 69, 86, 67, 32, 99,
                                                    111, 100, 101, 99, 32, 45, 32, 67, 111, 112,
                                                    121, 114, 105, 103, 104, 116, 32, 50, 48, 49,
                                                    51, 45, 50, 48, 49, 56, 32, 40, 99, 41, 32, 77,
                                                    117, 108, 116, 105, 99, 111, 114, 101, 119, 97,
                                                    114, 101, 44, 32, 73, 110, 99, 32, 45, 32, 104,
                                                    116, 116, 112, 58, 47, 47, 120, 50, 54, 53, 46,
                                                    111, 114, 103, 32, 45, 32, 111, 112, 116, 105,
                                                    111, 110, 115, 58, 32, 99, 112, 117, 105, 100,
                                                    61, 49, 48, 53, 48, 49, 49, 49, 32, 102, 114,
                                                    97, 109, 101, 45, 116, 104, 114, 101, 97, 100,
                                                    115, 61, 51, 32, 110, 117, 109, 97, 45, 112,
                                                    111, 111, 108, 115, 61, 56, 32, 119, 112, 112,
                                                    32, 110, 111, 45, 112, 109, 111, 100, 101, 32,
                                                    110, 111, 45, 112, 109, 101, 32, 110, 111, 45,
                                                    112, 115, 110, 114, 32, 110, 111, 45, 115, 115,
                                                    105, 109, 32, 108, 111, 103, 45, 108, 101, 118,
                                                    101, 108, 61, 50, 32, 98, 105, 116, 100, 101,
                                                    112, 116, 104, 61, 56, 32, 105, 110, 112, 117,
                                                    116, 45, 99, 115, 112, 61, 49, 32, 102, 112,
                                                    115, 61, 51, 48, 47, 49, 32, 105, 110, 112,
                                                    117, 116, 45, 114, 101, 115, 61, 49, 57, 50,
                                                    48, 120, 49, 48, 56, 48, 32, 105, 110, 116,
                                                    101, 114, 108, 97, 99, 101, 61, 48, 32, 116,
                                                    111, 116, 97, 108, 45, 102, 114, 97, 109, 101,
                                                    115, 61, 48, 32, 108, 101, 118, 101, 108, 45,
                                                    105, 100, 99, 61, 48, 32, 104, 105, 103, 104,
                                                    45, 116, 105, 101, 114, 61, 49, 32, 117, 104,
                                                    100, 45, 98, 100, 61, 48, 32, 114, 101, 102,
                                                    61, 52, 32, 110, 111, 45, 97, 108, 108, 111,
                                                    119, 45, 110, 111, 110, 45, 99, 111, 110, 102,
                                                    111, 114, 109, 97, 110, 99, 101, 32, 110, 111,
                                                    45, 114, 101, 112, 101, 97, 116, 45, 104, 101,
                                                    97, 100, 101, 114, 115, 32, 97, 110, 110, 101,
                                                    120, 98, 32, 110, 111, 45, 97, 117, 100, 32,
                                                    110, 111, 45, 104, 114, 100, 32, 105, 110, 102,
                                                    111, 32, 104, 97, 115, 104, 61, 48, 32, 110,
                                                    111, 45, 116, 101, 109, 112, 111, 114, 97, 108,
                                                    45, 108, 97, 121, 101, 114, 115, 32, 111, 112,
                                                    101, 110, 45, 103, 111, 112, 32, 109, 105, 110,
                                                    45, 107, 101, 121, 105, 110, 116, 61, 50, 53,
                                                    32, 107, 101, 121, 105, 110, 116, 61, 50, 53,
                                                    48, 32, 103, 111, 112, 45, 108, 111, 111, 107,
                                                    97, 104, 101, 97, 100, 61, 48, 32, 98, 102,
                                                    114, 97, 109, 101, 115, 61, 52, 32, 98, 45, 97,
                                                    100, 97, 112, 116, 61, 50, 32, 98, 45, 112,
                                                    121, 114, 97, 109, 105, 100, 32, 98, 102, 114,
                                                    97, 109, 101, 45, 98, 105, 97, 115, 61, 48, 32,
                                                    114, 99, 45, 108, 111, 111, 107, 97, 104, 101,
                                                    97, 100, 61, 50, 53, 32, 108, 111, 111, 107,
                                                    97, 104, 101, 97, 100, 45, 115, 108, 105, 99,
                                                    101, 115, 61, 52, 32, 115, 99, 101, 110, 101,
                                                    99, 117, 116, 61, 52, 48, 32, 114, 97, 100,
                                                    108, 61, 48, 32, 110, 111, 45, 105, 110, 116,
                                                    114, 97, 45, 114, 101, 102, 114, 101, 115, 104,
                                                    32, 99, 116, 117, 61, 54, 52, 32, 109, 105,
                                                    110, 45, 99, 117, 45, 115, 105, 122, 101, 61,
                                                    56, 32, 114, 101, 99, 116, 32, 110, 111, 45,
                                                    97, 109, 112, 32, 109, 97, 120, 45, 116, 117,
                                                    45, 115, 105, 122, 101, 61, 51, 50, 32, 116,
                                                    117, 45, 105, 110, 116, 101, 114, 45, 100, 101,
                                                    112, 116, 104, 61, 49, 32, 116, 117, 45, 105,
                                                    110, 116, 114, 97, 45, 100, 101, 112, 116, 104,
                                                    61, 49, 32, 108, 105, 109, 105, 116, 45, 116,
                                                    117, 61, 48, 32, 114, 100, 111, 113, 45, 108,
                                                    101, 118, 101, 108, 61, 50, 32, 100, 121, 110,
                                                    97, 109, 105, 99, 45, 114, 100, 61, 48, 46, 48,
                                                    48, 32, 110, 111, 45, 115, 115, 105, 109, 45,
                                                    114, 100, 32, 115, 105, 103, 110, 104, 105,
                                                    100, 101, 32, 110, 111, 45, 116, 115, 107, 105,
                                                    112, 32, 110, 114, 45, 105, 110, 116, 114, 97,
                                                    61, 48, 32, 110, 114, 45, 105, 110, 116, 101,
                                                    114, 61, 48, 32, 110, 111, 45, 99, 111, 110,
                                                    115, 116, 114, 97, 105, 110, 101, 100, 45, 105,
                                                    110, 116, 114, 97, 32, 115, 116, 114, 111, 110,
                                                    103, 45, 105, 110, 116, 114, 97, 45, 115, 109,
                                                    111, 111, 116, 104, 105, 110, 103, 32, 109, 97,
                                                    120, 45, 109, 101, 114, 103, 101, 61, 51, 32,
                                                    108, 105, 109, 105, 116, 45, 114, 101, 102,
                                                    115, 61, 51, 32, 108, 105, 109, 105, 116, 45,
                                                    109, 111, 100, 101, 115, 32, 109, 101, 61, 51,
                                                    32, 115, 117, 98, 109, 101, 61, 51, 32, 109,
                                                    101, 114, 97, 110, 103, 101, 61, 53, 55, 32,
                                                    116, 101, 109, 112, 111, 114, 97, 108, 45, 109,
                                                    118, 112, 32, 119, 101, 105, 103, 104, 116,
                                                    112, 32, 110, 111, 45, 119, 101, 105, 103, 104,
                                                    116, 98, 32, 110, 111, 45, 97, 110, 97, 108,
                                                    121, 122, 101, 45, 115, 114, 99, 45, 112, 105,
                                                    99, 115, 32, 100, 101, 98, 108, 111, 99, 107,
                                                    61, 48, 58, 48, 32, 115, 97, 111, 32, 110, 111,
                                                    45, 115, 97, 111, 45, 110, 111, 110, 45, 100,
                                                    101, 98, 108, 111, 99, 107, 32, 114, 100, 61,
                                                    52, 32, 110, 111, 45, 101, 97, 114, 108, 121,
                                                    45, 115, 107, 105, 112, 32, 114, 115, 107, 105,
                                                    112, 32, 110, 111, 45, 102, 97, 115, 116, 45,
                                                    105, 110, 116, 114, 97, 32, 110, 111, 45, 116,
                                                    115, 107, 105, 112, 45, 102, 97, 115, 116, 32,
                                                    110, 111, 45, 99, 117, 45, 108, 111, 115, 115,
                                                    108, 101, 115, 115, 32, 110, 111, 45, 98, 45,
                                                    105, 110, 116, 114, 97, 32, 110, 111, 45, 115,
                                                    112, 108, 105, 116, 114, 100, 45, 115, 107,
                                                    105, 112, 32, 114, 100, 112, 101, 110, 97, 108,
                                                    116, 121, 61, 48, 32, 112, 115, 121, 45, 114,
                                                    100, 61, 50, 46, 48, 48, 32, 112, 115, 121, 45,
                                                    114, 100, 111, 113, 61, 49, 46, 48, 48, 32,
                                                    110, 111, 45, 114, 100, 45, 114, 101, 102, 105,
                                                    110, 101, 32, 110, 111, 45, 108, 111, 115, 115,
                                                    108, 101, 115, 115, 32, 99, 98, 113, 112, 111,
                                                    102, 102, 115, 61, 48, 32, 99, 114, 113, 112,
                                                    111, 102, 102, 115, 61, 48, 32, 114, 99, 61,
                                                    97, 98, 114, 32, 98, 105, 116, 114, 97, 116,
                                                    101, 61, 50, 51, 50, 48, 48, 32, 113, 99, 111,
                                                    109, 112, 61, 48, 46, 54, 48, 32, 113, 112,
                                                    115, 116, 101, 112, 61, 52, 32, 115, 116, 97,
                                                    116, 115, 45, 119, 114, 105, 116, 101, 61, 48,
                                                    32, 115, 116, 97, 116, 115, 45, 114, 101, 97,
                                                    100, 61, 48, 32, 105, 112, 114, 97, 116, 105,
                                                    111, 61, 49, 46, 52, 48, 32, 112, 98, 114, 97,
                                                    116, 105, 111, 61, 49, 46, 51, 48, 32, 97, 113,
                                                    45, 109, 111, 100, 101, 61, 49, 32, 97, 113,
                                                    45, 115, 116, 114, 101, 110, 103, 116, 104, 61,
                                                    49, 46, 48, 48, 32, 99, 117, 116, 114, 101,
                                                    101, 32, 122, 111, 110, 101, 45, 99, 111, 117,
                                                    110, 116, 61, 48, 32, 110, 111, 45, 115, 116,
                                                    114, 105, 99, 116, 45, 99, 98, 114, 32, 113,
                                                    103, 45, 115, 105, 122, 101, 61, 51, 50, 32,
                                                    110, 111, 45, 114, 99, 45, 103, 114, 97, 105,
                                                    110, 32, 113, 112, 109, 97, 120, 61, 54, 57,
                                                    32, 113, 112, 109, 105, 110, 61, 48, 32, 110,
                                                    111, 45, 99, 111, 110, 115, 116, 45, 118, 98,
                                                    118, 32, 115, 97, 114, 61, 49, 32, 111, 118,
                                                    101, 114, 115, 99, 97, 110, 61, 48, 32, 118,
                                                    105, 100, 101, 111, 102, 111, 114, 109, 97,
                                                    116, 61, 53, 32, 114, 97, 110, 103, 101, 61,
                                                    48, 32, 99, 111, 108, 111, 114, 112, 114, 105,
                                                    109, 61, 50, 32, 116, 114, 97, 110, 115, 102,
                                                    101, 114, 61, 50, 32, 99, 111, 108, 111, 114,
                                                    109, 97, 116, 114, 105, 120, 61, 50, 32, 99,
                                                    104, 114, 111, 109, 97, 108, 111, 99, 61, 48,
                                                    32, 100, 105, 115, 112, 108, 97, 121, 45, 119,
                                                    105, 110, 100, 111, 119, 61, 48, 32, 109, 97,
                                                    120, 45, 99, 108, 108, 61, 48, 44, 48, 32, 109,
                                                    105, 110, 45, 108, 117, 109, 97, 61, 48, 32,
                                                    109, 97, 120, 45, 108, 117, 109, 97, 61, 50,
                                                    53, 53, 32, 108, 111, 103, 50, 45, 109, 97,
                                                    120, 45, 112, 111, 99, 45, 108, 115, 98, 61,
                                                    56, 32, 118, 117, 105, 45, 116, 105, 109, 105,
                                                    110, 103, 45, 105, 110, 102, 111, 32, 118, 117,
                                                    105, 45, 104, 114, 100, 45, 105, 110, 102, 111,
                                                    32, 115, 108, 105, 99, 101, 115, 61, 49, 32,
                                                    110, 111, 45, 111, 112, 116, 45, 113, 112, 45,
                                                    112, 112, 115, 32, 110, 111, 45, 111, 112, 116,
                                                    45, 114, 101, 102, 45, 108, 105, 115, 116, 45,
                                                    108, 101, 110, 103, 116, 104, 45, 112, 112,
                                                    115, 32, 110, 111, 45, 109, 117, 108, 116, 105,
                                                    45, 112, 97, 115, 115, 45, 111, 112, 116, 45,
                                                    114, 112, 115, 32, 115, 99, 101, 110, 101, 99,
                                                    117, 116, 45, 98, 105, 97, 115, 61, 48, 46, 48,
                                                    53, 32, 110, 111, 45, 111, 112, 116, 45, 99,
                                                    117, 45, 100, 101, 108, 116, 97, 45, 113, 112,
                                                    32, 110, 111, 45, 97, 113, 45, 109, 111, 116,
                                                    105, 111, 110, 32, 110, 111, 45, 104, 100, 114,
                                                    32, 110, 111, 45, 104, 100, 114, 45, 111, 112,
                                                    116, 32, 110, 111, 45, 100, 104, 100, 114, 49,
                                                    48, 45, 111, 112, 116, 32, 97, 110, 97, 108,
                                                    121, 115, 105, 115, 45, 114, 101, 117, 115,
                                                    101, 45, 108, 101, 118, 101, 108, 61, 53, 32,
                                                    115, 99, 97, 108, 101, 45, 102, 97, 99, 116,
                                                    111, 114, 61, 48, 32, 114, 101, 102, 105, 110,
                                                    101, 45, 105, 110, 116, 114, 97, 61, 48, 32,
                                                    114, 101, 102, 105, 110, 101, 45, 105, 110,
                                                    116, 101, 114, 61, 48, 32, 114, 101, 102, 105,
                                                    110, 101, 45, 109, 118, 61, 48, 32, 110, 111,
                                                    45, 108, 105, 109, 105, 116, 45, 115, 97, 111,
                                                    32, 99, 116, 117, 45, 105, 110, 102, 111, 61,
                                                    48, 32, 110, 111, 45, 108, 111, 119, 112, 97,
                                                    115, 115, 45, 100, 99, 116, 32, 114, 101, 102,
                                                    105, 110, 101, 45, 109, 118, 45, 116, 121, 112,
                                                    101, 61, 48, 32, 99, 111, 112, 121, 45, 112,
                                                    105, 99, 61, 49, 128
                                                ]]
                                            }
                                        ]
                                    },
                                    btrt: Some(Btrt {
                                        buffer_size_db: 0,
                                        max_bitrate: 25175730,
                                        avg_bitrate: 25175730
                                    }),
                                    colr: None,
                                    pasp: Some(Pasp {
                                        h_spacing: 1,
                                        v_spacing: 1,
                                    }),
                                    taic: None,
                                    fiel: Some(Fiel {
                                        field_count: 1,
                                        field_order: 0
                                    }),
                                }
                                .into()],
                            },
                            stco: Some(Stco { entries: vec![] }),
                            ..Default::default()
                        },
                        ..Default::default()
                    }
                },
                senc: None,
                udta: None,
            }],
            udta: Some(Udta {
                meta: Some(Meta {
                    hdlr: Hdlr {
                        handler: FourCC::new(b"mdir"),
                        name: "".into()
                    },
                    items: vec![Ilst {
                        name: None,
                        year: None,
                        covr: None,
                        desc: None,
                        ctoo: Some(Tool {
                            country_indicator: 0,
                            language_indicator: 0,
                            text: "Lavf61.7.100".into()
                        })
                    }
                    .into(),],
                }),
                ..Default::default()
            })
        }
    );

    // Make sure the hev1 atom encodes/decodes to the exact same content.
    let hev1 = &moov.trak[0].mdia.minf.stbl.stsd.codecs[0];
    hev1.assert_encode_decode();

    let mut buf = Vec::new();
    ftyp.encode(&mut buf).expect("failed to encode ftyp");
    moov.encode(&mut buf).expect("failed to encode moov");

    // assert_eq!(buf, ENCODED);
}