mp4-track 0.1.0

Track-level MP4 reader and progressive writer with bounded parsing
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use crate::{Error, FourCc, Limits};
use mp4_atom::ReadFrom as _;

pub(crate) const FTYP: FourCc = FourCc(*b"ftyp");
pub(crate) const MOOV: FourCc = FourCc(*b"moov");
pub(crate) const MOOF: FourCc = FourCc(*b"moof");
pub(crate) const MDAT: FourCc = FourCc(*b"mdat");

pub(crate) fn validate_typed_atom(kind: FourCc, body: &[u8]) -> Result<(), Error> {
    let size = body.len().checked_add(8).ok_or(Error::LimitExceeded {
        limit: "atom_size",
        value: u64::MAX,
        max: u64::from(u32::MAX),
    })?;
    let size = u32::try_from(size).map_err(|_| Error::LimitExceeded {
        limit: "atom_size",
        value: u64::try_from(body.len()).unwrap_or(u64::MAX),
        max: u64::from(u32::MAX).saturating_sub(8),
    })?;
    let mut encoded = Vec::with_capacity(usize::try_from(size).unwrap_or(0));
    encoded.extend_from_slice(&size.to_be_bytes());
    encoded.extend_from_slice(&kind.0);
    encoded.extend_from_slice(body);
    let mut cursor = std::io::Cursor::new(encoded);
    match mp4_atom::Any::read_from(&mut cursor) {
        Ok(_) => Ok(()),
        // This crate tolerates trailing bytes inside a box it has already
        // parsed, so a partial decode by the box layer is not a failure.
        Err(mp4_atom::Error::UnderDecode(_)) => Ok(()),
        Err(mp4_atom::Error::OverDecode(inner)) => Err(Error::Malformed {
            kind: FourCc(<[u8; 4]>::from(inner)),
            reason: "declared content exceeds the box body",
        }),
        Err(error) => Err(Error::Atom(error)),
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct BoxHeader {
    pub(crate) kind: FourCc,
    pub(crate) start: usize,
    pub(crate) header_len: usize,
    pub(crate) size: usize,
}

impl BoxHeader {
    pub(crate) fn body_start(self) -> Result<usize, Error> {
        self.start
            .checked_add(self.header_len)
            .ok_or(Error::Malformed {
                kind: self.kind,
                reason: "box body offset overflow",
            })
    }

    pub(crate) fn end(self) -> Result<usize, Error> {
        self.start.checked_add(self.size).ok_or(Error::Malformed {
            kind: self.kind,
            reason: "box end overflow",
        })
    }

    pub(crate) fn body(self, data: &[u8]) -> Result<&[u8], Error> {
        let start = self.body_start()?;
        let end = self.end()?;
        data.get(start..end).ok_or(Error::Malformed {
            kind: self.kind,
            reason: "box body is outside its parent",
        })
    }
}

fn u32_at(data: &[u8], at: usize) -> Option<u32> {
    let bytes: [u8; 4] = data.get(at..at.checked_add(4)?)?.try_into().ok()?;
    Some(u32::from_be_bytes(bytes))
}

fn u64_at(data: &[u8], at: usize) -> Option<u64> {
    let bytes: [u8; 8] = data.get(at..at.checked_add(8)?)?.try_into().ok()?;
    Some(u64::from_be_bytes(bytes))
}

pub(crate) fn parse_header(
    data: &[u8],
    at: usize,
    parent: Option<FourCc>,
    absolute_base: u64,
    allow_zero: bool,
) -> Result<BoxHeader, Error> {
    let absolute = absolute_base
        .checked_add(u64::try_from(at).map_err(|_| Error::Malformed {
            kind: parent.unwrap_or(FourCc(*b"root")),
            reason: "offset does not fit u64",
        })?)
        .ok_or(Error::Malformed {
            kind: parent.unwrap_or(FourCc(*b"root")),
            reason: "absolute offset overflow",
        })?;
    let remaining = data.len().saturating_sub(at);
    if remaining < 8 {
        return Err(Error::Truncated {
            at: absolute,
            needed: 8,
        });
    }
    let size32 = u32_at(data, at).ok_or(Error::Truncated {
        at: absolute,
        needed: 4,
    })?;
    let kind_at = at.checked_add(4).ok_or(Error::Truncated {
        at: absolute,
        needed: 8,
    })?;
    let kind_bytes: [u8; 4] = data
        .get(
            kind_at..kind_at.checked_add(4).ok_or(Error::Truncated {
                at: absolute,
                needed: 8,
            })?,
        )
        .ok_or(Error::Truncated {
            at: absolute,
            needed: 8,
        })?
        .try_into()
        .map_err(|_| Error::Truncated {
            at: absolute,
            needed: 8,
        })?;
    let kind = FourCc(kind_bytes);
    let (mut size, mut header_len) = match size32 {
        0 => {
            if !allow_zero {
                return Err(Error::ZeroSizeNested { kind, at: absolute });
            }
            (remaining, 8usize)
        }
        1 => {
            if remaining < 16 {
                return Err(Error::Truncated {
                    at: absolute,
                    needed: 16,
                });
            }
            let large_at = at.checked_add(8).ok_or(Error::Truncated {
                at: absolute,
                needed: 16,
            })?;
            let large = u64_at(data, large_at).ok_or(Error::Truncated {
                at: absolute,
                needed: 16,
            })?;
            let converted = usize::try_from(large).map_err(|_| Error::BoxExceedsFile {
                kind,
                at: absolute,
                declared_end: u64::MAX,
                file_len: u64::try_from(data.len()).unwrap_or(u64::MAX),
            })?;
            (converted, 16usize)
        }
        value => (
            usize::try_from(value).map_err(|_| Error::BoxSizeTooSmall {
                kind,
                size: u64::from(value),
                at: absolute,
            })?,
            8usize,
        ),
    };
    if kind == FourCc(*b"uuid") {
        header_len = header_len.checked_add(16).ok_or(Error::BoxSizeTooSmall {
            kind,
            size: u64::try_from(size).unwrap_or(u64::MAX),
            at: absolute,
        })?;
    }
    if size < header_len {
        return Err(Error::BoxSizeTooSmall {
            kind,
            size: u64::try_from(size).unwrap_or(u64::MAX),
            at: absolute,
        });
    }
    if size32 == 0 {
        size = remaining;
    }
    Ok(BoxHeader {
        kind,
        start: at,
        header_len,
        size,
    })
}

pub(crate) fn children(
    body: &[u8],
    parent: FourCc,
    absolute_base: u64,
) -> Result<Vec<(BoxHeader, &[u8])>, Error> {
    let mut result = Vec::new();
    let mut at = 0usize;
    while at < body.len() {
        let header = parse_header(body, at, Some(parent), absolute_base, false)?;
        let end = header.end()?;
        if end > body.len() {
            let child_at = absolute_base.saturating_add(u64::try_from(at).unwrap_or(u64::MAX));
            return Err(Error::ChildExceedsParent {
                parent,
                child: header.kind,
                at: child_at,
            });
        }
        result.push((header, header.body(body)?));
        at = end;
    }
    Ok(result)
}

fn is_container(kind: FourCc) -> bool {
    [
        FourCc(*b"moov"),
        FourCc(*b"trak"),
        FourCc(*b"mdia"),
        FourCc(*b"minf"),
        FourCc(*b"stbl"),
        FourCc(*b"edts"),
        FourCc(*b"dinf"),
        FourCc(*b"mvex"),
        FourCc(*b"moof"),
        FourCc(*b"traf"),
        FourCc(*b"udta"),
        FourCc(*b"ilst"),
    ]
    .contains(&kind)
}

pub(crate) fn validate_tree(
    data: &[u8],
    root: FourCc,
    absolute_base: u64,
    limits: Limits,
) -> Result<(), Error> {
    validate_children(data, root, absolute_base, 1, limits)
}

fn validate_children(
    data: &[u8],
    parent: FourCc,
    absolute_base: u64,
    depth: u8,
    limits: Limits,
) -> Result<(), Error> {
    if depth > limits.max_depth {
        return Err(Error::NestingTooDeep {
            depth,
            at: absolute_base,
        });
    }
    let mut at = 0usize;
    while at < data.len() {
        let header = parse_header(data, at, Some(parent), absolute_base, false)?;
        let end = header.end()?;
        if end > data.len() {
            return Err(Error::ChildExceedsParent {
                parent,
                child: header.kind,
                at: absolute_base.saturating_add(u64::try_from(at).unwrap_or(u64::MAX)),
            });
        }
        if is_container(header.kind) {
            let body = header.body(data)?;
            let child_base = absolute_base
                .checked_add(u64::try_from(header.body_start()?).unwrap_or(u64::MAX))
                .ok_or(Error::Malformed {
                    kind: header.kind,
                    reason: "absolute child offset overflow",
                })?;
            validate_children(
                body,
                header.kind,
                child_base,
                depth.checked_add(1).ok_or(Error::NestingTooDeep {
                    depth: u8::MAX,
                    at: child_base,
                })?,
                limits,
            )?;
        } else if header.kind == FourCc(*b"meta") {
            let body = header.body(data)?;
            let nested = body.get(4..).ok_or(Error::Malformed {
                kind: header.kind,
                reason: "missing full-box fields",
            })?;
            let child_base = absolute_base
                .checked_add(
                    u64::try_from(header.body_start()?.saturating_add(4)).unwrap_or(u64::MAX),
                )
                .ok_or(Error::Malformed {
                    kind: header.kind,
                    reason: "absolute child offset overflow",
                })?;
            validate_children(
                nested,
                header.kind,
                child_base,
                depth.checked_add(1).ok_or(Error::NestingTooDeep {
                    depth: u8::MAX,
                    at: child_base,
                })?,
                limits,
            )?;
        }
        at = end;
    }
    Ok(())
}

pub(crate) struct Cursor<'a> {
    data: &'a [u8],
    pos: usize,
    kind: FourCc,
}

impl<'a> Cursor<'a> {
    pub(crate) fn new(data: &'a [u8], kind: FourCc) -> Self {
        Self { data, pos: 0, kind }
    }

    pub(crate) fn position(&self) -> usize {
        self.pos
    }

    pub(crate) fn remaining(&self) -> usize {
        self.data.len().saturating_sub(self.pos)
    }

    pub(crate) fn skip(&mut self, count: usize) -> Result<(), Error> {
        self.take(count).map(|_| ())
    }

    pub(crate) fn take(&mut self, count: usize) -> Result<&'a [u8], Error> {
        let end = self.pos.checked_add(count).ok_or(Error::Malformed {
            kind: self.kind,
            reason: "field offset overflow",
        })?;
        let value = self.data.get(self.pos..end).ok_or(Error::Malformed {
            kind: self.kind,
            reason: "box field is truncated",
        })?;
        self.pos = end;
        Ok(value)
    }

    pub(crate) fn rest(&mut self) -> &'a [u8] {
        let value = self.data.get(self.pos..).unwrap_or_default();
        self.pos = self.data.len();
        value
    }

    pub(crate) fn u8(&mut self) -> Result<u8, Error> {
        self.take(1)?.first().copied().ok_or(Error::Malformed {
            kind: self.kind,
            reason: "missing byte",
        })
    }

    pub(crate) fn u16(&mut self) -> Result<u16, Error> {
        let bytes: [u8; 2] = self.take(2)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing u16",
        })?;
        Ok(u16::from_be_bytes(bytes))
    }

    pub(crate) fn i16(&mut self) -> Result<i16, Error> {
        let bytes: [u8; 2] = self.take(2)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing i16",
        })?;
        Ok(i16::from_be_bytes(bytes))
    }

    pub(crate) fn u24(&mut self) -> Result<u32, Error> {
        let bytes = self.take(3)?;
        let a = u32::from(bytes.first().copied().unwrap_or_default());
        let b = u32::from(bytes.get(1).copied().unwrap_or_default());
        let c = u32::from(bytes.get(2).copied().unwrap_or_default());
        Ok((a << 16) | (b << 8) | c)
    }

    pub(crate) fn u32(&mut self) -> Result<u32, Error> {
        let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing u32",
        })?;
        Ok(u32::from_be_bytes(bytes))
    }

    pub(crate) fn i32(&mut self) -> Result<i32, Error> {
        let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing i32",
        })?;
        Ok(i32::from_be_bytes(bytes))
    }

    pub(crate) fn u64(&mut self) -> Result<u64, Error> {
        let bytes: [u8; 8] = self.take(8)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing u64",
        })?;
        Ok(u64::from_be_bytes(bytes))
    }

    pub(crate) fn i64(&mut self) -> Result<i64, Error> {
        let bytes: [u8; 8] = self.take(8)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing i64",
        })?;
        Ok(i64::from_be_bytes(bytes))
    }

    pub(crate) fn fourcc(&mut self) -> Result<FourCc, Error> {
        let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| Error::Malformed {
            kind: self.kind,
            reason: "missing fourcc",
        })?;
        Ok(FourCc(bytes))
    }

    pub(crate) fn full_box(&mut self) -> Result<(u8, u32), Error> {
        Ok((self.u8()?, self.u24()?))
    }
}

pub(crate) fn one<'a>(
    items: &[(BoxHeader, &'a [u8])],
    kind: FourCc,
    parent: FourCc,
    required: bool,
) -> Result<Option<&'a [u8]>, Error> {
    let mut found = items.iter().filter(|(header, _)| header.kind == kind);
    let first = found.next().map(|(_, body)| *body);
    if found.next().is_some() {
        return Err(Error::DuplicateBox { kind, parent });
    }
    if required && first.is_none() {
        return Err(Error::MissingBox {
            kind,
            parent: Some(parent),
        });
    }
    Ok(first)
}