forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB070e7ebTahinli3 months ago374 commits

lib.rs84 lines, 3.5 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//! - **`esds` hands back bytes.** [`Esds::decoder_specific`] is the raw
20//! `DecoderSpecificInfo` — an AAC track's `AudioSpecificConfig` verbatim, not
21//! a triplet of parsed fields a caller has to reassemble.
22//! - **The edit list shifts, it never trims.** An initial empty edit is a delay
23//! and the first real edit's `media_time` is where the media starts; no sample
24//! is ever dropped for either, because a muxer writes `media_time` equal to
25//! the first composition offset and reading *that* as a trim throws away real
26//! pictures.
27//! - **Malformed input is an error, never a panic.** Box sizes are checked
28//! against their parent, table entry counts against the box that holds them
29//! and the sample count against the length of the file, so a crafted header
30//! cannot allocate the machine. `forbid(unsafe_code)`, and no arithmetic on a
31//! parsed number that is not checked or saturating.
32//! - **The muxer writes `mdat` first and `moov` last**, in one pass; see
33//! [`Mp4Muxer`] for what that costs and what it buys.
34//!
35//! What this crate deliberately does *not* do: convert between Annex-B and
36//! length-prefixed samples. An mp4 sample is the codec's own bytes and this
37//! writes what it is given — the bitstream-format question belongs to the codec
38//! crates, not to the container.
39//!
40//! No async, no unsafe, no external dependencies.
41
42#![forbid(unsafe_code)]
43#![warn(missing_docs)]
44
45mod boxes;
46mod demux;
47mod esds;
48mod mux;
49
50pub use demux::{Mp4Demuxer, Sample, TrackInfo};
51pub use esds::{Esds, object_type};
52pub use mux::Mp4Muxer;
53
54/// True when `head` looks like an ISOBMFF file: the second word of its first box
55/// is one of the types an mp4 or a `.mov` starts with.
56///
57/// `ftyp` is what every file written this century begins with; the rest are what
58/// a QuickTime file from before it does, and `styp`/`moof` are a fragment handed
59/// over on its own.
60pub fn is_mp4(head: &[u8]) -> bool {
61 let Some(kind) = head.get(4..8) else {
62 return false;
63 };
64 matches!(
65 kind,
66 b"ftyp" | b"styp" | b"moov" | b"moof" | b"mdat" | b"free" | b"skip" | b"wide" | b"pnot"
67 )
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn recognises_the_shapes_a_file_starts_with() {
76 assert!(is_mp4(b"\0\0\0\x18ftypisom"));
77 assert!(is_mp4(b"\0\0\0\x10moovxxxx"));
78 assert!(
79 !is_mp4(b"\x1aE\xdf\xa3\x01\x00\x00\x00"),
80 "that is Matroska"
81 );
82 assert!(!is_mp4(b"RIFF"), "and shorter than a header");
83 }
84}