use core::arch::x86_64::*;
use super::*;
use crate::{ColorMatrix, row::scalar};
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn deinterleave_vuya(ptr: *const u8) -> (__m128i, __m128i, __m128i, __m128i) {
unsafe {
let raw0 = _mm_loadu_si128(ptr.cast()); let raw1 = _mm_loadu_si128(ptr.add(16).cast()); let raw2 = _mm_loadu_si128(ptr.add(32).cast()); let raw3 = _mm_loadu_si128(ptr.add(48).cast());
let v_mask = _mm_setr_epi8(0, 4, 8, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let u_mask = _mm_setr_epi8(1, 5, 9, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let y_mask = _mm_setr_epi8(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let a_mask = _mm_setr_epi8(3, 7, 11, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let v0 = _mm_shuffle_epi8(raw0, v_mask); let v1 = _mm_shuffle_epi8(raw1, v_mask); let v2 = _mm_shuffle_epi8(raw2, v_mask); let v3 = _mm_shuffle_epi8(raw3, v_mask);
let u0 = _mm_shuffle_epi8(raw0, u_mask);
let u1 = _mm_shuffle_epi8(raw1, u_mask);
let u2 = _mm_shuffle_epi8(raw2, u_mask);
let u3 = _mm_shuffle_epi8(raw3, u_mask);
let y0 = _mm_shuffle_epi8(raw0, y_mask);
let y1 = _mm_shuffle_epi8(raw1, y_mask);
let y2 = _mm_shuffle_epi8(raw2, y_mask);
let y3 = _mm_shuffle_epi8(raw3, y_mask);
let a0 = _mm_shuffle_epi8(raw0, a_mask);
let a1 = _mm_shuffle_epi8(raw1, a_mask);
let a2 = _mm_shuffle_epi8(raw2, a_mask);
let a3 = _mm_shuffle_epi8(raw3, a_mask);
let v_01 = _mm_unpacklo_epi32(v0, v1); let v_23 = _mm_unpacklo_epi32(v2, v3); let u_01 = _mm_unpacklo_epi32(u0, u1);
let u_23 = _mm_unpacklo_epi32(u2, u3);
let y_01 = _mm_unpacklo_epi32(y0, y1);
let y_23 = _mm_unpacklo_epi32(y2, y3);
let a_01 = _mm_unpacklo_epi32(a0, a1);
let a_23 = _mm_unpacklo_epi32(a2, a3);
let v_vec = _mm_unpacklo_epi64(v_01, v_23); let u_vec = _mm_unpacklo_epi64(u_01, u_23); let y_vec = _mm_unpacklo_epi64(y_01, y_23); let a_vec = _mm_unpacklo_epi64(a_01, a_23);
(v_vec, u_vec, y_vec, a_vec)
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuya_to_rgb_or_rgba_row<const ALPHA: bool, const ALPHA_SRC: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
const { assert!(!ALPHA_SRC || ALPHA) };
debug_assert!(packed.len() >= width * 4, "packed row too short");
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp, "out row too short");
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
let bias = scalar::chroma_bias::<8>();
const RND: i32 = 1 << 14;
unsafe {
let rnd_v = _mm_set1_epi32(RND);
let y_off_v = _mm_set1_epi16(y_off as i16);
let y_scale_v = _mm_set1_epi32(y_scale);
let c_scale_v = _mm_set1_epi32(c_scale);
let bias_v = _mm_set1_epi16(bias as i16);
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(-1i8);
let mut x = 0usize;
while x + 16 <= width {
let (v_u8, u_u8, y_u8, a_u8) = deinterleave_vuya(packed.as_ptr().add(x * 4));
let v_lo_i16 = _mm_cvtepu8_epi16(v_u8);
let v_hi_i16 = _mm_cvtepu8_epi16(_mm_srli_si128::<8>(v_u8));
let u_lo_i16 = _mm_cvtepu8_epi16(u_u8);
let u_hi_i16 = _mm_cvtepu8_epi16(_mm_srli_si128::<8>(u_u8));
let y_lo_i16 = _mm_cvtepu8_epi16(y_u8);
let y_hi_i16 = _mm_cvtepu8_epi16(_mm_srli_si128::<8>(y_u8));
let u_sub_lo = _mm_sub_epi16(u_lo_i16, bias_v);
let u_sub_hi = _mm_sub_epi16(u_hi_i16, bias_v);
let v_sub_lo = _mm_sub_epi16(v_lo_i16, bias_v);
let v_sub_hi = _mm_sub_epi16(v_hi_i16, bias_v);
let u_lo_a = _mm_cvtepi16_epi32(u_sub_lo);
let u_lo_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_sub_lo));
let v_lo_a = _mm_cvtepi16_epi32(v_sub_lo);
let v_lo_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_sub_lo));
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 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 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 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 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 u_hi_a = _mm_cvtepi16_epi32(u_sub_hi);
let u_hi_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(u_sub_hi));
let v_hi_a = _mm_cvtepi16_epi32(v_sub_hi);
let v_hi_b = _mm_cvtepi16_epi32(_mm_srli_si128::<8>(v_sub_hi));
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_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_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_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_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(y_lo_i16, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y(y_hi_i16, 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);
let out_ptr = out.as_mut_ptr().add(x * bpp);
if ALPHA {
let a_vec = if ALPHA_SRC { a_u8 } else { alpha_u8 };
write_rgba_16(r_u8, g_u8, b_u8, a_vec, out_ptr);
} else {
write_rgb_16(r_u8, g_u8, b_u8, out_ptr);
}
x += 16;
}
if x < width {
scalar::vuya_to_rgb_or_rgba_row::<ALPHA, ALPHA_SRC>(
&packed[x * 4..],
&mut out[x * bpp..],
width - x,
matrix,
full_range,
);
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuya_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<false, false>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuya_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, true>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuyx_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
vuya_to_rgb_or_rgba_row::<true, false>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuya_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(luma_out.len() >= width, "luma row too short");
unsafe {
let y_mask = _mm_setr_epi8(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let raw0 = _mm_loadu_si128(packed.as_ptr().add(x * 4).cast());
let raw1 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 16).cast());
let raw2 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 32).cast());
let raw3 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 48).cast());
let y0 = _mm_shuffle_epi8(raw0, y_mask);
let y1 = _mm_shuffle_epi8(raw1, y_mask);
let y2 = _mm_shuffle_epi8(raw2, y_mask);
let y3 = _mm_shuffle_epi8(raw3, y_mask);
let y_01 = _mm_unpacklo_epi32(y0, y1);
let y_23 = _mm_unpacklo_epi32(y2, y3);
let y_vec = _mm_unpacklo_epi64(y_01, y_23);
_mm_storeu_si128(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 16;
}
if x < width {
scalar::vuya_to_luma_row(&packed[x * 4..], &mut luma_out[x..], width - x);
}
}
}
#[cfg_attr(not(any(feature = "std", feature = "alloc")), allow(dead_code))]
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuya_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
debug_assert!(packed.len() >= width * 4, "packed row too short");
debug_assert!(out.len() >= width, "out too short");
unsafe {
let y_mask = _mm_setr_epi8(2, 6, 10, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let raw0 = _mm_loadu_si128(packed.as_ptr().add(x * 4).cast());
let raw1 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 16).cast());
let raw2 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 32).cast());
let raw3 = _mm_loadu_si128(packed.as_ptr().add(x * 4 + 48).cast());
let y0 = _mm_shuffle_epi8(raw0, y_mask);
let y1 = _mm_shuffle_epi8(raw1, y_mask);
let y2 = _mm_shuffle_epi8(raw2, y_mask);
let y3 = _mm_shuffle_epi8(raw3, y_mask);
let y_01 = _mm_unpacklo_epi32(y0, y1); let y_23 = _mm_unpacklo_epi32(y2, y3); let y_vec = _mm_unpacklo_epi64(y_01, y_23);
let lo_u16 = _mm_cvtepu8_epi16(y_vec);
let hi_u16 = _mm_cvtepu8_epi16(_mm_srli_si128::<8>(y_vec));
_mm_storeu_si128(out.as_mut_ptr().add(x).cast(), lo_u16);
_mm_storeu_si128(out.as_mut_ptr().add(x + 8).cast(), hi_u16);
x += 16;
}
if x < width {
scalar::vuya_to_luma_u16_row(&packed[x * 4..], &mut out[x..], width - x);
}
}
}
#[allow(dead_code)]
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn vuyx_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
unsafe {
vuya_to_luma_u16_row(packed, out, width);
}
}