mp4_atom/moov/
mvhd.rs

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

ext! {
    name: Mvhd,
    versions: [0,1],
    flags: {}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mvhd {
    pub creation_time: u64,
    pub modification_time: u64,
    pub timescale: u32,
    pub duration: u64,

    pub rate: FixedPoint<u16>,
    pub volume: FixedPoint<u8>,

    pub matrix: Matrix,
    pub next_track_id: u32,
}

impl AtomExt for Mvhd {
    const KIND_EXT: FourCC = FourCC::new(b"mvhd");

    type Ext = MvhdExt;

    fn decode_body_ext(buf: &mut Bytes, ext: MvhdExt) -> Result<Self> {
        let (creation_time, modification_time, timescale, duration) = match ext.version {
            MvhdVersion::V1 => (
                u64::decode(buf)?,
                u64::decode(buf)?,
                u32::decode(buf)?,
                u64::decode(buf)?,
            ),
            MvhdVersion::V0 => (
                u32::decode(buf)? as u64,
                u32::decode(buf)? as u64,
                u32::decode(buf)?,
                u32::decode(buf)? as u64,
            ),
        };

        let rate = buf.decode()?;
        let volume = buf.decode()?;

        u16::decode(buf)?; // reserved
        u64::decode(buf)?; // reserved

        let matrix = buf.decode()?;

        <[u8; 24]>::decode(buf)?; // pre_defined = 0

        let next_track_id = buf.decode()?;

        Ok(Mvhd {
            creation_time,
            modification_time,
            timescale,
            duration,
            rate,
            volume,
            matrix,
            next_track_id,
        })
    }

    fn encode_body_ext(&self, buf: &mut BytesMut) -> Result<MvhdExt> {
        self.creation_time.encode(buf)?;
        self.modification_time.encode(buf)?;
        self.timescale.encode(buf)?;
        self.duration.encode(buf)?;

        self.rate.encode(buf)?;
        self.volume.encode(buf)?;

        0u16.encode(buf)?; // reserved
        0u64.encode(buf)?; // reserved

        self.matrix.encode(buf)?;

        [0u8; 24].encode(buf)?; // pre_defined = 0

        self.next_track_id.encode(buf)?;

        Ok(MvhdVersion::V1.into())
    }
}

impl Default for Mvhd {
    fn default() -> Self {
        Mvhd {
            creation_time: 0,
            modification_time: 0,
            timescale: 1000,
            duration: 0,
            rate: Default::default(),
            matrix: Default::default(),
            volume: Default::default(),
            next_track_id: 1,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mvhd32() {
        let expected = Mvhd {
            creation_time: 100,
            modification_time: 200,
            timescale: 1000,
            duration: 634634,
            rate: 1.into(),
            volume: 1.into(),
            matrix: Matrix::default(),
            next_track_id: 1,
        };

        let mut buf = BytesMut::new();
        expected.encode(&mut buf).unwrap();

        let mut buf = buf.freeze();
        let decoded = Mvhd::decode(&mut buf).unwrap();
        assert_eq!(decoded, expected);
    }

    #[test]
    fn test_mvhd64() {
        let expected = Mvhd {
            creation_time: 100,
            modification_time: 200,
            timescale: 1000,
            duration: 634634,
            rate: 1.into(),
            volume: 1.into(),
            matrix: Matrix::default(),
            next_track_id: 1,
        };

        let mut buf = BytesMut::new();
        expected.encode(&mut buf).unwrap();

        let mut buf = buf.freeze();
        let output = Mvhd::decode(&mut buf).unwrap();
        assert_eq!(output, expected);
    }
}