rff-core 0.6.2

Remade FFmpeg (Rust): core media primitives — frames, packets, formats, errors (libavutil-equivalent).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Core media enumerations: media types, codec identifiers, and raw sample
//! layouts (pixel formats for video, sample formats for audio).
//!
//! These deliberately cover only a small, growing subset of what FFmpeg
//! supports. Add variants as new codecs land — every variant is `#[non_exhaustive]`
//! friendly so downstream `match`es must keep a wildcard arm.

use std::fmt;

/// The broad category of an elementary stream.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MediaType {
    Video,
    Audio,
    Subtitle,
    Data,
    Attachment,
}

impl fmt::Display for MediaType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            MediaType::Video => "video",
            MediaType::Audio => "audio",
            MediaType::Subtitle => "subtitle",
            MediaType::Data => "data",
            MediaType::Attachment => "attachment",
        };
        f.write_str(s)
    }
}

/// Identifier for a specific codec (the *what*, independent of any particular
/// encoder/decoder *implementation*).
///
/// Marked `#[non_exhaustive]`: we add codecs over time, and external matchers
/// must handle the open-ended set.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum CodecId {
    /// Sentinel for "no/unknown codec".
    #[default]
    None,
    /// H.264 / AVC video.
    H264,
    /// H.265 / HEVC video. Decode only.
    Hevc,
    /// Opus audio.
    Opus,
    /// AVIF still image (an AV1 intra frame in a HEIF box).
    Avif,
    /// PNG still image (DEFLATE-compressed RGB/RGBA).
    Png,
    /// JPEG still image (a.k.a. MJPEG).
    Jpeg,
    /// GIF image (first frame; palette-based).
    Gif,
    /// WebP image (VP8 / VP8L).
    Webp,
    /// Linear PCM audio (uncompressed; layout in the stream's sample format).
    Pcm,
    /// Vorbis audio (Ogg Vorbis).
    Vorbis,
    /// FLAC lossless audio.
    Flac,
    /// JPEG XL image.
    Jxl,
    /// AAC audio (Advanced Audio Coding).
    Aac,
    /// VP9 video.
    Vp9,
    /// AV2 video (AOMedia's successor to AV1). Decode only.
    Av2,
    /// MP3 audio (MPEG-1/2 Audio Layer III).
    Mp3,
    /// Raw uncompressed video (planar/packed pixels; layout in the stream's
    /// pixel format). The video analog of [`CodecId::Pcm`].
    RawVideo,
    /// SubRip subtitle (`.srt`): plain-text cues with `HH:MM:SS,mmm` timing.
    Subrip,
    /// WebVTT subtitle (`.vtt`): plain-text cues with `HH:MM:SS.mmm` timing.
    WebVtt,
    /// Apple ProRes video (422 Proxy/LT/standard/HQ, 4444, 4444 XQ). Stream
    /// copy only; the profile travels as the stream's `codec_tag`.
    ProRes,
    /// Avid DNxHD / DNxHR video. Stream copy only.
    DnxHd,
    /// APV (Advanced Professional Video). Stream copy only.
    Apv,
    /// VP8 video. Stream copy only.
    Vp8,
    /// Apple Lossless (ALAC) audio. Stream copy only.
    Alac,
    /// Dolby Digital (AC-3) audio. Stream copy only.
    Ac3,
    /// Dolby Digital Plus (E-AC-3) audio. Stream copy only.
    Eac3,
    /// G.711 A-law audio (WAV format 0x0006, AIFF-C/CAF `alaw`).
    PcmAlaw,
    /// G.711 µ-law audio (WAV format 0x0007, AIFF-C/CAF `ulaw`).
    PcmMulaw,
    /// Microsoft ADPCM (WAV format 0x0002). One block per packet.
    AdpcmMs,
    /// IMA/DVI ADPCM as WAV stores it (format 0x0011). One block per packet.
    AdpcmImaWav,
    /// IMA ADPCM as QuickTime stores it (`ima4`: 34-byte blocks of 64 samples
    /// per channel), in AIFF-C, CAF and MOV.
    AdpcmImaQt,
    /// MPEG-1/2 Audio Layer II (MP2). Decode only.
    Mp2,
    /// MPEG-1/2 Audio Layer I (MP1). Decode only.
    Mp1,
}

