use super::super::{
GeometryOverflow, InsufficientBuffer, MixedSinker, MixedSinkerError, RowIndexOutOfRange,
RowShapeMismatch, RowSlice, WidthAlignment, check_dimensions_match,
packed_yuv422_triple_filter_resample, packed_yuv422_triple_resample, reset_high_bit_yuv_streams,
rgb_row_buf_or_scratch, rgba_plane_row_slice, rgba_u16_plane_row_slice,
};
use crate::{PixelSink, row::*, source::*};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use super::super::{
ChromaSitingChanged, FrozenOutputs, HsvFrameMut, NativeRouteChanged,
planar_8bit::YUV422P_CENTERED_H_PHASE, resample_preflight_check_only,
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use super::native::{
NativeYuv420U16, yuv420p16_native_preflight_check_only, yuv420p16_process_native,
};
#[cfg(feature = "yuv-planar")]
use crate::resample::{AveragingDomain, InsertionContext, InsertionPoint, select_insertion_point};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use crate::{
KernelMatrix,
resample::{PlanGeometry, ResampleError, ResamplePlan},
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use super::super::{chroma_420_bottom_sited_v, chroma_420_center_sited_h, chroma_420_top_sited_v};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use super::yuv420p::{reserve_420_chroma_prev_u16, reserve_420_chroma_top_y_u16};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
const HOST_NATIVE_BE: bool = cfg!(target_endian = "big");
#[cfg(all(
test,
feature = "std",
feature = "yuv-semi-planar",
feature = "yuv-planar"
))]
std::thread_local! {
static FORCE_P0XX_ALLOC_FAILURE: core::cell::Cell<bool> =
const { core::cell::Cell::new(false) };
}
#[cfg(all(
test,
feature = "std",
feature = "yuv-semi-planar",
feature = "yuv-planar"
))]
pub(crate) fn arm_p0xx_alloc_failure() {
FORCE_P0XX_ALLOC_FAILURE.with(|f| f.set(true));
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn grow_depack_scratch(
scratch: &mut std::vec::Vec<u16>,
len: usize,
fail: bool,
w: usize,
h: usize,
plan: &ResamplePlan,
) -> Result<(), MixedSinkerError> {
let _ = fail;
if scratch.len() < len {
#[cfg(all(
test,
feature = "std",
feature = "yuv-semi-planar",
feature = "yuv-planar"
))]
if fail && FORCE_P0XX_ALLOC_FAILURE.with(|f| f.take()) {
return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
PlanGeometry::new(w, h, plan.out_w(), plan.out_h()),
)));
}
scratch
.try_reserve_exact(len - scratch.len())
.map_err(|_| {
MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
w,
h,
plan.out_w(),
plan.out_h(),
)))
})?;
scratch.resize(len, 0);
}
Ok(())
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn p0xx_process_native<const BITS: u32, const BE: bool>(
plan: &ResamplePlan,
native_420_u16: &mut Option<std::boxed::Box<NativeYuv420U16>>,
y_scratch: &mut std::vec::Vec<u16>,
u_scratch: &mut std::vec::Vec<u16>,
v_scratch: &mut std::vec::Vec<u16>,
resample_outputs: &mut Option<FrozenOutputs>,
rgb: &mut Option<&mut [u8]>,
rgba: &mut Option<&mut [u8]>,
rgb_u16: &mut Option<&mut [u16]>,
rgba_u16: &mut Option<&mut [u16]>,
luma: &mut Option<&mut [u8]>,
hsv: &mut Option<HsvFrameMut<'_>>,
rgb_scratch: &mut std::vec::Vec<u8>,
rgb_scratch_u16: &mut std::vec::Vec<u16>,
y_row: &[u16],
uv_half: &[u16],
chroma_h_phase: f64,
chroma_v_phase: f64,
chroma_v_top: bool,
matrix: KernelMatrix,
full_range: bool,
idx: usize,
w: usize,
h: usize,
use_simd: bool,
) -> Result<(), MixedSinkerError> {
const {
assert!(
BITS > 8 && BITS <= 16,
"BITS must be in (8, 16] for high-bit semi-planar 4:2:0 P-format"
)
};
let need_luma = luma.is_some();
let need_color =
rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
let cw = w / 2;
if !yuv420p16_native_preflight_check_only(
native_420_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
idx,
need_luma,
need_color,
)? {
return Ok(());
}
grow_depack_scratch(y_scratch, w, true, w, h, plan)?;
let chroma_row = need_color && idx.is_multiple_of(2);
if chroma_row {
grow_depack_scratch(u_scratch, cw, false, w, h, plan)?;
grow_depack_scratch(v_scratch, cw, false, w, h, plan)?;
}
for (d, &s) in y_scratch[..w].iter_mut().zip(y_row.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*d = logical >> (16 - BITS);
}
if chroma_row {
for (i, pair) in uv_half.as_chunks::<2>().0.iter().enumerate() {
let u = if BE {
u16::from_be(pair[0])
} else {
u16::from_le(pair[0])
};
let v = if BE {
u16::from_be(pair[1])
} else {
u16::from_le(pair[1])
};
u_scratch[i] = u >> (16 - BITS);
v_scratch[i] = v >> (16 - BITS);
}
}
let (u_half, v_half): (&[u16], &[u16]) = if chroma_row {
(&u_scratch[..cw], &v_scratch[..cw])
} else {
(&[], &[])
};
yuv420p16_process_native::<BITS, HOST_NATIVE_BE>(
plan,
native_420_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
&y_scratch[..w],
u_half,
v_half,
matrix,
full_range,
idx,
w,
h,
|| {
ResamplePlan::area_chroma_420(
w / 2,
h,
plan.out_w(),
plan.out_h(),
chroma_h_phase,
chroma_v_phase,
chroma_v_top,
)
},
use_simd,
)
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
pub(crate) fn reserve_420_chroma_full_u16(
chroma_full: &mut std::vec::Vec<u16>,
width: usize,
height: usize,
) -> Result<(), MixedSinkerError> {
#[cfg(all(test, feature = "std", feature = "yuv-planar"))]
if super::super::FORCE_CHROMA_FULL_ALLOC_FAILURE.with(|f| f.take()) {
return Err(MixedSinkerError::Resample(ResampleError::AllocationFailed(
PlanGeometry::new(width, height, width, height),
)));
}
let needed = width
.checked_mul(2)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
width, height, 2,
)))?;
if chroma_full.len() < needed {
chroma_full
.try_reserve_exact(needed - chroma_full.len())
.map_err(|_| {
MixedSinkerError::Resample(ResampleError::AllocationFailed(PlanGeometry::new(
width, height, width, height,
)))
})?;
chroma_full.resize(needed, 0);
}
Ok(())
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
pub(crate) fn upsample_420_chroma_center_h_p0xx<'s, const BITS: u32>(
chroma_full: &'s mut [u16],
uv_half: &[u16],
width: usize,
big_endian: bool,
use_simd: bool,
) -> &'s [u16] {
debug_assert!(
chroma_full.len() >= 2 * width,
"chroma_full must be reserved via reserve_420_chroma_full_u16 first"
);
crate::row::chroma_upsample_2to1_center_h_p0xx_row::<BITS, false>(
uv_half,
&mut chroma_full[..2 * width],
width,
big_endian,
use_simd,
);
&chroma_full[..2 * width]
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
pub(crate) fn upsample_420_chroma_cosited_h_p0xx<'s, const BITS: u32>(
chroma_full: &'s mut [u16],
uv_half: &[u16],
width: usize,
big_endian: bool,
) -> &'s [u16] {
debug_assert!(
chroma_full.len() >= 2 * width,
"chroma_full must be reserved via reserve_420_chroma_full_u16 first"
);
crate::row::scalar::chroma_upsample_2to1_cosited_h_p0xx::<BITS, false>(
uv_half,
&mut chroma_full[..2 * width],
width,
big_endian,
);
&chroma_full[..2 * width]
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
pub(crate) fn upsample_420_chroma_center_h_p0xx_low_packed<'s, const BITS: u32>(
chroma_full: &'s mut [u16],
uv_half: &[u16],
width: usize,
big_endian: bool,
use_simd: bool,
) -> &'s [u16] {
debug_assert!(
chroma_full.len() >= 2 * width,
"chroma_full must be reserved via reserve_420_chroma_full_u16 first"
);
crate::row::chroma_upsample_2to1_center_h_p0xx_row::<BITS, true>(
uv_half,
&mut chroma_full[..2 * width],
width,
big_endian,
use_simd,
);
&chroma_full[..2 * width]
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[inline]
pub(crate) fn stage_420_chroma_prev_p0xx(
chroma_prev: &mut [u16],
chroma_prev_row: &mut Option<usize>,
uv_half: &[u16],
idx: usize,
width: usize,
) {
debug_assert!(
chroma_prev.len() >= width,
"chroma_prev must be reserved via reserve_420_chroma_prev_u16 first"
);
chroma_prev[..width].copy_from_slice(&uv_half[..width]);
*chroma_prev_row = Some(idx / 2);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
pub(crate) fn upsample_420_chroma_sited_p0xx<'s, const BITS: u32>(
chroma_full: &'s mut [u16],
chroma_prev: &mut [u16],
chroma_prev_row: &mut Option<usize>,
uv_half: &[u16],
idx: usize,
bottom_v: bool,
center_h: bool,
stage: bool,
width: usize,
big_endian: bool,
use_simd: bool,
) -> &'s [u16] {
debug_assert!(
chroma_full.len() >= 2 * width,
"chroma_full must be reserved via reserve_420_chroma_full_u16 first"
);
let chroma_row = idx / 2;
let do_vblend =
bottom_v && idx & 1 == 0 && chroma_row > 0 && *chroma_prev_row == Some(chroma_row - 1);
let result = if do_vblend {
debug_assert!(
chroma_prev.len() >= width,
"chroma_prev must be reserved via reserve_420_chroma_prev_u16 first"
);
if center_h {
crate::row::chroma_upsample_420_bottom_even_h_p0xx_row::<BITS>(
&chroma_prev[..width],
uv_half,
&mut chroma_full[..2 * width],
width,
big_endian,
use_simd,
);
} else {
crate::row::chroma_upsample_420_bottomleft_even_h_p0xx_row::<BITS>(
&chroma_prev[..width],
uv_half,
&mut chroma_full[..2 * width],
width,
big_endian,
use_simd,
);
}
&chroma_full[..2 * width]
} else if center_h {
upsample_420_chroma_center_h_p0xx::<BITS>(chroma_full, uv_half, width, big_endian, use_simd)
} else {
upsample_420_chroma_cosited_h_p0xx::<BITS>(chroma_full, uv_half, width, big_endian)
};
if bottom_v && stage {
stage_420_chroma_prev_p0xx(chroma_prev, chroma_prev_row, uv_half, idx, width);
}
result
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
macro_rules! p0xx_by_bits {
($bits:expr; $k10:path, $k12:path, $k16:path; $($arg:expr),* $(,)?) => {
match $bits {
10 => $k10($($arg),*),
12 => $k12($($arg),*),
16 => $k16($($arg),*),
_ => unreachable!("BITS pinned to 10/12/16 by the P0xx call sites"),
}
};
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn emit_p4xx_rgb_u8_wire<const BITS: u32>(
y: &[u16],
uv_full: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
big_endian: bool,
) {
p0xx_by_bits!(
BITS;
p410_to_rgb_row_endian, p412_to_rgb_row_endian, p416_to_rgb_row_endian;
y, uv_full, rgb_out, width, matrix, full_range, use_simd, big_endian
);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn emit_p4xx_rgba_u8_wire<const BITS: u32>(
y: &[u16],
uv_full: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
big_endian: bool,
) {
p0xx_by_bits!(
BITS;
p410_to_rgba_row_endian, p412_to_rgba_row_endian, p416_to_rgba_row_endian;
y, uv_full, rgba_out, width, matrix, full_range, use_simd, big_endian
);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn emit_p4xx_rgb_u16_wire<const BITS: u32>(
y: &[u16],
uv_full: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
big_endian: bool,
) {
p0xx_by_bits!(
BITS;
p410_to_rgb_u16_row_endian, p412_to_rgb_u16_row_endian, p416_to_rgb_u16_row_endian;
y, uv_full, rgb_out, width, matrix, full_range, use_simd, big_endian
);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn emit_p4xx_rgba_u16_wire<const BITS: u32>(
y: &[u16],
uv_full: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
big_endian: bool,
) {
p0xx_by_bits!(
BITS;
p410_to_rgba_u16_row_endian, p412_to_rgba_u16_row_endian, p416_to_rgba_u16_row_endian;
y, uv_full, rgba_out, width, matrix, full_range, use_simd, big_endian
);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn emit_p4xx_hsv_u8_wire<const BITS: u32>(
y: &[u16],
uv_full: &[u16],
h_out: &mut [u8],
s_out: &mut [u8],
v_out: &mut [u8],
width: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
big_endian: bool,
) {
p0xx_by_bits!(
BITS;
p410_to_hsv_row_endian, p412_to_hsv_row_endian, p416_to_hsv_row_endian;
y, uv_full, h_out, s_out, v_out, width, matrix, full_range, use_simd, big_endian
);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn p0xx_top_identity_color_row<const BITS: u32, const BE: bool>(
rgb: &mut Option<&mut [u8]>,
rgba: &mut Option<&mut [u8]>,
rgb_u16: &mut Option<&mut [u16]>,
rgba_u16: &mut Option<&mut [u16]>,
hsv: &mut Option<HsvFrameMut<'_>>,
rgb_scratch: &mut std::vec::Vec<u8>,
y: &[u16],
uv_full: &[u16],
one_plane_start: usize,
one_plane_end: usize,
w: usize,
h: usize,
matrix: KernelMatrix,
full_range: bool,
use_simd: bool,
) -> Result<(), MixedSinkerError> {
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_rgb_u16 = rgb_u16.is_some();
let want_rgba_u16 = rgba_u16.is_some();
if want_rgba_u16 && !want_rgb_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
emit_p4xx_rgba_u16_wire::<BITS>(
y,
uv_full,
rgba_u16_row,
w,
matrix,
full_range,
use_simd,
BE,
);
} else if want_rgb_u16 {
let rgb_u16_buf = rgb_u16.as_deref_mut().unwrap();
let rgb_plane_end = one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
let rgb_u16_row = &mut rgb_u16_buf[rgb_plane_start..rgb_plane_end];
emit_p4xx_rgb_u16_wire::<BITS>(y, uv_full, rgb_u16_row, w, matrix, full_range, use_simd, BE);
if want_rgba_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_u16_to_rgba_u16_row::<BITS>(rgb_u16_row, rgba_u16_row, w);
}
}
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (hh, ss, vv) = hsv.hsv();
emit_p4xx_hsv_u8_wire::<BITS>(
y,
uv_full,
&mut hh[one_plane_start..one_plane_end],
&mut ss[one_plane_start..one_plane_end],
&mut vv[one_plane_start..one_plane_end],
w,
matrix,
full_range,
use_simd,
BE,
);
return Ok(());
}
if want_rgba && !need_rgb_kernel {
let rgba_buf = rgba.as_deref_mut().unwrap();
let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
emit_p4xx_rgba_u8_wire::<BITS>(y, uv_full, rgba_row, w, matrix, full_range, use_simd, BE);
return Ok(());
}
if !need_rgb_kernel {
return Ok(());
}
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
emit_p4xx_rgb_u8_wire::<BITS>(y, uv_full, rgb_row, w, matrix, full_range, use_simd, BE);
if let Some(hsv) = hsv.as_mut() {
let (hh, ss, vv) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut hh[one_plane_start..one_plane_end],
&mut ss[one_plane_start..one_plane_end],
&mut vv[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = rgba.as_deref_mut() {
let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
}
Ok(())
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn p0xx_top_reconstruct_area<const BITS: u32, const BE: bool>(
luma_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
rgb_stream: &mut Option<std::boxed::Box<crate::resample::AreaStream<u8>>>,
rgb_stream_u16: &mut Option<std::boxed::Box<crate::resample::AreaStream<u16>>>,
resample_outputs: &mut Option<FrozenOutputs>,
rgb: &mut Option<&mut [u8]>,
rgba: &mut Option<&mut [u8]>,
rgb_u16: &mut Option<&mut [u16]>,
rgba_u16: &mut Option<&mut [u16]>,
luma: &mut Option<&mut [u8]>,
hsv: &mut Option<HsvFrameMut<'_>>,
luma_scratch_u16: &mut std::vec::Vec<u16>,
rgb_scratch: &mut std::vec::Vec<u8>,
rgb_scratch_u16: &mut std::vec::Vec<u16>,
chroma_full_u16: &mut std::vec::Vec<u16>,
chroma_prev_u16: &mut std::vec::Vec<u16>,
chroma_prev_row: &mut Option<usize>,
chroma_top_pending: &mut Option<(usize, KernelMatrix, bool)>,
chroma_top_y_u16: &mut std::vec::Vec<u16>,
y_row: &[u16],
uv_half: &[u16],
w: usize,
h: usize,
plan: &ResamplePlan,
idx: usize,
use_simd: bool,
matrix: KernelMatrix,
full_range: bool,
center_sited: bool,
) -> Result<core::ops::ControlFlow<()>, MixedSinkerError> {
let cursor = if luma.is_some() {
luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
let expected_source = cursor + usize::from(chroma_top_pending.is_some());
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected_source),
idx,
)? {
return Ok(core::ops::ControlFlow::Break(()));
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
reserve_420_chroma_top_y_u16(chroma_top_y_u16, w, h)?;
let is_last = idx + 1 == h;
if idx & 1 == 0 {
if let Some((p_idx, p_matrix, p_full_range)) = chroma_top_pending.take() {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
true,
center_sited,
false,
w,
BE,
use_simd,
);
let held_y: &[u16] = &chroma_top_y_u16[..w];
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
p_idx,
use_simd,
p_matrix,
p_full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(held_y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(
held_y,
uv_full,
scratch,
w,
p_matrix,
p_full_range,
use_simd,
BE,
)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(
held_y,
uv_full,
scratch,
w,
p_matrix,
p_full_range,
use_simd,
BE,
)
},
)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y_row.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
} else if !is_last {
chroma_top_y_u16[..w].copy_from_slice(&y_row[..w]);
*chroma_top_pending = Some((idx, matrix, full_range));
} else {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y_row.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
}
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
Ok(core::ops::ControlFlow::Continue(()))
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
#[allow(clippy::too_many_arguments)]
fn p0xx_top_reconstruct_filter<const BITS: u32, const BE: bool>(
luma_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
rgb_filter_stream: &mut Option<std::boxed::Box<crate::resample::FilterStream<u8>>>,
rgb_filter_stream_u16: &mut Option<std::boxed::Box<crate::resample::FilterStream<u16>>>,
resample_outputs: &mut Option<FrozenOutputs>,
rgb: &mut Option<&mut [u8]>,
rgba: &mut Option<&mut [u8]>,
rgb_u16: &mut Option<&mut [u16]>,
rgba_u16: &mut Option<&mut [u16]>,
luma: &mut Option<&mut [u8]>,
hsv: &mut Option<HsvFrameMut<'_>>,
luma_scratch_u16: &mut std::vec::Vec<u16>,
rgb_scratch: &mut std::vec::Vec<u8>,
rgb_scratch_u16: &mut std::vec::Vec<u16>,
chroma_full_u16: &mut std::vec::Vec<u16>,
chroma_prev_u16: &mut std::vec::Vec<u16>,
chroma_prev_row: &mut Option<usize>,
chroma_top_pending: &mut Option<(usize, KernelMatrix, bool)>,
chroma_top_y_u16: &mut std::vec::Vec<u16>,
y_row: &[u16],
uv_half: &[u16],
w: usize,
h: usize,
plan: &ResamplePlan,
idx: usize,
use_simd: bool,
matrix: KernelMatrix,
full_range: bool,
center_sited: bool,
) -> Result<core::ops::ControlFlow<()>, MixedSinkerError> {
let cursor = if luma.is_some() {
luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
let expected_source = cursor + usize::from(chroma_top_pending.is_some());
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected_source),
idx,
)? {
return Ok(core::ops::ControlFlow::Break(()));
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
reserve_420_chroma_top_y_u16(chroma_top_y_u16, w, h)?;
let is_last = idx + 1 == h;
if idx & 1 == 0 {
if let Some((p_idx, p_matrix, p_full_range)) = chroma_top_pending.take() {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
true,
center_sited,
false,
w,
BE,
use_simd,
);
let held_y: &[u16] = &chroma_top_y_u16[..w];
packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
p_idx,
use_simd,
p_matrix,
p_full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(held_y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(
held_y,
uv_full,
scratch,
w,
p_matrix,
p_full_range,
use_simd,
BE,
)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(
held_y,
uv_full,
scratch,
w,
p_matrix,
p_full_range,
use_simd,
BE,
)
},
)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y_row.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
} else if !is_last {
chroma_top_y_u16[..w].copy_from_slice(&y_row[..w]);
*chroma_top_pending = Some((idx, matrix, full_range));
} else {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y_row.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
emit_p4xx_rgb_u8_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
emit_p4xx_rgb_u16_wire::<BITS>(y_row, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
}
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
Ok(core::ops::ControlFlow::Continue(()))
}
impl<'a, R, const BE: bool> MixedSinker<'a, P010<BE>, R> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected_elements = self.frame_elems(3)?;
if buf.len() < expected_elements {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected_elements, buf.len()),
));
}
self.rgb_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
self.set_rgba(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaBuffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> P010Sink<BE> for MixedSinker<'_, P010<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P010<BE>, R> {
type Input<'r> = P010Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
if self.width & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(
self.width,
)));
}
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P010Row<'_>) -> Result<(), Self::Error> {
const BITS: u32 = 10;
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
if w & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(w)));
}
if row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y10,
idx,
w,
row.y().len(),
)));
}
if row.uv_half().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvHalf10,
idx,
w,
row.uv_half().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
#[cfg(feature = "yuv-planar")]
let chroma_location = self.chroma_location.clone();
let Self {
rgb,
rgb_u16,
rgba,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
plan,
#[cfg(feature = "yuv-planar")]
native,
#[cfg(feature = "yuv-planar")]
native_420_u16,
#[cfg(feature = "yuv-planar")]
p0xx_y_half,
#[cfg(feature = "yuv-planar")]
p0xx_u_half,
#[cfg(feature = "yuv-planar")]
p0xx_v_half,
#[cfg(feature = "yuv-planar")]
frozen_native_route,
#[cfg(feature = "yuv-planar")]
chroma_full_u16,
#[cfg(feature = "yuv-planar")]
frozen_chroma_centered,
#[cfg(feature = "yuv-planar")]
frozen_chroma_bottom_v,
#[cfg(feature = "yuv-planar")]
frozen_chroma_top_v,
#[cfg(feature = "yuv-planar")]
chroma_top_pending,
#[cfg(feature = "yuv-planar")]
chroma_top_y_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_row,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_half) = (row.y(), row.uv_half());
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_h_phase = if center_sited {
YUV422P_CENTERED_H_PHASE
} else {
0.0
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_v_phase = if bottom_v { 1.0 } else { 0.0 };
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let want_color =
rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let need_output = luma.is_some()
|| rgb.is_some()
|| rgba.is_some()
|| rgb_u16.is_some()
|| rgba_u16.is_some()
|| hsv.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
if plan.kind().is_filter() {
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
{
plan.ensure_single_kernel_filter()?;
if top_v && want_color {
match p0xx_top_reconstruct_filter::<BITS, BE>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p410_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p410_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
);
if r.is_ok() && bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
}
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p010_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p010_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
#[cfg(feature = "yuv-planar")]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(feature = "yuv-planar"),
with_native: *native,
area_plan: true,
},
),
InsertionPoint::NativeCodes
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_native_route
&& frozen != take_native
{
return Err(MixedSinkerError::NativeRouteChanged(
NativeRouteChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
if take_native {
let stale_native = idx == 0
&& native_420_u16.as_ref().is_some_and(|join| {
(join.chroma_phase_centered() == Some(!center_sited)
|| join.chroma_bottom() == Some(!bottom_v)
|| join.chroma_top() == Some(!top_v))
&& join.next_y() == 0
});
let prev_native = if stale_native {
native_420_u16.take()
} else {
None
};
let native_result = p0xx_process_native::<BITS, BE>(
plan,
native_420_u16,
p0xx_y_half,
p0xx_u_half,
p0xx_v_half,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
y,
uv_half,
chroma_h_phase,
chroma_v_phase,
top_v,
matrix,
full_range,
idx,
w,
h,
use_simd,
);
if stale_native && native_result.is_err() {
*native_420_u16 = prev_native;
}
native_result?;
#[cfg(feature = "yuv-semi-planar")]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
#[cfg(feature = "yuv-semi-planar")]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if top_v && want_color {
match p0xx_top_reconstruct_area::<BITS, BE>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p410_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p410_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
if bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| p010_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE),
|scratch| {
p010_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_rgb_u16 = rgb_u16.is_some();
let want_rgba_u16 = rgba_u16.is_some();
let want_color = want_rgb || want_rgba || want_hsv || want_rgb_u16 || want_rgba_u16;
let need_output = want_color || luma.is_some();
if !need_output {
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
#[cfg(feature = "yuv-planar")]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(feature = "yuv-planar")]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
let need_centered_chroma = (center_sited || bottom_v) && want_color && !top_v;
#[cfg(feature = "yuv-planar")]
if need_centered_chroma || (top_v && want_color) {
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
reserve_420_chroma_top_y_u16(chroma_top_y_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if bottom_v || top_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
if want_hsv && want_rgba && !want_rgb {
rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
}
#[cfg(feature = "yuv-planar")]
if (bottom_v || top_v) && !want_color {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
}
#[cfg(feature = "yuv-planar")]
let centered: Option<&[u16]> = if need_centered_chroma {
Some(upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
bottom_v,
center_sited,
true,
w,
BE,
use_simd,
))
} else {
None
};
#[cfg(not(feature = "yuv-planar"))]
let centered: Option<&[u16]> = None;
#[cfg(feature = "yuv-planar")]
if need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
if let Some(luma) = luma.as_deref_mut() {
p010_to_luma_row_endian(row.y(), &mut luma[one_plane_start..one_plane_end], w, BE);
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
let is_last = idx + 1 == h;
if idx & 1 == 0 {
if let Some((p_idx, p_matrix, p_full_range)) = chroma_top_pending.take() {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
true,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
&chroma_top_y_u16[..w],
uv_full,
p_idx * w,
p_idx * w + w,
w,
h,
p_matrix,
p_full_range,
use_simd,
)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
} else if !is_last {
chroma_top_y_u16[..w].copy_from_slice(&row.y()[..w]);
*chroma_top_pending = Some((idx, row.matrix(), row.full_range()));
} else {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
}
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
return Ok(());
}
if want_rgba_u16 && !want_rgb_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p410_to_rgba_u16_row_endian(
row.y(),
uv_full,
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p010_to_rgba_u16_row_endian(
row.y(),
row.uv_half(),
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
} else if want_rgb_u16 {
let rgb_u16_buf = rgb_u16.as_deref_mut().unwrap();
let rgb_plane_end =
one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
let rgb_u16_row = &mut rgb_u16_buf[rgb_plane_start..rgb_plane_end];
if let Some(uv_full) = centered {
p410_to_rgb_u16_row_endian(
row.y(),
uv_full,
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p010_to_rgb_u16_row_endian(
row.y(),
row.uv_half(),
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if want_rgba_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_u16_to_rgba_u16_row::<BITS>(rgb_u16_row, rgba_u16_row, w);
}
}
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (h, s, v) = hsv.hsv();
if let Some(uv_full) = centered {
p410_to_hsv_row_endian(
row.y(),
uv_full,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p010_to_hsv_row_endian(
row.y(),
row.uv_half(),
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if want_rgba && !need_rgb_kernel {
let rgba_buf = rgba.as_deref_mut().unwrap();
let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p410_to_rgba_row_endian(
row.y(),
uv_full,
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p010_to_rgba_row_endian(
row.y(),
row.uv_half(),
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if !need_rgb_kernel {
return Ok(());
}
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
if let Some(uv_full) = centered {
p410_to_rgb_row_endian(
row.y(),
uv_full,
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p010_to_rgb_row_endian(
row.y(),
row.uv_half(),
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = rgba.as_deref_mut() {
let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
}
Ok(())
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, P012<BE>, R> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected_elements = self.frame_elems(3)?;
if buf.len() < expected_elements {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected_elements, buf.len()),
));
}
self.rgb_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
self.set_rgba(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaBuffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> P012Sink<BE> for MixedSinker<'_, P012<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P012<BE>, R> {
type Input<'r> = P012Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
if self.width & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(
self.width,
)));
}
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P012Row<'_>) -> Result<(), Self::Error> {
const BITS: u32 = 12;
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
if w & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(w)));
}
if row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y12,
idx,
w,
row.y().len(),
)));
}
if row.uv_half().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvHalf12,
idx,
w,
row.uv_half().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
#[cfg(feature = "yuv-planar")]
let chroma_location = self.chroma_location.clone();
let Self {
rgb,
rgb_u16,
rgba,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
plan,
#[cfg(feature = "yuv-planar")]
native,
#[cfg(feature = "yuv-planar")]
native_420_u16,
#[cfg(feature = "yuv-planar")]
p0xx_y_half,
#[cfg(feature = "yuv-planar")]
p0xx_u_half,
#[cfg(feature = "yuv-planar")]
p0xx_v_half,
#[cfg(feature = "yuv-planar")]
frozen_native_route,
#[cfg(feature = "yuv-planar")]
chroma_full_u16,
#[cfg(feature = "yuv-planar")]
frozen_chroma_centered,
#[cfg(feature = "yuv-planar")]
frozen_chroma_bottom_v,
#[cfg(feature = "yuv-planar")]
frozen_chroma_top_v,
#[cfg(feature = "yuv-planar")]
chroma_top_pending,
#[cfg(feature = "yuv-planar")]
chroma_top_y_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_row,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_half) = (row.y(), row.uv_half());
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_h_phase = if center_sited {
YUV422P_CENTERED_H_PHASE
} else {
0.0
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_v_phase = if bottom_v { 1.0 } else { 0.0 };
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let want_color =
rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let need_output = luma.is_some()
|| rgb.is_some()
|| rgba.is_some()
|| rgb_u16.is_some()
|| rgba_u16.is_some()
|| hsv.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
if plan.kind().is_filter() {
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
{
plan.ensure_single_kernel_filter()?;
if top_v && want_color {
match p0xx_top_reconstruct_filter::<BITS, BE>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p412_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p412_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
);
if r.is_ok() && bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
}
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p012_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p012_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
#[cfg(feature = "yuv-planar")]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(feature = "yuv-planar"),
with_native: *native,
area_plan: true,
},
),
InsertionPoint::NativeCodes
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_native_route
&& frozen != take_native
{
return Err(MixedSinkerError::NativeRouteChanged(
NativeRouteChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
if take_native {
let stale_native = idx == 0
&& native_420_u16.as_ref().is_some_and(|join| {
(join.chroma_phase_centered() == Some(!center_sited)
|| join.chroma_bottom() == Some(!bottom_v)
|| join.chroma_top() == Some(!top_v))
&& join.next_y() == 0
});
let prev_native = if stale_native {
native_420_u16.take()
} else {
None
};
let native_result = p0xx_process_native::<BITS, BE>(
plan,
native_420_u16,
p0xx_y_half,
p0xx_u_half,
p0xx_v_half,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
y,
uv_half,
chroma_h_phase,
chroma_v_phase,
top_v,
matrix,
full_range,
idx,
w,
h,
use_simd,
);
if stale_native && native_result.is_err() {
*native_420_u16 = prev_native;
}
native_result?;
#[cfg(feature = "yuv-semi-planar")]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
#[cfg(feature = "yuv-semi-planar")]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if top_v && want_color {
match p0xx_top_reconstruct_area::<BITS, BE>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p412_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p412_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
if bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| p012_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE),
|scratch| {
p012_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_rgb_u16 = rgb_u16.is_some();
let want_rgba_u16 = rgba_u16.is_some();
let want_color = want_rgb || want_rgba || want_hsv || want_rgb_u16 || want_rgba_u16;
let need_output = want_color || luma.is_some();
if !need_output {
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
#[cfg(feature = "yuv-planar")]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(feature = "yuv-planar")]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
let need_centered_chroma = (center_sited || bottom_v) && want_color && !top_v;
#[cfg(feature = "yuv-planar")]
if need_centered_chroma || (top_v && want_color) {
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
reserve_420_chroma_top_y_u16(chroma_top_y_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if bottom_v || top_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
if want_hsv && want_rgba && !want_rgb {
rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
}
#[cfg(feature = "yuv-planar")]
if (bottom_v || top_v) && !want_color {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
}
#[cfg(feature = "yuv-planar")]
let centered: Option<&[u16]> = if need_centered_chroma {
Some(upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
bottom_v,
center_sited,
true,
w,
BE,
use_simd,
))
} else {
None
};
#[cfg(not(feature = "yuv-planar"))]
let centered: Option<&[u16]> = None;
#[cfg(feature = "yuv-planar")]
if need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
if let Some(luma) = luma.as_deref_mut() {
p012_to_luma_row_endian(row.y(), &mut luma[one_plane_start..one_plane_end], w, BE);
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
let is_last = idx + 1 == h;
if idx & 1 == 0 {
if let Some((p_idx, p_matrix, p_full_range)) = chroma_top_pending.take() {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
true,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
&chroma_top_y_u16[..w],
uv_full,
p_idx * w,
p_idx * w + w,
w,
h,
p_matrix,
p_full_range,
use_simd,
)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
} else if !is_last {
chroma_top_y_u16[..w].copy_from_slice(&row.y()[..w]);
*chroma_top_pending = Some((idx, row.matrix(), row.full_range()));
} else {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
}
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
return Ok(());
}
if want_rgba_u16 && !want_rgb_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p412_to_rgba_u16_row_endian(
row.y(),
uv_full,
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p012_to_rgba_u16_row_endian(
row.y(),
row.uv_half(),
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
} else if want_rgb_u16 {
let rgb_u16_buf = rgb_u16.as_deref_mut().unwrap();
let rgb_plane_end =
one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
let rgb_u16_row = &mut rgb_u16_buf[rgb_plane_start..rgb_plane_end];
if let Some(uv_full) = centered {
p412_to_rgb_u16_row_endian(
row.y(),
uv_full,
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p012_to_rgb_u16_row_endian(
row.y(),
row.uv_half(),
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if want_rgba_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_u16_to_rgba_u16_row::<BITS>(rgb_u16_row, rgba_u16_row, w);
}
}
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (h, s, v) = hsv.hsv();
if let Some(uv_full) = centered {
p412_to_hsv_row_endian(
row.y(),
uv_full,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p012_to_hsv_row_endian(
row.y(),
row.uv_half(),
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if want_rgba && !need_rgb_kernel {
let rgba_buf = rgba.as_deref_mut().unwrap();
let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p412_to_rgba_row_endian(
row.y(),
uv_full,
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p012_to_rgba_row_endian(
row.y(),
row.uv_half(),
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if !need_rgb_kernel {
return Ok(());
}
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
if let Some(uv_full) = centered {
p412_to_rgb_row_endian(
row.y(),
uv_full,
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p012_to_rgb_row_endian(
row.y(),
row.uv_half(),
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = rgba.as_deref_mut() {
let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
}
Ok(())
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, P016<BE>, R> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected_elements = self.frame_elems(3)?;
if buf.len() < expected_elements {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected_elements, buf.len()),
));
}
self.rgb_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
self.set_rgba(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaBuffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> P016Sink<BE> for MixedSinker<'_, P016<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P016<BE>, R> {
type Input<'r> = P016Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
if self.width & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(
self.width,
)));
}
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P016Row<'_>) -> Result<(), Self::Error> {
const BITS: u32 = 16;
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
if w & 1 != 0 {
return Err(MixedSinkerError::WidthAlignment(WidthAlignment::odd(w)));
}
if row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y16,
idx,
w,
row.y().len(),
)));
}
if row.uv_half().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvHalf16,
idx,
w,
row.uv_half().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
#[cfg(feature = "yuv-planar")]
let chroma_location = self.chroma_location.clone();
let Self {
rgb,
rgb_u16,
rgba,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
plan,
#[cfg(feature = "yuv-planar")]
native,
#[cfg(feature = "yuv-planar")]
native_420_u16,
#[cfg(feature = "yuv-planar")]
p0xx_y_half,
#[cfg(feature = "yuv-planar")]
p0xx_u_half,
#[cfg(feature = "yuv-planar")]
p0xx_v_half,
#[cfg(feature = "yuv-planar")]
frozen_native_route,
#[cfg(feature = "yuv-planar")]
chroma_full_u16,
#[cfg(feature = "yuv-planar")]
frozen_chroma_centered,
#[cfg(feature = "yuv-planar")]
frozen_chroma_bottom_v,
#[cfg(feature = "yuv-planar")]
frozen_chroma_top_v,
#[cfg(feature = "yuv-planar")]
chroma_top_pending,
#[cfg(feature = "yuv-planar")]
chroma_top_y_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_u16,
#[cfg(feature = "yuv-planar")]
chroma_prev_row,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_half) = (row.y(), row.uv_half());
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_h_phase = if center_sited {
YUV422P_CENTERED_H_PHASE
} else {
0.0
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let chroma_v_phase = if bottom_v { 1.0 } else { 0.0 };
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let want_color =
rgb.is_some() || rgba.is_some() || hsv.is_some() || rgb_u16.is_some() || rgba_u16.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
let need_output = luma.is_some()
|| rgb.is_some()
|| rgba.is_some()
|| rgb_u16.is_some()
|| rgba_u16.is_some()
|| hsv.is_some();
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
if plan.kind().is_filter() {
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
{
plan.ensure_single_kernel_filter()?;
if top_v && want_color {
match p0xx_top_reconstruct_filter::<BITS, BE>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_filter_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_filter_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p416_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p416_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
);
if r.is_ok() && bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
}
let r = packed_yuv422_triple_filter_resample::<BITS>(
luma_filter_stream_u16,
rgb_filter_stream,
rgb_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p016_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p016_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if r.is_ok() && need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return r;
}
#[cfg(feature = "yuv-planar")]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(feature = "yuv-planar"),
with_native: *native,
area_plan: true,
},
),
InsertionPoint::NativeCodes
);
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if need_output
&& let Some(frozen) = *frozen_native_route
&& frozen != take_native
{
return Err(MixedSinkerError::NativeRouteChanged(
NativeRouteChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
if take_native {
let stale_native = idx == 0
&& native_420_u16.as_ref().is_some_and(|join| {
(join.chroma_phase_centered() == Some(!center_sited)
|| join.chroma_bottom() == Some(!bottom_v)
|| join.chroma_top() == Some(!top_v))
&& join.next_y() == 0
});
let prev_native = if stale_native {
native_420_u16.take()
} else {
None
};
let native_result = p0xx_process_native::<BITS, BE>(
plan,
native_420_u16,
p0xx_y_half,
p0xx_u_half,
p0xx_v_half,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
rgb_scratch,
rgb_scratch_u16,
y,
uv_half,
chroma_h_phase,
chroma_v_phase,
top_v,
matrix,
full_range,
idx,
w,
h,
use_simd,
);
if stale_native && native_result.is_err() {
*native_420_u16 = prev_native;
}
native_result?;
#[cfg(feature = "yuv-semi-planar")]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
#[cfg(feature = "yuv-semi-planar")]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if top_v && want_color {
match p0xx_top_reconstruct_area::<BITS, BE>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
chroma_top_pending,
chroma_top_y_u16,
y,
uv_half,
w,
h,
plan,
idx,
use_simd,
matrix,
full_range,
center_sited,
)? {
core::ops::ControlFlow::Break(()) => return Ok(()),
core::ops::ControlFlow::Continue(()) => {}
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if (center_sited || bottom_v) && want_color {
let expected = if luma.is_some() {
luma_stream_u16.as_ref().map_or(0, |s| s.next_y())
} else if rgb.is_some() || rgba.is_some() || hsv.is_some() {
rgb_stream.as_ref().map_or(0, |s| s.next_y())
} else {
rgb_stream_u16.as_ref().map_or(0, |s| s.next_y())
};
if let core::ops::ControlFlow::Break(()) = resample_preflight_check_only(
resample_outputs,
luma,
&None,
rgb,
rgba,
rgb_u16,
rgba_u16,
&None,
&None,
&None,
&None,
&None,
hsv,
&None,
Some(expected),
idx,
)? {
return Ok(());
}
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
if bottom_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
uv_half,
idx,
bottom_v,
center_sited,
false,
w,
BE,
use_simd,
);
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| {
p416_to_rgb_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
|scratch| {
p416_to_rgb_u16_row_endian(y, uv_full, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
if bottom_v {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, uv_half, idx, w);
}
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
packed_yuv422_triple_resample::<BITS>(
luma_stream_u16,
rgb_stream,
rgb_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
luma_scratch_u16,
rgb_scratch,
rgb_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|scratch| {
for (dst, &s) in scratch[..w].iter_mut().zip(y.iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*dst = logical >> (16 - BITS);
}
},
|scratch| p016_to_rgb_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE),
|scratch| {
p016_to_rgb_u16_row_endian(y, uv_half, scratch, w, matrix, full_range, use_simd, BE)
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_chroma_centered.is_none() && need_output {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
return Ok(());
}
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_rgb_u16 = rgb_u16.is_some();
let want_rgba_u16 = rgba_u16.is_some();
let want_color = want_rgb || want_rgba || want_hsv || want_rgb_u16 || want_rgba_u16;
let need_output = want_color || luma.is_some();
if !need_output {
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
#[cfg(feature = "yuv-planar")]
let center_sited = chroma_420_center_sited_h(&chroma_location);
#[cfg(feature = "yuv-planar")]
let bottom_v = chroma_420_bottom_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
let top_v = chroma_420_top_sited_v(&chroma_location);
#[cfg(feature = "yuv-planar")]
if need_output
&& let Some(frozen) = *frozen_chroma_centered
&& (frozen != center_sited
|| *frozen_chroma_bottom_v != Some(bottom_v)
|| *frozen_chroma_top_v != Some(top_v))
{
return Err(MixedSinkerError::ChromaSitingChanged(
ChromaSitingChanged::new(idx),
));
}
#[cfg(feature = "yuv-planar")]
let need_centered_chroma = (center_sited || bottom_v) && want_color && !top_v;
#[cfg(feature = "yuv-planar")]
if need_centered_chroma || (top_v && want_color) {
reserve_420_chroma_full_u16(chroma_full_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
reserve_420_chroma_top_y_u16(chroma_top_y_u16, w, h)?;
}
#[cfg(feature = "yuv-planar")]
if bottom_v || top_v {
reserve_420_chroma_prev_u16(chroma_prev_u16, w, h)?;
}
if want_hsv && want_rgba && !want_rgb {
rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
}
#[cfg(feature = "yuv-planar")]
if (bottom_v || top_v) && !want_color {
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
}
#[cfg(feature = "yuv-planar")]
let centered: Option<&[u16]> = if need_centered_chroma {
Some(upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
bottom_v,
center_sited,
true,
w,
BE,
use_simd,
))
} else {
None
};
#[cfg(not(feature = "yuv-planar"))]
let centered: Option<&[u16]> = None;
#[cfg(feature = "yuv-planar")]
if need_output && frozen_chroma_centered.is_none() {
*frozen_chroma_centered = Some(center_sited);
*frozen_chroma_bottom_v = Some(bottom_v);
*frozen_chroma_top_v = Some(top_v);
}
if let Some(luma) = luma.as_deref_mut() {
p016_to_luma_row_endian(row.y(), &mut luma[one_plane_start..one_plane_end], w, BE);
}
#[cfg(feature = "yuv-planar")]
if top_v && want_color {
let is_last = idx + 1 == h;
if idx & 1 == 0 {
if let Some((p_idx, p_matrix, p_full_range)) = chroma_top_pending.take() {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
true,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
&chroma_top_y_u16[..w],
uv_full,
p_idx * w,
p_idx * w + w,
w,
h,
p_matrix,
p_full_range,
use_simd,
)?;
}
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
} else if !is_last {
chroma_top_y_u16[..w].copy_from_slice(&row.y()[..w]);
*chroma_top_pending = Some((idx, row.matrix(), row.full_range()));
} else {
let uv_full = upsample_420_chroma_sited_p0xx::<BITS>(
chroma_full_u16,
chroma_prev_u16,
chroma_prev_row,
row.uv_half(),
idx,
false,
center_sited,
false,
w,
BE,
use_simd,
);
p0xx_top_identity_color_row::<BITS, BE>(
rgb,
rgba,
rgb_u16,
rgba_u16,
hsv,
rgb_scratch,
row.y(),
uv_full,
one_plane_start,
one_plane_end,
w,
h,
row.matrix(),
row.full_range(),
use_simd,
)?;
}
stage_420_chroma_prev_p0xx(chroma_prev_u16, chroma_prev_row, row.uv_half(), idx, w);
return Ok(());
}
if want_rgba_u16 && !want_rgb_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p416_to_rgba_u16_row_endian(
row.y(),
uv_full,
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p016_to_rgba_u16_row_endian(
row.y(),
row.uv_half(),
rgba_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
} else if want_rgb_u16 {
let rgb_u16_buf = rgb_u16.as_deref_mut().unwrap();
let rgb_plane_end =
one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
let rgb_u16_row = &mut rgb_u16_buf[rgb_plane_start..rgb_plane_end];
if let Some(uv_full) = centered {
p416_to_rgb_u16_row_endian(
row.y(),
uv_full,
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p016_to_rgb_u16_row_endian(
row.y(),
row.uv_half(),
rgb_u16_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if want_rgba_u16 {
let rgba_u16_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row =
rgba_u16_plane_row_slice(rgba_u16_buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_u16_to_rgba_u16_row::<BITS>(rgb_u16_row, rgba_u16_row, w);
}
}
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (h, s, v) = hsv.hsv();
if let Some(uv_full) = centered {
p416_to_hsv_row_endian(
row.y(),
uv_full,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p016_to_hsv_row_endian(
row.y(),
row.uv_half(),
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if want_rgba && !need_rgb_kernel {
let rgba_buf = rgba.as_deref_mut().unwrap();
let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
if let Some(uv_full) = centered {
p416_to_rgba_row_endian(
row.y(),
uv_full,
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p016_to_rgba_row_endian(
row.y(),
row.uv_half(),
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
return Ok(());
}
if !need_rgb_kernel {
return Ok(());
}
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
if let Some(uv_full) = centered {
p416_to_rgb_row_endian(
row.y(),
uv_full,
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
} else {
p016_to_rgb_row_endian(
row.y(),
row.uv_half(),
rgb_row,
w,
row.matrix(),
row.full_range(),
use_simd,
BE,
);
}
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = rgba.as_deref_mut() {
let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
}
Ok(())
}
}