use crate::{
ChromaLocation, KernelMatrix, PixelSink,
resample::{AreaResampler, AveragingDomain},
sinker::MixedSinker,
source::{Yuv440p, Yuv440pRow, Yuv444p, yuv440p_to, yuv444p_to},
};
use mediaframe::frame::{Yuv440pFrame, Yuv444pFrame};
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(plane: &[u8], sw: usize, sh: usize, ow: usize, oh: usize) -> Vec<u8> {
let hw = area_weights(sw, ow);
let vw = area_weights(sh, oh);
let denom = (sw * sh) as u64;
let mut out = vec![0u8; 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 u8;
}
}
out
}
fn bin_chroma_bottom(c: &[u8], sw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u8> {
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![0u8; 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 u8;
}
}
out
}
fn recon_full_bottom(u: &[u8], v: &[u8], sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>) {
let vblend = |plane: &[u8], cr: usize, prev: usize| -> Vec<u8> {
(0..sw)
.map(|c| {
let a = u32::from(plane[prev * sw + c]);
let b = u32::from(plane[cr * sw + c]);
((a + b + 1) >> 1) as u8
})
.collect::<Vec<u8>>()
};
let mut uf = vec![0u8; sw * sh];
let mut vf = vec![0u8; 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 ramp(sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let ch = sh / 2;
let mut y = vec![0u8; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = (40 + (i as u32 * 3) % 160) as u8;
}
let mut u = vec![0u8; sw * ch];
let mut v = vec![0u8; sw * ch];
for r in 0..ch {
for c in 0..sw {
u[r * sw + c] = (30 + c * 12 + r * 40).min(240) as u8;
v[r * sw + c] = (230u32.saturating_sub((c * 10 + r * 40) as u32)).max(16) as u8;
}
}
(y, u, v)
}
fn vramp(sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let ch = sh / 2;
let y = vec![128u8; sw * sh];
let mut u = vec![0u8; sw * ch];
let mut v = vec![0u8; sw * ch];
for r in 0..ch {
for c in 0..sw {
u[r * sw + c] = (20 + r * 40).min(240) as u8;
v[r * sw + c] = (220u32.saturating_sub((r * 40) as u32)).max(16) as u8;
}
}
(y, u, v)
}
fn flat_chroma(sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let ch = sh / 2;
let mut y = vec![0u8; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = (40 + (i as u32 * 7) % 170) as u8;
}
(y, vec![110u8; sw * ch], vec![140u8; sw * ch])
}
type Outs = (
Vec<u8>,
Vec<u8>,
(Vec<u8>, Vec<u8>, Vec<u8>),
Vec<u8>,
Vec<u16>,
);
#[allow(clippy::too_many_arguments)]
fn run(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
native: bool,
simd: bool,
) -> Outs {
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgba = vec![0u8; ow * oh * 4];
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let mut luma = vec![0u8; ow * oh];
let mut luma_u16 = vec![0u16; ow * oh];
{
let mut sink =
MixedSinker::<Yuv440p, 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_rgba(&mut rgba)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap();
let f = Yuv440pFrame::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, (hh, ss, vv), luma, luma_u16)
}
#[allow(clippy::too_many_arguments)]
fn bottom_native_oracle(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> Outs {
let ch = sh / 2;
let yb = bin_cosited(y, sw, sh, ow, oh);
let ub = bin_chroma_bottom(u, sw, ch, ow, oh);
let vb = bin_chroma_bottom(v, sw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgba = vec![0u8; ow * oh * 4];
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let mut luma = vec![0u8; ow * oh];
let mut luma_u16 = vec![0u16; ow * oh];
{
let mut sink = MixedSinker::<Yuv444p>::new(ow, oh)
.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_luma_u16(&mut luma_u16)
.unwrap();
let f = Yuv444pFrame::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, (hh, ss, vv), luma, luma_u16)
}
#[allow(clippy::too_many_arguments)]
fn encoded_oracle_rgb_bottom(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> Vec<u8> {
let (uf, vf) = recon_full_bottom(u, v, sw, sh);
let mut rgb = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv444p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv444pFrame::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
}
fn direct_bottom_rgb(y: &[u8], u: &[u8], v: &[u8], sw: usize, sh: usize, simd: bool) -> Vec<u8> {
let mut rgb = vec![0u8; sw * sh * 3];
{
let mut sink = MixedSinker::<Yuv440p>::new(sw, sh)
.with_chroma_location(ChromaLocation::Bottom)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv440pFrame::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn cosited_group_is_byte_identical_across_tiers() {
let (y, u, v) = ramp(8, 8);
for native in [true, false] {
let base = run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::Unspecified,
native,
true,
);
for loc in [
ChromaLocation::Left,
ChromaLocation::Center,
ChromaLocation::other("unassigned-7"),
] {
let got = run(&y, &u, &v, 8, 8, 4, 4, loc.clone(), native, true);
assert_eq!(got.0, base.0, "rgb {loc:?} native={native}");
assert_eq!(got.1, base.1, "rgba {loc:?} native={native}");
assert_eq!(got.2, base.2, "hsv {loc:?} native={native}");
assert_eq!(got.3, base.3, "luma {loc:?} native={native}");
assert_eq!(got.4, base.4, "luma_u16 {loc:?} native={native}");
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched 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);
let bl = run(
&y,
&u,
&v,
8,
8,
4,
4,
ChromaLocation::BottomLeft,
native,
true,
);
assert_eq!(bl.0, bottom.0, "rgb BottomLeft==Bottom native={native}");
assert_eq!(bl.1, bottom.1, "rgba BottomLeft==Bottom native={native}");
assert_eq!(bl.2, bottom.2, "hsv BottomLeft==Bottom native={native}");
assert_eq!(bl.3, bottom.3, "luma BottomLeft==Bottom native={native}");
assert_eq!(
bl.4, bottom.4,
"luma_u16 BottomLeft==Bottom native={native}"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in [(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 4, 4), (16, 8, 6, 5)] {
let (y, u, v) = ramp(sw, sh);
let o = bottom_native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
let n = run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Bottom,
true,
true,
);
assert_eq!(n.0, o.0, "rgb {sw}x{sh}->{ow}x{oh}");
assert_eq!(n.1, o.1, "rgba {sw}x{sh}->{ow}x{oh}");
assert_eq!(n.2, o.2, "hsv {sw}x{sh}->{ow}x{oh}");
assert_eq!(n.3, o.3, "luma {sw}x{sh}->{ow}x{oh}");
assert_eq!(n.4, o.4, "luma_u16 {sw}x{sh}->{ow}x{oh}");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_native_simd_matches_scalar() {
let (y, u, v) = ramp(8, 8);
let s = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, true, false);
let d = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, true, true);
assert_eq!(s.0, d.0, "rgb scalar vs simd");
assert_eq!(s.2, d.2, "hsv scalar vs simd");
assert_eq!(s.3, d.3, "luma scalar vs simd");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_encoded_output_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in [(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 6, 4)] {
let (y, u, v) = ramp(sw, sh);
let oracle = encoded_oracle_rgb_bottom(&y, &u, &v, sw, sh, ow, oh, true);
let got = run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::Bottom,
false,
true,
);
assert_eq!(got.0, oracle, "rgb {sw}x{sh}->{ow}x{oh}");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_filter_equals_reconstruct_then_filter() {
use crate::resample::{FilteredResampler, Triangle};
let (sw, sh, ow, oh) = (8usize, 8usize, 4usize, 4usize);
let (y, u, v) = vramp(sw, sh);
let filter_440 = |loc: ChromaLocation| -> Vec<u8> {
let mut rgb = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<Yuv440p, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(true)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv440pFrame::new(
&y, &u, &v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
};
let got = filter_440(ChromaLocation::Bottom);
let (uf, vf) = recon_full_bottom(&u, &v, sw, sh);
let mut oracle = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<Yuv444p, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(true)
.with_rgb(&mut oracle)
.unwrap();
let f = Yuv444pFrame::new(
&y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(got, oracle, "filter-tier bottom == reconstruct-then-filter");
assert_ne!(
got,
filter_440(ChromaLocation::Left),
"filter-tier bottom must differ from co-sited on a vertical ramp"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_linear_equals_reconstruct_then_linear() {
let (sw, sh, ow, oh) = (8usize, 8usize, 4usize, 4usize);
let (y, u, v) = ramp(sw, sh);
let mut got = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv440p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_averaging_domain(AveragingDomain::Linear)
.with_chroma_location(ChromaLocation::Bottom)
.with_rgb(&mut got)
.unwrap();
let f = Yuv440pFrame::new(
&y, &u, &v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
let (uf, vf) = recon_full_bottom(&u, &v, sw, sh);
let mut oracle = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv444p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_averaging_domain(AveragingDomain::Linear)
.with_rgb(&mut oracle)
.unwrap();
let f = Yuv444pFrame::new(
&y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(got, oracle, "linear-tier bottom == reconstruct-then-linear");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_encoded_identity_matches_direct_decode() {
let (y, u, v) = vramp(8, 8);
let res = run(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Bottom, false, true);
let direct = direct_bottom_rgb(&y, &u, &v, 8, 8, true);
assert_eq!(
res.0, direct,
"identity encoded resample bottom == direct decode"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_differs_from_cosited_on_vertical_ramp() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let cos = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true);
let bot = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true);
assert_ne!(
bot.0, cos.0,
"bottom rgb must differ from co-sited (native={native})"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn bottom_equals_cosited_on_flat_chroma() {
let (y, u, v) = flat_chroma(8, 8);
for native in [true, false] {
let cos = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true);
let bot = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Bottom, native, true);
assert_eq!(bot.0, cos.0, "flat-chroma bottom rgb (native={native})");
assert_eq!(bot.2, cos.2, "flat-chroma bottom hsv (native={native})");
}
}
fn in_sequence_flip_row1<R>(
mut sink: MixedSinker<'_, Yuv440p, R>,
y: &[u8],
u: &[u8],
v: &[u8],
loc1: ChromaLocation,
loc2: ChromaLocation,
) -> Result<(), super::super::MixedSinkerError> {
sink.set_chroma_location(loc1.clone());
PixelSink::begin_frame(&mut sink, 8, 8).unwrap();
let row0 = Yuv440pRow::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 = Yuv440pRow::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-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn in_sequence_mid_frame_phase_change_rejected_across_tiers() {
use super::super::MixedSinkerError;
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::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap();
let err = in_sequence_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::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_rgb(&mut rgb)
.unwrap();
let err = in_sequence_flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"encoded-rgb {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let (mut hh, mut ss, mut vv) = (vec![0u8; 4 * 4], vec![0u8; 4 * 4], vec![0u8; 4 * 4]);
let sink = MixedSinker::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
let err = in_sequence_flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"hsv {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink = MixedSinker::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_averaging_domain(AveragingDomain::Linear)
.with_native(false)
.with_rgb(&mut rgb)
.unwrap();
let err = in_sequence_flip_row1(sink, &y, &u, &v, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"linear {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgb = vec![0u8; 4 * 4 * 3];
let sink = MixedSinker::<
Yuv440p,
crate::resample::FilteredResampler<crate::resample::Triangle>,
>::with_resampler(
8,
8,
crate::resample::FilteredResampler::new(4, 4, crate::resample::Triangle),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
let err = in_sequence_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 run_reuse_native(
y: &[u8],
u: &[u8],
v: &[u8],
loc1: ChromaLocation,
loc2: ChromaLocation,
) -> Vec<u8> {
let mut rgb = vec![0u8; 4 * 4 * 3];
{
let mut sink =
MixedSinker::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(true)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv440pFrame::new(y, u, v, 8, 8, 8, 8, 8);
sink.set_chroma_location(loc1.clone());
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
sink.set_chroma_location(loc2.clone());
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
}
fn run_hsv_only(y: &[u8], u: &[u8], v: &[u8], loc: ChromaLocation) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let (mut hh, mut ss, mut vv) = (vec![0u8; 4 * 4], vec![0u8; 4 * 4], vec![0u8; 4 * 4]);
{
let mut sink =
MixedSinker::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_chroma_location(loc.clone())
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
let f = Yuv440pFrame::new(y, u, v, 8, 8, 8, 8, 8);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(hh, ss, vv)
}
fn run_reuse_hsv(
y: &[u8],
u: &[u8],
v: &[u8],
loc1: ChromaLocation,
loc2: ChromaLocation,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let (mut hh, mut ss, mut vv) = (vec![0u8; 4 * 4], vec![0u8; 4 * 4], vec![0u8; 4 * 4]);
{
let mut sink =
MixedSinker::<Yuv440p, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_native(false)
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
let f = Yuv440pFrame::new(y, u, v, 8, 8, 8, 8, 8);
sink.set_chroma_location(loc1.clone());
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
sink.set_chroma_location(loc2.clone());
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(hh, ss, vv)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched 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 reused = run_reuse_native(&y, &u, &v, loc1.clone(), loc2.clone());
let fresh = run(&y, &u, &v, 8, 8, 4, 4, loc2.clone(), true, true).0;
assert_eq!(
reused, fresh,
"native reuse {loc1:?}->{loc2:?} must rebuild"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn hsv_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 reused = run_reuse_hsv(&y, &u, &v, loc1.clone(), loc2.clone());
let fresh = run_hsv_only(&y, &u, &v, loc2.clone());
assert_eq!(reused, fresh, "hsv reuse {loc1:?}->{loc2:?} must rebuild");
}
}
fn bin_chroma_top(c: &[u8], sw: usize, ch: usize, ow: usize, oh: usize) -> Vec<u8> {
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![0u8; 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 u8;
}
}
out
}
fn recon_full_top(u: &[u8], v: &[u8], sw: usize, sh: usize) -> (Vec<u8>, Vec<u8>) {
let ch = sh.div_ceil(2);
let vblend = |plane: &[u8], a: usize, b: usize| -> Vec<u8> {
(0..sw)
.map(|c| {
let x = u32::from(plane[a * sw + c]);
let y = u32::from(plane[b * sw + c]);
((x + y + 1) >> 1) as u8
})
.collect::<Vec<u8>>()
};
let mut uf = vec![0u8; sw * sh];
let mut vf = vec![0u8; 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)
}
#[allow(clippy::too_many_arguments)]
fn top_native_oracle(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> Outs {
let ch = sh / 2;
let yb = bin_cosited(y, sw, sh, ow, oh);
let ub = bin_chroma_top(u, sw, ch, ow, oh);
let vb = bin_chroma_top(v, sw, ch, ow, oh);
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgba = vec![0u8; ow * oh * 4];
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let mut luma = vec![0u8; ow * oh];
let mut luma_u16 = vec![0u16; ow * oh];
{
let mut sink = MixedSinker::<Yuv444p>::new(ow, oh)
.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_luma_u16(&mut luma_u16)
.unwrap();
let f = Yuv444pFrame::new(
&yb, &ub, &vb, ow as u32, oh as u32, ow as u32, ow as u32, ow as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, (hh, ss, vv), luma, luma_u16)
}
#[allow(clippy::too_many_arguments)]
fn encoded_oracle_rgb_top(
y: &[u8],
u: &[u8],
v: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
simd: bool,
) -> Vec<u8> {
let (uf, vf) = recon_full_top(u, v, sw, sh);
let mut rgb = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv444p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv444pFrame::new(
y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
}
fn direct_top_rgb(y: &[u8], u: &[u8], v: &[u8], sw: usize, sh: usize, simd: bool) -> Vec<u8> {
let mut rgb = vec![0u8; sw * sh * 3];
{
let mut sink = MixedSinker::<Yuv440p>::new(sw, sh)
.with_chroma_location(ChromaLocation::Top)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv440pFrame::new(
y, u, v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_native_equals_code_domain_oracle() {
for (sw, sh, ow, oh) in [(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 6, 4), (8, 8, 8, 8)] {
let (y, u, v) = ramp(sw, sh);
let got = run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, true, true);
let oracle = top_native_oracle(&y, &u, &v, sw, sh, ow, oh, true);
assert_eq!(got.0, oracle.0, "rgb {sw}x{sh}->{ow}x{oh}");
assert_eq!(got.1, oracle.1, "rgba {sw}x{sh}->{ow}x{oh}");
assert_eq!(got.2, oracle.2, "hsv {sw}x{sh}->{ow}x{oh}");
assert_eq!(got.3, oracle.3, "luma {sw}x{sh}->{ow}x{oh}");
assert_eq!(got.4, oracle.4, "luma_u16 {sw}x{sh}->{ow}x{oh}");
let tl = run(
&y,
&u,
&v,
sw,
sh,
ow,
oh,
ChromaLocation::TopLeft,
true,
true,
);
assert_eq!(tl.0, got.0, "TopLeft==Top rgb {sw}x{sh}->{ow}x{oh}");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_native_simd_matches_scalar() {
let (y, u, v) = ramp(8, 8);
let s = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, true, false);
let d = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, true, true);
assert_eq!(
s, d,
"top native must be bit-identical across SIMD and scalar"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_encoded_output_equals_rgb_reconstruct_then_bin() {
for (sw, sh, ow, oh) in [(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 6, 4)] {
let (y, u, v) = ramp(sw, sh);
let oracle = encoded_oracle_rgb_top(&y, &u, &v, sw, sh, ow, oh, true);
let got = run(&y, &u, &v, sw, sh, ow, oh, ChromaLocation::Top, false, true);
assert_eq!(got.0, oracle, "rgb {sw}x{sh}->{ow}x{oh}");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_filter_equals_reconstruct_then_filter() {
use crate::resample::{FilteredResampler, Triangle};
let (sw, sh, ow, oh) = (8usize, 8usize, 4usize, 4usize);
let (y, u, v) = vramp(sw, sh);
let filter_440 = |loc: ChromaLocation| -> Vec<u8> {
let mut rgb = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<Yuv440p, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(true)
.with_rgb(&mut rgb)
.unwrap();
let f = Yuv440pFrame::new(
&y, &u, &v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
rgb
};
let got = filter_440(ChromaLocation::Top);
let (uf, vf) = recon_full_top(&u, &v, sw, sh);
let mut oracle = vec![0u8; ow * oh * 3];
{
let mut sink = MixedSinker::<Yuv444p, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_simd(true)
.with_rgb(&mut oracle)
.unwrap();
let f = Yuv444pFrame::new(
&y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(got, oracle, "filter-tier top == reconstruct-then-filter");
assert_ne!(
got,
filter_440(ChromaLocation::Left),
"filter-tier top must differ from co-sited on a vertical ramp"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_linear_equals_reconstruct_then_linear() {
let (sw, sh, ow, oh) = (8usize, 8usize, 4usize, 4usize);
let (y, u, v) = ramp(sw, sh);
let mut got = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv440p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_averaging_domain(AveragingDomain::Linear)
.with_chroma_location(ChromaLocation::Top)
.with_rgb(&mut got)
.unwrap();
let f = Yuv440pFrame::new(
&y, &u, &v, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
let (uf, vf) = recon_full_top(&u, &v, sw, sh);
let mut oracle = vec![0u8; ow * oh * 3];
{
let mut sink =
MixedSinker::<Yuv444p, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_averaging_domain(AveragingDomain::Linear)
.with_rgb(&mut oracle)
.unwrap();
let f = Yuv444pFrame::new(
&y, &uf, &vf, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32,
);
yuv444p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(got, oracle, "linear-tier top == reconstruct-then-linear");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_encoded_identity_matches_direct_decode() {
let (y, u, v) = vramp(8, 8);
let direct = direct_top_rgb(&y, &u, &v, 8, 8, true);
let row_stage = run(&y, &u, &v, 8, 8, 8, 8, ChromaLocation::Top, false, true);
assert_eq!(
row_stage.0, direct,
"identity encoded resample top == direct decode"
);
use crate::resample::{FilteredResampler, Triangle};
let mut filt = vec![0u8; 8 * 8 * 3];
{
let mut sink = MixedSinker::<Yuv440p, FilteredResampler<Triangle>>::with_resampler(
8,
8,
FilteredResampler::new(8, 8, Triangle),
)
.unwrap()
.with_chroma_location(ChromaLocation::Top)
.with_rgb(&mut filt)
.unwrap();
let f = Yuv440pFrame::new(&y, &u, &v, 8, 8, 8, 8, 8);
yuv440p_to(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(
filt, direct,
"identity filter resample top == direct decode"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_differs_from_bottom_and_cosited_on_vertical_ramp() {
let (y, u, v) = vramp(8, 8);
for native in [true, false] {
let cos = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, 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.0, cos.0,
"top must differ from co-sited (native={native})"
);
assert_ne!(
top.0, bot.0,
"top must differ from bottom (native={native})"
);
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn top_equals_cosited_on_flat_chroma() {
let (y, u, v) = flat_chroma(8, 8);
for native in [true, false] {
let cos = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Left, native, true);
let top = run(&y, &u, &v, 8, 8, 4, 4, ChromaLocation::Top, native, true);
assert_eq!(top.0, cos.0, "flat-chroma top rgb (native={native})");
assert_eq!(top.2, cos.2, "flat-chroma top hsv (native={native})");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn native_join_rebuilds_on_top_siting_change_across_frames() {
let (y, u, v) = vramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Left, ChromaLocation::Top),
(ChromaLocation::Top, ChromaLocation::Left),
(ChromaLocation::Bottom, ChromaLocation::Top),
(ChromaLocation::Top, ChromaLocation::Bottom),
] {
let reused = run_reuse_native(&y, &u, &v, loc1.clone(), loc2.clone());
let fresh = run(&y, &u, &v, 8, 8, 4, 4, loc2.clone(), true, true).0;
assert_eq!(
reused, fresh,
"native reuse {loc1:?}->{loc2:?} must rebuild"
);
}
}