rskit_media/encoding/
color.rs1use std::sync::Arc;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum ColorSpace {
10 Bt601,
12 Bt709,
14 Bt2020,
16 Smpte240m,
18 Srgb,
20 DciP3,
22 DisplayP3,
24 Unknown,
26}
27
28impl ColorSpace {
29 pub fn as_ffmpeg_arg(&self) -> Option<&str> {
31 match self {
32 Self::Bt601 => Some("bt470bg"),
33 Self::Bt709 => Some("bt709"),
34 Self::Bt2020 => Some("bt2020nc"),
35 Self::Smpte240m => Some("smpte240m"),
36 Self::Srgb => Some("bt709"),
37 Self::DciP3 => Some("bt2020nc"),
38 Self::DisplayP3 => Some("bt2020nc"),
39 Self::Unknown => None,
40 }
41 }
42
43 pub fn from_ffmpeg(s: &str) -> Self {
45 match s {
46 "bt709" => Self::Bt709,
47 "bt470bg" | "smpte170m" => Self::Bt601,
48 "bt2020nc" | "bt2020c" => Self::Bt2020,
49 "smpte240m" => Self::Smpte240m,
50 _ => Self::Unknown,
51 }
52 }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
57pub enum ColorRange {
58 Limited,
60 Full,
62}
63
64impl ColorRange {
65 pub fn as_ffmpeg_arg(&self) -> &str {
67 match self {
68 Self::Limited => "tv",
69 Self::Full => "pc",
70 }
71 }
72
73 pub fn from_ffmpeg(s: &str) -> Option<Self> {
75 match s {
76 "tv" | "limited" | "mpeg" => Some(Self::Limited),
77 "pc" | "full" | "jpeg" => Some(Self::Full),
78 _ => None,
79 }
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
88pub struct PixelFormat(Arc<str>);
89
90impl PixelFormat {
91 pub fn new(id: impl Into<Arc<str>>) -> Self {
93 Self(id.into())
94 }
95
96 pub fn id(&self) -> &str {
98 &self.0
99 }
100
101 pub fn bit_depth(&self) -> Option<u8> {
103 match self.0.as_ref() {
104 "yuv420p" | "yuv422p" | "yuv444p" | "rgb24" | "bgr24" | "nv12" | "nv21" | "yuyv422"
105 | "uyvy422" => Some(8),
106 "yuv420p10le" | "yuv422p10le" | "yuv444p10le" | "p010le" => Some(10),
107 "yuv420p12le" | "yuv422p12le" | "yuv444p12le" => Some(12),
108 "rgb48le" | "yuv444p16le" => Some(16),
109 _ => None,
110 }
111 }
112
113 pub fn has_alpha(&self) -> bool {
115 matches!(
116 self.0.as_ref(),
117 "rgba" | "bgra" | "argb" | "abgr" | "yuva420p" | "yuva444p"
118 )
119 }
120}
121
122impl std::fmt::Display for PixelFormat {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 f.write_str(&self.0)
125 }
126}
127
128pub mod pixel_formats {
130 use super::PixelFormat;
131
132 pub fn yuv420p() -> PixelFormat {
134 PixelFormat::new("yuv420p")
135 }
136 pub fn yuv422p() -> PixelFormat {
138 PixelFormat::new("yuv422p")
139 }
140 pub fn yuv444p() -> PixelFormat {
142 PixelFormat::new("yuv444p")
143 }
144 pub fn yuv420p10le() -> PixelFormat {
146 PixelFormat::new("yuv420p10le")
147 }
148 pub fn yuv420p12le() -> PixelFormat {
150 PixelFormat::new("yuv420p12le")
151 }
152 pub fn rgb24() -> PixelFormat {
154 PixelFormat::new("rgb24")
155 }
156 pub fn rgba() -> PixelFormat {
158 PixelFormat::new("rgba")
159 }
160 pub fn nv12() -> PixelFormat {
162 PixelFormat::new("nv12")
163 }
164 pub fn p010le() -> PixelFormat {
166 PixelFormat::new("p010le")
167 }
168}