use crate::dsp::dsp::{VP8_SSIM_KERNEL, VP8DistoStats};
const WEIGHT: [u32; (2 * VP8_SSIM_KERNEL + 1) as usize] = [1, 2, 3, 4, 3, 2, 1];
const WEIGHT_SUM: u32 = 16 * 16;
#[expect(
clippy::cast_sign_loss,
reason = "the C code converts the non-negative correlation sum to uint64_t"
)]
#[expect(
clippy::cast_precision_loss,
reason = "the C code divides the two 64-bit sums as doubles"
)]
#[expect(
clippy::similar_names,
reason = "the C names xmym, xmxm and ymym of the SSIM formula"
)]
fn ssim_calculation(stats: &VP8DistoStats, n: u32) -> f64 {
let w2 = n.wrapping_mul(n);
let c1 = 20_u32.wrapping_mul(w2);
let c2 = 60_u32.wrapping_mul(w2);
let c3 = (8_u32 * 8).wrapping_mul(w2); let xmxm = u64::from(stats.xm) * u64::from(stats.xm);
let ymym = u64::from(stats.ym) * u64::from(stats.ym);
if xmxm.wrapping_add(ymym) >= u64::from(c3) {
let xmym = i64::from(stats.xm) * i64::from(stats.ym);
let sxy = i64::from(stats.xym) * i64::from(n) - xmym; let sxx = u64::from(stats.xxm)
.wrapping_mul(u64::from(n))
.wrapping_sub(xmxm);
let syy = u64::from(stats.yym)
.wrapping_mul(u64::from(n))
.wrapping_sub(ymym);
let num_s = (2 * (if sxy < 0 { 0 } else { sxy as u64 }) + u64::from(c2)) >> 8;
let den_s = sxx.wrapping_add(syy).wrapping_add(u64::from(c2)) >> 8;
let fnum = ((2 * xmym + i64::from(c1)) as u64).wrapping_mul(num_s);
let fden = xmxm
.wrapping_add(ymym)
.wrapping_add(u64::from(c1))
.wrapping_mul(den_s);
let r = fnum as f64 / fden as f64;
debug_assert!((0.0..=1.0).contains(&r));
return r;
}
1.0 }
#[must_use]
pub fn vp8ssim_from_stats(stats: &VP8DistoStats) -> f64 {
ssim_calculation(stats, WEIGHT_SUM)
}
#[must_use]
pub fn vp8ssim_from_stats_clipped(stats: &VP8DistoStats) -> f64 {
ssim_calculation(stats, stats.w)
}
#[must_use]
#[expect(
clippy::too_many_arguments,
reason = "mirrors the parameters of VP8SSIMGetClippedFunc"
)]
#[expect(
clippy::cast_sign_loss,
reason = "the clipped window starts at a non-negative row and column"
)]
fn ssim_get_clipped_c(
src1: &[u8],
stride1: i32,
src2: &[u8],
stride2: i32,
xo: i32,
yo: i32,
w: i32,
h: i32,
) -> f64 {
let mut stats = VP8DistoStats::default();
let kernel = VP8_SSIM_KERNEL;
let ymin = if yo - kernel < 0 { 0 } else { yo - kernel };
let ymax = if yo + kernel > h - 1 {
h - 1
} else {
yo + kernel
};
let xmin = if xo - kernel < 0 { 0 } else { xo - kernel };
let xmax = if xo + kernel > w - 1 {
w - 1
} else {
xo + kernel
};
let mut row1 = ymin as usize * stride1 as usize;
let mut row2 = ymin as usize * stride2 as usize;
for y in ymin..=ymax {
for x in xmin..=xmax {
let wy = WEIGHT[(kernel + y - yo) as usize];
let wx = WEIGHT[(kernel + x - xo) as usize];
let weight = wx * wy;
let s1 = u32::from(src1[row1 + x as usize]);
let s2 = u32::from(src2[row2 + x as usize]);
stats.w += weight;
stats.xm += weight * s1;
stats.ym += weight * s2;
stats.xxm += weight * s1 * s1;
stats.xym += weight * s1 * s2;
stats.yym += weight * s2 * s2;
}
row1 += stride1 as usize;
row2 += stride2 as usize;
}
vp8ssim_from_stats_clipped(&stats)
}
#[must_use]
#[expect(clippy::cast_sign_loss, reason = "the strides are non-negative")]
fn ssim_get_c(src1: &[u8], stride1: i32, src2: &[u8], stride2: i32) -> f64 {
let mut stats = VP8DistoStats::default();
let mut row1 = 0_usize;
let mut row2 = 0_usize;
for &wy in &WEIGHT {
for (x, &wx) in WEIGHT.iter().enumerate() {
let weight = wx * wy;
let s1 = u32::from(src1[row1 + x]);
let s2 = u32::from(src2[row2 + x]);
stats.xm += weight * s1;
stats.ym += weight * s2;
stats.xxm += weight * s1 * s1;
stats.xym += weight * s1 * s2;
stats.yym += weight * s2 * s2;
}
row1 += stride1 as usize;
row2 += stride2 as usize;
}
vp8ssim_from_stats(&stats)
}
#[must_use]
fn accumulate_sse_c(src1: &[u8], src2: &[u8], len: i32) -> u32 {
let len = usize::try_from(len).unwrap_or(0);
let mut sse2: u32 = 0;
for i in 0..len {
let diff = i32::from(src1[i]) - i32::from(src2[i]);
sse2 = sse2.wrapping_add((diff * diff).cast_unsigned());
}
sse2
}
#[must_use]
#[expect(
clippy::too_many_arguments,
reason = "mirrors the parameters of VP8SSIMGetClippedFunc"
)]
pub fn vp8ssim_get_clipped(
src1: &[u8],
stride1: i32,
src2: &[u8],
stride2: i32,
xo: i32,
yo: i32,
w: i32,
h: i32,
) -> f64 {
ssim_get_clipped_c(src1, stride1, src2, stride2, xo, yo, w, h)
}
#[must_use]
pub fn vp8ssim_get(src1: &[u8], stride1: i32, src2: &[u8], stride2: i32) -> f64 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
if crate::dsp::cpu::features().sse2 {
return crate::dsp::ssim_sse2::ssim_get(src1, stride1, src2, stride2);
}
}
ssim_get_c(src1, stride1, src2, stride2)
}
#[must_use]
pub fn vp8_accumulate_sse(src1: &[u8], src2: &[u8], len: i32) -> u32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
if crate::dsp::cpu::features().sse2 {
return crate::dsp::ssim_sse2::accumulate_sse(src1, src2, len);
}
}
accumulate_sse_c(src1, src2, len)
}