use crate::{Transfer, hdr};
use super::bits_mask;
use crate::ColorMatrix;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum IctcpTransfer {
Pq,
Hlg,
}
impl IctcpTransfer {
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Pq => "pq",
Self::Hlg => "hlg",
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn for_transfer(transfer: &Transfer) -> Option<Self> {
match transfer {
Transfer::SmpteSt2084Pq => Some(Self::Pq),
Transfer::AribStdB67Hlg => Some(Self::Hlg),
_ => None,
}
}
}
const LMS_TO_RGB: [[f32; 3]; 3] = [
[3.436_606_6_f32, -2.506_452_f32, 0.069_845_42_f32],
[-0.791_329_56_f32, 1.9836005_f32, -0.19227089_f32],
[-0.025_949_9_f32, -0.098_913_714_f32, 1.124_863_6_f32],
];
const ICTCP_TO_LMSP_PQ: [[f32; 3]; 3] = [
[1.0_f32, 0.008_609_037_f32, 0.111029625_f32],
[1.0_f32, -0.008_609_037_f32, -0.111029625_f32],
[1.0_f32, 0.560_031_35_f32, -0.320_627_18_f32],
];
const ICTCP_TO_LMSP_HLG: [[f32; 3]; 3] = [
[1.0_f32, 0.015_718_58_f32, 0.209_581_06_f32],
[1.0_f32, -0.015_718_58_f32, -0.209_581_06_f32],
[1.0_f32, 1.021_271_1_f32, -0.605_274_5_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_ictcp<const BITS: u32>(i: u16, ct: u16, cp: 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)
};
[
(i as i32 - y_off) as f32 / y_range,
(ct as i32 - chroma_bias) as f32 / c_range,
(cp as i32 - chroma_bias) as f32 / c_range,
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn ictcp_norm_to_rgb_linear(norm: [f32; 3], transfer: IctcpTransfer) -> [f32; 3] {
let lms_p = match transfer {
IctcpTransfer::Pq => matmul3(&ICTCP_TO_LMSP_PQ, norm),
IctcpTransfer::Hlg => matmul3(&ICTCP_TO_LMSP_HLG, norm),
};
let lms = match transfer {
IctcpTransfer::Pq => [
hdr::pq_eotf(lms_p[0]),
hdr::pq_eotf(lms_p[1]),
hdr::pq_eotf(lms_p[2]),
],
IctcpTransfer::Hlg => [
hdr::hlg_inverse_oetf(lms_p[0]),
hdr::hlg_inverse_oetf(lms_p[1]),
hdr::hlg_inverse_oetf(lms_p[2]),
],
};
matmul3(&LMS_TO_RGB, lms)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn ictcp_norm_to_rgb_prime(norm: [f32; 3], transfer: IctcpTransfer) -> [f32; 3] {
let lin = ictcp_norm_to_rgb_linear(norm, transfer);
match transfer {
IctcpTransfer::Pq => [
hdr::pq_inverse_eotf(lin[0]),
hdr::pq_inverse_eotf(lin[1]),
hdr::pq_inverse_eotf(lin[2]),
],
IctcpTransfer::Hlg => [
hdr::hlg_oetf(lin[0]),
hdr::hlg_oetf(lin[1]),
hdr::hlg_oetf(lin[2]),
],
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn ictcp_pixel_to_rgb_prime<const BITS: u32, const BE: bool>(
i: u16,
ct: u16,
cp: u16,
full_range: bool,
transfer: IctcpTransfer,
) -> [f32; 3] {
let norm = dequant_ictcp::<BITS>(
load_sample::<BITS, BE>(i),
load_sample::<BITS, BE>(ct),
load_sample::<BITS, BE>(cp),
full_range,
);
ictcp_norm_to_rgb_prime(norm, transfer)
}
#[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 ictcp_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,
transfer: IctcpTransfer,
) {
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 = ictcp_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range, transfer);
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 ictcp_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,
transfer: IctcpTransfer,
) {
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 = ictcp_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range, transfer);
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 ictcp_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,
transfer: IctcpTransfer,
) {
ictcp_444p_n_to_rgb_or_rgba_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range, transfer);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn ictcp_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,
transfer: IctcpTransfer,
) {
ictcp_444p_n_to_rgb_or_rgba_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range, transfer);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn ictcp_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,
transfer: IctcpTransfer,
) {
ictcp_444p_n_to_rgb_or_rgba_u16_row::<BITS, false, BE>(
y, u, v, rgb_out, width, full_range, transfer,
);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn ictcp_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,
transfer: IctcpTransfer,
) {
ictcp_444p_n_to_rgb_or_rgba_u16_row::<BITS, true, BE>(
y, u, v, rgba_out, width, full_range, transfer,
);
}
#[cfg(all(test, feature = "std"))]
mod tests;