use crate::{
ChromaLocation, KernelMatrix, PixelSink,
frame::Y2xxFrame,
resample::{AreaResampler, FilteredResampler, Triangle},
sinker::{MixedSinker, MixedSinkerError},
source::{
Y210, Y210Row, Y212, Y212Row, Y216, Y216Row, Yuv422p10, Yuv422p12, Yuv422p16, Yuv444p10,
Yuv444p12, Yuv444p16, y210_to, y210_to_endian, y212_to, y212_to_endian, y216_to,
y216_to_endian, yuv422p10_to, yuv422p12_to, yuv422p16_to, yuv444p10_to, yuv444p12_to,
yuv444p16_to,
},
};
use mediaframe::frame::{
Yuv422p10Frame, Yuv422p12Frame, Yuv422p16Frame, Yuv444p10Frame, Yuv444p12Frame, Yuv444p16Frame,
};
const SRC: usize = 8;
const CW: usize = SRC / 2;
const OUT: usize = 4;
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 as_be(host: &[u16]) -> Vec<u16> {
host.iter().map(|v| v.to_be()).collect()
}
macro_rules! hibit_422_packed_resample_siting {
(
$mod:ident, $bits:expr,
$M:ident, $Row:ident, $w:ident, $wbe:ident,
$M422:ident, $F422:ident, $w422:ident,
$M444:ident, $F444:ident, $w444:ident
) => {
mod $mod {
use super::*;
const MASK: u16 = ((1u32 << $bits) - 1) as u16;
const MID: u16 = 1u16 << ($bits - 1);
const SHIFT: u32 = 16 - $bits;
fn pack(v: u16) -> u16 {
v << SHIFT
}
fn pack_packed(y: &[u16], u: &[u16], v: &[u16]) -> Vec<u16> {
let mut buf = vec![0u16; SRC * 2 * SRC];
for row in 0..SRC {
for cx in 0..CW {
let base = row * 2 * SRC + cx * 4;
buf[base] = pack(y[row * SRC + cx * 2]);
buf[base + 1] = pack(u[row * CW + cx]);
buf[base + 2] = pack(y[row * SRC + cx * 2 + 1]);
buf[base + 3] = pack(v[row * CW + cx]);
}
}
buf
}
fn ramp() -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let step = (MASK as u32 / 16).max(1);
let mut y = vec![0u16; SRC * SRC];
let mut u = vec![0u16; CW * SRC];
let mut v = vec![0u16; CW * SRC];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 37) & MASK as u32) as u16;
}
for r in 0..SRC {
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() -> (Vec<u16>, Vec<u16>, Vec<u16>) {
let mut y = vec![0u16; SRC * SRC];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 29) & MASK as u32) as u16;
}
(y, vec![MID; CW * SRC], vec![MID; CW * SRC])
}
fn run(
y: &[u16],
u: &[u16],
v: &[u16],
loc: ChromaLocation,
native: bool,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let packed = pack_packed(y, u, v);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<$M, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.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 =
Y2xxFrame::<$bits, false>::try_new(&packed, SRC as u32, SRC as u32, (2 * SRC) as u32)
.unwrap();
$w(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn run_filter(
y: &[u16],
u: &[u16],
v: &[u16],
loc: ChromaLocation,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let packed = pack_packed(y, u, v);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$M, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, Triangle),
)
.unwrap()
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f =
Y2xxFrame::<$bits, false>::try_new(&packed, SRC as u32, SRC as u32, (2 * SRC) as u32)
.unwrap();
$w(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn run_be(
y: &[u16],
u: &[u16],
v: &[u16],
loc: ChromaLocation,
native: bool,
) -> (Vec<u8>, Vec<u16>) {
let packed = as_be(&pack_packed(y, u, v));
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$M<true>, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_native(native)
.with_chroma_location(loc.clone())
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f =
Y2xxFrame::<$bits, true>::try_new(&packed, SRC as u32, SRC as u32, (2 * SRC) as u32)
.unwrap();
$wbe::<_, true>(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn run_yuv422p(
y: &[u16],
u: &[u16],
v: &[u16],
loc: ChromaLocation,
native: bool,
simd: bool,
) -> (Vec<u8>, Vec<u16>) {
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$M422, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.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 = $F422::new(
y, u, v, SRC as u32, SRC as u32, SRC as u32, CW as u32, CW as u32,
);
$w422(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn native_oracle(y: &[u16], u: &[u16], v: &[u16], simd: bool) -> (Vec<u8>, Vec<u16>) {
let yb = bin_cosited_u16(y, SRC, SRC, OUT, OUT);
let ub = bin_chroma_centered_u16(u, CW, SRC, OUT, OUT);
let vb = bin_chroma_centered_u16(v, CW, SRC, OUT, OUT);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$M444>::new(OUT, OUT)
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap();
let f = $F444::new(
&yb, &ub, &vb, OUT as u32, OUT as u32, OUT as u32, OUT as u32, OUT as u32,
);
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16)
}
fn rgb_domain_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
simd: bool,
filter: bool,
) -> (Vec<u8>, Vec<u16>) {
let mut uf = vec![0u16; SRC * SRC];
let mut vf = vec![0u16; SRC * SRC];
for r in 0..SRC {
uf[r * SRC..r * SRC + SRC]
.copy_from_slice(&recon_full_row_u16(&u[r * CW..r * CW + CW], CW));
vf[r * SRC..r * SRC + SRC]
.copy_from_slice(&recon_full_row_u16(&v[r * CW..r * CW + CW], CW));
}
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgb16 = vec![0u16; OUT * OUT * 3];
let f = $F444::new(
y, &uf, &vf, SRC as u32, SRC as u32, SRC as u32, SRC as u32, SRC as u32,
);
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, 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(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.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() {
let (y, u, v) = ramp();
for native in [true, false] {
let base = run(&y, &u, &v, ChromaLocation::Unspecified, native, true);
for loc in [
ChromaLocation::Left,
ChromaLocation::TopLeft,
ChromaLocation::BottomLeft,
ChromaLocation::other("unassigned-7"),
] {
assert_eq!(
run(&y, &u, &v, loc.clone(), native, true),
base,
"co-sited siting {loc:?} must keep the byte-identical decode (native={native})"
);
}
}
let base = run_filter(&y, &u, &v, ChromaLocation::Unspecified, true);
assert_eq!(run_filter(&y, &u, &v, ChromaLocation::Left, true), base);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_equals_planar_yuv422p_across_tiers() {
let (y, u, v) = ramp();
for loc in [
ChromaLocation::Center,
ChromaLocation::Top,
ChromaLocation::Bottom,
] {
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, loc.clone(), native, true),
run_yuv422p(&y, &u, &v, loc.clone(), native, true),
"centered packed {loc:?} must equal centered planar Yuv422p (native={native})"
);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_native_equals_code_domain_oracle() {
let (y, u, v) = ramp();
let want = native_oracle(&y, &u, &v, true);
for loc in [
ChromaLocation::Center,
ChromaLocation::Top,
ChromaLocation::Bottom,
] {
assert_eq!(
run(&y, &u, &v, loc.clone(), true, true),
want,
"centered native {loc:?} must equal the code-domain reconstruct-then-bin oracle"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_row_stage_equals_rgb_reconstruct_then_bin() {
let (y, u, v) = ramp();
let want = rgb_domain_oracle(&y, &u, &v, true, false);
for loc in [
ChromaLocation::Center,
ChromaLocation::Top,
ChromaLocation::Bottom,
] {
assert_eq!(
run(&y, &u, &v, loc.clone(), false, true),
want,
"centered row-stage {loc:?} must equal the RGB-domain reconstruct-then-bin oracle"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_filter_equals_rgb_reconstruct_then_bin() {
let (y, u, v) = ramp();
let want = rgb_domain_oracle(&y, &u, &v, true, true);
assert_eq!(
run_filter(&y, &u, &v, ChromaLocation::Center, true),
want,
"centered filter must equal the RGB-domain Triangle reconstruct-then-bin oracle"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_simd_matches_scalar() {
let (y, u, v) = ramp();
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, ChromaLocation::Center, native, true),
run(&y, &u, &v, ChromaLocation::Center, native, false),
"centered SIMD vs scalar must agree (native={native})"
);
}
assert_eq!(
run_filter(&y, &u, &v, ChromaLocation::Center, true),
run_filter(&y, &u, &v, 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();
for native in [true, false] {
let cos = run(&y, &u, &v, ChromaLocation::Left, native, true);
let cen = run(&y, &u, &v, 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();
for native in [true, false] {
assert_eq!(
run(&y, &u, &v, ChromaLocation::Left, native, true),
run(&y, &u, &v, 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();
for native in [true, false] {
assert_eq!(
run_be(&y, &u, &v, ChromaLocation::Center, native),
run(&y, &u, &v, ChromaLocation::Center, native, true),
"BE centered decode must equal LE for the same logical planes (native={native})"
);
}
}
fn flip_row1<R>(
mut sink: MixedSinker<'_, $M, R>,
y: &[u16],
u: &[u16],
v: &[u16],
loc1: ChromaLocation,
loc2: ChromaLocation,
) -> Result<(), MixedSinkerError> {
let packed = pack_packed(y, u, v);
sink.set_chroma_location(loc1.clone());
PixelSink::begin_frame(&mut sink, SRC as u32, SRC as u32).unwrap();
let row0 = $Row::for_tests(&packed[0..2 * SRC], 0, M, FR);
PixelSink::process(&mut sink, row0).unwrap();
sink.set_chroma_location(loc2.clone());
let row1 = $Row::for_tests(&packed[2 * SRC..4 * SRC], 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();
for (loc1, loc2) in [
(ChromaLocation::Center, ChromaLocation::Left),
(ChromaLocation::Left, ChromaLocation::Center),
] {
let mut rgb = vec![0u8; OUT * OUT * 3];
let sink =
MixedSinker::<$M, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.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; OUT * OUT * 3];
let sink =
MixedSinker::<$M, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.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; OUT * OUT * 3];
let sink = MixedSinker::<$M, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, 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_422_packed_resample_siting!(
y210,
10,
Y210,
Y210Row,
y210_to,
y210_to_endian,
Yuv422p10,
Yuv422p10Frame,
yuv422p10_to,
Yuv444p10,
Yuv444p10Frame,
yuv444p10_to
);
hibit_422_packed_resample_siting!(
y212,
12,
Y212,
Y212Row,
y212_to,
y212_to_endian,
Yuv422p12,
Yuv422p12Frame,
yuv422p12_to,
Yuv444p12,
Yuv444p12Frame,
yuv444p12_to
);
hibit_422_packed_resample_siting!(
y216,
16,
Y216,
Y216Row,
y216_to,
y216_to_endian,
Yuv422p16,
Yuv422p16Frame,
yuv422p16_to,
Yuv444p16,
Yuv444p16Frame,
yuv444p16_to
);