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
423
424
use crate::*;

#[test]
fn heif() {
    // Complete image from libheif
    const ENCODED: &[u8] = include_bytes!("image.heif");

    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"heic".into(),
            minor_version: 0,
            compatible_brands: vec![b"mif1".into(), b"heic".into(), b"miaf".into()],
        }
    );

    let meta = Meta::decode(buf).expect("failed to decode meta");
    assert_eq!(
        meta,
        Meta {
            hdlr: Hdlr {
                handler: b"pict".into(),
                name: "".into()
            },
            items: vec![
                Pitm { item_id: 1 }.into(),
                Iloc {
                    item_locations: vec![
                        ItemLocation {
                            item_id: 1,
                            construction_method: 0,
                            data_reference_index: 0,
                            base_offset: 656,
                            extents: vec![ItemLocationExtent {
                                item_reference_index: 0,
                                offset: 0,
                                length: 43
                            }]
                        },
                        ItemLocation {
                            item_id: 2,
                            construction_method: 0,
                            data_reference_index: 0,
                            base_offset: 699,
                            extents: vec![ItemLocationExtent {
                                item_reference_index: 0,
                                offset: 0,
                                length: 35
                            }]
                        }
                    ]
                }
                .into(),
                Iinf {
                    item_infos: [
                        ItemInfoEntry {
                            item_id: 1,
                            item_protection_index: 0,
                            item_type: Some(FourCC::new(b"hvc1")),
                            item_name: "".into(),
                            content_type: None,
                            content_encoding: None,
                            item_not_in_presentation: false,
                        },
                        ItemInfoEntry {
                            item_id: 2,
                            item_protection_index: 0,
                            item_type: Some(FourCC::new(b"hvc1")),
                            item_name: "".into(),
                            content_type: None,
                            content_encoding: None,
                            item_not_in_presentation: false,
                        },
                    ]
                    .to_vec()
                }
                .into(),
                Iprp {
                    ipco: Ipco {
                        properties: vec![
                            any::Any::Hvcc(Hvcc {
                                configuration_version: 1,
                                general_profile_space: 0,
                                general_tier_flag: false,
                                general_profile_idc: 3,
                                general_profile_compatibility_flags: [112, 0, 0, 0],
                                general_constraint_indicator_flags: [0, 0, 0, 0, 0, 0],
                                general_level_idc: 30,
                                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, 3, 112, 0, 0, 3, 0, 144, 0, 0,
                                            3, 0, 0, 3, 0, 30, 186, 2, 64
                                        ]]
                                    },
                                    HvcCArray {
                                        completeness: false,
                                        nal_unit_type: 33,
                                        nalus: vec![vec![
                                            66, 1, 1, 3, 112, 0, 0, 3, 0, 144, 0, 0, 3, 0, 0, 3, 0,
                                            30, 160, 32, 129, 5, 150, 234, 174, 154, 230, 224, 33,
                                            160, 192, 128, 0, 0, 3, 0, 128, 0, 0, 3, 0, 132
                                        ]]
                                    },
                                    HvcCArray {
                                        completeness: false,
                                        nal_unit_type: 34,
                                        nalus: vec![vec![68, 1, 193, 115, 193, 137]]
                                    }
                                ]
                            }),
                            Any::Colr(Colr::Nclx {
                                colour_primaries: 1,
                                transfer_characteristics: 13,
                                matrix_coefficients: 6,
                                full_range_flag: true
                            }),
                            Any::Ispe(Ispe {
                                width: 64,
                                height: 64
                            }),
                            Any::Clap(Clap {
                                clean_aperture_width_n: 8,
                                clean_aperture_width_d: 1,
                                clean_aperture_height_n: 1,
                                clean_aperture_height_d: 1,
                                horiz_off_n: -56,
                                horiz_off_d: 2,
                                vert_off_n: -63,
                                vert_off_d: 2
                            }),
                            Any::Pixi(Pixi {
                                bits_per_channel: vec![8, 8, 8]
                            }),
                            Any::Hvcc(Hvcc {
                                configuration_version: 1,
                                general_profile_space: 0,
                                general_tier_flag: false,
                                general_profile_idc: 4,
                                general_profile_compatibility_flags: [8, 0, 0, 0],
                                general_constraint_indicator_flags: [0, 0, 0, 0, 0, 0],
                                general_level_idc: 30,
                                min_spatial_segmentation_idc: 0,
                                parallelism_type: 0,
                                chroma_format_idc: 0,
                                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, 4, 8, 0, 0, 3, 0, 159, 248, 0,
                                            0, 3, 0, 0, 30, 186, 2, 64
                                        ]]
                                    },
                                    HvcCArray {
                                        completeness: false,
                                        nal_unit_type: 33,
                                        nalus: vec![vec![
                                            66, 1, 1, 4, 8, 0, 0, 3, 0, 159, 248, 0, 0, 3, 0, 0,
                                            30, 192, 130, 4, 22, 91, 170, 186, 107, 155, 2, 0, 0,
                                            3, 0, 2, 0, 0, 3, 0, 2, 16
                                        ]]
                                    },
                                    HvcCArray {
                                        completeness: false,
                                        nal_unit_type: 34,
                                        nalus: vec![vec![68, 1, 193, 115, 193, 137]]
                                    }
                                ]
                            }),
                            Any::Pixi(Pixi {
                                bits_per_channel: vec![8]
                            }),
                            Any::Auxc(Auxc {
                                aux_type: "urn:mpeg:hevc:2015:auxid:1".into(),
                                aux_subtype: vec![]
                            }),
                        ]
                    },
                    ipma: vec![Ipma {
                        item_properties: vec![
                            PropertyAssociations {
                                item_id: 1,
                                associations: vec![
                                    PropertyAssociation {
                                        essential: true,
                                        property_index: 1
                                    },
                                    PropertyAssociation {
                                        essential: false,
                                        property_index: 2
                                    },
                                    PropertyAssociation {
                                        essential: false,
                                        property_index: 3
                                    },
                                    PropertyAssociation {
                                        essential: false,
                                        property_index: 5
                                    },
                                    PropertyAssociation {
                                        essential: true,
                                        property_index: 4
                                    }
                                ]
                            },
                            PropertyAssociations {
                                item_id: 2,
                                associations: vec![
                                    PropertyAssociation {
                                        essential: true,
                                        property_index: 6
                                    },
                                    PropertyAssociation {
                                        essential: false,
                                        property_index: 3
                                    },
                                    PropertyAssociation {
                                        essential: false,
                                        property_index: 7
                                    },
                                    PropertyAssociation {
                                        essential: true,
                                        property_index: 8
                                    },
                                    PropertyAssociation {
                                        essential: true,
                                        property_index: 4
                                    }
                                ]
                            }
                        ]
                    }]
                }
                .into(),
                Iref {
                    references: vec![Reference {
                        reference_type: FourCC::new(b"auxl"),
                        from_item_id: 2,
                        to_item_ids: vec![1]
                    }]
                }
                .into(),
            ],
        }
    );

    let mdat = Mdat::decode(buf).expect("failed to decode mdat");

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

    assert_eq!(buf, ENCODED);
}

