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_bottom_u16(c: &[u16], sw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u16> {
let sh = 2 * ch;
let mut r2 = vec![0u64; sw * sh];
for r in 0..sh {
let cr = r / 2;
let prev = cr.saturating_sub(1);
for j in 0..sw {
r2[r * sw + j] = if r & 1 == 0 {
u64::from(c[prev * sw + j]) + u64::from(c[cr * sw + j]) } else {
2 * u64::from(c[cr * sw + j]) };
}
}
let hw = area_weights(sw, ow);
let vw = area_weights(sh, oh);
let denom = (2 * 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 * r2[(vs + dy) * sw + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
fn recon_full_bottom_u16(u: &[u16], v: &[u16], sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>) {
let vblend = |plane: &[u16], cr: usize, prev: usize| -> Vec<u16> {
let mut out = vec![0u16; sw];
crate::row::scalar::chroma_upsample_440_bottom_v_u16(
&plane[prev * sw..prev * sw + sw],
&plane[cr * sw..cr * sw + sw],
&mut out,
sw,
);
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 * sw..cr * sw + sw].to_vec(),
v[cr * sw..cr * sw + sw].to_vec(),
)
};
uf[r * sw..r * sw + sw].copy_from_slice(&uh);
vf[r * sw..r * sw + sw].copy_from_slice(&vh);
}
(uf, vf)
}
fn bin_chroma_top_u16(c: &[u16], sw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u16> {
let sh = 2 * ch;
let mut r2 = vec![0u64; sw * sh];
for r in 0..sh {
let cr = r / 2;
let next = (cr + 1).min(ch - 1);
for j in 0..sw {
r2[r * sw + j] = if r & 1 == 0 {
2 * u64::from(c[cr * sw + j]) } else {
u64::from(c[cr * sw + j]) + u64::from(c[next * sw + j]) };
}
}
let hw = area_weights(sw, ow);
let vw = area_weights(sh, oh);
let denom = (2 * 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 * r2[(vs + dy) * sw + hs + dx];
}
s += vwt * hsum;
}
out[oy * ow + ox] = rdhu(s, denom) as u16;
}
}
out
}
fn recon_full_top_u16(u: &[u16], v: &[u16], sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>) {
let ch = sh.div_ceil(2);
let vblend = |plane: &[u16], a: usize, b: usize| -> Vec<u16> {
let mut out = vec![0u16; sw];
crate::row::scalar::chroma_upsample_440_bottom_v_u16(
&plane[a * sw..a * sw + sw],
&plane[b * sw..b * sw + sw],
&mut out,
sw,
);
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 {
(
u[cr * sw..cr * sw + sw].to_vec(),
v[cr * sw..cr * sw + sw].to_vec(),
)
} else {
let next = (cr + 1).min(ch - 1);
(vblend(u, cr, next), vblend(v, cr, next))
};
uf[r * sw..r * sw + sw].copy_from_slice(&uh);
vf[r * sw..r * sw + sw].copy_from_slice(&vh);
}
(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_440_resample_siting {
(
$mod:ident, $bits:expr,
$M440:ident, $F440:ident, $w440:ident,
$M440be:ty, $F440be:ident, $w440be: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 ch = sh / 2;
let step = (MASK as u32 / 16).max(1);
let mut y = vec![0u16; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 37) & MASK as u32) as u16;
}
let mut u = vec![0u16; sw * ch];
let mut v = vec![0u16; sw * ch];
for r in 0..ch {
for c in 0..sw {
u[r * sw + c] = (step * c as u32 + step + r as u32 * step * 2).min(MASK as u32) as u16;
v[r * sw + c] = (MASK as u32)
.saturating_sub(step * c as u32 + r as u32 * step * 2)
.max(step) as u16;
}
}
(y, u, v)
}
fn vramp(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let ch = sh / 2;
let step = (MASK as u32 / 8).max(1);
let y = vec![MID; sw * sh];
let mut u = vec![0u16; sw * ch];
let mut v = vec![0u16; sw * ch];
for r in 0..ch {
for c in 0..sw {
u[r * sw + c] = (step + r as u32 * step).min(MASK as u32) as u16;
v[r * sw + c] = (MASK as u32).saturating_sub(r as u32 * step).max(step) as u16;
}
}
(y, u, v)
}
fn flat(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let ch = 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; sw * ch], vec![MID; sw * 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 mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink =
MixedSinker::<$M440, 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 = $F440::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
$w440(&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 mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
{
let mut sink = MixedSinker::<$M440, 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 = $F440::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
$w440(&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 (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::<$M440be, 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 = $F440be::try_new(
&yb, &ub, &vb, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
)
.unwrap();
$w440be(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
#[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 ch = sh / 2;
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_bottom_u16(u, sw, ch, ow, oh);
let vb = bin_chroma_bottom_u16(v, sw, 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 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)
}
#[allow(clippy::type_complexity)]
fn direct_bottom_full(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
loc: ChromaLocation,
simd: bool,
) -> (
Vec<u8>,
Vec<u8>,
(Vec<u8>, Vec<u8>, Vec<u8>),
Vec<u8>,
Vec<u16>,
Vec<u16>,
) {
let mut rgb = vec![0u8; sw * sh * 3];
let mut rgba = vec![0u8; sw * sh * 4];
let (mut hh, mut ss, mut vv) = (vec![0u8; sw * sh], vec![0u8; sw * sh], vec![0u8; sw * sh]);
let mut luma = vec![0u8; sw * sh];
let mut rgb16 = vec![0u16; sw * sh * 3];
let mut rgba16 = vec![0u16; sw * sh * 4];
{
let mut sink = MixedSinker::<$M440>::new(sw, sh)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap();
let f = $F440::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
$w440(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, (hh, ss, vv), luma, rgb16, rgba16)
}
#[allow(clippy::type_complexity)]
fn identity_resample_full(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
loc: ChromaLocation,
simd: bool,
) -> (
Vec<u8>,
Vec<u8>,
(Vec<u8>, Vec<u8>, Vec<u8>),
Vec<u8>,
Vec<u16>,
Vec<u16>,
) {
let mut rgb = vec![0u8; sw * sh * 3];
let mut rgba = vec![0u8; sw * sh * 4];
let (mut hh, mut ss, mut vv) = (vec![0u8; sw * sh], vec![0u8; sw * sh], vec![0u8; sw * sh]);
let mut luma = vec![0u8; sw * sh];
let mut rgb16 = vec![0u16; sw * sh * 3];
let mut rgba16 = vec![0u16; sw * sh * 4];
{
let mut sink =
MixedSinker::<$M440, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(sw, sh))
.unwrap()
.with_native(false)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap();
let f = $F440::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
$w440(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, (hh, ss, vv), luma, rgb16, rgba16)
}
#[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::Center,
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::Center, 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 bottom_left_resamples_as_bottom_across_tiers() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
let bottom = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true);
assert_eq!(
run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::BottomLeft,
native,
true
),
bottom,
"BottomLeft must resample as Bottom (native={native})"
);
}
let bottom = run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, true);
assert_eq!(
run_filter(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::BottomLeft, true),
bottom,
"BottomLeft must filter as Bottom"
);
}
#[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) = ramp(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) = ramp(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_filter() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(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_cosited_on_a_vertical_chroma_ramp() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let cos = 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!(
cos, bot,
"bottom must differ from co-sited 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_all_outputs() {
let (y, u, v) = vramp(8, 8);
for loc in [ChromaLocation::Bottom, ChromaLocation::BottomLeft] {
assert_eq!(
identity_resample_full(&y, &u, &v, 8, 8, loc.clone(), true),
direct_bottom_full(&y, &u, &v, 8, 8, loc.clone(), true),
"identity resample {loc:?} == direct decode (all outputs)"
);
}
}
fn flip_row1<R>(
mut sink: MixedSinker<'_, $M440, 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..8], &v[0..8], 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..8], &v[0..8], 1, M, FR);
PixelSink::process(&mut sink, row1)
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn in_sequence_mid_frame_phase_change_rejected_across_tiers() {
let (y, u, v) = ramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Left, ChromaLocation::Bottom),
(ChromaLocation::Bottom, ChromaLocation::Left),
(ChromaLocation::Center, ChromaLocation::Bottom),
(ChromaLocation::Bottom, ChromaLocation::Center),
(ChromaLocation::Left, ChromaLocation::BottomLeft),
(ChromaLocation::BottomLeft, ChromaLocation::Top),
] {
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink =
MixedSinker::<$M440, 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::<$M440, 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::<$M440, 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:?}"
);
let mut rgb = vec![0u8; 8 * 8 * 3];
let sink = MixedSinker::<$M440>::new(8, 8).with_rgb(&mut rgb).unwrap();
let err = flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"identity {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn native_join_rebuilds_on_siting_change_across_frames() {
let (y, u, v) = vramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Left, ChromaLocation::Bottom),
(ChromaLocation::Bottom, ChromaLocation::Left),
] {
let mut rgb = vec![0u8; 4 * 4 * 3];
let mut rgb16 = vec![0u16; 4 * 4 * 3];
{
let mut sink =
MixedSinker::<$M440, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F440::new(&y, &u, &v, 8, 8, 8, 8, 8);
sink.set_chroma_location(loc1.clone());
$w440(&f, FR, sink.set_kernel_matrix(M)).unwrap();
sink.set_chroma_location(loc2.clone());
$w440(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
let fresh = run(&y, &u, &v, 8, 8, 4, 4, loc2.clone(), true, true);
assert_eq!(
(rgb, rgb16),
fresh,
"native reuse {loc1:?}->{loc2:?} must rebuild the phased join"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn row_stage_bottom_reuse_across_frames_no_stale_blend() {
let (ya, ua, va) = vramp(8, 8);
let (yb, ub, vb) = ramp(8, 8);
let mut rgb = vec![0u8; 4 * 4 * 3];
let mut rgb16 = vec![0u16; 4 * 4 * 3];
{
let mut sink =
MixedSinker::<$M440, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_chroma_location(ChromaLocation::Bottom)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let fa = $F440::new(&ya, &ua, &va, 8, 8, 8, 8, 8);
$w440(&fa, FR, sink.set_kernel_matrix(M)).unwrap();
let fb = $F440::new(&yb, &ub, &vb, 8, 8, 8, 8, 8);
$w440(&fb, FR, sink.set_kernel_matrix(M)).unwrap();
}
let fresh = run(
&yb,
&ub,
&vb,
8,
8,
4,
4,
ChromaLocation::Bottom,
false,
true,
);
assert_eq!(
(rgb, rgb16),
fresh,
"row-stage Bottom sink reused across frames must not carry stale chroma"
);
}
#[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 ch = sh / 2;
let yb = bin_cosited_u16(y, sw, sh, ow, oh);
let ub = bin_chroma_top_u16(u, sw, ch, ow, oh);
let vb = bin_chroma_top_u16(v, sw, 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_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)
}
#[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) = ramp(sw, sh);
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true, true),
top_native_oracle(&y, &u, &v, sw, sh, ow, oh, true),
"native top == code-domain oracle ({sw}x{sh}->{ow}x{oh})"
);
assert_eq!(
run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::TopLeft,
true,
true
),
run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true, true),
"TopLeft == Top ({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) = ramp(sw, sh);
assert_eq!(
run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, false, true).0,
top_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, false).0,
"row-stage top == reconstruct-then-bin ({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_filter() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v) = ramp(sw, sh);
assert_eq!(
run_filter(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true).0,
top_rgb_domain_oracle(&y, &u, &v, sw, sh, ow, oh, true, true).0,
"filter top == reconstruct-then-filter ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_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::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_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_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::Top, native),
run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true),
"BE top decode must equal LE (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_differs_from_cosited_and_bottom_on_a_vertical_chroma_ramp() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let cos = 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);
let top = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true);
assert_ne!(top, cos, "top must differ from co-sited (native={native})");
assert_ne!(top, bot, "top must differ from bottom (native={native})");
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_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::Top, native, true),
"top must equal co-sited on flat chroma (native={native})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn top_identity_resample_matches_direct_decode_all_outputs() {
let (y, u, v) = vramp(8, 8);
for loc in [ChromaLocation::Top, ChromaLocation::TopLeft] {
assert_eq!(
identity_resample_full(&y, &u, &v, 8, 8, loc.clone(), true),
direct_bottom_full(&y, &u, &v, 8, 8, loc.clone(), true),
"identity resample {loc:?} == direct decode (all outputs)"
);
}
}
}
};
}
hibit_440_resample_siting!(
p10,
10,
Yuv440p10,
Yuv440p10Frame,
yuv440p10_to,
Yuv440p10<true>,
Yuv440p10BeFrame,
yuv440p10_to_endian,
Yuv444p10,
Yuv444p10Frame,
yuv444p10_to,
Yuv440p10Row
);
hibit_440_resample_siting!(
p12,
12,
Yuv440p12,
Yuv440p12Frame,
yuv440p12_to,
Yuv440p12<true>,
Yuv440p12BeFrame,
yuv440p12_to_endian,
Yuv444p12,
Yuv444p12Frame,
yuv444p12_to,
Yuv440p12Row
);