use super::*;
const VISIBLE_ENERGY: f32 = 2e-4;
const MAX_BOOST: f32 = 0.6;
const BUSY_TEXTURE: f32 = 0.06;
pub(crate) fn enabled(ctx: &EncodingContext, distance: f32) -> bool {
if ctx.arbiter != crate::Arbiter::Ssimulacra2
|| !ctx.coding.is_xyb()
|| !(1.0..=4.0).contains(&distance)
{
return false;
}
if matches!(ctx.speed, crate::Speed::Fast | crate::Speed::Fastest) {
return true;
}
if !ctx.chroma_heavy() || ctx.screen_content() {
return false;
}
match ctx.speed {
crate::Speed::Medium | crate::Speed::MediumSlow => true,
crate::Speed::Slow => {
distance >= crate::quant_weights::QM_SS2_MIN_DISTANCE
&& ctx.luma_texture() >= BUSY_TEXTURE
}
#[cfg(feature = "extraslow")]
crate::Speed::ExtraSlow => distance >= 1.5 && ctx.luma_texture() >= BUSY_TEXTURE,
#[cfg(not(feature = "extraslow"))]
crate::Speed::ExtraSlow => {
distance >= crate::quant_weights::QM_SS2_MIN_DISTANCE
&& ctx.luma_texture() >= BUSY_TEXTURE
}
_ => false,
}
}
pub(super) fn scale(
ctx: &EncodingContext,
strategy: u8,
luma: &[f32],
qac: f32,
distance: f32,
metadata: f32,
) -> f32 {
if metadata == 0.0
|| !matches!(
strategy,
STRATEGY_DCT32X32 | STRATEGY_DCT32X16 | STRATEGY_DCT16X32
)
|| !enabled(ctx, distance)
{
return 1.0;
}
let (cx, cy) = (
AcStrategyImage::covered_blocks_x_of(strategy),
AcStrategyImage::covered_blocks_y_of(strategy),
);
let erased = erased_detail(
luma,
inverse_matrix_for(ctx, strategy, 1),
qac,
cx.max(cy),
cx.min(cy),
);
1.0 + MAX_BOOST * (erased / (VISIBLE_ENERGY * distance * distance) - 1.0).clamp(0.0, 1.0)
}
fn erased_detail(luma: &[f32], inverse: &[f32], qac: f32, cx: usize, cy: usize) -> f32 {
let width = cx * 8;
let mut energy = 0.0f64;
for y in 0..cy * 8 {
for x in 0..width {
if x < cx * 2 && y < cy * 2 {
continue;
}
let i = y * width + x;
if (luma[i] * inverse[i] * qac).abs() < 0.5 {
energy += f64::from(luma[i]).powi(2);
}
}
}
energy as f32
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "extraslow"))]
#[test]
fn featureless_extraslow_retains_the_slow_selection_policy() {
for distance in [1.0, 1.5, 2.25, 4.0] {
let active = [crate::Speed::Slow, crate::Speed::ExtraSlow].map(|speed| {
let ctx = EncodingContext::new(
speed,
crate::Arbiter::Ssimulacra2,
crate::xyb::XybMatrix::SPEC,
distance,
1,
);
ctx.set_chroma_heavy(true);
ctx.set_luma_texture(0.07);
enabled(&ctx, distance)
});
assert_eq!(active[0], active[1], "distance {distance}");
}
}
#[test]
fn rectangular_orientations_use_the_same_canonical_coefficient_layout() {
let ctx = EncodingContext::new(
crate::Speed::Medium,
crate::Arbiter::Ssimulacra2,
crate::xyb::XybMatrix::SPEC,
1.0,
1,
);
ctx.set_chroma_heavy(true);
assert!(enabled(&ctx, 1.0));
let mut luma = [0.0; 512];
luma[36] = 0.02;
let qac = 0.25 / (luma[36] * inverse_matrix_for(&ctx, STRATEGY_DCT32X16, 1)[36]);
for strategy in [STRATEGY_DCT32X16, STRATEGY_DCT16X32] {
assert_eq!(scale(&ctx, strategy, &luma, qac, 1.0, 1.0), 1.0);
}
luma[36] = 0.0;
luma[8] = 0.02;
let qac = 0.25 / (luma[8] * inverse_matrix_for(&ctx, STRATEGY_DCT32X16, 1)[8]);
let tall = scale(&ctx, STRATEGY_DCT32X16, &luma, qac, 1.0, 1.0);
let wide = scale(&ctx, STRATEGY_DCT16X32, &luma, qac, 1.0, 1.0);
assert!(tall > 1.0);
assert_eq!(tall, wide);
}
#[test]
fn erased_detail_excludes_low_frequencies_and_surviving_coefficients() {
for (cx, cy) in [(4, 4), (4, 2), (2, 4)] {
let width = cx * 8;
let mut luma = vec![0.0; cx * cy * 64];
let inverse = vec![1.0; luma.len()];
luma[0] = 100.0;
luma[(cy * 2 - 1) * width + cx * 2 - 1] = 100.0;
assert_eq!(erased_detail(&luma, &inverse, 1.0, cx, cy), 0.0);
luma[cx * 2] = 0.25;
assert_eq!(erased_detail(&luma, &inverse, 1.0, cx, cy), 0.0625);
assert_eq!(erased_detail(&luma, &inverse, 2.0, cx, cy), 0.0);
}
}
}