forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB552edc4Tahinli2 months ago2048 commits

lib.rs51 lines, 2.5 KiBHistoryRawDownload
1//! Matroska (`.mkv`, `.mka`, `.mks`, `.mk3d`) and WebM, both directions.
2//!
3//! [`MatroskaDemuxer`] reads a container into [`ec_core::Packet`]s and
4//! [`MatroskaMuxer`] writes them back out; both speak the [`ec_core::Demuxer`]
5//! and [`ec_core::Muxer`] contracts, so nothing above them knows which container
6//! it is holding.
7//!
8//! Contracts worth knowing before implementing against this crate:
9//!
10//! - **Seeking uses the reader it was opened with.** `Cues` are read once, on
11//! the first seek — from in front of the clusters where a muxer put them
12//! there, through the `SeekHead` where it put them at the far end, and from a
13//! walk of the cluster headers for a file that has no index at all. A seek
14//! then lands on a random-access point at or before the target
15//! ([`ec_core::SeekMode::SyncBefore`]) without ever reopening anything.
16//! - **Timing is integer.** `TimestampScale` becomes an exact
17//! [`ec_core::TimeBase`] (`1/1000` for the millisecond every muxer writes) and
18//! every packet carries it; the frame rate comes off `DefaultDuration`, which
19//! is the only exact statement of it a Matroska file makes.
20//! - **Packets are zero-copy.** One cluster is read into one allocation and
21//! every packet of it is a [`ec_core::Buf`] slice of that allocation — a
22//! refcount bump, not a copy. A track under `ContentEncodings` is the one
23//! exception: unpacking it has to build the frame it hands back.
24//! - **A compressed track is never read as if it were plain.** zlib and header
25//! stripping are undone; anything else — encryption, bzlib, lzo1x — is refused
26//! by name, because a compressed track read raw decodes into garbage and that
27//! is the one thing this must not do quietly.
28//! - **Malformed input is an error, never a panic.** A cluster whose element
29//! chain breaks is resynchronised on the next `Cluster` id, so a truncated
30//! download plays its prefix.
31//!
32//! No async, no unsafe.
33
34#![forbid(unsafe_code)]
35#![warn(missing_docs)]
36
37mod demux;
38mod ebml;
39mod mux;
40
41pub use demux::MatroskaDemuxer;
42pub use mux::{MAX_TRACKS, MUX_TIME_BASE, MatroskaMuxer};
43
44/// True when `head` starts with an EBML header, which is what a Matroska or
45/// WebM file begins with and what a probe needs to see to hand a file here.
46///
47/// The doc type is checked by [`MatroskaDemuxer::new`], not here: this answers
48/// "EBML", and a `.webm` and a `.mkv` are the same four magic bytes.
49pub fn is_matroska(head: &[u8]) -> bool {
50 head.starts_with(&[0x1A, 0x45, 0xDF, 0xA3])
51}