use super::*;
use crate::{
ChromaLocation, ColorInfo, ColorMatrix, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer,
};
#[test]
fn interpretation_default_and_projections() {
assert_eq!(
St428Interpretation::default(),
St428Interpretation::FfmpegTabulated
);
assert!(St428Interpretation::FfmpegTabulated.is_ffmpeg_tabulated());
assert!(!St428Interpretation::FfmpegTabulated.is_cie_xyz());
assert_eq!(
St428Interpretation::FfmpegTabulated.as_str(),
"ffmpeg-tabulated"
);
assert!(St428Interpretation::CieXyz.is_cie_xyz());
assert!(!St428Interpretation::CieXyz.is_ffmpeg_tabulated());
assert_eq!(St428Interpretation::CieXyz.as_str(), "cie-xyz");
}
#[test]
fn sink_toggle_roundtrips() {
let mut rgb = std::vec![0u8; 4 * 2 * 3];
let sink = MixedSinker::<Yuv420p>::new(4, 2)
.with_rgb(&mut rgb)
.unwrap();
assert_eq!(
sink.st428_interpretation(),
St428Interpretation::FfmpegTabulated
);
let sink = sink.with_st428_interpretation(St428Interpretation::CieXyz);
assert_eq!(sink.st428_interpretation(), St428Interpretation::CieXyz);
let mut sink = sink;
sink.set_st428_interpretation(St428Interpretation::FfmpegTabulated);
assert_eq!(
sink.st428_interpretation(),
St428Interpretation::FfmpegTabulated
);
}
#[test]
fn guard_ffmpeg_tabulated_permits_everything() {
for matrix in [
KernelMatrix::ChromaDerivedNcl,
KernelMatrix::Bt709,
KernelMatrix::Bt601,
] {
for primaries in [Primaries::SmpteSt428, Primaries::Bt2020, Primaries::Bt709] {
assert!(
st428_chroma_derived_guard(matrix, &primaries, St428Interpretation::FfmpegTabulated)
.is_ok(),
"FfmpegTabulated must permit ({matrix:?}, {primaries:?})"
);
}
}
}
#[test]
fn guard_cie_xyz_rejects_chroma_derived_over_smpte_st428() {
let err = st428_chroma_derived_guard(
KernelMatrix::ChromaDerivedNcl,
&Primaries::SmpteSt428,
St428Interpretation::CieXyz,
)
.expect_err("ChromaDerivedNcl over SmpteSt428 must be rejected in CIE-XYZ mode");
let mapped: MixedSinkerError = MixedSinkerError::St428CieXyzUnsupported(err);
assert!(mapped.is_st_428_cie_xyz_unsupported());
}
#[test]
fn guard_cie_xyz_ignores_non_st428_and_fixed_matrices() {
for primaries in [Primaries::Bt2020, Primaries::Bt709, Primaries::Smpte170M] {
assert!(
st428_chroma_derived_guard(
KernelMatrix::ChromaDerivedNcl,
&primaries,
St428Interpretation::CieXyz
)
.is_ok(),
"CIE-XYZ must not affect ChromaDerivedNcl over {primaries:?}"
);
}
for matrix in [
KernelMatrix::Bt601,
KernelMatrix::Bt709,
KernelMatrix::Bt2020Ncl,
] {
assert!(
st428_chroma_derived_guard(matrix, &Primaries::SmpteSt428, St428Interpretation::CieXyz)
.is_ok(),
"CIE-XYZ must not affect the fixed {matrix:?} over SmpteSt428"
);
}
}
#[test]
fn cie_xyz_predicate_is_smpte_st428_only() {
assert!(Primaries::SmpteSt428.is_cie_xyz());
for primaries in [
Primaries::Bt709,
Primaries::Bt2020,
Primaries::Smpte170M,
Primaries::SmpteRp431,
Primaries::Unspecified,
] {
assert!(!primaries.is_cie_xyz(), "{primaries:?} is not CIE XYZ");
}
}
fn decode(
matrix: KernelMatrix,
primaries: Primaries,
interp: St428Interpretation,
) -> Result<std::vec::Vec<u8>, MixedSinkerError> {
let (w, h) = (4u32, 2u32);
let n = (w * h) as usize;
let cn = ((w / 2) * (h / 2)) as usize;
let y = std::vec![128u8; n];
let u = std::vec![110u8; cn];
let v = std::vec![140u8; cn];
let src = Yuv420pFrame::new(&y, &u, &v, w, h, w, w / 2, w / 2);
let mut rgb = std::vec![0u8; n * 3];
let spec = ColorSpec::from_info(
PixelFormat::Yuv420p,
ColorInfo::new(
primaries,
Transfer::Unspecified,
crate::ColorMatrix::from(matrix),
DynamicRange::Limited,
ChromaLocation::Left,
),
);
{
let mut sink = MixedSinker::<Yuv420p>::new(w as usize, h as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap()
.with_st428_interpretation(interp);
yuv420p_to(&src, false, sink.set_kernel_matrix(matrix))?;
}
Ok(rgb)
}
#[test]
fn cie_xyz_rejects_chroma_derived_smpte_st428_end_to_end() {
let err = decode(
KernelMatrix::ChromaDerivedNcl,
Primaries::SmpteSt428,
St428Interpretation::CieXyz,
)
.expect_err("CIE-XYZ ChromaDerivedNcl over SmpteSt428 must be rejected");
assert!(
matches!(err, MixedSinkerError::St428CieXyzUnsupported(_)),
"unexpected error variant: {err:?}"
);
}
#[test]
fn ffmpeg_tabulated_is_byte_identical_to_untoggled() {
let toggled = decode(
KernelMatrix::ChromaDerivedNcl,
Primaries::SmpteSt428,
St428Interpretation::FfmpegTabulated,
)
.expect("FFmpeg-tabulated ChromaDerivedNcl over SmpteSt428 decodes");
let untoggled = {
let (w, h) = (4u32, 2u32);
let n = (w * h) as usize;
let cn = ((w / 2) * (h / 2)) as usize;
let y = std::vec![128u8; n];
let u = std::vec![110u8; cn];
let v = std::vec![140u8; cn];
let src = Yuv420pFrame::new(&y, &u, &v, w, h, w, w / 2, w / 2);
let mut rgb = std::vec![0u8; n * 3];
let spec = ColorSpec::from_info(
PixelFormat::Yuv420p,
ColorInfo::new(
Primaries::SmpteSt428,
Transfer::Unspecified,
ColorMatrix::ChromaDerivedNcl,
DynamicRange::Limited,
ChromaLocation::Left,
),
);
{
let mut sink = MixedSinker::<Yuv420p>::new(w as usize, h as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap();
yuv420p_to(
&src,
false,
sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
}
rgb
};
assert_eq!(
toggled, untoggled,
"the default toggle must not perturb output"
);
}
#[test]
fn cie_xyz_is_a_no_op_for_non_st428_primaries() {
let tabulated = decode(
KernelMatrix::ChromaDerivedNcl,
Primaries::Bt2020,
St428Interpretation::FfmpegTabulated,
)
.expect("BT.2020 ChromaDerivedNcl decodes");
let cie_xyz = decode(
KernelMatrix::ChromaDerivedNcl,
Primaries::Bt2020,
St428Interpretation::CieXyz,
)
.expect("BT.2020 is unaffected by the CIE-XYZ interpretation");
assert_eq!(
tabulated, cie_xyz,
"non-ST 428-1 primaries derive identically in both modes"
);
}
const TOP_W: u32 = 4;
const TOP_H: u32 = 4;
fn top_frame() -> (std::vec::Vec<u8>, std::vec::Vec<u8>, std::vec::Vec<u8>) {
let (w, h) = (TOP_W as usize, TOP_H as usize);
let (cw, ch) = (w / 2, h / 2);
let y = std::vec![128u8; w * h];
let mut u = std::vec![0u8; cw * ch];
let mut v = std::vec![0u8; cw * ch];
for r in 0..ch {
for c in 0..cw {
u[r * cw + c] = (24 + r * 48).min(240) as u8;
v[r * cw + c] = (216 - r * 48).max(16) as u8;
}
}
(y, u, v)
}
fn top_row<'a>(
yp: &'a [u8],
up: &'a [u8],
vp: &'a [u8],
r: usize,
matrix: KernelMatrix,
) -> Yuv420pRow<'a> {
let w = TOP_W as usize;
let cw = w / 2;
let cr = r / 2;
Yuv420pRow::for_tests(
&yp[r * w..r * w + w],
&up[cr * cw..cr * cw + cw],
&vp[cr * cw..cr * cw + cw],
r,
matrix,
false,
)
}
fn top_spec(primaries: Primaries) -> ColorSpec {
ColorSpec::from_info(
PixelFormat::Yuv420p,
ColorInfo::new(
primaries,
Transfer::Unspecified,
ColorMatrix::ChromaDerivedNcl,
DynamicRange::Limited,
ChromaLocation::Top,
),
)
}
fn top_walker_decode(primaries: Primaries, interp: St428Interpretation) -> std::vec::Vec<u8> {
let (yp, up, vp) = top_frame();
let (w, h) = (TOP_W as usize, TOP_H as usize);
let src = Yuv420pFrame::new(&yp, &up, &vp, TOP_W, TOP_H, TOP_W, TOP_W / 2, TOP_W / 2);
let mut rgb = std::vec![0u8; w * h * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&top_spec(primaries))
.unwrap()
.with_st428_interpretation(interp);
yuv420p_to(
&src,
false,
sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
}
rgb
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_delay_ncl_direct_matches_walker() {
let (yp, up, vp) = top_frame();
let (w, h) = (TOP_W as usize, TOP_H as usize);
for primaries in [Primaries::Bt709, Primaries::SmpteSt428] {
let reference = top_walker_decode(primaries.clone(), St428Interpretation::FfmpegTabulated);
let mut rgb = std::vec![0u8; w * h * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&top_spec(primaries.clone()))
.unwrap();
crate::PixelSink::begin_frame(&mut sink, TOP_W, TOP_H).unwrap();
for r in 0..h {
crate::PixelSink::process(
&mut sink,
top_row(&yp, &up, &vp, r, KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
}
}
assert_eq!(
rgb, reference,
"{primaries:?}: the Top forward-delay decode must match the in-order walker"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_delay_freezes_submit_colorimetry_across_cie_xyz_switch() {
let (yp, up, vp) = top_frame();
let (w, h) = (TOP_W as usize, TOP_H as usize);
let bt709_ref = top_walker_decode(Primaries::Bt709, St428Interpretation::CieXyz);
let smpte_ref = top_walker_decode(Primaries::SmpteSt428, St428Interpretation::FfmpegTabulated);
assert_ne!(
bt709_ref, smpte_ref,
"Bt709 vs SmpteSt428 ChromaDerivedNcl must differ for this chroma"
);
let mut rgb = std::vec![0u8; w * h * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&top_spec(Primaries::Bt709))
.unwrap()
.with_st428_interpretation(St428Interpretation::CieXyz);
crate::PixelSink::begin_frame(&mut sink, TOP_W, TOP_H).unwrap();
crate::PixelSink::process(
&mut sink,
top_row(&yp, &up, &vp, 0, KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
crate::PixelSink::process(
&mut sink,
top_row(&yp, &up, &vp, 1, KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
assert!(
sink.chroma_top_pending.is_some(),
"the odd ChromaDerivedNcl row must be held pending its even-row flush"
);
sink
.set_color_spec(&top_spec(Primaries::SmpteSt428))
.unwrap();
assert_eq!(sink.primaries(), Primaries::SmpteSt428);
crate::PixelSink::process(&mut sink, top_row(&yp, &up, &vp, 2, KernelMatrix::Bt601))
.expect("the fixed-matrix even row is permitted and flushes the held row");
crate::PixelSink::process(&mut sink, top_row(&yp, &up, &vp, 3, KernelMatrix::Bt601))
.expect("the trailing odd row clamps co-sited");
}
let mut rgb_ref = std::vec![0u8; w * h * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgb(&mut rgb_ref)
.unwrap()
.with_color_spec(&top_spec(Primaries::Bt709))
.unwrap()
.with_st428_interpretation(St428Interpretation::CieXyz);
crate::PixelSink::begin_frame(&mut sink, TOP_W, TOP_H).unwrap();
crate::PixelSink::process(
&mut sink,
top_row(&yp, &up, &vp, 0, KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
crate::PixelSink::process(
&mut sink,
top_row(&yp, &up, &vp, 1, KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
crate::PixelSink::process(&mut sink, top_row(&yp, &up, &vp, 2, KernelMatrix::Bt601)).unwrap();
crate::PixelSink::process(&mut sink, top_row(&yp, &up, &vp, 3, KernelMatrix::Bt601)).unwrap();
}
assert_eq!(
rgb, rgb_ref,
"the held odd row must decode through its FROZEN Bt709 primaries, not the switched SmpteSt428"
);
}