use crate::{
KernelMatrix, PixelSink,
resample::{AreaResampler, Bicublin, FilterStream, ResampleError, SwscaleBicubic, Triangle},
sinker::{MixedSinker, MixedSinkerError},
source::{Yuv420p, Yuv420pRow, Yuv422p, yuv420p_to, yuv422p_to},
};
use mediaframe::frame::{Yuv420pFrame, Yuv422pFrame};
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;
fn ramp_420(sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let (cw, ch) = (sw / 2, sh / 2);
let mut y = vec![0u8; sw * sh];
let mut u = vec![0u8; cw * ch];
let mut v = vec![0u8; cw * ch];
for (i, p) in y.iter_mut().enumerate() {
*p = (40 + (i % 100) * 2) as u8;
}
for (i, p) in u.iter_mut().enumerate() {
*p = (70 + (i % 30) * 5) as u8;
}
for (i, p) in v.iter_mut().enumerate() {
*p = (200u8).wrapping_sub(((i % 40) * 4) as u8);
}
(y, u, v)
}
struct Outputs {
rgb: Vec<u8>,
rgba: Vec<u8>,
luma: Vec<u8>,
luma_u16: Vec<u16>,
}
fn bicublin_outputs(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
) -> Outputs {
let cw = (sw / 2) as u32;
let src = Yuv420pFrame::new(y, u, v, sw as u32, sh as u32, sw as u32, cw, cw);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgba = vec![0u8; ow * oh * 4];
let mut luma = vec![0u8; ow * oh];
let mut luma_u16 = vec![0u16; ow * oh];
{
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(sw, sh, Bicublin::to(ow, oh))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
yuv420p_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
Outputs {
rgb,
rgba,
luma,
luma_u16,
}
}
fn plane_filter<K: crate::resample::FilterKernel>(
kernel: K,
plane: &[u8],
pw: usize,
ph: usize,
ow: usize,
oh: usize,
) -> Vec<u8> {
let plan = crate::resample::ResamplePlan::filter(pw, ph, ow, oh, &kernel).expect("filter plan");
let fh = plan.filter_h().expect("h windows");
let fv = plan.filter_v().expect("v windows");
let mut stream = FilterStream::<u8>::new(fh, fv, pw, ph, 1).expect("geometry");
let mut out = vec![0u8; ow * oh];
for row in 0..ph {
stream
.feed_row(row, &plane[row * pw..(row + 1) * pw], true, |oy, fin| {
out[oy * ow..(oy + 1) * ow].copy_from_slice(fin);
})
.expect("rows in order");
}
out
}
fn per_plane_oracle(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
) -> (Vec<u8>, Vec<u8>) {
let (cw, ch) = (sw / 2, sh / 2);
let y_f = plane_filter(SwscaleBicubic, y, sw, sh, ow, oh);
let u_f = plane_filter(Triangle, u, cw, ch, ow, oh);
let v_f = plane_filter(Triangle, v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
for oy in 0..oh {
crate::row::yuv_444_to_rgb_row(
&y_f[oy * ow..(oy + 1) * ow],
&u_f[oy * ow..(oy + 1) * ow],
&v_f[oy * ow..(oy + 1) * ow],
&mut rgb[oy * ow * 3..(oy + 1) * ow * 3],
ow,
M,
FR,
true,
);
}
(rgb, y_f)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_equals_independent_per_plane_oracle() {
for &(sw, sh, ow, oh) in &[
(8usize, 8usize, 4usize, 4usize),
(4, 4, 7, 7),
(16, 12, 6, 5),
] {
let (y, u, v) = ramp_420(sw, sh);
let got = bicublin_outputs(&y, &u, &v, sw, sh, ow, oh);
let (want_rgb, want_luma) = per_plane_oracle(&y, &u, &v, sw, sh, ow, oh);
for (i, (&g, &w)) in got.rgb.iter().zip(want_rgb.iter()).enumerate() {
assert_eq!(
g, w,
"bicublin {sw}x{sh}->{ow}x{oh} rgb[{i}]: {g} vs per-plane oracle {w}"
);
}
for (i, (&g, &w)) in got.luma.iter().zip(want_luma.iter()).enumerate() {
assert_eq!(
g, w,
"bicublin {sw}x{sh}->{ow}x{oh} luma[{i}]: {g} vs cubic native-Y {w}"
);
}
for (px, c) in got.rgba.chunks_exact(4).zip(want_rgb.chunks_exact(3)) {
assert_eq!(&px[..3], c, "bicublin rgba colour == rgb");
assert_eq!(px[3], 0xFF, "bicublin rgba opaque alpha");
}
for (&lo, &hi) in got.luma.iter().zip(got.luma_u16.iter()) {
assert_eq!(hi, lo as u16, "bicublin luma_u16 == luma zero-extended");
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_luma_chroma_kernels_differ_from_single_kernel() {
let (sw, sh, ow, oh) = (16usize, 12usize, 6usize, 5usize);
let (y, u, v) = ramp_420(sw, sh);
let got = bicublin_outputs(&y, &u, &v, sw, sh, ow, oh);
let single_cubic = {
let (cw, ch) = (sw / 2, sh / 2);
let y_f = plane_filter(SwscaleBicubic, &y, sw, sh, ow, oh);
let u_f = plane_filter(SwscaleBicubic, &u, cw, ch, ow, oh);
let v_f = plane_filter(SwscaleBicubic, &v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
for oy in 0..oh {
crate::row::yuv_444_to_rgb_row(
&y_f[oy * ow..(oy + 1) * ow],
&u_f[oy * ow..(oy + 1) * ow],
&v_f[oy * ow..(oy + 1) * ow],
&mut rgb[oy * ow * 3..(oy + 1) * ow * 3],
ow,
M,
FR,
true,
);
}
rgb
};
let single_linear = {
let (cw, ch) = (sw / 2, sh / 2);
let y_f = plane_filter(Triangle, &y, sw, sh, ow, oh);
let u_f = plane_filter(Triangle, &u, cw, ch, ow, oh);
let v_f = plane_filter(Triangle, &v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
for oy in 0..oh {
crate::row::yuv_444_to_rgb_row(
&y_f[oy * ow..(oy + 1) * ow],
&u_f[oy * ow..(oy + 1) * ow],
&v_f[oy * ow..(oy + 1) * ow],
&mut rgb[oy * ow * 3..(oy + 1) * ow * 3],
ow,
M,
FR,
true,
);
}
rgb
};
assert_ne!(
got.rgb, single_cubic,
"bicublin must differ from a single-cubic filter (chroma is linear, not cubic)"
);
assert_ne!(
got.rgb, single_linear,
"bicublin must differ from a single-linear filter (luma is cubic, not linear)"
);
}
fn chroma_row(plane: &[u8], cw: usize, idx: usize) -> &[u8] {
let c = idx / 2;
&plane[c * cw..(c + 1) * cw]
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_mid_frame_output_change_is_rejected() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let mut luma_a = vec![0u8; OW * OH];
let mut luma_b = vec![0u8; OW * OH];
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_luma(&mut luma_a)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.unwrap();
sink.set_luma(&mut luma_b).unwrap();
let err = sink
.process(Yuv420pRow::for_tests(
yr(1),
chroma_row(&u, cw, 1),
chroma_row(&v, cw, 1),
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"mid-frame output change must be ResampleOutputsChanged, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_out_of_sequence_row_is_rejected() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
let mut luma = vec![0u8; OW * OH];
{
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
let err = sink
.process(Yuv420pRow::for_tests(
yr(1),
chroma_row(&u, cw, 1),
chroma_row(&v, cw, 1),
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(crate::resample::ResampleError::OutOfSequenceRow(_))
),
"out-of-sequence row must be OutOfSequenceRow, got {err:?}"
);
for i in 0..SH {
sink
.process(Yuv420pRow::for_tests(
yr(i),
chroma_row(&u, cw, i),
chroma_row(&v, cw, i),
i,
M,
FR,
))
.unwrap();
}
}
let want = plane_filter(SwscaleBicubic, &y, SW, SH, OW, OH);
assert_eq!(
luma, want,
"retried-in-order luma == cubic native-Y filter (rejected OOS row did not poison state)"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_same_size_still_filters_all_planes() {
for &(sw, sh) in &[(8usize, 8usize), (16, 12), (6, 10)] {
let (ow, oh) = (sw, sh);
let (y, u, v) = ramp_420(sw, sh);
let got = bicublin_outputs(&y, &u, &v, sw, sh, ow, oh);
let (want_rgb, want_luma) = per_plane_oracle(&y, &u, &v, sw, sh, ow, oh);
assert_eq!(
got.rgb, want_rgb,
"same-size {sw}x{sh} bicublin rgb == per-plane oracle (chroma still linear-filtered)"
);
assert_eq!(
got.luma, want_luma,
"same-size {sw}x{sh} bicublin luma == cubic native-Y (luma still cubic-filtered)"
);
let cw_u = (sw / 2) as u32;
let src = Yuv420pFrame::new(&y, &u, &v, sw as u32, sh as u32, sw as u32, cw_u, cw_u);
let mut identity_rgb = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<Yuv420p>::new(sw, sh)
.with_rgb(&mut identity_rgb)
.unwrap();
yuv420p_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_ne!(
got.rgb, identity_rgb,
"same-size {sw}x{sh} bicublin rgb must differ from the identity convert (chroma is \
linear-filter-upsampled, not the dropped-filter pass-through)"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_plan_rejected_by_non_yuv420p_filter_sink() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (cw, ch) = (SW / 2, SH);
let y = vec![60u8; SW * SH];
let u = vec![90u8; cw * ch];
let v = vec![150u8; cw * ch];
let src = Yuv422pFrame::new(
&y, &u, &v, SW as u32, SH as u32, SW as u32, cw as u32, cw as u32,
);
let mut rgb = vec![0u8; OW * OH * 3];
let mut sink = MixedSinker::<Yuv422p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.expect("plan builds (the format only matters at process time)")
.with_rgb(&mut rgb)
.unwrap();
let err = yuv422p_to(&src, FR, sink.set_kernel_matrix(M)).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::UnsupportedFilter(_))
),
"a bicublin plan on a Yuv422p filter sink must be UnsupportedFilter, got {err:?}"
);
assert!(
rgb.iter().all(|&b| b == 0),
"a rejected bicublin plan must not touch the Yuv422p sink's output"
);
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_box_alloc_failure_is_recoverable() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
let mut rgb = vec![0u8; OW * OH * 3];
{
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
crate::resample::arm_box_failure();
let err = sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row box-alloc refusal must surface AllocationFailed (not abort), got {err:?}"
);
}
assert!(
rgb.iter().all(|&b| b == 0),
"a box-alloc-refused first row must not touch the output"
);
let mut rgb2 = vec![0u8; OW * OH * 3];
{
let cw_u = (SW / 2) as u32;
let src = Yuv420pFrame::new(&y, &u, &v, SW as u32, SH as u32, SW as u32, cw_u, cw_u);
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_rgb(&mut rgb2)
.unwrap();
yuv420p_to(&src, FR, sink.set_kernel_matrix(M))
.expect("a fresh frame after the consumed failpoint resamples cleanly");
}
let (want_rgb, _) = per_plane_oracle(&y, &u, &v, SW, SH, OW, OH);
assert_eq!(
rgb2, want_rgb,
"the recovered frame equals the per-plane oracle (box-alloc refusal was recoverable)"
);
}
#[test]
#[cfg(all(feature = "std", feature = "rgb"))]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_first_build_scratch_oom_leaves_freeze_unfrozen_for_retry() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
let mut rgb = vec![0u8; OW * OH * 3];
let mut luma = vec![0u8; OW * OH];
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
crate::sinker::mixed::arm_native_rgb_scratch_failure();
let err = sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-build RGB-scratch OOM must surface AllocationFailed, got {err:?}"
);
sink.set_luma(&mut luma).unwrap();
sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.expect("row 0 must succeed after a first-build scratch OOM (freeze uncommitted)");
}
#[test]
#[cfg(all(feature = "std", feature = "rgb"))]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bicublin_colour_capability_rebuild_scratch_oom_leaves_freeze_unfrozen_for_retry() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
let mut luma = vec![0u8; OW * OH];
let mut rgb = vec![0u8; OW * OH * 3];
let (mut hh, mut ss, mut vv) = (vec![0u8; OW * OH], vec![0u8; OW * OH], vec![0u8; OW * OH]);
let mut sink = MixedSinker::<Yuv420p, Bicublin>::with_resampler(SW, SH, Bicublin::to(OW, OH))
.unwrap()
.with_luma(&mut luma)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
for i in 0..SH {
sink
.process(Yuv420pRow::for_tests(
yr(i),
chroma_row(&u, cw, i),
chroma_row(&v, cw, i),
i,
M,
FR,
))
.expect("luma-only frame builds the bicublin join");
}
sink.begin_frame(SW as u32, SH as u32).unwrap();
sink.set_rgb(&mut rgb).unwrap();
crate::sinker::mixed::arm_native_rgb_scratch_failure();
let err = sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"the colour-capability rebuild scratch OOM must surface AllocationFailed, got {err:?}"
);
sink.set_hsv(&mut hh, &mut ss, &mut vv).unwrap();
sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.expect("row 0 with a changed output set must succeed after a rebuild scratch OOM");
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn area_stream_box_alloc_failure_is_recoverable() {
const SW: usize = 8;
const SH: usize = 8;
const OW: usize = 4;
const OH: usize = 4;
let (y, u, v) = ramp_420(SW, SH);
let cw = SW / 2;
let yr = |i: usize| &y[i * SW..(i + 1) * SW];
let mut rgb = vec![0u8; OW * OH * 3];
{
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(SW, SH, AreaResampler::to(OW, OH))
.unwrap()
.with_native(false)
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SW as u32, SH as u32).unwrap();
crate::resample::arm_box_failure();
let err = sink
.process(Yuv420pRow::for_tests(
yr(0),
chroma_row(&u, cw, 0),
chroma_row(&v, cw, 0),
0,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row stream box-alloc refusal must surface AllocationFailed (not abort), got {err:?}"
);
}
assert!(
rgb.iter().all(|&b| b == 0),
"a box-alloc-refused first row must not touch the output"
);
let mut rgb2 = vec![0u8; OW * OH * 3];
{
let cw_u = (SW / 2) as u32;
let src = Yuv420pFrame::new(&y, &u, &v, SW as u32, SH as u32, SW as u32, cw_u, cw_u);
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(SW, SH, AreaResampler::to(OW, OH))
.unwrap()
.with_native(false)
.with_rgb(&mut rgb2)
.unwrap();
yuv420p_to(&src, FR, sink.set_kernel_matrix(M))
.expect("a fresh frame after the consumed failpoint resamples cleanly");
}
assert!(
rgb2.iter().any(|&b| b != 0),
"the recovered frame produced real output (box-alloc refusal was recoverable)"
);
}