#[allow(unused_imports)]
pub(super) use super::*;
pub(super) use crate::ChromaLocation;
use crate::ColorMatrix;
pub(super) const W: u32 = 16;
pub(super) const H: u32 = 8;
pub(super) fn ramp_planes() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut y = std::vec![0u8; w * h];
for r in 0..h {
for x in 0..w {
y[r * w + x] = (16 + x * 8 + r * 5).min(235) as u8;
}
}
let mut u = std::vec![0u8; cw * h];
let mut v = std::vec![0u8; cw * h];
for r in 0..h {
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)
}
pub(super) fn pack_yuyv(y: &[u8], u: &[u8], v: &[u8]) -> Vec<u8> {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut out = std::vec![0u8; 2 * w * h];
for r in 0..h {
for c in 0..cw {
let base = r * 2 * w + c * 4;
out[base] = y[r * w + 2 * c];
out[base + 1] = u[r * cw + c];
out[base + 2] = y[r * w + 2 * c + 1];
out[base + 3] = v[r * cw + c];
}
}
out
}
pub(super) fn pack_uyvy(y: &[u8], u: &[u8], v: &[u8]) -> Vec<u8> {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut out = std::vec![0u8; 2 * w * h];
for r in 0..h {
for c in 0..cw {
let base = r * 2 * w + c * 4;
out[base] = u[r * cw + c];
out[base + 1] = y[r * w + 2 * c];
out[base + 2] = v[r * cw + c];
out[base + 3] = y[r * w + 2 * c + 1];
}
}
out
}
pub(super) fn pack_yvyu(y: &[u8], u: &[u8], v: &[u8]) -> Vec<u8> {
let w = W as usize;
let h = H as usize;
let cw = w / 2;
let mut out = std::vec![0u8; 2 * w * h];
for r in 0..h {
for c in 0..cw {
let base = r * 2 * w + c * 4;
out[base] = y[r * w + 2 * c];
out[base + 1] = v[r * cw + c];
out[base + 2] = y[r * w + 2 * c + 1];
out[base + 3] = u[r * cw + c];
}
}
out
}
pub(super) 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
}
pub(super) fn ref_full_chroma(u422: &[u8], v422: &[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 urow = ref_upsample_center_h(&u422[r * cw..r * cw + cw], w);
let vrow = ref_upsample_center_h(&v422[r * cw..r * 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)
}
macro_rules! packed_siting_suite {
($modname:ident, $Marker:ident, $Row:ident, $Frame:ident, $walker:ident, $pack:ident) => {
mod $modname {
use super::*;
fn convert_rgb(loc: ChromaLocation, simd: bool) -> Vec<u8> {
let (yp, up, vp) = ramp_planes();
let packed = $pack(&yp, &up, &vp);
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(simd);
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
rgb
}
#[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,
ChromaLocation::TopLeft,
ChromaLocation::BottomLeft,
] {
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_planes();
let packed = $pack(&yp, &up, &vp);
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Left);
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let chroma_len = sink.chroma_full.len();
let half_len = sink.packed_yuv_u_half.len();
let y_len = sink.packed_yuv_y_full.len();
drop(sink);
assert_eq!(
chroma_len, 0,
"co-sited path must not grow the chroma scratch"
);
assert_eq!(
half_len, 0,
"co-sited path must not grow the de-interleave scratch"
);
assert_eq!(
y_len, 0,
"co-sited path must not grow the Y de-interleave 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_planes();
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 packed 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_planes();
let packed = $pack(&yp, &up, &vp);
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let chroma_len = sink.chroma_full.len();
let half_len = sink.packed_yuv_u_half.len();
let y_len = sink.packed_yuv_y_full.len();
drop(sink);
assert_eq!(
chroma_len,
2 * W as usize,
"centered path stages U+V at full width"
);
assert_eq!(
half_len,
(W / 2) as usize,
"centered path stages the de-interleaved half-width chroma"
);
assert_eq!(
y_len,
W as usize,
"centered path stages the de-interleaved full-width Y"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_and_bottom_route_like_center_horizontally() {
let center = convert_rgb(ChromaLocation::Center, true);
assert_eq!(convert_rgb(ChromaLocation::Top, true), center);
assert_eq!(convert_rgb(ChromaLocation::Bottom, true), center);
}
#[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 centered_matches_yuv422p_centered() {
let (yp, up, vp) = ramp_planes();
let src422 = Yuv422pFrame::new(&yp, &up, &vp, W, H, W, W / 2, W / 2);
let mut rgb422 = std::vec![0u8; (W * H * 3) as usize];
let mut sink422 = MixedSinker::<Yuv422p>::new(W as usize, H as usize)
.with_rgb(&mut rgb422)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
yuv422p_to(&src422, false, sink422.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
assert_eq!(
convert_rgb(ChromaLocation::Center, true),
rgb422,
"centered packed must equal centered Yuv422p of the same planes"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn centered_chroma_derived_ncl_uses_matrix_tag_fallback() {
use crate::{ColorInfo, ColorSpec, DynamicRange, PixelFormat, Primaries, Transfer};
let (yp, up, vp) = ramp_planes();
let packed = $pack(&yp, &up, &vp);
let spec = |loc: ChromaLocation| {
ColorSpec::from_info(
PixelFormat::Yuv422p,
ColorInfo::new(
Primaries::Bt2020,
Transfer::Bt709,
ColorMatrix::ChromaDerivedNcl,
DynamicRange::Limited,
loc,
),
)
};
let decode_cdn = |loc: ChromaLocation| -> Vec<u8> {
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec(loc)).unwrap();
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::ChromaDerivedNcl)).unwrap();
rgb
};
let decode_bt709 = |loc: ChromaLocation| -> Vec<u8> {
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc.clone());
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
rgb
};
assert_eq!(
decode_cdn(ChromaLocation::Center),
decode_bt709(ChromaLocation::Center),
"centered ChromaDerivedNcl must resolve via the BT.709 matrix-tag fallback"
);
assert_eq!(
decode_cdn(ChromaLocation::Left),
decode_bt709(ChromaLocation::Left),
"default ChromaDerivedNcl must resolve via the same matrix-tag fallback"
);
assert_ne!(
decode_cdn(ChromaLocation::Center),
decode_cdn(ChromaLocation::Left),
"centered ChromaDerivedNcl must still shift chroma vs the co-sited default"
);
}
#[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_planes();
let (u444, v444) = ref_full_chroma(&up, &vp);
let packed = $pack(&yp, &up, &vp);
{
let src = $Frame::new(&packed, W, H, W * 2);
let mut rgba = std::vec![0u8; (W * H * 4) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgba(&mut rgba)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
$walker(&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 = $Frame::new(&packed, W, H, 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::<$Marker>::new(W as usize, H as usize)
.with_hsv(&mut h, &mut s, &mut v)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
$walker(&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_planes();
let packed = $pack(&yp, &up, &vp);
let src = $Frame::new(&packed, W, H, W * 2);
let info = ColorInfo::new(
Primaries::Bt709,
Transfer::Bt709,
ColorMatrix::Bt601,
DynamicRange::Limited,
ChromaLocation::Center,
);
let spec = ColorSpec::from_info(PixelFormat::Yuv422p, info);
let opts = YuvOptions::from_color_spec(&spec);
let mut rgb = std::vec![0u8; (W * H * 3) as usize];
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec).unwrap();
$walker(&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 centered_alloc_failure_leaves_outputs_untouched() {
use crate::resample::ResampleError;
let (yp, up, vp) = ramp_planes();
let packed = $pack(&yp, &up, &vp);
{
let src = $Frame::new(&packed, W, H, W * 2);
let mut luma_ok = std::vec![0xABu8; (W * H) as usize];
let mut rgb_ok = std::vec![0xCDu8; (W * H * 3) as usize];
let mut sink_ok = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_luma(&mut luma_ok)
.unwrap()
.with_rgb(&mut rgb_ok)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
$walker(&src, false, sink_ok.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
drop(sink_ok);
assert!(
luma_ok.iter().any(|&b| b != 0xAB),
"control: the centered path writes luma when the scratch alloc is not armed"
);
}
let src = $Frame::new(&packed, W, H, 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::<$Marker>::new(W as usize, H as usize)
.with_luma(&mut luma)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(ChromaLocation::Center);
crate::sinker::mixed::arm_chroma_full_alloc_failure();
let err = $walker(&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"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn no_output_row_does_not_allocate_chroma_scratch() {
let (yp, up, vp) = ramp_planes();
let packed = $pack(&yp, &up, &vp);
let src = $Frame::new(&packed, W, H, W * 2);
let mut sink = MixedSinker::<$Marker>::new(W as usize, H as usize)
.with_chroma_location(ChromaLocation::Center);
$walker(&src, false, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
let chroma_len = sink.chroma_full.len();
let half_len = sink.packed_yuv_u_half.len();
drop(sink);
assert_eq!(
chroma_len, 0,
"a no-output centered row must not reserve the chroma scratch"
);
assert_eq!(
half_len, 0,
"a no-output centered row must not reserve the de-interleave scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "constructs an absurd geometry; the no-op contract is the point, not Miri"
)]
fn 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 packed = std::vec![128u8; 2 * w]; let mut sink =
MixedSinker::<$Marker>::new(w, h).with_chroma_location(ChromaLocation::Center);
let row = crate::source::$Row::for_tests(&packed, idx, KernelMatrix::Bt601, false);
crate::PixelSink::process(&mut sink, row).unwrap();
let chroma_len = sink.chroma_full.len();
drop(sink);
assert_eq!(
chroma_len, 0,
"a no-output large-geometry row must allocate nothing"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn direct_path_mid_frame_siting_flip_is_rejected() {
let (yp, up, vp) = ramp_planes();
let packed = $pack(&yp, &up, &vp);
let w = W as usize;
let h = H as usize;
for (loc1, loc2) in [
(ChromaLocation::Left, ChromaLocation::Center),
(ChromaLocation::Center, ChromaLocation::Left),
] {
let want = convert_rgb(loc1.clone(), true);
let mut rgb = std::vec![0u8; w * h * 3];
let mut sink = MixedSinker::<$Marker>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_chroma_location(loc1.clone())
.with_simd(true);
crate::PixelSink::begin_frame(&mut sink, W, H).unwrap();
let row0 = crate::source::$Row::for_tests(&packed[0..2 * w], 0, KernelMatrix::Bt601, false);
crate::PixelSink::process(&mut sink, row0).unwrap();
let scratch_len = sink.chroma_full.len();
sink.set_chroma_location(loc2.clone());
let row1 = crate::source::$Row::for_tests(&packed[2 * w..4 * w], 1, KernelMatrix::Bt601, false);
let err = crate::PixelSink::process(&mut sink, row1).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"direct path {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
assert_eq!(
sink.chroma_full.len(),
scratch_len,
"{loc1:?}->{loc2:?}: a rejected flip must not grow the chroma scratch"
);
sink.set_chroma_location(loc1.clone());
for r in 1..h {
let row = crate::source::$Row::for_tests(
&packed[r * 2 * w..(r + 1) * 2 * w],
r,
KernelMatrix::Bt601,
false,
);
crate::PixelSink::process(&mut sink, row).unwrap();
}
drop(sink);
assert_eq!(
rgb, want,
"{loc1:?}: retry after a rejected flip must match a clean in-order decode"
);
}
}
}
};
}
packed_siting_suite!(
yuyv,
Yuyv422,
Yuyv422Row,
Yuyv422Frame,
yuyv422_to,
pack_yuyv
);
packed_siting_suite!(
uyvy,
Uyvy422,
Uyvy422Row,
Uyvy422Frame,
uyvy422_to,
pack_uyvy
);
packed_siting_suite!(
yvyu,
Yvyu422,
Yvyu422Row,
Yvyu422Frame,
yvyu422_to,
pack_yvyu
);