#[test]
fn avif() {
    // Complete image from libavif
    const ENCODED: &[u8] = include_bytes!("image.avif");

    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"avif".into(),
            minor_version: 0,
            compatible_brands: vec![
                b"avif".into(),
                b"mif1".into(),
                b"miaf".into(),
                b"MA1A".into()
            ],
        }
    );

    let meta = Meta::decode(buf).expect("failed to decode meta");
    assert_eq!(
        meta,
        Meta {
            hdlr: Hdlr {
                handler: b"pict".into(),
                name: "".into()
            },
            items: vec![
                Pitm { item_id: 1 }.into(),
                Iloc {
                    item_locations: vec![ItemLocation {
                        item_id: 1,
                        construction_method: 0,
                        data_reference_index: 0,
                        base_offset: 0,
                        extents: vec![ItemLocationExtent {
                            item_reference_index: 0,
                            offset: 312,
                            length: 26
                        }]
                    }]
                }
                .into(),
                Iinf {
                    item_infos: vec![ItemInfoEntry {
                        item_id: 1,
                        item_protection_index: 0,
                        item_type: Some(FourCC::new(b"av01")),
                        item_name: "Color".into(),
                        content_type: None,
                        content_encoding: None,
                        item_not_in_presentation: false
                    }]
                }
                .into(),
                Iprp {
                    ipco: Ipco {
                        properties: vec![
                            Any::Ispe(Ispe {
                                width: 25,
                                height: 18
                            }),
                            Any::Pixi(Pixi {
                                bits_per_channel: vec![8, 8, 8]
                            }),
                            Any::Av1c(Av1c {
                                seq_profile: 1,
                                seq_level_idx_0: 0,
                                seq_tier_0: false,
                                high_bitdepth: false,
                                twelve_bit: false,
                                monochrome: false,
                                chroma_subsampling_x: false,
                                chroma_subsampling_y: false,
                                chroma_sample_position: 0,
                                initial_presentation_delay: None,
                                config_obus: vec![]
                            }),
                            Any::Colr(Colr::Nclx {
                                colour_primaries: 1,
                                transfer_characteristics: 13,
                                matrix_coefficients: 6,
                                full_range_flag: true
                            }),
                            Any::Pasp(Pasp {
                                h_spacing: 2,
                                v_spacing: 3
                            }),
                            Any::Irot(Irot { angle: 1 }),
                            Any::Imir(Imir { axis: 1 }),
                        ]
                    },
                    ipma: vec![Ipma {
                        item_properties: vec![PropertyAssociations {
                            item_id: 1,
                            associations: vec![
                                PropertyAssociation {
                                    essential: false,
                                    property_index: 1
                                },
                                PropertyAssociation {
                                    essential: false,
                                    property_index: 2
                                },
                                PropertyAssociation {
                                    essential: true,
                                    property_index: 3
                                },
                                PropertyAssociation {
                                    essential: false,
                                    property_index: 4
                                },
                                PropertyAssociation {
                                    essential: false,
                                    property_index: 5
                                },
                                PropertyAssociation {
                                    essential: true,
                                    property_index: 6
                                },
                                PropertyAssociation {
                                    essential: true,
                                    property_index: 7
                                }
                            ]
                        }]
                    }]
                }
                .into(),
            ],
        }
    );

    let mdat = Mdat::decode(buf).expect("failed to decode mdat");

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

    assert_eq!(buf, ENCODED);
}