use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn uyyvyy411_to_rgb_row(
packed: &[u8],
rgb_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
uyyvyy411_to_rgb_or_rgba_row::<false>(packed, rgb_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn uyyvyy411_to_rgba_row(
packed: &[u8],
rgba_out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
unsafe {
uyyvyy411_to_rgb_or_rgba_row::<true>(packed, rgba_out, width, matrix, full_range);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn uyyvyy411_to_rgb_or_rgba_row<const ALPHA: bool>(
packed: &[u8],
out: &mut [u8],
width: usize,
matrix: ColorMatrix,
full_range: bool,
) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(out.len() >= width * bpp);
let coeffs = scalar::Coefficients::for_matrix(matrix);
let (y_off, y_scale, c_scale) = scalar::range_params_n::<8, 8>(full_range);
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 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 y_mask_p0 = _mm_setr_epi8(1, 2, 4, 5, 7, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1);
let y_mask_p1 = _mm_setr_epi8(5, 6, 8, 9, 11, 12, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let uv_mask_p0 = _mm_setr_epi8(0, 6, 12, -1, 3, 9, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1);
let uv_mask_p1 = _mm_setr_epi8(
-1, -1, -1, 10, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1,
);
let dup_lo_mask = _mm_setr_epi8(0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3);
let dup_hi_mask = _mm_setr_epi8(4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6; let p0 = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p1 = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let y_p0 = _mm_shuffle_epi8(p0, y_mask_p0);
let y_p1 = _mm_shuffle_epi8(p1, y_mask_p1);
let y_vec = _mm_unpacklo_epi64(y_p0, y_p1);
let uv_p0 = _mm_shuffle_epi8(p0, uv_mask_p0);
let uv_p1 = _mm_shuffle_epi8(p1, uv_mask_p1);
let uv = _mm_or_si128(uv_p0, uv_p1);
let u_packed = uv;
let v_packed = _mm_srli_si128::<4>(uv);
let u_i32 = _mm_sub_epi32(_mm_cvtepu8_epi32(u_packed), _mm_set1_epi32(128));
let v_i32 = _mm_sub_epi32(_mm_cvtepu8_epi32(v_packed), _mm_set1_epi32(128));
let u_d = q15_shift(_mm_add_epi32(_mm_mullo_epi32(u_i32, c_scale_v), rnd_v));
let v_d = q15_shift(_mm_add_epi32(_mm_mullo_epi32(v_i32, c_scale_v), rnd_v));
let r_lo = _mm_srai_epi32::<15>(_mm_add_epi32(
_mm_add_epi32(_mm_mullo_epi32(cru, u_d), _mm_mullo_epi32(crv, v_d)),
rnd_v,
));
let g_lo = _mm_srai_epi32::<15>(_mm_add_epi32(
_mm_add_epi32(_mm_mullo_epi32(cgu, u_d), _mm_mullo_epi32(cgv, v_d)),
rnd_v,
));
let b_lo = _mm_srai_epi32::<15>(_mm_add_epi32(
_mm_add_epi32(_mm_mullo_epi32(cbu, u_d), _mm_mullo_epi32(cbv, v_d)),
rnd_v,
));
let r_chroma = _mm_packs_epi32(r_lo, r_lo);
let g_chroma = _mm_packs_epi32(g_lo, g_lo);
let b_chroma = _mm_packs_epi32(b_lo, b_lo);
let r_dup_lo = _mm_shuffle_epi8(r_chroma, dup_lo_mask);
let r_dup_hi = _mm_shuffle_epi8(r_chroma, dup_hi_mask);
let g_dup_lo = _mm_shuffle_epi8(g_chroma, dup_lo_mask);
let g_dup_hi = _mm_shuffle_epi8(g_chroma, dup_hi_mask);
let b_dup_lo = _mm_shuffle_epi8(b_chroma, dup_lo_mask);
let b_dup_hi = _mm_shuffle_epi8(b_chroma, dup_hi_mask);
let y_low_i16 = _mm_cvtepu8_epi16(y_vec);
let y_high_i16 = _mm_cvtepu8_epi16(_mm_srli_si128::<8>(y_vec));
let y_scaled_lo = scale_y(y_low_i16, y_off_v, y_scale_v, rnd_v);
let y_scaled_hi = scale_y(y_high_i16, y_off_v, y_scale_v, rnd_v);
let b_lo16 = _mm_adds_epi16(y_scaled_lo, b_dup_lo);
let b_hi16 = _mm_adds_epi16(y_scaled_hi, b_dup_hi);
let g_lo16 = _mm_adds_epi16(y_scaled_lo, g_dup_lo);
let g_hi16 = _mm_adds_epi16(y_scaled_hi, g_dup_hi);
let r_lo16 = _mm_adds_epi16(y_scaled_lo, r_dup_lo);
let r_hi16 = _mm_adds_epi16(y_scaled_hi, r_dup_hi);
let b_u8 = _mm_packus_epi16(b_lo16, b_hi16);
let g_u8 = _mm_packus_epi16(g_lo16, g_hi16);
let r_u8 = _mm_packus_epi16(r_lo16, r_hi16);
if ALPHA {
write_rgba_16(r_u8, g_u8, b_u8, alpha_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_block = (x / 4) * 6;
let tail_packed = &packed[tail_block..(width / 4) * 6];
let tail_out = &mut out[x * bpp..width * bpp];
let tail_w = width - x;
if ALPHA {
scalar::uyyvyy411_to_rgba_row(tail_packed, tail_out, tail_w, matrix, full_range);
} else {
scalar::uyyvyy411_to_rgb_row(tail_packed, tail_out, tail_w, matrix, full_range);
}
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn uyyvyy411_to_luma_row(packed: &[u8], luma_out: &mut [u8], width: usize) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
debug_assert!(luma_out.len() >= width);
unsafe {
let y_mask_p0 = _mm_setr_epi8(1, 2, 4, 5, 7, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1);
let y_mask_p1 = _mm_setr_epi8(5, 6, 8, 9, 11, 12, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6;
let p0 = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p1 = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let y_p0 = _mm_shuffle_epi8(p0, y_mask_p0);
let y_p1 = _mm_shuffle_epi8(p1, y_mask_p1);
let y_vec = _mm_unpacklo_epi64(y_p0, y_p1);
_mm_storeu_si128(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 16;
}
if x < width {
let tail_block = (x / 4) * 6;
scalar::uyyvyy411_to_luma_row(
&packed[tail_block..(width / 4) * 6],
&mut luma_out[x..width],
width - x,
);
}
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
pub(crate) unsafe fn uyyvyy411_to_luma_u16_row(packed: &[u8], out: &mut [u16], width: usize) {
debug_assert_eq!(
width & 3,
0,
"packed YUV 4:1:1 requires width multiple of 4"
);
debug_assert!(packed.len() >= width * 3 / 2);
debug_assert!(out.len() >= width);
unsafe {
let y_mask_p0 = _mm_setr_epi8(1, 2, 4, 5, 7, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1);
let y_mask_p1 = _mm_setr_epi8(5, 6, 8, 9, 11, 12, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1);
let mut x = 0usize;
while x + 16 <= width {
let block = (x / 4) * 6;
let p0 = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p1 = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let y_p0 = _mm_shuffle_epi8(p0, y_mask_p0);
let y_p1 = _mm_shuffle_epi8(p1, y_mask_p1);
let lo = _mm_cvtepu8_epi16(y_p0);
let hi = _mm_cvtepu8_epi16(y_p1);
_mm_storeu_si128(out.as_mut_ptr().add(x).cast(), lo);
_mm_storeu_si128(out.as_mut_ptr().add(x + 8).cast(), hi);
x += 16;
}
if x < width {
let tail_block = (x / 4) * 6;
scalar::uyyvyy411_to_luma_u16_row(
&packed[tail_block..(width / 4) * 6],
&mut out[x..width],
width - x,
);
}
}
}