#![allow(clippy::excessive_precision)]
use crate::ac_context::{
fine_block_context, fine_non_zero_context, fine_zero_density_contexts_offset,
zero_density_context, zero_density_context_8x8,
};
use crate::dc_group_data::{
AcStrategyImage, DcGroupData, STRATEGY_AFV0, STRATEGY_AFV1, STRATEGY_AFV2, STRATEGY_AFV3,
STRATEGY_DCT, STRATEGY_DCT2X2, STRATEGY_DCT4X4, STRATEGY_DCT4X8, STRATEGY_DCT8X4,
STRATEGY_DCT8X16, STRATEGY_DCT8X32, STRATEGY_DCT16X8, STRATEGY_DCT16X16, STRATEGY_DCT16X32,
STRATEGY_DCT32X8, STRATEGY_DCT32X16, STRATEGY_DCT32X32, STRATEGY_DCT32X64, STRATEGY_DCT64X32,
STRATEGY_DCT64X64, STRATEGY_IDENTITY,
};
#[cfg(feature = "extraslow")]
use crate::dc_group_data::{STRATEGY_DCT64X128, STRATEGY_DCT128X64, STRATEGY_DCT128X128};
use crate::dct::{DctInput, dc_from_dct8x16, dc_from_dct16x8, dc_from_dct16x16, fmla};
use crate::encoding_context::EncodingContext;
use crate::entropy::{FrozenTokenPrices, Token, pack_signed};
use crate::image::{Image3B, Image3F, Image3S, Rect};
use crate::inflated_cost::Footprint;
use crate::quant_weights::INV_DC_QUANT;
use crate::util::{HeapMatrix, heap_array, round_i32};
use std::sync::OnceLock;
mod trellis;
use trellis::{
RESTORE_FEW_COLORS_PENALTY, RESTORE_FEW_COLORS_PER_BLOCK, RdoqScratch, RdoqSearch, few_colors,
rdoq_block,
};
pub(crate) use trellis::{chroma_rdoq_weight, rdoq_channel_weights, rdoq_lambda};
const K_GROUP_DIM_IN_BLOCKS: usize = 32;
pub(crate) const MAX_BLOCK_COEFFICIENTS: usize = if cfg!(feature = "extraslow") {
128 * 128
} else {
64 * 64
};
#[inline]
pub(crate) fn predict_from_top_and_left(
row_top: Option<&[u8]>,
row: &[u8],
x: usize,
default_val: u8,
) -> u8 {
if x == 0 {
match row_top {
Some(t) => t[x],
None => default_val,
}
} else if row_top.is_none() {
row[x - 1]
} else {
(row_top.unwrap()[x] as u16 + row[x - 1] as u16).div_ceil(2) as u8
}
}
#[inline]
fn num_nonzero_except_dc(block: &[i32; 64]) -> i32 {
let mut count: i32 = 0;
for &z in block[1..64].iter() {
if z != 0 {
count += 1;
}
}
count
}
#[inline]
fn num_nonzero_except_llf(block: &[i32], cx: usize, cy: usize) -> i32 {
let row_stride = cx * 8;
let xsize_pixels = cx * 8;
let ysize_pixels = cy * 8;
let mut count: i32 = 0;
for v in 0..ysize_pixels {
for u in 0..xsize_pixels {
if v < cy && u < cx {
continue;
}
if block[v * row_stride + u] != 0 {
count += 1;
}
}
}
count
}
pub(crate) type QuantizeBlockAcFn = fn(
block_in: &[f32],
c: usize,
qm: &[f32],
quant: i32,
scale: f32,
qm_multiplier: f32,
distance: f32,
xsize: usize,
ysize: usize,
block_out: &mut [i32],
);
pub(crate) type QuantizeDcFn = fn(input: &[f32], scale: f32, output: &mut [i16]);
pub(crate) type QuantizeDcCflFn =
fn(input: &[f32], y_quant: &[i16], scale: f32, cfl: f32, output: &mut [i16]);
pub(crate) type QuantizeDcI32Fn = fn(input: &[f32], scale: f32, output: &mut [i32]);
pub(crate) type QuantizeDcCflI32Fn =
fn(input: &[f32], y_quant: &[i32], scale: f32, cfl: f32, output: &mut [i32]);
#[derive(Clone, Copy)]
pub(crate) struct QuantizeDcMethods {
pub(crate) quantize: QuantizeDcFn,
pub(crate) quantize_cfl: QuantizeDcCflFn,
pub(crate) quantize_i32: QuantizeDcI32Fn,
pub(crate) quantize_cfl_i32: QuantizeDcCflI32Fn,
}
pub(crate) fn dc_from_coefficients(
ctx: &EncodingContext,
raw_strategy: u8,
coeffs: &[f32],
dc: &mut [f32],
) {
match raw_strategy {
STRATEGY_DCT
| STRATEGY_IDENTITY
| STRATEGY_DCT2X2
| STRATEGY_DCT4X4
| STRATEGY_DCT4X8
| STRATEGY_DCT8X4
| STRATEGY_AFV0..=STRATEGY_AFV3 => dc[0] = coeffs[0],
STRATEGY_DCT16X8 => dc_from_dct16x8(
coeffs.first_chunk::<128>().unwrap(),
dc.first_chunk_mut::<2>().unwrap(),
),
STRATEGY_DCT8X16 => dc_from_dct8x16(
coeffs.first_chunk::<128>().unwrap(),
dc.first_chunk_mut::<2>().unwrap(),
),
STRATEGY_DCT16X16 => dc_from_dct16x16(
coeffs.first_chunk::<256>().unwrap(),
dc.first_chunk_mut::<4>().unwrap(),
),
STRATEGY_DCT32X32 => (ctx.dc_from_dct32x32)(
coeffs.first_chunk::<1024>().unwrap(),
dc.first_chunk_mut::<16>().unwrap(),
),
STRATEGY_DCT64X64 => (ctx.dc_from_dct64x64)(
coeffs.first_chunk::<4096>().unwrap(),
dc.first_chunk_mut::<64>().unwrap(),
),
STRATEGY_DCT64X32 => (ctx.dc_from_dct64x32)(
coeffs.first_chunk::<2048>().unwrap(),
dc.first_chunk_mut::<32>().unwrap(),
),
STRATEGY_DCT32X64 => (ctx.dc_from_dct32x64)(
coeffs.first_chunk::<2048>().unwrap(),
dc.first_chunk_mut::<32>().unwrap(),
),
STRATEGY_DCT32X16 => (ctx.dc_from_dct32x16)(
coeffs.first_chunk::<512>().unwrap(),
dc.first_chunk_mut::<8>().unwrap(),
),
STRATEGY_DCT16X32 => (ctx.dc_from_dct16x32)(
coeffs.first_chunk::<512>().unwrap(),
dc.first_chunk_mut::<8>().unwrap(),
),
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => (ctx.dc_from_dct8x32)(
coeffs.first_chunk::<256>().unwrap(),
dc.first_chunk_mut::<4>().unwrap(),
),
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 => (ctx.dc_from_dct128x128)(
coeffs.first_chunk::<16384>().unwrap(),
dc.first_chunk_mut::<256>().unwrap(),
),
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X64 => (ctx.dc_from_dct128x64)(
coeffs.first_chunk::<8192>().unwrap(),
dc.first_chunk_mut::<128>().unwrap(),
),
#[cfg(feature = "extraslow")]
STRATEGY_DCT64X128 => (ctx.dc_from_dct64x128)(
coeffs.first_chunk::<8192>().unwrap(),
dc.first_chunk_mut::<128>().unwrap(),
),
_ => unreachable!("invalid raw strategy {raw_strategy}"),
}
}
#[allow(dead_code)]
pub(crate) fn quantize_dc_i32_scalar(input: &[f32], scale: f32, output: &mut [i32]) {
debug_assert_eq!(input.len(), output.len());
for (&value, target) in input.iter().zip(output) {
*target = round_i32(value * scale);
}
}
#[allow(dead_code)]
pub(crate) fn quantize_dc_cfl_i32_scalar(
input: &[f32],
y_quant: &[i32],
scale: f32,
cfl: f32,
output: &mut [i32],
) {
debug_assert_eq!(input.len(), y_quant.len());
debug_assert_eq!(input.len(), output.len());
for ((&value, &yq), target) in input.iter().zip(y_quant).zip(output) {
*target = round_i32(fmla(value, scale, -(yq as f32) * cfl));
}
}
#[allow(dead_code)]
pub(crate) fn quantize_dc_scalar(input: &[f32], scale: f32, output: &mut [i16]) {
debug_assert_eq!(input.len(), output.len());
for (&value, target) in input.iter().zip(output) {
*target = (value * scale).round() as i16;
}
}
#[allow(dead_code)]
pub(crate) fn quantize_dc_cfl_scalar(
input: &[f32],
y_quant: &[i16],
scale: f32,
cfl: f32,
output: &mut [i16],
) {
debug_assert_eq!(input.len(), y_quant.len());
debug_assert_eq!(input.len(), output.len());
for ((&value, &yq), target) in input.iter().zip(y_quant).zip(output) {
*target = fmla(value, scale, -(yq as f32) * cfl).round() as i16;
}
}
static QUANTIZE_DC_METHODS: OnceLock<QuantizeDcMethods> = OnceLock::new();
#[cfg(all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm"))]
fn select_quantize_dc_methods() -> QuantizeDcMethods {
QuantizeDcMethods {
quantize: crate::wasm::quantize_dc_wasm,
quantize_cfl: crate::wasm::quantize_dc_cfl_wasm,
quantize_i32: crate::wasm::quantize_dc_i32_wasm,
quantize_cfl_i32: crate::wasm::quantize_dc_cfl_i32_wasm,
}
}
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
fn select_quantize_dc_methods() -> QuantizeDcMethods {
QuantizeDcMethods {
quantize: |input, scale, output| unsafe {
crate::neon::quantize_dc_neon(input, scale, output)
},
quantize_cfl: |input, y_quant, scale, cfl, output| unsafe {
crate::neon::quantize_dc_cfl_neon(input, y_quant, scale, cfl, output)
},
quantize_i32: |input, scale, output| unsafe {
crate::neon::quantize_dc_i32_neon(input, scale, output)
},
quantize_cfl_i32: |input, y_quant, scale, cfl, output| unsafe {
crate::neon::quantize_dc_cfl_i32_neon(input, y_quant, scale, cfl, output)
},
}
}
#[cfg(not(any(
all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm"),
all(target_arch = "aarch64", feature = "neon")
)))]
fn select_quantize_dc_methods() -> QuantizeDcMethods {
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("fma") {
return QuantizeDcMethods {
quantize: |input, scale, output| unsafe {
crate::avx::quantize_dc_avx2(input, scale, output)
},
quantize_cfl: |input, y_quant, scale, cfl, output| unsafe {
crate::avx::quantize_dc_cfl_avx2(input, y_quant, scale, cfl, output)
},
quantize_i32: |input, scale, output| unsafe {
crate::avx::quantize_dc_i32_avx2(input, scale, output)
},
quantize_cfl_i32: |input, y_quant, scale, cfl, output| unsafe {
crate::avx::quantize_dc_cfl_i32_avx2(input, y_quant, scale, cfl, output)
},
};
}
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "sse"))]
if is_x86_feature_detected!("sse4.1") {
return QuantizeDcMethods {
quantize: |input, scale, output| unsafe {
crate::sse::quantize_dc_sse41(input, scale, output)
},
quantize_cfl: |input, y_quant, scale, cfl, output| unsafe {
crate::sse::quantize_dc_cfl_sse41(input, y_quant, scale, cfl, output)
},
quantize_i32: |input, scale, output| unsafe {
crate::sse::quantize_dc_i32_sse41(input, scale, output)
},
quantize_cfl_i32: |input, y_quant, scale, cfl, output| unsafe {
crate::sse::quantize_dc_cfl_i32_sse41(input, y_quant, scale, cfl, output)
},
};
}
QuantizeDcMethods {
quantize: quantize_dc_scalar,
quantize_cfl: quantize_dc_cfl_scalar,
quantize_i32: quantize_dc_i32_scalar,
quantize_cfl_i32: quantize_dc_cfl_i32_scalar,
}
}
pub(crate) fn selected_quantize_dc_methods() -> QuantizeDcMethods {
*QUANTIZE_DC_METHODS.get_or_init(select_quantize_dc_methods)
}
static QUANTIZE_BLOCK_AC_METHOD: OnceLock<QuantizeBlockAcFn> = OnceLock::new();
#[inline]
pub(crate) fn quantize_ac_thresholds(
c: usize,
xsize: usize,
ysize: usize,
distance: f32,
) -> [f32; 4] {
quantize_ac_thresholds_scaled(c, xsize, ysize, distance, 1.0)
}
const X_THRESHOLD_EXTRA: f32 = 0.08;
const X_FIRST_THRESHOLD_WINDOW: f32 = 0.5;
static X_DEADZONE_WINDOW: [f32; 4] = [0.5, 0.75, 1.5, 2.0];
#[inline]
fn x_deadzone_window(distance: f32) -> f32 {
let [a, b, c, d] = X_DEADZONE_WINDOW;
let up = ((distance - a) / (b - a)).clamp(0.0, 1.0);
let down = ((d - distance) / (d - c)).clamp(0.0, 1.0);
up.min(down)
}
#[inline]
pub(crate) fn quantize_ac_thresholds_scaled(
c: usize,
xsize: usize,
ysize: usize,
distance: f32,
qm_multiplier: f32,
) -> [f32; 4] {
let mut normal = [0.58f32, 0.635, 0.66, 0.7];
if c == 0 {
let w = x_deadzone_window(distance);
normal[0] += w * (X_FIRST_THRESHOLD_WINDOW - normal[0]);
for t in &mut normal[1..] {
*t += (1.0 - w) * X_THRESHOLD_EXTRA;
}
}
if c == 2 {
let b_hf_threshold = if qm_multiplier > 1.0 { 0.65 } else { 0.75 };
for t in &mut normal[1..] {
*t = b_hf_threshold;
}
}
if xsize > 1 || ysize > 1 {
let delta =
(0.003_f32 * xsize as f32 * ysize as f32).clamp(0.0, if c > 0 { 0.08 } else { 0.12 });
for t in &mut normal {
*t -= delta;
}
}
let high_quality = match c {
1 => [0.50, 0.51, 0.52, 0.54],
0 | 2 => normal,
_ => unreachable!("invalid channel {c}"),
};
let t = ((distance - 0.1) / 0.9).clamp(0.0, 1.0);
std::array::from_fn(|i| high_quality[i] + t * (normal[i] - high_quality[i]))
}
#[inline]
pub(crate) fn quantize_ac_q_scaled(quant: i32, scale: f32, qm_multiplier: f32) -> f32 {
scale * quant as f32 * qm_multiplier
}
fn select_quantize_block_ac_fn() -> QuantizeBlockAcFn {
#[cfg(all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm"))]
{
crate::wasm::quantize_block_ac_wasm
}
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
{
|block_in, c, qm, quant, scale, qm_multiplier, distance, xsize, ysize, block_out| unsafe {
crate::neon::quantize_block_ac_neon(
block_in,
c,
qm,
quant,
scale,
qm_multiplier,
distance,
xsize,
ysize,
block_out,
);
}
}
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
{
if is_x86_feature_detected!("avx2") {
return |block_in,
c,
qm,
quant,
scale,
qm_multiplier,
distance,
xsize,
ysize,
block_out| unsafe {
crate::avx::quantize_block_ac_avx2(
block_in,
c,
qm,
quant,
scale,
qm_multiplier,
distance,
xsize,
ysize,
block_out,
);
};
}
}
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "sse"))]
{
if is_x86_feature_detected!("sse4.1") {
return |block_in,
c,
qm,
quant,
scale,
qm_multiplier,
distance,
xsize,
ysize,
block_out| unsafe {
crate::sse::quantize_block_ac_sse41(
block_in,
c,
qm,
quant,
scale,
qm_multiplier,
distance,
xsize,
ysize,
block_out,
);
};
}
}
#[cfg(not(any(
all(target_arch = "aarch64", feature = "neon"),
all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm")
)))]
{
quantize_block_ac_scalar
}
}
#[inline]
pub(crate) fn selected_quantize_block_ac_fn() -> QuantizeBlockAcFn {
*QUANTIZE_BLOCK_AC_METHOD.get_or_init(select_quantize_block_ac_fn)
}
#[allow(dead_code)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn quantize_block_ac_scalar(
block_in: &[f32],
c: usize,
qm: &[f32],
quant: i32,
scale: f32,
qm_multiplier: f32,
distance: f32,
xsize: usize,
ysize: usize,
block_out: &mut [i32],
) {
let thr = quantize_ac_thresholds_scaled(c, xsize, ysize, distance, qm_multiplier);
let q_scaled = quantize_ac_q_scaled(quant, scale, qm_multiplier);
let width = xsize * 8;
let height = ysize * 8;
debug_assert_eq!(
qm.len(),
width * height,
"quant matrix size {} != transform size {}x{}={} (wrong matrix for strategy?)",
qm.len(),
width,
height,
width * height
);
debug_assert!(block_in.len() >= width * height, "block_in too small");
debug_assert!(block_out.len() >= width * height, "block_out too small");
let n = width * height;
let qm = &qm[..n];
let block_in = &block_in[..n];
let block_out = &mut block_out[..n];
let half = width / 2;
for (y, ((qm_row, in_row), out_row)) in qm
.chunks_exact(width)
.zip(block_in.chunks_exact(width))
.zip(block_out.chunks_exact_mut(width))
.take(height)
.enumerate()
{
let yfix = if y >= height / 2 { 2 } else { 0 };
let thr_lo = thr[yfix];
let thr_hi = thr[yfix + 1];
for (x, ((&qmv, &inv), out)) in qm_row
.iter()
.zip(in_row.iter())
.zip(out_row.iter_mut())
.enumerate()
{
let threshold = if x >= half { thr_hi } else { thr_lo };
let q = qmv * q_scaled;
let val = q * inv;
*out = if val.abs() >= threshold {
val.round() as i32
} else {
0
};
}
}
}
pub(crate) static DEFAULT_QUANT_BIASES: [f32; 4] = [
1.0 - 0.05465007330715401,
1.0 - 0.07005449891748593,
1.0 - 0.049935103337343655,
0.145,
];
pub(crate) static EXPLICIT_QUANT_BIASES: [f32; 4] =
[1.0 - 0.054_650_075, 1.0 - 0.070_054_5, 1.0, 0.145];
const BIAS_QUOTIENTS: usize = 64;
const fn bias_quotients(numerator: f32) -> [f32; BIAS_QUOTIENTS] {
let mut table = [0.0; BIAS_QUOTIENTS];
let mut q = 2;
while q < BIAS_QUOTIENTS {
table[q] = numerator / q as f32;
q += 1;
}
table
}
static DEFAULT_BIAS_QUOTIENTS: [f32; BIAS_QUOTIENTS] = bias_quotients(DEFAULT_QUANT_BIASES[3]);
fn bias_quotients_for(numerator: f32) -> &'static [f32; BIAS_QUOTIENTS] {
if numerator.to_bits() == DEFAULT_QUANT_BIASES[3].to_bits() {
return &DEFAULT_BIAS_QUOTIENTS;
}
static EXPLICIT: std::sync::OnceLock<(u32, [f32; BIAS_QUOTIENTS])> = std::sync::OnceLock::new();
let (bits, table) = EXPLICIT.get_or_init(|| {
let mut table = [0.0; BIAS_QUOTIENTS];
for (q, t) in table.iter_mut().enumerate().skip(2) {
*t = numerator / q as f32;
}
(numerator.to_bits(), table)
});
assert_eq!(
*bits,
numerator.to_bits(),
"a third dequantization numerator"
);
table
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct DequantBias {
pub(crate) one: f32,
pub(crate) numerator: f32,
quotients: &'static [f32; BIAS_QUOTIENTS],
pub(crate) small_levels: [f32; SMALL_LEVELS],
}
pub(crate) const SMALL_LEVELS: usize = 16;
const fn small_levels(one: f32, quotients: &[f32; BIAS_QUOTIENTS]) -> [f32; SMALL_LEVELS] {
let mut table = [0.0; SMALL_LEVELS];
table[1] = one;
let mut q = 2;
while q < SMALL_LEVELS {
table[q] = q as f32 - quotients[q];
q += 1;
}
table
}
impl DequantBias {
pub(crate) const DEFAULT: [DequantBias; 3] = [
DequantBias::new(DEFAULT_QUANT_BIASES[0]),
DequantBias::new(DEFAULT_QUANT_BIASES[1]),
DequantBias::new(DEFAULT_QUANT_BIASES[2]),
];
const fn new(one: f32) -> Self {
Self {
one,
numerator: DEFAULT_QUANT_BIASES[3],
quotients: &DEFAULT_BIAS_QUOTIENTS,
small_levels: small_levels(one, &DEFAULT_BIAS_QUOTIENTS),
}
}
pub(crate) fn channels(explicit: bool) -> [DequantBias; 3] {
if !explicit {
return Self::DEFAULT;
}
let f16 = |v: f32| crate::util::f16_bits_to_f32(crate::util::f32_to_f16_bits(v));
let numerator = f16(EXPLICIT_QUANT_BIASES[3]);
let quotients = bias_quotients_for(numerator);
std::array::from_fn(|c| {
let one = f16(EXPLICIT_QUANT_BIASES[c]);
DequantBias {
one,
numerator,
quotients,
small_levels: small_levels(one, quotients),
}
})
}
#[inline]
pub(crate) fn level_f32(self, quant: f32) -> f32 {
let aq = quant.abs();
if aq < 1.125 {
if quant == 0.0 {
0.0
} else {
self.one.copysign(quant)
}
} else {
let a = aq as usize;
if a < BIAS_QUOTIENTS && a as f32 == aq {
let t = self.quotients[a];
quant - if quant < 0.0 { -t } else { t }
} else {
quant - self.numerator / quant
}
}
}
#[inline]
pub(crate) fn level(self, quant: i32) -> f32 {
let a = quant.unsigned_abs();
if a <= 1 {
if quant == 0 {
0.0
} else if quant > 0 {
self.one
} else {
-self.one
}
} else {
let q = quant as f32;
if (a as usize) < BIAS_QUOTIENTS {
let t = self.quotients[a as usize];
q - if quant < 0 { -t } else { t }
} else {
q - self.numerator / q
}
}
}
}
fn quantize_roundtrip_y_block(
ctx: &EncodingContext,
qm: &[f32],
dqm: &[f32],
scale: f32,
quant: i32,
distance: f32,
xsize: usize,
ysize: usize,
inout: &mut [f32],
quantized: &mut [i32],
) {
(ctx.quantize_block_ac)(
inout, 1, qm, quant, scale, 1.0, distance, xsize, ysize, quantized,
);
let inv_qac = 1.0 / (scale * quant as f32);
let bias = ctx.dequant_bias(1);
let size = xsize * ysize * 64;
for (out, (&q, &dq)) in inout[..size]
.iter_mut()
.zip(quantized[..size].iter().zip(dqm[..size].iter()))
{
*out = bias.level(q) * dq * inv_qac;
}
}
pub(crate) type ReconCapture<'a> = (
&'a mut crate::image::Image3F,
usize,
&'a mut Vec<(u16, u16, u32)>,
);
pub(crate) struct AcBlockCache {
xsize_blocks: usize,
keys: [Vec<u32>; 3],
quantized: [Vec<i32>; 3],
dc: Image3S,
dc_f: [Vec<f32>; 3],
recon: Image3F,
tokens: Vec<Vec<Token>>,
spans: Vec<(u32, u32)>,
nz_shift: Vec<[u8; 3]>,
pub(crate) epoch: u32,
cell_gen: Vec<u32>,
bits: Vec<[f32; 3]>,
pub(crate) trial: bool,
pub(crate) trial_bits: f64,
}
impl AcBlockCache {
pub(crate) fn new(
xsize_blocks: usize,
ysize_blocks: usize,
xsize: usize,
ysize: usize,
) -> Self {
let n = xsize_blocks * ysize_blocks;
Self {
xsize_blocks,
keys: std::array::from_fn(|_| vec![u32::MAX; n]),
quantized: std::array::from_fn(|_| vec![0; n * 64]),
dc: Image3S::new(xsize_blocks, ysize_blocks),
dc_f: std::array::from_fn(|_| vec![0.0; n]),
recon: Image3F::new(xsize, ysize),
tokens: Vec::new(),
spans: Vec::new(),
nz_shift: vec![[0; 3]; n],
epoch: 0,
cell_gen: vec![0; n],
bits: vec![[f32::NAN; 3]; n],
trial: false,
trial_bits: 0.0,
}
}
#[cfg(feature = "extraslow")]
pub(crate) fn invalidate(&mut self, x: usize, y: usize, w: usize, h: usize) {
for row in y..y + h {
for key in &mut self.keys {
key[row * self.xsize_blocks + x..row * self.xsize_blocks + x + w].fill(u32::MAX);
}
}
}
#[cfg(feature = "extraslow")]
pub(crate) fn compact_tokens(&mut self) {
let streams = self.tokens.len();
for (s, arena) in self.tokens.iter_mut().enumerate() {
let live: usize = self.spans[s..]
.iter()
.step_by(streams)
.map(|&(_, len)| len as usize)
.sum();
if arena.len() <= live.saturating_mul(2).max(4096) {
continue;
}
let mut compact = Vec::with_capacity(live);
for (start, len) in self.spans[s..].iter_mut().step_by(streams) {
let old_start = *start as usize;
*start = compact.len() as u32;
compact.extend_from_slice(&arena[old_start..old_start + *len as usize]);
}
*arena = compact;
}
}
#[cfg(feature = "extraslow")]
pub(crate) fn snapshot(&self, x: usize, y: usize, w: usize, h: usize) -> MemoSnapshot {
let mut snap = MemoSnapshot {
rect: (x, y, w, h),
..Default::default()
};
let streams = if self.keys[0].is_empty() {
0
} else {
self.spans.len() / (self.keys[0].len() * 3)
};
let (pw, ph) = (self.recon.xsize(), self.recon.ysize());
for row in y..y + h {
let cells = row * self.xsize_blocks + x..row * self.xsize_blocks + x + w;
for c in 0..3 {
snap.keys.extend_from_slice(&self.keys[c][cells.clone()]);
snap.quantized
.extend_from_slice(&self.quantized[c][cells.start * 64..cells.end * 64]);
snap.dc
.extend_from_slice(&self.dc.plane_row(c, row)[x..x + w]);
snap.dc_f.extend_from_slice(&self.dc_f[c][cells.clone()]);
}
if streams > 0 {
snap.spans.extend_from_slice(
&self.spans[cells.start * 3 * streams..cells.end * 3 * streams],
);
}
snap.nz_shift
.extend_from_slice(&self.nz_shift[cells.clone()]);
snap.bits.extend_from_slice(&self.bits[cells.clone()]);
snap.cell_gen.extend_from_slice(&self.cell_gen[cells]);
}
for c in 0..3 {
for py in (y * 8).min(ph)..((y + h) * 8).min(ph) {
snap.recon.extend_from_slice(
&self.recon.plane_row(c, py)[(x * 8).min(pw)..((x + w) * 8).min(pw)],
);
}
}
snap.streams = streams;
snap
}
#[cfg(feature = "extraslow")]
pub(crate) fn restore(&mut self, snap: &MemoSnapshot) {
let (x, y, w, h) = snap.rect;
let streams = if self.keys[0].is_empty() {
0
} else {
self.spans.len() / (self.keys[0].len() * 3)
};
let (pw, ph) = (self.recon.xsize(), self.recon.ysize());
let (mut k, mut q, mut sp, mut n) = (0, 0, 0, 0);
for row in y..y + h {
let cells = row * self.xsize_blocks + x..row * self.xsize_blocks + x + w;
for c in 0..3 {
self.keys[c][cells.clone()].copy_from_slice(&snap.keys[k..k + w]);
self.quantized[c][cells.start * 64..cells.end * 64]
.copy_from_slice(&snap.quantized[q..q + w * 64]);
self.dc.plane_row_mut(c, row)[x..x + w].copy_from_slice(&snap.dc[k..k + w]);
self.dc_f[c][cells.clone()].copy_from_slice(&snap.dc_f[k..k + w]);
k += w;
q += w * 64;
}
if streams > 0 && streams == snap.streams {
let len = w * 3 * streams;
self.spans[cells.start * 3 * streams..cells.end * 3 * streams]
.copy_from_slice(&snap.spans[sp..sp + len]);
sp += len;
} else if streams > 0 {
for key in &mut self.keys {
key[cells.clone()].fill(u32::MAX);
}
}
self.nz_shift[cells.clone()].copy_from_slice(&snap.nz_shift[n..n + w]);
self.bits[cells.clone()].copy_from_slice(&snap.bits[n..n + w]);
self.cell_gen[cells].fill(self.epoch);
n += w;
}
let mut r = 0;
for c in 0..3 {
for py in (y * 8).min(ph)..((y + h) * 8).min(ph) {
let row =
&mut self.recon.plane_row_mut(c, py)[(x * 8).min(pw)..((x + w) * 8).min(pw)];
let len = row.len();
row.copy_from_slice(&snap.recon[r..r + len]);
r += len;
}
}
}
pub(crate) fn recon_mut(&mut self) -> &mut Image3F {
&mut self.recon
}
#[cfg(feature = "extraslow")]
pub(crate) fn sync_into(
&self,
synced: u32,
dst: &mut Image3F,
x0: usize,
y0: usize,
) -> Option<(usize, usize, usize, usize)> {
let (mut rx0, mut ry0, mut rx1, mut ry1) = (usize::MAX, usize::MAX, 0usize, 0usize);
let (w, h) = (self.recon.xsize(), self.recon.ysize());
for (i, &g) in self.cell_gen.iter().enumerate() {
if g <= synced {
continue;
}
let (bx, by) = (i % self.xsize_blocks, i / self.xsize_blocks);
let (cx0, cy0) = (bx * 8, by * 8);
let (cx1, cy1) = ((cx0 + 8).min(w), (cy0 + 8).min(h));
if cx0 >= cx1 || cy0 >= cy1 {
continue;
}
let differs = (0..3).any(|c| {
(cy0..cy1).any(|y| {
self.recon.plane_row(c, y)[cx0..cx1]
!= dst.plane_row(c, y0 + y)[x0 + cx0..x0 + cx1]
})
});
if !differs {
continue;
}
for c in 0..3 {
for y in cy0..cy1 {
dst.plane_row_mut(c, y0 + y)[x0 + cx0..x0 + cx1]
.copy_from_slice(&self.recon.plane_row(c, y)[cx0..cx1]);
}
}
rx0 = rx0.min(x0 + cx0);
ry0 = ry0.min(y0 + cy0);
rx1 = rx1.max(x0 + cx1);
ry1 = ry1.max(y0 + cy1);
}
(rx1 > 0).then_some((rx0, ry0, rx1, ry1))
}
}
#[cfg(feature = "extraslow")]
#[derive(Default)]
pub(crate) struct MemoSnapshot {
rect: (usize, usize, usize, usize),
streams: usize,
keys: Vec<u32>,
quantized: Vec<i32>,
dc: Vec<i16>,
dc_f: Vec<f32>,
spans: Vec<(u32, u32)>,
nz_shift: Vec<[u8; 3]>,
bits: Vec<[f32; 3]>,
cell_gen: Vec<u32>,
recon: Vec<f32>,
}
struct ReconScratch {
err: Box<[f32; MAX_BLOCK_COEFFICIENTS]>,
pix: Box<[f32; MAX_BLOCK_COEFFICIENTS]>,
}
impl Default for ReconScratch {
fn default() -> Self {
Self {
err: heap_array(0.0),
pix: heap_array(0.0),
}
}
}
#[cfg(feature = "extraslow")]
pub(crate) type PolisherSlot = Option<Box<crate::coef_polish::CoefPolisher>>;
#[cfg(not(feature = "extraslow"))]
pub(crate) type PolisherSlot = Option<Box<()>>;
pub(crate) struct AcGroupScratch {
pub(crate) polisher: PolisherSlot,
coeffs: HeapMatrix<f32, 3, MAX_BLOCK_COEFFICIENTS>,
quantized: HeapMatrix<i32, 3, MAX_BLOCK_COEFFICIENTS>,
source_y: Option<Box<[f32; MAX_BLOCK_COEFFICIENTS]>>,
block: Box<[i32; MAX_BLOCK_COEFFICIENTS]>,
rdoq: Option<RdoqScratch>,
gather: Box<[f32; MAX_BLOCK_COEFFICIENTS]>,
few_color_keys: Vec<u64>,
recon: Option<ReconScratch>,
clip: Option<Box<crate::clip_continuation::ClipScratch>>,
}
impl AcGroupScratch {
pub(crate) fn transform_buffers(
&mut self,
) -> (
&mut [[f32; MAX_BLOCK_COEFFICIENTS]; 3],
&mut [f32; MAX_BLOCK_COEFFICIENTS],
) {
(&mut self.coeffs, &mut self.gather)
}
}
impl Default for AcGroupScratch {
fn default() -> Self {
Self {
polisher: None,
coeffs: HeapMatrix::new(0.0),
quantized: HeapMatrix::new(0),
source_y: None,
block: heap_array(0),
rdoq: None,
gather: heap_array(0.0),
few_color_keys: Vec::new(),
clip: None,
recon: None,
}
}
}
#[derive(Clone)]
pub(crate) struct SourceDc {
pub(crate) y: crate::image::Plane<f32>,
pub(crate) b: crate::image::Plane<f32>,
pub(crate) x: crate::image::Plane<f32>,
}
#[inline]
fn pass_coefficient(c: i32, coeff_shifts: &[u32], pass: usize) -> i32 {
let mut remaining = c;
let mut sent = 0;
for &shift in &coeff_shifts[..=pass] {
sent = remaining.signum() * (remaining.abs() >> shift);
remaining -= sent << shift;
}
sent
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn write_ac_group(
ctx: &EncodingContext,
scratch: &mut AcGroupScratch,
opsin: &Image3F,
opsin_origin: (usize, usize),
group_brect: Rect,
scale: f32,
scale_dc: f32,
dc_step: [f32; 3],
distance: f32,
x_qm_scale: u32,
dc_data: &DcGroupData,
ytob_dc: i32,
quant_dc: &mut Image3S,
mut source_dc: Option<&mut SourceDc>,
qorigin_x: usize,
qorigin_y: usize,
num_nzeros: &mut [Image3B],
coeff_shifts: &[u32],
rdoq_prices: Option<&FrozenTokenPrices>,
coeff_orders: &crate::coeff_order::CoeffOrders,
alternative_orders: &[crate::coeff_order::CoeffOrders],
mut order_stats: Option<&mut crate::coeff_order::OrderStats>,
measure_chroma_distortion: bool,
qf_threshold: u32,
mut recon_y: Option<ReconCapture<'_>>,
mut memo: Option<&mut AcBlockCache>,
out: &mut [Vec<Token>],
) -> f32 {
let (out, alternative_out) = out.split_at_mut(coeff_shifts.len());
debug_assert_eq!(alternative_out.len(), alternative_orders.len());
debug_assert!(alternative_out.is_empty() || coeff_shifts.len() == 1);
let matrices = ctx.matrices();
let xsize_blocks = group_brect.xsize;
let ysize_blocks = group_brect.ysize;
let inv_factor = [
INV_DC_QUANT[0] / dc_step[0] * scale_dc,
INV_DC_QUANT[1] / dc_step[1] * scale_dc,
INV_DC_QUANT[2] / dc_step[2] * scale_dc,
];
let cfl_factor_b = crate::color_correlation::dc_cfl_factor(dc_step, ytob_dc, ctx.cfl_frame());
let x_qm_mul = 1.25f32.powf(x_qm_scale as f32 - 2.0);
let nzeros_by0 = group_brect.y0 % K_GROUP_DIM_IN_BLOCKS;
let mut chroma_distortion = 0.0f32;
let cfl_factor_x = crate::color_correlation::dc_cfl_factor_x(dc_step, ctx.cfl_base_x());
let AcGroupScratch {
polisher,
coeffs,
quantized,
source_y,
block: pblock,
rdoq,
gather,
few_color_keys,
recon: recon_scratch,
clip: clip_scratch,
} = scratch;
#[cfg(not(feature = "extraslow"))]
let _ = polisher;
let mut stream_starts = Vec::new();
for by in 0..ysize_blocks {
let nz_by = nzeros_by0 + by;
let global_by = group_brect.y0 + by;
for bx in 0..xsize_blocks {
let global_bx = group_brect.x0 + bx;
if !dc_data.ac_strategy.is_first_block(global_bx, global_by) {
continue;
}
if let Some((_, _, starts)) = recon_y.as_mut() {
starts.push((global_bx as u16, global_by as u16, out[0].len() as u32));
}
let raw_strategy = dc_data.ac_strategy.raw_strategy(global_bx, global_by);
let cov_x = AcStrategyImage::covered_blocks_x_of(raw_strategy);
let cov_y = AcStrategyImage::covered_blocks_y_of(raw_strategy);
let (cx, cy) = if cov_y > cov_x {
(cov_y, cov_x)
} else {
(cov_x, cov_y)
};
let size = cx * cy * 64;
let quant_ac = dc_data.raw_quant_field.row(global_by)[global_bx] as i32;
let lby = global_by - qorigin_y;
let predicted_ctx: [u8; 3] = std::array::from_fn(|c| {
let nzero_map = &num_nzeros[0];
let row_top = (nz_by != 0).then(|| nzero_map.plane_row(c, nz_by - 1));
predict_from_top_and_left(row_top, nzero_map.plane_row(c, nz_by), bx, 32)
});
let memo_keys: [u32; 3] = [
quant_ac as u32
| u32::from(predicted_ctx[0]) << 8
| u32::from(predicted_ctx[1]) << 16
| x_qm_scale << 24,
quant_ac as u32 | u32::from(predicted_ctx[1]) << 8,
quant_ac as u32
| u32::from(predicted_ctx[2]) << 8
| u32::from(predicted_ctx[1]) << 16
| ctx.b_qm_scale() << 24,
];
let bi = lby * memo.as_deref().map_or(0, |m| m.xsize_blocks) + bx;
let streams = out.len() + alternative_out.len();
let token_memo = coeff_shifts.len() == 1 && order_stats.is_none();
let tokens_cached = token_memo
&& memo
.as_deref()
.is_some_and(|m| m.spans.len() == m.keys[0].len() * streams * 3);
let hit: [bool; 3] = std::array::from_fn(|c| {
memo.as_deref()
.is_some_and(|m| m.keys[c][bi] == memo_keys[c])
});
debug_assert!(memo.is_none() || !measure_chroma_distortion);
if let Some(m) = memo.as_deref() {
for c in 0..3 {
if !hit[c] {
continue;
}
for j in 0..cov_y {
if !tokens_cached {
for i in 0..cov_x {
let slot = ((lby + j) * m.xsize_blocks + bx + i) * 64;
let k = (j * cov_x + i) * 64;
quantized[c][k..k + 64]
.copy_from_slice(&m.quantized[c][slot..slot + 64]);
}
}
let src = &m.dc.plane_row(c, lby + j)[bx..bx + cov_x];
quant_dc.plane_row_mut(c, lby + j)[bx..bx + cov_x].copy_from_slice(src);
}
}
}
let memo_hit = hit.iter().all(|&h| h);
if !memo_hit {
let footprint = Footprint::of_strategy(
opsin,
opsin_origin.0 + bx * 8,
opsin_origin.1 + by * 8,
raw_strategy,
);
debug_assert_eq!((footprint.width, footprint.height), (cov_x * 8, cov_y * 8));
let visible = footprint.visible_fraction();
let restore_penalty = if rdoq_prices.is_some() {
let x0 = opsin_origin.0 + bx * 8;
let y0 = opsin_origin.1 + by * 8;
let x1 = (x0 + cov_x * 8).min(opsin.xsize());
let y1 = (y0 + cov_y * 8).min(opsin.ysize());
let limit = RESTORE_FEW_COLORS_PER_BLOCK * cov_x * cov_y;
if few_colors(opsin, x0, y0, x1, y1, limit, few_color_keys) {
RESTORE_FEW_COLORS_PENALTY
} else {
1.0
}
} else {
1.0
};
let (inv_qm_y, qm_y): (&[f32], &[f32]) = match raw_strategy {
STRATEGY_DCT => (&matrices.inv_matrix(1)[..], &matrices.matrix(1)[..]),
STRATEGY_IDENTITY => (
&matrices.inv_matrix_identity(1)[..],
&matrices.matrix_identity(1)[..],
),
STRATEGY_DCT2X2 => (
&matrices.inv_matrix_dct2x2(1)[..],
&matrices.matrix_dct2x2(1)[..],
),
STRATEGY_DCT4X4 => (&matrices.inv_matrix_4x4(1)[..], &matrices.matrix_4x4(1)[..]),
STRATEGY_DCT4X8 | STRATEGY_DCT8X4 => {
(&matrices.inv_matrix_4x8(1)[..], &matrices.matrix_4x8(1)[..])
}
STRATEGY_AFV0..=STRATEGY_AFV3 => {
(&matrices.inv_matrix_afv(1)[..], &matrices.matrix_afv(1)[..])
}
STRATEGY_DCT16X16 => (&matrices.inv_matrix_16x16(1)[..], &matrices.matrix_16x16(1)[..]),
STRATEGY_DCT32X32 => (&matrices.inv_matrix_32x32(1)[..], &matrices.matrix_32x32(1)[..]),
STRATEGY_DCT64X64 => (&matrices.inv_matrix_64x64(1)[..], &matrices.matrix_64x64(1)[..]),
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 | STRATEGY_DCT128X64 | STRATEGY_DCT64X128 => (
matrices.inv_matrix_128(raw_strategy, 1),
matrices.matrix_128(raw_strategy, 1),
),
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => {
(&matrices.inv_matrix_64x32(1)[..], &matrices.matrix_64x32(1)[..])
}
STRATEGY_DCT32X16 | STRATEGY_DCT16X32 => {
(&matrices.inv_matrix_32x16(1)[..], &matrices.matrix_32x16(1)[..])
}
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => {
(&matrices.inv_matrix_8x32(1)[..], &matrices.matrix_8x32(1)[..])
}
_ => (&matrices.inv_matrix_16x8(1)[..], &matrices.matrix_16x8(1)[..]),
};
let clip_shift = ctx.speed.effort().clip_continuation && (!hit[1] || !hit[2]) && {
let (input, stride) = footprint.pixels(opsin.plane(1), &mut gather[..]);
crate::clip_continuation::continue_saturated(
ctx,
raw_strategy,
cx,
cy,
inv_qm_y,
qm_y,
quant_ac,
scale,
distance,
input,
stride,
clip_scratch.get_or_insert_with(Default::default),
)
};
for c in 0..3 {
if hit[c] {
continue;
}
let (input, stride) = footprint.pixels(opsin.plane(c), &mut gather[..]);
let (input, stride) = if clip_shift && c != 0 {
let (pw, ph) = crate::inflated_cost::strategy_pixel_dims(raw_strategy);
(
crate::clip_continuation::shifted_input(
clip_scratch.as_mut().expect("shift computed"),
input,
stride,
pw,
ph,
),
pw,
)
} else {
(input, stride)
};
match raw_strategy {
STRATEGY_DCT => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.dct8x8)(DctInput::new(input, stride), dst);
}
STRATEGY_IDENTITY => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.identity8x8)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT2X2 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.dct2x2_8x8)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT16X8 => {
let dst: &mut [f32; 128] = coeffs[c].first_chunk_mut::<128>().unwrap();
(ctx.dct16x8)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT8X16 => {
let dst: &mut [f32; 128] = coeffs[c].first_chunk_mut::<128>().unwrap();
(ctx.dct8x16)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT16X16 => {
let dst: &mut [f32; 256] = coeffs[c].first_chunk_mut::<256>().unwrap();
(ctx.dct16x16)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT32X32 => {
let dst: &mut [f32; 1024] =
coeffs[c].first_chunk_mut::<1024>().unwrap();
(ctx.dct32x32)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT64X64 => {
let dst: &mut [f32; 4096] =
coeffs[c].first_chunk_mut::<4096>().unwrap();
(ctx.dct64x64)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT64X32 => {
let dst: &mut [f32; 2048] =
coeffs[c].first_chunk_mut::<2048>().unwrap();
(ctx.dct64x32)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT32X64 => {
let dst: &mut [f32; 2048] =
coeffs[c].first_chunk_mut::<2048>().unwrap();
(ctx.dct32x64)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT4X4 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.dct4x4)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT4X8 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.dct4x8)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT8X4 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.dct8x4)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT32X16 => {
let dst: &mut [f32; 512] = coeffs[c].first_chunk_mut::<512>().unwrap();
(ctx.dct32x16)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT16X32 => {
let dst: &mut [f32; 512] = coeffs[c].first_chunk_mut::<512>().unwrap();
(ctx.dct16x32)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT32X8 => {
let dst: &mut [f32; 256] = coeffs[c].first_chunk_mut::<256>().unwrap();
(ctx.dct32x8)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT8X32 => {
let dst: &mut [f32; 256] = coeffs[c].first_chunk_mut::<256>().unwrap();
(ctx.dct8x32)(DctInput::new(input, stride), dst);
}
STRATEGY_AFV0 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.afv0)(DctInput::new(input, stride), dst);
}
STRATEGY_AFV1 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.afv1)(DctInput::new(input, stride), dst);
}
STRATEGY_AFV2 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.afv2)(DctInput::new(input, stride), dst);
}
STRATEGY_AFV3 => {
let dst: &mut [f32; 64] = coeffs[c].first_chunk_mut::<64>().unwrap();
(ctx.afv3)(DctInput::new(input, stride), dst);
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 => {
let dst = coeffs[c].first_chunk_mut::<16384>().unwrap();
(ctx.dct128x128)(DctInput::new(input, stride), dst);
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X64 => {
let dst = coeffs[c].first_chunk_mut::<8192>().unwrap();
(ctx.dct128x64)(DctInput::new(input, stride), dst);
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT64X128 => {
let dst = coeffs[c].first_chunk_mut::<8192>().unwrap();
(ctx.dct64x128)(DctInput::new(input, stride), dst);
}
_ => unreachable!("invalid raw strategy {}", raw_strategy),
}
}
let mut dc_vals = [[0.0f32; 256]; 3];
for c in 0..3 {
dc_from_coefficients(ctx, raw_strategy, &coeffs[c], &mut dc_vals[c]);
}
if let Some(m) = memo.as_deref() {
for c in 0..3 {
if hit[c] {
for iy in 0..cov_y {
let row = (lby + iy) * m.xsize_blocks;
dc_vals[c][iy * cov_x..(iy + 1) * cov_x]
.copy_from_slice(&m.dc_f[c][row + bx..row + bx + cov_x]);
}
}
}
}
let covered_dc = cov_x * cov_y;
let mut y_dc_q = [0i16; 256];
if let Some(m) = memo.as_deref().filter(|_| hit[1]) {
for iy in 0..cov_y {
y_dc_q[iy * cov_x..(iy + 1) * cov_x]
.copy_from_slice(&m.dc.plane_row(1, lby + iy)[bx..bx + cov_x]);
}
} else {
(ctx.quantize_dc)(
&dc_vals[1][..covered_dc],
inv_factor[1],
&mut y_dc_q[..covered_dc],
);
}
if let Some(source) = source_dc.as_mut() {
for iy in 0..cov_y {
let bx = global_bx - qorigin_x;
source.y.row_mut(global_by - qorigin_y + iy)[bx..bx + cov_x]
.copy_from_slice(&dc_vals[1][iy * cov_x..(iy + 1) * cov_x]);
}
}
for iy in 0..cov_y {
let lbx = global_bx - qorigin_x;
let quant_target = &mut quant_dc.plane_row_mut(1, global_by - qorigin_y + iy)
[lbx..lbx + cov_x];
let row_start = iy * cov_x;
quant_target.copy_from_slice(&y_dc_q[row_start..row_start + cov_x]);
}
if let Some(m) = memo.as_deref().filter(|_| hit[1]) {
for j in 0..cov_y {
for i in 0..cov_x {
let slot = ((lby + j) * m.xsize_blocks + bx + i) * 64;
let k = (j * cov_x + i) * 64;
quantized[1][k..k + 64]
.copy_from_slice(&m.quantized[1][slot..slot + 64]);
}
}
let inv_qac = 1.0 / (scale * quant_ac as f32);
for (out, (&q, &dq)) in coeffs[1][..size]
.iter_mut()
.zip(quantized[1][..size].iter().zip(qm_y[..size].iter()))
{
*out = ctx.dequant_bias(1).level(q) * dq * inv_qac;
}
} else {
if rdoq_prices.is_some() || recon_y.is_some() {
let source_y = source_y.get_or_insert_with(|| heap_array(0.0));
source_y[..size].copy_from_slice(&coeffs[1][..size]);
}
quantize_roundtrip_y_block(
ctx,
inv_qm_y,
qm_y,
scale,
quant_ac,
distance,
cx,
cy,
&mut coeffs[1][..size],
&mut quantized[1][..size],
);
if let Some(prices) = rdoq_prices {
let RdoqScratch {
choices: rdoq_choices,
costs: rdoq_costs,
} = rdoq.get_or_insert_with(RdoqScratch::default);
let strategy_code = dc_data.ac_strategy.strategy_code(global_bx, global_by);
let nzero_map = &num_nzeros[0];
let row_top = (nz_by != 0).then(|| nzero_map.plane_row(1, nz_by - 1));
let predicted = predict_from_top_and_left(
row_top,
nzero_map.plane_row(1, nz_by),
bx,
32,
);
rdoq_block(
rdoq_channel_weights(ctx.coding)[1],
prices,
coeff_orders.scan_for(strategy_code, 1),
&source_y.as_ref().expect("luma saved for RDOQ")[..size],
inv_qm_y,
quantize_ac_q_scaled(quant_ac, scale, 1.0),
&mut quantized[1][..size],
raw_strategy,
strategy_code,
1,
ctx.dequant_bias(1),
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
rdoq_lambda(ctx.arbiter, quant_ac as f32 / qf_threshold.max(1) as f32)
/ visible,
restore_penalty,
RdoqSearch::of(ctx.speed.effort()),
rdoq_choices,
rdoq_costs,
);
let inv_qac = 1.0 / (scale * quant_ac as f32);
for (out, (&q, &dq)) in coeffs[1][..size]
.iter_mut()
.zip(quantized[1][..size].iter().zip(qm_y[..size].iter()))
{
*out = ctx.dequant_bias(1).level(q) * dq * inv_qac;
}
}
}
if !hit[1]
&& let Some((recon, y_off, _)) = recon_y.as_mut()
{
let ReconScratch {
err: recon_err,
pix: recon_pix,
} = recon_scratch.get_or_insert_with(ReconScratch::default);
let source_y = source_y.as_ref().expect("luma saved for reconstruction");
let y_off = *y_off;
for ((e, &q), &src) in recon_err[..size]
.iter_mut()
.zip(&coeffs[1][..size])
.zip(&source_y[..size])
{
*e = q - src;
}
let wc = cx * 8;
for row in recon_err[..cy * wc].chunks_exact_mut(wc) {
row[..cx].fill(0.0);
}
crate::inflated_cost::reconstruct_error(
ctx.idct,
raw_strategy,
&recon_err[..size],
&mut recon_pix[..size],
);
let (pw, ph) = crate::inflated_cost::strategy_pixel_dims(raw_strategy);
let (px0, py0) = (opsin_origin.0 + bx * 8, opsin_origin.1 + by * 8);
let plane = opsin.plane(1);
let recon = recon.plane_mut(1);
for y in 0..ph {
let (sy, ly) = (py0 + y, y_off + by * 8 + y);
if sy >= plane.ysize() || ly >= recon.ysize() {
break;
}
let src_row = plane.row(sy);
let dst_row = recon.row_mut(ly);
for x in 0..pw {
let (sx, lx) = (px0 + x, bx * 8 + x);
if sx >= src_row.len() || lx >= dst_row.len() {
break;
}
dst_row[lx] = src_row[sx] + recon_pix[y * pw + x];
}
}
}
let tx = global_bx / 8;
let ty = global_by / 8;
let cmap_x = dc_data.ytox_map.row(ty)[tx];
let cmap_b = dc_data.ytob_map.row(ty)[tx];
let x_factor = crate::color_correlation::y_to_x_ratio(ctx.cfl_frame(), cmap_x);
let b_factor = crate::color_correlation::y_to_b_ratio(ctx.cfl_frame(), cmap_b);
{
let wc = cx * 8;
for row in coeffs[1][..cy * wc].chunks_exact_mut(wc) {
row[..cx].fill(0.0);
}
}
{
let [c0, c1, c2] = &mut **coeffs;
(ctx.apply_cfl)(
&mut c0[..size],
&c1[..size],
&mut c2[..size],
[x_factor, 0.0, b_factor],
);
}
let mut x_dc_post = [0.0f32; 256];
let mut b_dc_post = [0.0f32; 256];
match raw_strategy {
STRATEGY_DCT
| STRATEGY_IDENTITY
| STRATEGY_DCT2X2
| STRATEGY_DCT4X4
| STRATEGY_DCT4X8
| STRATEGY_DCT8X4
| STRATEGY_AFV0..=STRATEGY_AFV3 => {
x_dc_post[0] = coeffs[0][0];
b_dc_post[0] = coeffs[2][0];
}
STRATEGY_DCT16X8 => {
let xb: &[f32; 128] = coeffs[0].first_chunk::<128>().unwrap();
let bb: &[f32; 128] = coeffs[2].first_chunk::<128>().unwrap();
dc_from_dct16x8(xb, x_dc_post.first_chunk_mut::<2>().unwrap());
dc_from_dct16x8(bb, b_dc_post.first_chunk_mut::<2>().unwrap());
}
STRATEGY_DCT8X16 => {
let xb: &[f32; 128] = coeffs[0].first_chunk::<128>().unwrap();
let bb: &[f32; 128] = coeffs[2].first_chunk::<128>().unwrap();
dc_from_dct8x16(xb, x_dc_post.first_chunk_mut::<2>().unwrap());
dc_from_dct8x16(bb, b_dc_post.first_chunk_mut::<2>().unwrap());
}
STRATEGY_DCT16X16 => {
let xb: &[f32; 256] = coeffs[0].first_chunk::<256>().unwrap();
let bb: &[f32; 256] = coeffs[2].first_chunk::<256>().unwrap();
dc_from_dct16x16(xb, x_dc_post.first_chunk_mut::<4>().unwrap());
dc_from_dct16x16(bb, b_dc_post.first_chunk_mut::<4>().unwrap());
}
STRATEGY_DCT32X32 => {
let xb: &[f32; 1024] = coeffs[0].first_chunk::<1024>().unwrap();
let bb: &[f32; 1024] = coeffs[2].first_chunk::<1024>().unwrap();
(ctx.dc_from_dct32x32)(xb, x_dc_post.first_chunk_mut::<16>().unwrap());
(ctx.dc_from_dct32x32)(bb, b_dc_post.first_chunk_mut::<16>().unwrap());
}
STRATEGY_DCT64X64 => {
let xb: &[f32; 4096] = coeffs[0].first_chunk::<4096>().unwrap();
let bb: &[f32; 4096] = coeffs[2].first_chunk::<4096>().unwrap();
(ctx.dc_from_dct64x64)(xb, x_dc_post.first_chunk_mut::<64>().unwrap());
(ctx.dc_from_dct64x64)(bb, b_dc_post.first_chunk_mut::<64>().unwrap());
}
STRATEGY_DCT64X32 => {
let xb: &[f32; 2048] = coeffs[0].first_chunk::<2048>().unwrap();
let bb: &[f32; 2048] = coeffs[2].first_chunk::<2048>().unwrap();
(ctx.dc_from_dct64x32)(xb, x_dc_post.first_chunk_mut::<32>().unwrap());
(ctx.dc_from_dct64x32)(bb, b_dc_post.first_chunk_mut::<32>().unwrap());
}
STRATEGY_DCT32X64 => {
let xb: &[f32; 2048] = coeffs[0].first_chunk::<2048>().unwrap();
let bb: &[f32; 2048] = coeffs[2].first_chunk::<2048>().unwrap();
(ctx.dc_from_dct32x64)(xb, x_dc_post.first_chunk_mut::<32>().unwrap());
(ctx.dc_from_dct32x64)(bb, b_dc_post.first_chunk_mut::<32>().unwrap());
}
STRATEGY_DCT32X16 => {
let xb: &[f32; 512] = coeffs[0].first_chunk::<512>().unwrap();
let bb: &[f32; 512] = coeffs[2].first_chunk::<512>().unwrap();
(ctx.dc_from_dct32x16)(xb, x_dc_post.first_chunk_mut::<8>().unwrap());
(ctx.dc_from_dct32x16)(bb, b_dc_post.first_chunk_mut::<8>().unwrap());
}
STRATEGY_DCT16X32 => {
let xb: &[f32; 512] = coeffs[0].first_chunk::<512>().unwrap();
let bb: &[f32; 512] = coeffs[2].first_chunk::<512>().unwrap();
(ctx.dc_from_dct16x32)(xb, x_dc_post.first_chunk_mut::<8>().unwrap());
(ctx.dc_from_dct16x32)(bb, b_dc_post.first_chunk_mut::<8>().unwrap());
}
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => {
let xb: &[f32; 256] = coeffs[0].first_chunk::<256>().unwrap();
let bb: &[f32; 256] = coeffs[2].first_chunk::<256>().unwrap();
(ctx.dc_from_dct8x32)(xb, x_dc_post.first_chunk_mut::<4>().unwrap());
(ctx.dc_from_dct8x32)(bb, b_dc_post.first_chunk_mut::<4>().unwrap());
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 => {
(ctx.dc_from_dct128x128)(
coeffs[0].first_chunk::<16384>().unwrap(),
x_dc_post.first_chunk_mut::<256>().unwrap(),
);
(ctx.dc_from_dct128x128)(
coeffs[2].first_chunk::<16384>().unwrap(),
b_dc_post.first_chunk_mut::<256>().unwrap(),
);
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X64 => {
(ctx.dc_from_dct128x64)(
coeffs[0].first_chunk::<8192>().unwrap(),
x_dc_post.first_chunk_mut::<128>().unwrap(),
);
(ctx.dc_from_dct128x64)(
coeffs[2].first_chunk::<8192>().unwrap(),
b_dc_post.first_chunk_mut::<128>().unwrap(),
);
}
#[cfg(feature = "extraslow")]
STRATEGY_DCT64X128 => {
(ctx.dc_from_dct64x128)(
coeffs[0].first_chunk::<8192>().unwrap(),
x_dc_post.first_chunk_mut::<128>().unwrap(),
);
(ctx.dc_from_dct64x128)(
coeffs[2].first_chunk::<8192>().unwrap(),
b_dc_post.first_chunk_mut::<128>().unwrap(),
);
}
_ => unreachable!(),
}
let inv_qm_x: &[f32] = match raw_strategy {
STRATEGY_DCT => &matrices.inv_matrix(0)[..],
STRATEGY_IDENTITY => &matrices.inv_matrix_identity(0)[..],
STRATEGY_DCT2X2 => &matrices.inv_matrix_dct2x2(0)[..],
STRATEGY_DCT4X4 => &matrices.inv_matrix_4x4(0)[..],
STRATEGY_DCT4X8 | STRATEGY_DCT8X4 => &matrices.inv_matrix_4x8(0)[..],
STRATEGY_AFV0..=STRATEGY_AFV3 => &matrices.inv_matrix_afv(0)[..],
STRATEGY_DCT16X16 => &matrices.inv_matrix_16x16(0)[..],
STRATEGY_DCT32X32 => &matrices.inv_matrix_32x32(0)[..],
STRATEGY_DCT64X64 => &matrices.inv_matrix_64x64(0)[..],
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 | STRATEGY_DCT128X64 | STRATEGY_DCT64X128 => {
matrices.inv_matrix_128(raw_strategy, 0)
}
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => &matrices.inv_matrix_64x32(0)[..],
STRATEGY_DCT32X16 | STRATEGY_DCT16X32 => &matrices.inv_matrix_32x16(0)[..],
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => &matrices.inv_matrix_8x32(0)[..],
_ => &matrices.inv_matrix_16x8(0)[..],
};
let mut chroma_dc_q = [0i16; 256];
if !hit[0] {
if cfl_factor_x != 0.0 {
(ctx.quantize_dc_cfl)(
&x_dc_post[..covered_dc],
&y_dc_q[..covered_dc],
inv_factor[0],
cfl_factor_x,
&mut chroma_dc_q[..covered_dc],
);
} else {
(ctx.quantize_dc)(
&x_dc_post[..covered_dc],
inv_factor[0],
&mut chroma_dc_q[..covered_dc],
);
}
for iy in 0..cov_y {
let lbx = global_bx - qorigin_x;
let quant_dc_row = &mut quant_dc
.plane_row_mut(0, global_by - qorigin_y + iy)
[lbx..lbx + cov_x];
let row_start = iy * cov_x;
quant_dc_row.copy_from_slice(&chroma_dc_q[row_start..row_start + cov_x]);
}
(ctx.quantize_block_ac)(
&coeffs[0][..size],
0,
inv_qm_x,
quant_ac,
scale,
x_qm_mul,
distance,
cx,
cy,
&mut quantized[0][..size],
);
if let Some(prices) = rdoq_prices {
let RdoqScratch {
choices: rdoq_choices,
costs: rdoq_costs,
} = rdoq.get_or_insert_with(RdoqScratch::default);
let strategy_code = dc_data.ac_strategy.strategy_code(global_bx, global_by);
let nzero_map = &num_nzeros[0];
let row_top = (nz_by != 0).then(|| nzero_map.plane_row(0, nz_by - 1));
let predicted = predict_from_top_and_left(
row_top,
nzero_map.plane_row(0, nz_by),
bx,
32,
);
rdoq_block(
rdoq_channel_weights(ctx.coding)[0],
prices,
coeff_orders.scan_for(strategy_code, 0),
&coeffs[0][..size],
inv_qm_x,
quantize_ac_q_scaled(quant_ac, scale, x_qm_mul),
&mut quantized[0][..size],
raw_strategy,
strategy_code,
0,
ctx.dequant_bias(0),
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
rdoq_lambda(ctx.arbiter, quant_ac as f32 / qf_threshold.max(1) as f32)
/ visible,
restore_penalty,
RdoqSearch::of(ctx.speed.effort()),
rdoq_choices,
rdoq_costs,
);
}
}
let row_stride = cx * 8;
debug_assert!(row_stride.is_power_of_two());
let row_shift = row_stride.trailing_zeros();
let column_mask = row_stride - 1;
if let Some(source) = source_dc.as_mut() {
for iy in 0..cov_y {
let bx = global_bx - qorigin_x;
source.b.row_mut(global_by - qorigin_y + iy)[bx..bx + cov_x]
.copy_from_slice(&b_dc_post[iy * cov_x..(iy + 1) * cov_x]);
source.x.row_mut(global_by - qorigin_y + iy)[bx..bx + cov_x]
.copy_from_slice(&x_dc_post[iy * cov_x..(iy + 1) * cov_x]);
}
}
let inv_qm_b: &[f32] = match raw_strategy {
STRATEGY_DCT => &matrices.inv_matrix(2)[..],
STRATEGY_IDENTITY => &matrices.inv_matrix_identity(2)[..],
STRATEGY_DCT2X2 => &matrices.inv_matrix_dct2x2(2)[..],
STRATEGY_DCT4X4 => &matrices.inv_matrix_4x4(2)[..],
STRATEGY_DCT4X8 | STRATEGY_DCT8X4 => &matrices.inv_matrix_4x8(2)[..],
STRATEGY_AFV0..=STRATEGY_AFV3 => &matrices.inv_matrix_afv(2)[..],
STRATEGY_DCT16X16 => &matrices.inv_matrix_16x16(2)[..],
STRATEGY_DCT32X32 => &matrices.inv_matrix_32x32(2)[..],
STRATEGY_DCT64X64 => &matrices.inv_matrix_64x64(2)[..],
#[cfg(feature = "extraslow")]
STRATEGY_DCT128X128 | STRATEGY_DCT128X64 | STRATEGY_DCT64X128 => {
matrices.inv_matrix_128(raw_strategy, 2)
}
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => &matrices.inv_matrix_64x32(2)[..],
STRATEGY_DCT32X16 | STRATEGY_DCT16X32 => &matrices.inv_matrix_32x16(2)[..],
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => &matrices.inv_matrix_8x32(2)[..],
_ => &matrices.inv_matrix_16x8(2)[..],
};
if !hit[2] {
(ctx.quantize_dc_cfl)(
&b_dc_post[..covered_dc],
&y_dc_q[..covered_dc],
inv_factor[2],
cfl_factor_b,
&mut chroma_dc_q[..covered_dc],
);
for iy in 0..cov_y {
let lbx = global_bx - qorigin_x;
let quant_dc_row = &mut quant_dc
.plane_row_mut(2, global_by - qorigin_y + iy)
[lbx..lbx + cov_x];
let row_start = iy * cov_x;
quant_dc_row.copy_from_slice(&chroma_dc_q[row_start..row_start + cov_x]);
}
(ctx.quantize_block_ac)(
&coeffs[2][..size],
2,
inv_qm_b,
quant_ac,
scale,
ctx.b_qm_mul(),
distance,
cx,
cy,
&mut quantized[2][..size],
);
if let Some(prices) = rdoq_prices {
let RdoqScratch {
choices: rdoq_choices,
costs: rdoq_costs,
} = rdoq.get_or_insert_with(RdoqScratch::default);
let strategy_code = dc_data.ac_strategy.strategy_code(global_bx, global_by);
let nzero_map = &num_nzeros[0];
let row_top = (nz_by != 0).then(|| nzero_map.plane_row(2, nz_by - 1));
let predicted = predict_from_top_and_left(
row_top,
nzero_map.plane_row(2, nz_by),
bx,
32,
);
rdoq_block(
rdoq_channel_weights(ctx.coding)[2],
prices,
coeff_orders.scan_for(strategy_code, 2),
&coeffs[2][..size],
inv_qm_b,
quantize_ac_q_scaled(quant_ac, scale, ctx.b_qm_mul()),
&mut quantized[2][..size],
raw_strategy,
strategy_code,
2,
ctx.dequant_bias(2),
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
rdoq_lambda(ctx.arbiter, quant_ac as f32 / qf_threshold.max(1) as f32)
/ visible,
restore_penalty,
RdoqSearch::of(ctx.speed.effort()),
rdoq_choices,
rdoq_costs,
);
}
}
if measure_chroma_distortion && !hit[0] {
let q_scaled_x = quantize_ac_q_scaled(quant_ac, scale, x_qm_mul);
let bias_x = ctx.dequant_bias(0);
for i in 0..size {
let v = i >> row_shift;
let u = i & column_mask;
if v < cy && u < cx {
continue;
}
let ideal = coeffs[0][i] * inv_qm_x[i] * q_scaled_x;
let error = ideal - bias_x.level(quantized[0][i]);
chroma_distortion += ctx.channel_weight(0) * error * error;
}
}
if measure_chroma_distortion && !hit[2] {
let q_scaled_b = quantize_ac_q_scaled(quant_ac, scale, ctx.b_qm_mul());
let bias_b = ctx.dequant_bias(2);
for i in 0..size {
let v = i >> row_shift;
let u = i & column_mask;
if v < cy && u < cx {
continue;
}
let ideal = coeffs[2][i] * inv_qm_b[i] * q_scaled_b;
let error = ideal - bias_b.level(quantized[2][i]);
chroma_distortion =
fmla(ctx.channel_weight(2), error * error, chroma_distortion);
}
}
if let Some((recon, y_off, _)) = recon_y.as_mut() {
let ReconScratch {
err: recon_err,
pix: recon_pix,
} = recon_scratch.get_or_insert_with(ReconScratch::default);
let y_off = *y_off;
let q_x = quantize_ac_q_scaled(quant_ac, scale, x_qm_mul);
let q_b = quantize_ac_q_scaled(quant_ac, scale, ctx.b_qm_mul());
for (c, q_scaled) in [(0usize, q_x), (2, q_b)] {
if hit[c] {
continue;
}
let bias = ctx.dequant_bias(c);
let qm = matrices.matrix_of(raw_strategy, c);
let inv_q_scaled = 1.0 / q_scaled;
for i in 0..size {
let (v, u) = (i >> row_shift, i & column_mask);
recon_err[i] = if v < cy && u < cx {
0.0
} else {
bias.level(quantized[c][i]) * (qm[i] * inv_q_scaled) - coeffs[c][i]
};
}
crate::inflated_cost::reconstruct_error(
ctx.idct,
raw_strategy,
&recon_err[..size],
&mut recon_pix[..size],
);
let (pw, ph) = crate::inflated_cost::strategy_pixel_dims(raw_strategy);
let (px0, py0) = (opsin_origin.0 + bx * 8, opsin_origin.1 + by * 8);
let plane = opsin.plane(c);
let dst = recon.plane_mut(c);
for y in 0..ph {
let (sy, ly) = (py0 + y, y_off + by * 8 + y);
if sy >= plane.ysize() || ly >= dst.ysize() {
break;
}
let src_row = plane.row(sy);
let dst_row = dst.row_mut(ly);
for x in 0..pw {
let (sx, lx) = (px0 + x, bx * 8 + x);
if sx >= src_row.len() || lx >= dst_row.len() {
break;
}
dst_row[lx] = src_row[sx] + recon_pix[y * pw + x];
}
}
}
}
if let Some(m) = memo.as_deref_mut() {
let epoch = m.epoch;
for j in 0..cov_y {
let row = (lby + j) * m.xsize_blocks;
m.cell_gen[row + bx..row + bx + cov_x].fill(epoch);
}
for c in 0..3 {
if hit[c] {
continue;
}
m.keys[c][bi] = memo_keys[c];
for j in 0..cov_y {
let row = (lby + j) * m.xsize_blocks;
m.dc_f[c][row + bx..row + bx + cov_x]
.copy_from_slice(&dc_vals[c][j * cov_x..(j + 1) * cov_x]);
for i in 0..cov_x {
let slot = (row + bx + i) * 64;
let k = (j * cov_x + i) * 64;
m.quantized[c][slot..slot + 64]
.copy_from_slice(&quantized[c][k..k + 64]);
}
let src = &quant_dc.plane_row(c, lby + j)[bx..bx + cov_x];
m.dc.plane_row_mut(c, lby + j)[bx..bx + cov_x].copy_from_slice(src);
}
}
}
#[cfg(feature = "extraslow")]
if let (Some(p), Some(prices), Some((plane, y_off, _))) = (
polisher.as_deref_mut().filter(|p| p.is_current()),
rdoq_prices,
recon_y.as_mut(),
) {
let strategy_code = dc_data.ac_strategy.strategy_code(global_bx, global_by);
let qf_hi = quant_ac as u32 > qf_threshold;
let weights = rdoq_channel_weights(ctx.coding);
let cw = chroma_rdoq_weight(distance);
macro_rules! channel {
($c:expr, $source:expr, $inv_qm:expr, $q_scaled:expr) => {{
let c: usize = $c;
let block_ctx = fine_block_context(c, strategy_code, qf_hi);
crate::coef_polish::PolishChannel {
source: $source,
inv_qm: $inv_qm,
q_scaled: $q_scaled,
bias: ctx.dequant_bias(c),
weight: weights[c] * if c == 1 { 1.0 } else { cw },
scan: coeff_orders.scan_for(strategy_code, c),
histo_offset: fine_zero_density_contexts_offset(block_ctx),
nzero_ctx: fine_non_zero_context(
predicted_ctx[c] as u32,
block_ctx,
),
}
}};
}
let (pw, ph) = crate::inflated_cost::strategy_pixel_dims(raw_strategy);
let source_y = source_y.as_ref().expect("luma saved for polish");
let block = crate::coef_polish::PolishBlock {
raw_strategy,
cx,
cy,
size,
px0: opsin_origin.0 + bx * 8,
py0: opsin_origin.1 + by * 8,
pw,
ph,
lambda: rdoq_lambda(
ctx.arbiter,
quant_ac as f32 / qf_threshold.max(1) as f32,
) / visible,
channels: [
channel!(
0,
&coeffs[0][..size],
inv_qm_x,
quantize_ac_q_scaled(quant_ac, scale, x_qm_mul)
),
channel!(
1,
&source_y[..size],
inv_qm_y,
quantize_ac_q_scaled(quant_ac, scale, 1.0)
),
channel!(
2,
&coeffs[2][..size],
inv_qm_b,
quantize_ac_q_scaled(quant_ac, scale, ctx.b_qm_mul())
),
],
cfl: [x_factor, 1.0, b_factor],
};
let [q0, q1, q2] = &mut **quantized;
p.polish(
ctx,
prices,
&block,
&mut [&mut q0[..size], &mut q1[..size], &mut q2[..size]],
(&mut **plane, bx * 8, *y_off + by * 8),
);
}
}
if let (Some(source), Some(m)) = (source_dc.as_mut(), memo.as_deref()) {
for iy in 0..cov_y {
let row = (lby + iy) * m.xsize_blocks;
for (c, plane) in [(0, &mut source.x), (1, &mut source.y), (2, &mut source.b)] {
plane.row_mut(lby + iy)[bx..bx + cov_x]
.copy_from_slice(&m.dc_f[c][row + bx..row + bx + cov_x]);
}
}
}
if let Some(m) = memo.as_deref_mut()
&& token_memo
&& m.spans.len() != m.keys[0].len() * streams * 3
{
m.spans = vec![(0, 0); m.keys[0].len() * streams * 3];
m.tokens = vec![Vec::new(); streams];
}
let strategy_code = dc_data.ac_strategy.strategy_code(global_bx, global_by);
let covered_blocks = cx * cy;
let log2_covered_blocks = match covered_blocks {
1 => 0,
2 => 1,
4 => 2,
8 => 3,
16 => 4,
32 => 5,
64 => 6,
128 => 7,
256 => 8,
_ => unreachable!("invalid covered_blocks {}", covered_blocks),
};
for &c in &[1usize, 0, 2] {
if tokens_cached
&& hit[c]
&& let Some(m) = memo.as_deref_mut()
{
if m.trial && m.bits[bi][c].is_finite() {
m.trial_bits += f64::from(m.bits[bi][c]);
for iy in 0..cov_y {
num_nzeros[0].plane_row_mut(c, nz_by + iy)[bx..bx + cov_x]
.fill(m.nz_shift[bi][c]);
}
continue;
}
for s in 0..streams {
let (start, len) = m.spans[(bi * 3 + c) * streams + s];
let (start, len) = (start as usize, len as usize);
let dst = if s < out.len() {
&mut out[s]
} else {
&mut alternative_out[s - out.len()]
};
dst.extend_from_slice(&m.tokens[s][start..start + len]);
}
for iy in 0..cov_y {
num_nzeros[0].plane_row_mut(c, nz_by + iy)[bx..bx + cov_x]
.fill(m.nz_shift[bi][c]);
}
continue;
}
if token_memo && memo.is_some() {
stream_starts.clear();
stream_starts.extend(
out.iter()
.map(Vec::len)
.chain(alternative_out.iter().map(Vec::len)),
);
}
let full_block = &quantized[c][..size];
for pass in 0..coeff_shifts.len() {
let block = if pass == 0 && coeff_shifts[0] == 0 {
full_block
} else {
for k in 0..size {
pblock[k] = pass_coefficient(full_block[k], coeff_shifts, pass);
}
&pblock[..size]
};
let num_nzeros = &mut num_nzeros[pass];
let out = &mut out[pass];
let nzeros = if covered_blocks == 1 {
num_nonzero_except_dc(block.first_chunk::<64>().unwrap())
} else {
num_nonzero_except_llf(block, cx, cy)
};
let shifted =
((nzeros as usize + covered_blocks - 1) >> log2_covered_blocks) as u8;
if pass == 0
&& let Some(m) = memo.as_deref_mut()
{
m.nz_shift[lby * m.xsize_blocks + bx][c] = shifted;
}
for iy in 0..cov_y {
let target_row =
&mut num_nzeros.plane_row_mut(c, nz_by + iy)[bx..bx + cov_x];
for target in target_row.iter_mut() {
*target = shifted;
}
}
let row_top: Option<&[u8]> = if nz_by == 0 {
None
} else {
Some(num_nzeros.plane_row(c, nz_by - 1))
};
let row = num_nzeros.plane_row(c, nz_by);
let predicted = predict_from_top_and_left(row_top, row, bx, 32);
let block_ctx =
fine_block_context(c, strategy_code, quant_ac as u32 > qf_threshold);
let nzero_ctx = fine_non_zero_context(predicted as u32, block_ctx);
let histo_offset = fine_zero_density_contexts_offset(block_ctx);
let token_start = out.len();
write_token_into(Token::new(nzero_ctx, nzeros as u32), out);
let mut prev: usize = if nzeros as usize > size / 16 { 0 } else { 1 };
let mut remaining = nzeros;
let scan = coeff_orders.scan_for(strategy_code, c);
if pass == 0
&& let Some(stats) = order_stats.as_deref_mut()
&& let Some(slot) = crate::coeff_order::order_slot_of(strategy_code)
{
if c == 1 && pass == 0 {
stats.tally_block(slot);
}
for (raw, &coef) in block[..size].iter().enumerate() {
if coef != 0 {
stats.tally(slot, c, raw);
}
}
}
let mut k = covered_blocks;
while k < size && remaining != 0 {
let raw = scan[k] as usize;
let coef = block[raw];
let ctx = histo_offset as usize
+ if covered_blocks == 1 {
zero_density_context_8x8(remaining as usize, k, prev)
} else {
zero_density_context(
remaining as usize,
k,
covered_blocks,
log2_covered_blocks,
prev,
)
};
write_token_into(Token::new(ctx as u32, pack_signed(coef)), out);
prev = if coef != 0 { 1 } else { 0 };
if coef != 0 {
remaining -= 1;
}
k += 1;
}
for (alternative_out, orders) in
alternative_out.iter_mut().zip(alternative_orders)
{
let alternative_scan = orders.scan_for(strategy_code, c);
if alternative_scan == scan {
alternative_out.extend_from_slice(&out[token_start..]);
continue;
}
write_token_into(Token::new(nzero_ctx, nzeros as u32), alternative_out);
let scan = alternative_scan;
let mut prev = if nzeros as usize > size / 16 { 0 } else { 1 };
let mut remaining = nzeros;
let mut k = covered_blocks;
while k < size && remaining != 0 {
let raw = scan[k] as usize;
let coef = block[raw];
let ctx = histo_offset as usize
+ if covered_blocks == 1 {
zero_density_context_8x8(remaining as usize, k, prev)
} else {
zero_density_context(
remaining as usize,
k,
covered_blocks,
log2_covered_blocks,
prev,
)
};
write_token_into(
Token::new(ctx as u32, pack_signed(coef)),
alternative_out,
);
prev = if coef != 0 { 1 } else { 0 };
if coef != 0 {
remaining -= 1;
}
k += 1;
}
}
debug_assert_eq!(
remaining, 0,
"remaining nzeros at end: strategy={} c={} pass={}",
strategy_code, c, pass
);
}
if token_memo && let Some(m) = memo.as_deref_mut() {
for s in 0..streams {
let src = if s < out.len() {
&out[s]
} else {
&alternative_out[s - out.len()]
};
let new_tokens = &src[stream_starts[s]..];
if s == 0 {
m.bits[bi][c] = rdoq_prices.map_or(f32::NAN, |p| {
new_tokens.iter().map(|&k| p.token_bits(k)).sum()
});
}
let arena = &mut m.tokens[s];
m.spans[(bi * 3 + c) * streams + s] =
(arena.len() as u32, new_tokens.len() as u32);
arena.extend_from_slice(new_tokens);
}
}
}
}
}
chroma_distortion
}
#[inline]
fn write_token_into(t: Token, out: &mut Vec<Token>) {
out.push(t);
}
#[cfg(test)]
mod tests {
#[test]
fn dequant_bias_tables_match_the_division() {
use super::DequantBias;
use std::hint::black_box;
for explicit in [false, true] {
for bias in DequantBias::channels(explicit) {
let numerator = black_box(bias.numerator);
for quant in -300i32..=300 {
let want = match quant.unsigned_abs() {
0 => 0.0,
1 => bias.one.copysign(quant as f32),
_ => quant as f32 - numerator / black_box(quant as f32),
};
assert_eq!(bias.level(quant).to_bits(), want.to_bits(), "q = {quant}");
let level = bias.level_f32(quant as f32);
assert_eq!(level.to_bits(), want.to_bits(), "q = {quant} (f32)");
}
for quant in [2.5f32, -7.25, 63.5, 1.5, -1.75, 1e9] {
let want = quant - numerator / black_box(quant);
assert_eq!(
bias.level_f32(quant).to_bits(),
want.to_bits(),
"q = {quant}"
);
}
}
}
}
#[cfg(feature = "extraslow")]
#[test]
fn block_memo_compaction_preserves_all_streams_and_snapshot_rollback() {
use super::AcBlockCache;
use crate::entropy::Token;
let mut memo = AcBlockCache::new(2, 2, 16, 16);
memo.tokens = vec![Vec::new(); 3];
memo.spans = vec![(0, 0); 4 * 3 * 3];
let write = |memo: &mut AcBlockCache, cell: usize, version: u32| {
for c in 0..3 {
for s in 0..3 {
let arena = &mut memo.tokens[s];
let start = arena.len() as u32;
let len = (cell + c + s) * 47;
arena.extend(
(0..len)
.map(|i| Token::new((cell * 3 + c) as u32, version * 1000 + i as u32)),
);
memo.spans[(cell * 3 + c) * 3 + s] = (start, len as u32);
}
}
};
let read = |memo: &AcBlockCache| -> Vec<Vec<(u32, u32)>> {
memo.spans
.iter()
.enumerate()
.map(|(i, &(start, len))| {
memo.tokens[i % 3][start as usize..(start + len) as usize]
.iter()
.map(|t| (t.context, t.value))
.collect()
})
.collect()
};
for version in 0..80 {
write(&mut memo, version as usize % 4, version);
let expected = read(&memo);
memo.compact_tokens();
assert_eq!(read(&memo), expected);
for s in 0..3 {
let live: usize = memo.spans[s..]
.iter()
.step_by(3)
.map(|&(_, len)| len as usize)
.sum();
assert!(memo.tokens[s].len() <= (2 * live).max(4096));
}
}
let before = read(&memo);
let snapshot = memo.snapshot(1, 0, 1, 1);
write(&mut memo, 1, 100);
write(&mut memo, 2, 101);
let changed = read(&memo);
memo.restore(&snapshot);
drop(snapshot);
memo.compact_tokens();
let restored = read(&memo);
assert_eq!(&restored[9..18], &before[9..18]);
assert_eq!(&restored[18..27], &changed[18..27]);
memo.spans.fill((0, 0));
for arena in &mut memo.tokens {
arena.resize(8192, Token::new(0, 0));
}
memo.compact_tokens();
assert!(memo.tokens.iter().all(|arena| arena.capacity() == 0));
AcBlockCache::new(0, 0, 0, 0).compact_tokens();
}
#[test]
fn ac_scratch_allocates_optional_buffers_only_for_active_tools_and_reuses_them() {
use super::{AcGroupScratch, write_ac_group};
use crate::coeff_order::CoeffOrders;
use crate::dc_group_data::DcGroupData;
use crate::encoding_context::EncodingContext;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
use crate::image::{Image3B, Image3F, Image3S, Rect};
let (w, h) = (17, 19);
let mut image = Image3F::new(w, h);
for c in 0..3 {
for (i, value) in image.plane_mut(c).as_mut_slice().iter_mut().enumerate() {
*value = ((i * 97 + c * 13) % 257) as f32 / 257.0;
}
}
let mut dc = DcGroupData::new(w.div_ceil(8), h.div_ceil(8)).unwrap();
dc.raw_quant_field.as_mut_slice().fill(8);
let orders = CoeffOrders::natural();
let samples: Vec<_> = (0..4096).map(|i| Token::new(i % 2, i % 17)).collect();
let mut code = build_entropy_code_no_cluster(&samples, 2, &mut Vec::new());
code.context_map = (0..crate::ac_context::K_NUM_FINE_AC_CONTEXTS)
.map(|i| (i % 2) as u8)
.collect();
let prices = FrozenTokenPrices::new(&code);
let run = |ctx: &EncodingContext,
scratch: &mut AcGroupScratch,
rdoq: bool,
capture: bool,
shifts: &[u32]| {
let mut quant_dc = Image3S::new(w.div_ceil(8), h.div_ceil(8));
let mut nzeros: Vec<_> = shifts.iter().map(|_| Image3B::new(32, 32)).collect();
let mut tokens: Vec<_> = shifts.iter().map(|_| Vec::new()).collect();
let mut recon = Image3F::new(w, h);
let mut starts = Vec::new();
write_ac_group(
ctx,
scratch,
&image,
(0, 0),
Rect::new(0, 0, w.div_ceil(8), h.div_ceil(8)),
1.0,
1.0,
[1.0; 3],
2.0,
2,
&dc,
0,
&mut quant_dc,
None,
0,
0,
&mut nzeros,
shifts,
rdoq.then_some(&prices),
&orders,
&[],
None,
false,
8,
capture.then_some((&mut recon, 0, &mut starts)),
None,
&mut tokens,
);
(
tokens
.into_iter()
.map(|stream| {
stream
.into_iter()
.map(|t| (t.context, t.value))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>(),
[0, 1, 2].map(|c| {
recon
.plane_data(c)
.iter()
.map(|v| v.to_bits())
.collect::<Vec<_>>()
}),
)
};
let pointers = |scratch: &AcGroupScratch| {
(
scratch.source_y.as_ref().map(|v| v.as_ptr()),
scratch
.rdoq
.as_ref()
.map(|v| (v.choices.as_ptr(), v.costs[0].as_ptr())),
scratch
.recon
.as_ref()
.map(|v| (v.err.as_ptr(), v.pix.as_ptr())),
)
};
for speed in [
crate::Speed::UltraFast,
crate::Speed::Fastest,
crate::Speed::Fast,
crate::Speed::Medium,
] {
let ctx = EncodingContext::new(
speed,
crate::Arbiter::Ssimulacra2,
crate::xyb::XybMatrix::SPEC,
2.0,
1,
);
let mut scratch = AcGroupScratch::default();
for shifts in [&[0u32][..], &[2, 0]] {
run(&ctx, &mut scratch, false, false, shifts);
assert_eq!(pointers(&scratch), (None, None, None));
}
}
let ctx = EncodingContext::new(
crate::Speed::Slow,
crate::Arbiter::Ssimulacra2,
crate::xyb::XybMatrix::SPEC,
2.0,
1,
);
for (rdoq, capture) in [(true, false), (false, true), (true, true)] {
let mut scratch = AcGroupScratch::default();
for shifts in [&[0u32][..], &[2, 0]] {
let expected = run(&ctx, &mut scratch, rdoq, capture, shifts);
assert!(scratch.source_y.is_some());
assert_eq!(scratch.rdoq.is_some(), rdoq);
assert_eq!(scratch.recon.is_some(), capture);
let storage = pointers(&scratch);
assert_eq!(run(&ctx, &mut scratch, rdoq, capture, shifts), expected);
assert_eq!(pointers(&scratch), storage);
let mut fresh = AcGroupScratch::default();
assert_eq!(run(&ctx, &mut fresh, rdoq, capture, shifts), expected);
}
}
}
#[cfg(feature = "extraslow")]
#[test]
fn block_memo_matches_fresh_evaluation_on_partial_and_full_hits() {
use super::{AcBlockCache, AcGroupScratch, SourceDc, write_ac_group};
use crate::coeff_order::CoeffOrders;
use crate::dc_group_data::DcGroupData;
use crate::encoding_context::EncodingContext;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
use crate::image::{Image3B, Image3F, Image3S, Plane, Rect};
use crate::xyb::rgb_to_xyb_pixel_f32;
let ctx = EncodingContext::new(
crate::Speed::Slow,
crate::Arbiter::Ssimulacra2,
crate::xyb::XybMatrix::SPEC,
2.0,
1,
);
assert!(ctx.speed.effort().clip_continuation);
let (w, h) = (56, 16);
let black = rgb_to_xyb_pixel_f32(&ctx.xyb, 0.0, 0.0, 0.0);
let white = rgb_to_xyb_pixel_f32(&ctx.xyb, 1.0, 1.0, 1.0);
let mut image = Image3F::new(w, h);
for y in 0..h {
for x in 0..w {
let edge = (x / 8 + y / 8) % 7 + 1;
let (vx, vy, vb) = if x % 8 < edge { black } else { white };
image.plane_row_mut(0, y)[x] = vx;
image.plane_row_mut(1, y)[x] = vy;
image.plane_row_mut(2, y)[x] = vb;
}
}
let (bw, bh) = (w / 8, h / 8);
let orders = CoeffOrders::natural();
let samples: Vec<_> = (0..4096).map(|i| Token::new(i % 2, i % 17)).collect();
let mut code = build_entropy_code_no_cluster(&samples, 2, &mut Vec::new());
code.context_map = (0..crate::ac_context::K_NUM_FINE_AC_CONTEXTS)
.map(|i| (i % 2) as u8)
.collect();
let prices = FrozenTokenPrices::new(&code);
let run = |dc: &DcGroupData, b_scale: u32, memo: Option<&mut AcBlockCache>| {
ctx.set_b_qm_scale(b_scale);
let mut scratch = AcGroupScratch::default();
let mut quant_dc = Image3S::new(bw, bh);
let mut source = SourceDc {
y: Plane::new(bw, bh),
b: Plane::new(bw, bh),
x: Plane::new(bw, bh),
};
let mut nzeros = vec![Image3B::new(32, 32)];
let mut tokens = vec![Vec::new()];
let mut starts = Vec::new();
let mut memo = memo;
let mut recon = match memo.as_deref_mut() {
Some(m) => std::mem::replace(m.recon_mut(), Image3F::new(0, 0)),
None => Image3F::new(w, h),
};
write_ac_group(
&ctx,
&mut scratch,
&image,
(0, 0),
Rect::new(0, 0, bw, bh),
1.0,
1.0,
[1.0; 3],
2.0,
2,
dc,
0,
&mut quant_dc,
Some(&mut source),
0,
0,
&mut nzeros,
&[0],
Some(&prices),
&orders,
&[],
None,
false,
8,
Some((&mut recon, 0, &mut starts)),
memo.as_deref_mut(),
&mut tokens,
);
if let Some(m) = memo {
*m.recon_mut() = recon;
}
let bits =
|p: &Plane<f32>| p.as_slice().iter().map(|v| v.to_bits()).collect::<Vec<_>>();
(
tokens[0]
.iter()
.map(|t| (t.context, t.value))
.collect::<Vec<_>>(),
[0, 1, 2].map(|c| quant_dc.plane_data(c).to_vec()),
[bits(&source.x), bits(&source.y), bits(&source.b)],
)
};
for qf in [6u8, 10, 16] {
let mut dc = DcGroupData::new(bw, bh).unwrap();
dc.raw_quant_field.as_mut_slice().fill(qf);
let mut memo = AcBlockCache::new(bw, bh, w, h);
let warm = run(&dc, 6, Some(&mut memo));
assert_eq!(warm, run(&dc, 6, None), "cold memo, qf {qf}");
for b_scale in [7, 7] {
let fresh = run(&dc, b_scale, None);
assert_eq!(
run(&dc, b_scale, Some(&mut memo)),
fresh,
"qf {qf} b {b_scale}"
);
}
}
}
#[test]
fn pass_split_sums_back_and_rounds_toward_zero() {
for shifts in [&[1, 0][..], &[2, 0], &[3, 0], &[2, 1, 0], &[3, 2, 1, 0]] {
for c in -40..=40 {
let sent: Vec<i32> = (0..shifts.len())
.map(|p| super::pass_coefficient(c, shifts, p))
.collect();
let sum: i32 = sent.iter().zip(shifts).map(|(&v, &s)| v << s).sum();
assert_eq!(sum, c, "{shifts:?} {c}");
assert!(sent.iter().all(|&v| v == 0 || v.signum() == c.signum()));
assert_eq!(sent[0], c.signum() * (c.abs() >> shifts[0]));
}
}
}
use super::{
QuantizeDcMethods, quantize_ac_thresholds, quantize_ac_thresholds_scaled,
quantize_dc_cfl_scalar, quantize_dc_scalar, selected_quantize_dc_methods,
};
fn check_dc_quantizers(methods: QuantizeDcMethods) {
let input = [
f32::NEG_INFINITY,
-40_000.5,
-32_768.5,
-3.5,
-2.5,
-1.5,
-0.5,
-0.0,
0.0,
0.5,
1.5,
2.5,
3.5,
32_767.0,
32_767.5,
40_000.5,
f32::INFINITY,
f32::NAN,
11.25,
];
let y_quant = [
-31i16, -29, -23, -19, -17, -13, -11, -7, -5, -3, 2, 4, 8, 10, 14, 16, 20, 22, 26,
];
let input: Vec<f32> = (0..260).map(|i| input[i % input.len()]).collect();
let y_quant: Vec<i16> = (0..260).map(|i| y_quant[i % y_quant.len()]).collect();
for len in (0..=33).chain([63, 64, 65, 127, 128, 129, 257]) {
for offset in [0, 1, 3] {
let input = &input[offset..offset + len];
let y_quant = &y_quant[offset..offset + len];
for scale in [0.0, 0.5, 1.0, -1.25] {
let mut want = vec![0x1234i16; len + offset + 2];
let mut got = want.clone();
quantize_dc_scalar(input, scale, &mut want[offset..offset + len]);
(methods.quantize)(input, scale, &mut got[offset..offset + len]);
assert_eq!(
got, want,
"plain DC length {len}, offset={offset}, scale={scale}"
);
for cfl in [0.0, 0.5, -0.75] {
quantize_dc_cfl_scalar(
input,
y_quant,
scale,
cfl,
&mut want[offset..offset + len],
);
(methods.quantize_cfl)(
input,
y_quant,
scale,
cfl,
&mut got[offset..offset + len],
);
assert_eq!(
got, want,
"CfL DC length {len}, offset={offset}, scale={scale}, cfl={cfl}"
);
}
}
}
}
check_dc_i32_quantizers(methods);
}
fn check_dc_i32_quantizers(methods: QuantizeDcMethods) {
let special = [
f32::NEG_INFINITY,
f32::INFINITY,
f32::NAN,
-0.0,
0.0,
-0.5,
0.5,
-1.5,
1.5,
-2.5,
2.5,
f32::from_bits(0.5f32.to_bits() - 1),
f32::from_bits(0.5f32.to_bits() + 1),
-f32::from_bits(0.5f32.to_bits() - 1),
-f32::from_bits(0.5f32.to_bits() + 1),
-65_535.5,
65_535.5,
f32::from_bits(2_147_483_648.0f32.to_bits() - 1),
2_147_483_648.0,
-2_147_483_648.0,
-4_294_967_296.0,
];
let input: Vec<f32> = (0..257)
.map(|i| {
if i % 3 == 0 {
special[i / 3 % special.len()]
} else {
f32::from_bits(0x3b00_0000 + (i as u32 * 7919) % 0x1000_0000)
* if i % 2 == 0 { 1.0 } else { -1.0 }
}
})
.collect();
let y_quant: Vec<i32> = (0..257)
.map(|i| match i % 7 {
0 => i32::MIN,
1 => i32::MAX,
_ => (i * 7919) - 900_000,
})
.collect();
for len in (0..=33).chain([63, 64, 65, 127, 128, 129, 257]) {
for scale in [0.0, -0.0, 0.5, 1.0, -1.25, 1.137, 65535.0] {
let mut want = vec![0x1234_5678i32; len + 2];
let mut got = want.clone();
super::quantize_dc_i32_scalar(&input[..len], scale, &mut want[1..=len]);
(methods.quantize_i32)(&input[..len], scale, &mut got[1..=len]);
assert_eq!(got, want, "wide DC length {len}, scale={scale}");
for cfl in [0.0, -0.0, 0.5, -0.75, 0.137] {
super::quantize_dc_cfl_i32_scalar(
&input[..len],
&y_quant[..len],
scale,
cfl,
&mut want[1..=len],
);
(methods.quantize_cfl_i32)(
&input[..len],
&y_quant[..len],
scale,
cfl,
&mut got[1..=len],
);
assert_eq!(got, want, "wide CfL length {len}, scale={scale}, cfl={cfl}");
}
}
}
}
#[test]
fn selected_dc_quantizers_match_scalar() {
check_dc_quantizers(selected_quantize_dc_methods());
}
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
#[test]
fn avx2_dc_quantizers_match_scalar() {
if std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("fma") {
check_dc_quantizers(QuantizeDcMethods {
quantize: |input, scale, output| unsafe {
crate::avx::quantize_dc_avx2(input, scale, output)
},
quantize_cfl: |input, y_quant, scale, cfl, output| unsafe {
crate::avx::quantize_dc_cfl_avx2(input, y_quant, scale, cfl, output)
},
quantize_i32: |input, scale, output| unsafe {
crate::avx::quantize_dc_i32_avx2(input, scale, output)
},
quantize_cfl_i32: |input, y_quant, scale, cfl, output| unsafe {
crate::avx::quantize_dc_cfl_i32_avx2(input, y_quant, scale, cfl, output)
},
});
}
}
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "sse"))]
#[test]
fn sse41_dc_quantizers_match_scalar() {
if std::is_x86_feature_detected!("sse4.1") {
check_dc_quantizers(QuantizeDcMethods {
quantize: |input, scale, output| unsafe {
crate::sse::quantize_dc_sse41(input, scale, output)
},
quantize_cfl: |input, y_quant, scale, cfl, output| unsafe {
crate::sse::quantize_dc_cfl_sse41(input, y_quant, scale, cfl, output)
},
quantize_i32: |input, scale, output| unsafe {
crate::sse::quantize_dc_i32_sse41(input, scale, output)
},
quantize_cfl_i32: |input, y_quant, scale, cfl, output| unsafe {
crate::sse::quantize_dc_cfl_i32_sse41(input, y_quant, scale, cfl, output)
},
});
}
}
#[test]
fn deadzones_fade_to_existing_values_by_distance_one() {
assert_eq!(
quantize_ac_thresholds(1, 1, 1, 0.1),
[0.50, 0.51, 0.52, 0.54]
);
assert_eq!(
quantize_ac_thresholds(2, 1, 1, 0.1),
[0.58, 0.75, 0.75, 0.75]
);
assert_eq!(
quantize_ac_thresholds(1, 1, 1, 1.0),
[0.58, 0.635, 0.66, 0.7]
);
assert_eq!(
quantize_ac_thresholds(2, 1, 1, 1.0),
[0.58, 0.75, 0.75, 0.75]
);
assert_eq!(
quantize_ac_thresholds_scaled(2, 1, 1, 1.0, 1.25),
[0.58, 0.65, 0.65, 0.65]
);
assert_eq!(
quantize_ac_thresholds_scaled(1, 1, 1, 1.0, 1.25),
quantize_ac_thresholds(1, 1, 1, 1.0)
);
}
}