impl CodecId {
    /// Short canonical name, matching the token used on the `ffmpeg` command
    /// line (`-c:v h264`, `-c:a opus`, ...).
    pub fn name(self) -> &'static str {
        match self {
            CodecId::None => "none",
            CodecId::H264 => "h264",
            CodecId::Hevc => "hevc",
            CodecId::Opus => "opus",
            CodecId::Avif => "avif",
            CodecId::Png => "png",
            CodecId::Jpeg => "mjpeg",
            CodecId::Gif => "gif",
            CodecId::Webp => "webp",
            CodecId::Pcm => "pcm",
            CodecId::Vorbis => "vorbis",
            CodecId::Flac => "flac",
            CodecId::Jxl => "jpegxl",
            CodecId::Aac => "aac",
            CodecId::Vp9 => "vp9",
            CodecId::Av2 => "av2",
            CodecId::Mp3 => "mp3",
            CodecId::RawVideo => "rawvideo",
            CodecId::Subrip => "subrip",
            CodecId::WebVtt => "webvtt",
            CodecId::ProRes => "prores",
            CodecId::DnxHd => "dnxhd",
            CodecId::Apv => "apv",
            CodecId::Vp8 => "vp8",
            CodecId::Alac => "alac",
            CodecId::Ac3 => "ac3",
            CodecId::Eac3 => "eac3",
            CodecId::PcmAlaw => "pcm_alaw",
            CodecId::PcmMulaw => "pcm_mulaw",
            CodecId::AdpcmMs => "adpcm_ms",
            CodecId::AdpcmImaWav => "adpcm_ima_wav",
            CodecId::AdpcmImaQt => "adpcm_ima_qt",
            CodecId::Mp2 => "mp2",
            CodecId::Mp1 => "mp1",
        }
    }

    /// The media category this codec produces.
    pub fn media_type(self) -> MediaType {
        match self {
            CodecId::None => MediaType::Data,
            CodecId::H264 | CodecId::Hevc => MediaType::Video,
            CodecId::Opus => MediaType::Audio,
            // Image codecs carry pixel data; we model them as (single-frame) video.
            CodecId::Avif => MediaType::Video,
            CodecId::Png => MediaType::Video,
            CodecId::Jpeg => MediaType::Video,
            CodecId::Gif => MediaType::Video,
            CodecId::Webp => MediaType::Video,
            CodecId::Pcm => MediaType::Audio,
            CodecId::Vorbis => MediaType::Audio,
            CodecId::Flac => MediaType::Audio,
            CodecId::Jxl => MediaType::Video,
            CodecId::Aac => MediaType::Audio,
            CodecId::Vp9 => MediaType::Video,
            CodecId::Av2 => MediaType::Video,
            CodecId::Mp3 => MediaType::Audio,
            CodecId::RawVideo => MediaType::Video,
            CodecId::Subrip => MediaType::Subtitle,
            CodecId::WebVtt => MediaType::Subtitle,
            CodecId::ProRes | CodecId::DnxHd | CodecId::Apv | CodecId::Vp8 => MediaType::Video,
            CodecId::Alac | CodecId::Ac3 | CodecId::Eac3 => MediaType::Audio,
            CodecId::PcmAlaw
            | CodecId::PcmMulaw
            | CodecId::AdpcmMs
            | CodecId::AdpcmImaWav
            | CodecId::AdpcmImaQt => MediaType::Audio,
            CodecId::Mp2 | CodecId::Mp1 => MediaType::Audio,
        }
    }

    /// The sample format a codec NAME pins, when the name carries one.
    ///
    /// `CodecId` is deliberately format-agnostic — `pcm_s16le` and `pcm_f32le`
    /// are both [`CodecId::Pcm`] — but for raw PCM the name IS the format, and
    /// dropping it means `-c:a pcm_s16le` silently writes whatever the decoder
    /// happened to emit. Compressed codecs return `None`: their sample format is
    /// an internal detail, not something the CLI name selects.
    pub fn sample_format_from_name(name: &str) -> Option<SampleFormat> {
        match name {
            "pcm_u8" => Some(SampleFormat::U8),
            "pcm_s16le" => Some(SampleFormat::S16),
            "pcm_s24le" => Some(SampleFormat::S24),
            "pcm_s32le" => Some(SampleFormat::S32),
            "pcm_f32le" => Some(SampleFormat::F32),
            "pcm_f64le" => Some(SampleFormat::F64),
            _ => None,
        }
    }

    /// Look up a codec id from its canonical CLI name.
    pub fn from_name(name: &str) -> Option<CodecId> {
        match name {
            "h264" | "avc" | "libx264" => Some(CodecId::H264),
            "hevc" | "h265" | "x265" | "libx265" => Some(CodecId::Hevc),
            "opus" | "libopus" => Some(CodecId::Opus),
            "avif" => Some(CodecId::Avif),
            "png" => Some(CodecId::Png),
            "mjpeg" | "jpeg" | "jpg" => Some(CodecId::Jpeg),
            "gif" => Some(CodecId::Gif),
            "webp" => Some(CodecId::Webp),
            "pcm" | "pcm_u8" | "pcm_s16le" | "pcm_s24le" | "pcm_s32le" | "pcm_f32le"
            | "pcm_f64le" => Some(CodecId::Pcm),
            "vorbis" => Some(CodecId::Vorbis),
            "flac" => Some(CodecId::Flac),
            "jpegxl" | "jxl" => Some(CodecId::Jxl),
            "aac" => Some(CodecId::Aac),
            "vp9" | "libvpx-vp9" => Some(CodecId::Vp9),
            "av2" | "libavm" => Some(CodecId::Av2),
            "mp3" | "mp3float" | "libmp3lame" => Some(CodecId::Mp3),
            "rawvideo" | "yuv4mpegpipe" => Some(CodecId::RawVideo),
            "srt" | "subrip" => Some(CodecId::Subrip),
            "vtt" | "webvtt" => Some(CodecId::WebVtt),
            "prores" | "prores_ks" => Some(CodecId::ProRes),
            "dnxhd" | "dnxhr" => Some(CodecId::DnxHd),
            "apv" => Some(CodecId::Apv),
            "vp8" | "libvpx" => Some(CodecId::Vp8),
            "alac" => Some(CodecId::Alac),
            "ac3" => Some(CodecId::Ac3),
            "eac3" => Some(CodecId::Eac3),
            "pcm_alaw" | "alaw" => Some(CodecId::PcmAlaw),
            "pcm_mulaw" | "mulaw" | "ulaw" => Some(CodecId::PcmMulaw),
            "adpcm_ms" => Some(CodecId::AdpcmMs),
            "adpcm_ima_wav" => Some(CodecId::AdpcmImaWav),
            "adpcm_ima_qt" | "ima4" => Some(CodecId::AdpcmImaQt),
            "mp2" | "mp2float" => Some(CodecId::Mp2),
            "mp1" | "mp1float" => Some(CodecId::Mp1),
            _ => None,
        }
    }
}

