rff_format/lib.rs
1//! `rff-format` — the container (muxer/demuxer) abstraction layer (FFmpeg's
2//! `libavformat` core, minus the formats themselves).
3//!
4//! A container is the file wrapper (AVI, MP4, Matroska, ...) that interleaves
5//! one or more elementary streams. This crate defines:
6//! * [`Stream`] — the description of one elementary stream inside a container,
7//! * [`Demuxer`] — reads a container, exposing streams and [`Packet`]s,
8//! * [`Muxer`] — writes streams + packets back into a container,
9//! * [`FormatRegistry`] — what individual format crates register into.
10//!
11//! Concrete formats live in their own crates (`rff-format-avi`, ...).
12
13pub mod aac;
14pub mod av1;
15pub mod avc;
16pub mod hvc;
17
18use std::collections::HashMap;
19use std::io::{Read, Seek, Write};
20
21use rff_core::{
22 CleanAperture, CodecId, ColorInfo, ColorRange, ContentLightLevel, Dictionary, Error,
23 MasteringDisplay, MediaType, Packet, PixelFormat, Rational, Result, SampleFormat, Timecode,
24};
25
26/// Description of one elementary stream within a container.
27#[derive(Debug, Clone)]
28pub struct Stream {
29 /// Position of this stream within its container.
30 pub index: usize,
31 pub codec_id: CodecId,
32 pub media_type: MediaType,
33 /// Unit of all timestamps on packets of this stream.
34 pub time_base: Rational,
35 // --- Video parameters (zero/ignored for non-video) ---
36 pub width: u32,
37 pub height: u32,
38 /// Raw pixel layout, when known (raw video). Self-describing codecs leave this `None`.
39 pub pixel_format: Option<PixelFormat>,
40 /// Luma/chroma value range. JPEG and PNG sources are always
41 /// [`ColorRange::Full`]; leaving it [`Unspecified`](ColorRange::Unspecified)
42 /// makes containers label the data limited-range, which silently corrupts it.
43 pub color_range: ColorRange,
44 // --- Audio parameters (zero/ignored for non-audio) ---
45 pub sample_rate: u32,
46 pub channels: u16,
47 /// Raw sample layout, when known (PCM). Self-describing codecs leave this `None`.
48 pub sample_format: Option<SampleFormat>,
49 /// Codec-private initialization data (e.g. H.264 SPS/PPS, OpusHead). Empty
50 /// for codecs whose bitstream is fully self-describing.
51 pub extradata: Vec<u8>,
52 /// Total stream duration in [`time_base`](Stream::time_base) units, when
53 /// the container declares it (MP4 `mdhd`, Matroska `Duration`). `None` when
54 /// unknown (live streams, TS).
55 pub duration: Option<i64>,
56 /// Total packet count (frames for video), when the container's index
57 /// declares it. `None` when unknown — never a guess.
58 pub nb_frames: Option<u64>,
59 /// The container's own code for this codec, when it has one worth
60 /// keeping: the MP4/QuickTime sample-entry fourcc (`apch` vs `apcn` names
61 /// the ProRes profile, `twos` vs `sowt` the PCM byte order on disk). A
62 /// muxer of the same family writes it back when it is valid for the
63 /// codec, so a remux keeps the original entry; `None` lets the muxer pick.
64 pub codec_tag: Option<[u8; 4]>,
65 // --- Video description (containers label; they do not convert) ---
66 /// Colour primaries / transfer / matrix (MP4 `colr`, Matroska `Colour`).
67 pub color: ColorInfo,
68 /// HDR mastering display (MP4 `mdcv`, Matroska `MasteringMetadata`).
69 pub mastering_display: Option<MasteringDisplay>,
70 /// HDR content light level (MP4 `clli`, Matroska `MaxCLL`/`MaxFALL`).
71 pub content_light: Option<ContentLightLevel>,
72 /// Pixel aspect ratio as `(horizontal, vertical)` spacing (MP4 `pasp`).
73 /// `None` when not recorded, which means square.
74 pub pixel_aspect: Option<(u32, u32)>,
75 /// Clean aperture (MP4 `clap`).
76 pub clean_aperture: Option<CleanAperture>,
77 /// Start timecode (a QuickTime `tmcd` track referenced by this video track).
78 pub timecode: Option<Timecode>,
79 /// The frames carry an alpha plane as a block addition (Matroska
80 /// `AlphaMode`; WebM VP8/VP9 alpha).
81 pub alpha_mode: bool,
82 // --- Audio timing the container records beside the packets ---
83 /// Samples the decoder must drop from the start, in nanoseconds
84 /// (Matroska `CodecDelay`; Opus pre-skip). The demuxer has already taken
85 /// it off packet timestamps; it is here so a remux can write it back.
86 pub codec_delay_ns: u64,
87 /// How far before a seek target decoding must start to converge, in
88 /// nanoseconds (Matroska `SeekPreRoll`; 80 ms for Opus).
89 pub seek_preroll_ns: u64,
90}
91
92impl Stream {
93 /// Create a stream with sane defaults for the given codec.
94 pub fn new(index: usize, codec_id: CodecId) -> Stream {
95 Stream {
96 index,
97 codec_id,
98 media_type: codec_id.media_type(),
99 time_base: Rational::new(1, 1000),
100 width: 0,
101 height: 0,
102 pixel_format: None,
103 color_range: ColorRange::Unspecified,
104 sample_rate: 0,
105 channels: 0,
106 sample_format: None,
107 extradata: Vec::new(),
108 duration: None,
109 nb_frames: None,
110 codec_tag: None,
111 color: ColorInfo::UNSPECIFIED,
112 mastering_display: None,
113 content_light: None,
114 pixel_aspect: None,
115 clean_aperture: None,
116 timecode: None,
117 alpha_mode: false,
118 codec_delay_ns: 0,
119 seek_preroll_ns: 0,
120 }
121 }
122}
123
124/// A byte source a demuxer reads from. (`Box<dyn Read + Send>` keeps the trait
125/// object-safe and lets inputs be files, network streams, or in-memory buffers.)
126pub type Input = Box<dyn Read + Send>;
127
128/// A byte source that can also seek — what a local file or an in-memory buffer
129/// offers and a pipe or socket does not. Blanket-implemented for every
130/// `Read + Seek` type.
131pub trait ReadSeek: Read + Seek {}
132impl<T: Read + Seek> ReadSeek for T {}
133
134/// A seekable [`Input`]. Formats whose index can sit anywhere in the file (MP4's
135/// `moov`) use it to read only that index and fetch each payload on demand,
136/// instead of buffering the whole file. It upcasts to [`Input`] for formats
137/// that only stream.
138pub type SeekInput = Box<dyn ReadSeek + Send>;
139/// A byte sink a muxer writes to.
140pub type Output = Box<dyn Write + Send>;
141
142/// A byte sink that can also seek -- a local file, an in-memory buffer; not a
143/// pipe or socket. Blanket-implemented for every `Write + Seek` type.
144pub trait WriteSeek: Write + Seek {}
145impl<T: Write + Seek> WriteSeek for T {}
146
147/// A seekable [`Output`]. Containers whose header records sizes or an index
148/// that is only known at the end (MP4 `mdat`/`moov`, Matroska Segment/Cues,
149/// AVI `movi`/`idx1`, WAV `RIFF`/`data`) use it to stream the payload as it
150/// arrives and patch the header afterwards, instead of holding the whole
151/// output in memory until the trailer (#28). It upcasts to [`Output`] for
152/// muxers that only stream.
153pub type SeekOutput = Box<dyn WriteSeek + Send>;
154
155/// Reads a container format: parses the header into [`Stream`]s, then yields
156/// [`Packet`]s until the input is exhausted.
157pub trait Demuxer: Send {
158 /// Parse the container header and return its streams.
159 fn read_header(&mut self) -> Result<Vec<Stream>>;
160
161 /// Read the next packet. Returns [`Error::Eof`] at end of input.
162 fn read_packet(&mut self) -> Result<Packet>;
163}
164
165/// Writes a container format: declare streams, write packets, finalize.
166pub trait Muxer: Send {
167 /// Hand the muxer output metadata (`-metadata title=...`). Called before
168 /// [`write_header`](Muxer::write_header); formats without a metadata slot
169 /// simply ignore it (the default).
170 fn set_metadata(&mut self, _metadata: &Dictionary) {}
171
172 /// Write the container header for the given set of streams.
173 fn write_header(&mut self, streams: &[Stream]) -> Result<()>;
174
175 /// Write one packet (its `stream_index` selects the target stream).
176 fn write_packet(&mut self, packet: &Packet) -> Result<()>;
177
178 /// Finalize the container (indexes, trailers, fixups).
179 fn write_trailer(&mut self) -> Result<()>;
180}
181
182/// Factory opening a demuxer over an input byte source.
183pub type DemuxerFactory = fn(Input) -> Box<dyn Demuxer>;
184/// Factory opening a demuxer over a [`SeekInput`] — registered alongside the
185/// format's [`DemuxerFactory`] by formats that read less when they can seek.
186pub type SeekableDemuxerFactory = fn(SeekInput) -> Box<dyn Demuxer>;
187/// Factory opening a muxer over an output byte sink.
188pub type MuxerFactory = fn(Output) -> Box<dyn Muxer>;
189/// Factory opening a muxer over a [`SeekOutput`] -- registered alongside the
190/// format's [`MuxerFactory`] by formats that can stream when they can seek.
191pub type SeekableMuxerFactory = fn(SeekOutput) -> Box<dyn Muxer>;
192/// Factory opening a muxer that owns its own file set (HLS/DASH write a
193/// playlist/manifest plus many segment files, so a single byte sink cannot
194/// model them). Receives the output path and the format options
195/// (`-hls_time 6` → `{"hls_time": "6"}`).
196pub type PathMuxerFactory = fn(&std::path::Path, &Dictionary) -> Result<Box<dyn Muxer>>;
197/// Content sniffer: scores how strongly a byte prefix looks like this format,
198/// `0` (no match) to `100` (certain) — mirrors FFmpeg's `AVInputFormat::read_probe`.
199pub type ProbeFn = fn(&[u8]) -> i32;
200
201/// What a container's **muxer** will accept: the machine-readable half of the
202/// rules each muxer already enforces when it writes a header.
203///
204/// Muxers validate at run time, and a few historically wrote a zero codec tag
205/// rather than refusing an unmappable codec. Declaring the same rules here lets
206/// a caller answer "what can this file become?" *before* opening an encoder —
207/// which is what `rff-targets` is built on.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub struct MuxCaps {
210 /// Every codec this muxer can write. Empty for demux-only formats.
211 pub codecs: &'static [CodecId],
212 /// Most streams the container holds; `None` when there is no fixed limit.
213 pub max_streams: Option<usize>,
214 /// True when the container holds exactly ONE picture, so extra frames are
215 /// refused or dropped (png, jpeg, gif, webp, avif, jxl, av2f).
216 pub still_image: bool,
217}
218
219impl MuxCaps {
220 /// A format that cannot be written at all (demux-only).
221 pub const NONE: MuxCaps = MuxCaps {
222 codecs: &[],
223 max_streams: Some(0),
224 still_image: false,
225 };
226
227 /// A container interleaving any number of streams (mp4, matroska, mpegts).
228 pub const fn container(codecs: &'static [CodecId]) -> MuxCaps {
229 MuxCaps {
230 codecs,
231 max_streams: None,
232 still_image: false,
233 }
234 }
235
236 /// A file holding exactly one elementary stream (wav, mp3, ivf, ogg).
237 pub const fn single(codecs: &'static [CodecId]) -> MuxCaps {
238 MuxCaps {
239 codecs,
240 max_streams: Some(1),
241 still_image: false,
242 }
243 }
244
245 /// Narrow a [`single`](MuxCaps::single) file to one picture (png, jpeg).
246 pub const fn image(self) -> MuxCaps {
247 MuxCaps {
248 codecs: self.codecs,
249 max_streams: self.max_streams,
250 still_image: true,
251 }
252 }
253
254 /// Can this muxer write `id`?
255 pub fn accepts(&self, id: CodecId) -> bool {
256 self.codecs.contains(&id)
257 }
258
259 /// Can this muxer carry any stream of `media` at all?
260 pub fn accepts_media(&self, media: MediaType) -> bool {
261 self.codecs.iter().any(|c| c.media_type() == media)
262 }
263
264 /// The codecs this muxer accepts for one media category.
265 pub fn codecs_for(&self, media: MediaType) -> impl Iterator<Item = CodecId> + '_ {
266 self.codecs
267 .iter()
268 .copied()
269 .filter(move |c| c.media_type() == media)
270 }
271}
272
273/// Static description of a container format and its read/write support.
274pub struct Format {
275 /// Short name (`avi`, `mp4`, ...), as used by `ffmpeg -f <name>`.
276 pub name: &'static str,
277 pub long_name: &'static str,
278 /// File extensions that imply this format (without the dot).
279 pub extensions: &'static [&'static str],
280 /// Present if this format can be demuxed (read).
281 pub demuxer: Option<DemuxerFactory>,
282 /// Present if this format can be muxed (written).
283 pub muxer: Option<MuxerFactory>,
284 /// Present for formats that mux into a *path* (playlist + segment files)
285 /// rather than one byte sink — HLS, DASH. Mutually exclusive with `muxer`.
286 pub muxer_path: Option<PathMuxerFactory>,
287 /// Content sniffer for magic-byte detection (independent of the filename).
288 pub probe: Option<ProbeFn>,
289 /// What this format's muxer accepts. [`MuxCaps::NONE`] for demux-only
290 /// formats. Keep it in step with the muxer — `tests/mux_caps.rs` gates it.
291 pub mux_caps: MuxCaps,
292}
293
294impl Format {
295 pub fn can_demux(&self) -> bool {
296 self.demuxer.is_some()
297 }
298 pub fn can_mux(&self) -> bool {
299 self.muxer.is_some() || self.muxer_path.is_some()
300 }
301}
302
303/// Holds every container format known to a running engine.
304#[derive(Default)]
305pub struct FormatRegistry {
306 by_name: HashMap<&'static str, Format>,
307 /// Seek-aware demuxer factories, keyed by format name (see
308 /// [`register_seekable_demuxer`](Self::register_seekable_demuxer)).
309 seekable: HashMap<&'static str, SeekableDemuxerFactory>,
310 /// Seek-aware muxer factories, keyed by format name (see
311 /// [`register_seekable_muxer`](Self::register_seekable_muxer)).
312 seekable_muxers: HashMap<&'static str, SeekableMuxerFactory>,
313}
314
315impl FormatRegistry {
316 pub fn new() -> FormatRegistry {
317 FormatRegistry::default()
318 }
319
320 pub fn register(&mut self, format: Format) {
321 self.by_name.insert(format.name, format);
322 }
323
324 /// Give the format named `name` a demuxer that takes a [`SeekInput`]. Used
325 /// by [`open_demuxer_seekable`](Self::open_demuxer_seekable) when the caller
326 /// has a seekable source; the plain [`Format::demuxer`] still serves pipes
327 /// and sockets.
328 pub fn register_seekable_demuxer(
329 &mut self,
330 name: &'static str,
331 factory: SeekableDemuxerFactory,
332 ) {
333 self.seekable.insert(name, factory);
334 }
335
336 pub fn by_name(&self, name: &str) -> Option<&Format> {
337 self.by_name.get(name)
338 }
339
340 /// Find a format whose extension list contains `ext` (case-insensitive,
341 /// no leading dot). The cheap "guess by filename" path; [`probe`](Self::probe)
342 /// inspects content instead.
343 pub fn by_extension(&self, ext: &str) -> Option<&Format> {
344 let ext = ext.trim_start_matches('.').to_ascii_lowercase();
345 self.by_name
346 .values()
347 .find(|f| f.extensions.iter().any(|e| e.eq_ignore_ascii_case(&ext)))
348 }
349
350 /// Identify a format by sniffing a byte prefix: the registered format whose
351 /// [`probe`](Format::probe) returns the highest positive score wins. Returns
352 /// `None` if nothing recognizes the data.
353 pub fn probe(&self, data: &[u8]) -> Option<&Format> {
354 self.by_name
355 .values()
356 .filter_map(|f| f.probe.map(|p| (p(data), f)))
357 .filter(|(score, _)| *score > 0)
358 .max_by_key(|(score, _)| *score)
359 .map(|(_, f)| f)
360 }
361
362 /// Open a demuxer for the format named `name`.
363 pub fn open_demuxer(&self, name: &str, input: Input) -> Result<Box<dyn Demuxer>> {
364 self.by_name
365 .get(name)
366 .and_then(|f| f.demuxer)
367 .map(|factory| factory(input))
368 .ok_or_else(|| Error::DemuxerNotFound(name.to_string()))
369 }
370
371 /// Open a demuxer over a seekable input: the format's seek-aware demuxer if
372 /// it registered one, otherwise its plain streaming demuxer (a seekable
373 /// source is still a byte stream). Callers that hold a file should prefer
374 /// this — it is what lets MP4 read only `moov` instead of the whole file.
375 pub fn open_demuxer_seekable(&self, name: &str, input: SeekInput) -> Result<Box<dyn Demuxer>> {
376 if !self.by_name.get(name).is_some_and(Format::can_demux) {
377 return Err(Error::DemuxerNotFound(name.to_string()));
378 }
379 match self.seekable.get(name) {
380 Some(factory) => Ok(factory(input)),
381 None => self.open_demuxer(name, input),
382 }
383 }
384
385 /// Give the format named `name` a muxer that takes a [`SeekOutput`]. Used
386 /// by [`open_muxer_seekable`](Self::open_muxer_seekable) when the caller
387 /// writes to something seekable; the plain [`Format::muxer`] still serves
388 /// pipes and sockets.
389 pub fn register_seekable_muxer(&mut self, name: &'static str, factory: SeekableMuxerFactory) {
390 self.seekable_muxers.insert(name, factory);
391 }
392
393 /// Open a muxer over a seekable output: the format's seek-aware muxer if it
394 /// registered one, otherwise its plain muxer (a seekable sink is still a
395 /// byte sink). Callers writing a file should prefer this -- it is what lets
396 /// MP4/Matroska/AVI/WAV stream instead of buffering the whole output.
397 pub fn open_muxer_seekable(&self, name: &str, output: SeekOutput) -> Result<Box<dyn Muxer>> {
398 if !self.by_name.get(name).is_some_and(|f| f.muxer.is_some()) {
399 return Err(Error::MuxerNotFound(name.to_string()));
400 }
401 match self.seekable_muxers.get(name) {
402 Some(factory) => Ok(factory(output)),
403 None => self.open_muxer(name, output),
404 }
405 }
406
407 /// Open a muxer for the format named `name`.
408 pub fn open_muxer(&self, name: &str, output: Output) -> Result<Box<dyn Muxer>> {
409 self.by_name
410 .get(name)
411 .and_then(|f| f.muxer)
412 .map(|factory| factory(output))
413 .ok_or_else(|| Error::MuxerNotFound(name.to_string()))
414 }
415
416 pub fn iter(&self) -> impl Iterator<Item = &Format> {
417 self.by_name.values()
418 }
419
420 pub fn len(&self) -> usize {
421 self.by_name.len()
422 }
423
424 pub fn is_empty(&self) -> bool {
425 self.by_name.is_empty()
426 }
427}
428
429#[cfg(test)]
430mod tests {
431 use super::*;
432 use std::io::Cursor;
433
434 /// Reports which factory built it via the stream count.
435 struct Tagged(usize);
436 impl Demuxer for Tagged {
437 fn read_header(&mut self) -> Result<Vec<Stream>> {
438 Ok((0..self.0).map(|i| Stream::new(i, CodecId::None)).collect())
439 }
440 fn read_packet(&mut self) -> Result<rff_core::Packet> {
441 Err(Error::Eof)
442 }
443 }
444
445 fn format(name: &'static str) -> Format {
446 Format {
447 name,
448 long_name: name,
449 extensions: &[],
450 demuxer: Some(|_| Box::new(Tagged(1))),
451 muxer: None,
452 muxer_path: None,
453 probe: None,
454 mux_caps: MuxCaps::NONE,
455 }
456 }
457
458 fn streams(mut d: Box<dyn Demuxer>) -> usize {
459 d.read_header().unwrap().len()
460 }
461
462 #[test]
463 fn seekable_open_prefers_the_seekable_factory_and_falls_back() {
464 let mut r = FormatRegistry::new();
465 r.register(format("plain"));
466 r.register(format("seeky"));
467 r.register_seekable_demuxer("seeky", |_| Box::new(Tagged(2)));
468 let input = || -> SeekInput { Box::new(Cursor::new(vec![0u8; 4])) };
469 assert_eq!(
470 streams(r.open_demuxer_seekable("seeky", input()).unwrap()),
471 2
472 );
473 // No seekable factory: the streaming one serves the seekable input.
474 assert_eq!(
475 streams(r.open_demuxer_seekable("plain", input()).unwrap()),
476 1
477 );
478 // A plain stream open never takes the seekable path.
479 assert_eq!(
480 streams(
481 r.open_demuxer("seeky", Box::new(Cursor::new(vec![])))
482 .unwrap()
483 ),
484 1
485 );
486 assert!(matches!(
487 r.open_demuxer_seekable("missing", input()),
488 Err(Error::DemuxerNotFound(_))
489 ));
490 }
491
492 /// Reports which factory built it through the error `write_trailer` returns.
493 struct TaggedMux(&'static str);
494 impl Muxer for TaggedMux {
495 fn write_header(&mut self, _: &[Stream]) -> Result<()> {
496 Ok(())
497 }
498 fn write_packet(&mut self, _: &rff_core::Packet) -> Result<()> {
499 Ok(())
500 }
501 fn write_trailer(&mut self) -> Result<()> {
502 Err(Error::invalid(self.0))
503 }
504 }
505
506 fn tag(mut m: Box<dyn Muxer>) -> String {
507 m.write_trailer().unwrap_err().to_string()
508 }
509
510 #[test]
511 fn seekable_muxer_open_prefers_the_seekable_factory_and_falls_back() {
512 let mut r = FormatRegistry::new();
513 let with_muxer = |name| Format {
514 muxer: Some(|_| Box::new(TaggedMux("plain"))),
515 ..format(name)
516 };
517 r.register(with_muxer("plain"));
518 r.register(with_muxer("seeky"));
519 r.register(format("demux_only"));
520 r.register_seekable_muxer("seeky", |_| Box::new(TaggedMux("seekable")));
521 let output = || -> SeekOutput { Box::new(Cursor::new(Vec::new())) };
522 assert!(tag(r.open_muxer_seekable("seeky", output()).unwrap()).contains("seekable"));
523 // No seekable factory: the plain one serves the seekable output.
524 assert!(tag(r.open_muxer_seekable("plain", output()).unwrap()).contains("plain"));
525 // A plain open never takes the seekable path.
526 assert!(tag(r.open_muxer("seeky", Box::new(Vec::new())).unwrap()).contains("plain"));
527 assert!(matches!(
528 r.open_muxer_seekable("demux_only", output()),
529 Err(Error::MuxerNotFound(_))
530 ));
531 assert!(matches!(
532 r.open_muxer_seekable("missing", output()),
533 Err(Error::MuxerNotFound(_))
534 ));
535 }
536}