use crate::{Transfer, hdr};
use super::bits_mask;
use crate::ColorMatrix;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Smpte2085Transfer {
Pq,
}
impl Smpte2085Transfer {
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Pq => "pq",
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn for_transfer(transfer: &Transfer) -> Option<Self> {
match transfer {
Transfer::SmpteSt2084Pq => Some(Self::Pq),
_ => None,
}
}
}
const WHITE_NORM_Z: f32 = 0.986_566_f32;
const WHITE_NORM_X: f32 = 0.991_902_f32;
const M_XYZ_TO_RGB_REC2020: [[f32; 3]; 3] = [
[1.716_651_f32, -0.355_670_78_f32, -0.253_366_3_f32],
[-0.666_684_3_f32, 1.616_481_2_f32, 0.015_768_547_f32],
[0.017_639_857_f32, -0.042_770_613_f32, 0.942_103_15_f32],
];
#[cfg_attr(not(tarpaulin), inline(always))]
fn matmul3(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] {
[
m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2],
m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2],
m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2],
]
}
#[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_smpte2085<const BITS: u32>(y: u16, dz: u16, dx: 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)
};
[
(y as i32 - y_off) as f32 / y_range,
(dz as i32 - chroma_bias) as f32 / c_range,
(dx as i32 - chroma_bias) as f32 / c_range,
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn recover_xyz_prime(norm: [f32; 3]) -> [f32; 3] {
let yp = norm[0];
let dz = norm[1];
let dx = norm[2];
let zp = (2.0_f32 * dz + yp) / WHITE_NORM_Z;
let xp = 2.0_f32 * dx + WHITE_NORM_X * yp;
[xp, yp, zp]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn smpte2085_norm_to_rgb_linear(norm: [f32; 3]) -> [f32; 3] {
let xyz_prime = recover_xyz_prime(norm);
let xyz = [
hdr::pq_eotf(xyz_prime[0]),
hdr::pq_eotf(xyz_prime[1]),
hdr::pq_eotf(xyz_prime[2]),
];
matmul3(&M_XYZ_TO_RGB_REC2020, xyz)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn smpte2085_norm_to_rgb_prime(norm: [f32; 3]) -> [f32; 3] {
let lin = smpte2085_norm_to_rgb_linear(norm);
[
hdr::pq_inverse_eotf(lin[0]),
hdr::pq_inverse_eotf(lin[1]),
hdr::pq_inverse_eotf(lin[2]),
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn smpte2085_pixel_to_rgb_prime<const BITS: u32, const BE: bool>(
y: u16,
dz: u16,
dx: u16,
full_range: bool,
) -> [f32; 3] {
let norm = dequant_smpte2085::<BITS>(
load_sample::<BITS, BE>(y),
load_sample::<BITS, BE>(dz),
load_sample::<BITS, BE>(dx),
full_range,
);
smpte2085_norm_to_rgb_prime(norm)
}
#[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 smpte2085_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,
) {
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 = smpte2085_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range);
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 smpte2085_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,
) {
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 = smpte2085_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range);
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 smpte2085_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,
) {
smpte2085_444p_n_to_rgb_or_rgba_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn smpte2085_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,
) {
smpte2085_444p_n_to_rgb_or_rgba_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn smpte2085_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,
) {
smpte2085_444p_n_to_rgb_or_rgba_u16_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn smpte2085_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,
) {
smpte2085_444p_n_to_rgb_or_rgba_u16_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range);
}
#[cfg(all(test, feature = "std"))]
mod tests;