impl fmt::Display for CodecId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// Luma/chroma value range of a Y'CbCr stream.
///
/// This is metadata, not a conversion: it records how the samples in a plane
/// should be interpreted, so containers can label them and consumers can avoid
/// re-scaling data that is already in the range they want.
///
/// Getting it wrong is expensive and silent. Writing full-range samples into a
/// y4m without an `XCOLORRANGE=FULL` tag makes every reader treat them as
/// limited-range; on real content that fabricated **12 dB** of error between two
/// byte-identical payloads (41.3 dB compared raw, 29.0 dB compared through the
/// mislabelled container).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ColorRange {
    /// Not known. Consumers apply their own default — for Y'CbCr that is
    /// conventionally limited range, which is why leaving this unset on
    /// full-range data is a bug rather than a neutral choice.
    #[default]
    Unspecified,
    /// "TV" / MPEG range: Y 16–235, Cb/Cr 16–240.
    Limited,
    /// "PC" / JPEG range: all components 0–255. JPEG and PNG are always this.
    Full,
}

impl ColorRange {
    /// The y4m `XCOLORRANGE` tag value, or `None` when unspecified.
    pub fn y4m_tag(self) -> Option<&'static str> {
        match self {
            ColorRange::Unspecified => None,
            ColorRange::Limited => Some("LIMITED"),
            ColorRange::Full => Some("FULL"),
        }
    }

    /// Parse a y4m `XCOLORRANGE` value.
    pub fn from_y4m_tag(tag: &str) -> ColorRange {
        match tag.trim().to_ascii_uppercase().as_str() {
            "FULL" => ColorRange::Full,
            "LIMITED" => ColorRange::Limited,
            _ => ColorRange::Unspecified,
        }
    }
}

