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

ext! {
    name: Tkhd,
    versions: [0, 1],
    flags: {
        track_enabled = 0,
        track_in_movie = 1,
        track_in_preview = 2,
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Tkhd {
    pub creation_time: u64,
    pub modification_time: u64,
    pub track_id: u32,
    pub duration: u64,
    pub layer: u16,
    pub alternate_group: u16,
    pub enabled: bool,

    pub volume: FixedPoint<u8>,
    pub matrix: Matrix,

    pub width: FixedPoint<u16>,
    pub height: FixedPoint<u16>,
}

impl AtomExt for Tkhd {
    const KIND_EXT: FourCC = FourCC::new(b"tkhd");

    type Ext = TkhdExt;

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

        u64::decode(buf)?; // reserved
        let layer = buf.decode()?;
        let alternate_group = buf.decode()?;
        let volume = buf.decode()?;

        u16::decode(buf)?; // reserved
        let matrix = Matrix::decode(buf)?;
        let width = buf.decode()?;
        let height = buf.decode()?;

        Ok(Tkhd {
            creation_time,
            modification_time,
            track_id,
            duration,
            layer,
            alternate_group,
            volume,
            matrix,
            width,
            height,
            enabled: ext.track_enabled,
        })
    }

    fn encode_body_ext(&self, buf: &mut BytesMut) -> Result<TkhdExt> {
        self.creation_time.encode(buf)?;
        self.modification_time.encode(buf)?;
        self.track_id.encode(buf)?;
        0u32.encode(buf)?; // reserved
        self.duration.encode(buf)?;

        0u64.encode(buf)?; // reserved
        self.layer.encode(buf)?;
        self.alternate_group.encode(buf)?;
        self.volume.encode(buf)?;
        0u16.encode(buf)?; // reserved
        self.matrix.encode(buf)?;

        self.width.encode(buf)?;
        self.height.encode(buf)?;

        Ok(TkhdExt {
            version: TkhdVersion::V1,
            track_enabled: self.enabled,
            ..Default::default()
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Matrix {
    pub a: i32,
    pub b: i32,
    pub u: i32,
    pub c: i32,
    pub d: i32,
    pub v: i32,
    pub x: i32,
    pub y: i32,
    pub w: i32,
}

impl Decode for Matrix {
    fn decode(buf: &mut Bytes) -> Result<Self> {
        Ok(Self {
            a: buf.decode()?,
            b: buf.decode()?,
            u: buf.decode()?,
            c: buf.decode()?,
            d: buf.decode()?,
            v: buf.decode()?,
            x: buf.decode()?,
            y: buf.decode()?,
            w: buf.decode()?,
        })
    }
}

impl Encode for Matrix {
    fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        self.a.encode(buf)?;
        self.b.encode(buf)?;
        self.u.encode(buf)?;
        self.c.encode(buf)?;
        self.d.encode(buf)?;
        self.v.encode(buf)?;
        self.x.encode(buf)?;
        self.y.encode(buf)?;
        self.w.encode(buf)
    }
}

impl std::fmt::Display for Matrix {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:#x} {:#x} {:#x} {:#x} {:#x} {:#x} {:#x} {:#x} {:#x}",
            self.a, self.b, self.u, self.c, self.d, self.v, self.x, self.y, self.w
        )
    }
}

impl Default for Matrix {
    fn default() -> Self {
        Self {
            // unity matrix according to ISO/IEC 14496-12:2005(E)
            a: 0x00010000,
            b: 0,
            u: 0,
            c: 0,
            d: 0x00010000,
            v: 0,
            x: 0,
            y: 0,
            w: 0x40000000,
        }
    }
}

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

    #[test]
    fn test_tkhd32() {
        let expected = Tkhd {
            creation_time: 100,
            modification_time: 200,
            track_id: 1,
            duration: 634634,
            layer: 0,
            alternate_group: 0,
            volume: 1.into(),
            matrix: Matrix::default(),
            width: 512.into(),
            height: 288.into(),
            enabled: true,
        };
        let mut buf = BytesMut::new();
        expected.encode(&mut buf).unwrap();

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

    #[test]
    fn test_tkhd64() {
        let expected = Tkhd {
            creation_time: 100,
            modification_time: 200,
            track_id: 1,
            duration: 634634,
            layer: 0,
            alternate_group: 0,
            volume: 1.into(),
            matrix: Matrix::default(),
            width: 512.into(),
            height: 288.into(),
            enabled: true,
        };
        let mut buf = BytesMut::new();
        expected.encode(&mut buf).unwrap();

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