use super::super::{
GeometryOverflow, InsufficientBuffer, MixedSinker, MixedSinkerError, RowIndexOutOfRange,
RowShapeMismatch, RowSlice, check_dimensions_match, packed_yuv444_triple_filter_resample,
packed_yuv444_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::{
FrozenOutputs, HsvFrameMut, NativePlanarYuvU16, NativeRouteChanged,
native_planar_hb_preflight_check_only, yuv_planar16_process_native,
};
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
use crate::{
KernelMatrix,
resample::{
AveragingDomain, InsertionContext, InsertionPoint, PlanGeometry, ResampleError, ResamplePlan,
select_insertion_point,
},
};
#[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_P4XX_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_p4xx_alloc_failure() {
FORCE_P4XX_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_P4XX_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 p4xx_process_native<const BITS: u32, const BE: bool>(
plan: &ResamplePlan,
native_planar_u16: &mut Option<std::boxed::Box<NativePlanarYuvU16>>,
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_full: &[u16],
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:4:4 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;
if !native_planar_hb_preflight_check_only(
native_planar_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&None,
hsv,
idx,
need_luma,
need_color,
)? {
return Ok(());
}
grow_depack_scratch(y_scratch, w, true, w, h, plan)?;
if need_color {
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 need_color {
for (i, pair) in uv_full.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_plane, v_plane): (&[u16], &[u16]) = if need_color {
(&u_scratch[..cw], &v_scratch[..cw])
} else {
(&[], &[])
};
yuv_planar16_process_native::<BITS, HOST_NATIVE_BE>(
plan,
native_planar_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
&y_scratch[..w],
u_plane,
v_plane,
matrix,
full_range,
idx,
w,
h,
1,
cw,
|| ResamplePlan::area(cw, h, plan.out_w(), plan.out_h()),
use_simd,
)
}
impl<'a, R, const BE: bool> MixedSinker<'a, P410<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 = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, 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> P410Sink<BE> for MixedSinker<'_, P410<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P410<BE>, R> {
type Input<'r> = P410Row<'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> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P410Row<'_>) -> 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 row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y10,
idx,
w,
row.y().len(),
)));
}
if row.uv_full().len() != 2 * w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvFull10,
idx,
2 * w,
row.uv_full().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native_planar_u16,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_y_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_u_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_v_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
frozen_native_route,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_full) = (row.y(), row.uv_full());
if plan.kind().is_filter() {
return packed_yuv444_triple_filter_resample::<BITS>(
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
);
}
#[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"))]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(all(feature = "yuv-semi-planar", 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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if take_native {
p4xx_process_native::<BITS, BE>(
plan,
native_planar_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_full,
matrix,
full_range,
idx,
w,
h,
use_simd,
)?;
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
return Ok(());
}
packed_yuv444_triple_resample::<BITS>(
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
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,
)?;
}
if let Some(luma) = luma.as_deref_mut() {
let dst = &mut luma[one_plane_start..one_plane_end];
for (d, &s) in dst.iter_mut().zip(row.y().iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*d = (logical >> 8) as u8;
}
}
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)?;
p410_to_rgba_u16_row_endian(
row.y(),
row.uv_full(),
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];
p410_to_rgb_u16_row_endian(
row.y(),
row.uv_full(),
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();
p410_to_hsv_row_endian(
row.y(),
row.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,
);
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)?;
p410_to_rgba_row_endian(
row.y(),
row.uv_full(),
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,
)?;
p410_to_rgb_row_endian(
row.y(),
row.uv_full(),
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, P412<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 = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, 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> P412Sink<BE> for MixedSinker<'_, P412<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P412<BE>, R> {
type Input<'r> = P412Row<'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> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P412Row<'_>) -> 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 row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y12,
idx,
w,
row.y().len(),
)));
}
if row.uv_full().len() != 2 * w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvFull12,
idx,
2 * w,
row.uv_full().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native_planar_u16,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_y_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_u_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_v_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
frozen_native_route,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_full) = (row.y(), row.uv_full());
if plan.kind().is_filter() {
return packed_yuv444_triple_filter_resample::<BITS>(
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
);
}
#[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"))]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(all(feature = "yuv-semi-planar", 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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if take_native {
p4xx_process_native::<BITS, BE>(
plan,
native_planar_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_full,
matrix,
full_range,
idx,
w,
h,
use_simd,
)?;
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
return Ok(());
}
packed_yuv444_triple_resample::<BITS>(
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
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,
)?;
}
if let Some(luma) = luma.as_deref_mut() {
let dst = &mut luma[one_plane_start..one_plane_end];
for (d, &s) in dst.iter_mut().zip(row.y().iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*d = (logical >> 8) as u8;
}
}
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)?;
p412_to_rgba_u16_row_endian(
row.y(),
row.uv_full(),
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];
p412_to_rgb_u16_row_endian(
row.y(),
row.uv_full(),
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();
p412_to_hsv_row_endian(
row.y(),
row.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,
);
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)?;
p412_to_rgba_row_endian(
row.y(),
row.uv_full(),
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,
)?;
p412_to_rgb_row_endian(
row.y(),
row.uv_full(),
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, P416<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 = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, 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> P416Sink<BE> for MixedSinker<'_, P416<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, P416<BE>, R> {
type Input<'r> = P416Row<'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> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuv_streams(self);
Ok(())
}
fn process(&mut self, row: P416Row<'_>) -> 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 row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y16,
idx,
w,
row.y().len(),
)));
}
if row.uv_full().len() != 2 * w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UvFull16,
idx,
2 * w,
row.uv_full().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
native_planar_u16,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_y_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_u_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
p0xx_v_half,
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
frozen_native_route,
..
} = self;
if let Some(plan) = plan.as_ref() {
let matrix = row.matrix();
let full_range = row.full_range();
let (y, uv_full) = (row.y(), row.uv_full());
if plan.kind().is_filter() {
return packed_yuv444_triple_filter_resample::<BITS>(
rgb_filter_stream,
rgb_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
);
}
#[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"))]
let take_native = matches!(
select_insertion_point(
AveragingDomain::Encoded,
InsertionContext {
native_eligible: cfg!(all(feature = "yuv-semi-planar", 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(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if take_native {
p4xx_process_native::<BITS, BE>(
plan,
native_planar_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_full,
matrix,
full_range,
idx,
w,
h,
use_simd,
)?;
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(true);
}
return Ok(());
}
packed_yuv444_triple_resample::<BITS>(
rgb_stream,
rgb_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
&mut None,
hsv,
rgb_scratch,
rgb_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
matrix,
full_range,
|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)
},
|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);
}
},
)?;
#[cfg(all(feature = "yuv-semi-planar", feature = "yuv-planar"))]
if frozen_native_route.is_none() && need_output {
*frozen_native_route = Some(false);
}
return Ok(());
}
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
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,
)?;
}
if let Some(luma) = luma.as_deref_mut() {
let dst = &mut luma[one_plane_start..one_plane_end];
for (d, &s) in dst.iter_mut().zip(row.y().iter()) {
let logical = if BE { u16::from_be(s) } else { u16::from_le(s) };
*d = (logical >> 8) as u8;
}
}
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)?;
p416_to_rgba_u16_row_endian(
row.y(),
row.uv_full(),
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];
p416_to_rgb_u16_row_endian(
row.y(),
row.uv_full(),
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();
p416_to_hsv_row_endian(
row.y(),
row.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,
);
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)?;
p416_to_rgba_row_endian(
row.y(),
row.uv_full(),
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,
)?;
p416_to_rgb_row_endian(
row.y(),
row.uv_full(),
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(())
}
}