#![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_DCT16X8, STRATEGY_DCT16X16, STRATEGY_DCT16X32, STRATEGY_DCT32X16,
STRATEGY_DCT32X32, STRATEGY_DCT32X64, STRATEGY_DCT64X32, STRATEGY_DCT64X64, STRATEGY_DCT64X128,
STRATEGY_DCT128X64, STRATEGY_DCT128X128, STRATEGY_IDENTITY,
};
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::{FastRound, HeapMatrix, heap_array};
use std::sync::OnceLock;
const RDOQ_MAX_STRIDE: usize = 2 * (256 + 1);
#[inline]
pub(crate) fn rdoq_channel_weights(coding: crate::CodingTransform) -> [f32; 3] {
match coding {
crate::CodingTransform::YCbCr => coding.channel_weights(),
crate::CodingTransform::Xyb | crate::CodingTransform::Rgb => {
crate::inflated_cost::CHANNEL_WEIGHT
}
}
}
const CHROMA_DZ_SAT_START: f32 = 0.3;
const CHROMA_DZ_SAT_WIDTH: f32 = 0.3;
const CHROMA_DZ_TARGET: f32 = 0.5;
const CHROMA_DZ_MIN_RELATIVE_ENERGY: f32 = 0.2;
#[inline]
fn chroma_block_saturation(dc: [f32; 3]) -> (f32, f32) {
let by = dc[2] - dc[1];
let chroma = (dc[0] * dc[0] + by * by).sqrt();
((chroma / dc[1].max(0.02)).min(1.0), chroma)
}
#[allow(clippy::too_many_arguments)]
fn chroma_deadzone_fixup(
c: usize,
coeffs: &[f32],
inv_qm: &[f32],
q_scaled: f32,
distance: f32,
qm_mul: f32,
f: f32,
dc_chroma: f32,
quantized: &mut [i32],
) {
if f <= 0.0 {
return;
}
let coeffs = &coeffs[..64];
let inv_qm = &inv_qm[..64];
let quantized = &mut quantized[..64];
let thr = quantize_ac_thresholds_scaled(c, 1, 1, distance, qm_mul);
let eff: [f32; 4] =
std::array::from_fn(|i| (thr[i] - f * (thr[i] - CHROMA_DZ_TARGET)).max(0.5));
let mut best = (0.0f32, usize::MAX);
let mut deleted = 0.0f32;
for i in 1..64 {
if quantized[i] != 0 {
continue;
}
let coeff = coeffs[i];
deleted += coeff * coeff;
let magnitude = (coeff * inv_qm[i] * q_scaled).abs();
if magnitude > best.0
&& magnitude >= eff[usize::from(i >= 32) * 2 + usize::from(i % 8 >= 4)]
{
best = (magnitude, i);
}
}
if best.1 == usize::MAX || deleted.sqrt() < CHROMA_DZ_MIN_RELATIVE_ENERGY * dc_chroma {
return;
}
quantized[best.1] = if coeffs[best.1] < 0.0 { -1 } else { 1 };
}
const RDOQ_MAX_CHOICES: usize = 64 * RDOQ_MAX_STRIDE;
const K_GROUP_DIM_IN_BLOCKS: usize = 32;
#[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
}
#[inline]
fn rdoq_candidates(ideal: f32, current: i32) -> ([i32; 5], usize) {
let rounded = ideal.fast_round() as i32;
if ideal.abs() > 2.5 && (ideal - rounded as f32).abs() < 0.25 {
return ([current; 5], 1);
}
let proposed = [
current,
0,
rounded,
rounded.saturating_sub(1),
rounded.saturating_add(1),
];
let mut candidates = [current; 5];
let mut len = 0;
for level in proposed {
if !candidates[..len].contains(&level) {
candidates[len] = level;
len += 1;
}
}
(candidates, len)
}
#[inline]
fn rdoq_distortion_weight(window_index: usize, window_len: usize, distance: f32) -> f32 {
let scan_position = window_index as f32 / window_len.max(1) as f32;
let strength = (distance - 2.0).clamp(0.0, 1.0);
1.0 + strength * (1.0 - scan_position).powi(2)
}
#[inline]
pub(crate) fn rdoq_lambda(qf_ratio: f32) -> f32 {
const BASE: f32 = 1.5;
crate::ac_strategy::RD_LAMBDA * 0.25 * BASE * qf_ratio.clamp(0.5, 2.0)
}
#[inline(always)]
fn update_rdoq_state_pair(
distortion: f32,
token_cost: [f32; 2],
tail: f32,
candidate: u8,
current: &mut [f32; 2],
choices: &mut [u8; 2],
) {
#[cfg(all(target_arch = "aarch64", feature = "neon", target_feature = "neon"))]
{
use std::arch::aarch64::*;
unsafe {
let cost = vadd_f32(
vadd_f32(vdup_n_f32(distortion), vld1_f32(token_cost.as_ptr())),
vdup_n_f32(tail),
);
let old = vld1_f32(current.as_ptr());
let better = vclt_f32(cost, old);
vst1_f32(current.as_mut_ptr(), vbsl_f32(better, cost, old));
let mask16 = vmovn_u32(vcombine_u32(better, better));
let mask8 = vmovn_u16(vcombine_u16(mask16, mask16));
let mask = vget_lane_u16::<0>(vreinterpret_u16_u8(mask8));
let old_choice = u16::from_ne_bytes(*choices);
let new_choice = u16::from_ne_bytes([candidate; 2]);
*choices = ((new_choice & mask) | (old_choice & !mask)).to_ne_bytes();
}
}
#[cfg(not(all(target_arch = "aarch64", feature = "neon", target_feature = "neon")))]
for prev in 0..2 {
let cost = distortion + token_cost[prev] + tail;
if cost < current[prev] {
current[prev] = cost;
choices[prev] = candidate;
}
}
}
fn rdoq_window_len(covered_blocks: usize, large_window: bool, distance: f32) -> usize {
if large_window && covered_blocks >= 8 && distance >= 3.0 {
(16 * covered_blocks).min(1024)
} else {
(24 * covered_blocks).min(64)
}
}
const RESTORE_FEW_COLORS_PER_BLOCK: usize = 24;
const RESTORE_FEW_COLORS_PENALTY: f32 = 2.0;
fn few_colors(
opsin: &Image3F,
x0: usize,
y0: usize,
x1: usize,
y1: usize,
limit: usize,
seen: &mut Vec<u64>,
) -> bool {
let key_at = |rows: &[&[f32]; 3], x: usize| {
rows.iter().fold(0u64, |k, r| {
(k << 21) | (((r[x] * 1024.0) + 0.5) as i64 as u64 & 0x1f_ffff)
})
};
seen.clear();
if limit > 64 {
for y in y0..y1 {
let rows = [
opsin.plane_row(0, y),
opsin.plane_row(1, y),
opsin.plane_row(2, y),
];
seen.extend((x0..x1).map(|x| key_at(&rows, x)));
}
seen.sort_unstable();
seen.dedup();
return seen.len() <= limit;
}
seen.reserve(limit);
for y in y0..y1 {
let rows = [
opsin.plane_row(0, y),
opsin.plane_row(1, y),
opsin.plane_row(2, y),
];
for x in x0..x1 {
let key = key_at(&rows, x);
if !seen.contains(&key) {
if seen.len() == limit {
return false;
}
seen.push(key);
}
}
}
true
}
#[allow(clippy::too_many_arguments)]
fn rdoq_block(
channel_weight: f32,
large_window: bool,
prices: &FrozenTokenPrices,
scan: &[u32],
source: &[f32],
inv_qm: &[f32],
q_scaled: f32,
block: &mut [i32],
raw_strategy: u8,
strategy_code: u8,
c: usize,
predicted: u8,
cx: usize,
cy: usize,
distance: f32,
qf_hi: bool,
qf_ratio: f32,
visible: f32,
restore_penalty: f32,
choices: &mut [u8; RDOQ_MAX_CHOICES],
costs: &mut [[f32; RDOQ_MAX_STRIDE]; 2],
) {
let covered_blocks = cx * cy;
let window_cap = rdoq_window_len(covered_blocks, large_window, distance);
let legacy_limit = window_cap <= 64 && !crate::dc_group_data::is_128_strategy(raw_strategy);
macro_rules! refine {
($factored:literal) => {
rdoq_block_banded::<$factored>(
channel_weight,
window_cap,
legacy_limit,
prices,
scan,
source,
inv_qm,
q_scaled,
block,
raw_strategy,
strategy_code,
c,
predicted,
cx,
cy,
distance,
qf_hi,
qf_ratio,
visible,
restore_penalty,
choices,
costs,
)
};
}
if covered_blocks == 1 {
refine!(false);
} else {
refine!(true);
}
}
#[allow(clippy::too_many_arguments)]
fn rdoq_block_banded<const FACTORED: bool>(
channel_weight: f32,
window_cap: usize,
legacy_limit: bool,
prices: &FrozenTokenPrices,
scan: &[u32],
source: &[f32],
inv_qm: &[f32],
q_scaled: f32,
block: &mut [i32],
raw_strategy: u8,
strategy_code: u8,
c: usize,
predicted: u8,
cx: usize,
cy: usize,
distance: f32,
qf_hi: bool,
qf_ratio: f32,
visible: f32,
restore_penalty: f32,
choices: &mut [u8; RDOQ_MAX_CHOICES],
costs: &mut [[f32; RDOQ_MAX_STRIDE]; 2],
) {
let lambda = rdoq_lambda(qf_ratio) / visible;
const MAX_NZERO_DELTA: usize = 6;
if !matches!(
raw_strategy,
STRATEGY_DCT
| STRATEGY_DCT16X8
| STRATEGY_DCT8X16
| STRATEGY_DCT16X16
| STRATEGY_DCT32X32
| STRATEGY_DCT32X16
| STRATEGY_DCT16X32
| STRATEGY_DCT64X64
| STRATEGY_DCT64X32
| STRATEGY_DCT32X64
| STRATEGY_DCT128X128
| STRATEGY_DCT128X64
| STRATEGY_DCT64X128
) {
return;
}
let covered_blocks = cx * cy;
let search_end = block.len().min(covered_blocks + window_cap);
let window_len = search_end - covered_blocks;
let base_window_len = (24 * covered_blocks)
.min(64)
.min(block.len() - covered_blocks);
const BAND_STRIDE: usize = 2 * (2 * MAX_NZERO_DELTA + 2);
assert!(window_len * BAND_STRIDE <= choices.len());
let block_ctx = fine_block_context(c, strategy_code, qf_hi);
let histo_offset = fine_zero_density_contexts_offset(block_ctx);
let log2_covered_blocks = covered_blocks.trailing_zeros() as usize;
let context = |remaining: usize, k: usize, prev: usize| -> u32 {
histo_offset
+ if covered_blocks == 1 {
zero_density_context_8x8(remaining, k, prev) as u32
} else {
zero_density_context(remaining, k, covered_blocks, log2_covered_blocks, prev) as u32
}
};
let suffix_nzeros = (search_end..block.len())
.filter(|&k| block[scan[k] as usize] != 0)
.count();
let mut suffix_cost = [0.0f32; 2];
for (initial_prev, target) in suffix_cost.iter_mut().enumerate() {
let mut remaining = suffix_nzeros;
let mut prev = initial_prev;
let mut k = search_end;
while k < block.len() && remaining != 0 {
let coef = block[scan[k] as usize];
*target += lambda
* prices.token_bits(Token::new(context(remaining, k, prev), pack_signed(coef)));
prev = usize::from(coef != 0);
remaining -= usize::from(coef != 0);
k += 1;
}
}
let max_nzeros = suffix_nzeros + window_len;
let legacy_stride = (max_nzeros + 1) * 2;
if legacy_limit
&& (legacy_stride > RDOQ_MAX_STRIDE || window_len * legacy_stride > RDOQ_MAX_CHOICES)
{
return;
}
let stride = BAND_STRIDE;
let [next_buf, current_buf] = costs;
let mut next = &mut next_buf[..stride];
let mut current_cost = &mut current_buf[..stride];
next[0] = suffix_cost[0];
next[1] = suffix_cost[1];
let mut next_min = suffix_nzeros;
let mut next_max = suffix_nzeros;
let mut original_after_nzeros = 0usize;
for window_index in (0..window_len).rev() {
let k = covered_blocks + window_index;
let idx = scan[k] as usize;
let ideal = source[idx] * inv_qm[idx] * q_scaled;
let distortion_weight = channel_weight
* if c == 1 {
1.0
} else {
chroma_rdoq_weight(distance)
}
* rdoq_distortion_weight(window_index.min(base_window_len), base_window_len, distance);
let (candidates, candidate_count) = rdoq_candidates(ideal, block[idx]);
let mut candidate_distortion = [0.0f32; 5];
for (d, &level) in candidate_distortion[..candidate_count]
.iter_mut()
.zip(candidates.iter())
{
*d = distortion_weight * (ideal - dequantized_level(level)).powi(2);
}
let processed_after = window_len - 1 - window_index;
let min_remaining = suffix_nzeros + original_after_nzeros.saturating_sub(MAX_NZERO_DELTA);
let max_remaining =
suffix_nzeros + (original_after_nzeros + MAX_NZERO_DELTA).min(processed_after);
let current_min = min_remaining;
let current_max = (max_remaining + 1).min(max_nzeros);
current_cost[..(current_max - current_min + 1) * 2].fill(f32::INFINITY);
if !FACTORED {
for next_remaining in min_remaining.max(next_min)..=max_remaining.min(next_max) {
for (candidate_index, &level) in candidates[..candidate_count].iter().enumerate() {
let nonzero = usize::from(level != 0);
let remaining = next_remaining + nonzero;
if remaining > max_nzeros {
continue;
}
let tail = next[(next_remaining - next_min) * 2 + nonzero];
if !tail.is_finite() {
continue;
}
let distortion = candidate_distortion[candidate_index];
let token_cost = if remaining == 0 {
[0.0; 2]
} else {
let bits =
prices.token_bits_pair(context(remaining, k, 0), pack_signed(level));
let rate = if block[idx] == 0 && level != 0 {
lambda * restore_penalty
} else {
lambda
};
[rate * bits[0], rate * bits[1]]
};
let state = (remaining - current_min) * 2;
update_rdoq_state_pair(
distortion,
token_cost,
tail,
candidate_index as u8,
(&mut current_cost[state..state + 2]).try_into().unwrap(),
(&mut choices
[window_index * stride + state..window_index * stride + state + 2])
.try_into()
.unwrap(),
);
}
}
} else {
let source_min = min_remaining.max(next_min);
let source_max = max_remaining.min(next_max);
let zero_candidate = candidates[..candidate_count].iter().position(|&q| q == 0);
let mut cached_context = u32::MAX;
let mut local_cost = [[f32::INFINITY; 2]; 5];
let mut nz_cost = [f32::INFINITY; 2];
let mut nz_before = [f32::INFINITY; 2];
let mut nz_choice = [u8::MAX; 2];
for remaining in current_min..=current_max {
let zero_tail = if zero_candidate.is_some()
&& remaining >= source_min
&& remaining <= source_max
{
next[(remaining - next_min) * 2]
} else {
f32::INFINITY
};
let nz_tail = if remaining > source_min && remaining - 1 <= source_max {
next[(remaining - 1 - next_min) * 2 + 1]
} else {
f32::INFINITY
};
if !zero_tail.is_finite() && !nz_tail.is_finite() {
continue;
}
let ctx = if remaining == 0 {
u32::MAX - 1
} else {
context(remaining, k, 0)
};
if ctx != cached_context {
cached_context = ctx;
nz_cost = [f32::INFINITY; 2];
nz_before = [f32::INFINITY; 2];
nz_choice = [u8::MAX; 2];
for (candidate_index, &level) in
candidates[..candidate_count].iter().enumerate()
{
let token_cost = if remaining == 0 {
[0.0; 2]
} else {
let bits = prices.token_bits_pair(ctx, pack_signed(level));
let rate = if block[idx] == 0 && level != 0 {
lambda * restore_penalty
} else {
lambda
};
[rate * bits[0], rate * bits[1]]
};
for prev in 0..2 {
let cost = candidate_distortion[candidate_index] + token_cost[prev];
local_cost[candidate_index][prev] = cost;
if level != 0 && cost < nz_cost[prev] {
nz_before[prev] = nz_cost[prev];
nz_cost[prev] = cost;
nz_choice[prev] = candidate_index as u8;
}
}
}
}
let state = (remaining - current_min) * 2;
let row = window_index * stride + state;
for prev in 0..2 {
let mut cost = if nz_tail.is_finite() {
nz_cost[prev] + nz_tail
} else {
f32::INFINITY
};
let mut choice = nz_choice[prev];
if cost.is_finite() && nz_before[prev] + nz_tail == cost {
for i in 0..choice as usize {
if candidates[i] != 0 && local_cost[i][prev] + nz_tail == cost {
choice = i as u8;
break;
}
}
}
if zero_tail.is_finite() {
let zero = zero_candidate.unwrap();
let zero_cost = local_cost[zero][prev] + zero_tail;
if zero_cost < cost {
cost = zero_cost;
choice = zero as u8;
}
}
current_cost[state + prev] = cost;
if cost.is_finite() {
choices[row + prev] = choice;
}
}
}
}
std::mem::swap(&mut current_cost, &mut next);
next_min = current_min;
next_max = current_max;
original_after_nzeros += usize::from(block[idx] != 0);
}
let nzero_ctx = fine_non_zero_context(predicted as u32, block_ctx);
let mut best_remaining = 0;
let mut best_cost = f32::INFINITY;
for remaining in next_min..=next_max {
let initial_prev = usize::from(remaining <= block.len() / 16);
let tail = next[(remaining - next_min) * 2 + initial_prev];
if !tail.is_finite() {
continue;
}
let cost = tail + lambda * prices.token_bits(Token::new(nzero_ctx, remaining as u32));
if cost < best_cost {
best_cost = cost;
best_remaining = remaining;
}
}
let mut remaining = best_remaining;
let mut prev = usize::from(remaining <= block.len() / 16);
let mut original_remaining = original_after_nzeros;
for window_index in 0..window_len {
let k = covered_blocks + window_index;
let idx = scan[k] as usize;
let ideal = source[idx] * inv_qm[idx] * q_scaled;
let (candidates, candidate_count) = rdoq_candidates(ideal, block[idx]);
original_remaining -= usize::from(block[idx] != 0);
let row_min = suffix_nzeros + original_remaining.saturating_sub(MAX_NZERO_DELTA);
let choice = choices[window_index * stride + (remaining - row_min) * 2 + prev] as usize;
debug_assert!(choice < candidate_count);
let level = candidates[choice];
block[idx] = level;
let nonzero = usize::from(level != 0);
remaining -= nonzero;
prev = nonzero;
}
}
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
fn rdoq_block_wide_oracle(
channel_weight: f32,
window_cap: usize,
legacy_limit: bool,
prices: &FrozenTokenPrices,
scan: &[u32],
source: &[f32],
inv_qm: &[f32],
q_scaled: f32,
block: &mut [i32],
raw_strategy: u8,
strategy_code: u8,
c: usize,
predicted: u8,
cx: usize,
cy: usize,
distance: f32,
qf_hi: bool,
qf_ratio: f32,
visible: f32,
choices: &mut [u8],
costs: &mut [Vec<f32>; 2],
) {
let lambda = rdoq_lambda(qf_ratio) / visible;
const MAX_NZERO_DELTA: usize = 6;
if !matches!(
raw_strategy,
STRATEGY_DCT
| STRATEGY_DCT16X8
| STRATEGY_DCT8X16
| STRATEGY_DCT16X16
| STRATEGY_DCT32X32
| STRATEGY_DCT32X16
| STRATEGY_DCT16X32
| STRATEGY_DCT64X64
| STRATEGY_DCT64X32
| STRATEGY_DCT32X64
) {
return;
}
let covered_blocks = cx * cy;
let search_end = block.len().min(covered_blocks + window_cap);
let window_len = search_end - covered_blocks;
let block_ctx = fine_block_context(c, strategy_code, qf_hi);
let histo_offset = fine_zero_density_contexts_offset(block_ctx);
let log2_covered_blocks = covered_blocks.trailing_zeros() as usize;
let context = |remaining: usize, k: usize, prev: usize| -> u32 {
histo_offset
+ if covered_blocks == 1 {
zero_density_context_8x8(remaining, k, prev) as u32
} else {
zero_density_context(remaining, k, covered_blocks, log2_covered_blocks, prev) as u32
}
};
let suffix_nzeros = (search_end..block.len())
.filter(|&k| block[scan[k] as usize] != 0)
.count();
let mut suffix_cost = [0.0f32; 2];
for (initial_prev, target) in suffix_cost.iter_mut().enumerate() {
let mut remaining = suffix_nzeros;
let mut prev = initial_prev;
let mut k = search_end;
while k < block.len() && remaining != 0 {
let coef = block[scan[k] as usize];
*target += lambda
* prices.token_bits(Token::new(context(remaining, k, prev), pack_signed(coef)));
prev = usize::from(coef != 0);
remaining -= usize::from(coef != 0);
k += 1;
}
}
let max_nzeros = suffix_nzeros + window_len;
let stride = (max_nzeros + 1) * 2;
if legacy_limit && (stride > RDOQ_MAX_STRIDE || window_len * stride > RDOQ_MAX_CHOICES) {
return;
}
assert!(costs[0].len() >= stride);
assert!(choices.len() >= window_len * stride);
let [next_buf, current_buf] = costs;
let mut next = &mut next_buf[..stride];
let mut current_cost = &mut current_buf[..stride];
next[suffix_nzeros * 2] = suffix_cost[0];
next[suffix_nzeros * 2 + 1] = suffix_cost[1];
let mut next_min = suffix_nzeros;
let mut next_max = suffix_nzeros;
let mut original_after_nzeros = 0usize;
for window_index in (0..window_len).rev() {
let k = covered_blocks + window_index;
let idx = scan[k] as usize;
let ideal = source[idx] * inv_qm[idx] * q_scaled;
let distortion_weight = channel_weight
* if c == 1 {
1.0
} else {
chroma_rdoq_weight(distance)
}
* rdoq_distortion_weight(
window_index.min((24 * covered_blocks).min(64)),
(24 * covered_blocks).min(64),
distance,
);
let (candidates, candidate_count) = rdoq_candidates(ideal, block[idx]);
let mut candidate_distortion = [0.0f32; 5];
for (d, &level) in candidate_distortion[..candidate_count]
.iter_mut()
.zip(candidates.iter())
{
*d = distortion_weight * (ideal - dequantized_level(level)).powi(2);
}
let processed_after = window_len - 1 - window_index;
let min_remaining = suffix_nzeros + original_after_nzeros.saturating_sub(MAX_NZERO_DELTA);
let max_remaining =
suffix_nzeros + (original_after_nzeros + MAX_NZERO_DELTA).min(processed_after);
let current_min = min_remaining;
let current_max = (max_remaining + 1).min(max_nzeros);
current_cost[current_min * 2..(current_max + 1) * 2].fill(f32::INFINITY);
for next_remaining in min_remaining.max(next_min)..=max_remaining.min(next_max) {
for (candidate_index, &level) in candidates[..candidate_count].iter().enumerate() {
let nonzero = usize::from(level != 0);
let remaining = next_remaining + nonzero;
if remaining > max_nzeros {
continue;
}
let tail = next[next_remaining * 2 + nonzero];
if !tail.is_finite() {
continue;
}
let distortion = candidate_distortion[candidate_index];
let token_cost = if remaining == 0 {
[0.0; 2]
} else {
let bits = prices.token_bits_pair(context(remaining, k, 0), pack_signed(level));
[lambda * bits[0], lambda * bits[1]]
};
let state = remaining * 2;
update_rdoq_state_pair(
distortion,
token_cost,
tail,
candidate_index as u8,
(&mut current_cost[state..state + 2]).try_into().unwrap(),
(&mut choices
[window_index * stride + state..window_index * stride + state + 2])
.try_into()
.unwrap(),
);
}
}
std::mem::swap(&mut current_cost, &mut next);
next_min = current_min;
next_max = current_max;
original_after_nzeros += usize::from(block[idx] != 0);
}
let nzero_ctx = fine_non_zero_context(predicted as u32, block_ctx);
let mut best_remaining = 0;
let mut best_cost = f32::INFINITY;
for remaining in next_min..=next_max {
let initial_prev = usize::from(remaining <= block.len() / 16);
let tail = next[remaining * 2 + initial_prev];
if !tail.is_finite() {
continue;
}
let cost = tail + lambda * prices.token_bits(Token::new(nzero_ctx, remaining as u32));
if cost < best_cost {
best_cost = cost;
best_remaining = remaining;
}
}
let mut remaining = best_remaining;
let mut prev = usize::from(remaining <= block.len() / 16);
for window_index in 0..window_len {
let k = covered_blocks + window_index;
let idx = scan[k] as usize;
let ideal = source[idx] * inv_qm[idx] * q_scaled;
let (candidates, candidate_count) = rdoq_candidates(ideal, block[idx]);
let choice = choices[window_index * stride + remaining * 2 + prev] as usize;
debug_assert!(choice < candidate_count);
let level = candidates[choice];
block[idx] = level;
let nonzero = usize::from(level != 0);
remaining -= nonzero;
prev = nonzero;
}
}
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,
}
#[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 = (value * scale).round() as i32;
}
}
#[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 = fmla(value, scale, -(yq as f32) * cfl).round() as i32;
}
}
#[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
};
}
}
}
const CHROMA_RDOQ_WEIGHT_HQ: f32 = 2.0;
const CHROMA_RDOQ_WEIGHT_D0: f32 = 2.25;
const CHROMA_RDOQ_WEIGHT_D1: f32 = 4.0;
#[inline]
pub(crate) fn chroma_rdoq_weight(distance: f32) -> f32 {
let t = ((distance - CHROMA_RDOQ_WEIGHT_D0) / (CHROMA_RDOQ_WEIGHT_D1 - CHROMA_RDOQ_WEIGHT_D0))
.clamp(0.0, 1.0);
CHROMA_RDOQ_WEIGHT_HQ + t * (1.0 - CHROMA_RDOQ_WEIGHT_HQ)
}
pub(crate) const DEFAULT_QUANT_BIAS_1: f32 = 1.0 - 0.07005449891748593;
pub(crate) const DEFAULT_QUANT_BIAS_3: f32 = 0.145;
#[inline]
pub(crate) fn dequantized_level_f32(quant: f32) -> f32 {
let aq = quant.abs();
if aq < 1.125 {
if quant == 0.0 {
0.0
} else {
DEFAULT_QUANT_BIAS_1.copysign(quant)
}
} else {
quant - DEFAULT_QUANT_BIAS_3 / quant
}
}
#[inline]
pub(crate) fn dequantized_level(quant: i32) -> f32 {
let aq = quant.unsigned_abs() as f32;
if aq < 1.125 {
if quant == 0 {
0.0
} else if quant > 0 {
DEFAULT_QUANT_BIAS_1
} else {
-DEFAULT_QUANT_BIAS_1
}
} else {
let q = quant as f32;
q - DEFAULT_QUANT_BIAS_3 / 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 size = xsize * ysize * 64;
for (out, (&q, &dq)) in inout[..size]
.iter_mut()
.zip(quantized[..size].iter().zip(dqm[..size].iter()))
{
*out = dequantized_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,
}
}
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);
}
}
}
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
}
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
}
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))
}
}
#[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>,
}
pub(crate) struct AcGroupScratch {
coeffs: HeapMatrix<f32, 3, 16384>,
quantized: HeapMatrix<i32, 3, 16384>,
source_y: Box<[f32; 16384]>,
block: Box<[i32; 16384]>,
rdoq_choices: Box<[u8; RDOQ_MAX_CHOICES]>,
rdoq_costs: HeapMatrix<f32, 2, RDOQ_MAX_STRIDE>,
gather: Box<[f32; 16384]>,
few_color_keys: Vec<u64>,
recon_err: Box<[f32; 16384]>,
recon_pix: Box<[f32; 16384]>,
}
impl Default for AcGroupScratch {
fn default() -> Self {
Self {
coeffs: HeapMatrix::new(0.0),
quantized: HeapMatrix::new(0),
source_y: heap_array(0.0),
block: heap_array(0),
rdoq_choices: heap_array(u8::MAX),
rdoq_costs: HeapMatrix::new(f32::INFINITY),
gather: heap_array(0.0),
few_color_keys: Vec::new(),
recon_err: heap_array(0.0),
recon_pix: heap_array(0.0),
}
}
}
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>,
}
#[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 chroma_deadzone = ctx.speed.effort().chroma_deadzone && ctx.coding.is_xyb();
let cfl_factor_x = crate::color_correlation::dc_cfl_factor_x(dc_step, ctx.cfl_base_x());
let AcGroupScratch {
coeffs,
quantized,
source_y,
block: pblock,
rdoq_choices,
rdoq_costs,
gather,
few_color_keys,
recon_err,
recon_pix,
} = scratch;
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 && source_dc.is_none()));
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
};
for c in 0..3 {
if hit[c] {
continue;
}
let (input, stride) = footprint.pixels(opsin.plane(c), &mut gather[..]);
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_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);
}
STRATEGY_DCT128X128 => {
let dst = coeffs[c].first_chunk_mut::<16384>().unwrap();
(ctx.dct128x128)(DctInput::new(input, stride), dst);
}
STRATEGY_DCT128X64 => {
let dst = coeffs[c].first_chunk_mut::<8192>().unwrap();
(ctx.dct128x64)(DctInput::new(input, stride), dst);
}
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];
match raw_strategy {
STRATEGY_DCT
| STRATEGY_IDENTITY
| STRATEGY_DCT2X2
| STRATEGY_DCT4X4
| STRATEGY_DCT4X8
| STRATEGY_DCT8X4
| STRATEGY_AFV0..=STRATEGY_AFV3 => {
for c in 0..3 {
dc_vals[c][0] = coeffs[c][0];
}
}
STRATEGY_DCT16X8 => {
for c in 0..3 {
let cb: &[f32; 128] = coeffs[c].first_chunk::<128>().unwrap();
dc_from_dct16x8(cb, dc_vals[c].first_chunk_mut::<2>().unwrap());
}
}
STRATEGY_DCT8X16 => {
for c in 0..3 {
let cb: &[f32; 128] = coeffs[c].first_chunk::<128>().unwrap();
dc_from_dct8x16(cb, dc_vals[c].first_chunk_mut::<2>().unwrap());
}
}
STRATEGY_DCT16X16 => {
for c in 0..3 {
let cb: &[f32; 256] = coeffs[c].first_chunk::<256>().unwrap();
dc_from_dct16x16(cb, dc_vals[c].first_chunk_mut::<4>().unwrap());
}
}
STRATEGY_DCT32X32 => {
for c in 0..3 {
let cb: &[f32; 1024] = coeffs[c].first_chunk::<1024>().unwrap();
(ctx.dc_from_dct32x32)(cb, dc_vals[c].first_chunk_mut::<16>().unwrap());
}
}
STRATEGY_DCT64X64 => {
for c in 0..3 {
let cb: &[f32; 4096] = coeffs[c].first_chunk::<4096>().unwrap();
(ctx.dc_from_dct64x64)(cb, dc_vals[c].first_chunk_mut::<64>().unwrap());
}
}
STRATEGY_DCT64X32 => {
for c in 0..3 {
let cb: &[f32; 2048] = coeffs[c].first_chunk::<2048>().unwrap();
(ctx.dc_from_dct64x32)(cb, dc_vals[c].first_chunk_mut::<32>().unwrap());
}
}
STRATEGY_DCT32X64 => {
for c in 0..3 {
let cb: &[f32; 2048] = coeffs[c].first_chunk::<2048>().unwrap();
(ctx.dc_from_dct32x64)(cb, dc_vals[c].first_chunk_mut::<32>().unwrap());
}
}
STRATEGY_DCT32X16 => {
for c in 0..3 {
let cb: &[f32; 512] = coeffs[c].first_chunk::<512>().unwrap();
(ctx.dc_from_dct32x16)(cb, dc_vals[c].first_chunk_mut::<8>().unwrap());
}
}
STRATEGY_DCT16X32 => {
for c in 0..3 {
let cb: &[f32; 512] = coeffs[c].first_chunk::<512>().unwrap();
(ctx.dc_from_dct16x32)(cb, dc_vals[c].first_chunk_mut::<8>().unwrap());
}
}
STRATEGY_DCT128X128 => {
for c in 0..3 {
(ctx.dc_from_dct128x128)(
coeffs[c].first_chunk::<16384>().unwrap(),
dc_vals[c].first_chunk_mut::<256>().unwrap(),
);
}
}
STRATEGY_DCT128X64 => {
for c in 0..3 {
(ctx.dc_from_dct128x64)(
coeffs[c].first_chunk::<8192>().unwrap(),
dc_vals[c].first_chunk_mut::<128>().unwrap(),
);
}
}
STRATEGY_DCT64X128 => {
for c in 0..3 {
(ctx.dc_from_dct64x128)(
coeffs[c].first_chunk::<8192>().unwrap(),
dc_vals[c].first_chunk_mut::<128>().unwrap(),
);
}
}
_ => unreachable!(),
}
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]);
}
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)[..]),
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)[..])
}
_ => (&matrices.inv_matrix_16x8(1)[..], &matrices.matrix_16x8(1)[..]),
};
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 = dequantized_level(q) * dq * inv_qac;
}
} else {
if rdoq_prices.is_some() || recon_y.is_some() {
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 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],
ctx.speed.effort().large_transform_rdoq,
prices,
coeff_orders.scan_for(strategy_code, 1),
&source_y[..size],
inv_qm_y,
quantize_ac_q_scaled(quant_ac, scale, 1.0),
&mut quantized[1][..size],
raw_strategy,
strategy_code,
1,
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
quant_ac as f32 / qf_threshold.max(1) as f32,
visible,
restore_penalty,
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 = dequantized_level(q) * dq * inv_qac;
}
}
}
if !hit[1]
&& let Some((recon, y_off, _)) = recon_y.as_mut()
{
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_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(),
);
}
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(),
);
}
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 (dz_f, dz_dc_chroma) = if chroma_deadzone && covered_dc == 1 {
let (sat, chroma) =
chroma_block_saturation([dc_vals[0][0], dc_vals[1][0], dc_vals[2][0]]);
(
((sat - CHROMA_DZ_SAT_START) / CHROMA_DZ_SAT_WIDTH).clamp(0.0, 1.0),
chroma,
)
} else {
(0.0, 0.0)
};
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)[..],
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)[..],
_ => &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],
);
chroma_deadzone_fixup(
0,
&coeffs[0][..size],
inv_qm_x,
quantize_ac_q_scaled(quant_ac, scale, x_qm_mul),
distance,
x_qm_mul,
dz_f,
dz_dc_chroma,
&mut quantized[0][..size],
);
if let Some(prices) = rdoq_prices {
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],
ctx.speed.effort().large_transform_rdoq,
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,
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
quant_ac as f32 / qf_threshold.max(1) as f32,
visible,
restore_penalty,
rdoq_choices,
rdoq_costs,
);
}
}
let row_stride = cx * 8;
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)[..],
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)[..],
_ => &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],
);
chroma_deadzone_fixup(
2,
&coeffs[2][..size],
inv_qm_b,
quantize_ac_q_scaled(quant_ac, scale, ctx.b_qm_mul()),
distance,
ctx.b_qm_mul(),
dz_f,
dz_dc_chroma,
&mut quantized[2][..size],
);
if let Some(prices) = rdoq_prices {
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],
ctx.speed.effort().large_transform_rdoq,
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,
predicted,
cx,
cy,
distance,
quant_ac as u32 > qf_threshold,
quant_ac as f32 / qf_threshold.max(1) as f32,
visible,
restore_penalty,
rdoq_choices,
rdoq_costs,
);
}
}
if measure_chroma_distortion && !hit[0] {
let q_scaled_x = quantize_ac_q_scaled(quant_ac, scale, x_qm_mul);
for i in 0..size {
let v = i / row_stride;
let u = i % row_stride;
if v < cy && u < cx {
continue;
}
let ideal = coeffs[0][i] * inv_qm_x[i] * q_scaled_x;
let error = ideal - dequantized_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());
for i in 0..size {
let v = i / row_stride;
let u = i % row_stride;
if v < cy && u < cx {
continue;
}
let ideal = coeffs[2][i] * inv_qm_b[i] * q_scaled_b;
let error = ideal - dequantized_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 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, inv_qm, q_scaled) in [(0usize, inv_qm_x, q_x), (2, inv_qm_b, q_b)] {
if hit[c] {
continue;
}
for i in 0..size {
let (v, u) = (i / row_stride, i % row_stride);
recon_err[i] = if v < cy && u < cx {
0.0
} else {
dequantized_level(quantized[c][i]) / (inv_qm[i] * 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);
}
}
}
}
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;
}
let stream_starts: Vec<usize> = out
.iter()
.map(|o| o.len())
.chain(alternative_out.iter().map(|o| o.len()))
.collect();
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 {
let mut remaining = full_block[k];
let mut sent = 0i32;
for p in 0..=pass {
sent = remaining >> coeff_shifts[p];
remaining -= sent << coeff_shifts[p];
}
pblock[k] = sent;
}
&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 few_colors_counts_distinct_source_colors() {
use crate::image::Image3F;
let mut xyb = Image3F::new(16, 8);
for c in 0..3 {
for y in 0..8 {
let row = xyb.plane_row_mut(c, y);
for x in 0..8 {
row[x] = if x < 4 { 0.1 } else { 0.5 };
row[8 + x] = (y * 8 + x) as f32 * 0.01;
}
}
}
let mut seen = vec![u64::MAX; 100];
let storage = seen.as_ptr();
assert!(super::few_colors(&xyb, 0, 0, 8, 8, 24, &mut seen));
assert!(super::few_colors(&xyb, 0, 0, 8, 8, 2, &mut seen));
assert!(!super::few_colors(&xyb, 0, 0, 8, 8, 1, &mut seen));
assert!(!super::few_colors(&xyb, 8, 0, 16, 8, 24, &mut seen));
assert!(super::few_colors(&xyb, 8, 0, 16, 8, 64, &mut seen));
assert!(!super::few_colors(&xyb, 0, 0, 8, 8, 0, &mut seen));
assert!(super::few_colors(&xyb, 0, 0, 0, 0, 0, &mut seen));
assert!(seen.is_empty());
assert_eq!(seen.as_ptr(), storage, "color storage should be reused");
}
use super::{
QuantizeDcMethods, chroma_block_saturation, chroma_deadzone_fixup, quantize_ac_thresholds,
quantize_ac_thresholds_scaled, quantize_dc_cfl_scalar, quantize_dc_scalar,
selected_quantize_dc_methods,
};
#[test]
fn rdoq_state_pair_preserves_scalar_cost_bits_and_choices() {
let special = [
0.0,
-0.0,
1.0,
-1.0,
f32::INFINITY,
f32::NEG_INFINITY,
f32::MAX,
f32::MIN_POSITIVE,
f32::from_bits(1),
f32::from_bits(0x7fc0_1234),
f32::from_bits(0xffc0_5678),
];
let mut seed = 73u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
if seed.is_multiple_of(4) {
special[seed as usize % special.len()]
} else {
f32::from_bits(seed)
}
};
let mut masks = [false; 4];
for case in 0..16_384 {
let distortion = random();
let token_cost = [random(), random()];
let tail = random();
let cost = token_cost.map(|price| distortion + price + tail);
let mut initial = [random(), random()];
if case % 8 == 0 {
initial = cost;
}
let initial_choices = [case as u8, (case >> 3) as u8];
let candidate = (case % 5) as u8;
let mut expected = initial;
let mut expected_choices = initial_choices;
let mut mask = 0;
for prev in 0..2 {
if cost[prev] < expected[prev] {
expected[prev] = cost[prev];
expected_choices[prev] = candidate;
mask |= 1 << prev;
}
}
masks[mask] = true;
let mut actual = initial;
let mut actual_choices = initial_choices;
super::update_rdoq_state_pair(
distortion,
token_cost,
tail,
candidate,
&mut actual,
&mut actual_choices,
);
assert_eq!(
actual.map(f32::to_bits),
expected.map(f32::to_bits),
"costs in case {case}"
);
assert_eq!(actual_choices, expected_choices, "choices in case {case}");
}
assert_eq!(masks, [true; 4]);
}
#[test]
fn rdoq_reused_scratch_matches_fresh_scratch() {
use crate::dc_group_data::STRATEGY_CODE_LUT;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
let tokens: Vec<_> = (0..4096)
.map(|i| Token::new(i % 2, if i % 3 == 0 { 0 } else { i % 17 }))
.collect();
let mut code = build_entropy_code_no_cluster(&tokens, 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 orders = crate::coeff_order::CoeffOrders::natural();
let mut choices = super::heap_array(0xA5u8);
let mut costs = Box::new([[f32::NAN; super::RDOQ_MAX_STRIDE]; 2]);
let mut seed = 73u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let mut changed = 0;
for case in 0..384 {
let (raw, cx, cy) = [
(super::STRATEGY_DCT, 1, 1),
(super::STRATEGY_DCT16X8, 2, 1),
(super::STRATEGY_DCT16X16, 2, 2),
(super::STRATEGY_DCT32X16, 4, 2),
(super::STRATEGY_DCT32X32, 4, 4),
][case % 5];
let strategy = STRATEGY_CODE_LUT[raw as usize];
let c = case % 3;
let coding = [
crate::CodingTransform::Xyb,
crate::CodingTransform::YCbCr,
crate::CodingTransform::Rgb,
][case / 3 % 3];
let mut scan = orders.scan_for(strategy, c).to_vec();
let size = cx * cy * 64;
if case % 2 == 0 {
for i in cx * cy..size {
let j = cx * cy + random() as usize % (size - cx * cy);
scan.swap(i, j);
}
}
let inv_qm = vec![1.; size];
let source: Vec<f32> = (0..size)
.map(|_| {
if random() % 10 < (case % 10) as u32 {
0.
} else {
(random() % 1024) as f32 / 128. - 4.
}
})
.collect();
let original: Vec<i32> = source.iter().map(|v| v.round() as i32).collect();
let mut expected = original.clone();
let mut actual = original.clone();
let mut fresh_choices = super::heap_array(u8::MAX);
let mut fresh_costs = Box::new([[f32::INFINITY; super::RDOQ_MAX_STRIDE]; 2]);
let run = |block: &mut [i32], choices: &mut _, costs: &mut _| {
super::rdoq_block(
super::rdoq_channel_weights(coding)[c],
false,
&prices,
&scan,
&source,
&inv_qm,
1.,
block,
raw,
strategy,
c,
(case % 64) as u8,
cx,
cy,
[1., 2., 3., 4.][case % 4],
case % 2 == 0,
[0.5, 1.0, 1.2, 2.0][case % 4],
1.0,
1.0,
choices,
costs,
);
};
run(&mut expected, &mut fresh_choices, &mut fresh_costs);
run(&mut actual, &mut choices, &mut costs);
assert_eq!(actual, expected, "scratch reuse in case {case}");
changed += usize::from(actual != original);
}
assert!(
changed > 100,
"exercise finite trellis paths, including changed blocks"
);
}
#[test]
fn rdoq_window_policy_is_coarse_quality_and_large_transform_only() {
for covered in [1, 2, 4, 8, 16, 32, 64] {
let old = (24 * covered).min(64);
assert_eq!(super::rdoq_window_len(covered, false, 8.), old);
assert_eq!(super::rdoq_window_len(covered, true, 2.999), old);
assert_eq!(
super::rdoq_window_len(covered, true, 3.),
if covered >= 8 { 16 * covered } else { old }
);
}
assert!(!crate::Speed::Slow.effort().large_transform_rdoq);
assert!(crate::Speed::ExtraSlow.effort().large_transform_rdoq);
}
#[test]
fn banded_rdoq_matches_legacy_and_reuses_scratch() {
use crate::dc_group_data::STRATEGY_CODE_LUT;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
let tokens: Vec<_> = (0..4096)
.map(|i| Token::new(i % 2, if i % 3 == 0 { 0 } else { i % 17 }))
.collect();
let mut code = build_entropy_code_no_cluster(&tokens, 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 orders = crate::coeff_order::CoeffOrders::natural();
let mut choices = super::heap_array(0xA5u8);
let mut costs = Box::new([[f32::NAN; super::RDOQ_MAX_STRIDE]; 2]);
let mut seed = 73u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let mut changed = 0;
for case in 0..120 {
let (raw, cx, cy) = [
(super::STRATEGY_DCT16X16, 2, 2),
(super::STRATEGY_DCT32X16, 4, 2),
(super::STRATEGY_DCT32X32, 4, 4),
(super::STRATEGY_DCT64X32, 8, 4),
(super::STRATEGY_DCT64X64, 8, 8),
][case % 5];
let strategy = STRATEGY_CODE_LUT[raw as usize];
let c = case % 3;
let coding = [
crate::CodingTransform::Xyb,
crate::CodingTransform::YCbCr,
crate::CodingTransform::Rgb,
][case / 3 % 3];
let mut scan = orders.scan_for(strategy, c).to_vec();
let size = cx * cy * 64;
if case % 2 == 0 {
for i in cx * cy..size {
let j = cx * cy + random() as usize % (size - cx * cy);
scan.swap(i, j);
}
}
let inv_qm = vec![1.; size];
let source: Vec<f32> = (0..size)
.map(|_| {
if !random().is_multiple_of(32) {
0.
} else {
if random() % 2 == 0 { 0.6 } else { -0.6 }
}
})
.collect();
let original: Vec<i32> = source.iter().map(|v| v.round() as i32).collect();
let mut expected = original.clone();
let mut actual = original.clone();
let mut fresh_choices = super::heap_array(u8::MAX);
let mut fresh_costs = Box::new([[f32::INFINITY; super::RDOQ_MAX_STRIDE]; 2]);
let run = |block: &mut [i32], choices: &mut _, costs: &mut _, cap: usize| {
super::rdoq_block_banded::<true>(
super::rdoq_channel_weights(coding)[c],
cap,
false,
&prices,
&scan,
&source,
&inv_qm,
1.,
block,
raw,
strategy,
c,
(case % 64) as u8,
cx,
cy,
[1., 2., 3., 4.][case % 4],
case % 2 == 0,
[0.5, 1.0, 1.2, 2.0][case % 4],
1.0,
1.0,
choices,
costs,
);
};
super::rdoq_block(
super::rdoq_channel_weights(coding)[c],
false,
&prices,
&scan,
&source,
&inv_qm,
1.,
&mut expected,
raw,
strategy,
c,
(case % 64) as u8,
cx,
cy,
[1., 2., 3., 4.][case % 4],
case % 2 == 0,
[0.5, 1.0, 1.2, 2.0][case % 4],
1.0,
1.0,
&mut fresh_choices,
&mut fresh_costs,
);
run(&mut actual, &mut choices, &mut costs, 64);
assert_eq!(actual, expected, "scratch reuse in case {case}");
changed += usize::from(actual != original);
for cap in [128, 256, 512, 1024] {
expected.copy_from_slice(&original);
actual.copy_from_slice(&original);
fresh_costs.fill([f32::INFINITY; super::RDOQ_MAX_STRIDE]);
fresh_choices.fill(u8::MAX);
run(&mut expected, &mut fresh_choices, &mut fresh_costs, cap);
run(&mut actual, &mut choices, &mut costs, cap);
assert_eq!(actual, expected, "cap={cap}, case={case}");
for &idx in &scan[..cx * cy] {
assert_eq!(
actual[idx as usize], original[idx as usize],
"LLF stays frozen"
);
}
}
}
assert!(
changed > 10,
"exercise finite trellis paths, including changed blocks: {changed}"
);
}
#[test]
fn banded_rdoq_matches_absolute_index_oracle_for_wide_dense_blocks() {
use crate::dc_group_data::STRATEGY_CODE_LUT;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
let tokens: Vec<_> = (0..4096)
.map(|i| Token::new(i % 2, if i % 3 == 0 { 0 } else { i % 17 }))
.collect();
let mut code = build_entropy_code_no_cluster(&tokens, 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 orders = crate::coeff_order::CoeffOrders::natural();
let mut choices = super::heap_array(0xA5u8);
let mut costs = Box::new([[f32::NAN; super::RDOQ_MAX_STRIDE]; 2]);
let mut seed = 719u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
for case in 0..70 {
let (raw, cx, cy) = [
(super::STRATEGY_DCT, 1, 1),
(super::STRATEGY_DCT16X8, 2, 1),
(super::STRATEGY_DCT16X16, 2, 2),
(super::STRATEGY_DCT32X16, 4, 2),
(super::STRATEGY_DCT32X32, 4, 4),
(super::STRATEGY_DCT64X32, 8, 4),
(super::STRATEGY_DCT64X64, 8, 8),
][case % 7];
let strategy = STRATEGY_CODE_LUT[raw as usize];
let c = case % 3;
let mut scan = orders.scan_for(strategy, c).to_vec();
let size = cx * cy * 64;
for i in cx * cy..size {
let j = cx * cy + random() as usize % (size - cx * cy);
scan.swap(i, j);
}
let inv_qm = vec![1.; size];
let source: Vec<f32> = (0..size)
.map(|_| {
if case % 3 == 0 && !random().is_multiple_of(32) {
0.
} else {
(random() % 81) as f32 / 10. - 4.
}
})
.collect();
let original: Vec<i32> = source.iter().map(|v| v.round() as i32).collect();
let base_cap = (24 * cx * cy).min(64);
for (cap, legacy_limit) in [
(base_cap, true),
(64, false),
(128, false),
(256, false),
(512, false),
(1024, false),
] {
let mut expected = original.clone();
let mut actual = original.clone();
let window_len = cap.min(size - cx * cy);
let mut reference_choices = vec![u8::MAX; window_len * (size + 1) * 2];
let mut reference_costs = [
vec![f32::INFINITY; (size + 1) * 2],
vec![f32::INFINITY; (size + 1) * 2],
];
super::rdoq_block_wide_oracle(
1.,
cap,
legacy_limit,
&prices,
&scan,
&source,
&inv_qm,
1.,
&mut expected,
raw,
strategy,
c,
17,
cx,
cy,
4.,
false,
1.,
1.,
&mut reference_choices,
&mut reference_costs,
);
super::rdoq_block_banded::<true>(
1.,
cap,
legacy_limit,
&prices,
&scan,
&source,
&inv_qm,
1.,
&mut actual,
raw,
strategy,
c,
17,
cx,
cy,
4.,
false,
1.,
1.,
1.0,
&mut choices,
&mut costs,
);
assert_eq!(
actual, expected,
"absolute-index oracle, case={case}, cap={cap}"
);
for &idx in &scan[..cx * cy] {
assert_eq!(actual[idx as usize], original[idx as usize]);
}
}
}
}
#[test]
fn factored_rdoq_preserves_cost_bits_choices_and_restore_penalties() {
use crate::dc_group_data::STRATEGY_CODE_LUT;
use crate::entropy::{FrozenTokenPrices, Token, build_entropy_code_no_cluster};
let tokens: Vec<_> = (0..16_384)
.map(|i| {
let context = i % 4;
let value = if i % 7 < context + 1 {
0
} else {
(i / 4) % 601
};
Token::new(context, value)
})
.collect();
let mut code = build_entropy_code_no_cluster(&tokens, 4, &mut Vec::new());
code.context_map = (0..crate::ac_context::K_NUM_FINE_AC_CONTEXTS)
.map(|i| ((i / 2 + i % 2) % 4) as u8)
.collect();
let prices = FrozenTokenPrices::new(&code);
let orders = crate::coeff_order::CoeffOrders::natural();
let mut choices0 = super::heap_array(0xA5u8);
let mut choices1 = super::heap_array(0xA5u8);
let mut costs0 = Box::new([[f32::NAN; super::RDOQ_MAX_STRIDE]; 2]);
let mut costs1 = costs0.clone();
let mut seed = 173u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let shapes = [
(super::STRATEGY_DCT, 1, 1),
(super::STRATEGY_DCT16X8, 2, 1),
(super::STRATEGY_DCT16X16, 2, 2),
(super::STRATEGY_DCT32X16, 4, 2),
(super::STRATEGY_DCT32X32, 4, 4),
(super::STRATEGY_DCT64X32, 8, 4),
(super::STRATEGY_DCT64X64, 8, 8),
(super::STRATEGY_DCT128X64, 16, 8),
(super::STRATEGY_DCT128X128, 16, 16),
];
let mut changed = 0;
for case in 0..144 {
let (raw, cx, cy) = shapes[case % shapes.len()];
let strategy = STRATEGY_CODE_LUT[raw as usize];
let c = case / shapes.len() % 3;
let size = cx * cy * 64;
let mut scan = orders.scan_for(strategy, c).to_vec();
if case % 2 == 0 {
for i in cx * cy..size {
let j = cx * cy + random() as usize % (size - cx * cy);
scan.swap(i, j);
}
}
let inv_qm: Vec<_> = (0..size).map(|i| 0.75 + (i % 9) as f32 / 16.).collect();
let source: Vec<f32> = (0..size)
.map(|_| {
let r = random();
if r % 10 < (case / shapes.len() % 10) as u32 {
0.
} else if case % 7 == 0 {
(r % 4001) as f32 / 8. - 250.
} else {
(r % 1024) as f32 / 128. - 4.
}
})
.collect();
let original: Vec<i32> = source.iter().map(|v| v.round() as i32).collect();
for cap in [64, 256, 1024] {
let mut expected = original.clone();
let mut actual = original.clone();
let run = |factored, block: &mut [i32], choices: &mut _, costs: &mut _| {
let refine = if factored {
super::rdoq_block_banded::<true>
} else {
super::rdoq_block_banded::<false>
};
refine(
[0.3, 1., 0.6][c],
cap,
false,
&prices,
&scan,
&source,
&inv_qm,
[0.75, 1.5][case % 2],
block,
raw,
strategy,
c,
(case % 256) as u8,
cx,
cy,
[0.3, 2., 4., 8.][case % 4],
case % 2 == 0,
[0.5, 1.2][case % 2],
[1., 0.125][case % 2],
[1., 2.][case / shapes.len() % 2],
choices,
costs,
);
};
run(false, &mut expected, &mut choices0, &mut costs0);
run(true, &mut actual, &mut choices1, &mut costs1);
assert_eq!(actual, expected, "coefficients, case={case}, cap={cap}");
assert_eq!(
choices0.as_slice(),
choices1.as_slice(),
"choices, case={case}, cap={cap}"
);
for (&a, &b) in costs0.iter().flatten().zip(costs1.iter().flatten()) {
assert_eq!(
a.to_bits(),
b.to_bits(),
"cost bits, case={case}, cap={cap}"
);
}
changed += usize::from(actual != original);
}
}
assert!(changed > 100, "must exercise changed blocks: {changed}");
}
#[test]
fn factored_rdoq_preserves_tail_rounding_ties() {
use crate::dc_group_data::STRATEGY_CODE_LUT;
use crate::entropy::{
FrozenTokenPrices, Token, build_entropy_code_no_cluster, pack_signed,
};
let mut tokens = vec![Token::new(0, 0); 1000];
tokens.extend((0..100).flat_map(|_| [Token::new(0, 1), Token::new(0, 2)]));
let mut code = build_entropy_code_no_cluster(&tokens, 1, &mut Vec::new());
code.context_map = vec![0; crate::ac_context::K_NUM_FINE_AC_CONTEXTS];
let prices = FrozenTokenPrices::new(&code);
let orders = crate::coeff_order::CoeffOrders::natural();
let raw = super::STRATEGY_DCT16X8;
let strategy = STRATEGY_CODE_LUT[raw as usize];
let scan = orders.scan_for(strategy, 1);
let inv_qm = [1.; 128];
let mut collapsed = 0;
for i in 1..100 {
let ideal = i as f32 * 1e-8;
let mut source = [0.; 128];
source[scan[2] as usize] = ideal;
let mut expected = [0; 128];
for &idx in &scan[3..54] {
source[idx as usize] = 1.;
expected[idx as usize] = 1;
}
let mut actual = expected;
let mut choices0 = super::heap_array(0xA5);
let mut choices1 = super::heap_array(0xA5);
let mut costs0 = Box::new([[f32::NAN; super::RDOQ_MAX_STRIDE]; 2]);
let mut costs1 = costs0.clone();
let run = |factored, block: &mut [i32], choices: &mut _, costs: &mut _| {
let refine = if factored {
super::rdoq_block_banded::<true>
} else {
super::rdoq_block_banded::<false>
};
refine(
1., 1, false, &prices, scan, &source, &inv_qm, 1., block, raw, strategy, 1, 17,
2, 1, 1., false, 1., 1., 2., choices, costs,
);
};
run(false, &mut expected, &mut choices0, &mut costs0);
run(true, &mut actual, &mut choices1, &mut costs1);
assert_eq!(actual, expected);
assert_eq!(choices0.as_slice(), choices1.as_slice());
for (&a, &b) in costs0.iter().flatten().zip(costs1.iter().flatten()) {
assert_eq!(a.to_bits(), b.to_bits());
}
let rate = 2. * super::rdoq_lambda(1.);
let minus = (ideal - super::dequantized_level(-1)).powi(2)
+ rate * prices.token_bits(Token::new(0, pack_signed(-1)));
let plus = (ideal - super::dequantized_level(1)).powi(2)
+ rate * prices.token_bits(Token::new(0, pack_signed(1)));
let tail = costs0[0][1];
if plus < minus && plus + tail == minus + tail {
collapsed += 1;
assert_eq!(choices1[2], 1);
assert_eq!(choices1[3], 1);
}
}
assert!(
collapsed > 0,
"must exercise distinct local costs with equal totals"
);
}
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,
];
for len in 1..=input.len() {
let mut want = [i16::MIN; 19];
let mut got = [i16::MIN; 19];
quantize_dc_scalar(&input[..len], 1.0, &mut want[..len]);
(methods.quantize)(&input[..len], 1.0, &mut got[..len]);
assert_eq!(got, want, "plain DC length {len}");
want.fill(i16::MIN);
got.fill(i16::MIN);
quantize_dc_cfl_scalar(&input[..len], &y_quant[..len], 1.0, 0.5, &mut want[..len]);
(methods.quantize_cfl)(&input[..len], &y_quant[..len], 1.0, 0.5, &mut got[..len]);
assert_eq!(got, want, "CfL DC length {len}");
}
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 chroma_block_saturation_is_zero_for_gray() {
assert_eq!(chroma_block_saturation([0.0, 0.5, 0.5]), (0.0, 0.0));
let (sat, chroma) = chroma_block_saturation([0.03, 0.5, 0.3]);
assert!((chroma - (0.0009f32 + 0.04).sqrt()).abs() < 1e-6);
assert!((sat - chroma / 0.5).abs() < 1e-6);
assert_eq!(chroma_block_saturation([0.5, 0.01, 0.9]).0, 1.0);
}
#[test]
fn chroma_deadzone_restores_one_strongest_deleted_coefficient() {
let inv_qm = [1.0f32; 64];
let mut coeffs = [0.0f32; 64];
coeffs[0] = 0.9; coeffs[3] = 0.6;
coeffs[10] = -0.7;
coeffs[20] = 0.3;
let thr = quantize_ac_thresholds_scaled(0, 1, 1, 2.0, 1.0);
assert!(thr[0] > 0.5 && thr[1] > 0.715 - 1e-6);
let run = |coeffs: &[f32; 64], f: f32, dc_chroma: f32| {
let mut q = [0i32; 64];
chroma_deadzone_fixup(0, coeffs, &inv_qm, 1.0, 2.0, 1.0, f, dc_chroma, &mut q);
q
};
assert_eq!(run(&coeffs, 0.0, 1.0), [0; 64]);
let q = run(&coeffs, 1.0, 1.0);
assert_eq!(q[10], -1);
assert_eq!(q.iter().filter(|&&v| v != 0).count(), 1);
assert_eq!(run(&coeffs, 1.0, 10.0), [0; 64]);
assert_eq!(run(&coeffs, 0.5, 1.0)[10], -1);
coeffs[10] = -0.45;
coeffs[3] = 0.45;
assert_eq!(run(&coeffs, 1.0, 1.0), [0; 64]);
}
#[test]
fn chroma_deadzone_preserves_threshold_boundaries_and_ties() {
chroma_deadzone_fixup(0, &[], &[], 1.0, 2.0, 1.0, 0.0, 0.0, &mut []);
let inv_qm = [1.0f32; 65];
let thr = quantize_ac_thresholds_scaled(0, 1, 1, 2.0, 1.0);
for i in 1..64 {
let t = thr[usize::from(i >= 32) * 2 + usize::from(i % 8 >= 4)];
let t = (t - 0.5 * (t - super::CHROMA_DZ_TARGET)).max(0.5);
for magnitude in [t.next_down(), t] {
let mut coeffs = [0.0; 65];
coeffs[i] = -magnitude;
coeffs[0] = 10.0;
coeffs[64] = 10.0;
let mut quantized = [0; 65];
chroma_deadzone_fixup(0, &coeffs, &inv_qm, 1.0, 2.0, 1.0, 0.5, 0.0, &mut quantized);
let mut expected = [0; 65];
if magnitude == t {
expected[i] = -1;
}
assert_eq!(quantized, expected, "threshold at AC index {i}");
}
}
let mut coeffs = [0.0; 65];
coeffs[1] = -0.75;
coeffs[63] = 0.75;
let mut quantized = [0; 65];
chroma_deadzone_fixup(0, &coeffs, &inv_qm, 1.0, 2.0, 1.0, 1.0, 0.0, &mut quantized);
assert_eq!(quantized[1], -1);
assert_eq!(quantized[63], 0);
coeffs[1] = 100.0;
chroma_deadzone_fixup(
0,
&coeffs,
&inv_qm,
1.0,
2.0,
1.0,
1.0,
10.0,
&mut quantized,
);
assert_eq!(quantized[1], -1);
assert_eq!(quantized[63], 0);
}
#[test]
fn chroma_deadzone_matches_original_scan() {
let reference = |c,
coeffs: &[f32],
inv_qm: &[f32],
q_scaled: f32,
distance: f32,
qm_mul: f32,
f: f32,
dc_chroma: f32,
quantized: &mut [i32]| {
if f <= 0.0 {
return;
}
let thr = quantize_ac_thresholds_scaled(c, 1, 1, distance, qm_mul);
let eff: [f32; 4] =
std::array::from_fn(|i| (thr[i] - f * (thr[i] - super::CHROMA_DZ_TARGET)).max(0.5));
let mut best = (0.0f32, usize::MAX);
let mut deleted = 0.0f32;
for i in 1..64 {
if quantized[i] != 0 {
continue;
}
deleted += coeffs[i] * coeffs[i];
let t = eff[usize::from(i >= 32) * 2 + usize::from(i % 8 >= 4)];
let ideal = coeffs[i] * inv_qm[i] * q_scaled;
if ideal.abs() >= t && ideal.abs() > best.0 {
best = (ideal.abs(), i);
}
}
if best.1 == usize::MAX
|| deleted.sqrt() < super::CHROMA_DZ_MIN_RELATIVE_ENERGY * dc_chroma
{
return;
}
quantized[best.1] = if coeffs[best.1] < 0.0 { -1 } else { 1 };
};
let mut seed = 73u32;
let mut random = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
(seed >> 8) as f32 / (1u32 << 24) as f32
};
for case in 0..8192 {
let c = case % 3;
let mut coeffs: [f32; 64] = std::array::from_fn(|_| 4.0 * random() - 2.0);
let inv_qm: [f32; 64] = std::array::from_fn(|_| 0.125 + 3.0 * random());
let zero_fraction = [0.0, 0.1, 0.5, 0.9, 1.0][case % 5];
let mut actual: [i32; 64] =
std::array::from_fn(|_| if random() < zero_fraction { 0 } else { -2 });
if case % 8 == 0 {
coeffs[1 + case % 63] =
[0.0, -0.0, f32::INFINITY, f32::NEG_INFINITY, f32::NAN][case / 8 % 5];
}
let mut expected = actual;
let q_scaled = [0.0, 0.125, 0.5, 1.0, 4.0, f32::NAN][case % 6];
let distance = 0.1 + 3.0 * random();
let qm_mul = [0.8, 1.0, 1.25][case / 3 % 3];
let f = [0.0, 0.25, 0.5, 1.0, f32::NAN][case / 5 % 5];
let deleted = (1..64)
.filter(|&i| actual[i] == 0)
.fold(0.0f32, |sum, i| sum + coeffs[i] * coeffs[i]);
let cutoff = deleted.sqrt() / super::CHROMA_DZ_MIN_RELATIVE_ENERGY;
let dc_chroma = [
0.0,
cutoff.next_down(),
cutoff,
cutoff.next_up(),
f32::NAN,
100.0 * random(),
][case / 6 % 6];
reference(
c,
&coeffs,
&inv_qm,
q_scaled,
distance,
qm_mul,
f,
dc_chroma,
&mut expected,
);
chroma_deadzone_fixup(
c,
&coeffs,
&inv_qm,
q_scaled,
distance,
qm_mul,
f,
dc_chroma,
&mut actual,
);
assert_eq!(actual, expected, "case {case}");
}
}
#[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)
);
}
}