use crate::internals::ProcessedOffset;
use crate::neon::utils::*;
use crate::yuv_support::{CbCrForwardTransform, YuvChromaRange, YuvSourceChannels};
use std::arch::aarch64::*;
#[cfg(feature = "rdm")]
#[target_feature(enable = "rdm")]
pub(crate) unsafe fn neon_rgba_to_yuv_rdm420<const ORIGIN_CHANNELS: u8>(
transform: &CbCrForwardTransform<i32>,
range: &YuvChromaRange,
y_plane0: &mut [u8],
y_plane1: &mut [u8],
u_plane: &mut [u8],
v_plane: &mut [u8],
rgba0: &[u8],
rgba1: &[u8],
start_cx: usize,
start_ux: usize,
width: usize,
) -> ProcessedOffset {
let source_channels: YuvSourceChannels = ORIGIN_CHANNELS.into();
let channels = source_channels.get_channels_count();
const V_SCALE: i32 = 4;
const V_HALF_SCALE: i32 = V_SCALE - 2;
const A_E: i32 = 2;
let u_ptr = u_plane;
let v_ptr = v_plane;
let y_bias = vdupq_n_s16(range.bias_y as i16 * (1 << A_E));
let uv_bias = vdupq_n_s16(range.bias_uv as i16 * (1 << A_E) + (1 << (A_E - 1)) - 1);
let weights_arr: [i16; 8] = [
transform.yr as i16,
transform.yg as i16,
transform.yb as i16,
transform.cb_r as i16,
transform.cb_g as i16,
transform.cb_b as i16,
transform.cr_r as i16,
transform.cr_g as i16,
];
let v_weights = vld1q_s16(weights_arr.as_ptr());
let v_cr_b = vdupq_n_s16(transform.cr_b as i16);
let mut cx = start_cx;
let mut ux = start_ux;
while cx + 16 <= width {
let (r_values0, g_values0, b_values0) =
neon_vld_rgb_for_yuv::<ORIGIN_CHANNELS>(rgba0.get_unchecked(cx * channels..).as_ptr());
let (r_values1, g_values1, b_values1) =
neon_vld_rgb_for_yuv::<ORIGIN_CHANNELS>(rgba1.get_unchecked(cx * channels..).as_ptr());
let r_high0 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(r_values0));
let g_high0 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(g_values0));
let b_high0 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(b_values0));
let r_high1 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(r_values1));
let g_high1 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(g_values1));
let b_high1 = vreinterpretq_s16_u16(vshll_high_n_u8::<V_SCALE>(b_values1));
let mut y_high0 = vqrdmlahq_laneq_s16::<0>(y_bias, r_high0, v_weights);
let mut y_high1 = vqrdmlahq_laneq_s16::<0>(y_bias, r_high1, v_weights);
y_high0 = vqrdmlahq_laneq_s16::<1>(y_high0, g_high0, v_weights);
y_high1 = vqrdmlahq_laneq_s16::<1>(y_high1, g_high1, v_weights);
y_high0 = vqrdmlahq_laneq_s16::<2>(y_high0, b_high0, v_weights);
y_high1 = vqrdmlahq_laneq_s16::<2>(y_high1, b_high1, v_weights);
let y0_high = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_high0));
let y1_high = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_high1));
let r_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(r_values0)));
let g_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(g_values0)));
let b_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(b_values0)));
let r_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(r_values1)));
let g_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(g_values1)));
let b_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(vget_low_u8(b_values1)));
let mut y_low0 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low0, v_weights);
let mut y_low1 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<1>(y_low0, g_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<1>(y_low1, g_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<2>(y_low0, b_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<2>(y_low1, b_low1, v_weights);
let y0_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low0));
let y1_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low1));
let y0 = vcombine_u8(y0_low, y0_high);
vst1q_u8(y_plane0.get_unchecked_mut(cx..).as_mut_ptr(), y0);
let y1 = vcombine_u8(y1_low, y1_high);
vst1q_u8(y_plane1.get_unchecked_mut(cx..).as_mut_ptr(), y1);
let r1l = vpaddlq_u8(r_values0);
let r1h = vpaddlq_u8(r_values1);
let g1l = vpaddlq_u8(g_values0);
let g1h = vpaddlq_u8(g_values1);
let b1l = vpaddlq_u8(b_values0);
let b1h = vpaddlq_u8(b_values1);
let r1hv = vaddq_u16(r1l, r1h);
let g1hv = vaddq_u16(g1l, g1h);
let b1hv = vaddq_u16(b1l, b1h);
let r1 = vreinterpretq_s16_u16(vshlq_n_u16::<V_HALF_SCALE>(r1hv));
let g1 = vreinterpretq_s16_u16(vshlq_n_u16::<V_HALF_SCALE>(g1hv));
let b1 = vreinterpretq_s16_u16(vshlq_n_u16::<V_HALF_SCALE>(b1hv));
let mut cbl = vqrdmlahq_laneq_s16::<3>(uv_bias, r1, v_weights);
let mut crl = vqrdmlahq_laneq_s16::<6>(uv_bias, r1, v_weights);
cbl = vqrdmlahq_laneq_s16::<4>(cbl, g1, v_weights);
crl = vqrdmlahq_laneq_s16::<7>(crl, g1, v_weights);
cbl = vqrdmlahq_laneq_s16::<5>(cbl, b1, v_weights);
crl = vqrdmlahq_laneq_s16::<0>(crl, b1, v_cr_b);
let cb = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(cbl));
let cr = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(crl));
vst1_u8(u_ptr.get_unchecked_mut(ux..).as_mut_ptr(), cb);
vst1_u8(v_ptr.get_unchecked_mut(ux..).as_mut_ptr(), cr);
ux += 8;
cx += 16;
}
while cx + 8 < width {
let src0 = rgba0.get_unchecked(cx * channels..);
let src1 = rgba1.get_unchecked(cx * channels..);
let y_dst0 = y_plane0.get_unchecked_mut(cx..);
let y_dst1 = y_plane1.get_unchecked_mut(cx..);
let u_dst = u_ptr.get_unchecked_mut(ux..);
let v_dst = v_ptr.get_unchecked_mut(ux..);
let (r_values0, g_values0, b_values0) =
neon_vld_h_rgb_for_yuv::<ORIGIN_CHANNELS>(src0.as_ptr());
let (r_values1, g_values1, b_values1) =
neon_vld_h_rgb_for_yuv::<ORIGIN_CHANNELS>(src1.as_ptr());
let r_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(r_values0));
let g_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(g_values0));
let b_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(b_values0));
let r_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(r_values1));
let g_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(g_values1));
let b_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(b_values1));
let mut y_low0 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low0, v_weights);
let mut y_low1 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<1>(y_low0, g_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<1>(y_low1, g_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<2>(y_low0, b_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<2>(y_low1, b_low1, v_weights);
let y0_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low0));
let y1_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low1));
vst1_u8(y_dst0.as_mut_ptr(), y0_low);
vst1_u8(y_dst1.as_mut_ptr(), y1_low);
let r1l = vpaddl_u8(r_values0);
let r1h = vpaddl_u8(r_values1);
let g1l = vpaddl_u8(g_values0);
let g1h = vpaddl_u8(g_values1);
let b1l = vpaddl_u8(b_values0);
let b1h = vpaddl_u8(b_values1);
let r1hv = vadd_u16(r1l, r1h);
let g1hv = vadd_u16(g1l, g1h);
let b1hv = vadd_u16(b1l, b1h);
let r1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(r1hv));
let g1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(g1hv));
let b1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(b1hv));
let mut cbl = vqrdmlah_laneq_s16::<3>(vget_low_s16(uv_bias), r1, v_weights);
let mut crl = vqrdmlah_laneq_s16::<6>(vget_low_s16(uv_bias), r1, v_weights);
cbl = vqrdmlah_laneq_s16::<4>(cbl, g1, v_weights);
crl = vqrdmlah_laneq_s16::<7>(crl, g1, v_weights);
cbl = vqrdmlah_laneq_s16::<5>(cbl, b1, v_weights);
crl = vqrdmlah_laneq_s16::<0>(crl, b1, v_cr_b);
let cb = vqshrn_n_u16::<A_E>(vcombine_u16(
vreinterpret_u16_s16(cbl),
vreinterpret_u16_s16(cbl),
));
let cr = vqshrn_n_u16::<A_E>(vcombine_u16(
vreinterpret_u16_s16(crl),
vreinterpret_u16_s16(crl),
));
vst1_lane_u32::<0>(u_dst.as_mut_ptr() as *mut u32, vreinterpret_u32_u8(cb));
vst1_lane_u32::<0>(v_dst.as_mut_ptr() as *mut u32, vreinterpret_u32_u8(cr));
ux += 4;
cx += 8;
}
if cx < width {
let diff = width - cx;
assert!(diff <= 8);
let mut src_buffer0: [u8; 8 * 4] = [0; 8 * 4];
let mut src_buffer1: [u8; 8 * 4] = [0; 8 * 4];
let mut y_buffer0: [u8; 8] = [0; 8];
let mut y_buffer1: [u8; 8] = [0; 8];
let mut u_buffer: [u8; 8] = [0; 8];
let mut v_buffer: [u8; 8] = [0; 8];
if diff % 2 != 0 {
let lst = (width - 1) * channels;
let last_items0 = rgba0.get_unchecked(lst..(lst + channels));
let last_items1 = rgba1.get_unchecked(lst..(lst + channels));
let dvb = diff * channels;
let dst0 = src_buffer0.get_unchecked_mut(dvb..(dvb + channels));
let dst1 = src_buffer1.get_unchecked_mut(dvb..(dvb + channels));
for (dst, src) in dst0.iter_mut().zip(last_items0) {
*dst = *src;
}
for (dst, src) in dst1.iter_mut().zip(last_items1) {
*dst = *src;
}
}
std::ptr::copy_nonoverlapping(
rgba0.get_unchecked(cx * channels..).as_ptr(),
src_buffer0.as_mut_ptr().cast(),
diff * channels,
);
std::ptr::copy_nonoverlapping(
rgba1.get_unchecked(cx * channels..).as_ptr(),
src_buffer1.as_mut_ptr().cast(),
diff * channels,
);
let (r_values0, g_values0, b_values0) =
neon_vld_h_rgb_for_yuv::<ORIGIN_CHANNELS>(src_buffer0.as_ptr().cast());
let (r_values1, g_values1, b_values1) =
neon_vld_h_rgb_for_yuv::<ORIGIN_CHANNELS>(src_buffer1.as_ptr().cast());
let r_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(r_values0));
let g_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(g_values0));
let b_low0 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(b_values0));
let r_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(r_values1));
let g_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(g_values1));
let b_low1 = vreinterpretq_s16_u16(vshll_n_u8::<V_SCALE>(b_values1));
let mut y_low0 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low0, v_weights);
let mut y_low1 = vqrdmlahq_laneq_s16::<0>(y_bias, r_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<1>(y_low0, g_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<1>(y_low1, g_low1, v_weights);
y_low0 = vqrdmlahq_laneq_s16::<2>(y_low0, b_low0, v_weights);
y_low1 = vqrdmlahq_laneq_s16::<2>(y_low1, b_low1, v_weights);
let y0_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low0));
let y1_low = vqshrn_n_u16::<A_E>(vreinterpretq_u16_s16(y_low1));
vst1_u8(y_buffer0.as_mut_ptr().cast(), y0_low);
vst1_u8(y_buffer1.as_mut_ptr().cast(), y1_low);
let r1l = vpaddl_u8(r_values0);
let r1h = vpaddl_u8(r_values1);
let g1l = vpaddl_u8(g_values0);
let g1h = vpaddl_u8(g_values1);
let b1l = vpaddl_u8(b_values0);
let b1h = vpaddl_u8(b_values1);
let r1hv = vadd_u16(r1l, r1h);
let g1hv = vadd_u16(g1l, g1h);
let b1hv = vadd_u16(b1l, b1h);
let r1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(r1hv));
let g1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(g1hv));
let b1 = vreinterpret_s16_u16(vshl_n_u16::<V_HALF_SCALE>(b1hv));
let mut cbl = vqrdmlah_laneq_s16::<3>(vget_low_s16(uv_bias), r1, v_weights);
let mut crl = vqrdmlah_laneq_s16::<6>(vget_low_s16(uv_bias), r1, v_weights);
cbl = vqrdmlah_laneq_s16::<4>(cbl, g1, v_weights);
crl = vqrdmlah_laneq_s16::<7>(crl, g1, v_weights);
cbl = vqrdmlah_laneq_s16::<5>(cbl, b1, v_weights);
crl = vqrdmlah_laneq_s16::<0>(crl, b1, v_cr_b);
let cb = vqshrn_n_u16::<A_E>(vcombine_u16(
vreinterpret_u16_s16(cbl),
vreinterpret_u16_s16(cbl),
));
let cr = vqshrn_n_u16::<A_E>(vcombine_u16(
vreinterpret_u16_s16(crl),
vreinterpret_u16_s16(crl),
));
vst1_lane_u32::<0>(u_buffer.as_mut_ptr() as *mut u32, vreinterpret_u32_u8(cb));
vst1_lane_u32::<0>(v_buffer.as_mut_ptr() as *mut u32, vreinterpret_u32_u8(cr));
let y_dst_0 = y_plane0.get_unchecked_mut(cx..);
std::ptr::copy_nonoverlapping(y_buffer0.as_ptr().cast(), y_dst_0.as_mut_ptr(), diff);
let y_dst_1 = y_plane1.get_unchecked_mut(cx..);
std::ptr::copy_nonoverlapping(y_buffer1.as_ptr().cast(), y_dst_1.as_mut_ptr(), diff);
cx += diff;
let hv = diff.div_ceil(2);
std::ptr::copy_nonoverlapping(
u_buffer.as_ptr().cast(),
u_ptr.get_unchecked_mut(ux..).as_mut_ptr(),
hv,
);
std::ptr::copy_nonoverlapping(
v_buffer.as_ptr().cast(),
v_ptr.get_unchecked_mut(ux..).as_mut_ptr(),
hv,
);
ux += hv;
}
ProcessedOffset { cx, ux }
}
#[inline(always)]
unsafe fn encode_16_part<const ORIGIN_CHANNELS: u8, const PRECISION: i32>(
rgba0: &[u8],
rgba1: &[u8],
y_plane0: &mut [u8],
y_plane1: &mut [u8],
u_plane: &mut [u8],
v_plane: &mut [u8],
y_bias: int32x4_t,
uv_bias: int32x4_t,
v_weights: int16x8_t,
v_cr_b: int16x8_t,
) {
let (r_values0, g_values0, b_values0) = neon_vld_rgb_for_yuv::<ORIGIN_CHANNELS>(rgba0.as_ptr());
let (r_values1, g_values1, b_values1) = neon_vld_rgb_for_yuv::<ORIGIN_CHANNELS>(rgba1.as_ptr());
let r_high0 = vreinterpretq_s16_u16(vmovl_high_u8(r_values0));
let g_high0 = vreinterpretq_s16_u16(vmovl_high_u8(g_values0));
let b_high0 = vreinterpretq_s16_u16(vmovl_high_u8(b_values0));
let r_h_low0 = vget_low_s16(r_high0);
let g_h_low0 = vget_low_s16(g_high0);
let b_h_low0 = vget_low_s16(b_high0);
let mut y0_h_high = vmlal_high_laneq_s16::<0>(y_bias, r_high0, v_weights);
let mut y0_h_low = vmlal_laneq_s16::<0>(y_bias, r_h_low0, v_weights);
y0_h_high = vmlal_high_laneq_s16::<1>(y0_h_high, g_high0, v_weights);
y0_h_low = vmlal_laneq_s16::<1>(y0_h_low, g_h_low0, v_weights);
y0_h_high = vmlal_high_laneq_s16::<2>(y0_h_high, b_high0, v_weights);
y0_h_low = vmlal_laneq_s16::<2>(y0_h_low, b_h_low0, v_weights);
let y0_high = vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(y0_h_low),
vshrn_n_s32::<PRECISION>(y0_h_high),
));
let r_high1 = vreinterpretq_s16_u16(vmovl_high_u8(r_values1));
let g_high1 = vreinterpretq_s16_u16(vmovl_high_u8(g_values1));
let b_high1 = vreinterpretq_s16_u16(vmovl_high_u8(b_values1));
let r_h_low1 = vget_low_s16(r_high1);
let g_h_low1 = vget_low_s16(g_high1);
let b_h_low1 = vget_low_s16(b_high1);
let mut y1_h_high = vmlal_high_laneq_s16::<0>(y_bias, r_high1, v_weights);
let mut y1_h_low = vmlal_laneq_s16::<0>(y_bias, r_h_low1, v_weights);
y1_h_high = vmlal_high_laneq_s16::<1>(y1_h_high, g_high1, v_weights);
y1_h_low = vmlal_laneq_s16::<1>(y1_h_low, g_h_low1, v_weights);
y1_h_high = vmlal_high_laneq_s16::<2>(y1_h_high, b_high1, v_weights);
y1_h_low = vmlal_laneq_s16::<2>(y1_h_low, b_h_low1, v_weights);
let y1_high = vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(y1_h_low),
vshrn_n_s32::<PRECISION>(y1_h_high),
));
let r_low0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_values0)));
let g_low0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(g_values0)));
let b_low0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_values0)));
let r_l_low0 = vget_low_s16(r_low0);
let g_l_low0 = vget_low_s16(g_low0);
let b_l_low0 = vget_low_s16(b_low0);
let mut y0_l_high = vmlal_high_laneq_s16::<0>(y_bias, r_low0, v_weights);
let mut y0_l_low = vmlal_laneq_s16::<0>(y_bias, r_l_low0, v_weights);
y0_l_high = vmlal_high_laneq_s16::<1>(y0_l_high, g_low0, v_weights);
y0_l_low = vmlal_laneq_s16::<1>(y0_l_low, g_l_low0, v_weights);
y0_l_high = vmlal_high_laneq_s16::<2>(y0_l_high, b_low0, v_weights);
y0_l_low = vmlal_laneq_s16::<2>(y0_l_low, b_l_low0, v_weights);
let y0_low = vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(y0_l_low),
vshrn_n_s32::<PRECISION>(y0_l_high),
));
let r_low1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_values1)));
let g_low1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(g_values1)));
let b_low1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_values1)));
let r_l_low1 = vget_low_s16(r_low1);
let g_l_low1 = vget_low_s16(g_low1);
let b_l_low1 = vget_low_s16(b_low1);
let mut y1_l_high = vmlal_high_laneq_s16::<0>(y_bias, r_low1, v_weights);
let mut y1_l_low = vmlal_laneq_s16::<0>(y_bias, r_l_low1, v_weights);
y1_l_high = vmlal_high_laneq_s16::<1>(y1_l_high, g_low1, v_weights);
y1_l_low = vmlal_laneq_s16::<1>(y1_l_low, g_l_low1, v_weights);
y1_l_high = vmlal_high_laneq_s16::<2>(y1_l_high, b_low1, v_weights);
y1_l_low = vmlal_laneq_s16::<2>(y1_l_low, b_l_low1, v_weights);
let y1_low = vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(y1_l_low),
vshrn_n_s32::<PRECISION>(y1_l_high),
));
let y00q = vmovn_u16(y0_low);
let y01q = vmovn_u16(y0_high);
let y10q = vmovn_u16(y1_low);
let y11q = vmovn_u16(y1_high);
let y0 = vcombine_u8(y00q, y01q);
let y1 = vcombine_u8(y10q, y11q);
vst1q_u8(y_plane0.as_mut_ptr(), y0);
vst1q_u8(y_plane1.as_mut_ptr(), y1);
let r1l = vpaddlq_u8(r_values0);
let r1h = vpaddlq_u8(r_values1);
let g1l = vpaddlq_u8(g_values0);
let g1h = vpaddlq_u8(g_values1);
let b1l = vpaddlq_u8(b_values0);
let b1h = vpaddlq_u8(b_values1);
let r1hv = vrhaddq_u16(r1l, r1h);
let g1hv = vrhaddq_u16(g1l, g1h);
let b1hv = vrhaddq_u16(b1l, b1h);
let r1 = vreinterpretq_s16_u16(vrshrq_n_u16::<1>(r1hv));
let g1 = vreinterpretq_s16_u16(vrshrq_n_u16::<1>(g1hv));
let b1 = vreinterpretq_s16_u16(vrshrq_n_u16::<1>(b1hv));
let mut cb_h = vmlal_high_laneq_s16::<3>(uv_bias, r1, v_weights);
let mut cb_l = vmlal_laneq_s16::<3>(uv_bias, vget_low_s16(r1), v_weights);
let mut cr_h = vmlal_high_laneq_s16::<6>(uv_bias, r1, v_weights);
let mut cr_l = vmlal_laneq_s16::<6>(uv_bias, vget_low_s16(r1), v_weights);
cb_h = vmlal_high_laneq_s16::<4>(cb_h, g1, v_weights);
cb_l = vmlal_laneq_s16::<4>(cb_l, vget_low_s16(g1), v_weights);
cr_h = vmlal_high_laneq_s16::<7>(cr_h, g1, v_weights);
cr_l = vmlal_laneq_s16::<7>(cr_l, vget_low_s16(g1), v_weights);
cb_h = vmlal_high_laneq_s16::<5>(cb_h, b1, v_weights);
cb_l = vmlal_laneq_s16::<5>(cb_l, vget_low_s16(b1), v_weights);
cr_h = vmlal_high_laneq_s16::<0>(cr_h, b1, v_cr_b);
cr_l = vmlal_laneq_s16::<0>(cr_l, vget_low_s16(b1), v_cr_b);
let cb = vmovn_u16(vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(cb_l),
vshrn_n_s32::<PRECISION>(cb_h),
)));
let cr = vmovn_u16(vreinterpretq_u16_s16(vcombine_s16(
vshrn_n_s32::<PRECISION>(cr_l),
vshrn_n_s32::<PRECISION>(cr_h),
)));
vst1_u8(u_plane.as_mut_ptr(), cb);
vst1_u8(v_plane.as_mut_ptr(), cr);
}
pub(crate) unsafe fn neon_rgba_to_yuv420<const ORIGIN_CHANNELS: u8>(
transform: &CbCrForwardTransform<i32>,
range: &YuvChromaRange,
y_plane0: &mut [u8],
y_plane1: &mut [u8],
u_plane: &mut [u8],
v_plane: &mut [u8],
rgba0: &[u8],
rgba1: &[u8],
start_cx: usize,
start_ux: usize,
width: usize,
) -> ProcessedOffset {
let source_channels: YuvSourceChannels = ORIGIN_CHANNELS.into();
let channels = source_channels.get_channels_count();
const PRECISION: i32 = 13;
let rounding_const_bias: i32 = (1 << (PRECISION - 1)) - 1;
let bias_y = range.bias_y as i32 * (1 << PRECISION) + rounding_const_bias;
let bias_uv = range.bias_uv as i32 * (1 << PRECISION) + rounding_const_bias;
let u_ptr = u_plane;
let v_ptr = v_plane;
let y_bias = vdupq_n_s32(bias_y);
let uv_bias = vdupq_n_s32(bias_uv);
let weights_arr: [i16; 8] = [
transform.yr as i16,
transform.yg as i16,
transform.yb as i16,
transform.cb_r as i16,
transform.cb_g as i16,
transform.cb_b as i16,
transform.cr_r as i16,
transform.cr_g as i16,
];
let v_weights = vld1q_s16(weights_arr.as_ptr());
let v_cr_b = vdupq_n_s16(transform.cr_b as i16);
let mut cx = start_cx;
let mut ux = start_ux;
while cx + 16 < width {
encode_16_part::<ORIGIN_CHANNELS, PRECISION>(
rgba0.get_unchecked(cx * channels..),
rgba1.get_unchecked(cx * channels..),
y_plane0.get_unchecked_mut(cx..),
y_plane1.get_unchecked_mut(cx..),
u_ptr.get_unchecked_mut(ux..),
v_ptr.get_unchecked_mut(ux..),
y_bias,
uv_bias,
v_weights,
v_cr_b,
);
ux += 8;
cx += 16;
}
if cx < width {
let diff = width - cx;
assert!(diff <= 16);
let mut src_buffer0: [u8; 16 * 4] = [0; 16 * 4];
let mut src_buffer1: [u8; 16 * 4] = [0; 16 * 4];
let mut y_buffer0: [u8; 16] = [0; 16];
let mut y_buffer1: [u8; 16] = [0; 16];
let mut u_buffer: [u8; 16] = [0; 16];
let mut v_buffer: [u8; 16] = [0; 16];
if diff % 2 != 0 {
let lst = (width - 1) * channels;
let last_items0 = rgba0.get_unchecked(lst..(lst + channels));
let last_items1 = rgba1.get_unchecked(lst..(lst + channels));
let dvb = diff * channels;
let dst0 = src_buffer0.get_unchecked_mut(dvb..(dvb + channels));
let dst1 = src_buffer1.get_unchecked_mut(dvb..(dvb + channels));
for (dst, src) in dst0.iter_mut().zip(last_items0) {
*dst = *src;
}
for (dst, src) in dst1.iter_mut().zip(last_items1) {
*dst = *src;
}
}
std::ptr::copy_nonoverlapping(
rgba0.get_unchecked(cx * channels..).as_ptr(),
src_buffer0.as_mut_ptr().cast(),
diff * channels,
);
std::ptr::copy_nonoverlapping(
rgba1.get_unchecked(cx * channels..).as_ptr(),
src_buffer1.as_mut_ptr().cast(),
diff * channels,
);
encode_16_part::<ORIGIN_CHANNELS, PRECISION>(
src_buffer0.as_slice(),
src_buffer1.as_slice(),
y_buffer0.as_mut_slice(),
y_buffer1.as_mut_slice(),
u_buffer.as_mut_slice(),
v_buffer.as_mut_slice(),
y_bias,
uv_bias,
v_weights,
v_cr_b,
);
let y_dst_0 = y_plane0.get_unchecked_mut(cx..);
std::ptr::copy_nonoverlapping(y_buffer0.as_ptr().cast(), y_dst_0.as_mut_ptr(), diff);
let y_dst_1 = y_plane1.get_unchecked_mut(cx..);
std::ptr::copy_nonoverlapping(y_buffer1.as_ptr().cast(), y_dst_1.as_mut_ptr(), diff);
cx += diff;
let hv = diff.div_ceil(2);
std::ptr::copy_nonoverlapping(
u_buffer.as_ptr().cast(),
u_ptr.get_unchecked_mut(ux..).as_mut_ptr(),
hv,
);
std::ptr::copy_nonoverlapping(
v_buffer.as_ptr().cast(),
v_ptr.get_unchecked_mut(ux..).as_mut_ptr(),
hv,
);
ux += hv;
}
ProcessedOffset { cx, ux }
}