use crate::{Primaries, Transfer, resample::bt2020_oetf};
use super::bits_mask;
use crate::ColorMatrix;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct ClSystem {
is_12_bit: bool,
}
impl ClSystem {
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn as_str(self) -> &'static str {
if self.is_12_bit {
"bt2020-cl-12"
} else {
"bt2020-cl-10"
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn is_12_bit(self) -> bool {
self.is_12_bit
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn resolve(primaries: &Primaries, transfer: &Transfer) -> Option<Self> {
match (primaries, transfer) {
(Primaries::Bt2020, Transfer::Bt2020_12Bit) => Some(Self { is_12_bit: true }),
(Primaries::Bt2020, Transfer::Bt2020_10Bit) => Some(Self { is_12_bit: false }),
_ => None,
}
}
}
const KR: f32 = 0.2627;
const KG: f32 = 0.6780;
const KB: f32 = 0.0593;
const NEG_2NB: f32 = 1.9404;
const POS_2PB: f32 = 1.5816;
const NEG_2NR: f32 = 1.7184;
const POS_2PR: f32 = 0.9936;
#[cfg_attr(not(tarpaulin), inline(always))]
fn load_sample<const BITS: u32, const BE: bool>(s: u16) -> u16 {
let raw = if BE { u16::from_be(s) } else { u16::from_le(s) };
raw & bits_mask::<BITS>()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn dequant_cl<const BITS: u32>(yc: u16, cbc: u16, crc: u16, full_range: bool) -> [f32; 3] {
let k: i32 = 1 << (BITS - 8);
let chroma_bias = 128 * k; let (y_off, y_range, c_range): (i32, f32, f32) = if full_range {
let in_max = ((1u32 << BITS) - 1) as f32;
(0, in_max, in_max)
} else {
(16 * k, (219 * k) as f32, (224 * k) as f32)
};
[
(yc as i32 - y_off) as f32 / y_range,
(cbc as i32 - chroma_bias) as f32 / c_range,
(crc as i32 - chroma_bias) as f32 / c_range,
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn cl_norm_to_rgb_linear(norm: [f32; 3], system: ClSystem) -> [f32; 3] {
let [yc_prime, cbc, crc] = norm;
let b_prime = yc_prime + cbc * if cbc <= 0.0 { NEG_2NB } else { POS_2PB };
let r_prime = yc_prime + crc * if crc <= 0.0 { NEG_2NR } else { POS_2PR };
let is12 = system.is_12_bit();
let yc = bt2020_oetf::oetf_inverse(yc_prime, is12);
let b = bt2020_oetf::oetf_inverse(b_prime, is12);
let r = bt2020_oetf::oetf_inverse(r_prime, is12);
let g = (yc - KB * b - KR * r) / KG;
[r, g, b]
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn cl_norm_to_rgb_prime(norm: [f32; 3], system: ClSystem) -> [f32; 3] {
let lin = cl_norm_to_rgb_linear(norm, system);
let is12 = system.is_12_bit();
[
bt2020_oetf::oetf(lin[0], is12),
bt2020_oetf::oetf(lin[1], is12),
bt2020_oetf::oetf(lin[2], is12),
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn cl_pixel_to_rgb_prime<const BITS: u32, const BE: bool>(
yc: u16,
cbc: u16,
crc: u16,
full_range: bool,
system: ClSystem,
) -> [f32; 3] {
let norm = dequant_cl::<BITS>(
load_sample::<BITS, BE>(yc),
load_sample::<BITS, BE>(cbc),
load_sample::<BITS, BE>(crc),
full_range,
);
cl_norm_to_rgb_prime(norm, system)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn narrow_unit_to_u8(c: f32) -> u8 {
let scaled = c.clamp(0.0_f32, 1.0_f32) * 255.0_f32 + 0.5_f32;
scaled.clamp(0.0_f32, 255.0_f32) as u8
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn narrow_unit_to_u16_native<const BITS: u32>(c: f32) -> u16 {
let out_max = ((1u32 << BITS) - 1) as f32;
let scaled = c.clamp(0.0_f32, 1.0_f32) * out_max + 0.5_f32;
scaled.clamp(0.0_f32, out_max) as u16
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn cl_444p_n_to_rgb_or_rgba_row<const BITS: u32, const ALPHA: bool, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
out: &mut [u8],
width: usize,
full_range: bool,
system: ClSystem,
) {
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width, "y row too short");
debug_assert!(u.len() >= width, "u row too short");
debug_assert!(v.len() >= width, "v row too short");
debug_assert!(out.len() >= width * bpp, "out row too short");
for x in 0..width {
let rgb = cl_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range, system);
out[x * bpp] = narrow_unit_to_u8(rgb[0]);
out[x * bpp + 1] = narrow_unit_to_u8(rgb[1]);
out[x * bpp + 2] = narrow_unit_to_u8(rgb[2]);
if ALPHA {
out[x * bpp + 3] = 0xFF;
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn cl_444p_n_to_rgb_or_rgba_u16_row<const BITS: u32, const ALPHA: bool, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
out: &mut [u16],
width: usize,
full_range: bool,
system: ClSystem,
) {
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width, "y row too short");
debug_assert!(u.len() >= width, "u row too short");
debug_assert!(v.len() >= width, "v row too short");
debug_assert!(out.len() >= width * bpp, "out row too short");
for x in 0..width {
let rgb = cl_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range, system);
out[x * bpp] = narrow_unit_to_u16_native::<BITS>(rgb[0]);
out[x * bpp + 1] = narrow_unit_to_u16_native::<BITS>(rgb[1]);
out[x * bpp + 2] = narrow_unit_to_u16_native::<BITS>(rgb[2]);
if ALPHA {
out[x * bpp + 3] = ((1u32 << BITS) - 1) as u16;
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn cl_444p_n_to_rgb_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u8],
width: usize,
full_range: bool,
system: ClSystem,
) {
cl_444p_n_to_rgb_or_rgba_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range, system);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn cl_444p_n_to_rgba_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u8],
width: usize,
full_range: bool,
system: ClSystem,
) {
cl_444p_n_to_rgb_or_rgba_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range, system);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn cl_444p_n_to_rgb_u16_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u16],
width: usize,
full_range: bool,
system: ClSystem,
) {
cl_444p_n_to_rgb_or_rgba_u16_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range, system);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn cl_444p_n_to_rgba_u16_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u16],
width: usize,
full_range: bool,
system: ClSystem,
) {
cl_444p_n_to_rgb_or_rgba_u16_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range, system);
}
#[cfg(all(test, feature = "std"))]
mod tests;