use crate::dsp::yuv::{
vp8_yuv_to_argb, vp8_yuv_to_bgr, vp8_yuv_to_bgra, vp8_yuv_to_rgb, vp8_yuv_to_rgb565,
vp8_yuv_to_rgba, vp8_yuv_to_rgba4444,
};
use crate::webp::decode::WEBP_CSP_MODE;
pub type WebPUpsampleLinePairFunc =
fn(&[u8], Option<(&[u8], &mut [u8])>, &[u8], &[u8], &[u8], &[u8], &mut [u8], i32);
pub type WebPYUV444Converter = fn(&[u8], &[u8], &[u8], &mut [u8], i32);
#[must_use]
fn load_uv(u: u8, v: u8) -> u32 {
u32::from(u) | (u32::from(v) << 16)
}
#[expect(
clippy::too_many_arguments,
reason = "mirrors the parameters of the UPSAMPLE_FUNC functions"
)]
#[expect(
clippy::similar_names,
reason = "the C names tl_uv, t_uv and uv of the packed samples"
)]
fn upsample_line_pair(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
xstep: usize,
func: impl Fn(u8, u8, u8, &mut [u8]),
) {
let len = usize::try_from(len).expect("the row length is not negative");
let last_pixel_pair = (len - 1) >> 1;
let mut bottom = bottom;
let mut tl_uv = load_uv(top_u[0], top_v[0]); let mut l_uv = load_uv(cur_u[0], cur_v[0]); let emit = |y: u8, uv: u32, dst: &mut [u8]| {
func(
y,
u8::try_from(uv & 0xff).expect("the low lane is a byte"),
u8::try_from(uv >> 16).expect("the high lane is a byte"),
dst,
);
};
{
let uv0 = (3 * tl_uv + l_uv + 0x0002_0002) >> 2;
emit(top_y[0], uv0, &mut top_dst[..]);
}
if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
let uv0 = (3 * l_uv + tl_uv + 0x0002_0002) >> 2;
emit(bottom_y[0], uv0, &mut bottom_dst[..]);
}
for x in 1..=last_pixel_pair {
let t_uv = load_uv(top_u[x], top_v[x]); let uv = load_uv(cur_u[x], cur_v[x]); let avg = tl_uv + t_uv + l_uv + uv + 0x0008_0008;
let diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3;
let diag_03 = (avg + 2 * (tl_uv + uv)) >> 3;
{
let uv0 = u32::midpoint(diag_12, tl_uv);
let uv1 = u32::midpoint(diag_03, t_uv);
emit(top_y[2 * x - 1], uv0, &mut top_dst[(2 * x - 1) * xstep..]);
emit(top_y[2 * x], uv1, &mut top_dst[2 * x * xstep..]);
}
if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
let uv0 = u32::midpoint(diag_03, l_uv);
let uv1 = u32::midpoint(diag_12, uv);
emit(
bottom_y[2 * x - 1],
uv0,
&mut bottom_dst[(2 * x - 1) * xstep..],
);
emit(bottom_y[2 * x], uv1, &mut bottom_dst[2 * x * xstep..]);
}
tl_uv = t_uv;
l_uv = uv;
}
if len & 1 == 0 {
{
let uv0 = (3 * tl_uv + l_uv + 0x0002_0002) >> 2;
emit(top_y[len - 1], uv0, &mut top_dst[(len - 1) * xstep..]);
}
if let Some((bottom_y, bottom_dst)) = bottom.as_mut() {
let uv0 = (3 * l_uv + tl_uv + 0x0002_0002) >> 2;
emit(bottom_y[len - 1], uv0, &mut bottom_dst[(len - 1) * xstep..]);
}
}
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_rgba_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
4,
vp8_yuv_to_rgba,
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_bgra_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
4,
vp8_yuv_to_bgra,
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_argb_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
4,
vp8_yuv_to_argb,
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_rgb_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
3,
|y, u, v, dst| {
vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), dst);
},
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_bgr_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
3,
|y, u, v, dst| {
vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), dst);
},
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_rgba4444_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
2,
|y, u, v, dst| {
vp8_yuv_to_rgba4444(i32::from(y), i32::from(u), i32::from(v), dst);
},
);
}
#[expect(
clippy::too_many_arguments,
reason = "the parameters of WebPUpsampleLinePairFunc"
)]
pub fn upsample_rgb565_line_pair_c(
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
upsample_line_pair(
top_y,
bottom,
top_u,
top_v,
cur_u,
cur_v,
top_dst,
len,
2,
|y, u, v, dst| {
vp8_yuv_to_rgb565(i32::from(y), i32::from(u), i32::from(v), dst);
},
);
}
static WEBP_UPSAMPLERS: [Option<WebPUpsampleLinePairFunc>; 13] = [
Some(upsample_rgb_line_pair_c), Some(upsample_rgba_line_pair_c), Some(upsample_bgr_line_pair_c), Some(upsample_bgra_line_pair_c), Some(upsample_argb_line_pair_c), Some(upsample_rgba4444_line_pair_c), Some(upsample_rgb565_line_pair_c), Some(upsample_rgba_line_pair_c), Some(upsample_bgra_line_pair_c), Some(upsample_argb_line_pair_c), Some(upsample_rgba4444_line_pair_c), None, None, ];
#[expect(
clippy::too_many_arguments,
reason = "mirrors the parameters of WebPUpsampleLinePairFunc, with the mode first"
)]
pub fn webp_upsamplers(
mode: WEBP_CSP_MODE,
top_y: &[u8],
bottom: Option<(&[u8], &mut [u8])>,
top_u: &[u8],
top_v: &[u8],
cur_u: &[u8],
cur_v: &[u8],
top_dst: &mut [u8],
len: i32,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
match mode {
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::upsampling_sse41::upsample_rgb_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::upsampling_sse41::upsample_bgr_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
_ => {}
}
}
if features.sse2 {
match mode {
WEBP_CSP_MODE::MODE_RGBA | WEBP_CSP_MODE::MODE_rgbA => {
return crate::dsp::upsampling_sse2::upsample_rgba_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_BGRA | WEBP_CSP_MODE::MODE_bgrA => {
return crate::dsp::upsampling_sse2::upsample_bgra_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::upsampling_sse2::upsample_rgb_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::upsampling_sse2::upsample_bgr_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_ARGB | WEBP_CSP_MODE::MODE_Argb => {
return crate::dsp::upsampling_sse2::upsample_argb_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_RGB_565 => {
return crate::dsp::upsampling_sse2::upsample_rgb565_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
WEBP_CSP_MODE::MODE_RGBA_4444 | WEBP_CSP_MODE::MODE_rgbA_4444 => {
return crate::dsp::upsampling_sse2::upsample_rgba4444_line_pair(
top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len,
);
}
_ => {}
}
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
if let Some(upsampler) = crate::dsp::upsampling_neon::upsamplers(mode) {
return upsampler(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let upsampler = WEBP_UPSAMPLERS[mode.0 as usize].expect("YUV modes have no upsampler");
upsampler(top_y, bottom, top_u, top_v, cur_u, cur_v, top_dst, len);
}
#[must_use]
pub fn webp_get_line_pair_converter(alpha_is_last: bool) -> WebPUpsampleLinePairFunc {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return if alpha_is_last {
crate::dsp::upsampling_sse2::upsample_bgra_line_pair
} else {
crate::dsp::upsampling_sse2::upsample_argb_line_pair
};
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let mode = if alpha_is_last {
WEBP_CSP_MODE::MODE_BGRA
} else {
WEBP_CSP_MODE::MODE_ARGB
};
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
if let Some(upsampler) = crate::dsp::upsampling_neon::upsamplers(mode) {
return upsampler;
}
}
WEBP_UPSAMPLERS[mode.0 as usize].expect("the ARGB and BGRA upsamplers exist")
}
fn yuv444_row(
y: &[u8],
u: &[u8],
v: &[u8],
dst: &mut [u8],
len: i32,
xstep: usize,
func: impl Fn(u8, u8, u8, &mut [u8]),
) {
let len = usize::try_from(len).expect("the row length is not negative");
for i in 0..len {
func(y[i], u[i], v[i], &mut dst[i * xstep..]);
}
}
pub(crate) fn webp_yuv444_to_rgba_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_rgba);
}
pub(crate) fn webp_yuv444_to_bgra_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_bgra);
}
pub(crate) fn webp_yuv444_to_argb_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 4, vp8_yuv_to_argb);
}
pub(crate) fn webp_yuv444_to_rgb_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 3, |y, u, v, out| {
vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), out);
});
}
pub(crate) fn webp_yuv444_to_bgr_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 3, |y, u, v, out| {
vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), out);
});
}
pub(crate) fn webp_yuv444_to_rgba4444_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 2, |y, u, v, out| {
vp8_yuv_to_rgba4444(i32::from(y), i32::from(u), i32::from(v), out);
});
}
pub(crate) fn webp_yuv444_to_rgb565_c(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
yuv444_row(y, u, v, dst, len, 2, |y, u, v, out| {
vp8_yuv_to_rgb565(i32::from(y), i32::from(u), i32::from(v), out);
});
}
static WEBP_YUV444_CONVERTERS: [Option<WebPYUV444Converter>; 13] = [
Some(webp_yuv444_to_rgb_c), Some(webp_yuv444_to_rgba_c), Some(webp_yuv444_to_bgr_c), Some(webp_yuv444_to_bgra_c), Some(webp_yuv444_to_argb_c), Some(webp_yuv444_to_rgba4444_c), Some(webp_yuv444_to_rgb565_c), Some(webp_yuv444_to_rgba_c), Some(webp_yuv444_to_bgra_c), Some(webp_yuv444_to_argb_c), Some(webp_yuv444_to_rgba4444_c), None, None, ];
pub fn webp_yuv444_converters(
mode: WEBP_CSP_MODE,
y: &[u8],
u: &[u8],
v: &[u8],
dst: &mut [u8],
len: i32,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
match mode {
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::upsampling_sse41::yuv444_to_rgb(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::upsampling_sse41::yuv444_to_bgr(y, u, v, dst, len);
}
_ => {}
}
}
if features.sse2 {
match mode {
WEBP_CSP_MODE::MODE_RGBA | WEBP_CSP_MODE::MODE_rgbA => {
return crate::dsp::upsampling_sse2::yuv444_to_rgba(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGRA | WEBP_CSP_MODE::MODE_bgrA => {
return crate::dsp::upsampling_sse2::yuv444_to_bgra(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::upsampling_sse2::yuv444_to_rgb(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::upsampling_sse2::yuv444_to_bgr(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_ARGB | WEBP_CSP_MODE::MODE_Argb => {
return crate::dsp::upsampling_sse2::yuv444_to_argb(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_RGB_565 => {
return crate::dsp::upsampling_sse2::yuv444_to_rgb565(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_RGBA_4444 | WEBP_CSP_MODE::MODE_rgbA_4444 => {
return crate::dsp::upsampling_sse2::yuv444_to_rgba4444(y, u, v, dst, len);
}
_ => {}
}
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let converter = WEBP_YUV444_CONVERTERS[mode.0 as usize].expect("YUV modes have no converter");
converter(y, u, v, dst, len);
}