Skip to main content

rff_core/
media.rs

1//! Core media enumerations: media types, codec identifiers, and raw sample
2//! layouts (pixel formats for video, sample formats for audio).
3//!
4//! These deliberately cover only a small, growing subset of what FFmpeg
5//! supports. Add variants as new codecs land — every variant is `#[non_exhaustive]`
6//! friendly so downstream `match`es must keep a wildcard arm.
7
8use std::fmt;
9
10/// The broad category of an elementary stream.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum MediaType {
13    Video,
14    Audio,
15    Subtitle,
16    Data,
17    Attachment,
18}
19
20impl fmt::Display for MediaType {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        let s = match self {
23            MediaType::Video => "video",
24            MediaType::Audio => "audio",
25            MediaType::Subtitle => "subtitle",
26            MediaType::Data => "data",
27            MediaType::Attachment => "attachment",
28        };
29        f.write_str(s)
30    }
31}
32
33/// Identifier for a specific codec (the *what*, independent of any particular
34/// encoder/decoder *implementation*).
35///
36/// Marked `#[non_exhaustive]`: we add codecs over time, and external matchers
37/// must handle the open-ended set.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
39#[non_exhaustive]
40pub enum CodecId {
41    /// Sentinel for "no/unknown codec".
42    #[default]
43    None,
44    /// H.264 / AVC video.
45    H264,
46    /// H.265 / HEVC video. Decode only.
47    Hevc,
48    /// Opus audio.
49    Opus,
50    /// AVIF still image (an AV1 intra frame in a HEIF box).
51    Avif,
52    /// PNG still image (DEFLATE-compressed RGB/RGBA).
53    Png,
54    /// JPEG still image (a.k.a. MJPEG).
55    Jpeg,
56    /// GIF image (first frame; palette-based).
57    Gif,
58    /// WebP image (VP8 / VP8L).
59    Webp,
60    /// Linear PCM audio (uncompressed; layout in the stream's sample format).
61    Pcm,
62    /// Vorbis audio (Ogg Vorbis).
63    Vorbis,
64    /// FLAC lossless audio.
65    Flac,
66    /// JPEG XL image.
67    Jxl,
68    /// AAC audio (Advanced Audio Coding).
69    Aac,
70    /// VP9 video.
71    Vp9,
72    /// AV2 video (AOMedia's successor to AV1). Decode only.
73    Av2,
74    /// MP3 audio (MPEG-1/2 Audio Layer III).
75    Mp3,
76    /// Raw uncompressed video (planar/packed pixels; layout in the stream's
77    /// pixel format). The video analog of [`CodecId::Pcm`].
78    RawVideo,
79    /// SubRip subtitle (`.srt`): plain-text cues with `HH:MM:SS,mmm` timing.
80    Subrip,
81    /// WebVTT subtitle (`.vtt`): plain-text cues with `HH:MM:SS.mmm` timing.
82    WebVtt,
83    /// Apple ProRes video (422 Proxy/LT/standard/HQ, 4444, 4444 XQ). Stream
84    /// copy only; the profile travels as the stream's `codec_tag`.
85    ProRes,
86    /// Avid DNxHD / DNxHR video. Stream copy only.
87    DnxHd,
88    /// APV (Advanced Professional Video). Stream copy only.
89    Apv,
90    /// VP8 video. Stream copy only.
91    Vp8,
92    /// Apple Lossless (ALAC) audio. Stream copy only.
93    Alac,
94    /// Dolby Digital (AC-3) audio. Stream copy only.
95    Ac3,
96    /// Dolby Digital Plus (E-AC-3) audio. Stream copy only.
97    Eac3,
98    /// G.711 A-law audio (WAV format 0x0006, AIFF-C/CAF `alaw`).
99    PcmAlaw,
100    /// G.711 µ-law audio (WAV format 0x0007, AIFF-C/CAF `ulaw`).
101    PcmMulaw,
102    /// Microsoft ADPCM (WAV format 0x0002). One block per packet.
103    AdpcmMs,
104    /// IMA/DVI ADPCM as WAV stores it (format 0x0011). One block per packet.
105    AdpcmImaWav,
106    /// IMA ADPCM as QuickTime stores it (`ima4`: 34-byte blocks of 64 samples
107    /// per channel), in AIFF-C, CAF and MOV.
108    AdpcmImaQt,
109    /// MPEG-1/2 Audio Layer II (MP2). Decode only.
110    Mp2,
111    /// MPEG-1/2 Audio Layer I (MP1). Decode only.
112    Mp1,
113}
114
115impl CodecId {
116    /// Short canonical name, matching the token used on the `ffmpeg` command
117    /// line (`-c:v h264`, `-c:a opus`, ...).
118    pub fn name(self) -> &'static str {
119        match self {
120            CodecId::None => "none",
121            CodecId::H264 => "h264",
122            CodecId::Hevc => "hevc",
123            CodecId::Opus => "opus",
124            CodecId::Avif => "avif",
125            CodecId::Png => "png",
126            CodecId::Jpeg => "mjpeg",
127            CodecId::Gif => "gif",
128            CodecId::Webp => "webp",
129            CodecId::Pcm => "pcm",
130            CodecId::Vorbis => "vorbis",
131            CodecId::Flac => "flac",
132            CodecId::Jxl => "jpegxl",
133            CodecId::Aac => "aac",
134            CodecId::Vp9 => "vp9",
135            CodecId::Av2 => "av2",
136            CodecId::Mp3 => "mp3",
137            CodecId::RawVideo => "rawvideo",
138            CodecId::Subrip => "subrip",
139            CodecId::WebVtt => "webvtt",
140            CodecId::ProRes => "prores",
141            CodecId::DnxHd => "dnxhd",
142            CodecId::Apv => "apv",
143            CodecId::Vp8 => "vp8",
144            CodecId::Alac => "alac",
145            CodecId::Ac3 => "ac3",
146            CodecId::Eac3 => "eac3",
147            CodecId::PcmAlaw => "pcm_alaw",
148            CodecId::PcmMulaw => "pcm_mulaw",
149            CodecId::AdpcmMs => "adpcm_ms",
150            CodecId::AdpcmImaWav => "adpcm_ima_wav",
151            CodecId::AdpcmImaQt => "adpcm_ima_qt",
152            CodecId::Mp2 => "mp2",
153            CodecId::Mp1 => "mp1",
154        }
155    }
156
157    /// The media category this codec produces.
158    pub fn media_type(self) -> MediaType {
159        match self {
160            CodecId::None => MediaType::Data,
161            CodecId::H264 | CodecId::Hevc => MediaType::Video,
162            CodecId::Opus => MediaType::Audio,
163            // Image codecs carry pixel data; we model them as (single-frame) video.
164            CodecId::Avif => MediaType::Video,
165            CodecId::Png => MediaType::Video,
166            CodecId::Jpeg => MediaType::Video,
167            CodecId::Gif => MediaType::Video,
168            CodecId::Webp => MediaType::Video,
169            CodecId::Pcm => MediaType::Audio,
170            CodecId::Vorbis => MediaType::Audio,
171            CodecId::Flac => MediaType::Audio,
172            CodecId::Jxl => MediaType::Video,
173            CodecId::Aac => MediaType::Audio,
174            CodecId::Vp9 => MediaType::Video,
175            CodecId::Av2 => MediaType::Video,
176            CodecId::Mp3 => MediaType::Audio,
177            CodecId::RawVideo => MediaType::Video,
178            CodecId::Subrip => MediaType::Subtitle,
179            CodecId::WebVtt => MediaType::Subtitle,
180            CodecId::ProRes | CodecId::DnxHd | CodecId::Apv | CodecId::Vp8 => MediaType::Video,
181            CodecId::Alac | CodecId::Ac3 | CodecId::Eac3 => MediaType::Audio,
182            CodecId::PcmAlaw
183            | CodecId::PcmMulaw
184            | CodecId::AdpcmMs
185            | CodecId::AdpcmImaWav
186            | CodecId::AdpcmImaQt => MediaType::Audio,
187            CodecId::Mp2 | CodecId::Mp1 => MediaType::Audio,
188        }
189    }
190
191    /// The sample format a codec NAME pins, when the name carries one.
192    ///
193    /// `CodecId` is deliberately format-agnostic — `pcm_s16le` and `pcm_f32le`
194    /// are both [`CodecId::Pcm`] — but for raw PCM the name IS the format, and
195    /// dropping it means `-c:a pcm_s16le` silently writes whatever the decoder
196    /// happened to emit. Compressed codecs return `None`: their sample format is
197    /// an internal detail, not something the CLI name selects.
198    pub fn sample_format_from_name(name: &str) -> Option<SampleFormat> {
199        match name {
200            "pcm_u8" => Some(SampleFormat::U8),
201            "pcm_s16le" => Some(SampleFormat::S16),
202            "pcm_s24le" => Some(SampleFormat::S24),
203            "pcm_s32le" => Some(SampleFormat::S32),
204            "pcm_f32le" => Some(SampleFormat::F32),
205            "pcm_f64le" => Some(SampleFormat::F64),
206            _ => None,
207        }
208    }
209
210    /// Look up a codec id from its canonical CLI name.
211    pub fn from_name(name: &str) -> Option<CodecId> {
212        match name {
213            "h264" | "avc" | "libx264" => Some(CodecId::H264),
214            "hevc" | "h265" | "x265" | "libx265" => Some(CodecId::Hevc),
215            "opus" | "libopus" => Some(CodecId::Opus),
216            "avif" => Some(CodecId::Avif),
217            "png" => Some(CodecId::Png),
218            "mjpeg" | "jpeg" | "jpg" => Some(CodecId::Jpeg),
219            "gif" => Some(CodecId::Gif),
220            "webp" => Some(CodecId::Webp),
221            "pcm" | "pcm_u8" | "pcm_s16le" | "pcm_s24le" | "pcm_s32le" | "pcm_f32le"
222            | "pcm_f64le" => Some(CodecId::Pcm),
223            "vorbis" => Some(CodecId::Vorbis),
224            "flac" => Some(CodecId::Flac),
225            "jpegxl" | "jxl" => Some(CodecId::Jxl),
226            "aac" => Some(CodecId::Aac),
227            "vp9" | "libvpx-vp9" => Some(CodecId::Vp9),
228            "av2" | "libavm" => Some(CodecId::Av2),
229            "mp3" | "mp3float" | "libmp3lame" => Some(CodecId::Mp3),
230            "rawvideo" | "yuv4mpegpipe" => Some(CodecId::RawVideo),
231            "srt" | "subrip" => Some(CodecId::Subrip),
232            "vtt" | "webvtt" => Some(CodecId::WebVtt),
233            "prores" | "prores_ks" => Some(CodecId::ProRes),
234            "dnxhd" | "dnxhr" => Some(CodecId::DnxHd),
235            "apv" => Some(CodecId::Apv),
236            "vp8" | "libvpx" => Some(CodecId::Vp8),
237            "alac" => Some(CodecId::Alac),
238            "ac3" => Some(CodecId::Ac3),
239            "eac3" => Some(CodecId::Eac3),
240            "pcm_alaw" | "alaw" => Some(CodecId::PcmAlaw),
241            "pcm_mulaw" | "mulaw" | "ulaw" => Some(CodecId::PcmMulaw),
242            "adpcm_ms" => Some(CodecId::AdpcmMs),
243            "adpcm_ima_wav" => Some(CodecId::AdpcmImaWav),
244            "adpcm_ima_qt" | "ima4" => Some(CodecId::AdpcmImaQt),
245            "mp2" | "mp2float" => Some(CodecId::Mp2),
246            "mp1" | "mp1float" => Some(CodecId::Mp1),
247            _ => None,
248        }
249    }
250}
251
252impl fmt::Display for CodecId {
253    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254        f.write_str(self.name())
255    }
256}
257
258/// Luma/chroma value range of a Y'CbCr stream.
259///
260/// This is metadata, not a conversion: it records how the samples in a plane
261/// should be interpreted, so containers can label them and consumers can avoid
262/// re-scaling data that is already in the range they want.
263///
264/// Getting it wrong is expensive and silent. Writing full-range samples into a
265/// y4m without an `XCOLORRANGE=FULL` tag makes every reader treat them as
266/// limited-range; on real content that fabricated **12 dB** of error between two
267/// byte-identical payloads (41.3 dB compared raw, 29.0 dB compared through the
268/// mislabelled container).
269#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
270pub enum ColorRange {
271    /// Not known. Consumers apply their own default — for Y'CbCr that is
272    /// conventionally limited range, which is why leaving this unset on
273    /// full-range data is a bug rather than a neutral choice.
274    #[default]
275    Unspecified,
276    /// "TV" / MPEG range: Y 16–235, Cb/Cr 16–240.
277    Limited,
278    /// "PC" / JPEG range: all components 0–255. JPEG and PNG are always this.
279    Full,
280}
281
282impl ColorRange {
283    /// The y4m `XCOLORRANGE` tag value, or `None` when unspecified.
284    pub fn y4m_tag(self) -> Option<&'static str> {
285        match self {
286            ColorRange::Unspecified => None,
287            ColorRange::Limited => Some("LIMITED"),
288            ColorRange::Full => Some("FULL"),
289        }
290    }
291
292    /// Parse a y4m `XCOLORRANGE` value.
293    pub fn from_y4m_tag(tag: &str) -> ColorRange {
294        match tag.trim().to_ascii_uppercase().as_str() {
295            "FULL" => ColorRange::Full,
296            "LIMITED" => ColorRange::Limited,
297            _ => ColorRange::Unspecified,
298        }
299    }
300}
301
302/// Raw pixel layout of a decoded video frame. A small starter subset.
303#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
304#[non_exhaustive]
305pub enum PixelFormat {
306    /// Planar Y'CbCr 4:2:0, 8-bit (the workhorse for H.264).
307    Yuv420p,
308    /// Planar Y'CbCr 4:2:2, 8-bit.
309    Yuv422p,
310    /// Planar Y'CbCr 4:4:4, 8-bit.
311    Yuv444p,
312    /// Planar Y'CbCr 4:2:0, 10-bit (each sample stored as little-endian u16).
313    Yuv420p10,
314    /// Planar Y'CbCr 4:2:2, 10-bit (little-endian u16 samples).
315    Yuv422p10,
316    /// Planar Y'CbCr 4:4:4, 10-bit (little-endian u16 samples).
317    Yuv444p10,
318    /// Planar Y'CbCr 4:2:0, 12-bit (little-endian u16 samples).
319    Yuv420p12,
320    /// Planar Y'CbCr 4:2:2, 12-bit (little-endian u16 samples).
321    Yuv422p12,
322    /// Planar Y'CbCr 4:4:4, 12-bit (little-endian u16 samples).
323    Yuv444p12,
324    /// Packed RGB, 8 bits per channel.
325    Rgb24,
326    /// Packed RGBA, 8 bits per channel.
327    Rgba,
328}
329
330impl PixelFormat {
331    pub fn name(self) -> &'static str {
332        match self {
333            PixelFormat::Yuv420p => "yuv420p",
334            PixelFormat::Yuv422p => "yuv422p",
335            PixelFormat::Yuv444p => "yuv444p",
336            PixelFormat::Yuv420p10 => "yuv420p10le",
337            PixelFormat::Yuv422p10 => "yuv422p10le",
338            PixelFormat::Yuv444p10 => "yuv444p10le",
339            PixelFormat::Yuv420p12 => "yuv420p12le",
340            PixelFormat::Yuv422p12 => "yuv422p12le",
341            PixelFormat::Yuv444p12 => "yuv444p12le",
342            PixelFormat::Rgb24 => "rgb24",
343            PixelFormat::Rgba => "rgba",
344        }
345    }
346
347    /// Bits per component sample (8 for the 8-bit formats, 10 for the 10-bit
348    /// planar ones).
349    pub fn bit_depth(self) -> u32 {
350        match self {
351            PixelFormat::Yuv420p10 | PixelFormat::Yuv422p10 | PixelFormat::Yuv444p10 => 10,
352            PixelFormat::Yuv420p12 | PixelFormat::Yuv422p12 | PixelFormat::Yuv444p12 => 12,
353            _ => 8,
354        }
355    }
356
357    /// Bytes used to store one component sample: 1 for 8-bit, 2 for the 10-bit
358    /// formats (whose samples live in the low bits of a little-endian `u16`).
359    pub fn bytes_per_sample(self) -> usize {
360        if self.bit_depth() > 8 {
361            2
362        } else {
363            1
364        }
365    }
366}
367
368/// Raw audio sample layout. Multi-byte samples are little-endian; containers
369/// that store big-endian PCM (QuickTime `twos`, `in24`, ...) swap on the way
370/// in and out, so a buffer of a given format always means one byte order.
371#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
372#[non_exhaustive]
373pub enum SampleFormat {
374    /// Signed 16-bit, interleaved.
375    S16,
376    /// 32-bit float, interleaved.
377    F32,
378    /// 32-bit float, planar (one buffer per channel).
379    F32Planar,
380    /// Unsigned 8-bit (128 = silence), interleaved.
381    U8,
382    /// Signed 24-bit packed in 3 bytes, interleaved.
383    S24,
384    /// Signed 32-bit, interleaved.
385    S32,
386    /// 64-bit float, interleaved.
387    F64,
388}
389
390impl SampleFormat {
391    pub fn name(self) -> &'static str {
392        match self {
393            SampleFormat::S16 => "s16",
394            SampleFormat::F32 => "flt",
395            SampleFormat::F32Planar => "fltp",
396            SampleFormat::U8 => "u8",
397            SampleFormat::S24 => "s24",
398            SampleFormat::S32 => "s32",
399            SampleFormat::F64 => "dbl",
400        }
401    }
402
403    /// Bytes occupied by one sample of one channel.
404    pub fn bytes_per_sample(self) -> usize {
405        match self {
406            SampleFormat::U8 => 1,
407            SampleFormat::S16 => 2,
408            SampleFormat::S24 => 3,
409            SampleFormat::S32 | SampleFormat::F32 | SampleFormat::F32Planar => 4,
410            SampleFormat::F64 => 8,
411        }
412    }
413}