use crate::{
KernelMatrix, PixelSink,
frame::{Ayuv64BeFrame, Ayuv64Frame, Ayuv64LeFrame},
resample::{AreaResampler, ResampleError},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::{Ayuv64, Ayuv64Row, ayuv64_to, ayuv64_to_endian},
};
const SRC: usize = 8;
const OUT: usize = 4;
const M: KernelMatrix = KernelMatrix::Bt709;
const FR: bool = true;
const FR_LIMITED: bool = false;
const MAX16: u32 = 65535;
fn packed_frame(seed: u32) -> Vec<u16> {
let mut buf = std::vec![0u16; SRC * SRC * 4];
super::pseudo_random_u16_low_n_bits(&mut buf, seed, 16);
buf
}
fn ayuv64_frame(buf: &[u16]) -> Ayuv64Frame<'_> {
Ayuv64Frame::try_new(buf, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap()
}
fn direct_rgba_u8(packed: &[u16], full_range: bool) -> Vec<u8> {
let frame = ayuv64_frame(packed);
let mut rgba = std::vec![0u8; SRC * SRC * 4];
{
let mut sink = MixedSinker::<Ayuv64>::new(SRC, SRC)
.with_rgba(&mut rgba)
.unwrap();
ayuv64_to(&frame, full_range, sink.set_kernel_matrix(M)).unwrap();
}
rgba
}
fn direct_rgba_u16(packed: &[u16], full_range: bool) -> Vec<u16> {
let frame = ayuv64_frame(packed);
let mut rgba = std::vec![0u16; SRC * SRC * 4];
{
let mut sink = MixedSinker::<Ayuv64>::new(SRC, SRC)
.with_rgba_u16(&mut rgba)
.unwrap();
ayuv64_to(&frame, full_range, sink.set_kernel_matrix(M)).unwrap();
}
rgba
}
fn block_mean_rgba<T: Copy + Into<u64>>(src: &[T], round_div_to: fn(u64) -> u64) -> Vec<u64> {
let mut out = std::vec![0u64; OUT * OUT * 4];
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..4 {
let mut acc = 0u64;
for dy in 0..2 {
for dx in 0..2 {
acc += src[((oy * 2 + dy) * SRC + ox * 2 + dx) * 4 + c].into();
}
}
out[(oy * OUT + ox) * 4 + c] = round_div_to(acc);
}
}
}
out
}
fn bin4(acc: u64) -> u64 {
(acc + 2) / 4
}
fn block_mean_rgba_u8(src: &[u8]) -> Vec<u8> {
block_mean_rgba(src, bin4)
.iter()
.map(|&v| v as u8)
.collect()
}
fn block_mean_rgba_u16(src: &[u16]) -> Vec<u16> {
block_mean_rgba(src, bin4)
.iter()
.map(|&v| v as u16)
.collect()
}
fn block_mean_native_y(packed: &[u16]) -> Vec<u16> {
let mut out = std::vec![0u16; OUT * OUT];
for oy in 0..OUT {
for ox in 0..OUT {
let mut acc = 0u64;
for dy in 0..2 {
for dx in 0..2 {
acc += packed[((oy * 2 + dy) * SRC + ox * 2 + dx) * 4 + 1] as u64;
}
}
out[oy * OUT + ox] = ((acc + 2) / 4) as u16;
}
}
out
}
fn premultiply<T: Copy + Into<u32> + TryFrom<u32>>(plane: &mut [T], max: u32) {
let half = max / 2;
for px in plane.chunks_exact_mut(4) {
let a: u32 = px[3].into();
for c in &mut px[..3] {
let v: u32 = (*c).into();
let pm = (v * a + half) / max;
*c = T::try_from(pm).unwrap_or_else(|_| unreachable!());
}
}
}
fn unpremultiply<T: Copy + Into<u32> + TryFrom<u32>>(plane: &[T], max: u32) -> Vec<T> {
let mut out: Vec<T> = plane.to_vec();
for (o, i) in out.chunks_exact_mut(4).zip(plane.chunks_exact(4)) {
let a: u32 = i[3].into();
for c in 0..3 {
let pm: u32 = i[c].into();
let straight = (pm * max + a / 2).checked_div(a).map_or(0, |q| q.min(max));
o[c] = T::try_from(straight).unwrap_or_else(|_| unreachable!());
}
o[3] = i[3];
}
out
}
fn drop_alpha_u8(rgba: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; rgba.len() / 4 * 3];
for (o, i) in out.chunks_exact_mut(3).zip(rgba.chunks_exact(4)) {
o.copy_from_slice(&i[..3]);
}
out
}
fn drop_alpha_u16(rgba: &[u16]) -> Vec<u16> {
let mut out = std::vec![0u16; rgba.len() / 4 * 3];
for (o, i) in out.chunks_exact_mut(3).zip(rgba.chunks_exact(4)) {
o.copy_from_slice(&i[..3]);
}
out
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_straight_all_outputs_match_their_own_block_mean() {
let packed = packed_frame(0xBEEF);
let frame = ayuv64_frame(&packed);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma = std::vec![0u8; OUT * OUT];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut hh = std::vec![0u8; OUT * OUT];
let mut ss = std::vec![0u8; OUT * OUT];
let mut vv = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let rgba_ref = block_mean_rgba_u8(&direct_rgba_u8(&packed, FR));
let rgb_ref = drop_alpha_u8(&rgba_ref);
assert_eq!(rgba, rgba_ref, "rgba == u8 block mean (alpha averaged)");
assert_eq!(rgb, rgb_ref, "rgb == drop-alpha(u8 block mean)");
let rgba_u16_ref = block_mean_rgba_u16(&direct_rgba_u16(&packed, FR));
assert_eq!(rgba_u16, rgba_u16_ref, "rgba_u16 == native u16 block mean");
assert_eq!(
rgb_u16,
drop_alpha_u16(&rgba_u16_ref),
"rgb_u16 == drop-alpha(u16 block mean)"
);
let y_binned = block_mean_native_y(&packed);
assert_eq!(luma_u16, y_binned, "luma_u16 == native-Y block mean");
let luma_ref: Vec<u8> = y_binned.iter().map(|&p| (p >> 8) as u8).collect();
assert_eq!(luma, luma_ref, "luma == native Y >> 8");
let mut h_ref = std::vec![0u8; OUT * OUT];
let mut s_ref = std::vec![0u8; OUT * OUT];
let mut v_ref = std::vec![0u8; OUT * OUT];
crate::row::rgb_to_hsv_row(
&rgb_ref,
&mut h_ref,
&mut s_ref,
&mut v_ref,
OUT * OUT,
false,
);
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 ayuv64_straight_alpha_is_averaged_not_forced_opaque() {
let mut packed = packed_frame(0x9E37);
for (i, px) in packed.chunks_exact_mut(4).enumerate() {
px[0] = (i as u16).wrapping_mul(700); }
let frame = ayuv64_frame(&packed);
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba_u16, block_mean_rgba_u16(&direct_rgba_u16(&packed, FR)));
assert!(
rgba_u16.chunks_exact(4).any(|px| px[3] != MAX16 as u16),
"resampled u16 alpha was forced opaque"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_uniform_gray_independent_u8_vs_u16_colour() {
let packed = std::vec![[40000u16, 38000, 32768, 32768]; SRC * SRC]
.into_iter()
.flatten()
.collect::<Vec<u16>>();
let full_u8 = direct_rgba_u8(&packed, FR);
let full_u16 = direct_rgba_u16(&packed, FR);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap();
ayuv64_to(&ayuv64_frame(&packed), FR, sink.set_kernel_matrix(M)).unwrap();
}
let gray_u8 = &full_u8[..3];
for px in rgb.chunks_exact(3) {
assert_eq!(
px, gray_u8,
"uniform-gray u8 RGB changed (narrowed from u16?)"
);
}
let gray_u16 = &full_u16[..3];
for px in rgb_u16.chunks_exact(3) {
assert_eq!(px, gray_u16, "uniform-gray u16 RGB changed under downscale");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_saturated_chroma_u8_is_not_a_narrowing_of_u16() {
let mut packed = std::vec![0u16; SRC * SRC * 4];
for (i, px) in packed.chunks_exact_mut(4).enumerate() {
px[0] = 0xFFFF; px[1] = 30000 + (i as u16) * 100; px[2] = (i as u16).wrapping_mul(2000); px[3] = 60000u16.wrapping_sub((i as u16).wrapping_mul(1500)); }
let frame = ayuv64_frame(&packed);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let rgb_ref = drop_alpha_u8(&block_mean_rgba_u8(&direct_rgba_u8(&packed, FR)));
let rgb_u16_ref = drop_alpha_u16(&block_mean_rgba_u16(&direct_rgba_u16(&packed, FR)));
assert_eq!(rgb, rgb_ref, "u8 RGB must bin the direct u8 conversion");
assert_eq!(
rgb_u16, rgb_u16_ref,
"u16 RGB must bin the direct u16 conversion"
);
let narrowed: Vec<u8> = rgb_u16_ref.iter().map(|&v| (v >> 8) as u8).collect();
assert_ne!(
rgb_ref, narrowed,
"fixture failed to exercise u8-vs-u16 divergence"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_premultiplied_independent_u8_and_u16() {
let packed = packed_frame(0x1234);
let frame = ayuv64_frame(&packed);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let mut pm8 = direct_rgba_u8(&packed, FR);
premultiply(&mut pm8, 255);
let oracle8 = unpremultiply(&block_mean_rgba_u8(&pm8), 255);
assert_eq!(rgba, oracle8, "premult u8 rgba");
let mut pm16 = direct_rgba_u16(&packed, FR);
premultiply(&mut pm16, MAX16);
let oracle16 = unpremultiply(&block_mean_rgba_u16(&pm16), MAX16);
assert_eq!(rgba_u16, oracle16, "premult u16 rgba");
assert_eq!(
luma_u16,
block_mean_native_y(&packed),
"premult luma_u16 == native-Y bin"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_premultiplied_transparent_block_does_not_bleed() {
let mut packed = packed_frame(0xABCD);
for off in [(0, 0), (1, 0), (0, 1), (1, 1)] {
let i = off.1 * SRC + off.0;
packed[i * 4] = 0; packed[i * 4 + 1] = 60000; }
let frame = ayuv64_frame(&packed);
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(
&rgba_u16[..4],
&[0, 0, 0, 0],
"transparent block bled colour"
);
let mut pm16 = direct_rgba_u16(&packed, FR);
premultiply(&mut pm16, MAX16);
let oracle16 = unpremultiply(&block_mean_rgba_u16(&pm16), MAX16);
assert_eq!(rgba_u16, oracle16, "premult u16 output != oracle");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_premultiplied_nonuniform_alpha_luma_is_native_y_not_colour() {
let mut packed = std::vec![0u16; SRC * SRC * 4];
for (i, px) in packed.chunks_exact_mut(4).enumerate() {
let odd = (i % SRC) % 2 == 1;
px[0] = if odd { 0 } else { 0xFFFF }; px[1] = if odd { 0xFFFF } else { 0 }; px[2] = 32768; px[3] = 32768; }
let frame = ayuv64_frame(&packed);
let mut luma = std::vec![0u8; OUT * OUT];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let y_binned = block_mean_native_y(&packed);
assert!(y_binned.iter().all(|&y| y == 32768), "native-Y bin sanity");
assert_eq!(luma_u16, y_binned, "premult luma_u16 == native-Y bin");
let luma_ref: Vec<u8> = y_binned.iter().map(|&p| (p >> 8) as u8).collect();
assert_eq!(luma, luma_ref, "premult luma == native-Y >> 8");
let mut pm16 = direct_rgba_u16(&packed, FR);
premultiply(&mut pm16, MAX16);
let color_oracle = unpremultiply(&block_mean_rgba_u16(&pm16), MAX16);
let color_r: Vec<u16> = color_oracle.chunks_exact(4).map(|px| px[0]).collect();
assert!(
color_r.iter().all(|&r| r < 1000),
"fixture failed to exercise divergence: {color_r:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_limited_range_luma_is_native_y() {
let packed = packed_frame(0xCAFE);
let frame = ayuv64_frame(&packed);
let render = |full_range: bool| {
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to(&frame, full_range, sink.set_kernel_matrix(M)).unwrap();
luma_u16
};
let lim = render(FR_LIMITED);
let full = render(FR);
assert_eq!(
lim,
block_mean_native_y(&packed),
"limited-range luma_u16 == native-Y bin"
);
assert_eq!(lim, full, "native-Y luma_u16 must be range-independent");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_straight_and_premult_differ_under_varying_alpha() {
let mut packed = packed_frame(0x77AA);
for (i, px) in packed.chunks_exact_mut(4).enumerate() {
px[0] = (i as u16).wrapping_mul(900).wrapping_add(1000); }
let render = |mode: AlphaMode| {
let frame = ayuv64_frame(&packed);
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(mode)
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
rgba_u16
};
assert_ne!(
render(AlphaMode::Straight),
render(AlphaMode::Premultiplied),
"alpha mode had no effect"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_le_be_resample_byte_identical() {
let logical: Vec<u16> = (0..SRC * SRC * 4)
.map(|i| match i % 4 {
0 => 0xABCDu16, 1 => 0x8000u16, 2 => 0x4000u16, _ => 0xC000u16, })
.collect();
let pix_le: Vec<u16> = logical.iter().map(|&v| super::as_le_u16(v)).collect();
let pix_be: Vec<u16> = logical.iter().map(|&v| super::as_be_u16(v)).collect();
let render_le = || {
let frame = Ayuv64LeFrame::try_new(&pix_le, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
(rgba, rgba_u16, luma_u16)
};
let render_be = || {
let frame = Ayuv64BeFrame::try_new(&pix_be, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut sink = MixedSinker::<Ayuv64<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to_endian(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
(rgba, rgba_u16, luma_u16)
};
assert_eq!(
render_le(),
render_be(),
"AYUV64 LE/BE resample outputs diverge"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_identity_plan_matches_direct() {
let packed = packed_frame(0x0F0F);
let mut rgba_u16 = std::vec![0u16; SRC * SRC * 4];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&ayuv64_frame(&packed), FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(
rgba_u16,
direct_rgba_u16(&packed, FR),
"identity plan == direct"
);
}
#[test]
fn ayuv64_default_alpha_mode_is_straight() {
let sink = MixedSinker::<Ayuv64>::new(SRC, SRC);
assert_eq!(sink.alpha_mode(), AlphaMode::Straight);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_cross_frame_reset_reuses_streams() {
let packed = packed_frame(0x5151);
let frame = ayuv64_frame(&packed);
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba_u16, block_mean_rgba_u16(&direct_rgba_u16(&packed, FR)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_accepts_alpha_mode_change_across_frames() {
let packed = packed_frame(0xB2B2);
let frame = ayuv64_frame(&packed);
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M))
.expect("a fresh frame must accept a different alpha mode");
}
let mut pm16 = direct_rgba_u16(&packed, FR);
premultiply(&mut pm16, MAX16);
let oracle16 = unpremultiply(&block_mean_rgba_u16(&pm16), MAX16);
assert_eq!(rgba_u16, oracle16, "premult frame 2 output");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_mid_frame_alpha_mode_flip_is_rejected() {
let packed = packed_frame(0x33AA);
let row_elems = SRC * 4;
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process(Ayuv64Row::for_tests(&packed[..row_elems], 0, M, FR))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process(Ayuv64Row::for_tests(
&packed[row_elems..2 * row_elems],
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"mid-frame alpha flip not rejected: {err:?}"
);
}
#[test]
fn ayuv64_out_of_sequence_first_row_is_rejected() {
let packed = packed_frame(0x44BB);
let row_elems = SRC * 4;
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
let err = sink
.process(Ayuv64Row::for_tests(
&packed[row_elems..2 * row_elems],
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"out-of-sequence first row not rejected: {err:?}"
);
assert!(
rgba_u16.iter().all(|&p| p == 0),
"rejected row mutated output"
);
}
#[test]
fn ayuv64_no_output_sink_is_a_noop() {
let packed = packed_frame(0x4242);
let frame = ayuv64_frame(&packed);
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn ayuv64_resample_simd_matches_scalar() {
let packed = packed_frame(0x2468);
let run = |simd: bool| {
let frame = ayuv64_frame(&packed);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut sink =
MixedSinker::<Ayuv64, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
ayuv64_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
(rgb, rgb_u16, luma_u16)
};
assert_eq!(run(true), run(false), "Ayuv64 resample SIMD != scalar");
}