use super::*;
use crate::{
KernelMatrix, PixelSink,
resample::{AreaResampler, ResampleError},
sinker::{MixedSinker, MixedSinkerError},
source::{Yuv422p10, Yuv444p10, yuv422p10_to, yuv444p10_to},
};
const SRC: usize = 12;
const CW: usize = SRC / 2;
const OUT: usize = 6;
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;
const STRIDE: u32 = (SRC.div_ceil(6) * 16) as u32;
const MASK: u16 = 0x3FF;
const MID: u16 = 1 << 9;
const TOL_U8: u8 = 5;
fn pack_v210_word(samples: [u16; 12]) -> [u8; 16] {
let mut out = [0u8; 16];
let pack = |a: u16, b: u16, c: u16| -> u32 {
(a as u32 & 0x3FF) | ((b as u32 & 0x3FF) << 10) | ((c as u32 & 0x3FF) << 20)
};
out[0..4].copy_from_slice(&pack(samples[0], samples[1], samples[2]).to_le_bytes());
out[4..8].copy_from_slice(&pack(samples[3], samples[4], samples[5]).to_le_bytes());
out[8..12].copy_from_slice(&pack(samples[6], samples[7], samples[8]).to_le_bytes());
out[12..16].copy_from_slice(&pack(samples[9], samples[10], samples[11]).to_le_bytes());
out
}
fn pack_v210_dims(y: &[u16], u: &[u16], v: &[u16], w: usize, h: usize) -> Vec<u8> {
let cw = w / 2;
let words_per_row = w.div_ceil(6);
let mut out = std::vec![0u8; words_per_row * 16 * h];
for row in 0..h {
for word in 0..words_per_row {
let px = word * 6;
let gy = |k: usize| -> u16 { if px + k < w { y[row * w + px + k] } else { 0 } };
let gc = |c: &[u16], k: usize| -> u16 {
let cu = px / 2 + k;
if cu < cw { c[row * cw + cu] } else { 0 }
};
let samples: [u16; 12] = [
gc(u, 0),
gy(0),
gc(v, 0),
gy(1),
gc(u, 1),
gy(2),
gc(v, 1),
gy(3),
gc(u, 2),
gy(4),
gc(v, 2),
gy(5),
];
let off = (row * words_per_row + word) * 16;
out[off..off + 16].copy_from_slice(&pack_v210_word(samples));
}
}
out
}
fn pack_v210(y: &[u16], u: &[u16], v: &[u16]) -> Vec<u8> {
pack_v210_dims(y, u, v, SRC, SRC)
}
fn v210_as_be(plane_le: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(plane_le.len());
for chunk in plane_le.chunks_exact(4) {
let w = u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
out.extend_from_slice(&w.to_be_bytes());
}
out
}
fn ramp_planes() -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let mut y = std::vec![0u16; SRC * SRC];
let mut u = std::vec![0u16; CW * SRC];
let mut v = std::vec![0u16; CW * SRC];
for row in 0..SRC {
for x in 0..SRC {
let i = row * SRC + x;
y[i] = (MID as u32 + ((i as u32 * 37) % (MASK as u32 / 4))) as u16 & MASK;
}
for cx in 0..CW {
let ci = row * CW + cx;
u[ci] =
(MID as u32 + ((ci as u32 * 53) % (MASK as u32 / 8)) - (MASK as u32 / 16)) as u16 & MASK;
v[ci] =
(MID as u32 + ((ci as u32 * 41) % (MASK as u32 / 8)) - (MASK as u32 / 16)) as u16 & MASK;
}
}
(y, u, v)
}
fn ramp_packed() -> Vec<u8> {
let (y, u, v) = ramp_planes();
pack_v210(&y, &u, &v)
}
fn uniform_gray_packed(y: u16) -> Vec<u8> {
let yp = std::vec![y & MASK; SRC * SRC];
let chroma = std::vec![MID & MASK; CW * SRC];
pack_v210(&yp, &chroma, &chroma)
}
fn out_of_gamut_packed() -> Vec<u8> {
let mut y = std::vec![0u16; SRC * SRC];
let mut u = std::vec![0u16; CW * SRC];
let mut v = std::vec![0u16; CW * SRC];
for row in 0..SRC {
for x in 0..SRC {
let i = row * SRC + x;
y[i] = ((i as u32 * MASK as u32) / (SRC * SRC) as u32) as u16 & MASK;
}
for cx in 0..CW {
let ci = row * CW + cx;
let hi = ci.is_multiple_of(2);
u[ci] = if hi { MASK } else { 0 };
v[ci] = if hi { 0 } else { MASK };
}
}
pack_v210(&y, &u, &v)
}
fn full_scale_luma_packed() -> Vec<u8> {
let (_, u, v) = ramp_planes();
let yp = std::vec![MASK; SRC * SRC];
pack_v210(&yp, &u, &v)
}
fn frame(buf: &[u8]) -> V210Frame<'_> {
V210Frame::new(buf, SRC as u32, SRC as u32, STRIDE)
}
fn logical_y(packed: &[u8], w: usize, h: usize) -> Vec<u16> {
let words_per_row = w.div_ceil(6);
let mut y = std::vec![0u16; w * h];
for row in 0..h {
for word in 0..words_per_row {
let off = (row * words_per_row + word) * 16;
let lane =
|l: usize| u32::from_le_bytes(packed[off + l * 4..off + l * 4 + 4].try_into().unwrap());
let (w0, w1, w2, w3) = (lane(0), lane(1), lane(2), lane(3));
let ys = [
(w0 >> 10) & 0x3FF,
w1 & 0x3FF,
(w1 >> 20) & 0x3FF,
(w2 >> 10) & 0x3FF,
w3 & 0x3FF,
(w3 >> 20) & 0x3FF,
];
let px = word * 6;
for (k, &yv) in ys.iter().enumerate() {
if px + k < w {
y[row * w + px + k] = yv as u16;
}
}
}
}
y
}
fn logical_uv(packed: &[u8], w: usize, h: usize) -> (Vec<u16>, Vec<u16>) {
let cw = w / 2;
let words_per_row = w.div_ceil(6);
let mut u = std::vec![0u16; cw * h];
let mut v = std::vec![0u16; cw * h];
for row in 0..h {
for word in 0..words_per_row {
let off = (row * words_per_row + word) * 16;
let lane =
|l: usize| u32::from_le_bytes(packed[off + l * 4..off + l * 4 + 4].try_into().unwrap());
let (w0, w1, w2, w3) = (lane(0), lane(1), lane(2), lane(3));
let us = [w0 & 0x3FF, (w1 >> 10) & 0x3FF, (w2 >> 20) & 0x3FF];
let vs = [(w0 >> 20) & 0x3FF, w2 & 0x3FF, (w3 >> 10) & 0x3FF];
let cu = word * 3;
for k in 0..3 {
if cu + k < cw {
u[row * cw + cu + k] = us[k] as u16;
v[row * cw + cu + k] = vs[k] as u16;
}
}
}
}
(u, v)
}
fn bin_to(plane: &[u16], in_w: usize, in_h: usize, out_w: usize, out_h: usize) -> Vec<u16> {
let (rx, ry) = (in_w / out_w, in_h / out_h);
let denom = (rx * ry) as u32;
let mut out = std::vec![0u16; out_w * out_h];
for oy in 0..out_h {
for ox in 0..out_w {
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_w + ox] = ((s + denom / 2) / denom) as u16;
}
}
out
}
fn run(packed: &[u8], native: bool) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<V210, 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();
v210_to(&frame(packed), FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn be_run(packed_le: &[u8], native: bool) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let p = v210_as_be(packed_le);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut luma = std::vec![0u8; OUT * OUT];
{
let be_frame = V210BeFrame::try_new(&p, SRC as u32, SRC as u32, STRIDE).unwrap();
let mut sink = MixedSinker::<V210<true>, 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();
v210_to_endian::<_, true>(&be_frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb_u16, luma)
}
fn oracle(packed: &[u8]) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let yl = logical_y(packed, SRC, SRC);
let (u, v) = logical_uv(packed, SRC, SRC);
let yb = bin_to(&yl, SRC, SRC, OUT, OUT);
let ub = bin_to(&u, CW, SRC, OUT, OUT);
let vb = bin_to(&v, CW, SRC, OUT, OUT);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<Yuv444p10>::new(OUT, OUT)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap();
let f = Yuv444p10Frame::new(
&yb, &ub, &vb, OUT as u32, OUT as u32, OUT as u32, OUT as u32, OUT as u32,
);
yuv444p10_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
let luma: Vec<u8> = yb.iter().map(|&by| (by.min(MASK) >> 2) as u8).collect();
(rgb, rgb_u16, luma)
}
fn planar_twin_native(packed: &[u8], w: usize, h: usize) -> (Vec<u8>, Vec<u16>, Vec<u8>) {
let yl = logical_y(packed, w, h);
let (u, v) = logical_uv(packed, w, h);
let cw = w / 2;
let ow = w / 2;
let oh = h / 2;
let mut rgb = std::vec![0u8; ow * oh * 3];
let mut rgb_u16 = std::vec![0u16; ow * oh * 3];
let mut luma = std::vec![0u8; ow * oh];
{
let mut sink =
MixedSinker::<Yuv422p10, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap();
let f = Yuv422p10Frame::new(
&yl, &u, &v, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
yuv422p10_to(&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, SRC, SRC);
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_equals_planar_twin_partial_word() {
const W: usize = 8;
const H: usize = 8;
let cw = W / 2;
let mut y = std::vec![0u16; W * H];
let mut u = std::vec![0u16; cw * H];
let mut v = std::vec![0u16; cw * H];
for row in 0..H {
for x in 0..W {
y[row * W + x] =
(MID as u32 + (((row * W + x) as u32 * 29) % (MASK as u32 / 4))) as u16 & MASK;
}
for cx in 0..cw {
let ci = row * cw + cx;
u[ci] = (MID as u32 + ((ci as u32 * 47) % (MASK as u32 / 8))) as u16 & MASK;
v[ci] = (MID as u32 + ((ci as u32 * 31) % (MASK as u32 / 8))) as u16 & MASK;
}
}
let packed = pack_v210_dims(&y, &u, &v, W, H);
let stride = (W.div_ceil(6) * 16) as u32;
let mut n_rgb = std::vec![0u8; (W / 2) * (H / 2) * 3];
let mut n_luma = std::vec![0u8; (W / 2) * (H / 2)];
{
let mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(W, H, AreaResampler::to(W / 2, H / 2))
.unwrap()
.with_native(true)
.with_rgb(&mut n_rgb)
.unwrap()
.with_luma(&mut n_luma)
.unwrap();
v210_to(
&V210Frame::new(&packed, W as u32, H as u32, stride),
FR,
sink.set_kernel_matrix(M),
)
.unwrap();
}
let (t_rgb, _, t_luma) = planar_twin_native(&packed, W, H);
assert_eq!(
n_rgb, t_rgb,
"partial-word u8 rgb must match the planar twin"
);
assert_eq!(
n_luma, t_luma,
"partial-word 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) << 2;
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 = be_run(&packed, true);
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) = be_run(&packed, true);
let (r_rgb, r_rgb16, r_luma) = be_run(&packed, false);
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) << 2;
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(&logical_y(&packed, SRC, SRC), SRC, SRC, OUT, OUT);
let luma_ref: Vec<u8> = y_ref.iter().map(|&c| (c >> 2) 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(&logical_y(&packed, SRC, SRC), SRC, SRC, OUT, OUT);
let expect: Vec<u8> = yb.iter().map(|&by| (by.min(MASK) >> 2) as u8).collect();
assert_eq!(
n_luma, expect,
"full-scale binned Y must clamp to native-max before narrowing, not wrap"
);
let sat = (MASK >> 2) 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(&logical_y(&packed, SRC, SRC), SRC, SRC, OUT, OUT);
let expect: Vec<u8> = yb.iter().map(|&by| (by.min(MASK) >> 2) 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 mut ref_rgb = std::vec![0u8; SRC * SRC * 3];
let mut ref_rgb16 = std::vec![0u16; SRC * SRC * 3];
{
let mut sink = MixedSinker::<V210>::new(SRC, SRC)
.with_rgb(&mut ref_rgb)
.unwrap()
.with_rgb_u16(&mut ref_rgb16)
.unwrap();
v210_to(&frame(&packed), 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 >> 2) 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(&logical_y(&packed, SRC, SRC), SRC, SRC, OUT, OUT)
.iter()
.map(|&by| by.min(MASK))
.collect();
{
let mut luma_u16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_luma_u16(&mut luma_u16)
.unwrap();
v210_to(&frame(&packed), 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 = v210_as_be(&packed);
let mut luma_u16 = std::vec![0u16; OUT * OUT];
{
let be_frame = V210BeFrame::try_new(&p, SRC as u32, SRC as u32, STRIDE).unwrap();
let mut sink = MixedSinker::<V210<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(true)
.with_luma_u16(&mut luma_u16)
.unwrap();
v210_to_endian::<_, true>(&be_frame, 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 mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<V210, 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();
v210_to(&frame(&packed), 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 mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true);
v210_to(&frame(&packed), 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 (y1, u1, v1) = ramp_planes();
let p1 = pack_v210(&y1, &u1, &v1);
let inv = |p: &[u16]| -> Vec<u16> { p.iter().map(|&x| MASK - (x & MASK)).collect() };
let p2 = pack_v210(&inv(&y1), &inv(&u1), &inv(&v1));
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<V210, 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();
v210_to(&frame(&p1), FR, sink.set_kernel_matrix(M)).unwrap();
v210_to(&frame(&p2), FR, sink.set_kernel_matrix(M)).unwrap();
}
let y_ref = bin_to(&logical_y(&p2, SRC, SRC), SRC, SRC, OUT, OUT);
let luma_ref: Vec<u8> = y_ref.iter().map(|&c| (c >> 2) as u8).collect();
assert_eq!(luma, luma_ref, "join did not reset between frames");
}
fn row_slice(packed: &[u8], idx: usize) -> &[u8] {
let stride = STRIDE as usize;
&packed[idx * stride..(idx + 1) * stride]
}
#[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 mut luma = std::vec![0u8; OUT * OUT];
let mut sink =
MixedSinker::<V210, 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 err = sink
.process(V210Row::for_tests(row_slice(&packed, 3), 3, M, FR))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"expected OutOfSequenceRow, got {err:?}"
);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
sink.set_rgb(&mut rgb).unwrap();
sink
.process(V210Row::for_tests(row_slice(&packed, 0), 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 mut luma = std::vec![0u8; OUT * OUT];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut sink =
MixedSinker::<V210, 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(V210Row::for_tests(row_slice(&packed, r), 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_v210_alloc_failure();
let err = sink
.process(V210Row::for_tests(row_slice(&packed, 2), 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 = std::vec![0u16; OUT * OUT * 3];
let mut sink2 =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16b)
.unwrap();
let err2 = sink2
.process(V210Row::for_tests(row_slice(&packed, 0), 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 mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16)
.unwrap();
crate::sinker::mixed::arm_v210_alloc_failure();
let err0 = sink
.process(V210Row::for_tests(row_slice(&packed, 0), 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_v210_alloc_failure();
let err2 = sink
.process(V210Row::for_tests(row_slice(&packed, 2), 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 = std::vec![0u16; OUT * OUT * 3];
let mut sink2 =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_rgb_u16(&mut rgb_u16b)
.unwrap();
let err3 = sink2
.process(V210Row::for_tests(row_slice(&packed, 0), 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 mut luma = std::vec![0u8; OUT * OUT];
let mut sink =
MixedSinker::<V210, 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(V210Row::for_tests(row_slice(&packed, 0), 0, M, FR))
.expect("native row 0 freezes the route and succeeds");
sink.set_native(false);
let err = sink
.process(V210Row::for_tests(row_slice(&packed, 1), 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 mut luma = std::vec![0u8; OUT * OUT];
let mut sink =
MixedSinker::<V210, 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(V210Row::for_tests(row_slice(&packed, 0), 0, M, FR))
.expect("row-stage row 0 freezes the route and succeeds");
sink.set_native(true);
let err = sink
.process(V210Row::for_tests(row_slice(&packed, 1), 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 mut luma = std::vec![0u8; OUT * OUT];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut sink =
MixedSinker::<V210, 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(V210Row::for_tests(row_slice(&packed, 0), 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(V210Row::for_tests(row_slice(&packed, 1), 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"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_only_native_skips_chroma_planning() {
let packed = uniform_gray_packed(MID);
crate::sinker::mixed::arm_planar_hb_native_chroma_failure();
let mut luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_luma(&mut luma)
.unwrap();
v210_to(&frame(&packed), FR, sink.set_kernel_matrix(M))
.expect("luma-only native must not plan chroma");
}
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut sink =
MixedSinker::<V210, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap();
assert!(
v210_to(&frame(&packed), FR, sink.set_kernel_matrix(M)).is_err(),
"colour native must reach chroma planning (the armed failpoint fires)"
);
}