Skip to main content

mp4_atom/moov/
mod.rs

1mod ainf;
2mod mvex;
3mod mvhd;
4mod trak;
5mod udta;
6
7pub use ainf::*;
8pub use mvex::*;
9pub use mvhd::*;
10pub use trak::*;
11pub use udta::*;
12
13use crate::*;
14
15#[derive(Debug, Clone, PartialEq, Default)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub struct Moov {
18    pub mvhd: Mvhd,
19    pub meta: Option<Meta>,
20    pub mvex: Option<Mvex>,
21    pub trak: Vec<Trak>,
22    pub udta: Option<Udta>,
23    pub ainf: Option<Ainf>,
24}
25
26impl Atom for Moov {
27    const KIND: FourCC = FourCC::new(b"moov");
28
29    nested! {
30        required: [ Mvhd ],
31        optional: [ Ainf, Meta, Mvex, Udta ],
32        multiple: [ Trak ],
33    }
34}
35
36#[cfg(test)]
37mod test {
38    use super::*;
39
40    #[test]
41    fn test_meta() {
42        const ENCODED: &[u8] = &[
43            0x00, 0x00, 0x03, 0x3A, 0x6D, 0x6F, 0x6F, 0x76, 0x00, 0x00, 0x00, 0x6C, 0x6D, 0x76,
44            0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xEF, 0xCF, 0xD6, 0xE0, 0xEF, 0xCF, 0xD6,
45            0x00, 0x01, 0x5F, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
46            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
47            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
48            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49            0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51            0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x8D, 0x6D, 0x65, 0x74, 0x61, 0x00, 0x00,
52            0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x68, 0x64, 0x6C, 0x72, 0x00, 0x00, 0x00, 0x00,
53            0x00, 0x00, 0x00, 0x00, 0x49, 0x44, 0x33, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x49, 0x44, 0x33, 0x32,
55            0x00, 0x00, 0x00, 0x00, 0x15, 0xC7, 0x49, 0x44, 0x33, 0x04, 0x00, 0x00, 0x00, 0x00,
56            0x00, 0x49, 0x50, 0x52, 0x49, 0x56, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x68, 0x74,
57            0x74, 0x70, 0x73, 0x3A, 0x2F, 0x2F, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2E, 0x63,
58            0x6F, 0x6D, 0x2F, 0x67, 0x6F, 0x6F, 0x67, 0x6C, 0x65, 0x2F, 0x73, 0x68, 0x61, 0x6B,
59            0x61, 0x2D, 0x70, 0x61, 0x63, 0x6B, 0x61, 0x67, 0x65, 0x72, 0x00, 0x76, 0x32, 0x2E,
60            0x36, 0x2E, 0x31, 0x2D, 0x36, 0x33, 0x34, 0x61, 0x66, 0x36, 0x35, 0x2D, 0x72, 0x65,
61            0x6C, 0x65, 0x61, 0x73, 0x65, 0x00, 0x00, 0x02, 0x01, 0x74, 0x72, 0x61, 0x6B, 0x00,
62            0x00, 0x00, 0x5C, 0x74, 0x6B, 0x68, 0x64, 0x00, 0x00, 0x00, 0x07, 0xE0, 0xEF, 0xCF,
63            0xD6, 0xE0, 0xEF, 0xCF, 0xD6, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
64            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
67            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x07,
68            0x80, 0x00, 0x00, 0x04, 0x38, 0x00, 0x00, 0x00, 0x00, 0x01, 0x79, 0x6D, 0x64, 0x69,
69            0x61, 0x00, 0x00, 0x00, 0x20, 0x6D, 0x64, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0xE0,
70            0xEF, 0xCF, 0xD6, 0xE0, 0xEF, 0xCF, 0xD6, 0x00, 0x01, 0x5F, 0x90, 0x00, 0x00, 0x00,
71            0x00, 0x55, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2D, 0x68, 0x64, 0x6C, 0x72, 0x00,
72            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x69, 0x64, 0x65, 0x00, 0x00, 0x00,
73            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6F,
74            0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00, 0x00, 0x00, 0x01, 0x24, 0x6D, 0x69,
75            0x6E, 0x66, 0x00, 0x00, 0x00, 0x24, 0x64, 0x69, 0x6E, 0x66, 0x00, 0x00, 0x00, 0x1C,
76            0x64, 0x72, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
77            0x00, 0x0C, 0x75, 0x72, 0x6C, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xE4,
78            0x73, 0x74, 0x62, 0x6C, 0x00, 0x00, 0x00, 0x98, 0x73, 0x74, 0x73, 0x64, 0x00, 0x00,
79            0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x88, 0x61, 0x76, 0x63, 0x31,
80            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x04, 0x38,
82            0x00, 0x48, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
83            0x0A, 0x41, 0x56, 0x43, 0x20, 0x43, 0x6F, 0x64, 0x69, 0x6E, 0x67, 0x00, 0x00, 0x00,
84            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85            0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x32, 0x61, 0x76,
86            0x63, 0x43, 0x01, 0x64, 0x00, 0x28, 0xFF, 0xE1, 0x00, 0x1A, 0x67, 0x64, 0x00, 0x28,
87            0xAC, 0xD9, 0x40, 0x78, 0x02, 0x27, 0xE5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00,
88            0x00, 0x03, 0x00, 0xF0, 0x3C, 0x60, 0xC6, 0x58, 0x01, 0x00, 0x05, 0x68, 0xEB, 0xEC,
89            0xB2, 0x2C, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x74, 0x73, 0x00, 0x00, 0x00, 0x00,
90            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x73, 0x74, 0x73, 0x63, 0x00, 0x00,
91            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x73, 0x74, 0x73, 0x7A,
92            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93            0x00, 0x10, 0x73, 0x74, 0x63, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94            0x00, 0x00, 0x00, 0x14, 0x76, 0x6D, 0x68, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
95            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x65, 0x64, 0x74, 0x73,
96            0x00, 0x00, 0x00, 0x1C, 0x65, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97            0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x70, 0x00, 0x01, 0x00, 0x00,
98            0x00, 0x00, 0x00, 0x38, 0x6D, 0x76, 0x65, 0x78, 0x00, 0x00, 0x00, 0x10, 0x6D, 0x65,
99            0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x01, 0xB9, 0x31, 0x50, 0x00, 0x00, 0x00, 0x20,
100            0x74, 0x72, 0x65, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
101            0x00, 0x01, 0x00, 0x00, 0x0B, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102        ];
103
104        let decoded = Moov::decode(&mut &ENCODED[..]).unwrap();
105        assert_eq!(
106            decoded,
107            Moov {
108                mvhd: Mvhd {
109                    creation_time: 3773812694,
110                    modification_time: 3773812694,
111                    timescale: 90000,
112                    rate: 1.into(),
113                    volume: 1.into(),
114                    next_track_id: 2,
115                    ..Default::default()
116                },
117                meta: Some(Meta {
118                    hdlr: Hdlr {
119                        handler: FourCC::new(b"ID32"),
120                        name: "".into()
121                    },
122                    items: vec![Any::Unknown(
123                        FourCC::new(b"ID32"),
124                        vec![
125                            0, 0, 0, 0, 21, 199, 73, 68, 51, 4, 0, 0, 0, 0, 0, 73, 80, 82, 73, 86,
126                            0, 0, 0, 63, 0, 0, 104, 116, 116, 112, 115, 58, 47, 47, 103, 105, 116,
127                            104, 117, 98, 46, 99, 111, 109, 47, 103, 111, 111, 103, 108, 101, 47,
128                            115, 104, 97, 107, 97, 45, 112, 97, 99, 107, 97, 103, 101, 114, 0, 118,
129                            50, 46, 54, 46, 49, 45, 54, 51, 52, 97, 102, 54, 53, 45, 114, 101, 108,
130                            101, 97, 115, 101
131                        ]
132                    )]
133                }),
134                mvex: Some(Mvex {
135                    mehd: Some(Mehd {
136                        fragment_duration: 28914000
137                    }),
138                    trex: vec![Trex {
139                        track_id: 1,
140                        default_sample_description_index: 1,
141                        default_sample_duration: 3000,
142                        ..Default::default()
143                    }]
144                }),
145                trak: vec![Trak {
146                    tkhd: Tkhd {
147                        creation_time: 3773812694,
148                        modification_time: 3773812694,
149                        track_id: 1,
150                        enabled: true,
151                        width: 1920.into(),
152                        height: 1080.into(),
153                        ..Default::default()
154                    },
155                    edts: Some(Edts {
156                        elst: Some(Elst {
157                            entries: vec![ElstEntry {
158                                media_time: 6000,
159                                media_rate: 1,
160                                ..Default::default()
161                            }]
162                        })
163                    }),
164                    mdia: Mdia {
165                        mdhd: Mdhd {
166                            creation_time: 3773812694,
167                            modification_time: 3773812694,
168                            timescale: 90000,
169                            duration: 0,
170                            language: "und".to_string(),
171                        },
172                        hdlr: Hdlr {
173                            handler: b"vide".into(),
174                            name: "VideoHandler".to_string(),
175                        },
176                        minf: Minf {
177                            vmhd: Some(Vmhd {
178                                graphics_mode: 0,
179                                op_color: RgbColor {
180                                    red: 0,
181                                    green: 0,
182                                    blue: 0
183                                }
184                            }),
185                            dinf: Dinf {
186                                dref: Dref {
187                                    urls: vec![Url::default()]
188                                }
189                            },
190                            stbl: Stbl {
191                                stsd: Stsd {
192                                    codecs: vec![Avc1 {
193                                        visual: Visual {
194                                            data_reference_index: 1,
195                                            width: 1920,
196                                            height: 1080,
197                                            horizresolution: 72.into(),
198                                            vertresolution: 72.into(),
199                                            frame_count: 1,
200                                            compressor: "AVC Coding".into(),
201                                            depth: 24,
202                                        },
203                                        avcc: Avcc {
204                                            configuration_version: 1,
205                                            avc_profile_indication: 100,
206                                            profile_compatibility: 0,
207                                            avc_level_indication: 40,
208                                            length_size: 4,
209                                            sequence_parameter_sets: vec![vec![
210                                                103, 100, 0, 40, 172, 217, 64, 120, 2, 39, 229,
211                                                132, 0, 0, 3, 0, 4, 0, 0, 3, 0, 240, 60, 96, 198,
212                                                88
213                                            ]],
214                                            picture_parameter_sets: vec![vec![
215                                                104, 235, 236, 178, 44
216                                            ]],
217                                            ext: None,
218                                        },
219                                        btrt: None,
220                                        colr: None,
221                                        pasp: None,
222                                        taic: None,
223                                        fiel: None,
224                                    }
225                                    .into()],
226                                },
227                                stco: Some(Stco::default()),
228                                ..Default::default()
229                            },
230                            ..Default::default()
231                        }
232                    },
233                    ..Default::default()
234                }],
235                ..Default::default()
236            }
237        )
238    }
239}