use crate::{
KernelMatrix,
PixelSink,
frame::{
Y2xxFrame, Yuv422p10LeFrame, Yuv422p12LeFrame, Yuv422p16LeFrame, Yuv444p10LeFrame,
Yuv444p12LeFrame, Yuv444p16LeFrame,
},
resample::{AreaResampler, ResampleError},
sinker::{MixedSinker, MixedSinkerError},
source::{
Yuv422p10, Yuv422p12, Yuv422p16, Yuv444p10, Yuv444p12, Yuv444p16, yuv422p10_to, yuv422p12_to,
yuv422p16_to, yuv444p10_to, yuv444p12_to, yuv444p16_to,
},
};
const SRC: usize = 8;
const CW: usize = SRC / 2;
const OUT: usize = 4;
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;
const TOL_U8: u8 = 5;
fn bin_to_out(plane: &[u16], in_w: usize, in_h: usize) -> Vec<u16> {
let (rx, ry) = (in_w / OUT, in_h / OUT);
let denom = (rx * ry) as u32;
let mut out = vec![0u16; OUT * OUT];
for oy in 0..OUT {
for ox in 0..OUT {
let mut s = 0u32;
for dy in 0..ry {
for dx in 0..rx {
s += plane[(oy * ry + dy) * in_w + ox * rx + dx] as u32;
}
}
out[oy * OUT + ox] = ((s + denom / 2) / denom) as u16;
}
}
out
}
fn as_le(host: &[u16]) -> Vec<u16> {
host
.iter()
.map(|v| u16::from_ne_bytes(v.to_le_bytes()))
.collect()
}
fn as_be(host: &[u16]) -> Vec<u16> {
host
.iter()
.map(|v| u16::from_ne_bytes(v.to_be_bytes()))
.collect()
}
macro_rules! y2xx_native_suite {
(
$mod:ident, $marker:ident, $row:ident, $walker:ident, $walker_be:ident,
// The bin-then-convert oracle bins chroma to FULL output resolution, so it
// feeds a 4:4:4 sink (`Yuv444pN`). The twin-parity check feeds the de-packed
$oracle_marker:ident, $oracle_frame:ident, $oracle_walker:ident,
$twin_marker:ident, $twin_frame:ident, $twin_walker:ident, $bits:literal,
) => {
mod $mod {
use super::*;
use crate::source::{$marker, $row, $walker, $walker_be};
const SHIFT: u32 = 16 - $bits;
const MASK: u16 = ((1u32 << $bits) - 1) as u16;
const MID: u16 = (1u16 << ($bits - 1));
fn ramp_packed() -> Vec<u16> {
let mut buf = vec![0u16; SRC * 2 * SRC];
for row in 0..SRC {
for cx in 0..CW {
let i0 = row * SRC + cx * 2;
let i1 = i0 + 1;
let ci = row * CW + cx;
let y0 = (MID as u32 + ((i0 as u32 * 37) % (MASK as u32 / 4))) as u16 & MASK;
let y1 = (MID as u32 + ((i1 as u32 * 37) % (MASK as u32 / 4))) as u16 & MASK;
let u =
(MID as u32 + ((ci as u32 * 53) % (MASK as u32 / 8)) - (MASK as u32 / 16)) as u16
& MASK;
let v =
(MID as u32 + ((ci as u32 * 41) % (MASK as u32 / 8)) - (MASK as u32 / 16)) as u16
& MASK;
let base = row * 2 * SRC + cx * 4;
buf[base] = y0 << SHIFT;
buf[base + 1] = u << SHIFT;
buf[base + 2] = y1 << SHIFT;
buf[base + 3] = v << SHIFT;
}
}
buf
}
fn uniform_gray_packed(y: u16) -> Vec<u16> {
let mut buf = vec![0u16; SRC * 2 * SRC];
for q in 0..(SRC * SRC / 2) {
let base = q * 4;
buf[base] = (y & MASK) << SHIFT;
buf[base + 1] = (MID & MASK) << SHIFT;
buf[base + 2] = (y & MASK) << SHIFT;
buf[base + 3] = (MID & MASK) << SHIFT;
}
buf
}
fn out_of_gamut_packed() -> Vec<u16> {
let mut buf = vec![0u16; SRC * 2 * SRC];
for row in 0..SRC {
for cx in 0..CW {
let i0 = row * SRC + cx * 2;
let i1 = i0 + 1;
let ci = row * CW + cx;
let y0 = ((i0 as u32 * MASK as u32) / (SRC * SRC) as u32) as u16 & MASK;
let y1 = ((i1 as u32 * MASK as u32) / (SRC * SRC) as u32) as u16 & MASK;
let hi = ci % 2 == 0;
let base = row * 2 * SRC + cx * 4;
buf[base] = y0 << SHIFT;
buf[base + 1] = if hi { MASK } else { 0 } << SHIFT;
buf[base + 2] = y1 << SHIFT;
buf[base + 3] = if hi { 0 } else { MASK } << SHIFT;
}
}
buf
}
fn full_scale_luma_packed() -> Vec<u16> {
let mut buf = ramp_packed();
for q in 0..(SRC * SRC / 2) {
buf[q * 4] = MASK << SHIFT;
buf[q * 4 + 2] = MASK << SHIFT;
}
buf
}
fn frame(buf: &[u16]) -> Y2xxFrame<'_, $bits, false> {
Y2xxFrame::try_new(buf, SRC as u32, SRC as u32, (2 * SRC) as u32).unwrap()
}
fn frame_be(buf: &[u16]) -> Y2xxFrame<'_, $bits, true> {
Y2xxFrame::try_new(buf, SRC as u32, SRC as u32, (2 * SRC) as u32).unwrap()
}
fn logical_y(packed: &[u16]) -> Vec<u16> {
let mut y = vec![0u16; SRC * SRC];
for (i, group) in packed.chunks_exact(4).enumerate() {
y[i * 2] = group[0] >> SHIFT;
y[i * 2 + 1] = group[2] >> SHIFT;
}
y
}
fn depack_uv(packed: &[u16]) -> (Vec<u16>, Vec<u16>) {
let mut u = vec![0u16; CW * SRC];
let mut v = vec![0u16; CW * SRC];
for (i, group) in packed.chunks_exact(4).enumerate() {
u[i] = group[1] >> SHIFT;
v[i] = group[3] >> SHIFT;
}
(u, v)
}
fn run(packed: &[u16], native: bool) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let p = as_le(packed);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(native)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
$walker(&frame(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn native_be_run(packed: &[u16]) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let p = as_be(packed);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<$marker<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
$walker_be::<_, true>(&frame_be(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn rowstage_be_run(packed: &[u16]) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let p = as_be(packed);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<$marker<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(false)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
$walker_be::<_, true>(&frame_be(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn oracle(packed: &[u16]) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let yl = logical_y(packed);
let (u, v) = depack_uv(packed);
let yb = bin_to_out(&yl, SRC, SRC);
let ub = bin_to_out(&u, CW, SRC);
let vb = bin_to_out(&v, CW, SRC);
let (ye, ue, ve) = (as_le(&yb), as_le(&ub), as_le(&vb));
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$oracle_marker>::new(OUT, OUT)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap();
let f = $oracle_frame::try_new(
&ye, &ue, &ve, OUT as u32, OUT as u32, OUT as u32, OUT as u32, OUT as u32,
)
.unwrap();
$oracle_walker(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
let luma: Vec<u8> = yb
.iter()
.map(|&by| (by.min(MASK) >> ($bits - 8)) as u8)
.collect();
(rgb, rgb_u16, luma)
}
fn planar_twin_native(packed: &[u16]) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let yl = logical_y(packed);
let (u, v) = depack_uv(packed);
let (ye, ue, ve) = (as_le(&yl), as_le(&u), as_le(&v));
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<$twin_marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
let f = $twin_frame::try_new(
&ye, &ue, &ve, SRC as u32, SRC as u32, SRC as u32, CW as u32, CW as u32,
)
.unwrap();
$twin_walker(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn max_delta_u8(a: &[u8], b: &[u8]) -> u8 {
a.iter().zip(b).map(|(&x, &y)| x.abs_diff(y)).max().unwrap_or(0)
}
fn max_delta_u16(a: &[u16], b: &[u16]) -> u16 {
a.iter().zip(b).map(|(&x, &y)| x.abs_diff(y)).max().unwrap_or(0)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_equals_bin_then_convert_oracle() {
let packed = ramp_packed();
let (n_rgb, n_rgb16, n_luma) = run(&packed, true);
let (o_rgb, o_rgb16, o_luma) = oracle(&packed);
assert_eq!(n_rgb, o_rgb, "u8 rgb must equal the bin-then-convert oracle");
assert_eq!(
n_rgb16, o_rgb16,
"u16 rgb must equal the bin-then-convert oracle (clamp-for-clamp)"
);
assert_eq!(n_luma, o_luma, "luma must equal the binned-then-narrowed Y");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_equals_planar_twin() {
let packed = ramp_packed();
let (n_rgb, n_rgb16, n_luma) = run(&packed, true);
let (t_rgb, t_rgb16, t_luma) = planar_twin_native(&packed);
assert_eq!(n_rgb, t_rgb, "u8 rgb must match the planar twin");
assert_eq!(n_rgb16, t_rgb16, "u16 rgb must match the planar twin");
assert_eq!(n_luma, t_luma, "luma must match the planar twin");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_within_tolerance_of_rowstage() {
let packed = ramp_packed();
let (n_rgb, n_rgb16, n_luma) = run(&packed, true);
let (r_rgb, r_rgb16, r_luma) = run(&packed, false);
assert_eq!(n_luma, r_luma, "luma must be bit-identical across tiers");
let d_u8 = max_delta_u8(&n_rgb, &r_rgb);
assert!(
d_u8 <= TOL_U8,
"u8 native-vs-rowstage max delta {d_u8} exceeds tolerance {TOL_U8}"
);
let tol_u16: u16 = (TOL_U8 as u16) << ($bits - 8);
let d_u16 = max_delta_u16(&n_rgb16, &r_rgb16);
assert!(
d_u16 <= tol_u16,
"u16 native-vs-rowstage max delta {d_u16} exceeds tolerance {tol_u16}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_be_matches_native_le() {
let packed = ramp_packed();
let le = run(&packed, true);
let be = native_be_run(&packed);
assert_eq!(be.0, le.0, "BE u8 colour must match LE");
assert_eq!(be.1, le.1, "BE u16 colour must match LE");
assert_eq!(be.2, le.2, "BE luma must match LE");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_be_within_tolerance_of_rowstage_be() {
let packed = ramp_packed();
let (n_rgb, n_rgb16, n_luma) = native_be_run(&packed);
let (r_rgb, r_rgb16, r_luma) = rowstage_be_run(&packed);
assert_eq!(n_luma, r_luma, "BE luma must be bit-identical across tiers");
let d_u8 = max_delta_u8(&n_rgb, &r_rgb);
assert!(
d_u8 <= TOL_U8,
"BE u8 native-vs-rowstage max delta {d_u8} exceeds tolerance {TOL_U8}"
);
let tol_u16: u16 = (TOL_U8 as u16) << ($bits - 8);
let d_u16 = max_delta_u16(&n_rgb16, &r_rgb16);
assert!(
d_u16 <= tol_u16,
"BE u16 native-vs-rowstage max delta {d_u16} exceeds tolerance {tol_u16}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_luma_matches_inter_area_oracle() {
let packed = ramp_packed();
let (_, _, n_luma) = run(&packed, true);
let y_ref = bin_to_out(&logical_y(&packed), SRC, SRC);
let luma_ref: Vec<u8> = y_ref.iter().map(|&c| (c >> ($bits - 8)) as u8).collect();
assert_eq!(
n_luma, luma_ref,
"native luma must equal the INTER_AREA bin of the de-packed Y"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_luma_clamps_full_scale_y() {
let packed = full_scale_luma_packed();
let (_, _, n_luma) = run(&packed, true);
let yb = bin_to_out(&logical_y(&packed), SRC, SRC);
let expect: Vec<u8> = yb
.iter()
.map(|&by| (by.min(MASK) >> ($bits - 8)) as u8)
.collect();
assert_eq!(
n_luma, expect,
"full-scale binned Y must clamp to native-max before narrowing, not wrap"
);
let sat = (MASK >> ($bits - 8)) as u8;
assert!(
n_luma.iter().all(|&l| l == sat),
"all full-scale luma must saturate to {sat}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rowstage_luma_clamps_full_scale_y() {
let packed = full_scale_luma_packed();
let (_, _, r_luma) = run(&packed, false);
let yb = bin_to_out(&logical_y(&packed), SRC, SRC);
let expect: Vec<u8> = yb
.iter()
.map(|&by| (by.min(MASK) >> ($bits - 8)) as u8)
.collect();
assert_eq!(
r_luma, expect,
"row-stage full-scale luma must clamp to native-max before narrowing, not wrap"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn out_of_gamut_native_vs_rowstage_pinned() {
let packed = out_of_gamut_packed();
let (n_rgb, _, n_luma) = run(&packed, true);
let (r_rgb, _, r_luma) = run(&packed, false);
assert_eq!(n_luma, r_luma, "luma stays bit-identical out of gamut");
let d = max_delta_u8(&n_rgb, &r_rgb);
assert!(
d > TOL_U8,
"crafted out-of-gamut case must diverge beyond the in-gamut tolerance {TOL_U8}, got {d}"
);
assert!(d < u8::MAX, "out-of-gamut delta stays bounded, got {d}");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uniform_gray_leaves_color_unchanged() {
let packed = uniform_gray_packed((MID as u32 + (MASK as u32 / 8)) as u16 & MASK);
let (n_rgb, n_rgb16, _) = run(&packed, true);
let p = as_le(&packed);
let mut ref_rgb = vec![0u8; SRC * SRC * 3];
let mut ref_rgb16 = vec![0u16; SRC * SRC * 3];
{
let mut sink = MixedSinker::<$marker>::new(SRC, SRC)
.with_rgb(&mut ref_rgb)
.unwrap()
.with_rgb_u16(&mut ref_rgb16)
.unwrap();
$walker(&frame(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
for px in n_rgb.chunks_exact(3) {
assert_eq!(px, &ref_rgb[..3], "uniform-gray u8 colour drifted");
}
for px in n_rgb16.chunks_exact(3) {
assert_eq!(px, &ref_rgb16[..3], "uniform-gray u16 colour drifted");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn u8_and_u16_color_are_independent_bins() {
let packed = ramp_packed();
let (n_rgb, n_rgb16, _) = run(&packed, true);
let narrowed: Vec<u8> = n_rgb16.iter().map(|&c| (c >> ($bits - 8)) as u8).collect();
assert_ne!(
n_rgb, narrowed,
"u8 colour must be an independent bin, not a narrowed u16 bin"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_luma_u16_equals_clamped_binned_y() {
let packed = ramp_packed();
let luma_u16_oracle: Vec<u16> = bin_to_out(&logical_y(&packed), SRC, SRC)
.iter()
.map(|&by| by.min(MASK))
.collect();
{
let p = as_le(&packed);
let mut luma_u16 = vec![0u16; OUT * OUT];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma_u16(&mut luma_u16)
.unwrap();
$walker(&frame(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(
luma_u16, luma_u16_oracle,
"native luma_u16 (LE) must equal the clamped-binned-Y oracle"
);
}
{
let p = as_be(&packed);
let mut luma_u16 = vec![0u16; OUT * OUT];
{
let mut sink = MixedSinker::<$marker<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma_u16(&mut luma_u16)
.unwrap();
$walker_be::<_, true>(&frame_be(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(
luma_u16, luma_u16_oracle,
"native luma_u16 (BE) must equal the clamped-binned-Y oracle"
);
}
{
let p = as_le(&packed);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut luma_u16 = vec![0u16; OUT * OUT];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
$walker(&frame(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
let (o_rgb, _, _) = oracle(&packed);
assert_eq!(
rgb, o_rgb,
"rgb + luma_u16 must take the NATIVE route — rgb equals the native \
oracle, not row-stage"
);
assert_eq!(
luma_u16, luma_u16_oracle,
"rgb + luma_u16: luma_u16 must equal the clamped-binned-Y oracle"
);
}
}
#[test]
fn no_outputs_is_a_no_op() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true);
$walker(&frame(&p), FR, sink.set_kernel_matrix(M)).unwrap();
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn resets_join_across_frames() {
let packed1 = ramp_packed();
let invert = |p: &[u16]| -> Vec<u16> {
p.iter()
.map(|&x| (MASK.wrapping_sub(x >> SHIFT) & MASK) << SHIFT)
.collect()
};
let packed2 = invert(&packed1);
let p1 = as_le(&packed1);
let p2 = as_le(&packed2);
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
$walker(&frame(&p1), FR, sink.set_kernel_matrix(M)).unwrap();
$walker(&frame(&p2), FR, sink.set_kernel_matrix(M)).unwrap();
}
let y_ref = bin_to_out(&logical_y(&packed2), SRC, SRC);
let luma_ref: Vec<u8> = y_ref.iter().map(|&c| (c >> ($bits - 8)) as u8).collect();
assert_eq!(luma, luma_ref, "join did not reset between frames");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn out_of_sequence_first_row_rejected_and_does_not_poison_retry() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut luma = vec![0u8; OUT * OUT];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
let row3 = &p[3 * 2 * SRC..4 * 2 * SRC];
let err = sink.process($row::for_tests(row3, 3, M, FR)).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"expected OutOfSequenceRow, got {err:?}"
);
let mut rgb = vec![0u8; OUT * OUT * 3];
sink.set_rgb(&mut rgb).unwrap();
sink
.process($row::for_tests(&p[..2 * SRC], 0, M, FR))
.expect("row 0 must succeed after a rejected out-of-sequence first row");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn frozen_mid_frame_change_rejected_before_scratch_alloc() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut luma = vec![0u8; OUT * OUT];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
for r in 0..2 {
sink
.process($row::for_tests(&p[r * 2 * SRC..(r + 1) * 2 * SRC], r, M, FR))
.expect("luma-only rows freeze a luma-only output set");
}
sink.set_rgb_u16(&mut rgb_u16).unwrap();
crate::sinker::mixed::arm_y2xx_alloc_failure();
let err = sink
.process($row::for_tests(&p[2 * 2 * SRC..3 * 2 * SRC], 2, M, FR))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"mid-frame output change must reject as ResampleOutputsChanged before \
the scratch alloc, got {err:?}"
);
assert!(
rgb_u16.iter().all(|&b| b == 0),
"rejected mid-frame-change row touched the new colour output"
);
let mut rgb_u16b = vec![0u16; OUT * OUT * 3];
let mut sink2 = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16b)
.unwrap();
let err2 = sink2.process($row::for_tests(&p[..2 * SRC], 0, M, FR)).unwrap_err();
assert!(
matches!(
err2,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"armed failpoint must still be live and fire on the first in-sequence \
colour reserve, got {err2:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn oos_after_recoverable_alloc_failure_rejected_before_scratch_alloc() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16)
.unwrap();
crate::sinker::mixed::arm_y2xx_alloc_failure();
let err0 = sink.process($row::for_tests(&p[..2 * SRC], 0, M, FR)).unwrap_err();
assert!(
matches!(
err0,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"the recoverable scratch failure on row 0 must surface AllocationFailed, got {err0:?}"
);
crate::sinker::mixed::arm_y2xx_alloc_failure();
let err2 = sink
.process($row::for_tests(&p[2 * 2 * SRC..3 * 2 * SRC], 2, M, FR))
.unwrap_err();
assert!(
matches!(
err2,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"an out-of-sequence row after a recoverable scratch failure must reject \
as OutOfSequenceRow, never AllocationFailed, got {err2:?}"
);
assert!(
rgb_u16.iter().all(|&b| b == 0),
"neither the recoverable-failure nor the out-of-sequence row touched the colour output"
);
let mut rgb_u16b = vec![0u16; OUT * OUT * 3];
let mut sink2 = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16b)
.unwrap();
let err3 = sink2.process($row::for_tests(&p[..2 * SRC], 0, M, FR)).unwrap_err();
assert!(
matches!(
err3,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"the failpoint re-armed in step 2 must still be live and fire on the \
first in-sequence colour reserve, got {err3:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_to_rowstage_route_flip_mid_frame_rejected() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut luma = vec![0u8; OUT * OUT];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&p[..2 * SRC], 0, M, FR))
.expect("native row 0 freezes the route and succeeds");
sink.set_native(false);
let err = sink
.process($row::for_tests(&p[2 * SRC..2 * 2 * SRC], 1, M, FR))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::NativeRouteChanged(_)),
"a native -> row-stage mid-frame route flip must reject as NativeRouteChanged, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rowstage_to_native_route_flip_mid_frame_rejected() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut luma = vec![0u8; OUT * OUT];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(false)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&p[..2 * SRC], 0, M, FR))
.expect("row-stage row 0 freezes the route and succeeds");
sink.set_native(true);
let err = sink
.process($row::for_tests(&p[2 * SRC..2 * 2 * SRC], 1, M, FR))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::NativeRouteChanged(_)),
"a row-stage -> native mid-frame route flip must reject as NativeRouteChanged, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_u16_attach_mid_frame_rejected_as_outputs_changed() {
let packed = ramp_packed();
let p = as_le(&packed);
let mut luma = vec![0u8; OUT * OUT];
let mut luma_u16 = vec![0u16; OUT * OUT];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&p[..2 * SRC], 0, M, FR))
.expect("native luma row 0 freezes the output set and the route");
sink.set_luma_u16(&mut luma_u16).unwrap();
let err = sink
.process($row::for_tests(&p[2 * SRC..2 * 2 * SRC], 1, M, FR))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"a mid-frame luma_u16 attach must reject as ResampleOutputsChanged (the \
frozen-output check), not NativeRouteChanged, got {err:?}"
);
assert!(
luma_u16.iter().all(|&b| b == 0),
"the rejected mid-frame-change row must not touch the new luma_u16 output"
);
}
}
};
}
y2xx_native_suite!(
y210,
Y210,
Y210Row,
y210_to,
y210_to_endian,
Yuv444p10,
Yuv444p10LeFrame,
yuv444p10_to,
Yuv422p10,
Yuv422p10LeFrame,
yuv422p10_to,
10,
);
y2xx_native_suite!(
y212,
Y212,
Y212Row,
y212_to,
y212_to_endian,
Yuv444p12,
Yuv444p12LeFrame,
yuv444p12_to,
Yuv422p12,
Yuv422p12LeFrame,
yuv422p12_to,
12,
);
y2xx_native_suite!(
y216,
Y216,
Y216Row,
y216_to,
y216_to_endian,
Yuv444p16,
Yuv444p16LeFrame,
yuv444p16_to,
Yuv422p16,
Yuv422p16LeFrame,
yuv422p16_to,
16,
);
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_only_native_skips_chroma_planning() {
use crate::source::{Y210, y210_to};
let mut packed = vec![0u16; SRC * 2 * SRC];
for q in 0..(SRC * SRC / 2) {
let base = q * 4;
packed[base] = (1u16 << 9) << (16 - 10);
packed[base + 1] = (1u16 << 9) << (16 - 10);
packed[base + 2] = (1u16 << 9) << (16 - 10);
packed[base + 3] = (1u16 << 9) << (16 - 10);
}
let p = as_le(&packed);
let frame =
Y2xxFrame::<10, false>::try_new(&p, SRC as u32, SRC as u32, (2 * SRC) as u32).unwrap();
crate::sinker::mixed::arm_planar_hb_native_chroma_failure();
let mut luma = vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Y210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
y210_to(&frame, FR, sink.set_kernel_matrix(M)).expect("luma-only native must not plan chroma");
}
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut sink =
MixedSinker::<Y210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap();
assert!(
y210_to(&frame, FR, sink.set_kernel_matrix(M)).is_err(),
"colour native must reach chroma planning (the armed failpoint fires)"
);
}
#[test]
#[cfg(feature = "rgb")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_delegate_grow_failure_through_packed_wrapper_is_transactional() {
use crate::source::{Y210, Y210Row};
let mut packed = vec![0u16; SRC * 2 * SRC];
for q in 0..(SRC * SRC / 2) {
let base = q * 4;
for k in 0..4 {
packed[base + k] = (1u16 << 9) << (16 - 10);
}
}
let p = as_le(&packed);
let row = |r: usize| Y210Row::for_tests(&p[r * 2 * SRC..(r + 1) * 2 * SRC], r, M, FR);
let mut luma = vec![0u8; OUT * OUT];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut sink =
MixedSinker::<Y210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
for r in 0..SRC {
sink
.process(row(r))
.expect("luma-only frame builds the native join");
}
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink.set_rgb_u16(&mut rgb_u16).unwrap();
crate::sinker::mixed::arm_planar_hb_native_rgb_u16_scratch_failure();
let err = sink.process(row(0)).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"a delegate u16-scratch OOM (after the packed wrapper's de-pack) must surface \
AllocationFailed, got {err:?}"
);
sink.set_rgb(&mut rgb).unwrap();
sink
.process(row(0))
.expect("row 0 with a changed output set must succeed after a delegate u16-scratch OOM");
for r in 1..SRC {
sink
.process(row(r))
.expect("the recovered join must process the rest of the frame");
}
assert!(
rgb.iter().any(|&b| b != 0) && rgb_u16.iter().any(|&b| b != 0),
"the recovered join must produce real u8 and u16 colour output"
);
}