use super::PackedPlane;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorMatrix {
Bt601,
Bt709,
Fcc,
Smpte240m,
}
impl ColorMatrix {
pub fn kr_kb(self) -> (f64, f64) {
match self {
Self::Bt601 => (0.299, 0.114),
Self::Bt709 => (0.2126, 0.0722),
Self::Fcc => (0.30, 0.11),
Self::Smpte240m => (0.212, 0.087),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kr_kb_of_every_matrix() {
assert_eq!(ColorMatrix::Bt601.kr_kb(), (0.299, 0.114));
assert_eq!(ColorMatrix::Bt709.kr_kb(), (0.2126, 0.0722));
assert_eq!(ColorMatrix::Fcc.kr_kb(), (0.30, 0.11));
assert_eq!(ColorMatrix::Smpte240m.kr_kb(), (0.212, 0.087));
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorRange {
Limited,
Full,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ChromaSiting {
Left,
Center,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct YuvSpec {
pub matrix: ColorMatrix,
pub range: ColorRange,
pub siting: ChromaSiting,
}
impl YuvSpec {
pub fn new(matrix: ColorMatrix, range: ColorRange, siting: ChromaSiting) -> Self {
Self {
matrix,
range,
siting,
}
}
}
#[non_exhaustive]
pub enum YuvPlanes<'a, R: Runtime> {
Nv12 {
y: &'a Tensor<R>,
uv: &'a Tensor<R>,
},
I420 {
y: &'a Tensor<R>,
u: &'a Tensor<R>,
v: &'a Tensor<R>,
},
P010 {
y: &'a Tensor<R>,
uv: &'a Tensor<R>,
},
Nv12Packed {
y: PackedPlane<'a, R>,
uv: PackedPlane<'a, R>,
},
I420Packed {
y: PackedPlane<'a, R>,
u: PackedPlane<'a, R>,
v: PackedPlane<'a, R>,
},
P010Packed {
y: PackedPlane<'a, R>,
uv: PackedPlane<'a, R>,
},
}
impl<R: Runtime> Clone for YuvPlanes<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Runtime> Copy for YuvPlanes<'_, R> {}
impl<R: Runtime> std::fmt::Debug for YuvPlanes<'_, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.format_name())
}
}
impl<'a, R: Runtime> YuvPlanes<'a, R> {
pub fn format_name(&self) -> &'static str {
match self {
Self::Nv12 { .. } | Self::Nv12Packed { .. } => "NV12",
Self::I420 { .. } | Self::I420Packed { .. } => "I420",
Self::P010 { .. } | Self::P010Packed { .. } => "P010",
}
}
pub fn bits(&self) -> u32 {
match self {
Self::Nv12 { .. }
| Self::I420 { .. }
| Self::Nv12Packed { .. }
| Self::I420Packed { .. } => 8,
Self::P010 { .. } | Self::P010Packed { .. } => 10,
}
}
pub fn sample_bytes(&self) -> usize {
if self.bits() > 8 { 2 } else { 1 }
}
pub fn is_packed(&self) -> bool {
matches!(
self,
Self::Nv12Packed { .. } | Self::I420Packed { .. } | Self::P010Packed { .. }
)
}
pub fn y(&self) -> &'a Tensor<R> {
match *self {
Self::Nv12 { y, .. } | Self::I420 { y, .. } | Self::P010 { y, .. } => y,
Self::Nv12Packed { y, .. }
| Self::I420Packed { y, .. }
| Self::P010Packed { y, .. } => y.words,
}
}
}