/// Raw pixel layout of a decoded video frame. A small starter subset.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PixelFormat {
    /// Planar Y'CbCr 4:2:0, 8-bit (the workhorse for H.264).
    Yuv420p,
    /// Planar Y'CbCr 4:2:2, 8-bit.
    Yuv422p,
    /// Planar Y'CbCr 4:4:4, 8-bit.
    Yuv444p,
    /// Planar Y'CbCr 4:2:0, 10-bit (each sample stored as little-endian u16).
    Yuv420p10,
    /// Planar Y'CbCr 4:2:2, 10-bit (little-endian u16 samples).
    Yuv422p10,
    /// Planar Y'CbCr 4:4:4, 10-bit (little-endian u16 samples).
    Yuv444p10,
    /// Planar Y'CbCr 4:2:0, 12-bit (little-endian u16 samples).
    Yuv420p12,
    /// Planar Y'CbCr 4:2:2, 12-bit (little-endian u16 samples).
    Yuv422p12,
    /// Planar Y'CbCr 4:4:4, 12-bit (little-endian u16 samples).
    Yuv444p12,
    /// Packed RGB, 8 bits per channel.
    Rgb24,
    /// Packed RGBA, 8 bits per channel.
    Rgba,
}

impl PixelFormat {
    pub fn name(self) -> &'static str {
        match self {
            PixelFormat::Yuv420p => "yuv420p",
            PixelFormat::Yuv422p => "yuv422p",
            PixelFormat::Yuv444p => "yuv444p",
            PixelFormat::Yuv420p10 => "yuv420p10le",
            PixelFormat::Yuv422p10 => "yuv422p10le",
            PixelFormat::Yuv444p10 => "yuv444p10le",
            PixelFormat::Yuv420p12 => "yuv420p12le",
            PixelFormat::Yuv422p12 => "yuv422p12le",
            PixelFormat::Yuv444p12 => "yuv444p12le",
            PixelFormat::Rgb24 => "rgb24",
            PixelFormat::Rgba => "rgba",
        }
    }

    /// Bits per component sample (8 for the 8-bit formats, 10 for the 10-bit
    /// planar ones).
    pub fn bit_depth(self) -> u32 {
        match self {
            PixelFormat::Yuv420p10 | PixelFormat::Yuv422p10 | PixelFormat::Yuv444p10 => 10,
            PixelFormat::Yuv420p12 | PixelFormat::Yuv422p12 | PixelFormat::Yuv444p12 => 12,
            _ => 8,
        }
    }

    /// Bytes used to store one component sample: 1 for 8-bit, 2 for the 10-bit
    /// formats (whose samples live in the low bits of a little-endian `u16`).
    pub fn bytes_per_sample(self) -> usize {
        if self.bit_depth() > 8 {
            2
        } else {
            1
        }
    }
}

/// Raw audio sample layout. Multi-byte samples are little-endian; containers
/// that store big-endian PCM (QuickTime `twos`, `in24`, ...) swap on the way
/// in and out, so a buffer of a given format always means one byte order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SampleFormat {
    /// Signed 16-bit, interleaved.
    S16,
    /// 32-bit float, interleaved.
    F32,
    /// 32-bit float, planar (one buffer per channel).
    F32Planar,
    /// Unsigned 8-bit (128 = silence), interleaved.
    U8,
    /// Signed 24-bit packed in 3 bytes, interleaved.
    S24,
    /// Signed 32-bit, interleaved.
    S32,
    /// 64-bit float, interleaved.
    F64,
}

impl SampleFormat {
    pub fn name(self) -> &'static str {
        match self {
            SampleFormat::S16 => "s16",
            SampleFormat::F32 => "flt",
            SampleFormat::F32Planar => "fltp",
            SampleFormat::U8 => "u8",
            SampleFormat::S24 => "s24",
            SampleFormat::S32 => "s32",
            SampleFormat::F64 => "dbl",
        }
    }

    /// Bytes occupied by one sample of one channel.
    pub fn bytes_per_sample(self) -> usize {
        match self {
            SampleFormat::U8 => 1,
            SampleFormat::S16 => 2,
            SampleFormat::S24 => 3,
            SampleFormat::S32 | SampleFormat::F32 | SampleFormat::F32Planar => 4,
            SampleFormat::F64 => 8,
        }
    }
}