use crate::{
KernelMatrix, PixelSink,
frame::Rgbf16LeFrame,
resample::{AreaResampler, ResampleError},
sinker::{MixedSinker, MixedSinkerError},
source::{Rgbf16, Rgbf16Row, rgbf16_to},
};
const SRC: usize = 8;
const OUT: usize = 4;
fn as_le_f16(host: &[half::f16]) -> Vec<half::f16> {
host
.iter()
.map(|&v| half::f16::from_bits(v.to_bits().to_le()))
.collect()
}
fn packed_frame_f16() -> Vec<half::f16> {
let mut buf = std::vec![half::f16::ZERO; SRC * SRC * 3];
for (i, px) in buf.chunks_exact_mut(3).enumerate() {
let i = i as i32;
px[0] = half::f16::from_f32((i % 5) as f32);
px[1] = half::f16::from_f32((100 - i) as f32);
px[2] = half::f16::from_f32(-((i % 7) as f32));
}
buf
}
fn prebinned_packed_f16(src: &[half::f16]) -> Vec<half::f16> {
let mut out = std::vec![half::f16::ZERO; OUT * OUT * 3];
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..3 {
let mut acc = 0.0f64;
for dy in 0..2 {
for dx in 0..2 {
acc += src[((oy * 2 + dy) * SRC + ox * 2 + dx) * 3 + c].to_f32() as f64;
}
}
out[(oy * OUT + ox) * 3 + c] = half::f16::from_f32((acc / 4.0) as f32);
}
}
}
out
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_downscale_rgb_f32_is_f16_rounded_area_mean() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let prebinned = prebinned_packed_f16(&host);
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..3 {
let i = (oy * OUT + ox) * 3 + c;
assert_eq!(rgb_f32[i], prebinned[i].to_f32(), "({ox},{oy}) c{c}");
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_downscale_rgb_f32_preserves_hdr_and_negative() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert!(
rgb_f32.iter().any(|&v| v > 1.0),
"binned rgb_f32 lost all HDR (> 1.0) values"
);
assert!(
rgb_f32.iter().any(|&v| v < 0.0),
"binned rgb_f32 clamped away negative values"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_all_outputs_match_direct_conversion_of_prebinned_frame() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut rgb_f16 = std::vec![half::f16::ZERO; OUT * OUT * 3];
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma = std::vec![0u8; OUT * OUT];
let mut luma_u16 = std::vec![0u16; OUT * OUT];
let mut h = std::vec![0u8; OUT * OUT];
let mut s_ = std::vec![0u8; OUT * OUT];
let mut v_ = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap()
.with_rgb_f16(&mut rgb_f16)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap()
.with_hsv(&mut h, &mut s_, &mut v_)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let prebinned = prebinned_packed_f16(&host);
let prebinned_wire = as_le_f16(&prebinned);
let mut ref_rgb = std::vec![0u8; OUT * OUT * 3];
let mut ref_rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut ref_rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut ref_rgb_f16 = std::vec![half::f16::ZERO; OUT * OUT * 3];
let mut ref_rgba = std::vec![0u8; OUT * OUT * 4];
let mut ref_rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut ref_luma = std::vec![0u8; OUT * OUT];
let mut ref_luma_u16 = std::vec![0u16; OUT * OUT];
let mut ref_h = std::vec![0u8; OUT * OUT];
let mut ref_s = std::vec![0u8; OUT * OUT];
let mut ref_v = std::vec![0u8; OUT * OUT];
{
let binned =
Rgbf16LeFrame::try_new(&prebinned_wire, OUT as u32, OUT as u32, (OUT * 3) as u32).unwrap();
let mut sink = MixedSinker::<Rgbf16>::new(OUT, OUT)
.with_rgb(&mut ref_rgb)
.unwrap()
.with_rgb_u16(&mut ref_rgb_u16)
.unwrap()
.with_rgb_f32(&mut ref_rgb_f32)
.unwrap()
.with_rgb_f16(&mut ref_rgb_f16)
.unwrap()
.with_rgba(&mut ref_rgba)
.unwrap()
.with_rgba_u16(&mut ref_rgba_u16)
.unwrap()
.with_luma(&mut ref_luma)
.unwrap()
.with_luma_u16(&mut ref_luma_u16)
.unwrap()
.with_hsv(&mut ref_h, &mut ref_s, &mut ref_v)
.unwrap();
rgbf16_to(&binned, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(rgb, ref_rgb, "rgb");
assert_eq!(rgb_u16, ref_rgb_u16, "rgb_u16");
assert_eq!(rgb_f32, ref_rgb_f32, "rgb_f32 (f16-rounded, full parity)");
assert_eq!(rgb_f16, ref_rgb_f16, "rgb_f16 (full parity)");
assert_eq!(rgba, ref_rgba, "rgba");
assert_eq!(rgba_u16, ref_rgba_u16, "rgba_u16");
assert_eq!(luma, ref_luma, "luma");
assert_eq!(luma_u16, ref_luma_u16, "luma_u16 (narrowed, full parity)");
assert_eq!(h, ref_h, "hsv H");
assert_eq!(s_, ref_s, "hsv S");
assert_eq!(v_, ref_v, "hsv V");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_identity_plan_matches_new_sink() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut direct = std::vec![0.0f32; SRC * SRC * 3];
{
let mut sink = MixedSinker::<Rgbf16>::new(SRC, SRC)
.with_rgb_f32(&mut direct)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let mut via_area = std::vec![0.0f32; SRC * SRC * 3];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
.unwrap()
.with_rgb_f32(&mut via_area)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(direct, via_area, "identity-plan resample == direct sink");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_no_output_sink_is_a_noop() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
assert!(
!sink.rgb_stream_f32_allocated(),
"no-output sink allocated the f32 stream"
);
assert_eq!(
sink.rgb_packed_scratch_f16_capacity(),
0,
"no-output sink grew the packed f16 scratch"
);
assert_eq!(
sink.rgb_scratch_capacity(),
0,
"no-output sink grew the u8 narrow scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_scratch_sizing_is_output_gated() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let src = Rgbf16LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
rgbf16_to(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
assert!(
sink.rgb_packed_scratch_f16_capacity() >= OUT * 3,
"f32 output did not size the packed f16 scratch"
);
assert_eq!(
sink.rgb_scratch_capacity(),
0,
"f32-only sink grew the u8 narrow scratch"
);
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut sink_u16 =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap();
rgbf16_to(&src, true, sink_u16.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
assert_eq!(
sink_u16.rgb_scratch_capacity(),
0,
"u16-only sink grew the u8 narrow scratch"
);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut sink2 =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
rgbf16_to(&src, true, sink2.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
assert!(
sink2.rgb_scratch_capacity() >= OUT * 3,
"u8 output did not size the narrow scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_resample_reuses_stream_across_frames() {
let host1 = packed_frame_f16();
let host2: Vec<half::f16> = host1
.iter()
.map(|&v| half::f16::from_f32(-v.to_f32()))
.collect();
let wire1 = as_le_f16(&host1);
let wire2 = as_le_f16(&host2);
let src1 = Rgbf16LeFrame::try_new(&wire1, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let src2 = Rgbf16LeFrame::try_new(&wire2, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut out = std::vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut out)
.unwrap();
rgbf16_to(&src1, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
rgbf16_to(&src2, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let prebinned2 = prebinned_packed_f16(&host2);
for i in 0..OUT * OUT * 3 {
assert_eq!(out[i], prebinned2[i].to_f32(), "frame2 elem {i}");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_out_of_sequence_first_row_rejected_before_allocation() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let row3 = &wire[3 * SRC * 3..4 * SRC * 3];
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
let err = sink
.process(Rgbf16Row::for_tests(row3, 3, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"expected OutOfSequenceRow, got {err:?}"
);
assert!(
!sink.rgb_stream_f32_allocated(),
"stream allocated for a rejected row"
);
assert_eq!(
sink.rgb_packed_scratch_f16_capacity(),
0,
"packed f16 scratch grown for a rejected row"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_mid_frame_out_of_sequence_rejected() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process(Rgbf16Row::for_tests(
&wire[..SRC * 3],
0,
KernelMatrix::Bt709,
true,
))
.unwrap();
let err = sink
.process(Rgbf16Row::for_tests(
&wire[2 * SRC * 3..3 * SRC * 3],
2,
KernelMatrix::Bt709,
true,
))
.unwrap_err();
assert!(matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_mid_frame_output_change_rejected() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let mut rgb_f32 = std::vec![0.0f32; OUT * OUT * 3];
let mut luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process(Rgbf16Row::for_tests(
&wire[..SRC * 3],
0,
KernelMatrix::Bt709,
true,
))
.unwrap();
sink.set_luma(&mut luma).unwrap();
let err = sink
.process(Rgbf16Row::for_tests(
&wire[SRC * 3..SRC * 6],
1,
KernelMatrix::Bt709,
true,
))
.unwrap_err();
assert!(matches!(err, MixedSinkerError::ResampleOutputsChanged(_)));
}
assert!(luma.iter().all(|&l| l == 0));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgbf16_first_row_emit_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
let host = packed_frame_f16();
let wire = as_le_f16(&host);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Rgbf16, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
crate::sinker::mixed::arm_source_rgb_scratch_failure();
let err = sink
.process(Rgbf16Row::for_tests(
&wire[..SRC * 3],
0,
KernelMatrix::Bt709,
true,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row emit-scratch OOM must surface AllocationFailed, got {err:?}"
);
assert!(
!sink.rgb_stream_f32_allocated(),
"an emit-owned scratch OOM must leave the rgb_stream_f32 field None (no partial commit)"
);
sink.set_luma(&mut luma).unwrap();
for r in 0..SRC {
sink
.process(Rgbf16Row::for_tests(
&wire[r * SRC * 3..(r + 1) * SRC * 3],
r,
KernelMatrix::Bt709,
true,
))
.expect("frame replay after a first-row emit-scratch OOM must succeed");
}
}
assert!(
rgb.iter().any(|&b| b != 0),
"the recovered frame produced real rgb output (emit-scratch OOM was recoverable)"
);
assert!(
luma.iter().any(|&b| b != 0),
"the newly-attached luma output was driven on the accepted retry"
);
}