use crate::{
KernelMatrix, PixelSink,
resample::{AreaResampler, ResampleError},
row::rgb_to_hsv_row,
sinker::{MixedSinker, MixedSinkerError},
source::{Uyyvyy411, Uyyvyy411Row, uyyvyy411_to},
};
use mediaframe::frame::Uyyvyy411Frame;
use super::force_row_stage;
const SRC_W: usize = 8;
const SRC_H: usize = 8;
const OUT_W: usize = 4;
const OUT_H: usize = 4;
fn build_uyyvyy411(
width: usize,
height: usize,
y_at: impl Fn(usize, usize) -> u8,
u_at: impl Fn(usize, usize) -> u8,
v_at: impl Fn(usize, usize) -> u8,
) -> Vec<u8> {
assert_eq!(width & 3, 0, "uyyvyy411 width must be multiple of 4");
let mut buf = std::vec![0u8; width * 3 / 2 * height];
for row in 0..height {
let base = row * width * 3 / 2;
for col in (0..width).step_by(4) {
let blk = base + (col / 4) * 6;
buf[blk] = u_at(col, row);
buf[blk + 1] = y_at(col, row);
buf[blk + 2] = y_at(col + 1, row);
buf[blk + 3] = v_at(col, row);
buf[blk + 4] = y_at(col + 2, row);
buf[blk + 5] = y_at(col + 3, row);
}
}
buf
}
fn deinterleave_y(packed: &[u8], width: usize, height: usize) -> Vec<u8> {
let mut y = std::vec![0u8; width * height];
for row in 0..height {
let base = row * width * 3 / 2;
for col in (0..width).step_by(4) {
let blk = base + (col / 4) * 6;
let o = row * width + col;
y[o] = packed[blk + 1];
y[o + 1] = packed[blk + 2];
y[o + 2] = packed[blk + 4];
y[o + 3] = packed[blk + 5];
}
}
y
}
fn block_mean_2x2_plane(plane: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; OUT_W * OUT_H];
for oy in 0..OUT_H {
for ox in 0..OUT_W {
let mut s = 0u32;
for dy in 0..2 {
for dx in 0..2 {
s += plane[(oy * 2 + dy) * SRC_W + ox * 2 + dx] as u32;
}
}
out[oy * OUT_W + ox] = ((s + 2) / 4) as u8;
}
}
out
}
fn block_mean_2x2_rgb(rgb: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; OUT_W * OUT_H * 3];
for oy in 0..OUT_H {
for ox in 0..OUT_W {
for c in 0..3 {
let mut s = 0u32;
for dy in 0..2 {
for dx in 0..2 {
s += rgb[((oy * 2 + dy) * SRC_W + ox * 2 + dx) * 3 + c] as u32;
}
}
out[(oy * OUT_W + ox) * 3 + c] = ((s + 2) / 4) as u8;
}
}
}
out
}
fn ramp_frame() -> Vec<u8> {
build_uyyvyy411(
SRC_W,
SRC_H,
|x, y| 32 + ((x + y * SRC_W) % 64) as u8,
|x, y| 100 + ((x / 4 + y) % 40) as u8,
|x, y| 140 - ((x / 4 + y) % 40) as u8,
)
}
fn direct_rgb(packed: &[u8]) -> Vec<u8> {
let src = Uyyvyy411Frame::new(packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let mut rgb = std::vec![0u8; SRC_W * SRC_H * 3];
{
let mut sink = MixedSinker::<Uyyvyy411>::new(SRC_W, SRC_H)
.with_rgb(&mut rgb)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
rgb
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_resample_rgb_matches_binned_direct_conversion() {
let packed = ramp_frame();
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let mut rgb_a = std::vec![0u8; OUT_W * OUT_H * 3];
{
let mut sink = force_row_stage(
MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap(),
)
.with_rgb(&mut rgb_a)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
let expected = block_mean_2x2_rgb(&direct_rgb(&packed));
assert_eq!(
rgb_a, expected,
"rgb: row-stage resample must equal block-mean of the direct conversion"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_resample_luma_is_area_downscaled_y_plane() {
let packed = ramp_frame();
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let (mut luma, mut luma_u16) = (
std::vec![0u8; OUT_W * OUT_H],
std::vec![0u16; OUT_W * OUT_H],
);
{
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
let y_ref = block_mean_2x2_plane(&deinterleave_y(&packed, SRC_W, SRC_H));
assert_eq!(luma, y_ref, "luma must be the area-downscaled Y plane");
let y_ref_u16: Vec<u16> = y_ref.iter().map(|&b| b as u16).collect();
assert_eq!(
luma_u16, y_ref_u16,
"luma_u16 must be the area-downscaled Y, zero-extended"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_resample_luma_comes_from_y_not_rgb_under_saturated_chroma() {
let packed = build_uyyvyy411(SRC_W, SRC_H, |_, _| 16, |_, _| 240, |_, _| 16);
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let mut luma = std::vec![0u8; OUT_W * OUT_H];
{
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap()
.with_luma(&mut luma)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
assert!(
luma.iter().all(|&b| b == 16),
"luma must area-resample the Y plane (16), not RGB-derived luma; got {luma:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_resample_all_outputs_combo() {
let packed = ramp_frame();
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let mut rgb = std::vec![0u8; OUT_W * OUT_H * 3];
let mut rgba = std::vec![0u8; OUT_W * OUT_H * 4];
let mut luma = std::vec![0u8; OUT_W * OUT_H];
let mut luma_u16 = std::vec![0u16; OUT_W * OUT_H];
let mut hh = std::vec![0u8; OUT_W * OUT_H];
let mut ss = std::vec![0u8; OUT_W * OUT_H];
let mut vv = std::vec![0u8; OUT_W * OUT_H];
{
let mut sink = force_row_stage(
MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap(),
)
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
let expected_rgb = block_mean_2x2_rgb(&direct_rgb(&packed));
assert_eq!(rgb, expected_rgb, "rgb");
for i in 0..(OUT_W * OUT_H) {
assert_eq!(rgba[i * 4], rgb[i * 3], "RGBA R at {i}");
assert_eq!(rgba[i * 4 + 1], rgb[i * 3 + 1], "RGBA G at {i}");
assert_eq!(rgba[i * 4 + 2], rgb[i * 3 + 2], "RGBA B at {i}");
assert_eq!(rgba[i * 4 + 3], 0xFF, "RGBA alpha at {i}");
}
let y_ref = block_mean_2x2_plane(&deinterleave_y(&packed, SRC_W, SRC_H));
assert_eq!(luma, y_ref, "luma from Y");
let y_ref_u16: Vec<u16> = y_ref.iter().map(|&b| b as u16).collect();
assert_eq!(luma_u16, y_ref_u16, "luma_u16 from Y");
let mut h_ref = std::vec![0u8; OUT_W * OUT_H];
let mut s_ref = std::vec![0u8; OUT_W * OUT_H];
let mut v_ref = std::vec![0u8; OUT_W * OUT_H];
for oy in 0..OUT_H {
rgb_to_hsv_row(
&expected_rgb[oy * OUT_W * 3..(oy + 1) * OUT_W * 3],
&mut h_ref[oy * OUT_W..(oy + 1) * OUT_W],
&mut s_ref[oy * OUT_W..(oy + 1) * OUT_W],
&mut v_ref[oy * OUT_W..(oy + 1) * OUT_W],
OUT_W,
true,
);
}
assert_eq!(hh, h_ref, "hsv H");
assert_eq!(ss, s_ref, "hsv S");
assert_eq!(vv, v_ref, "hsv V");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_identity_plan_matches_new_sink() {
let packed = ramp_frame();
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let direct = direct_rgb(&packed);
let mut via_area = std::vec![0u8; SRC_W * SRC_H * 3];
{
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(SRC_W, SRC_H),
)
.unwrap()
.with_rgb(&mut via_area)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
assert_eq!(direct, via_area, "identity plan must match the direct sink");
}
#[test]
fn uyyvyy411_resample_no_outputs_is_a_no_op() {
let packed = ramp_frame();
let src = Uyyvyy411Frame::new(&packed, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap();
uyyvyy411_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn uyyvyy411_resample_reuses_streams_across_frames() {
let p1 = ramp_frame();
let mut p2 = p1.clone();
for b in p2.iter_mut() {
*b = 255 - *b;
}
let (mut luma, mut rgb) = (
std::vec![0u8; OUT_W * OUT_H],
std::vec![0u8; OUT_W * OUT_H * 3],
);
{
let mut sink = force_row_stage(
MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap(),
)
.with_luma(&mut luma)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
let f1 = Uyyvyy411Frame::new(&p1, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
let f2 = Uyyvyy411Frame::new(&p2, SRC_W as u32, SRC_H as u32, (SRC_W * 3 / 2) as u32);
uyyvyy411_to(&f1, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
uyyvyy411_to(&f2, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
let y2_ref = block_mean_2x2_plane(&deinterleave_y(&p2, SRC_W, SRC_H));
assert_eq!(luma, y2_ref, "frame 2 luma must area-downscale frame 2's Y");
let rgb2_ref = block_mean_2x2_rgb(&direct_rgb(&p2));
assert_eq!(rgb, rgb2_ref, "frame 2 rgb must bin frame 2's conversion");
}
#[test]
fn uyyvyy411_resample_rejects_out_of_sequence_rows() {
let packed = ramp_frame();
let mut rgb = std::vec![0u8; OUT_W * OUT_H * 3];
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC_W as u32, SRC_H as u32).unwrap();
let row_bytes = SRC_W * 3 / 2;
let row2 = Uyyvyy411Row::for_tests(
&packed[row_bytes * 2..row_bytes * 3],
2,
KernelMatrix::Bt601,
true,
);
let err = sink.process(row2).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"got {err:?}"
);
}
#[test]
fn uyyvyy411_resample_rejects_changed_output_set_midframe() {
let packed = ramp_frame();
let mut rgb = std::vec![0u8; OUT_W * OUT_H * 3];
let mut luma = std::vec![0u8; OUT_W * OUT_H];
let row_bytes = SRC_W * 3 / 2;
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC_W as u32, SRC_H as u32).unwrap();
sink
.process(Uyyvyy411Row::for_tests(
&packed[0..row_bytes],
0,
KernelMatrix::Bt601,
true,
))
.unwrap();
sink.set_luma(&mut luma).unwrap();
let err = sink
.process(Uyyvyy411Row::for_tests(
&packed[row_bytes..row_bytes * 2],
1,
KernelMatrix::Bt601,
true,
))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"got {err:?}"
);
}
#[test]
fn uyyvyy411_rejected_first_row_does_not_poison_output_retry() {
let packed = ramp_frame();
let row_bytes = SRC_W * 3 / 2;
let mut luma = std::vec![0u8; OUT_W * OUT_H];
let mut sink = MixedSinker::<Uyyvyy411, AreaResampler>::with_resampler(
SRC_W,
SRC_H,
AreaResampler::to(OUT_W, OUT_H),
)
.unwrap()
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SRC_W as u32, SRC_H as u32).unwrap();
let err = sink
.process(Uyyvyy411Row::for_tests(
&packed[row_bytes * 3..row_bytes * 4],
3,
KernelMatrix::Bt601,
true,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"expected OutOfSequenceRow, got {err:?}"
);
let mut rgb = std::vec![0u8; OUT_W * OUT_H * 3];
sink.set_rgb(&mut rgb).unwrap();
sink
.process(Uyyvyy411Row::for_tests(
&packed[0..row_bytes],
0,
KernelMatrix::Bt601,
true,
))
.expect("row 0 must succeed after a rejected out-of-sequence first row");
}