use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_444p16_to_rgb_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<false, false, BE>(
y, u, v, None, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_444p16_to_rgba_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<true, false, BE>(
y, u, v, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgba_with_alpha_src_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_row::<true, true, BE>(
y,
u,
v,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgb_or_rgba_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: Option<&[u16]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width);
debug_assert!(u.len() >= width);
debug_assert!(v.len() >= width);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 8>(full_range);
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi32(y_off);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias16_v = _mm_set1_epi16(-32768i16);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm_set1_epi8(-1);
let mut x = 0usize;
while x + 16 <= width {
let y_low = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let y_high = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x + 8) as *const u8);
let u_lo_vec = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x) as *const u8);
let u_hi_vec = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x + 8) as *const u8);
let v_lo_vec = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x) as *const u8);
let v_hi_vec = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x + 8) as *const u8);
let u_lo_i16 = _mm_sub_epi16(u_lo_vec, bias16_v);
let u_hi_i16 = _mm_sub_epi16(u_hi_vec, bias16_v);
let v_lo_i16 = _mm_sub_epi16(v_lo_vec, bias16_v);
let v_hi_i16 = _mm_sub_epi16(v_hi_vec, bias16_v);
let u_lo_a = _mm_cvtepi16_epi32(u_lo_i16);
let u_lo_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_lo_i16));
let u_hi_a = _mm_cvtepi16_epi32(u_hi_i16);
let u_hi_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_hi_i16));
let v_lo_a = _mm_cvtepi16_epi32(v_lo_i16);
let v_lo_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_lo_i16));
let v_hi_a = _mm_cvtepi16_epi32(v_hi_i16);
let v_hi_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_hi_i16));
let u_d_lo_a = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_a, c_scale_v), rnd_v));
let u_d_lo_b = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_b, c_scale_v), rnd_v));
let u_d_hi_a = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_a, c_scale_v), rnd_v));
let u_d_hi_b = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_b, c_scale_v), rnd_v));
let v_d_lo_a = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_a, c_scale_v), rnd_v));
let v_d_lo_b = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_b, c_scale_v), rnd_v));
let v_d_hi_a = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_a, c_scale_v), rnd_v));
let v_d_hi_b = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_b, c_scale_v), rnd_v));
let r_chroma_lo = chroma_i16x8(cru, crv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let r_chroma_hi = chroma_i16x8(cru, crv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let g_chroma_lo = chroma_i16x8(cgu, cgv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let g_chroma_hi = chroma_i16x8(cgu, cgv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let b_chroma_lo = chroma_i16x8(cbu, cbv, u_d_lo_a, v_d_lo_a, u_d_lo_b, v_d_lo_b, rnd_v);
let b_chroma_hi = chroma_i16x8(cbu, cbv, u_d_hi_a, v_d_hi_a, u_d_hi_b, v_d_hi_b, rnd_v);
let y_scaled_lo = scale_y_u16(y_low, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y_u16(y_high, y_off_v, y_scale_v, rnd_v);
let r_lo = _mm_adds_epi16(y_scaled_lo, r_chroma_lo);
let r_hi = _mm_adds_epi16(y_scaled_hi, r_chroma_hi);
let g_lo = _mm_adds_epi16(y_scaled_lo, g_chroma_lo);
let g_hi = _mm_adds_epi16(y_scaled_hi, g_chroma_hi);
let b_lo = _mm_adds_epi16(y_scaled_lo, b_chroma_lo);
let b_hi = _mm_adds_epi16(y_scaled_hi, b_chroma_hi);
let r_u8 = _mm_packus_epi16(r_lo, r_hi);
let g_u8 = _mm_packus_epi16(g_lo, g_hi);
let b_u8 = _mm_packus_epi16(b_lo, b_hi);
if ALPHA {
let a_u8 = if ALPHA_SRC {
let a_ptr = a_src.as_ref().unwrap_unchecked().as_ptr();
let a_lo =
_mm_srli_epi16::<8>(endian::load_endian_u16x8::<BE>(a_ptr.add(x) as *const u8));
let a_hi = _mm_srli_epi16::<8>(endian::load_endian_u16x8::<BE>(
a_ptr.add(x + 8) as *const u8
));
_mm_packus_epi16(a_lo, a_hi)
} else {
alpha_u8
};
write_rgba_16(r_u8, g_u8, b_u8, a_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_16(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 16;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u[x..width];
let tail_v = &v[x..width];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_444p16_to_rgba_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_444p16_to_rgba_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_444p16_to_rgb_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_444p16_to_rgb_u16_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<false, false, BE>(
y, u, v, None, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_444p16_to_rgba_u16_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<true, false, BE>(
y, u, v, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgba_u16_with_alpha_src_row<const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_444p16_to_rgb_or_rgba_u16_row::<true, true, BE>(
y,
u,
v,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_444p16_to_rgb_or_rgba_u16_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u: &[u16],
v: &[u16],
a_src: Option<&[u16]>,
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width);
debug_assert!(u.len() >= width);
debug_assert!(v.len() >= width);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 16>(full_range);
const RND: i64 = 1 << 14;
unsafe {
let alpha_u16 = _mm_set1_epi16(-1i16);
let rnd_v = _mm_set1_epi64x(RND);
let y_off_v = _mm_set1_epi32(y_off);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias16_v = _mm_set1_epi16(-32768i16);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
let mut x = 0usize;
while x + 8 <= width {
let y_vec = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let u_vec = endian::load_endian_u16x8::<BE>(u.as_ptr().add(x) as *const u8);
let v_vec = endian::load_endian_u16x8::<BE>(v.as_ptr().add(x) as *const u8);
let u_i16 = _mm_sub_epi16(u_vec, bias16_v);
let v_i16 = _mm_sub_epi16(v_vec, bias16_v);
let rnd32_v = _mm_set1_epi32(1 << 14);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd32_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd32_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd32_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_i32, c_scale_v), rnd32_v));
let u_d_lo_even = u_d_lo;
let u_d_lo_odd = _mm_shuffle_epi32::<0xF5>(u_d_lo);
let v_d_lo_even = v_d_lo;
let v_d_lo_odd = _mm_shuffle_epi32::<0xF5>(v_d_lo);
let u_d_hi_even = u_d_hi;
let u_d_hi_odd = _mm_shuffle_epi32::<0xF5>(u_d_hi);
let v_d_hi_even = v_d_hi;
let v_d_hi_odd = _mm_shuffle_epi32::<0xF5>(v_d_hi);
let r_ch_lo_even = chroma_i64x2(cru, crv, u_d_lo_even, v_d_lo_even, rnd_v);
let r_ch_lo_odd = chroma_i64x2(cru, crv, u_d_lo_odd, v_d_lo_odd, rnd_v);
let r_ch_hi_even = chroma_i64x2(cru, crv, u_d_hi_even, v_d_hi_even, rnd_v);
let r_ch_hi_odd = chroma_i64x2(cru, crv, u_d_hi_odd, v_d_hi_odd, rnd_v);
let g_ch_lo_even = chroma_i64x2(cgu, cgv, u_d_lo_even, v_d_lo_even, rnd_v);
let g_ch_lo_odd = chroma_i64x2(cgu, cgv, u_d_lo_odd, v_d_lo_odd, rnd_v);
let g_ch_hi_even = chroma_i64x2(cgu, cgv, u_d_hi_even, v_d_hi_even, rnd_v);
let g_ch_hi_odd = chroma_i64x2(cgu, cgv, u_d_hi_odd, v_d_hi_odd, rnd_v);
let b_ch_lo_even = chroma_i64x2(cbu, cbv, u_d_lo_even, v_d_lo_even, rnd_v);
let b_ch_lo_odd = chroma_i64x2(cbu, cbv, u_d_lo_odd, v_d_lo_odd, rnd_v);
let b_ch_hi_even = chroma_i64x2(cbu, cbv, u_d_hi_even, v_d_hi_even, rnd_v);
let b_ch_hi_odd = chroma_i64x2(cbu, cbv, u_d_hi_odd, v_d_hi_odd, rnd_v);
let r_ch_lo_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(r_ch_lo_even, r_ch_lo_odd),
_mm_unpackhi_epi32(r_ch_lo_even, r_ch_lo_odd),
);
let r_ch_hi_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(r_ch_hi_even, r_ch_hi_odd),
_mm_unpackhi_epi32(r_ch_hi_even, r_ch_hi_odd),
);
let g_ch_lo_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(g_ch_lo_even, g_ch_lo_odd),
_mm_unpackhi_epi32(g_ch_lo_even, g_ch_lo_odd),
);
let g_ch_hi_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(g_ch_hi_even, g_ch_hi_odd),
_mm_unpackhi_epi32(g_ch_hi_even, g_ch_hi_odd),
);
let b_ch_lo_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(b_ch_lo_even, b_ch_lo_odd),
_mm_unpackhi_epi32(b_ch_lo_even, b_ch_lo_odd),
);
let b_ch_hi_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(b_ch_hi_even, b_ch_hi_odd),
_mm_unpackhi_epi32(b_ch_hi_even, b_ch_hi_odd),
);
let y_lo_pair = _mm_cvtepu16_epi32(y_vec);
let y_hi_pair = _mm_cvtepu16_epi32(_mm_srli_si128::<8>(y_vec));
let y_lo_sub = _mm_sub_epi32(y_lo_pair, y_off_v);
let y_hi_sub = _mm_sub_epi32(y_hi_pair, y_off_v);
let y_lo_even = scale_y16_i64(y_lo_sub, y_scale_v, rnd_v);
let y_lo_odd = scale_y16_i64(_mm_shuffle_epi32::<0xF5>(y_lo_sub), y_scale_v, rnd_v);
let y_hi_even = scale_y16_i64(y_hi_sub, y_scale_v, rnd_v);
let y_hi_odd = scale_y16_i64(_mm_shuffle_epi32::<0xF5>(y_hi_sub), y_scale_v, rnd_v);
let y_lo_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(y_lo_even, y_lo_odd),
_mm_unpackhi_epi32(y_lo_even, y_lo_odd),
);
let y_hi_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(y_hi_even, y_hi_odd),
_mm_unpackhi_epi32(y_hi_even, y_hi_odd),
);
let r_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, r_ch_lo_i32),
_mm_add_epi32(y_hi_i32, r_ch_hi_i32),
);
let g_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, g_ch_lo_i32),
_mm_add_epi32(y_hi_i32, g_ch_hi_i32),
);
let b_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, b_ch_lo_i32),
_mm_add_epi32(y_hi_i32, b_ch_hi_i32),
);
if ALPHA {
let a_v = if ALPHA_SRC {
endian::load_endian_u16x8::<BE>(
a_src.as_ref().unwrap_unchecked().as_ptr().add(x) as *const u8
)
} else {
alpha_u16
};
write_rgba_u16_8(r_u16, g_u16, b_u16, a_v, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_u16_8(r_u16, g_u16, b_u16, out.as_mut_ptr().add(x * 3));
}
x += 8;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u[x..width];
let tail_v = &v[x..width];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_444p16_to_rgba_u16_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_444p16_to_rgba_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_444p16_to_rgb_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_420p16_to_rgb_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<false, false, BE>(
y, u_half, v_half, None, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_420p16_to_rgba_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<true, false, BE>(
y, u_half, v_half, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgba_with_alpha_src_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: &[u16],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_row::<true, true, BE>(
y,
u_half,
v_half,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgb_or_rgba_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: Option<&[u16]>,
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert_eq!(width & 1, 0);
debug_assert!(y.len() >= width);
debug_assert!(u_half.len() >= width / 2);
debug_assert!(v_half.len() >= width / 2);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 8>(full_range);
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi32(y_off);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias16_v = _mm_set1_epi16(-32768i16);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm_set1_epi8(-1);
let mut x = 0usize;
while x + 16 <= width {
let y_low = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let y_high = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x + 8) as *const u8);
let u_vec = endian::load_endian_u16x8::<BE>(u_half.as_ptr().add(x / 2) as *const u8);
let v_vec = endian::load_endian_u16x8::<BE>(v_half.as_ptr().add(x / 2) as *const u8);
let u_i16 = _mm_sub_epi16(u_vec, bias16_v);
let v_i16 = _mm_sub_epi16(v_vec, bias16_v);
let u_lo_i32 = _mm_cvtepi16_epi32(u_i16);
let u_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_i16));
let v_lo_i32 = _mm_cvtepi16_epi32(v_i16);
let v_hi_i32 = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_i16));
let u_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_lo_i32, c_scale_v), rnd_v));
let u_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_hi_i32, c_scale_v), rnd_v));
let v_d_lo = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_lo_i32, c_scale_v), rnd_v));
let v_d_hi = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_hi_i32, c_scale_v), rnd_v));
let r_chroma = chroma_i16x8(cru, crv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let g_chroma = chroma_i16x8(cgu, cgv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let b_chroma = chroma_i16x8(cbu, cbv, u_d_lo, v_d_lo, u_d_hi, v_d_hi, rnd_v);
let r_dup_lo = _mm_unpacklo_epi16(r_chroma, r_chroma);
let r_dup_hi = _mm_unpackhi_epi16(r_chroma, r_chroma);
let g_dup_lo = _mm_unpacklo_epi16(g_chroma, g_chroma);
let g_dup_hi = _mm_unpackhi_epi16(g_chroma, g_chroma);
let b_dup_lo = _mm_unpacklo_epi16(b_chroma, b_chroma);
let b_dup_hi = _mm_unpackhi_epi16(b_chroma, b_chroma);
let y_scaled_lo = scale_y_u16(y_low, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y_u16(y_high, y_off_v, y_scale_v, rnd_v);
let r_lo = _mm_adds_epi16(y_scaled_lo, r_dup_lo);
let r_hi = _mm_adds_epi16(y_scaled_hi, r_dup_hi);
let g_lo = _mm_adds_epi16(y_scaled_lo, g_dup_lo);
let g_hi = _mm_adds_epi16(y_scaled_hi, g_dup_hi);
let b_lo = _mm_adds_epi16(y_scaled_lo, b_dup_lo);
let b_hi = _mm_adds_epi16(y_scaled_hi, b_dup_hi);
let r_u8 = _mm_packus_epi16(r_lo, r_hi);
let g_u8 = _mm_packus_epi16(g_lo, g_hi);
let b_u8 = _mm_packus_epi16(b_lo, b_hi);
if ALPHA {
let a_u8 = if ALPHA_SRC {
let a_ptr = a_src.as_ref().unwrap_unchecked().as_ptr();
let a_lo =
_mm_srli_epi16::<8>(endian::load_endian_u16x8::<BE>(a_ptr.add(x) as *const u8));
let a_hi = _mm_srli_epi16::<8>(endian::load_endian_u16x8::<BE>(
a_ptr.add(x + 8) as *const u8
));
_mm_packus_epi16(a_lo, a_hi)
} else {
alpha_u8
};
write_rgba_16(r_u8, g_u8, b_u8, a_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_16(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 16;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u_half[x / 2..width / 2];
let tail_v = &v_half[x / 2..width / 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_420p16_to_rgba_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_420p16_to_rgba_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_420p16_to_rgb_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_420p16_to_rgb_u16_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgb_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<false, false, BE>(
y, u_half, v_half, None, rgb_out, width, matrix, full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn yuv_420p16_to_rgba_u16_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<true, false, BE>(
y, u_half, v_half, None, rgba_out, width, matrix, full_range,
);
}
}
#[cfg(feature = "yuva")]
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgba_u16_with_alpha_src_row<const BE: bool>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: &[u16],
rgba_out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
yuv_420p16_to_rgb_or_rgba_u16_row::<true, true, BE>(
y,
u_half,
v_half,
Some(a_src),
rgba_out,
width,
matrix,
full_range,
);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
#[allow(clippy::too_many_arguments)]
pub(crate) unsafe fn yuv_420p16_to_rgb_or_rgba_u16_row<
const ALPHA: bool,
const ALPHA_SRC: bool,
const BE: bool,
>(
y: &[u16],
u_half: &[u16],
v_half: &[u16],
a_src: Option<&[u16]>,
out: &mut [u16],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert_eq!(width & 1, 0);
debug_assert!(y.len() >= width);
debug_assert!(u_half.len() >= width / 2);
debug_assert!(v_half.len() >= width / 2);
debug_assert!(out.len() >= width * bpp);
if ALPHA_SRC {
debug_assert!(a_src.as_ref().is_some_and(|s| s.len() >= width));
}
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<16, 16>(full_range);
const RND: i64 = 1 << 14;
unsafe {
let alpha_u16 = _mm_set1_epi16(-1i16);
let rnd_v = _mm_set1_epi64x(RND);
let y_off_v = _mm_set1_epi32(y_off);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias16_v = _mm_set1_epi16(-32768i16);
let cru = _mm_set1_epi32(coeffs.r_u());
let crv = _mm_set1_epi32(coeffs.r_v());
let cgu = _mm_set1_epi32(coeffs.g_u());
let cgv = _mm_set1_epi32(coeffs.g_v());
let cbu = _mm_set1_epi32(coeffs.b_u());
let cbv = _mm_set1_epi32(coeffs.b_v());
let bswap_u16 = _mm_setr_epi8(
1, 0, 3, 2, 5, 4, 7, 6, -128, -128, -128, -128, -128, -128, -128, -128,
);
let mut x = 0usize;
while x + 8 <= width {
let y_vec = endian::load_endian_u16x8::<BE>(y.as_ptr().add(x) as *const u8);
let u_vec4 = _mm_loadl_epi64(u_half.as_ptr().add(x / 2).cast());
let v_vec4 = _mm_loadl_epi64(v_half.as_ptr().add(x / 2).cast());
let u_vec4 = if BE {
_mm_shuffle_epi8(u_vec4, bswap_u16)
} else {
u_vec4
};
let v_vec4 = if BE {
_mm_shuffle_epi8(v_vec4, bswap_u16)
} else {
v_vec4
};
let u_i16 = _mm_sub_epi16(u_vec4, bias16_v);
let v_i16 = _mm_sub_epi16(v_vec4, bias16_v);
let rnd32_v = _mm_set1_epi32(1 << 14);
let u_i32 = _mm_cvtepi16_epi32(u_i16);
let v_i32 = _mm_cvtepi16_epi32(v_i16);
let u_d = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_i32, c_scale_v), rnd32_v));
let v_d = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_i32, c_scale_v), rnd32_v));
let u_d_even = u_d; let v_d_even = v_d;
let u_d_odd = _mm_shuffle_epi32::<0xF5>(u_d); let v_d_odd = _mm_shuffle_epi32::<0xF5>(v_d);
let r_ch_even = chroma_i64x2(cru, crv, u_d_even, v_d_even, rnd_v);
let r_ch_odd = chroma_i64x2(cru, crv, u_d_odd, v_d_odd, rnd_v);
let g_ch_even = chroma_i64x2(cgu, cgv, u_d_even, v_d_even, rnd_v);
let g_ch_odd = chroma_i64x2(cgu, cgv, u_d_odd, v_d_odd, rnd_v);
let b_ch_even = chroma_i64x2(cbu, cbv, u_d_even, v_d_even, rnd_v);
let b_ch_odd = chroma_i64x2(cbu, cbv, u_d_odd, v_d_odd, rnd_v);
let r_ch_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(r_ch_even, r_ch_odd),
_mm_unpackhi_epi32(r_ch_even, r_ch_odd),
);
let g_ch_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(g_ch_even, g_ch_odd),
_mm_unpackhi_epi32(g_ch_even, g_ch_odd),
);
let b_ch_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(b_ch_even, b_ch_odd),
_mm_unpackhi_epi32(b_ch_even, b_ch_odd),
);
let r_dup_lo = _mm_unpacklo_epi32(r_ch_i32, r_ch_i32);
let r_dup_hi = _mm_unpackhi_epi32(r_ch_i32, r_ch_i32);
let g_dup_lo = _mm_unpacklo_epi32(g_ch_i32, g_ch_i32);
let g_dup_hi = _mm_unpackhi_epi32(g_ch_i32, g_ch_i32);
let b_dup_lo = _mm_unpacklo_epi32(b_ch_i32, b_ch_i32);
let b_dup_hi = _mm_unpackhi_epi32(b_ch_i32, b_ch_i32);
let y_lo_pair = _mm_cvtepu16_epi32(y_vec); let y_hi_pair = _mm_cvtepu16_epi32(_mm_srli_si128::<8>(y_vec));
let y_lo_sub = _mm_sub_epi32(y_lo_pair, y_off_v);
let y_hi_sub = _mm_sub_epi32(y_hi_pair, y_off_v);
let y_lo_even = scale_y16_i64(y_lo_sub, y_scale_v, rnd_v);
let y_lo_odd = scale_y16_i64(_mm_shuffle_epi32::<0xF5>(y_lo_sub), y_scale_v, rnd_v);
let y_hi_even = scale_y16_i64(y_hi_sub, y_scale_v, rnd_v);
let y_hi_odd = scale_y16_i64(_mm_shuffle_epi32::<0xF5>(y_hi_sub), y_scale_v, rnd_v);
let y_lo_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(y_lo_even, y_lo_odd),
_mm_unpackhi_epi32(y_lo_even, y_lo_odd),
);
let y_hi_i32 = _mm_unpacklo_epi64(
_mm_unpacklo_epi32(y_hi_even, y_hi_odd),
_mm_unpackhi_epi32(y_hi_even, y_hi_odd),
);
let r_lo_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, r_dup_lo),
_mm_add_epi32(y_hi_i32, r_dup_hi),
);
let g_lo_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, g_dup_lo),
_mm_add_epi32(y_hi_i32, g_dup_hi),
);
let b_lo_u16 = _mm_packus_epi32(
_mm_add_epi32(y_lo_i32, b_dup_lo),
_mm_add_epi32(y_hi_i32, b_dup_hi),
);
if ALPHA {
let a_v = if ALPHA_SRC {
endian::load_endian_u16x8::<BE>(
a_src.as_ref().unwrap_unchecked().as_ptr().add(x) as *const u8
)
} else {
alpha_u16
};
write_rgba_u16_8(
r_lo_u16,
g_lo_u16,
b_lo_u16,
a_v,
out.as_mut_ptr().add(x * 4),
);
} else {
write_rgb_u16_8(r_lo_u16, g_lo_u16, b_lo_u16, out.as_mut_ptr().add(x * 3));
}
x += 8;
}
if x < width {
let tail_y = &y[x..width];
let tail_u = &u_half[x / 2..width / 2];
let tail_v = &v_half[x / 2..width / 2];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA_SRC {
let tail_a = &a_src.as_ref().unwrap_unchecked()[x..width];
scalar::yuv_420p16_to_rgba_u16_with_alpha_src_row::<BE>(
tail_y, tail_u, tail_v, tail_a, tail_out, tail_w, matrix, full_range,
);
} else if ALPHA {
scalar::yuv_420p16_to_rgba_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
} else {
scalar::yuv_420p16_to_rgb_u16_row::<BE>(
tail_y, tail_u, tail_v, tail_out, tail_w, matrix, full_range,
);
}
}
}
}