use crate::{
ChromaLocation, KernelMatrix, PixelSink,
frame::*,
resample::{AreaResampler, FilteredResampler, Triangle},
sinker::{MixedSinker, MixedSinkerError},
source::*,
};
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;
fn rdhu(a: u64, d: u64) -> u64 {
let q = a / d;
let r = a % d;
q + u64::from(r >= d - d / 2)
}
fn area_weights(src: usize, out: usize) -> Vec<(usize, Vec<u64>)> {
let (src64, out64) = (src as u64, out as u64);
(0..out)
.map(|o| {
let lo = o as u64 * src64;
let hi = lo + src64;
let start = (lo / out64) as usize;
let mut w = Vec::new();
let mut i = start as u64;
loop {
let clo = i * out64;
if clo >= hi {
break;
}
let chi = clo + out64;
let ov = chi.min(hi) - clo.max(lo);
if ov == 0 {
break;
}
w.push(ov);
if chi >= hi {
break;
}
i += 1;
}
(start, w)
})
.collect()
}
fn bin_cosited_u16(plane: &[u16], sw: usize, sh: usize, ow: usize, oh: usize) -> Vec<u16> {
let hw = area_weights(sw, ow);
let vw = area_weights(sh, oh);
let denom = (sw * sh) as u64;
let mut out = vec![0u16; ow * oh];
for (oy, (vs, vwin)) in vw.iter().enumerate() {
for (ox, (hs, hwin)) in hw.iter().enumerate() {
let mut s = 0u64;
for (dy, &vwt) in vwin.iter().enumerate() {
let mut hsum = 0u64;
for (dx, &hwt) in hwin.iter().enumerate() {
hsum += hwt * u64::from(plane[(vs + dy) * sw + hs + dx]);
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
fn bin_chroma_centered_u16(c: &[u16], cw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u16> {
let full = 2 * cw;
let mut r4 = vec![0u64; full * ch];
for r in 0..ch {
let row = &c[r * cw..r * cw + cw];
for j in 0..cw {
let l = u64::from(row[j.saturating_sub(1)]);
let m = u64::from(row[j]);
let rt = u64::from(row[if j + 1 < cw { j + 1 } else { j }]);
r4[r * full + 2 * j] = l + 3 * m;
r4[r * full + 2 * j + 1] = 3 * m + rt;
}
}
let hw = area_weights(full, ow);
let vw = area_weights(ch, oh);
let denom = (4 * full * ch) as u64;
let mut out = vec![0u16; ow * oh];
for (oy, (vs, vwin)) in vw.iter().enumerate() {
for (ox, (hs, hwin)) in hw.iter().enumerate() {
let mut s = 0u64;
for (dy, &vwt) in vwin.iter().enumerate() {
let mut hsum = 0u64;
for (dx, &hwt) in hwin.iter().enumerate() {
hsum += hwt * r4[(vs + dy) * full + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
fn recon_full_row_u16(c: &[u16], cw: usize) -> Vec<u16> {
let mut out = vec![0u16; 2 * cw];
for j in 0..cw {
let l = u32::from(c[j.saturating_sub(1)]);
let m = u32::from(c[j]);
let r = u32::from(c[if j + 1 < cw { j + 1 } else { j }]);
out[2 * j] = ((l + 3 * m + 2) >> 2) as u16;
out[2 * j + 1] = ((3 * m + r + 2) >> 2) as u16;
}
out
}
fn bin_chroma_top_u16(c: &[u16], cw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u16> {
let full = 2 * cw;
let sh = 2 * ch;
let mut r8 = vec![0u64; full * sh];
for r in 0..sh {
let cr = r / 2;
let next = if cr + 1 < ch { cr + 1 } else { cr }; let vrow: Vec<u64> = (0..cw)
.map(|j| {
if r & 1 == 0 {
2 * u64::from(c[cr * cw + j]) } else {
u64::from(c[cr * cw + j]) + u64::from(c[next * cw + j]) }
})
.collect();
for j in 0..cw {
let l = vrow[j.saturating_sub(1)];
let m = vrow[j];
let rt = vrow[if j + 1 < cw { j + 1 } else { j }];
r8[r * full + 2 * j] = l + 3 * m;
r8[r * full + 2 * j + 1] = 3 * m + rt;
}
}
let hw = area_weights(full, ow);
let vw = area_weights(sh, oh);
let denom = (8 * full * sh) as u64;
let mut out = vec![0u16; ow * oh];
for (oy, (vs, vwin)) in vw.iter().enumerate() {
for (ox, (hs, hwin)) in hw.iter().enumerate() {
let mut s = 0u64;
for (dy, &vwt) in vwin.iter().enumerate() {
let mut hsum = 0u64;
for (dx, &hwt) in hwin.iter().enumerate() {
hsum += hwt * r8[(vs + dy) * full + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
fn recon_full_chroma_top_u16(u: &[u16], v: &[u16], sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>) {
let cw = sw / 2;
let ch = sh.div_ceil(2);
let vblend = |plane: &[u16], a: usize, b: usize| -> Vec<u16> {
(0..cw)
.map(|c| {
let x = u32::from(plane[a * cw + c]);
let z = u32::from(plane[b * cw + c]);
((x + z + 1) >> 1) as u16
})
.collect::<Vec<u16>>()
};
let mut uf = vec![0u16; sw * sh];
let mut vf = vec![0u16; sw * sh];
for r in 0..sh {
let cr = r / 2;
let (uh, vh) = if r & 1 == 0 {
(
u[cr * cw..cr * cw + cw].to_vec(),
v[cr * cw..cr * cw + cw].to_vec(),
)
} else {
let next = if cr + 1 < ch { cr + 1 } else { cr };
(vblend(u, cr, next), vblend(v, cr, next))
};
uf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&uh, cw));
vf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&vh, cw));
}
(uf, vf)
}
fn as_be(host: &[u16]) -> Vec<u16> {
host.iter().map(|v| v.to_be()).collect()
}
const GEOMS: [(usize, usize, usize, usize); 4] =
[(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 4, 4), (16, 8, 6, 5)];
macro_rules! hibit_420_resample_siting {
(
$mod:ident, $bits:expr,
$M420:ident, $F420:ident, $w420:ident,
$M420be:ty, $F420be:ident, $w420be:ident,
$M444:ident, $F444:ident, $w444:ident,
$Row:ident
) => {
mod $mod {
use super::*;
const MASK: u16 = ((1u32 << $bits) - 1) as u16;
const MID: u16 = 1u16 << ($bits - 1);
fn ramp(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let step = (MASK as u32 / 16).max(1);
let mut y = vec![0u16; sw * sh];
let mut u = vec![0u16; cw * ch];
let mut v = vec![0u16; cw * ch];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 37) & MASK as u32) as u16;
}
for r in 0..ch {
for c in 0..cw {
u[r * cw + c] = (step * c as u32 + step + r as u32 * 5).min(MASK as u32) as u16;
v[r * cw + c] = (MASK as u32).saturating_sub(step * c as u32).max(step) as u16;
}
}
(y, u, v)
}
fn flat(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let mut y = vec![0u16; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 29) & MASK as u32) as u16;
}
(y, vec![MID; cw * ch], vec![MID; cw * ch])
}
#[allow(clippy::too_many_arguments)]
fn run(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
native: bool,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink =
MixedSinker::<$M420, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(native)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F420::new(
y, u, v, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
);
$w420(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[allow(clippy::too_many_arguments)]
fn run_filter(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M420, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F420::new(
y, u, v, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
);
$w420(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[allow(clippy::too_many_arguments)]
fn run_be(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
native: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let (yb, ub, vb) = (as_be(y), as_be(u), as_be(v));
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M420be, AreaResampler>::with_resampler(
sw,
sh,
AreaResampler::to(ow, oh),
)
.unwrap()
.with_native(native)
.with_chroma_location(loc.clone())
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F420be::try_new(
&yb, &ub, &vb, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
)
.unwrap();
$w420be(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[allow(clippy::too_many_arguments)]
fn native_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_centered_u16(u, cw, ch, ow, oh);
let vb = bin_chroma_centered_u16(v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M444>::new(ow, oh)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F444::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[allow(clippy::too_many_arguments)]
fn rgb_domain_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
filter: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let mut uf = vec![0u16; sw * sh];
let mut vf = vec![0u16; sw * sh];
for r in 0..sh {
let cr = r / 2;
uf[r * sw..r * sw + sw]
.copy_from_slice(&recon_full_row_u16(&u[cr * cw..cr * cw + cw], cw));
vf[r * sw..r * sw + sw]
.copy_from_slice(&recon_full_row_u16(&v[cr * cw..cr * cw + cw], cw));
}
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
let f = $F444::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
} else {
let mut sink =
MixedSinker::<$M444, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn cosited_group_is_byte_identical_across_tiers() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(sw, sh);
for native in [true, false] {
let base = run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Unspecified,
native,
true,
);
for loc in [ChromaLocation::Left, ChromaLocation::other("unassigned-7")] {
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, loc.clone(), native, true),
base,
"co-sited {loc:?} must keep the byte-identical decode \
(native={native}, {sw}x{sh}->{ow}x{oh})"
);
}
}
let base = run_filter(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Unspecified,
true,
);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Left, true),
base,
"co-sited filter must stay byte-identical ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(sw, sh);
let want = native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
for loc in [ChromaLocation::Center] {
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, loc.clone(), true, true),
want,
"centered native {loc:?} must equal the code-domain oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_row_stage_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(sw, sh);
let want = rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, false);
for loc in [ChromaLocation::Center] {
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, loc.clone(), false, true),
want,
"centered row-stage {loc:?} must equal the RGB-domain oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_filter_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(sw, sh);
let want = rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, true);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Center, true),
want,
"centered filter must equal the RGB-domain Triangle oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_simd_matches_scalar() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true),
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::Center,
native,
false
),
"centered SIMD vs scalar must agree (native={native})"
);
}
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, true),
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, false),
"centered filter SIMD vs scalar must agree"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_differs_from_cosited_on_a_chroma_ramp() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
let cos = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true);
let cen = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true);
assert_ne!(
cos, cen,
"centered must differ from co-sited on a chroma ramp (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_equals_cosited_on_flat_chroma() {
let (y, u, v) = flat(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true),
"centered must equal co-sited on flat chroma (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn be_centered_matches_le() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
assert_eq!(
run_be(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true),
"BE centered decode must equal LE for the same logical planes (native={native})"
);
}
}
fn flip_row1<R>(
mut sink: MixedSinker<'_, $M420, R>,
y: &[u16],
u: &[u16],
v: &[u16],
loc1: ChromaLocation,
loc2: ChromaLocation,
) -> Result<(), MixedSinkerError> {
sink.set_chroma_location(loc1.clone());
PixelSink::begin_frame(&mut sink, 8, 8).unwrap();
let row0 = $Row::for_tests(&y[0..8], &u[0..4], &v[0..4], 0, M, FR);
PixelSink::process(&mut sink, row0).unwrap();
sink.set_chroma_location(loc2.clone());
let row1 = $Row::for_tests(&y[8..16], &u[0..4], &v[0..4], 1, M, FR);
PixelSink::process(&mut sink, row1)
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn mid_frame_siting_change_rejected() {
let (y, u, v) = ramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Center, ChromaLocation::Left),
(ChromaLocation::Left, ChromaLocation::Center),
] {
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink =
MixedSinker::<$M420, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"native {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink =
MixedSinker::<$M420, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_rgb(&mut rgb)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"row-stage {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink = MixedSinker::<$M420, FilteredResampler<Triangle>>::with_resampler(
8,
8,
FilteredResampler::new(4, 4, Triangle),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"filter {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
}
}
fn vramp(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let step = (MASK as u32 / 8).max(1);
let y = vec![MID; sw * sh];
let mut u = vec![0u16; cw * ch];
let mut v = vec![0u16; cw * ch];
for r in 0..ch {
for c in 0..cw {
u[r * cw + c] = (step + r as u32 * step).min(MASK as u32) as u16;
v[r * cw + c] = (MASK as u32).saturating_sub(r as u32 * step).max(step) as u16;
}
}
(y, u, v)
}
fn bin_chroma_bottom_u16(c: &[u16], cw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u16> {
let full = 2 * cw;
let sh = 2 * ch;
let mut r8 = vec![0u64; full * sh];
for r in 0..sh {
let cr = r / 2;
let prev = cr.saturating_sub(1);
let vrow: Vec<u32> = (0..cw)
.map(|j| {
if r & 1 == 0 {
u32::from(c[prev * cw + j]) + u32::from(c[cr * cw + j]) } else {
2 * u32::from(c[cr * cw + j]) }
})
.collect();
for j in 0..cw {
let l = vrow[j.saturating_sub(1)];
let m = vrow[j];
let rt = vrow[if j + 1 < cw { j + 1 } else { j }];
r8[r * full + 2 * j] = u64::from(l + 3 * m);
r8[r * full + 2 * j + 1] = u64::from(3 * m + rt);
}
}
let hw = area_weights(full, ow);
let vw = area_weights(sh, oh);
let denom = (8 * full * sh) as u64; let mut out = vec![0u16; ow * oh];
for (oy, (vs, vwin)) in vw.iter().enumerate() {
for (ox, (hs, hwin)) in hw.iter().enumerate() {
let mut s = 0u64;
for (dy, &vwt) in vwin.iter().enumerate() {
let mut hsum = 0u64;
for (dx, &hwt) in hwin.iter().enumerate() {
hsum += hwt * r8[(vs + dy) * full + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
#[allow(clippy::too_many_arguments)]
fn bottom_native_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_bottom_u16(u, cw, ch, ow, oh);
let vb = bin_chroma_bottom_u16(v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M444>::new(ow, oh)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F444::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn recon_full_bottom_u16(u: &[u16], v: &[u16], sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>) {
let cw = sw / 2;
let vblend = |plane: &[u16], cr: usize, prev: usize| -> Vec<u16> {
(0..cw)
.map(|c| {
let a = u32::from(plane[prev * cw + c]);
let b = u32::from(plane[cr * cw + c]);
((a + b + 1) >> 1) as u16
})
.collect::<Vec<u16>>()
};
let mut uf = vec![0u16; sw * sh];
let mut vf = vec![0u16; sw * sh];
for r in 0..sh {
let cr = r / 2;
let (uh, vh) = if r & 1 == 0 {
let prev = cr.saturating_sub(1);
(vblend(u, cr, prev), vblend(v, cr, prev))
} else {
(
u[cr * cw..cr * cw + cw].to_vec(),
v[cr * cw..cr * cw + cw].to_vec(),
)
};
uf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&uh, cw));
vf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&vh, cw));
}
(uf, vf)
}
#[allow(clippy::too_many_arguments)]
fn bottom_rgb_domain_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
filter: bool,
) -> (Vec<u8>, Vec<u16>) {
let (uf, vf) = recon_full_bottom_u16(u, v, sw, sh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
let f = $F444::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
} else {
let mut sink =
MixedSinker::<$M444, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn direct_bottom(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let mut rgb = vec![0u8; sw * sh * 3];
let mut rgb16 = vec![0u16; sw * sh * 3];
{
let mut sink = MixedSinker::<$M420>::new(sw, sh)
.with_chroma_location(ChromaLocation::Bottom)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F420::new(
y, u, v, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
);
$w420(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn bin_chroma_bottomleft_u16(
c: &[u16],
cw: usize,
ch: usize,
ow: usize,
oh: usize,
) -> Vec<u16> {
let sh = 2 * ch;
let mut r2 = vec![0u64; cw * sh];
for r in 0..sh {
let cr = r / 2;
let prev = cr.saturating_sub(1);
for j in 0..cw {
r2[r * cw + j] = if r & 1 == 0 {
u64::from(c[prev * cw + j]) + u64::from(c[cr * cw + j]) } else {
2 * u64::from(c[cr * cw + j]) };
}
}
let hw = area_weights(cw, ow);
let vw = area_weights(sh, oh);
let denom = (2 * cw * sh) as u64; let mut out = vec![0u16; ow * oh];
for (oy, (vs, vwin)) in vw.iter().enumerate() {
for (ox, (hs, hwin)) in hw.iter().enumerate() {
let mut s = 0u64;
for (dy, &vwt) in vwin.iter().enumerate() {
let mut hsum = 0u64;
for (dx, &hwt) in hwin.iter().enumerate() {
hsum += hwt * r2[(vs + dy) * cw + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
#[allow(clippy::too_many_arguments)]
fn bottomleft_native_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_bottomleft_u16(u, cw, ch, ow, oh);
let vb = bin_chroma_bottomleft_u16(v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M444>::new(ow, oh)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F444::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn recon_full_bottomleft_u16(
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
) -> (Vec<u16>, Vec<u16>) {
let cw = sw / 2;
let vblend = |plane: &[u16], cr: usize, prev: usize| -> Vec<u16> {
(0..cw)
.map(|c| {
let a = u32::from(plane[prev * cw + c]);
let b = u32::from(plane[cr * cw + c]);
((a + b + 1) >> 1) as u16
})
.collect::<Vec<u16>>()
};
let replicate = |half: &[u16]| -> Vec<u16> {
let mut out = vec![0u16; 2 * cw];
for j in 0..cw {
out[2 * j] = half[j];
out[2 * j + 1] = half[j];
}
out
};
let mut uf = vec![0u16; sw * sh];
let mut vf = vec![0u16; sw * sh];
for r in 0..sh {
let cr = r / 2;
let (uh, vh) = if r & 1 == 0 {
let prev = cr.saturating_sub(1);
(vblend(u, cr, prev), vblend(v, cr, prev))
} else {
(
u[cr * cw..cr * cw + cw].to_vec(),
v[cr * cw..cr * cw + cw].to_vec(),
)
};
uf[r * sw..r * sw + sw].copy_from_slice(&replicate(&uh));
vf[r * sw..r * sw + sw].copy_from_slice(&replicate(&vh));
}
(uf, vf)
}
#[allow(clippy::too_many_arguments)]
fn bottomleft_rgb_domain_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
filter: bool,
) -> (Vec<u8>, Vec<u16>) {
let (uf, vf) = recon_full_bottomleft_u16(u, v, sw, sh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
let f = $F444::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
} else {
let mut sink =
MixedSinker::<$M444, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottomleft_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = bottomleft_native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
assert_eq!(
run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::BottomLeft,
true,
true
),
o,
"bottom-left native must equal the co-sited V-fold code-domain oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottomleft_row_stage_and_filter_equal_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
assert_eq!(
run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::BottomLeft,
false,
true
),
bottomleft_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, false),
"bottom-left row-stage == RGB reconstruct-then-bin ({sw}x{sh}->{ow}x{oh})"
);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::BottomLeft, true),
bottomleft_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, true),
"bottom-left filter == RGB reconstruct-then-Triangle ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottomleft_be_and_simd_agree_and_fold_the_phase() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let le = run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::BottomLeft,
native,
true,
);
assert_eq!(
run_be(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::BottomLeft, native),
le,
"BE bottom-left must equal LE (native={native})"
);
assert_eq!(
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::BottomLeft,
native,
false
),
le,
"bottom-left SIMD vs scalar must agree (native={native})"
);
assert_ne!(
le,
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true),
"bottom-left must differ from co-sited on a vertical ramp (native={native})"
);
}
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::BottomLeft, true),
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::BottomLeft, false),
"bottom-left filter SIMD vs scalar must agree"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottomleft_differs_from_bottom_on_a_horizontal_ramp() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
assert_ne!(
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::BottomLeft,
native,
true
),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true),
"bottom-left must differ from bottom on a horizontal ramp (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = bottom_native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
assert_eq!(
run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Bottom,
true,
true
),
o,
"bottom native must equal the V-fold code-domain oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_row_stage_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = bottom_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, false);
assert_eq!(
run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Bottom,
false,
true
),
o,
"bottom row-stage must equal the RGB-domain reconstruct-then-bin \
oracle ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_filter_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = bottom_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, true);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Bottom, true),
o,
"bottom filter must equal the RGB-domain Triangle oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_simd_matches_scalar() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true),
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::Bottom,
native,
false
),
"bottom SIMD vs scalar must agree (native={native})"
);
}
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, true),
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, false),
"bottom filter SIMD vs scalar must agree"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_be_matches_le() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
assert_eq!(
run_be(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true),
"BE bottom decode must equal LE (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_differs_from_center_on_a_vertical_chroma_ramp() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let cen = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true);
let bot = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true);
assert_ne!(
cen, bot,
"bottom must differ from center on a vertical chroma ramp (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_equals_cosited_on_flat_chroma() {
let (y, u, v) = flat(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true),
"bottom must equal co-sited on flat chroma (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_identity_resample_matches_direct_decode() {
let (y, u, v) = vramp(8, 8);
assert_eq!(
run(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Bottom, false, true),
direct_bottom(&y, &u, &v, 8, 8, true),
"identity resample bottom == direct decode"
);
}
#[allow(clippy::too_many_arguments)]
fn top_native_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let (cw, ch) = (sw / 2, sh / 2);
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_top_u16(u, cw, ch, ow, oh);
let vb = bin_chroma_top_u16(v, cw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M444>::new(ow, oh)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F444::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[allow(clippy::too_many_arguments)]
fn top_rgb_domain_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
filter: bool,
) -> (Vec<u8>, Vec<u16>) {
let (uf, vf) = recon_full_chroma_top_u16(u, v, sw, sh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
let f = $F444::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
} else {
let mut sink =
MixedSinker::<$M444, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn direct_top(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let cw = sw / 2;
let mut rgb = vec![0u8; sw * sh * 3];
let mut rgb16 = vec![0u16; sw * sh * 3];
{
let mut sink = MixedSinker::<$M420>::new(sw, sh)
.with_chroma_location(ChromaLocation::Top)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F420::new(
y, u, v, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
);
$w420(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = top_native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true, true),
o,
"top native must equal the V-fold code-domain oracle ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_row_stage_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = top_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, false);
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, false, true),
o,
"top row-stage must equal the RGB-domain reconstruct-then-bin oracle \
({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_filter_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = vramp(sw, sh);
let o = top_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, true);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true),
o,
"top filter must equal the RGB-domain Triangle oracle ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_all_tiers_simd_matches_scalar_and_be_matches_le() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, false),
"top SIMD vs scalar must agree (native={native})"
);
assert_eq!(
run_be(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true),
"BE top decode must equal LE (native={native})"
);
}
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, true),
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, false),
"top filter SIMD vs scalar must agree"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_identity_all_tiers_match_direct_decode() {
let (y, u, v) = vramp(8, 8);
let direct = direct_top(&y, &u, &v, 8, 8, true);
assert_eq!(
run(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Top, true, true),
direct,
"identity native top == direct decode"
);
assert_eq!(
run(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Top, false, true),
direct,
"identity row-stage top == direct decode"
);
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Top, true),
direct,
"identity filter top == direct decode"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_two_row_final_flush_odd_height_matches_direct_decode() {
for sh in [5usize, 7] {
let (sw, cw, ch) = (8usize, 4usize, sh.div_ceil(2));
let y = vec![MID; sw * sh];
let mut u = vec![0u16; cw * ch];
let mut v = vec![0u16; cw * ch];
let step = (MASK as u32 / 8).max(1);
for r in 0..ch {
for c in 0..cw {
u[r * cw + c] = (step + r as u32 * step).min(MASK as u32) as u16;
v[r * cw + c] = (MASK as u32).saturating_sub(r as u32 * step).max(step) as u16;
}
}
let direct = direct_top(&y, &u, &v, sw, sh, true);
assert_eq!(
run(&y, &u, &v, sw, sh, sw, sh, ChromaLocation::Top, false, true),
direct,
"odd-height two-row final flush (row-stage) sh={sh}"
);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, sw, sh, ChromaLocation::Top, true),
direct,
"odd-height two-row final flush (filter) sh={sh}"
);
let center = run(
&y,
&u,
&v,
sw,
sh,
sw,
sh,
ChromaLocation::Center,
false,
true,
);
assert_ne!(
direct.0, center.0,
"top must fold vertically vs center sh={sh}"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_differs_from_bottom_and_center_and_equals_cosited_on_flat() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let top = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true);
let bot = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true);
let cen = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true);
assert_ne!(top, cen, "top must differ from center (native={native})");
assert_ne!(
top, bot,
"top (v=0) must differ from bottom (v=1) (native={native})"
);
}
let (y, u, v) = flat(8, 8);
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Center, native, true),
"top must equal centered-h on flat chroma (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn topleft_differs_from_top_on_a_horizontal_ramp() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
assert_ne!(
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::TopLeft,
native,
true
),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true),
"TopLeft (h=0) must differ from Top (h=0.5) on a horizontal ramp (native={native})"
);
assert_eq!(
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::TopLeft,
native,
true
),
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::TopLeft,
native,
false
),
"topleft SIMD vs scalar must agree (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn bottom_final_even_row_reserve_alloc_failure_retries_with_blend() {
use super::super::MixedSinkerError;
use crate::resample::ResampleError;
let (sw, sh, ow, oh) = (8usize, 7usize, 4usize, 3usize);
let cw = sw / 2;
let ch = sh.div_ceil(2);
let step = (MASK as u32 / 8).max(1);
let y = vec![MID; sw * sh];
let mut u = vec![0u16; cw * ch];
let mut v = vec![0u16; cw * ch];
for r in 0..ch {
for c in 0..cw {
u[r * cw + c] = (step + r as u32 * step).min(MASK as u32) as u16;
v[r * cw + c] = (MASK as u32).saturating_sub(r as u32 * step).max(step) as u16;
}
}
let run_frame = |arm_fail: bool, loc: ChromaLocation| -> Vec<u8> {
let mut rgb = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<$M420, AreaResampler>::with_resampler(
sw,
sh,
AreaResampler::to(ow, oh),
)
.unwrap()
.with_native(false)
.with_chroma_location(loc.clone())
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(sw as u32, sh as u32).unwrap();
let feed = |sink: &mut MixedSinker<'_, $M420, AreaResampler>, r: usize| {
let yr = &y[r * sw..(r + 1) * sw];
let cr = r / 2;
let ur = &u[cr * cw..(cr + 1) * cw];
let vr = &v[cr * cw..(cr + 1) * cw];
sink.process($Row::for_tests(yr, ur, vr, r, M, FR))
};
for r in 0..sh - 1 {
feed(&mut sink, r).unwrap();
}
if arm_fail {
super::super::arm_chroma_prev_alloc_failure();
let err = feed(&mut sink, sh - 1).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"armed final-row chroma_prev reserve must surface AllocationFailed, got {err:?}"
);
}
feed(&mut sink, sh - 1).unwrap();
}
rgb
};
let reference = run_frame(false, ChromaLocation::Bottom);
let retried = run_frame(true, ChromaLocation::Bottom);
assert_eq!(
retried, reference,
"the post-failure retry of the final EVEN row must blend with the predecessor (not clamp)"
);
assert_ne!(
reference,
run_frame(false, ChromaLocation::Center),
"the bottom vertical blend must move the final even row vs co-sited Center"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn mid_frame_center_bottom_flip_rejected() {
let (y, u, v) = vramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Center, ChromaLocation::Bottom),
(ChromaLocation::Bottom, ChromaLocation::Center),
] {
for native in [true, false] {
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink =
MixedSinker::<$M420, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(native)
.with_rgb(&mut rgb)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"native={native} {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
}
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink = MixedSinker::<$M420, FilteredResampler<Triangle>>::with_resampler(
8,
8,
FilteredResampler::new(4, 4, Triangle),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"filter {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
}
}
}
};
}
hibit_420_resample_siting!(
p10,
10,
Yuv420p10,
Yuv420p10Frame,
yuv420p10_to,
Yuv420p10<true>,
Yuv420p10BeFrame,
yuv420p10_to_endian,
Yuv444p10,
Yuv444p10Frame,
yuv444p10_to,
Yuv420p10Row
);
hibit_420_resample_siting!(
p12,
12,
Yuv420p12,
Yuv420p12Frame,
yuv420p12_to,
Yuv420p12<true>,
Yuv420p12BeFrame,
yuv420p12_to_endian,
Yuv444p12,
Yuv444p12Frame,
yuv444p12_to,
Yuv420p12Row
);
hibit_420_resample_siting!(
p16,
16,
Yuv420p16,
Yuv420p16Frame,
yuv420p16_to,
Yuv420p16<true>,
Yuv420p16BeFrame,
yuv420p16_to_endian,
Yuv444p16,
Yuv444p16Frame,
yuv444p16_to,
Yuv420p16Row
);