use std::fmt;
#[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)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum CodecId {
#[default]
None,
H264,
Hevc,
Opus,
Avif,
Png,
Jpeg,
Gif,
Webp,
Pcm,
Vorbis,
Flac,
Jxl,
Aac,
Vp9,
Av2,
Mp3,
RawVideo,
Subrip,
WebVtt,
}
impl CodecId {
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",
}
}
pub fn media_type(self) -> MediaType {
match self {
CodecId::None => MediaType::Data,
CodecId::H264 | CodecId::Hevc => MediaType::Video,
CodecId::Opus => MediaType::Audio,
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,
}
}
pub fn sample_format_from_name(name: &str) -> Option<SampleFormat> {
match name {
"pcm_s16le" => Some(SampleFormat::S16),
"pcm_f32le" => Some(SampleFormat::F32),
_ => None,
}
}
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_s16le" | "pcm_f32le" => 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),
_ => None,
}
}
}
impl fmt::Display for CodecId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ColorRange {
#[default]
Unspecified,
Limited,
Full,
}
impl ColorRange {
pub fn y4m_tag(self) -> Option<&'static str> {
match self {
ColorRange::Unspecified => None,
ColorRange::Limited => Some("LIMITED"),
ColorRange::Full => Some("FULL"),
}
}
pub fn from_y4m_tag(tag: &str) -> ColorRange {
match tag.trim().to_ascii_uppercase().as_str() {
"FULL" => ColorRange::Full,
"LIMITED" => ColorRange::Limited,
_ => ColorRange::Unspecified,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PixelFormat {
Yuv420p,
Yuv422p,
Yuv444p,
Yuv420p10,
Yuv422p10,
Yuv444p10,
Yuv420p12,
Yuv422p12,
Yuv444p12,
Rgb24,
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",
}
}
pub fn bit_depth(self) -> u32 {
match self {
PixelFormat::Yuv420p10 | PixelFormat::Yuv422p10 | PixelFormat::Yuv444p10 => 10,
PixelFormat::Yuv420p12 | PixelFormat::Yuv422p12 | PixelFormat::Yuv444p12 => 12,
_ => 8,
}
}
pub fn bytes_per_sample(self) -> usize {
if self.bit_depth() > 8 {
2
} else {
1
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SampleFormat {
S16,
F32,
F32Planar,
}
impl SampleFormat {
pub fn name(self) -> &'static str {
match self {
SampleFormat::S16 => "s16",
SampleFormat::F32 => "flt",
SampleFormat::F32Planar => "fltp",
}
}
pub fn bytes_per_sample(self) -> usize {
match self {
SampleFormat::S16 => 2,
SampleFormat::F32 | SampleFormat::F32Planar => 4,
}
}
}