Skip to main content

zenpixels_convert/
ycbcr.rs

1//! ITU-T H.273 Table 4 YCbCr / GBR / YCgCo matrix coefficients ([`YcbcrMatrix`]).
2//!
3//! For reconstructing multi-plane `PlanarView` buffers (YCbCr, GBR, planar RGB/RGBA,
4//! or monochrome) into interleaved pixels, see [`crate::planar`]
5//! ([`reconstruct_planar_to_buf`](crate::reconstruct_planar_to_buf),
6//! [`reconstruct_planar_into`](crate::reconstruct_planar_into),
7//! [`reconstruct_planar_f32`](crate::reconstruct_planar_f32)).
8
9use zenpixels::{MatrixCoefficients, Primaries};
10
11/// YCbCr / GBR / YCgCo luma-chroma matrix standard (ITU-T H.273 Table 4).
12///
13/// Every distinct H.273 Table 4 non-constant-luminance code has its own variant so
14/// [`from_cicp_code`](Self::from_cicp_code) ↔ [`to_cicp_code`](Self::to_cicp_code) is
15/// 100% lossless (e.g. `5` = [`Bt470Bg`](Self::Bt470Bg) and `6` = [`Bt601`](Self::Bt601)
16/// stay distinct on the wire while sharing coefficients via
17/// [`same_coefficients`](Self::same_coefficients)).
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
19#[non_exhaustive]
20pub enum YcbcrMatrix {
21    /// Identity / GBR (`MC = 0`: `Y = G, Cb = B, Cr = R`, lossless RGB in planar form).
22    Identity,
23    /// ITU-R BT.709 (`MC = 1`: `Kr = 0.2126, Kb = 0.0722`).
24    Bt709,
25    /// FCC Title 47 CFR 73.682 (`MC = 4`: `Kr = 0.30, Kb = 0.11`).
26    Fcc,
27    /// ITU-R BT.470-6 System B, G (`MC = 5`: `Kr = 0.299, Kb = 0.114`).
28    Bt470Bg,
29    /// SMPTE 170M / ITU-R BT.601 (`MC = 6`: `Kr = 0.299, Kb = 0.114`).
30    Bt601,
31    /// SMPTE 240M (`MC = 7`: `Kr = 0.212, Kb = 0.087`).
32    Smpte240M,
33    /// YCgCo (`MC = 8`: `R = Y - Cg + Co, G = Y + Cg, B = Y - Cg - Co`).
34    YCgCo,
35    /// ITU-R BT.2020 NCL (`MC = 9`: `Kr = 0.2627, Kb = 0.0593`).
36    Bt2020,
37    /// Chromaticity-derived non-constant luminance (`MC = 12`: `Kr, Kb` derived from primaries).
38    ChromaDerivedNcl,
39}
40
41impl YcbcrMatrix {
42    /// Losslessly map an ITU-T H.273 Table 4 `matrix_coefficients` code to a [`YcbcrMatrix`].
43    ///
44    /// Recognizes `0` (`Identity` / GBR), `1` (`Bt709`), `4` (`Fcc`), `5` (`Bt470Bg`),
45    /// `6` (`Bt601`), `7` (`Smpte240M`), `8` (`YCgCo`), `9` (`Bt2020`), and `12`
46    /// (`ChromaDerivedNcl`). Returns `None` for unspecified (`2`), reserved (`3`), or
47    /// non-NCL matrix codes.
48    #[inline]
49    #[must_use]
50    pub const fn from_cicp_code(code: u8) -> Option<Self> {
51        match code {
52            0 => Some(Self::Identity),
53            1 => Some(Self::Bt709),
54            4 => Some(Self::Fcc),
55            5 => Some(Self::Bt470Bg),
56            6 => Some(Self::Bt601),
57            7 => Some(Self::Smpte240M),
58            8 => Some(Self::YCgCo),
59            9 => Some(Self::Bt2020),
60            12 => Some(Self::ChromaDerivedNcl),
61            _ => None,
62        }
63    }
64
65    /// Losslessly map a typed [`MatrixCoefficients`] wrapper to a [`YcbcrMatrix`].
66    ///
67    /// Returns `None` for unspecified (`2`), reserved (`3`), or non-NCL matrix codes
68    /// (such as `BT2020_CL` or `ICTCP`).
69    #[inline]
70    #[must_use]
71    pub const fn from_matrix_coefficients(mc: MatrixCoefficients) -> Option<Self> {
72        Self::from_cicp_code(mc.code())
73    }
74
75    /// Exact typed [`MatrixCoefficients`] for this matrix.
76    #[inline]
77    #[must_use]
78    pub const fn to_matrix_coefficients(self) -> MatrixCoefficients {
79        MatrixCoefficients::from_code(self.to_cicp_code())
80    }
81
82    /// Exact ITU-T H.273 Table 4 `matrix_coefficients` code for this matrix.
83    #[inline]
84    #[must_use]
85    pub const fn to_cicp_code(self) -> u8 {
86        match self {
87            Self::Identity => 0,
88            Self::Bt709 => 1,
89            Self::Fcc => 4,
90            Self::Bt470Bg => 5,
91            Self::Bt601 => 6,
92            Self::Smpte240M => 7,
93            Self::YCgCo => 8,
94            Self::Bt2020 => 9,
95            Self::ChromaDerivedNcl => 12,
96        }
97    }
98
99    /// Whether `self` and `other` define identical luma-chroma reconstruction coefficients
100    /// (e.g. `Bt470Bg` (`MC = 5`) and `Bt601` (`MC = 6`) share `Kr = 0.299, Kb = 0.114`).
101    #[inline]
102    #[must_use]
103    pub const fn same_coefficients(self, other: Self) -> bool {
104        matches!(
105            (self, other),
106            (Self::Identity, Self::Identity)
107                | (Self::Bt709, Self::Bt709)
108                | (Self::Fcc, Self::Fcc)
109                | (Self::Bt470Bg | Self::Bt601, Self::Bt470Bg | Self::Bt601)
110                | (Self::Smpte240M, Self::Smpte240M)
111                | (Self::YCgCo, Self::YCgCo)
112                | (Self::Bt2020, Self::Bt2020)
113                | (Self::ChromaDerivedNcl, Self::ChromaDerivedNcl)
114        )
115    }
116
117    /// Fixed `(Kr, Kb)` weights for standard NCL matrices (`None` for `Identity`, `YCgCo`,
118    /// or `ChromaDerivedNcl`).
119    #[inline]
120    #[must_use]
121    pub const fn kr_kb(self) -> Option<(f32, f32)> {
122        match self {
123            Self::Bt709 => Some((0.2126, 0.0722)),
124            Self::Fcc => Some((0.30, 0.11)),
125            Self::Bt470Bg | Self::Bt601 => Some((0.299, 0.114)),
126            Self::Smpte240M => Some((0.212, 0.087)),
127            Self::Bt2020 => Some((0.2627, 0.0593)),
128            Self::Identity | Self::YCgCo | Self::ChromaDerivedNcl => None,
129        }
130    }
131
132    /// Resolve `(Kr, Kb)` weights, deriving them from `primaries` when `self` is
133    /// [`ChromaDerivedNcl`](Self::ChromaDerivedNcl).
134    #[inline]
135    #[must_use]
136    pub fn kr_kb_for(self, primaries: Option<Primaries>) -> Option<(f32, f32)> {
137        if self == Self::ChromaDerivedNcl {
138            let p = primaries?;
139            if p == Primaries::Unknown {
140                return None;
141            }
142            let [kr, kg, kb] = p.luma_weights();
143            if kg <= 1e-6 {
144                return None;
145            }
146            return Some((kr, kb));
147        }
148        self.kr_kb()
149    }
150}
151
152impl From<YcbcrMatrix> for MatrixCoefficients {
153    #[inline]
154    fn from(matrix: YcbcrMatrix) -> Self {
155        matrix.to_matrix_coefficients()
156    }
157}