use std::sync::LazyLock;
use crate::dsp::dsp::{
BPS, MAX_COEFF_THRESH, VP8Histogram, webp_transform_ac3_mul1, webp_transform_ac3_mul2,
};
use crate::enc::vp8i_enc::{
C8DC8, C8HE8, C8TM8, C8VE8, I4DC4, I4HD4, I4HE4, I4HU4, I4LD4, I4RD4, I4TM4, I4VE4, I4VL4,
I4VR4, I16DC16, I16HE16, I16TM16, I16VE16, MAX_LEVEL, VP8Matrix, quantdiv,
};
const BPS_U: usize = BPS as usize;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value is in 0..=255 on the branch that converts it"
)]
fn clip_8b(v: i32) -> u8 {
if v & !0xff == 0 {
v as u8
} else if v < 0 {
0
} else {
255
}
}
fn clip_max(v: i32, max: i32) -> i32 {
if v > max { max } else { v }
}
pub static VP8_DSP_SCAN: [i32; 16 + 4 + 4] = [
0,
4,
8,
12,
4 * BPS,
4 + 4 * BPS,
8 + 4 * BPS,
12 + 4 * BPS,
8 * BPS,
4 + 8 * BPS,
8 + 8 * BPS,
12 + 8 * BPS,
12 * BPS,
4 + 12 * BPS,
8 + 12 * BPS,
12 + 12 * BPS,
0,
4,
4 * BPS,
4 + 4 * BPS,
8,
12,
8 + 4 * BPS,
12 + 4 * BPS,
];
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "k is in 0..=MAX_COEFF_THRESH"
)]
pub fn vp8_set_histogram_data(
distribution: &[i32; MAX_COEFF_THRESH as usize + 1],
histo: &mut VP8Histogram,
) {
let mut max_value = 0;
let mut last_non_zero = 1;
for (k, &value) in distribution.iter().enumerate() {
if value > 0 {
if value > max_value {
max_value = value;
}
last_non_zero = k as i32;
}
}
histo.max_value = max_value;
histo.last_non_zero = last_non_zero;
}
#[expect(
clippy::cast_sign_loss,
reason = "the block offsets and clipped values are non-negative"
)]
pub fn collect_histogram_c(
ref_: &[u8],
pred: &[u8],
start_block: i32,
end_block: i32,
histo: &mut VP8Histogram,
) {
let mut distribution = [0_i32; MAX_COEFF_THRESH as usize + 1];
for j in start_block..end_block {
let mut out = [0_i16; 16];
let off = VP8_DSP_SCAN[j as usize] as usize;
vp8f_transform(&ref_[off..], &pred[off..], &mut out);
for &coeff in &out {
let v = i32::from(coeff).abs() >> 3;
let clipped_value = clip_max(v, MAX_COEFF_THRESH);
distribution[clipped_value as usize] += 1;
}
}
vp8_set_histogram_data(&distribution, histo);
}
static CLIP1: LazyLock<[u8; 255 + 510 + 1]> = LazyLock::new(init_tables);
fn init_tables() -> [u8; 255 + 510 + 1] {
let mut clip1 = [0_u8; 255 + 510 + 1];
for (index, entry) in clip1.iter_mut().enumerate() {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "the index is below 766"
)]
let i = index as i32 - 255;
*entry = clip_8b(i);
}
clip1
}
fn store(dst: &mut [u8], ref_: &[u8], x: usize, y: usize, v: i32) {
dst[x + y * BPS_U] = clip_8b(i32::from(ref_[x + y * BPS_U]) + (v >> 3));
}
fn i_transform_one(ref_: &[u8], input: &[i16], dst: &mut [u8]) {
let mut c = [0_i32; 4 * 4];
for i in 0..4 {
let inp = &input[i..];
let a = i32::from(inp[0]) + i32::from(inp[8]);
let b = i32::from(inp[0]) - i32::from(inp[8]);
let c1 = webp_transform_ac3_mul2(i32::from(inp[4]))
- webp_transform_ac3_mul1(i32::from(inp[12]));
let d = webp_transform_ac3_mul1(i32::from(inp[4]))
+ webp_transform_ac3_mul2(i32::from(inp[12]));
let tmp = &mut c[4 * i..];
tmp[0] = a + d;
tmp[1] = b + c1;
tmp[2] = b - c1;
tmp[3] = a - d;
}
for i in 0..4 {
let tmp = &c[i..];
let dc = tmp[0] + 4;
let a = dc + tmp[8];
let b = dc - tmp[8];
let c1 = webp_transform_ac3_mul2(tmp[4]) - webp_transform_ac3_mul1(tmp[12]);
let d = webp_transform_ac3_mul1(tmp[4]) + webp_transform_ac3_mul2(tmp[12]);
store(dst, ref_, 0, i, a + d);
store(dst, ref_, 1, i, b + c1);
store(dst, ref_, 2, i, b - c1);
store(dst, ref_, 3, i, a - d);
}
}
pub fn i_transform_c(ref_: &[u8], input: &[i16], dst: &mut [u8], do_two: bool) {
i_transform_one(ref_, input, dst);
if do_two {
i_transform_one(&ref_[4..], &input[16..], &mut dst[4..]);
}
}
#[expect(
clippy::cast_possible_truncation,
reason = "C stores the 12-bit coefficients into int16_t"
)]
pub fn f_transform_c(src: &[u8], ref_: &[u8], out: &mut [i16]) {
let mut tmp = [0_i32; 16];
for i in 0..4 {
let s = &src[i * BPS_U..];
let r = &ref_[i * BPS_U..];
let d0 = i32::from(s[0]) - i32::from(r[0]); let d1 = i32::from(s[1]) - i32::from(r[1]);
let d2 = i32::from(s[2]) - i32::from(r[2]);
let d3 = i32::from(s[3]) - i32::from(r[3]);
let a0 = d0 + d3; let a1 = d1 + d2;
let a2 = d1 - d2;
let a3 = d0 - d3;
tmp[i * 4] = (a0 + a1) * 8; tmp[1 + i * 4] = (a2 * 2217 + a3 * 5352 + 1812) >> 9; tmp[2 + i * 4] = (a0 - a1) * 8;
tmp[3 + i * 4] = (a3 * 2217 - a2 * 5352 + 937) >> 9;
}
for i in 0..4 {
let a0 = tmp[i] + tmp[12 + i]; let a1 = tmp[4 + i] + tmp[8 + i];
let a2 = tmp[4 + i] - tmp[8 + i];
let a3 = tmp[i] - tmp[12 + i];
out[i] = ((a0 + a1 + 7) >> 4) as i16; out[4 + i] = (((a2 * 2217 + a3 * 5352 + 12000) >> 16) + i32::from(a3 != 0)) as i16;
out[8 + i] = ((a0 - a1 + 7) >> 4) as i16;
out[12 + i] = ((a3 * 2217 - a2 * 5352 + 51000) >> 16) as i16;
}
}
fn f_transform2_c(src: &[u8], ref_: &[u8], out: &mut [i16]) {
vp8f_transform(src, ref_, out);
vp8f_transform(&src[4..], &ref_[4..], &mut out[16..]);
}
#[expect(
clippy::cast_possible_truncation,
reason = "C stores the 15-bit coefficients into int16_t"
)]
pub fn f_transform_wht_c(input: &[i16], out: &mut [i16]) {
let mut tmp = [0_i32; 16];
for i in 0..4 {
let inp = &input[i * 64..];
let a0 = i32::from(inp[0]) + i32::from(inp[2 * 16]); let a1 = i32::from(inp[16]) + i32::from(inp[3 * 16]);
let a2 = i32::from(inp[16]) - i32::from(inp[3 * 16]);
let a3 = i32::from(inp[0]) - i32::from(inp[2 * 16]);
tmp[i * 4] = a0 + a1; tmp[1 + i * 4] = a3 + a2;
tmp[2 + i * 4] = a3 - a2;
tmp[3 + i * 4] = a0 - a1;
}
for i in 0..4 {
let a0 = tmp[i] + tmp[8 + i]; let a1 = tmp[4 + i] + tmp[12 + i];
let a2 = tmp[4 + i] - tmp[12 + i];
let a3 = tmp[i] - tmp[8 + i];
let b0 = a0 + a1; let b1 = a3 + a2;
let b2 = a3 - a2;
let b3 = a0 - a1;
out[i] = (b0 >> 1) as i16; out[4 + i] = (b1 >> 1) as i16;
out[8 + i] = (b2 >> 1) as i16;
out[12 + i] = (b3 >> 1) as i16;
}
}
pub type PredLeft<'a> = Option<(&'a [u8], usize)>;
#[expect(clippy::cast_sign_loss, reason = "the block offsets are non-negative")]
const fn at(offset: i32) -> usize {
offset as usize
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C memsets the low byte of the value"
)]
fn fill(dst: &mut [u8], value: i32, size: usize) {
for j in 0..size {
dst[j * BPS_U..j * BPS_U + size].fill(value as u8);
}
}
fn vertical_pred(dst: &mut [u8], top: Option<&[u8]>, size: usize) {
if let Some(top) = top {
for j in 0..size {
dst[j * BPS_U..j * BPS_U + size].copy_from_slice(&top[..size]);
}
} else {
fill(dst, 127, size);
}
}
fn horizontal_pred(dst: &mut [u8], left: PredLeft<'_>, size: usize) {
if let Some((left, pos)) = left {
for j in 0..size {
dst[j * BPS_U..j * BPS_U + size].fill(left[pos + j]);
}
} else {
fill(dst, 129, size);
}
}
fn true_motion(dst: &mut [u8], left: PredLeft<'_>, top: Option<&[u8]>, size: usize) {
if let Some((left_buf, pos)) = left {
if let Some(top) = top {
let clip = 255 - usize::from(left_buf[pos - 1]);
for y in 0..size {
let clip_table = clip + usize::from(left_buf[pos + y]);
let row = &mut dst[y * BPS_U..];
for x in 0..size {
row[x] = CLIP1[clip_table + usize::from(top[x])];
}
}
} else {
horizontal_pred(dst, left, size);
}
} else {
if top.is_some() {
vertical_pred(dst, top, size);
} else {
fill(dst, 129, size);
}
}
}
fn dc_mode(
dst: &mut [u8],
left: PredLeft<'_>,
top: Option<&[u8]>,
size: usize,
round: i32,
shift: i32,
) {
let mut dc = 0_i32;
if let Some(top) = top {
for &sample in &top[..size] {
dc += i32::from(sample);
}
if let Some((left, pos)) = left {
for &sample in &left[pos..pos + size] {
dc += i32::from(sample);
}
} else {
dc += dc;
}
dc = (dc + round) >> shift;
} else if let Some((left, pos)) = left {
for &sample in &left[pos..pos + size] {
dc += i32::from(sample);
}
dc += dc;
dc = (dc + round) >> shift;
} else {
dc = 0x80;
}
fill(dst, dc, size);
}
fn intra_chroma_preds_c(dst: &mut [u8], left: PredLeft<'_>, top: Option<&[u8]>) {
dc_mode(&mut dst[at(C8DC8)..], left, top, 8, 8, 4);
vertical_pred(&mut dst[at(C8VE8)..], top, 8);
horizontal_pred(&mut dst[at(C8HE8)..], left, 8);
true_motion(&mut dst[at(C8TM8)..], left, top, 8);
let dst = &mut dst[8..];
let top = top.map(|top| &top[8..]);
let left = left.map(|(left, pos)| (left, pos + 16));
dc_mode(&mut dst[at(C8DC8)..], left, top, 8, 8, 4);
vertical_pred(&mut dst[at(C8VE8)..], top, 8);
horizontal_pred(&mut dst[at(C8HE8)..], left, 8);
true_motion(&mut dst[at(C8TM8)..], left, top, 8);
}
pub fn intra16_preds_c(dst: &mut [u8], left: PredLeft<'_>, top: Option<&[u8]>) {
dc_mode(&mut dst[at(I16DC16)..], left, top, 16, 16, 5);
vertical_pred(&mut dst[at(I16VE16)..], top, 16);
horizontal_pred(&mut dst[at(I16HE16)..], left, 16);
true_motion(&mut dst[at(I16TM16)..], left, top, 16);
}
fn dst_set(dst: &mut [u8], x: usize, y: usize, v: u8) {
dst[x + y * BPS_U] = v;
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C casts the average to uint8_t"
)]
fn avg3(a: i32, b: i32, c: i32) -> u8 {
((a + 2 * b + c + 2) >> 2) as u8
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the average of two samples fits uint8_t"
)]
fn avg2(a: i32, b: i32) -> u8 {
((a + b + 1) >> 1) as u8
}
fn tp(top: &[u8], pos: usize, offset: isize) -> i32 {
i32::from(
top[pos
.checked_add_signed(offset)
.expect("the boundary sample exists")],
)
}
fn ve4(dst: &mut [u8], top: &[u8], p: usize) {
let vals = [
avg3(tp(top, p, -1), tp(top, p, 0), tp(top, p, 1)),
avg3(tp(top, p, 0), tp(top, p, 1), tp(top, p, 2)),
avg3(tp(top, p, 1), tp(top, p, 2), tp(top, p, 3)),
avg3(tp(top, p, 2), tp(top, p, 3), tp(top, p, 4)),
];
for i in 0..4 {
dst[i * BPS_U..i * BPS_U + 4].copy_from_slice(&vals);
}
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn he4(dst: &mut [u8], top: &[u8], p: usize) {
let x = tp(top, p, -1);
let i = tp(top, p, -2);
let j = tp(top, p, -3);
let k = tp(top, p, -4);
let l = tp(top, p, -5);
dst[..4].fill(avg3(x, i, j));
dst[BPS_U..BPS_U + 4].fill(avg3(i, j, k));
dst[2 * BPS_U..2 * BPS_U + 4].fill(avg3(j, k, l));
dst[3 * BPS_U..3 * BPS_U + 4].fill(avg3(k, l, l));
}
#[expect(
clippy::cast_possible_wrap,
reason = "the sum of eight samples fits int"
)]
fn dc4(dst: &mut [u8], top: &[u8], p: usize) {
let mut dc: u32 = 4;
for i in 0..4 {
#[expect(clippy::cast_sign_loss, reason = "samples are non-negative")]
{
dc += (tp(top, p, i) + tp(top, p, -5 + i)) as u32;
}
}
fill(dst, (dc >> 3) as i32, 4);
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn rd4(dst: &mut [u8], top: &[u8], p: usize) {
let x = tp(top, p, -1);
let i = tp(top, p, -2);
let j = tp(top, p, -3);
let k = tp(top, p, -4);
let l = tp(top, p, -5);
let a = tp(top, p, 0);
let b = tp(top, p, 1);
let c = tp(top, p, 2);
let d = tp(top, p, 3);
dst_set(dst, 0, 3, avg3(j, k, l));
let v = avg3(i, j, k);
dst_set(dst, 0, 2, v);
dst_set(dst, 1, 3, v);
let v = avg3(x, i, j);
dst_set(dst, 0, 1, v);
dst_set(dst, 1, 2, v);
dst_set(dst, 2, 3, v);
let v = avg3(a, x, i);
dst_set(dst, 0, 0, v);
dst_set(dst, 1, 1, v);
dst_set(dst, 2, 2, v);
dst_set(dst, 3, 3, v);
let v = avg3(b, a, x);
dst_set(dst, 1, 0, v);
dst_set(dst, 2, 1, v);
dst_set(dst, 3, 2, v);
let v = avg3(c, b, a);
dst_set(dst, 2, 0, v);
dst_set(dst, 3, 1, v);
dst_set(dst, 3, 0, avg3(d, c, b));
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn ld4(dst: &mut [u8], top: &[u8], p: usize) {
let a = tp(top, p, 0);
let b = tp(top, p, 1);
let c = tp(top, p, 2);
let d = tp(top, p, 3);
let e = tp(top, p, 4);
let f = tp(top, p, 5);
let g = tp(top, p, 6);
let h = tp(top, p, 7);
dst_set(dst, 0, 0, avg3(a, b, c));
let v = avg3(b, c, d);
dst_set(dst, 1, 0, v);
dst_set(dst, 0, 1, v);
let v = avg3(c, d, e);
dst_set(dst, 2, 0, v);
dst_set(dst, 1, 1, v);
dst_set(dst, 0, 2, v);
let v = avg3(d, e, f);
dst_set(dst, 3, 0, v);
dst_set(dst, 2, 1, v);
dst_set(dst, 1, 2, v);
dst_set(dst, 0, 3, v);
let v = avg3(e, f, g);
dst_set(dst, 3, 1, v);
dst_set(dst, 2, 2, v);
dst_set(dst, 1, 3, v);
let v = avg3(f, g, h);
dst_set(dst, 3, 2, v);
dst_set(dst, 2, 3, v);
dst_set(dst, 3, 3, avg3(g, h, h));
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn vr4(dst: &mut [u8], top: &[u8], p: usize) {
let x = tp(top, p, -1);
let i = tp(top, p, -2);
let j = tp(top, p, -3);
let k = tp(top, p, -4);
let a = tp(top, p, 0);
let b = tp(top, p, 1);
let c = tp(top, p, 2);
let d = tp(top, p, 3);
let v = avg2(x, a);
dst_set(dst, 0, 0, v);
dst_set(dst, 1, 2, v);
let v = avg2(a, b);
dst_set(dst, 1, 0, v);
dst_set(dst, 2, 2, v);
let v = avg2(b, c);
dst_set(dst, 2, 0, v);
dst_set(dst, 3, 2, v);
dst_set(dst, 3, 0, avg2(c, d));
dst_set(dst, 0, 3, avg3(k, j, i));
dst_set(dst, 0, 2, avg3(j, i, x));
let v = avg3(i, x, a);
dst_set(dst, 0, 1, v);
dst_set(dst, 1, 3, v);
let v = avg3(x, a, b);
dst_set(dst, 1, 1, v);
dst_set(dst, 2, 3, v);
let v = avg3(a, b, c);
dst_set(dst, 2, 1, v);
dst_set(dst, 3, 3, v);
dst_set(dst, 3, 1, avg3(b, c, d));
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn vl4(dst: &mut [u8], top: &[u8], p: usize) {
let a = tp(top, p, 0);
let b = tp(top, p, 1);
let c = tp(top, p, 2);
let d = tp(top, p, 3);
let e = tp(top, p, 4);
let f = tp(top, p, 5);
let g = tp(top, p, 6);
let h = tp(top, p, 7);
dst_set(dst, 0, 0, avg2(a, b));
let v = avg2(b, c);
dst_set(dst, 1, 0, v);
dst_set(dst, 0, 2, v);
let v = avg2(c, d);
dst_set(dst, 2, 0, v);
dst_set(dst, 1, 2, v);
let v = avg2(d, e);
dst_set(dst, 3, 0, v);
dst_set(dst, 2, 2, v);
dst_set(dst, 0, 1, avg3(a, b, c));
let v = avg3(b, c, d);
dst_set(dst, 1, 1, v);
dst_set(dst, 0, 3, v);
let v = avg3(c, d, e);
dst_set(dst, 2, 1, v);
dst_set(dst, 1, 3, v);
let v = avg3(d, e, f);
dst_set(dst, 3, 1, v);
dst_set(dst, 2, 3, v);
dst_set(dst, 3, 2, avg3(e, f, g));
dst_set(dst, 3, 3, avg3(f, g, h));
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "L is a sample"
)]
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn hu4(dst: &mut [u8], top: &[u8], p: usize) {
let i = tp(top, p, -2);
let j = tp(top, p, -3);
let k = tp(top, p, -4);
let l = tp(top, p, -5);
dst_set(dst, 0, 0, avg2(i, j));
let v = avg2(j, k);
dst_set(dst, 2, 0, v);
dst_set(dst, 0, 1, v);
let v = avg2(k, l);
dst_set(dst, 2, 1, v);
dst_set(dst, 0, 2, v);
dst_set(dst, 1, 0, avg3(i, j, k));
let v = avg3(j, k, l);
dst_set(dst, 3, 0, v);
dst_set(dst, 1, 1, v);
let v = avg3(k, l, l);
dst_set(dst, 3, 1, v);
dst_set(dst, 1, 2, v);
let v = l as u8;
dst_set(dst, 3, 2, v);
dst_set(dst, 2, 2, v);
dst_set(dst, 0, 3, v);
dst_set(dst, 1, 3, v);
dst_set(dst, 2, 3, v);
dst_set(dst, 3, 3, v);
}
#[expect(clippy::many_single_char_names, reason = "the C sample names")]
fn hd4(dst: &mut [u8], top: &[u8], p: usize) {
let x = tp(top, p, -1);
let i = tp(top, p, -2);
let j = tp(top, p, -3);
let k = tp(top, p, -4);
let l = tp(top, p, -5);
let a = tp(top, p, 0);
let b = tp(top, p, 1);
let c = tp(top, p, 2);
let v = avg2(i, x);
dst_set(dst, 0, 0, v);
dst_set(dst, 2, 1, v);
let v = avg2(j, i);
dst_set(dst, 0, 1, v);
dst_set(dst, 2, 2, v);
let v = avg2(k, j);
dst_set(dst, 0, 2, v);
dst_set(dst, 2, 3, v);
dst_set(dst, 0, 3, avg2(l, k));
dst_set(dst, 3, 0, avg3(a, b, c));
dst_set(dst, 2, 0, avg3(x, a, b));
let v = avg3(i, x, a);
dst_set(dst, 1, 0, v);
dst_set(dst, 3, 1, v);
let v = avg3(j, i, x);
dst_set(dst, 1, 1, v);
dst_set(dst, 3, 2, v);
let v = avg3(k, j, i);
dst_set(dst, 1, 2, v);
dst_set(dst, 3, 3, v);
dst_set(dst, 1, 3, avg3(l, k, j));
}
#[expect(clippy::cast_sign_loss, reason = "samples are non-negative")]
fn tm4(dst: &mut [u8], top: &[u8], p: usize) {
let clip = 255 - tp(top, p, -1) as usize;
for y in 0..4 {
#[expect(clippy::cast_possible_wrap, reason = "y is below 4")]
let clip_table = clip + tp(top, p, -2 - y as isize) as usize;
let row = &mut dst[y * BPS_U..];
for x in 0..4 {
#[expect(clippy::cast_possible_wrap, reason = "x is below 4")]
{
row[x] = CLIP1[clip_table + tp(top, p, x as isize) as usize];
}
}
}
}
pub fn intra4_preds_c(dst: &mut [u8], top: &[u8], p: usize) {
dc4(&mut dst[at(I4DC4)..], top, p);
tm4(&mut dst[at(I4TM4)..], top, p);
ve4(&mut dst[at(I4VE4)..], top, p);
he4(&mut dst[at(I4HE4)..], top, p);
rd4(&mut dst[at(I4RD4)..], top, p);
vr4(&mut dst[at(I4VR4)..], top, p);
ld4(&mut dst[at(I4LD4)..], top, p);
vl4(&mut dst[at(I4VL4)..], top, p);
hd4(&mut dst[at(I4HD4)..], top, p);
hu4(&mut dst[at(I4HU4)..], top, p);
}
fn get_sse(a: &[u8], b: &[u8], w: usize, h: usize) -> i32 {
let mut count = 0;
for y in 0..h {
for x in 0..w {
let diff = i32::from(a[x + y * BPS_U]) - i32::from(b[x + y * BPS_U]);
count += diff * diff;
}
}
count
}
#[must_use]
pub fn sse16x16_c(a: &[u8], b: &[u8]) -> i32 {
get_sse(a, b, 16, 16)
}
#[must_use]
pub fn sse16x8_c(a: &[u8], b: &[u8]) -> i32 {
get_sse(a, b, 16, 8)
}
#[must_use]
pub fn sse8x8_c(a: &[u8], b: &[u8]) -> i32 {
get_sse(a, b, 8, 8)
}
#[must_use]
pub fn sse4x4_c(a: &[u8], b: &[u8]) -> i32 {
get_sse(a, b, 4, 4)
}
fn mean16x4_c(ref_: &[u8], dc: &mut [u32]) {
for k in 0..4 {
let mut avg: u32 = 0;
for y in 0..4 {
for x in 0..4 {
avg += u32::from(ref_[4 * k + x + y * BPS_U]);
}
}
dc[k] = avg;
}
}
fn t_transform(input: &[u8], w: &[u16; 16]) -> i32 {
let mut sum = 0;
let mut tmp = [0_i32; 16];
for i in 0..4 {
let inp = &input[i * BPS_U..];
let a0 = i32::from(inp[0]) + i32::from(inp[2]);
let a1 = i32::from(inp[1]) + i32::from(inp[3]);
let a2 = i32::from(inp[1]) - i32::from(inp[3]);
let a3 = i32::from(inp[0]) - i32::from(inp[2]);
tmp[i * 4] = a0 + a1;
tmp[1 + i * 4] = a3 + a2;
tmp[2 + i * 4] = a3 - a2;
tmp[3 + i * 4] = a0 - a1;
}
for i in 0..4 {
let a0 = tmp[i] + tmp[8 + i];
let a1 = tmp[4 + i] + tmp[12 + i];
let a2 = tmp[4 + i] - tmp[12 + i];
let a3 = tmp[i] - tmp[8 + i];
let b0 = a0 + a1;
let b1 = a3 + a2;
let b2 = a3 - a2;
let b3 = a0 - a1;
sum += i32::from(w[i]) * b0.abs();
sum += i32::from(w[4 + i]) * b1.abs();
sum += i32::from(w[8 + i]) * b2.abs();
sum += i32::from(w[12 + i]) * b3.abs();
}
sum
}
#[must_use]
pub fn disto4x4_c(a: &[u8], b: &[u8], w: &[u16; 16]) -> i32 {
let sum1 = t_transform(a, w);
let sum2 = t_transform(b, w);
(sum2 - sum1).abs() >> 5
}
#[must_use]
pub fn disto16x16_c(a: &[u8], b: &[u8], w: &[u16; 16]) -> i32 {
let mut d = 0;
for y in (0..16 * BPS_U).step_by(4 * BPS_U) {
for x in (0..16).step_by(4) {
d += disto4x4_c(&a[x + y..], &b[x + y..], w);
}
}
d
}
static ZIGZAG: [u8; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_possible_wrap,
reason = "C converts between int, int16_t and uint32_t at these points"
)]
pub fn quantize_block_c(input: &mut [i16], out: &mut [i16], mtx: &VP8Matrix) -> i32 {
let mut last = -1;
for n in 0..16 {
let j = usize::from(ZIGZAG[n]);
let sign = input[j] < 0;
let coeff = (if sign {
-i32::from(input[j])
} else {
i32::from(input[j])
} + i32::from(mtx.sharpen[j])) as u32;
if coeff > mtx.zthresh[j] {
let q = u32::from(mtx.q[j]);
let i_q = u32::from(mtx.iq[j]);
let b = mtx.bias[j];
let mut level = quantdiv(coeff, i_q, b);
if level > MAX_LEVEL {
level = MAX_LEVEL;
}
if sign {
level = -level;
}
input[j] = (level * q as i32) as i16;
out[n] = level as i16;
if level != 0 {
last = n as i32;
}
} else {
out[n] = 0;
input[j] = 0;
}
}
i32::from(last >= 0)
}
pub fn quantize2_blocks_c(input: &mut [i16], out: &mut [i16], mtx: &VP8Matrix) -> i32 {
let mut nz = vp8_enc_quantize_block(&mut input[..16], &mut out[..16], mtx);
nz |= vp8_enc_quantize_block(&mut input[16..32], &mut out[16..32], mtx) << 1;
nz
}
fn copy(src: &[u8], dst: &mut [u8], w: usize, h: usize) {
for y in 0..h {
dst[y * BPS_U..y * BPS_U + w].copy_from_slice(&src[y * BPS_U..y * BPS_U + w]);
}
}
fn copy4x4_c(src: &[u8], dst: &mut [u8]) {
copy(src, dst, 4, 4);
}
fn copy16x8_c(src: &[u8], dst: &mut [u8]) {
copy(src, dst, 16, 8);
}
pub fn vp8_collect_histogram(
ref_: &[u8],
pred: &[u8],
start_block: i32,
end_block: i32,
histo: &mut VP8Histogram,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::collect_histogram(
ref_,
pred,
start_block,
end_block,
histo,
);
}
if features.sse2 {
return crate::dsp::enc_sse2::collect_histogram(
ref_,
pred,
start_block,
end_block,
histo,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::collect_histogram(ref_, pred, start_block, end_block, histo);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
collect_histogram_c(ref_, pred, start_block, end_block, histo);
}
pub fn vp8i_transform(ref_: &[u8], input: &[i16], dst: &mut [u8], do_two: bool) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::i_transform(ref_, input, dst, do_two);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::i_transform(ref_, input, dst, do_two);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
i_transform_c(ref_, input, dst, do_two);
}
pub fn vp8f_transform(src: &[u8], ref_: &[u8], out: &mut [i16]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::f_transform(src, ref_, out);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::f_transform(src, ref_, out);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
f_transform_c(src, ref_, out);
}
pub fn vp8f_transform2(src: &[u8], ref_: &[u8], out: &mut [i16]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::f_transform2(src, ref_, out);
}
}
f_transform2_c(src, ref_, out);
}
pub fn vp8f_transform_wht(input: &[i16], out: &mut [i16]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::f_transform_wht(input, out);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::f_transform_wht(input, out);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
f_transform_wht_c(input, out);
}
pub fn vp8_enc_pred_luma4(dst: &mut [u8], top: &[u8], top_pos: usize) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::intra4_preds(dst, top, top_pos);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::intra4_preds(dst, top, top_pos);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
intra4_preds_c(dst, top, top_pos);
}
pub fn vp8_enc_pred_luma16(dst: &mut [u8], left: PredLeft<'_>, top: Option<&[u8]>) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::intra16_preds(dst, left, top);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::intra16_preds(dst, left, top);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
intra16_preds_c(dst, left, top);
}
pub fn vp8_enc_pred_chroma8(dst: &mut [u8], left: PredLeft<'_>, top: Option<&[u8]>) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::intra_chroma_preds(dst, left, top);
}
}
intra_chroma_preds_c(dst, left, top);
}
#[must_use]
pub fn vp8sse16x16(a: &[u8], b: &[u8]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::sse16x16(a, b);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::sse16x16(a, b);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
sse16x16_c(a, b)
}
#[must_use]
pub fn vp8sse16x8(a: &[u8], b: &[u8]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::sse16x8(a, b);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::sse16x8(a, b);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
sse16x8_c(a, b)
}
#[must_use]
pub fn vp8sse8x8(a: &[u8], b: &[u8]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::sse8x8(a, b);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::sse8x8(a, b);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
sse8x8_c(a, b)
}
#[must_use]
pub fn vp8sse4x4(a: &[u8], b: &[u8]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::sse4x4(a, b);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::sse4x4(a, b);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
sse4x4_c(a, b)
}
#[must_use]
pub fn vp8t_disto4x4(a: &[u8], b: &[u8], w: &[u16; 16]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::disto4x4(a, b, w);
}
if features.sse2 {
return crate::dsp::enc_sse2::disto4x4(a, b, w);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::disto4x4(a, b, w);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
disto4x4_c(a, b, w)
}
#[must_use]
pub fn vp8t_disto16x16(a: &[u8], b: &[u8], w: &[u16; 16]) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::disto16x16(a, b, w);
}
if features.sse2 {
return crate::dsp::enc_sse2::disto16x16(a, b, w);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::disto16x16(a, b, w);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
disto16x16_c(a, b, w)
}
pub fn vp8_mean16x4(ref_: &[u8], dc: &mut [u32]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::enc_sse2::mean16x4(ref_, dc);
}
}
mean16x4_c(ref_, dc);
}
pub fn vp8_enc_quantize_block(input: &mut [i16], out: &mut [i16], mtx: &VP8Matrix) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::quantize_block(input, out, mtx);
}
if features.sse2 {
return crate::dsp::enc_sse2::quantize_block(input, out, mtx);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::quantize_block(input, out, mtx);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
quantize_block_c(input, out, mtx)
}
pub fn vp8_enc_quantize2_blocks(input: &mut [i16], out: &mut [i16], mtx: &VP8Matrix) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::quantize2_blocks(input, out, mtx);
}
if features.sse2 {
return crate::dsp::enc_sse2::quantize2_blocks(input, out, mtx);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::quantize2_blocks(input, out, mtx);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
quantize2_blocks_c(input, out, mtx)
}
pub fn vp8_enc_quantize_block_wht(input: &mut [i16], out: &mut [i16], mtx: &VP8Matrix) -> i32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::enc_sse41::quantize_block_wht(input, out, mtx);
}
if features.sse2 {
return crate::dsp::enc_sse2::quantize_block_wht(input, out, mtx);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::enc_neon::quantize_block(input, out, mtx);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
quantize_block_c(input, out, mtx)
}
pub fn vp8_copy4x4(src: &[u8], dst: &mut [u8]) {
copy4x4_c(src, dst);
}
pub fn vp8_copy16x8(src: &[u8], dst: &mut [u8]) {
copy16x8_c(src, dst);
}