use core::arch::x86_64::*;
use super::*;
#[inline]
#[target_feature(enable = "avx2")]
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 = "avx2")]
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 = "avx2")]
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 = _mm256_set1_epi32(RND);
let y_off_v = _mm256_set1_epi16(y_off as i16);
let y_scale_v = _mm256_set1_epi32(y_scale);
let c_scale_v = _mm256_set1_epi32(c_scale);
let cru = _mm256_set1_epi32(coeffs.r_u());
let crv = _mm256_set1_epi32(coeffs.r_v());
let cgu = _mm256_set1_epi32(coeffs.g_u());
let cgv = _mm256_set1_epi32(coeffs.g_v());
let cbu = _mm256_set1_epi32(coeffs.b_u());
let cbv = _mm256_set1_epi32(coeffs.b_v());
let alpha_u8 = _mm256_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_mask = _mm256_setr_epi8(
0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, );
let perm_low = _mm256_setr_epi32(0, -1, -1, -1, 1, -1, -1, -1);
let perm_high = _mm256_setr_epi32(2, -1, -1, -1, 3, -1, -1, -1);
let mut x = 0usize;
while x + 32 <= width {
let block = (x / 4) * 6;
let p0_a = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p0_b = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let p1_a = _mm_loadu_si128(packed.as_ptr().add(block + 24).cast());
let p1_b = _mm_loadu_si128(packed.as_ptr().add(block + 32).cast());
let y_w0 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p0_a, y_mask_p0),
_mm_shuffle_epi8(p0_b, y_mask_p1),
);
let y_w1 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p1_a, y_mask_p0),
_mm_shuffle_epi8(p1_b, y_mask_p1),
);
let y_vec = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(y_w0), y_w1);
let uv_w0 = _mm_or_si128(
_mm_shuffle_epi8(p0_a, uv_mask_p0),
_mm_shuffle_epi8(p0_b, uv_mask_p1),
);
let uv_w1 = _mm_or_si128(
_mm_shuffle_epi8(p1_a, uv_mask_p0),
_mm_shuffle_epi8(p1_b, uv_mask_p1),
);
let u_packed = _mm_unpacklo_epi32(uv_w0, uv_w1);
let v_packed = _mm_unpacklo_epi32(_mm_srli_si128::<4>(uv_w0), _mm_srli_si128::<4>(uv_w1));
let u_i32 = _mm256_sub_epi32(_mm256_cvtepu8_epi32(u_packed), _mm256_set1_epi32(128));
let v_i32 = _mm256_sub_epi32(_mm256_cvtepu8_epi32(v_packed), _mm256_set1_epi32(128));
let u_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(u_i32, c_scale_v),
rnd_v,
));
let v_d = q15_shift(_mm256_add_epi32(
_mm256_mullo_epi32(v_i32, c_scale_v),
rnd_v,
));
let r_i32 = _mm256_srai_epi32::<15>(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cru, u_d), _mm256_mullo_epi32(crv, v_d)),
rnd_v,
));
let g_i32 = _mm256_srai_epi32::<15>(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cgu, u_d), _mm256_mullo_epi32(cgv, v_d)),
rnd_v,
));
let b_i32 = _mm256_srai_epi32::<15>(_mm256_add_epi32(
_mm256_add_epi32(_mm256_mullo_epi32(cbu, u_d), _mm256_mullo_epi32(cbv, v_d)),
rnd_v,
));
let r_chroma = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(r_i32, r_i32));
let g_chroma = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(g_i32, g_i32));
let b_chroma = _mm256_permute4x64_epi64::<0xD8>(_mm256_packs_epi32(b_i32, b_i32));
let r_for_lo = _mm256_permutevar8x32_epi32(r_chroma, perm_low);
let g_for_lo = _mm256_permutevar8x32_epi32(g_chroma, perm_low);
let b_for_lo = _mm256_permutevar8x32_epi32(b_chroma, perm_low);
let r_for_hi = _mm256_permutevar8x32_epi32(r_chroma, perm_high);
let g_for_hi = _mm256_permutevar8x32_epi32(g_chroma, perm_high);
let b_for_hi = _mm256_permutevar8x32_epi32(b_chroma, perm_high);
let r_dup_lo = _mm256_shuffle_epi8(r_for_lo, dup_mask);
let g_dup_lo = _mm256_shuffle_epi8(g_for_lo, dup_mask);
let b_dup_lo = _mm256_shuffle_epi8(b_for_lo, dup_mask);
let r_dup_hi = _mm256_shuffle_epi8(r_for_hi, dup_mask);
let g_dup_hi = _mm256_shuffle_epi8(g_for_hi, dup_mask);
let b_dup_hi = _mm256_shuffle_epi8(b_for_hi, dup_mask);
let y_low_i16 = _mm256_cvtepu8_epi16(_mm256_castsi256_si128(y_vec));
let y_high_i16 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256::<1>(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_lo = _mm256_adds_epi16(y_scaled_lo, b_dup_lo);
let b_hi = _mm256_adds_epi16(y_scaled_hi, b_dup_hi);
let g_lo = _mm256_adds_epi16(y_scaled_lo, g_dup_lo);
let g_hi = _mm256_adds_epi16(y_scaled_hi, g_dup_hi);
let r_lo = _mm256_adds_epi16(y_scaled_lo, r_dup_lo);
let r_hi = _mm256_adds_epi16(y_scaled_hi, r_dup_hi);
let b_u8 = narrow_u8x32(b_lo, b_hi);
let g_u8 = narrow_u8x32(g_lo, g_hi);
let r_u8 = narrow_u8x32(r_lo, r_hi);
if ALPHA {
write_rgba_32(r_u8, g_u8, b_u8, alpha_u8, out.as_mut_ptr().add(x * 4));
} else {
write_rgb_32(r_u8, g_u8, b_u8, out.as_mut_ptr().add(x * 3));
}
x += 32;
}
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 = "avx2")]
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 + 32 <= width {
let block = (x / 4) * 6;
let p0_a = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p0_b = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let p1_a = _mm_loadu_si128(packed.as_ptr().add(block + 24).cast());
let p1_b = _mm_loadu_si128(packed.as_ptr().add(block + 32).cast());
let y_w0 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p0_a, y_mask_p0),
_mm_shuffle_epi8(p0_b, y_mask_p1),
);
let y_w1 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p1_a, y_mask_p0),
_mm_shuffle_epi8(p1_b, y_mask_p1),
);
let y_vec = _mm256_inserti128_si256::<1>(_mm256_castsi128_si256(y_w0), y_w1);
_mm256_storeu_si256(luma_out.as_mut_ptr().add(x).cast(), y_vec);
x += 32;
}
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 = "avx2")]
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 + 32 <= width {
let block = (x / 4) * 6;
let p0_a = _mm_loadu_si128(packed.as_ptr().add(block).cast());
let p0_b = _mm_loadu_si128(packed.as_ptr().add(block + 8).cast());
let p1_a = _mm_loadu_si128(packed.as_ptr().add(block + 24).cast());
let p1_b = _mm_loadu_si128(packed.as_ptr().add(block + 32).cast());
let y_w0 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p0_a, y_mask_p0),
_mm_shuffle_epi8(p0_b, y_mask_p1),
);
let y_w1 = _mm_unpacklo_epi64(
_mm_shuffle_epi8(p1_a, y_mask_p0),
_mm_shuffle_epi8(p1_b, y_mask_p1),
);
let w_lo = _mm256_cvtepu8_epi16(y_w0);
let w_hi = _mm256_cvtepu8_epi16(y_w1);
_mm256_storeu_si256(out.as_mut_ptr().add(x).cast(), w_lo);
_mm256_storeu_si256(out.as_mut_ptr().add(x + 16).cast(), w_hi);
x += 32;
}
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,
);
}
}
}