forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB86fb0f3Tahinli7 days ago3927 commits

lib.rs95 lines, 4.2 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 inside the muxer. An mp4 sample is the codec's own
45//! bytes and [`Mp4Muxer`] writes what it is given; [`config`] holds the helpers
46//! that turn encoder output into those bytes and their sample-entry records.
47//!
48//! No async, no unsafe, no external dependencies.
49
50#![forbid(unsafe_code)]
51#![warn(missing_docs)]
52
53mod asc;
54mod boxes;
55mod demux;
56mod esds;
57mod mux;
58
59pub mod config;
60
61pub use demux::{Edit, Mp4Demuxer, Sample, TrackInfo};
62pub use esds::{Esds, object_type};
63pub use mux::Mp4Muxer;
64
65/// True when `head` looks like an ISOBMFF file: the second word of its first box
66/// is one of the types an mp4 or a `.mov` starts with.
67///
68/// `ftyp` is what every file written this century begins with; the rest are what
69/// a QuickTime file from before it does, and `styp`/`moof` are a fragment handed
70/// over on its own.
71pub fn is_mp4(head: &[u8]) -> bool {
72 let Some(kind) = head.get(4..8) else {
73 return false;
74 };
75 matches!(
76 kind,
77 b"ftyp" | b"styp" | b"moov" | b"moof" | b"mdat" | b"free" | b"skip" | b"wide" | b"pnot"
78 )
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn recognises_the_shapes_a_file_starts_with() {
87 assert!(is_mp4(b"\0\0\0\x18ftypisom"));
88 assert!(is_mp4(b"\0\0\0\x10moovxxxx"));
89 assert!(
90 !is_mp4(b"\x1aE\xdf\xa3\x01\x00\x00\x00"),
91 "that is Matroska"
92 );
93 assert!(!is_mp4(b"RIFF"), "and shorter than a header");
94 }
95}