use super::*;
use crate::{ChromaLocation, ColorMatrix};
const W: u32 = 16;
const H: u32 = 8;
fn ramp_yuv420p() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let ch = 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] = (16 + c * 24 + r * 3).min(240) as u8;
v[r * cw + c] = (240 - c * 24).max(16) as u8;
}
}
(y, u, v)
}
fn ref_upsample_center_h(c_half: &[u8], width: usize) -> Vec<u8> {
let half = width / 2;
let mut out = std::vec![0u8; width];
for j in 0..half {
let l = c_half[j.saturating_sub(1)] as i32;
let m = c_half[j] as i32;
let r = c_half[if j + 1 < half { j + 1 } else { j }] as i32;
out[2 * j] = ((l + 3 * m + 2) >> 2) as u8;
out[2 * j + 1] = ((3 * m + r + 2) >> 2) as u8;
}
out
}
fn ref_full_chroma(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut u444 = std::vec![0u8; w * h];
let mut v444 = std::vec![0u8; w * h];
for r in 0..h {
let cr = r / 2;
let urow = ref_upsample_center_h(&u420[cr * cw..cr * cw + cw], w);
let vrow = ref_upsample_center_h(&v420[cr * cw..cr * cw + cw], w);
u444[r * w..r * w + w].copy_from_slice(&urow);
v444[r * w..r * w + w].copy_from_slice(&vrow);
}
(u444, v444)
}
fn convert_rgb(loc: ChromaLocation, simd: bool) -> Vec<u8> {
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(simd);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
rgb
}
#[test]
fn center_upsample_kernel_matches_hand_computed() {
let c_half = [0u8, 0, 100, 100];
let mut out = [0u8; 8];
crate::row::scalar::chroma_upsample_2to1_center_h(&c_half, &mut out, 8);
assert_eq!(out, [0, 0, 0, 25, 75, 100, 100, 100]);
}
#[test]
fn center_upsample_kernel_clamps_edges() {
let c_half = [10u8, 20];
let mut out = [0u8; 4];
crate::row::scalar::chroma_upsample_2to1_center_h(&c_half, &mut out, 4);
assert_eq!(out, [10, 13, 18, 20]);
assert_eq!(out[0], c_half[0], "left edge even column is co-sited");
assert_eq!(out[3], c_half[1], "right edge odd column is co-sited");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn default_and_cosited_sitings_are_byte_identical() {
let baseline = convert_rgb(ChromaLocation::Unspecified, true);
for loc in [
ChromaLocation::Unspecified,
ChromaLocation::other("unassigned-99"),
ChromaLocation::Left,
] {
assert_eq!(
convert_rgb(loc.clone(), true),
baseline,
"siting {loc:?} must keep the byte-identical default decode"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn default_path_does_not_allocate_chroma_scratch() {
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Left);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let chroma_len = sink.chroma_full.len();
drop(sink);
assert_eq!(
chroma_len, 0,
"co-sited path must not grow the chroma scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_rgb_matches_upsample_then_444_reference() {
let (yp, up, vp) = ramp_yuv420p();
let (u444, v444) = ref_full_chroma(&up, &vp);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
convert_rgb(ChromaLocation::Center, true),
rgb_ref,
"centered 4:2:0 RGB must equal upsample-then-4:4:4"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_grows_chroma_scratch_to_full_width() {
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let chroma_len = sink.chroma_full.len();
drop(sink);
assert_eq!(
chroma_len,
2 * W as usize,
"centered path stages U+V at full width"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_forward_vblend_differs_from_center_and_bottom() {
let center = convert_rgb(ChromaLocation::Center, true);
assert_ne!(
convert_rgb(ChromaLocation::Top, true),
center,
"Top's forward vertical box blend must differ from Center on a vertically-varying ramp"
);
assert_ne!(
convert_rgb(ChromaLocation::Top, true),
convert_rgb(ChromaLocation::Bottom, true),
"Top (forward, odd rows) must differ from Bottom (backward, even rows)"
);
assert_ne!(
convert_rgb(ChromaLocation::Bottom, true),
center,
"Bottom's vertical box blend must differ from Center on a vertically-varying chroma ramp"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_phase_differs_from_default() {
assert_ne!(
convert_rgb(ChromaLocation::Center, true),
convert_rgb(ChromaLocation::Left, true),
"centered siting must shift chroma vs the co-sited default"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_path_simd_matches_scalar() {
assert_eq!(
convert_rgb(ChromaLocation::Center, true),
convert_rgb(ChromaLocation::Center, false),
"centered path must be bit-identical across the SIMD and scalar tiers"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_rgba_and_hsv_match_444_reference() {
let (yp, up, vp) = ramp_yuv420p();
let (u444, v444) = ref_full_chroma(&up, &vp);
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgba = std::vec![0u8; (W * H * 4) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgba(&mut rgba)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgba(&mut rgba_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
rgba, rgba_ref,
"centered RGBA must equal upsample-then-4:4:4"
);
}
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let (mut h, mut s, mut v) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_hsv(&mut h, &mut s, &mut v)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let (mut hr, mut sr, mut vr) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_hsv(&mut hr, &mut sr, &mut vr)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
(h, s, v),
(hr, sr, vr),
"centered HSV must equal upsample-then-4:4:4"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn color_spec_center_drives_decode_without_manual_chroma_call() {
use crate::{ColorInfo, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer, YuvOptions};
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let info = ColorInfo::new(
Primaries::Bt709,
Transfer::Bt709,
ColorMatrix::Bt601,
DynamicRange::Limited,
ChromaLocation::Center,
);
let spec = ColorSpec::from_info(PixelFormat::Yuv420p, info);
let opts = YuvOptions::from_color_spec(&spec);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
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, opts.full_range(), &mut sink).unwrap();
drop(sink);
assert_ne!(
rgb,
convert_rgb(ChromaLocation::Unspecified, true),
"ColorSpec ChromaLocation::Center must change the decode via the options path"
);
assert_eq!(
rgb,
convert_rgb(ChromaLocation::Center, true),
"ColorSpec-driven centered decode must equal the explicit centered path"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn center_chroma_derived_ncl_uses_primaries_not_bt709() {
use crate::{
ColorInfo, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer,
row::{
yuv_444_to_rgb_row, yuv_444_to_rgb_row_primaries, yuv_444_to_rgba_row,
yuv_444_to_rgba_row_primaries,
},
};
let w = W as usize;
let h = H as usize;
let (yp, up, vp) = ramp_yuv420p();
let (u444, v444) = ref_full_chroma(&up, &vp);
let prim = Primaries::Bt2020;
let info = ColorInfo::new(
prim.clone(),
Transfer::Bt709,
ColorMatrix::ChromaDerivedNcl,
DynamicRange::Limited,
ChromaLocation::Center,
);
let spec = ColorSpec::from_info(PixelFormat::Yuv420p, info);
let decode_rgb = |simd: bool| {
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, 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(&spec)
.unwrap()
.with_simd(simd);
yuv420p_to(
&src,
false,
sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
rgb
};
let mut rgb_derived = std::vec![0u8; w * h * 3];
let mut rgb_bt709 = std::vec![0u8; w * h * 3];
for r in 0..h {
let (ys, us, vs) = (
&yp[r * w..r * w + w],
&u444[r * w..r * w + w],
&v444[r * w..r * w + w],
);
yuv_444_to_rgb_row_primaries(
ys,
us,
vs,
&mut rgb_derived[r * w * 3..(r + 1) * w * 3],
w,
KernelMatrix::ChromaDerivedNcl,
&prim,
false,
true,
);
yuv_444_to_rgb_row(
ys,
us,
vs,
&mut rgb_bt709[r * w * 3..(r + 1) * w * 3],
w,
KernelMatrix::Bt709,
false,
true,
);
}
let rgb = decode_rgb(true);
assert_eq!(
rgb, rgb_derived,
"centered ChromaDerivedNcl RGB must use the primaries-derived coefficients"
);
assert_ne!(
rgb, rgb_bt709,
"centered ChromaDerivedNcl RGB must NOT decode as the BT.709 fallback (the bug)"
);
assert_ne!(
rgb_derived, rgb_bt709,
"sanity: Bt2020-derived and BT.709 coefficients differ on the chroma ramp"
);
assert_eq!(
rgb,
decode_rgb(false),
"centered ChromaDerivedNcl must be scalar-routed (no SIMD/scalar split)"
);
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgba = std::vec![0u8; w * h * 4];
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgba(&mut rgba)
.unwrap()
.with_color_spec(&spec)
.unwrap();
yuv420p_to(
&src,
false,
sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl),
)
.unwrap();
drop(sink);
let mut rgba_derived = std::vec![0u8; w * h * 4];
let mut rgba_bt709 = std::vec![0u8; w * h * 4];
for r in 0..h {
let (ys, us, vs) = (
&yp[r * w..r * w + w],
&u444[r * w..r * w + w],
&v444[r * w..r * w + w],
);
yuv_444_to_rgba_row_primaries(
ys,
us,
vs,
&mut rgba_derived[r * w * 4..(r + 1) * w * 4],
w,
KernelMatrix::ChromaDerivedNcl,
&prim,
false,
true,
);
yuv_444_to_rgba_row(
ys,
us,
vs,
&mut rgba_bt709[r * w * 4..(r + 1) * w * 4],
w,
KernelMatrix::Bt709,
false,
true,
);
}
assert_eq!(
rgba, rgba_derived,
"centered ChromaDerivedNcl RGBA must use the primaries-derived coefficients"
);
assert_ne!(
rgba, rgba_bt709,
"centered ChromaDerivedNcl RGBA must NOT decode as the BT.709 fallback (the bug)"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_alloc_failure_leaves_outputs_untouched() {
use crate::resample::ResampleError;
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut luma = std::vec![0xABu8; (W * H) as usize];
let mut rgb = std::vec![0xCDu8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_luma(&mut luma)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
super::super::arm_chroma_full_alloc_failure();
let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
drop(sink);
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"centered chroma-scratch refusal must surface as a recoverable AllocationFailed, got {err:?}"
);
assert!(
luma.iter().all(|&b| b == 0xAB),
"luma must be untouched on the centered alloc-failure path"
);
assert!(
rgb.iter().all(|&b| b == 0xCD),
"rgb must be untouched on the centered alloc-failure path"
);
}
#[cfg(feature = "yuva")]
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgb_scratch_alloc_failure_leaves_outputs_untouched() {
use crate::resample::ResampleError;
let (yp, up, vp) = ramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut luma = std::vec![0xABu8; (W * H) as usize];
let mut rgba = std::vec![0xCDu8; (W * H * 4) as usize];
let (mut hh, mut ss, mut vv) = (
std::vec![0xCDu8; (W * H) as usize],
std::vec![0xCDu8; (W * H) as usize],
std::vec![0xCDu8; (W * H) as usize],
);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_luma(&mut luma)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
super::super::arm_rgb_scratch_alloc_failure();
let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
drop(sink);
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"RGB-scratch refusal must surface as a recoverable AllocationFailed, got {err:?}"
);
assert!(
luma.iter().all(|&b| b == 0xAB),
"luma must be untouched on the rgb-scratch alloc-failure path"
);
}
fn vramp_yuv420p() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let ch = 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] = (20 + r * 40).min(240) as u8;
v[r * cw + c] = (220 - r * 40).max(16) as u8;
}
}
(y, u, v)
}
fn ref_full_chroma_bottom(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let ch = h / 2;
let mut u444 = std::vec![0u8; w * h];
let mut v444 = std::vec![0u8; w * h];
let vblend = |plane: &[u8], cr: usize, prev: usize| -> Vec<u8> {
(0..cw)
.map(|c| {
let a = plane[prev * cw + c] as u32;
let b = plane[cr * cw + c] as u32;
((a + b + 1) >> 1) as u8
})
.collect::<Vec<u8>>()
};
for r in 0..h {
let cr = r / 2;
let (uhalf, vhalf) = if r & 1 == 0 {
let prev = cr.saturating_sub(1);
(vblend(u420, cr, prev), vblend(v420, cr, prev))
} else {
let _ = ch;
(
u420[cr * cw..cr * cw + cw].to_vec(),
v420[cr * cw..cr * cw + cw].to_vec(),
)
};
let urow = ref_upsample_center_h(&uhalf, w);
let vrow = ref_upsample_center_h(&vhalf, w);
u444[r * w..r * w + w].copy_from_slice(&urow);
v444[r * w..r * w + w].copy_from_slice(&vrow);
}
(u444, v444)
}
fn convert_rgb_with(loc: ChromaLocation, simd: bool, yp: &[u8], up: &[u8], vp: &[u8]) -> Vec<u8> {
let src = Yuv420pFrame::new(yp, up, vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(simd);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
rgb
}
#[test]
fn bottom_even_kernel_matches_hand_computed() {
let prev = [0u8, 0, 100, 100];
let cur = [40u8, 40, 60, 60];
let mut out = [0u8; 8];
crate::row::scalar::chroma_upsample_420_bottom_even_h(&prev, &cur, &mut out, 8);
assert_eq!(out, [20, 20, 20, 35, 65, 80, 80, 80]);
}
#[test]
fn bottom_even_kernel_equals_center_when_rows_match() {
let cur = [10u8, 40, 90, 30];
let mut bottom = [0u8; 8];
let mut center = [0u8; 8];
crate::row::scalar::chroma_upsample_420_bottom_even_h(&cur, &cur, &mut bottom, 8);
crate::row::scalar::chroma_upsample_2to1_center_h(&cur, &mut center, 8);
assert_eq!(
bottom, center,
"prev == cur must collapse the vertical blend to the horizontal centered path"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_rgb_matches_vblend_then_444_reference() {
let (yp, up, vp) = vramp_yuv420p();
let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
rgb_ref,
"bottom-sited 4:2:0 RGB must equal vertical-blend + horizontal-upsample then 4:4:4"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_differs_from_center_and_default_vertically() {
let (yp, up, vp) = vramp_yuv420p();
let bottom = convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp);
assert_ne!(
bottom,
convert_rgb_with(ChromaLocation::Center, true, &yp, &up, &vp),
"Bottom (v=1) must differ from Center (horizontal-only) on a vertical chroma ramp"
);
assert_ne!(
bottom,
convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
"Bottom must differ from the vertical-replicate default"
);
assert_ne!(
bottom,
convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
"Top (forward, odd rows) must differ from Bottom's backward even-row blend"
);
}
fn ref_full_chroma_top(u420: &[u8], v420: &[u8], centered: bool) -> (Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let ch = h / 2;
let up_h = |half: &[u8]| -> Vec<u8> {
if centered {
ref_upsample_center_h(half, w)
} else {
ref_upsample_cosited_h(half, w)
}
};
let vblend = |plane: &[u8], cr: usize, next: usize| -> Vec<u8> {
(0..cw)
.map(|c| {
let a = plane[cr * cw + c] as u32;
let b = plane[next * cw + c] as u32;
((a + b + 1) >> 1) as u8
})
.collect::<Vec<u8>>()
};
let mut u444 = std::vec![0u8; w * h];
let mut v444 = std::vec![0u8; w * h];
for r in 0..h {
let cr = r / 2;
let (uhalf, vhalf) = if r & 1 == 0 {
(
u420[cr * cw..cr * cw + cw].to_vec(),
v420[cr * cw..cr * cw + cw].to_vec(),
)
} else {
let next = (cr + 1).min(ch - 1);
(vblend(u420, cr, next), vblend(v420, cr, next))
};
u444[r * w..r * w + w].copy_from_slice(&up_h(&uhalf));
v444[r * w..r * w + w].copy_from_slice(&up_h(&vhalf));
}
(u444, v444)
}
fn top_ref_rgb(centered: bool, yp: &[u8], up: &[u8], vp: &[u8]) -> Vec<u8> {
let (u444, v444) = ref_full_chroma_top(up, vp, centered);
let ref_src = Yuv444pFrame::new(yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
rgb_ref
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_rgb_matches_forward_reconstruct_reference() {
let (yp, up, vp) = vramp_yuv420p();
let rgb_ref = top_ref_rgb(true, &yp, &up, &vp);
assert_eq!(
convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
rgb_ref,
"top-sited 4:2:0 RGB (SIMD) must equal forward-blend + centered-upsample then 4:4:4"
);
assert_eq!(
convert_rgb_with(ChromaLocation::Top, false, &yp, &up, &vp),
rgb_ref,
"top-sited 4:2:0 RGB (scalar) must match the same reference"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn topleft_rgb_matches_cosited_forward_reference() {
let (yp, up, vp) = vramp_yuv420p();
let rgb_ref = top_ref_rgb(false, &yp, &up, &vp);
assert_eq!(
convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
rgb_ref,
"topleft-sited RGB (SIMD) must equal forward-blend + co-sited-replicate then 4:4:4"
);
assert_eq!(
convert_rgb_with(ChromaLocation::TopLeft, false, &yp, &up, &vp),
rgb_ref,
"topleft-sited RGB (scalar) must match the same reference"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_differs_from_topleft_horizontally() {
let (yp, up, vp) = ramp_yuv420p();
assert_ne!(
convert_rgb_with(ChromaLocation::Top, true, &yp, &up, &vp),
convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
"Top (h=0.5) must differ from TopLeft (h=0) on a horizontal chroma ramp"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_rgba_and_hsv_match_forward_reference() {
let (yp, up, vp) = vramp_yuv420p();
let (u444, v444) = ref_full_chroma_top(&up, &vp, true);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
{
let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
let mut rs = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgba(&mut rgba_ref)
.unwrap();
yuv444p_to(&ref_src, false, rs.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgba = std::vec![0u8; (W * H * 4) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgba(&mut rgba)
.unwrap()
.with_chroma_location(ChromaLocation::Top);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
assert_eq!(rgba, rgba_ref, "Top RGBA must match the forward reference");
}
{
let (mut hh, mut ss, mut vv) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut rs = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv444p_to(&ref_src, false, rs.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let (mut h2, mut s2, mut v2) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_hsv(&mut h2, &mut s2, &mut v2)
.unwrap()
.with_chroma_location(ChromaLocation::Top);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
assert_eq!(
(h2, s2, v2),
(hh, ss, vv),
"Top HSV-direct must match the forward reference"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_maintains_forward_delay_buffers() {
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Top);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
assert_eq!(
sink.chroma_prev.len(),
W as usize,
"Top maintains the width-byte (half U + half V) chroma lookback"
);
assert_eq!(
sink.chroma_top_y.len(),
W as usize,
"Top allocates the forward-delay Y buffer"
);
drop(sink);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_survives_frame_reuse() {
let (yp, up, vp) = vramp_yuv420p();
let rgb_ref = top_ref_rgb(true, &yp, &up, &vp);
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Top);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
drop(sink);
assert_eq!(
rgb, rgb_ref,
"second frame must match a fresh Top reconstruct"
);
}
fn top_delay_row<'a>(yp: &'a [u8], up: &'a [u8], vp: &'a [u8], r: usize) -> Yuv420pRow<'a> {
let w = 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,
KernelMatrix::Bt601,
false,
)
}
fn top_flip_rejected_and_retryable(held_loc: ChromaLocation, flipped_loc: ChromaLocation) {
let (yp, up, vp) = vramp_yuv420p();
let w = W as usize;
let mut rgb_ref = std::vec![0u8; w * H as usize * 3];
{
let mut ref_sink = MixedSinker::<Yuv420p>::new(w, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap()
.with_chroma_location(held_loc.clone());
crate::PixelSink::begin_frame(&mut ref_sink, W, H).unwrap();
for r in 0..3 {
crate::PixelSink::process(&mut ref_sink, top_delay_row(&yp, &up, &vp, r)).unwrap();
}
}
let mut rgb = std::vec![0u8; w * H as usize * 3];
let mut sink = MixedSinker::<Yuv420p>::new(w, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(held_loc.clone());
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 0)).unwrap();
crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 1)).unwrap();
assert!(
sink.chroma_top_pending.is_some(),
"{held_loc:?}: the odd row must be held pending its even-row flush"
);
let held_y = sink.chroma_top_y.clone();
sink.set_chroma_location(flipped_loc.clone());
let err = crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 2)).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"{held_loc:?} -> {flipped_loc:?}: mid-frame flip must be ChromaSitingChanged, got {err:?}"
);
assert!(
sink.chroma_top_pending.is_some(),
"{held_loc:?} -> {flipped_loc:?}: a rejected flip must leave the held odd row pending"
);
assert_eq!(
sink.chroma_top_y, held_y,
"{held_loc:?} -> {flipped_loc:?}: a rejected flip must not touch the buffered odd-row luma"
);
sink.set_chroma_location(held_loc.clone());
crate::PixelSink::process(&mut sink, top_delay_row(&yp, &up, &vp, 2)).unwrap();
drop(sink);
assert_eq!(
rgb, rgb_ref,
"{held_loc:?}: retry after a rejected flip must match a clean in-order decode"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_mid_frame_flip_to_center_rejected_and_retryable() {
top_flip_rejected_and_retryable(ChromaLocation::Top, ChromaLocation::Center);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn topleft_mid_frame_flip_to_left_rejected_and_retryable() {
top_flip_rejected_and_retryable(ChromaLocation::TopLeft, ChromaLocation::Left);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_rgba_and_hsv_match_vblend_then_444_reference() {
let (yp, up, vp) = vramp_yuv420p();
let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgba = std::vec![0u8; (W * H * 4) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgba(&mut rgba)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgba(&mut rgba_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(rgba, rgba_ref, "bottom RGBA must equal vblend-then-4:4:4");
}
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let (mut h, mut s, mut v) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_hsv(&mut h, &mut s, &mut v)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let (mut hr, mut sr, mut vr) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_hsv(&mut hr, &mut sr, &mut vr)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
(h, s, v),
(hr, sr, vr),
"bottom HSV must equal vblend-then-4:4:4"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_path_simd_matches_scalar() {
let (yp, up, vp) = vramp_yuv420p();
assert_eq!(
convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
convert_rgb_with(ChromaLocation::Bottom, false, &yp, &up, &vp),
"bottom path must be bit-identical across the SIMD and scalar tiers"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_grows_chroma_prev_lookback() {
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let prev_len = sink.chroma_prev.len();
drop(sink);
assert_eq!(
prev_len, W as usize,
"bottom-sited path stages a width-byte (half-width U+V) chroma lookback"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn non_bottom_sitings_do_not_grow_chroma_prev() {
for loc in [
ChromaLocation::Center,
ChromaLocation::Left,
ChromaLocation::Unspecified,
] {
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc.clone());
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let prev_len = sink.chroma_prev.len();
drop(sink);
assert_eq!(
prev_len, 0,
"siting {loc:?} must not grow the vertical chroma lookback"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_chroma_prev_alloc_failure_leaves_outputs_untouched() {
use crate::resample::ResampleError;
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut luma = std::vec![0xABu8; (W * H) as usize];
let mut rgb = std::vec![0xCDu8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_luma(&mut luma)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
super::super::arm_chroma_prev_alloc_failure();
let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
drop(sink);
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"bottom chroma-lookback refusal must surface as a recoverable AllocationFailed, got {err:?}"
);
assert!(
luma.iter().all(|&b| b == 0xAB),
"luma must be untouched on the bottom lookback alloc-failure path"
);
assert!(
rgb.iter().all(|&b| b == 0xCD),
"rgb must be untouched on the bottom lookback alloc-failure path"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_luma_only_chroma_prev_alloc_failure_leaves_luma_untouched() {
use crate::resample::ResampleError;
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut luma = std::vec![0xABu8; (W * H) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_luma(&mut luma)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
super::super::arm_chroma_prev_alloc_failure();
let err = yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap_err();
drop(sink);
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"luma-only bottom lookback refusal must surface as a recoverable AllocationFailed, got {err:?}"
);
assert!(
luma.iter().all(|&b| b == 0xAB),
"luma must be untouched when the luma-only bottom lookback reservation fails"
);
}
fn ref_centered_row_rgb(
yp: &[u8],
up: &[u8],
vp: &[u8],
chroma_row: usize,
out_row: usize,
) -> Vec<u8> {
let w = W as usize;
let cw = w / 2;
let urow = ref_upsample_center_h(&up[chroma_row * cw..chroma_row * cw + cw], w);
let vrow = ref_upsample_center_h(&vp[chroma_row * cw..chroma_row * cw + cw], w);
let mut rgb = std::vec![0u8; w * 3];
crate::row::yuv_444_to_rgb_row(
&yp[out_row * w..out_row * w + w],
&urow,
&vrow,
&mut rgb,
w,
KernelMatrix::Bt601,
false,
true,
);
rgb
}
fn drive_bottom_rows(yp: &[u8], up: &[u8], vp: &[u8], rows: &[usize]) -> Vec<u8> {
let w = W as usize;
let h = H as usize;
let cw = 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_chroma_location(ChromaLocation::Bottom);
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
for &r in rows {
let cr = r / 2;
let row = Yuv420pRow::for_tests(
&yp[r * w..r * w + w],
&up[cr * cw..cr * cw + cw],
&vp[cr * cw..cr * cw + cw],
r,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(&mut sink, row).unwrap();
}
drop(sink);
rgb
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_even_row_without_valid_prev_clamps_not_stale() {
let (yp, up, vp) = vramp_yuv420p();
let w = W as usize;
let rgb = drive_bottom_rows(&yp, &up, &vp, &[2]);
let got_row2 = &rgb[2 * w * 3..3 * w * 3];
let want = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
assert_eq!(
got_row2,
&want[..],
"an even row with no valid vertical predecessor must clamp to the centered \
(horizontal-only) decode, never blend stale data"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_even_row_after_skip_clamps_not_stale() {
let (yp, up, vp) = vramp_yuv420p();
let w = W as usize;
let rgb = drive_bottom_rows(&yp, &up, &vp, &[0, 1, 4]);
let got_row4 = &rgb[4 * w * 3..5 * w * 3];
let want = ref_centered_row_rgb(&yp, &up, &vp, 2, 4);
assert_eq!(
got_row4,
&want[..],
"an even row whose lookback holds a non-adjacent (stale) chroma row must \
clamp, never blend the stale row"
);
let stale = {
let cw = w / 2;
let mut e_u = std::vec![0u8; cw];
let mut e_v = std::vec![0u8; cw];
for c in 0..cw {
e_u[c] = (((up[c] as u32) + (up[2 * cw + c] as u32) + 1) >> 1) as u8;
e_v[c] = (((vp[c] as u32) + (vp[2 * cw + c] as u32) + 1) >> 1) as u8;
}
let urow = ref_upsample_center_h(&e_u, w);
let vrow = ref_upsample_center_h(&e_v, w);
let mut rgb = std::vec![0u8; w * 3];
crate::row::yuv_444_to_rgb_row(
&yp[4 * w..4 * w + w],
&urow,
&vrow,
&mut rgb,
w,
KernelMatrix::Bt601,
false,
true,
);
rgb
};
assert_ne!(
got_row4,
&stale[..],
"guard: the clamp must NOT coincide with the stale (row0xrow2) blend"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_two_frames_no_cross_frame_stale_blend() {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let ch = h / 2;
let (ya, ua, va) = vramp_yuv420p();
let yb = std::vec![128u8; w * h];
let mut ub = std::vec![0u8; cw * ch];
let mut vb = std::vec![0u8; cw * ch];
for r in 0..ch {
for c in 0..cw {
ub[r * cw + c] = (200 - r * 30).max(16) as u8;
vb[r * cw + c] = (30 + r * 30).min(240) as u8;
}
}
let mut rgb = std::vec![0u8; w * h * 3];
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
for r in 0..h {
let cr = r / 2;
let row = Yuv420pRow::for_tests(
&ya[r * w..r * w + w],
&ua[cr * cw..cr * cw + cw],
&va[cr * cw..cr * cw + cw],
r,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(&mut sink, row).unwrap();
}
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
let row0 = Yuv420pRow::for_tests(
&yb[0..w],
&ub[0..cw],
&vb[0..cw],
0,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(&mut sink, row0).unwrap();
drop(sink);
let got_row0 = &rgb[0..w * 3];
let want = ref_centered_row_rgb(&yb, &ub, &vb, 0, 0);
assert_eq!(
got_row0,
&want[..],
"frame 2 row 0 must clamp to plane B's centered decode, never blend frame 1's chroma"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_luma_only_then_late_color_box_blends() {
let (yp, up, vp) = vramp_yuv420p();
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; w * h * 3];
let mut ref_sink = MixedSinker::<Yuv444p>::new(w, h)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
drop(ref_sink);
let mut luma = std::vec![0u8; w * h];
let mut rgb = std::vec![0u8; w * h * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(w, h)
.with_luma(&mut luma)
.unwrap()
.with_chroma_location(ChromaLocation::Bottom);
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
let feed = |sink: &mut MixedSinker<'_, Yuv420p>, r: usize| {
let cr = r / 2;
let row = Yuv420pRow::for_tests(
&yp[r * w..r * w + w],
&up[cr * cw..cr * cw + cw],
&vp[cr * cw..cr * cw + cw],
r,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(sink, row).unwrap();
};
feed(&mut sink, 0);
feed(&mut sink, 1);
sink.set_rgb(&mut rgb).unwrap();
feed(&mut sink, 2);
feed(&mut sink, 3);
}
let got_row2 = &rgb[2 * w * 3..3 * w * 3];
assert_eq!(
got_row2,
&rgb_ref[2 * w * 3..3 * w * 3],
"a luma-only-then-late-colour row 2 must box-blend chroma rows 0,1 (all-output reference)"
);
let clamp = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
assert_ne!(
got_row2,
&clamp[..],
"row 2 must NOT clamp — the lookback was maintained through the luma-only rows"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_no_output_row_does_not_grow_chroma_prev() {
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_chroma_location(ChromaLocation::Bottom);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let prev_len = sink.chroma_prev.len();
drop(sink);
assert_eq!(
prev_len, 0,
"a no-output bottom-sited row must not reserve the vertical lookback"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_no_output_rows_do_not_enable_late_color_blend() {
let (yp, up, vp) = vramp_yuv420p();
let w = W as usize;
let cw = w / 2;
let mut rgb = std::vec![0u8; w * H as usize * 3];
{
let mut sink =
MixedSinker::<Yuv420p>::new(w, H as usize).with_chroma_location(ChromaLocation::Bottom);
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
let feed = |sink: &mut MixedSinker<'_, Yuv420p>, r: usize| {
let cr = r / 2;
let row = Yuv420pRow::for_tests(
&yp[r * w..r * w + w],
&up[cr * cw..cr * cw + cw],
&vp[cr * cw..cr * cw + cw],
r,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(sink, row).unwrap();
};
feed(&mut sink, 0);
feed(&mut sink, 1);
sink.set_rgb(&mut rgb).unwrap();
feed(&mut sink, 2);
feed(&mut sink, 3);
}
let got_row2 = &rgb[2 * w * 3..3 * w * 3];
let clamp = ref_centered_row_rgb(&yp, &up, &vp, 1, 2);
assert_eq!(
got_row2,
&clamp[..],
"a no-output predecessor row must not enable a later colour even row to box-blend through it"
);
let (u444, v444) = ref_full_chroma_bottom(&up, &vp);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; w * H as usize * 3];
let mut ref_sink = MixedSinker::<Yuv444p>::new(w, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
drop(ref_sink);
assert_ne!(
got_row2,
&rgb_ref[2 * w * 3..3 * w * 3],
"guard: the clamp must differ from the box-blend, so the no-output rows were truly invisible"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "constructs an absurd geometry; the no-op contract is the point, not Miri"
)]
fn bottom_no_output_row_large_geometry_does_not_overflow() {
let w: usize = 4;
let idx: usize = 1 << 30; let h: usize = idx + 1; assert!(
(idx as u64) * (w as u64) > u32::MAX as u64,
"test geometry must exceed u32::MAX to exercise the 32-bit offset overflow"
);
let y = std::vec![128u8; w];
let c = std::vec![128u8; w / 2];
let mut sink = MixedSinker::<Yuv420p>::new(w, h).with_chroma_location(ChromaLocation::Bottom);
let row = Yuv420pRow::for_tests(&y, &c, &c, idx, KernelMatrix::Bt601, false);
crate::PixelSink::process(&mut sink, row).unwrap();
let prev_len = sink.chroma_prev.len();
drop(sink);
assert_eq!(
prev_len, 0,
"a no-output large-geometry row must allocate nothing (lookback included)"
);
}
fn ref_upsample_cosited_h(c_half: &[u8], width: usize) -> Vec<u8> {
let half = width / 2;
let mut out = std::vec![0u8; width];
for j in 0..half {
out[2 * j] = c_half[j];
out[2 * j + 1] = c_half[j];
}
out
}
fn ref_full_chroma_bottomleft(u420: &[u8], v420: &[u8]) -> (Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut u444 = std::vec![0u8; w * h];
let mut v444 = std::vec![0u8; w * h];
let vblend = |plane: &[u8], cr: usize, prev: usize| -> Vec<u8> {
(0..cw)
.map(|c| {
let a = plane[prev * cw + c] as u32;
let b = plane[cr * cw + c] as u32;
((a + b + 1) >> 1) as u8
})
.collect::<Vec<u8>>()
};
for r in 0..h {
let cr = r / 2;
let (uhalf, vhalf) = if r & 1 == 0 {
let prev = cr.saturating_sub(1);
(vblend(u420, cr, prev), vblend(v420, cr, prev))
} else {
(
u420[cr * cw..cr * cw + cw].to_vec(),
v420[cr * cw..cr * cw + cw].to_vec(),
)
};
let urow = ref_upsample_cosited_h(&uhalf, w);
let vrow = ref_upsample_cosited_h(&vhalf, w);
u444[r * w..r * w + w].copy_from_slice(&urow);
v444[r * w..r * w + w].copy_from_slice(&vrow);
}
(u444, v444)
}
#[test]
fn cosited_upsample_kernel_matches_hand_computed() {
let c_half = [10u8, 20, 30, 40];
let mut out = [0u8; 8];
crate::row::scalar::chroma_upsample_2to1_cosited_h(&c_half, &mut out, 8);
assert_eq!(out, [10, 10, 20, 20, 30, 30, 40, 40]);
}
#[test]
fn bottomleft_even_kernel_matches_hand_computed() {
let prev = [0u8, 0, 100, 100];
let cur = [40u8, 40, 60, 60];
let mut out = [0u8; 8];
crate::row::scalar::chroma_upsample_420_bottomleft_even_h(&prev, &cur, &mut out, 8);
assert_eq!(out, [20, 20, 20, 20, 80, 80, 80, 80]);
}
#[test]
fn bottomleft_even_kernel_equals_cosited_when_rows_match() {
let cur = [10u8, 40, 90, 30];
let mut bl = [0u8; 8];
let mut cosited = [0u8; 8];
crate::row::scalar::chroma_upsample_420_bottomleft_even_h(&cur, &cur, &mut bl, 8);
crate::row::scalar::chroma_upsample_2to1_cosited_h(&cur, &mut cosited, 8);
assert_eq!(
bl, cosited,
"prev == cur must collapse the vertical blend to the co-sited replicate"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_rgb_matches_cosited_vblend_then_444_reference() {
let (yp, up, vp) = vramp_yuv420p();
let (u444, v444) = ref_full_chroma_bottomleft(&up, &vp);
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgb_ref = std::vec![0u8; (W * H * 3) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgb(&mut rgb_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
rgb_ref,
"bottom-left 4:2:0 RGB must equal co-sited-replicate + vertical-blend then 4:4:4"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_differs_from_bottom_horizontally() {
let (yp, up, vp) = ramp_yuv420p();
assert_ne!(
convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
convert_rgb_with(ChromaLocation::Bottom, true, &yp, &up, &vp),
"BottomLeft (h=0) must differ from Bottom (h=0.5) on a horizontal chroma ramp"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_differs_from_topleft_vertically() {
let (yp, up, vp) = vramp_yuv420p();
let bl = convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp);
assert_ne!(
bl,
convert_rgb_with(ChromaLocation::TopLeft, true, &yp, &up, &vp),
"BottomLeft (v=1 backward) must differ from TopLeft (v=0 forward) on a vertical ramp"
);
assert_ne!(
bl,
convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
"BottomLeft must differ from the vertical-replicate default"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_rgba_and_hsv_match_cosited_vblend_then_444_reference() {
let (yp, up, vp) = vramp_yuv420p();
let (u444, v444) = ref_full_chroma_bottomleft(&up, &vp);
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgba = std::vec![0u8; (W * H * 4) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgba(&mut rgba)
.unwrap()
.with_chroma_location(ChromaLocation::BottomLeft);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let mut rgba_ref = std::vec![0u8; (W * H * 4) as usize];
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_rgba(&mut rgba_ref)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
rgba, rgba_ref,
"bottom-left RGBA must equal cosited-vblend-then-4:4:4"
);
}
{
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let (mut h, mut s, mut v) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_hsv(&mut h, &mut s, &mut v)
.unwrap()
.with_chroma_location(ChromaLocation::BottomLeft);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let ref_src = Yuv444pFrame::new(&yp, &u444, &v444, W, H, W, W, W);
let (mut hr, mut sr, mut vr) = (
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
std::vec![0u8; (W * H) as usize],
);
let mut ref_sink = MixedSinker::<Yuv444p>::new(W as usize, H as usize)
.with_hsv(&mut hr, &mut sr, &mut vr)
.unwrap();
yuv444p_to(
&ref_src,
false,
ref_sink.set_kernel_matrix(KernelMatrix::Bt601),
)
.unwrap();
assert_eq!(
(h, s, v),
(hr, sr, vr),
"bottom-left HSV must equal cosited-vblend-then-4:4:4"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_path_simd_matches_scalar() {
let (yp, up, vp) = vramp_yuv420p();
assert_eq!(
convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
convert_rgb_with(ChromaLocation::BottomLeft, false, &yp, &up, &vp),
"bottom-left path must be bit-identical across the SIMD and scalar tiers"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_equals_cosited_on_flat_chroma() {
let w = W as usize;
let h = H as usize;
let yp = std::vec![128u8; w * h];
let up = std::vec![90u8; (w / 2) * (h / 2)];
let vp = std::vec![150u8; (w / 2) * (h / 2)];
assert_eq!(
convert_rgb_with(ChromaLocation::BottomLeft, true, &yp, &up, &vp),
convert_rgb_with(ChromaLocation::Left, true, &yp, &up, &vp),
"flat-chroma BottomLeft must collapse to the co-sited decode"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottomleft_grows_chroma_prev_lookback() {
let (yp, up, vp) = vramp_yuv420p();
let src = Yuv420pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<Yuv420p>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::BottomLeft);
yuv420p_to(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let prev_len = sink.chroma_prev.len();
drop(sink);
assert_eq!(
prev_len, W as usize,
"bottom-left-sited path stages a width-byte (half-width U+V) chroma lookback"
);
}