use core::fmt;
use mediaframe::color::ChromaCoord;
use thiserror::Error;
use super::{
HLG_NOMINAL_PEAK_NITS, KNEE_START, PQ_PEAK_NITS, REFERENCE_WHITE_NITS, SDR_DISPLAY_GAMMA, expf32,
hlg_inverse_oetf, hlg_system_gamma, powf32, pq_eotf,
};
use crate::{ColorSpec, Primaries, Transfer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HdrTransfer {
Pq,
Hlg,
}
impl HdrTransfer {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn of(transfer: &Transfer) -> Option<Self> {
match transfer {
Transfer::SmpteSt2084Pq => Some(Self::Pq),
Transfer::AribStdB67Hlg => Some(Self::Hlg),
_ => None,
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(self) -> &'static str {
match self {
Self::Pq => "smpte2084",
Self::Hlg => "arib-std-b67",
}
}
}
impl fmt::Display for HdrTransfer {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
#[error(
"pixon has no BT.709 conversion for a {transfer} picture over {primaries} primaries: \
the HDR to SDR mapping converts D65-white RGB gamuts only"
)]
pub struct UnsupportedPrimaries {
transfer: HdrTransfer,
primaries: Primaries,
}
impl UnsupportedPrimaries {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn transfer(&self) -> HdrTransfer {
self.transfer
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn primaries(&self) -> &Primaries {
&self.primaries
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error)]
#[non_exhaustive]
pub enum MapError {
#[error(
"the u16 R'G'B' holds {samples} samples and the u8 output {bytes} bytes; \
both must hold the same whole number of three-channel pixels"
)]
LengthMismatch {
samples: usize,
bytes: usize,
},
#[error("{bits} is not a sample depth a u16 R'G'B' buffer holds (1 to 16)")]
Depth {
bits: u32,
},
}
#[derive(Clone, PartialEq)]
pub struct SdrMapping {
pub(super) hdr: Option<Hdr>,
}
#[derive(Clone, PartialEq)]
pub(super) struct Hdr {
transfer: HdrTransfer,
pub(super) to_bt709: [[f32; 3]; 3],
pub(super) luminance: [f32; 3],
ootf_exponent: f32,
table: [f32; 256],
}
impl SdrMapping {
pub fn for_picture(
transfer: &Transfer,
primaries: &Primaries,
) -> Result<Self, UnsupportedPrimaries> {
let Some(hdr) = HdrTransfer::of(transfer) else {
return Ok(Self::passthrough());
};
let gamut = match primaries {
Primaries::Unspecified => Primaries::Bt2020,
other => other.clone(),
};
let refused = || UnsupportedPrimaries {
transfer: hdr,
primaries: primaries.clone(),
};
let to_xyz = rgb_to_xyz(&gamut).ok_or_else(refused)?;
let bt709_to_xyz = rgb_to_xyz(&Primaries::Bt709).ok_or_else(refused)?;
let xyz_to_bt709 = invert(&bt709_to_xyz).ok_or_else(refused)?;
let to_bt709 = narrow(&multiply(&xyz_to_bt709, &to_xyz));
let luminance = [
to_xyz[1][0] as f32,
to_xyz[1][1] as f32,
to_xyz[1][2] as f32,
];
let table = core::array::from_fn(|code| {
let signal = code as f32 / 255.0;
match hdr {
HdrTransfer::Pq => pq_light(signal),
HdrTransfer::Hlg => hlg_inverse_oetf(signal),
}
});
Ok(Self {
hdr: Some(Hdr {
transfer: hdr,
to_bt709,
luminance,
ootf_exponent: hlg_system_gamma(HLG_NOMINAL_PEAK_NITS) - 1.0,
table,
}),
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn from_spec(spec: &ColorSpec) -> Result<Self, UnsupportedPrimaries> {
Self::for_picture(&spec.transfer(), &spec.primaries())
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn passthrough() -> Self {
Self { hdr: None }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn is_passthrough(&self) -> bool {
self.hdr.is_none()
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn transfer(&self) -> Option<HdrTransfer> {
self.hdr.as_ref().map(|hdr| hdr.transfer)
}
#[must_use]
pub fn map_signal(&self, rgb: [f32; 3]) -> [f32; 3] {
let Some(hdr) = &self.hdr else {
return rgb;
};
let rgb = rgb.map(unit);
let light = match hdr.transfer {
HdrTransfer::Pq => rgb.map(pq_light),
HdrTransfer::Hlg => hdr.hlg_light(rgb.map(hlg_inverse_oetf)),
};
hdr.display(light)
}
pub fn map_rgb8(&self, rgb: &mut [u8]) {
let Some(hdr) = &self.hdr else {
return;
};
for pixel in rgb.as_chunks_mut::<3>().0 {
let linear = [
hdr.table[pixel[0] as usize],
hdr.table[pixel[1] as usize],
hdr.table[pixel[2] as usize],
];
let light = match hdr.transfer {
HdrTransfer::Pq => linear,
HdrTransfer::Hlg => hdr.hlg_light(linear),
};
let sdr = hdr.display(light);
pixel[0] = quantize(sdr[0]);
pixel[1] = quantize(sdr[1]);
pixel[2] = quantize(sdr[2]);
}
}
pub fn map_rgb16(&self, rgb: &[u16], bits: u32, out: &mut [u8]) -> Result<(), MapError> {
if !(1..=16).contains(&bits) {
return Err(MapError::Depth { bits });
}
if rgb.len() != out.len() || !rgb.len().is_multiple_of(3) {
return Err(MapError::LengthMismatch {
samples: rgb.len(),
bytes: out.len(),
});
}
let full = ((1u32 << bits) - 1) as f32;
let signal = |code: u16| (code as f32 / full).min(1.0);
for (pixel, sdr) in rgb
.as_chunks::<3>()
.0
.iter()
.zip(out.as_chunks_mut::<3>().0)
{
let mapped = self.map_signal([signal(pixel[0]), signal(pixel[1]), signal(pixel[2])]);
sdr[0] = quantize(mapped[0]);
sdr[1] = quantize(mapped[1]);
sdr[2] = quantize(mapped[2]);
}
Ok(())
}
}
impl fmt::Debug for SdrMapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.hdr {
None => f.write_str("SdrMapping::Passthrough"),
Some(hdr) => f
.debug_struct("SdrMapping")
.field("transfer", &hdr.transfer)
.field("to_bt709", &hdr.to_bt709)
.field("luminance", &hdr.luminance)
.finish_non_exhaustive(),
}
}
}
impl Hdr {
#[cfg_attr(not(tarpaulin), inline(always))]
fn hlg_light(&self, scene: [f32; 3]) -> [f32; 3] {
let ys =
self.luminance[0] * scene[0] + self.luminance[1] * scene[1] + self.luminance[2] * scene[2];
if ys <= 0.0 {
return [0.0; 3];
}
let gain = HLG_NOMINAL_PEAK_NITS * powf32(ys, self.ootf_exponent) / REFERENCE_WHITE_NITS;
[gain * scene[0], gain * scene[1], gain * scene[2]]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn display(&self, light: [f32; 3]) -> [f32; 3] {
let m = &self.to_bt709;
let bt709 = [
m[0][0] * light[0] + m[0][1] * light[1] + m[0][2] * light[2],
m[1][0] * light[0] + m[1][1] * light[1] + m[1][2] * light[2],
m[2][0] * light[0] + m[2][1] * light[1] + m[2][2] * light[2],
];
bt709.map(|channel| encode(knee(channel.max(0.0))))
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn pq_light(signal: f32) -> f32 {
pq_eotf(signal) * (PQ_PEAK_NITS / REFERENCE_WHITE_NITS)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(super) fn knee(light: f32) -> f32 {
if light <= KNEE_START {
light
} else {
let room = 1.0 - KNEE_START;
KNEE_START + room * (1.0 - expf32(-(light - KNEE_START) / room))
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn encode(light: f32) -> f32 {
powf32(light.min(1.0), 1.0 / SDR_DISPLAY_GAMMA).min(1.0)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn unit(signal: f32) -> f32 {
if signal > 0.0 { signal.min(1.0) } else { 0.0 }
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn quantize(signal: f32) -> u8 {
(signal * 255.0 + 0.5) as u8
}
pub(super) fn rgb_to_xyz(gamut: &Primaries) -> Option<[[f64; 3]; 3]> {
if gamut.is_cie_xyz() || gamut.white_point() != Primaries::Bt709.white_point() {
return None;
}
let [red, green, blue] = gamut.chromaticities()?;
let white = gamut.white_point()?;
let unit_y = |c: ChromaCoord| {
let (x, y) = (f64::from(c.x()) / 50_000.0, f64::from(c.y()) / 50_000.0);
[x / y, 1.0, (1.0 - x - y) / y]
};
let (r, g, b, w) = (unit_y(red), unit_y(green), unit_y(blue), unit_y(white));
let primaries = [[r[0], g[0], b[0]], [r[1], g[1], b[1]], [r[2], g[2], b[2]]];
let scale = apply(&invert(&primaries)?, w);
Some(core::array::from_fn(|row| {
core::array::from_fn(|column| primaries[row][column] * scale[column])
}))
}
fn invert(m: &[[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> {
let cofactor =
|r0: usize, r1: usize, c0: usize, c1: usize| m[r0][c0] * m[r1][c1] - m[r0][c1] * m[r1][c0];
let c = [
[
cofactor(1, 2, 1, 2),
-cofactor(1, 2, 0, 2),
cofactor(1, 2, 0, 1),
],
[
-cofactor(0, 2, 1, 2),
cofactor(0, 2, 0, 2),
-cofactor(0, 2, 0, 1),
],
[
cofactor(0, 1, 1, 2),
-cofactor(0, 1, 0, 2),
cofactor(0, 1, 0, 1),
],
];
let determinant = m[0][0] * c[0][0] + m[0][1] * c[0][1] + m[0][2] * c[0][2];
if determinant == 0.0 || !determinant.is_finite() {
return None;
}
Some(core::array::from_fn(|row| {
core::array::from_fn(|column| c[column][row] / determinant)
}))
}
fn multiply(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> [[f64; 3]; 3] {
core::array::from_fn(|row| {
core::array::from_fn(|column| (0..3).map(|k| a[row][k] * b[k][column]).sum())
})
}
fn apply(m: &[[f64; 3]; 3], v: [f64; 3]) -> [f64; 3] {
core::array::from_fn(|row| m[row][0] * v[0] + m[row][1] * v[1] + m[row][2] * v[2])
}
fn narrow(m: &[[f64; 3]; 3]) -> [[f32; 3]; 3] {
core::array::from_fn(|row| core::array::from_fn(|column| m[row][column] as f32))
}