use std::sync::Arc;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ColorSpace {
Bt601,
Bt709,
Bt2020,
Smpte240m,
Srgb,
DciP3,
DisplayP3,
Unknown,
}
impl ColorSpace {
pub fn as_ffmpeg_arg(&self) -> Option<&str> {
match self {
Self::Bt601 => Some("bt470bg"),
Self::Bt709 => Some("bt709"),
Self::Bt2020 => Some("bt2020nc"),
Self::Smpte240m => Some("smpte240m"),
Self::Srgb => Some("bt709"),
Self::DciP3 => Some("bt2020nc"),
Self::DisplayP3 => Some("bt2020nc"),
Self::Unknown => None,
}
}
pub fn from_ffmpeg(s: &str) -> Self {
match s {
"bt709" => Self::Bt709,
"bt470bg" | "smpte170m" => Self::Bt601,
"bt2020nc" | "bt2020c" => Self::Bt2020,
"smpte240m" => Self::Smpte240m,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ColorRange {
Limited,
Full,
}
impl ColorRange {
pub fn as_ffmpeg_arg(&self) -> &str {
match self {
Self::Limited => "tv",
Self::Full => "pc",
}
}
pub fn from_ffmpeg(s: &str) -> Option<Self> {
match s {
"tv" | "limited" | "mpeg" => Some(Self::Limited),
"pc" | "full" | "jpeg" => Some(Self::Full),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PixelFormat(Arc<str>);
impl PixelFormat {
pub fn new(id: impl Into<Arc<str>>) -> Self {
Self(id.into())
}
pub fn id(&self) -> &str {
&self.0
}
pub fn bit_depth(&self) -> Option<u8> {
match self.0.as_ref() {
"yuv420p" | "yuv422p" | "yuv444p" | "rgb24" | "bgr24" | "nv12" | "nv21" | "yuyv422"
| "uyvy422" => Some(8),
"yuv420p10le" | "yuv422p10le" | "yuv444p10le" | "p010le" => Some(10),
"yuv420p12le" | "yuv422p12le" | "yuv444p12le" => Some(12),
"rgb48le" | "yuv444p16le" => Some(16),
_ => None,
}
}
pub fn has_alpha(&self) -> bool {
matches!(
self.0.as_ref(),
"rgba" | "bgra" | "argb" | "abgr" | "yuva420p" | "yuva444p"
)
}
}
impl std::fmt::Display for PixelFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
pub mod pixel_formats {
use super::PixelFormat;
pub fn yuv420p() -> PixelFormat {
PixelFormat::new("yuv420p")
}
pub fn yuv422p() -> PixelFormat {
PixelFormat::new("yuv422p")
}
pub fn yuv444p() -> PixelFormat {
PixelFormat::new("yuv444p")
}
pub fn yuv420p10le() -> PixelFormat {
PixelFormat::new("yuv420p10le")
}
pub fn yuv420p12le() -> PixelFormat {
PixelFormat::new("yuv420p12le")
}
pub fn rgb24() -> PixelFormat {
PixelFormat::new("rgb24")
}
pub fn rgba() -> PixelFormat {
PixelFormat::new("rgba")
}
pub fn nv12() -> PixelFormat {
PixelFormat::new("nv12")
}
pub fn p010le() -> PixelFormat {
PixelFormat::new("p010le")
}
}