use crate::{
ChromaLocation, KernelMatrix, PixelSink,
frame::*,
resample::{AreaResampler, FilteredResampler, Triangle},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::*,
};
const M: KernelMatrix = KernelMatrix::Bt601;
const FR: bool = true;
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()
}
const GEOMS: [(usize, usize, usize, usize); 4] =
[(8, 8, 4, 4), (8, 8, 5, 3), (12, 8, 4, 4), (16, 8, 6, 5)];
type Outs = (
Vec<u8>,
Vec<u8>,
Vec<u16>,
Vec<u16>,
(Vec<u8>, Vec<u8>, Vec<u8>),
Vec<u8>,
Vec<u16>,
);
type YuvOuts = (Vec<u8>, Vec<u16>, (Vec<u8>, Vec<u8>, Vec<u8>), Vec<u8>);
macro_rules! hibit_422_yuva_resample_siting {
(
$mod:ident, $bits:expr,
$M422:ident, $F422le:ident, $w422:ident,
$M422be:ty, $F422be:ident, $w422be:ident, $Row:ident,
$M444:ident, $F444:ident, $w444:ident,
$MYuv:ident, $FYuv:ident, $wYuv: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>, Vec<u16>) {
let cw = sw / 2;
let step = (MASK as u32 / 16).max(1);
let mut y = vec![0u16; sw * sh];
let mut u = vec![0u16; cw * sh];
let mut v = vec![0u16; cw * sh];
let mut a = vec![0u16; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 37) & MASK as u32) as u16;
}
for r in 0..sh {
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;
}
}
for (i, p) in a.iter_mut().enumerate() {
*p = ((MASK as u32 / 5 + i as u32 * 23) & MASK as u32) as u16;
}
(y, u, v, a)
}
fn flat(sw: usize, sh: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>, Vec<u16>) {
let cw = sw / 2;
let mut y = vec![0u16; sw * sh];
let mut a = vec![0u16; sw * sh];
for (i, p) in y.iter_mut().enumerate() {
*p = ((40 + i as u32 * 29) & MASK as u32) as u16;
}
for (i, p) in a.iter_mut().enumerate() {
*p = ((MASK as u32 / 3 + i as u32 * 17) & MASK as u32) as u16;
}
(y, vec![MID; cw * sh], vec![MID; cw * sh], a)
}
fn bufs(ow: usize, oh: usize) -> Outs {
(
vec![0u8; ow * oh * 3],
vec![0u8; ow * oh * 4],
vec![0u16; ow * oh * 3],
vec![0u16; ow * oh * 4],
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]),
vec![0u8; ow * oh],
vec![0u16; ow * oh],
)
}
#[allow(clippy::too_many_arguments)]
fn run(
y: &[u16],
u: &[u16],
v: &[u16],
a: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
simd: bool,
) -> Outs {
let cw = sw / 2;
let (
mut rgb,
mut rgba,
mut rgb16,
mut rgba16,
(mut hh, mut ss, mut vv),
mut luma,
mut luma16,
) = bufs(ow, oh);
{
let mut sink =
MixedSinker::<$M422, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma16)
.unwrap();
let f = $F422le::try_new(
y, u, v, a, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32, sw as u32,
)
.unwrap();
$w422(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, rgb16, rgba16, (hh, ss, vv), luma, luma16)
}
#[allow(clippy::too_many_arguments)]
fn run_filter(
y: &[u16],
u: &[u16],
v: &[u16],
a: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
simd: bool,
) -> Outs {
let cw = sw / 2;
let (
mut rgb,
mut rgba,
mut rgb16,
mut rgba16,
(mut hh, mut ss, mut vv),
mut luma,
mut luma16,
) = bufs(ow, oh);
{
let mut sink = MixedSinker::<$M422, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma16)
.unwrap();
let f = $F422le::try_new(
y, u, v, a, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32, sw as u32,
)
.unwrap();
$w422(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, rgb16, rgba16, (hh, ss, vv), luma, luma16)
}
#[allow(clippy::too_many_arguments)]
fn run_be(
y: &[u16],
u: &[u16],
v: &[u16],
a: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
) -> Outs {
let cw = sw / 2;
let (yb, ub, vb, ab) = (as_be(y), as_be(u), as_be(v), as_be(a));
let (
mut rgb,
mut rgba,
mut rgb16,
mut rgba16,
(mut hh, mut ss, mut vv),
mut luma,
mut luma16,
) = bufs(ow, oh);
{
let mut sink = MixedSinker::<$M422be, AreaResampler>::with_resampler(
sw,
sh,
AreaResampler::to(ow, oh),
)
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_chroma_location(loc.clone())
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma16)
.unwrap();
let f = $F422be::try_new(
&yb, &ub, &vb, &ab, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32, sw as u32,
)
.unwrap();
$w422be(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgba, rgb16, rgba16, (hh, ss, vv), luma, luma16)
}
#[allow(clippy::too_many_arguments)]
fn rgba_oracle(
y: &[u16],
u: &[u16],
v: &[u16],
a: &[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 {
uf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&u[r * cw..r * cw + cw], cw));
vf[r * sw..r * sw + sw].copy_from_slice(&recon_full_row_u16(&v[r * cw..r * cw + cw], cw));
}
let mut rgba = vec![0u8; ow * oh * 4];
let mut rgba16 = vec![0u16; ow * oh * 4];
let f = $F444::try_new(
y, &uf, &vf, a, sw as u32, sh as u32, sw as u32, sw as u32, sw as u32, sw as u32,
)
.unwrap();
if filter {
let mut sink = MixedSinker::<$M444, FilteredResampler<Triangle>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, Triangle),
)
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_simd(simd)
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba16)
.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_alpha_mode(AlphaMode::Straight)
.with_simd(simd)
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba16)
.unwrap();
$w444(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgba, rgba16)
}
#[allow(clippy::too_many_arguments)]
fn run_yuv(
y: &[u16],
u: &[u16],
v: &[u16],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
loc: ChromaLocation,
simd: bool,
) -> YuvOuts {
let cw = sw / 2;
let mut rgb = vec![0u8; ow * oh * 3];
let mut rgb16 = vec![0u16; ow * oh * 3];
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 sink =
MixedSinker::<$MYuv, AreaResampler>::with_resampler(sw, sh, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_chroma_location(loc.clone())
.with_simd(simd)
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap()
.with_luma(&mut luma)
.unwrap();
let f = $FYuv::new(
y, u, v, sw as u32, sh as u32, sw as u32, cw as u32, cw as u32,
);
$wYuv(&f, FR, sink.set_kernel_matrix(M)).unwrap();
}
(rgb, rgb16, (hh, ss, vv), luma)
}
#[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, a) = ramp(sw, sh);
let base = run(
&y,
&u,
&v,
&a,
sw,
sh,
ow,
oh,
ChromaLocation::Unspecified,
true,
);
for loc in [
ChromaLocation::Left,
ChromaLocation::TopLeft,
ChromaLocation::BottomLeft,
ChromaLocation::other("unassigned-7"),
] {
assert_eq!(
run(&y, &u, &v, &a, sw, sh, ow, oh, loc.clone(), true),
base,
"co-sited {loc:?} area must keep the byte-identical decode \
({sw}x{sh}->{ow}x{oh})"
);
}
let fbase = run_filter(
&y,
&u,
&v,
&a,
sw,
sh,
ow,
oh,
ChromaLocation::Unspecified,
true,
);
assert_eq!(
run_filter(&y, &u, &v, &a, sw, sh, ow, oh, ChromaLocation::Left, true),
fbase,
"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_area_rgba_equals_rgb_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v, a) = ramp(sw, sh);
let (want_rgba, want_rgba16) = rgba_oracle(&y, &u, &v, &a, sw, sh, ow, oh, true, false);
for loc in [ChromaLocation::Center, ChromaLocation::Top] {
let got = run(&y, &u, &v, &a, sw, sh, ow, oh, loc.clone(), true);
assert_eq!(
got.1, want_rgba,
"centered area rgba {loc:?} ({sw}x{sh}->{ow}x{oh})"
);
assert_eq!(
got.3, want_rgba16,
"centered area rgba_u16 {loc:?} ({sw}x{sh}->{ow}x{oh})"
);
}
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_filter_rgba_equals_rgb_domain_oracle() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v, a) = ramp(sw, sh);
let (want_rgba, want_rgba16) = rgba_oracle(&y, &u, &v, &a, sw, sh, ow, oh, true, true);
let got = run_filter(&y, &u, &v, &a, sw, sh, ow, oh, ChromaLocation::Center, true);
assert_eq!(
got.1, want_rgba,
"centered filter rgba ({sw}x{sh}->{ow}x{oh})"
);
assert_eq!(
got.3, want_rgba16,
"centered filter rgba_u16 ({sw}x{sh}->{ow}x{oh})"
);
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_chroma_matches_yuv422p_centered() {
for (sw, sh, ow, oh) in GEOMS {
let (y, u, v, a) = ramp(sw, sh);
for loc in [ChromaLocation::Center, ChromaLocation::Top] {
let ya = run(&y, &u, &v, &a, sw, sh, ow, oh, loc.clone(), true);
let yv = run_yuv(&y, &u, &v, sw, sh, ow, oh, loc.clone(), true);
assert_eq!(ya.0, yv.0, "rgb {loc:?} ({sw}x{sh}->{ow}x{oh})");
assert_eq!(ya.2, yv.1, "rgb_u16 {loc:?} ({sw}x{sh}->{ow}x{oh})");
assert_eq!(ya.4, yv.2, "hsv {loc:?} ({sw}x{sh}->{ow}x{oh})");
assert_eq!(ya.5, yv.3, "luma {loc:?} ({sw}x{sh}->{ow}x{oh})");
for px in 0..ow * oh {
assert_eq!(
&ya.1[px * 4..px * 4 + 3],
&ya.0[px * 3..px * 3 + 3],
"rgba colour vs rgb {loc:?} px {px}"
);
assert_eq!(
&ya.3[px * 4..px * 4 + 3],
&ya.2[px * 3..px * 3 + 3],
"rgba_u16 colour vs rgb_u16 {loc:?} px {px}"
);
}
}
}
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn alpha_is_identical_between_cosited_and_centered() {
for is_filter in [false, true] {
let (y, u, v, a) = ramp(8, 8);
let drive = |loc| {
if is_filter {
run_filter(&y, &u, &v, &a, 8, 8, 4, 4, loc, true)
} else {
run(&y, &u, &v, &a, 8, 8, 4, 4, loc, true)
}
};
let cos = drive(ChromaLocation::Left);
let cen = drive(ChromaLocation::Center);
let cos_a: Vec<u8> = cos.1.iter().skip(3).step_by(4).copied().collect();
let cen_a: Vec<u8> = cen.1.iter().skip(3).step_by(4).copied().collect();
assert_eq!(
cen_a, cos_a,
"centered α (u8) must equal co-sited (filter={is_filter})"
);
let cos_a16: Vec<u16> = cos.3.iter().skip(3).step_by(4).copied().collect();
let cen_a16: Vec<u16> = cen.3.iter().skip(3).step_by(4).copied().collect();
assert_eq!(
cen_a16, cos_a16,
"centered α (u16) must equal co-sited (filter={is_filter})"
);
assert_ne!(
cen.0, cos.0,
"centered colour must differ (filter={is_filter})"
);
}
}
#[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, a) = ramp(8, 8);
let cos = run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Left, true);
let cen = run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, true);
assert_ne!(
cos.0, cen.0,
"centered rgb must differ from co-sited on a ramp"
);
assert_ne!(
cos.3, cen.3,
"centered rgba_u16 must differ from co-sited on a ramp"
);
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_equals_cosited_on_flat_chroma() {
let (y, u, v, a) = flat(8, 8);
let cos = run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Left, true);
let cen = run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, true);
assert_eq!(cos.1, cen.1, "flat-chroma rgba");
assert_eq!(cos.3, cen.3, "flat-chroma rgba_u16");
assert_eq!(cos.4, cen.4, "flat-chroma hsv");
}
#[test]
#[cfg_attr(miri, ignore = "SIMD row kernels use intrinsics unsupported by Miri")]
fn centered_simd_matches_scalar() {
let (y, u, v, a) = ramp(8, 8);
assert_eq!(
run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, true),
run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, false),
"centered area SIMD vs scalar must agree"
);
assert_eq!(
run_filter(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, true),
run_filter(&y, &u, &v, &a, 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 be_centered_matches_le() {
let (y, u, v, a) = ramp(8, 8);
assert_eq!(
run_be(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center),
run(&y, &u, &v, &a, 8, 8, 4, 4, ChromaLocation::Center, true),
"BE centered decode must equal LE for the same logical planes"
);
}
fn flip_row1<R>(
mut sink: MixedSinker<'_, $M422, R>,
y: &[u16],
u: &[u16],
v: &[u16],
a: &[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], &a[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[4..8], &v[4..8], &a[8..16], 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, a) = ramp(8, 8);
for (loc1, loc2) in [
(ChromaLocation::Center, ChromaLocation::Left),
(ChromaLocation::Left, ChromaLocation::Center),
] {
let mut rgba = vec![0u8; 4 * 4 * 4];
let sink =
MixedSinker::<$M422, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_rgba(&mut rgba)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, &a, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"area {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgba16 = vec![0u16; 4 * 4 * 4];
let sink =
MixedSinker::<$M422, AreaResampler>::with_resampler(8, 8, AreaResampler::to(4, 4))
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_rgba_u16(&mut rgba16)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, &a, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"u16 area {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
let mut rgba = vec![0u8; 4 * 4 * 4];
let sink = MixedSinker::<$M422, FilteredResampler<Triangle>>::with_resampler(
8,
8,
FilteredResampler::new(4, 4, Triangle),
)
.unwrap()
.with_alpha_mode(AlphaMode::Straight)
.with_rgba(&mut rgba)
.unwrap();
let err = flip_row1(sink, &y, &u, &v, &a, loc1.clone(), loc2.clone()).unwrap_err();
assert!(
matches!(err, MixedSinkerError::ChromaSitingChanged(_)),
"filter {loc1:?}->{loc2:?}: want ChromaSitingChanged, got {err:?}"
);
}
}
}
};
}
hibit_422_yuva_resample_siting!(
p10,
10,
Yuva422p10,
Yuva422p10LeFrame,
yuva422p10_to,
Yuva422p10<true>,
Yuva422p10BeFrame,
yuva422p10_to_endian,
Yuva422p10Row,
Yuva444p10,
Yuva444p10Frame,
yuva444p10_to,
Yuv422p10,
Yuv422p10Frame,
yuv422p10_to
);
hibit_422_yuva_resample_siting!(
p12,
12,
Yuva422p12,
Yuva422p12LeFrame,
yuva422p12_to,
Yuva422p12<true>,
Yuva422p12BeFrame,
yuva422p12_to_endian,
Yuva422p12Row,
Yuva444p12,
Yuva444p12Frame,
yuva444p12_to,
Yuv422p12,
Yuv422p12Frame,
yuv422p12_to
);
hibit_422_yuva_resample_siting!(
p16,
16,
Yuva422p16,
Yuva422p16LeFrame,
yuva422p16_to,
Yuva422p16<true>,
Yuva422p16BeFrame,
yuva422p16_to_endian,
Yuva422p16Row,
Yuva444p16,
Yuva444p16Frame,
yuva444p16_to,
Yuv422p16,
Yuv422p16Frame,
yuv422p16_to
);