use crate::{
KernelMatrix,
resample::{AreaResampler, AreaStream, ResamplePlan},
row::yuv_444_to_hsv_row,
sinker::MixedSinker,
source::{Yuv410p, Yuv411p, Yuv420p, yuv410p_to, yuv411p_to, yuv420p_to},
};
use mediaframe::frame::{Yuv410pFrame, Yuv411pFrame, Yuv420pFrame};
const MATRICES: [KernelMatrix; 3] = [
KernelMatrix::Bt601,
KernelMatrix::Bt709,
KernelMatrix::Bt2020Ncl,
];
fn pat(i: usize, salt: usize) -> u8 {
((i
.wrapping_mul(37)
.wrapping_add(salt.wrapping_mul(101))
.wrapping_add(11))
& 0xFF) as u8
}
fn plane(w: usize, h: usize, salt: usize) -> Vec<u8> {
(0..w * h).map(|i| pat(i, salt)).collect()
}
fn area_bin(plane: &[u8], in_w: usize, in_h: usize, out_w: usize, out_h: usize) -> Vec<u8> {
let (rx, ry) = (in_w / out_w, in_h / out_h);
let denom = (rx * ry) as u32;
let mut out = vec![0u8; out_w * out_h];
for oy in 0..out_h {
for ox in 0..out_w {
let mut s = 0u32;
for dy in 0..ry {
for dx in 0..rx {
s += plane[(oy * ry + dy) * in_w + ox * rx + dx] as u32;
}
}
out[oy * out_w + ox] = ((s + denom / 2) / denom) as u8;
}
}
out
}
#[allow(clippy::too_many_arguments)]
fn yuv_domain_hsv_reference(
y: &[u8],
u: &[u8],
v: &[u8],
src_w: usize,
src_h: usize,
cw: usize,
ch: usize,
out_w: usize,
out_h: usize,
matrix: KernelMatrix,
full_range: bool,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let yb = area_bin(y, src_w, src_h, out_w, out_h);
let ub = area_bin(u, cw, ch, out_w, out_h);
let vb = area_bin(v, cw, ch, out_w, out_h);
let mut h = vec![0u8; out_w * out_h];
let mut s = vec![0u8; out_w * out_h];
let mut val = vec![0u8; out_w * out_h];
for oy in 0..out_h {
yuv_444_to_hsv_row(
&yb[oy * out_w..(oy + 1) * out_w],
&ub[oy * out_w..(oy + 1) * out_w],
&vb[oy * out_w..(oy + 1) * out_w],
&mut h[oy * out_w..(oy + 1) * out_w],
&mut s[oy * out_w..(oy + 1) * out_w],
&mut val[oy * out_w..(oy + 1) * out_w],
out_w,
matrix,
full_range,
true,
);
}
(h, s, val)
}
fn area_resample_plane(plan: &ResamplePlan, src: &[u8], src_w: usize, src_h: usize) -> Vec<u8> {
let (ow, oh) = (plan.out_w(), plan.out_h());
let mut out = vec![0u8; ow * oh];
let mut stream = AreaStream::<u8>::new(plan.h(), plan.v(), src_w, src_h, 1).unwrap();
for sy in 0..src_h {
stream
.feed_row(sy, &src[sy * src_w..(sy + 1) * src_w], true, |oy, row| {
out[oy * ow..(oy + 1) * ow].copy_from_slice(row);
})
.unwrap();
}
out
}
macro_rules! parity_vs_native {
($name:ident, $marker:path, $frame:path, $walker:path, $cw:expr, $ch:expr) => {
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
#[allow(elided_lifetimes_in_paths)]
fn $name() {
let (w, h) = (12usize, 12usize);
let (cw, ch) = ($cw(w, h), $ch(w, h));
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
for &(ow, oh) in &[(6usize, 6usize), (4, 4), (6, 4), (4, 6), (3, 3)] {
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let run = |native: bool| {
let (mut hh, mut ss, mut vv) =
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = <$frame>::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
w,
h,
AreaResampler::to(ow, oh),
)
.unwrap();
sink.set_native(native);
let mut sink = sink.with_hsv(&mut hh, &mut ss, &mut vv).unwrap();
$walker(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
((hh, ss, vv), scratch_len)
};
let (native, native_scratch) = run(true);
let (row, row_scratch) = run(false);
assert_eq!(
native, row,
"HSV must be bit-identical native-vs-rowstage {ow}x{oh} \
fr={full_range} {matrix:?}"
);
assert_eq!(native_scratch, 0, "native HSV-only RGB-free");
assert_eq!(row_scratch, 0, "row-stage HSV-only RGB-free");
}
}
}
}
};
}
parity_vs_native!(
yuv420p_hsv_only_rowstage_matches_native,
Yuv420p,
Yuv420pFrame,
yuv420p_to,
|w: usize, _h| w / 2,
|_w, h: usize| h / 2
);
parity_vs_native!(
yuv422p_hsv_only_rowstage_matches_native,
crate::source::Yuv422p,
mediaframe::frame::Yuv422pFrame,
crate::source::yuv422p_to,
|w: usize, _h| w / 2,
|_w, h: usize| h
);
parity_vs_native!(
yuv444p_hsv_only_rowstage_matches_native,
crate::source::Yuv444p,
mediaframe::frame::Yuv444pFrame,
crate::source::yuv444p_to,
|w: usize, _h| w,
|_w, h: usize| h
);
parity_vs_native!(
yuv440p_hsv_only_rowstage_matches_native,
crate::source::Yuv440p,
mediaframe::frame::Yuv440pFrame,
crate::source::yuv440p_to,
|w: usize, _h| w,
|_w, h: usize| h / 2
);
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_hsv_only_matches_yuv_domain_reference() {
let (w, h) = (16usize, 16usize);
let (cw, ch) = (w / 4, h / 4);
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
let (ow, oh) = (4usize, 4usize);
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv410pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink =
MixedSinker::<Yuv410p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv410p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
let want = yuv_domain_hsv_reference(&yp, &up, &vp, w, h, cw, ch, ow, oh, matrix, full_range);
assert_eq!((hh, ss, vv), want, "410p HSV fr={full_range} {matrix:?}");
assert_eq!(scratch_len, 0, "410p HSV-only RGB-free");
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv411p_hsv_only_matches_yuv_domain_reference() {
let (w, h) = (16usize, 16usize);
let cw = w.div_ceil(4);
let ch = h;
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
let (ow, oh) = (4usize, 4usize);
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv411pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink =
MixedSinker::<Yuv411p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv411p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
let want = yuv_domain_hsv_reference(&yp, &up, &vp, w, h, cw, ch, ow, oh, matrix, full_range);
assert_eq!((hh, ss, vv), want, "411p HSV fr={full_range} {matrix:?}");
assert_eq!(scratch_len, 0, "411p HSV-only RGB-free");
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_hsv_only_vertical_chroma_upsample() {
let (w, h) = (16usize, 16usize);
let (cw, ch) = (w / 4, h / 4);
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
for &(ow, oh) in &[(8usize, 16usize), (4, 16), (16, 16), (8, 12), (12, 16)] {
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv410pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink =
MixedSinker::<Yuv410p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv410p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
let yb = area_resample_plane(&ResamplePlan::area(w, h, ow, oh).unwrap(), &yp, w, h);
let chroma_plan = ResamplePlan::area(cw, h.div_ceil(4), ow, oh).unwrap();
let ub = area_resample_plane(&chroma_plan, &up, cw, ch);
let vb = area_resample_plane(&chroma_plan, &vp, cw, ch);
let (mut rh, mut rs, mut rv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
for oy in 0..oh {
yuv_444_to_hsv_row(
&yb[oy * ow..(oy + 1) * ow],
&ub[oy * ow..(oy + 1) * ow],
&vb[oy * ow..(oy + 1) * ow],
&mut rh[oy * ow..(oy + 1) * ow],
&mut rs[oy * ow..(oy + 1) * ow],
&mut rv[oy * ow..(oy + 1) * ow],
ow,
matrix,
full_range,
true,
);
}
assert_eq!(
(hh, ss, vv),
(rh, rs, rv),
"410p HSV vertical-chroma-upsample {ow}x{oh} fr={full_range} {matrix:?}"
);
assert_eq!(scratch_len, 0, "410p HSV-only RGB-free {ow}x{oh}");
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv410p_hsv_only_odd_height_chroma_weighting() {
let expand = |c: &[u8], cw: usize, w: usize, h: usize| -> Vec<u8> {
(0..w * h)
.map(|i| c[(i / w / 4) * cw + (i % w) / 4])
.collect()
};
for &(w, h) in &[(16usize, 5usize), (16, 6), (16, 7), (8, 5)] {
let (cw, ch) = (w / 4, h.div_ceil(4));
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
let ue = expand(&up, cw, w, h);
let ve = expand(&vp, cw, w, h);
for &(ow, oh) in &[(w, h), (w / 2, h), (w / 2, 2), (4, 1)] {
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let (mut hh, mut ss, mut vv) =
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv410pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink = MixedSinker::<Yuv410p, AreaResampler>::with_resampler(
w,
h,
AreaResampler::to(ow, oh),
)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv410p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
let lp = ResamplePlan::area(w, h, ow, oh).unwrap();
let yb = area_resample_plane(&lp, &yp, w, h);
let ub = area_resample_plane(&lp, &ue, w, h);
let vb = area_resample_plane(&lp, &ve, w, h);
let (mut rh, mut rs, mut rv) =
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
for oy in 0..oh {
yuv_444_to_hsv_row(
&yb[oy * ow..(oy + 1) * ow],
&ub[oy * ow..(oy + 1) * ow],
&vb[oy * ow..(oy + 1) * ow],
&mut rh[oy * ow..(oy + 1) * ow],
&mut rs[oy * ow..(oy + 1) * ow],
&mut rv[oy * ow..(oy + 1) * ow],
ow,
matrix,
full_range,
true,
);
}
assert_eq!(
(hh, ss, vv),
(rh, rs, rv),
"410p HSV odd-height {w}x{h}->{ow}x{oh} fr={full_range} {matrix:?}"
);
assert_eq!(scratch_len, 0, "410p HSV-only RGB-free {w}x{h}->{ow}x{oh}");
}
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yuv411p_hsv_only_odd_width_chroma_weighting() {
let expand = |c: &[u8], cw: usize, w: usize, h: usize| -> Vec<u8> {
(0..w * h).map(|i| c[(i / w) * cw + (i % w) / 4]).collect()
};
for &(w, h) in &[(13usize, 8usize), (14, 8), (15, 8), (5, 4)] {
let (cw, ch) = (w.div_ceil(4), h);
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
let ue = expand(&up, cw, w, h);
let ve = expand(&vp, cw, w, h);
for &(ow, oh) in &[(w, h), (w, h / 2), (3, h), (2, 1)] {
for &full_range in &[false, true] {
for &matrix in &MATRICES {
let (mut hh, mut ss, mut vv) =
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv411pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink = MixedSinker::<Yuv411p, AreaResampler>::with_resampler(
w,
h,
AreaResampler::to(ow, oh),
)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv411p_to(&frame, full_range, sink.set_kernel_matrix(matrix)).unwrap();
sink.rgb_scratch.len()
};
let lp = ResamplePlan::area(w, h, ow, oh).unwrap();
let yb = area_resample_plane(&lp, &yp, w, h);
let ub = area_resample_plane(&lp, &ue, w, h);
let vb = area_resample_plane(&lp, &ve, w, h);
let (mut rh, mut rs, mut rv) =
(vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
for oy in 0..oh {
yuv_444_to_hsv_row(
&yb[oy * ow..(oy + 1) * ow],
&ub[oy * ow..(oy + 1) * ow],
&vb[oy * ow..(oy + 1) * ow],
&mut rh[oy * ow..(oy + 1) * ow],
&mut rs[oy * ow..(oy + 1) * ow],
&mut rv[oy * ow..(oy + 1) * ow],
ow,
matrix,
full_range,
true,
);
}
assert_eq!(
(hh, ss, vv),
(rh, rs, rv),
"411p HSV odd-width {w}x{h}->{ow}x{oh} fr={full_range} {matrix:?}"
);
assert_eq!(scratch_len, 0, "411p HSV-only RGB-free {w}x{h}->{ow}x{oh}");
}
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn luma_plus_hsv_only_rowstage_is_rgb_free_and_matches_native() {
let (w, h) = (12usize, 12usize);
let (cw, ch) = (w / 2, h / 2);
let yp = plane(w, h, 7);
let up = plane(cw, ch, 9);
let vp = plane(cw, ch, 11);
let (ow, oh) = (6usize, 6usize);
let run = |native: bool| {
let mut luma = vec![0u8; ow * oh];
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv420pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap();
sink.set_native(native);
let mut sink = sink
.with_luma(&mut luma)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv420p_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
sink.rgb_scratch.len()
};
(luma, (hh, ss, vv), scratch_len)
};
let (n_luma, n_hsv, n_scratch) = run(true);
let (r_luma, r_hsv, r_scratch) = run(false);
assert_eq!(n_luma, r_luma, "luma bit-identical native-vs-rowstage");
assert_eq!(n_hsv, r_hsv, "hsv bit-identical native-vs-rowstage");
assert_eq!(n_scratch, 0, "native luma+HSV RGB-free");
assert_eq!(r_scratch, 0, "row-stage luma+HSV RGB-free");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgb_plus_hsv_rowstage_still_stages_rgb_scratch() {
let (w, h) = (12usize, 12usize);
let (cw, ch) = (w / 2, h / 2);
let yp = plane(w, h, 1);
let up = plane(cw, ch, 2);
let vp = plane(cw, ch, 3);
let (ow, oh) = (6usize, 6usize);
let mut rgb = vec![0u8; ow * oh * 3];
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let scratch_len = {
let frame = Yuv420pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_rgb(&mut rgb)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv420p_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
sink.rgb_scratch.len()
};
assert_eq!(
scratch_len,
w * 3,
"RGB + HSV row-stage stages the source-width RGB scratch (unchanged)"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn hsv_only_rowstage_reused_sink_resets_between_frames() {
let (w, h) = (12usize, 12usize);
let (cw, ch) = (w / 2, h / 2);
let yp = plane(w, h, 5);
let up = plane(cw, ch, 6);
let vp = plane(cw, ch, 8);
let (ow, oh) = (6usize, 6usize);
let frame = Yuv420pFrame::new(
&yp, &up, &vp, w as u32, h as u32, w as u32, cw as u32, cw as u32,
);
let (mut rh, mut rs, mut rv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
{
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_hsv(&mut rh, &mut rs, &mut rv)
.unwrap();
yuv420p_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
}
let (mut hh, mut ss, mut vv) = (vec![0u8; ow * oh], vec![0u8; ow * oh], vec![0u8; ow * oh]);
let mut sink =
MixedSinker::<Yuv420p, AreaResampler>::with_resampler(w, h, AreaResampler::to(ow, oh))
.unwrap()
.with_native(false)
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
yuv420p_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
yuv420p_to(&frame, true, sink.set_kernel_matrix(KernelMatrix::Bt601)).unwrap();
drop(sink);
assert_eq!(
(hh, ss, vv),
(rh, rs, rv),
"reused-sink second frame matches a fresh sink (reset worked)"
);
}