forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB25a6b5fTahinli2 months ago3171 commits

lib.rs92 lines, 4.1 KiBHistoryRawDownload
1//! MP4/ISOBMFF (`.mp4`, `.m4v`, `.m4a`) and QuickTime (`.mov`), both directions.
2//!
3//! [`Mp4Demuxer`] reads a container into [`ec_core::Packet`]s and [`Mp4Muxer`]
4//! writes them back out; both speak the [`ec_core::Demuxer`] and
5//! [`ec_core::Muxer`] contracts, so nothing above them knows which container it
6//! is holding.
7//!
8//! Contracts worth knowing before implementing against this crate:
9//!
10//! - **The frame rate is a rational read off `stts`, never a rounded field.**
11//! Whole track over whole track, so a constant table gives exactly
12//! `timescale/delta` and a 90 kHz table spreading 3753/3754 ticks a frame
13//! gives exactly `24000/1001`. An NTSC film that arrives as 23.976 leaves as
14//! 23.976, which is the one bug this crate was written to not have.
15//! - **A sample entry is read for what it says it is.** An `mp4a` holding MP3 is
16//! an MP3 track, an `ac-3`/`ec-3`/`alac`/`Opus`/`fLaC` entry is that codec,
17//! and an entry this build has no [`ec_core::CodecId`] for leaves its track
18//! listed as nothing rather than decoded as something else.
19//! - **QuickTime's own sound descriptions are read as such.** A `.mov` out of
20//! Resolve, Final Cut or `ffmpeg -f mov` states `mp4a` as a version-1 or
21//! version-2 SoundDescription — the second one carrying its sample rate as a
22//! double and its channel count as a 32-bit field — and keeps the `esds`
23//! inside a `wave` box rather than directly under the entry. Both shapes give
24//! the same codec, rate, channels and `AudioSpecificConfig` as the ISO
25//! branding of the same stream; a reader that walks past `wave` has a silent
26//! sound track instead.
27//! - **`esds` hands back bytes.** [`Esds::decoder_specific`] is the raw
28//! `DecoderSpecificInfo` — an AAC track's `AudioSpecificConfig` verbatim, not
29//! a triplet of parsed fields a caller has to reassemble.
30//! - **The edit list shifts, it never trims.** An initial empty edit is a delay
31//! and the first real edit's `media_time` is where the media starts; no sample
32//! is ever dropped for either, because a muxer writes `media_time` equal to
33//! the first composition offset and reading *that* as a trim throws away real
34//! pictures.
35//! - **Malformed input is an error, never a panic.** Box sizes are checked
36//! against their parent, table entry counts against the box that holds them
37//! and the sample count against the length of the file, so a crafted header
38//! cannot allocate the machine. `forbid(unsafe_code)`, and no arithmetic on a
39//! parsed number that is not checked or saturating.
40//! - **The muxer writes `mdat` first and `moov` last**, in one pass; see
41//! [`Mp4Muxer`] for what that costs and what it buys.
42//!
43//! What this crate deliberately does *not* do: convert between Annex-B and
44//! length-prefixed samples. An mp4 sample is the codec's own bytes and this
45//! writes what it is given — the bitstream-format question belongs to the codec
46//! crates, not to the container.
47//!
48//! No async, no unsafe, no external dependencies.
49
50#![forbid(unsafe_code)]
51#![warn(missing_docs)]
52
53mod boxes;
54mod demux;
55mod esds;
56mod mux;
57
58pub use demux::{Mp4Demuxer, Sample, TrackInfo};
59pub use esds::{Esds, object_type};
60pub use mux::Mp4Muxer;
61
62/// True when `head` looks like an ISOBMFF file: the second word of its first box
63/// is one of the types an mp4 or a `.mov` starts with.
64///
65/// `ftyp` is what every file written this century begins with; the rest are what
66/// a QuickTime file from before it does, and `styp`/`moof` are a fragment handed
67/// over on its own.
68pub fn is_mp4(head: &[u8]) -> bool {
69 let Some(kind) = head.get(4..8) else {
70 return false;
71 };
72 matches!(
73 kind,
74 b"ftyp" | b"styp" | b"moov" | b"moof" | b"mdat" | b"free" | b"skip" | b"wide" | b"pnot"
75 )
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn recognises_the_shapes_a_file_starts_with() {
84 assert!(is_mp4(b"\0\0\0\x18ftypisom"));
85 assert!(is_mp4(b"\0\0\0\x10moovxxxx"));
86 assert!(
87 !is_mp4(b"\x1aE\xdf\xa3\x01\x00\x00\x00"),
88 "that is Matroska"
89 );
90 assert!(!is_mp4(b"RIFF"), "and shorter than a header");
91 }
92}