#![forbid(unsafe_code)]
use super::dc_tokens::{
DC_PREDICTOR_WEIGHTED, DcPlanePrice, DcPredictorChoice, K_GRAD_RANGE_MAX, K_GRAD_RANGE_MID,
K_GRAD_RANGE_MIN, choose_dc_predictors_from_counts, collect_dc_tokens_with_gradient,
};
use super::quantization::DistanceParams;
use crate::coder_scratch::DcPredictorScratch;
use crate::color_correlation::{CflFrame, dc_cfl_factor, dc_cfl_factor_x};
use crate::dc_group_data::DcGroupData;
use crate::dc_ops::BdcCompensation;
use crate::dct::fmla;
use crate::entropy::{ALPHABET_SIZE, Token, f_log2, pack_signed, uint_encode};
use crate::quant_weights::INV_DC_QUANT;
use crate::static_entropy_codes::{K_GRADIENT_CONTEXT_LUT, K_NUM_DC_CONTEXTS};
use crate::util::{EncodeError, try_vec};
use crate::weighted_predictor::{WpParams, WpPrediction, WpState, WpUpdate};
static DC_RDO_LAMBDA: [f32; 2] = [0.04, 0.15];
static DC_RDO_LAMBDA_DISTANCE: [f32; 2] = [1.0, 2.0];
const DC_RDO_X_LAMBDA_SCALE: f32 = 1.5;
const DC_RDO_LUMA_LAMBDA_SCALE: f32 = 0.5;
const DC_RDO_MAX_ERROR: f32 = 1.0;
const DC_RDO_MIN_GAIN_BITS: f64 = 8.0;
struct Prices {
bits: Vec<[f32; ALPHABET_SIZE]>,
}
impl Prices {
fn new(
groups: &[DcGroupData],
scratch: &mut DcPredictorScratch,
) -> Result<(Self, DcPredictorChoice, [DcPlanePrice; 2]), EncodeError> {
let mut counts = try_vec![[0u32; ALPHABET_SIZE]; 6 * K_NUM_DC_CONTEXTS]?;
scratch.counts.as_flattened_mut().fill(0);
scratch.extra.fill(0);
let mut raw_bits = [0u64; 3];
for dc in groups {
let (wp, grad) = collect_dc_tokens_with_gradient(
dc,
&DC_PREDICTOR_WEIGHTED,
&mut Vec::new(),
false,
true,
WpParams::DEFAULT,
);
let mut offset = 0;
for (mchan, c) in [1, 0, 2].into_iter().enumerate() {
let end = offset + dc.quant_dc.plane(c).as_slice().len();
for (candidate, tokens) in [&wp[offset..end], &grad[offset..end]]
.into_iter()
.enumerate()
{
for token in tokens {
let context = token.context as usize;
let (symbol, extra, _) = uint_encode(token.value);
let slot = candidate * K_NUM_DC_CONTEXTS + context;
scratch.counts[slot][symbol as usize] += 1;
scratch.extra[slot] += u64::from(extra);
if candidate == 0 {
raw_bits[mchan] += u64::from(extra);
}
counts[(candidate * 3 + mchan) * K_NUM_DC_CONTEXTS + context]
[symbol as usize] += 1;
}
}
offset = end;
}
}
let predictors = choose_dc_predictors_from_counts(scratch);
let mut bits = try_vec![[0.0; ALPHABET_SIZE]; 3 * K_NUM_DC_CONTEXTS]?;
for (slot, bits) in bits.iter_mut().enumerate() {
let context = slot % K_NUM_DC_CONTEXTS;
let candidate = usize::from(predictors[context]);
let counts = &counts[candidate * 3 * K_NUM_DC_CONTEXTS + slot];
let total =
counts.iter().map(|&n| f64::from(n)).sum::<f64>() + 0.5 * ALPHABET_SIZE as f64;
for (&count, bits) in counts.iter().zip(bits) {
*bits = f_log2(total / (f64::from(count) + 0.5)) as f32;
}
}
let base_prices = std::array::from_fn(|i| {
let mchan = i + 1;
DcPlanePrice {
tokens: Vec::new(),
counts: counts[mchan * K_NUM_DC_CONTEXTS..(mchan + 1) * K_NUM_DC_CONTEXTS].to_vec(),
raw_bits: raw_bits[mchan],
}
});
Ok((Self { bits }, predictors, base_prices))
}
}
pub(super) fn refine_dc_rounding(
groups: &mut [DcGroupData],
distp: &DistanceParams,
ytob_dc: i32,
cfl: CflFrame,
compensate_b: bool,
scratch: &mut DcPredictorScratch,
) -> Result<(), EncodeError> {
if groups
.iter()
.any(|dc| dc.source_dc_y.is_none() || dc.source_dc_x.is_none() || dc.source_dc_b.is_none())
{
return Ok(());
}
let (prices, predictors, base_prices) = Prices::new(groups, scratch)?;
let inv: [f32; 3] =
std::array::from_fn(|c| INV_DC_QUANT[c] * distp.scale_dc / distp.dc_step[c]);
let factors = [
dc_cfl_factor_x(distp.dc_step, cfl.base_x),
0.0,
dc_cfl_factor(distp.dc_step, ytob_dc, cfl),
];
let compensation = BdcCompensation::new(distp);
let [d0, d1] = DC_RDO_LAMBDA_DISTANCE;
let t = ((distp.distance - d0) / (d1 - d0)).clamp(0.0, 1.0);
let lambda = DC_RDO_LAMBDA[0] + (DC_RDO_LAMBDA[1] - DC_RDO_LAMBDA[0]) * t;
let mut rounding = RoundingScratch::new();
let group_sizes: Vec<usize> = groups
.iter()
.map(|dc| dc.quant_dc.plane(0).as_slice().len())
.collect();
let mut previous = try_vec![0i16; group_sizes.iter().sum()]?;
let mut targets = try_vec![0.0f32; group_sizes.iter().copied().max().unwrap_or(0)]?;
let mut candidate_price = DcPlanePrice::default();
for ((mchan, c, lambda), base_price) in [(1, 0, lambda * DC_RDO_X_LAMBDA_SCALE), (2, 2, lambda)]
.into_iter()
.zip(base_prices)
{
let mut offset = 0;
for (dc, &len) in groups.iter().zip(&group_sizes) {
previous[offset..offset + len].copy_from_slice(dc.quant_dc.plane(c).as_slice());
offset += len;
}
candidate_price.clear();
let (mut base_error, mut candidate_error) = (0.0f64, 0.0f64);
let mut offset = 0;
for (dc, &len) in groups.iter_mut().zip(&group_sizes) {
let (w, h) = (dc.quant_dc.xsize(), dc.quant_dc.ysize());
let previous = &previous[offset..offset + len];
let targets = &mut targets[..len];
offset += len;
let source = match c {
0 => dc.source_dc_x.as_ref().unwrap(),
1 => dc.source_dc_y.as_ref().unwrap(),
_ => dc.source_dc_b.as_ref().unwrap(),
};
for (((target, &source), &yq), &sy) in targets
.iter_mut()
.zip(source.as_slice())
.zip(dc.quant_dc.plane(1).as_slice())
.zip(dc.source_dc_y.as_ref().unwrap().as_slice())
{
*target = fmla(source, inv[c], -f32::from(yq) * factors[c]);
if c == 2 && compensate_b {
*target = compensation.adjust(*target, sy, yq);
}
}
base_error += targets
.iter()
.zip(previous)
.map(|(&t, &q)| f64::from(t - f32::from(q)).powi(2))
.sum::<f64>();
refine_channel_with_scratch(
dc.quant_dc.plane_mut(c).as_mut_slice(),
targets,
w,
h,
mchan,
&predictors,
&prices,
lambda,
&mut rounding,
|_, weighted| candidate_price.add_token(weighted),
);
let levels = dc.quant_dc.plane(c).as_slice();
candidate_error += targets
.iter()
.zip(levels)
.map(|(&t, &q)| f64::from(t - f32::from(q)).powi(2))
.sum::<f64>();
}
let (base_bits, candidate_bits) = (base_price.bits(), candidate_price.bits());
let lambda = f64::from(lambda);
if candidate_bits + DC_RDO_MIN_GAIN_BITS >= base_bits
|| candidate_error + lambda * candidate_bits >= base_error + lambda * base_bits
{
let mut offset = 0;
for (dc, &len) in groups.iter_mut().zip(&group_sizes) {
dc.quant_dc
.plane_mut(c)
.as_mut_slice()
.copy_from_slice(&previous[offset..offset + len]);
offset += len;
}
}
}
Ok(())
}
#[inline(always)]
fn dc_rounding_bounds<const NATIVE_ROUND: bool>(target: f32) -> [i64; 2] {
if NATIVE_ROUND {
[target.floor() as i64, target.ceil() as i64]
} else {
dc_bounds_from_truncation(target)
}
}
#[inline]
fn dc_bounds_from_truncation(target: f32) -> [i64; 2] {
let bounded = target.clamp(f32::from(i16::MIN), f32::from(i16::MAX));
let truncated = bounded as i32;
let back = truncated as f32;
[
i64::from(truncated - i32::from(bounded < back)),
i64::from(truncated + i32::from(bounded > back)),
]
}
#[derive(Clone, Copy)]
struct TrellisNode {
level: i16,
back: u16,
cost: f32,
update: WpUpdate,
}
#[derive(Clone, Copy)]
struct TrellisStep {
level: i16,
back: u16,
}
type TrellisPrediction = (i64, usize, WpPrediction, f32);
struct RoundingScratch {
wp: WpState,
base: Vec<[u32; 4]>,
older: Vec<TrellisNode>,
previous: Vec<TrellisNode>,
column: Vec<TrellisNode>,
history: Vec<TrellisStep>,
starts: Vec<usize>,
predictions: Vec<TrellisPrediction>,
}
impl RoundingScratch {
fn new() -> Self {
Self {
wp: WpState::new(0),
base: Vec::new(),
older: Vec::new(),
previous: Vec::new(),
column: Vec::new(),
history: Vec::new(),
starts: Vec::new(),
predictions: Vec::new(),
}
}
}
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
fn refine_channel(
levels: &mut [i16],
targets: &[f32],
w: usize,
h: usize,
mchan: usize,
predictors: &DcPredictorChoice,
prices: &Prices,
lambda: f32,
mut emit: impl FnMut(Token),
) {
refine_channel_with_scratch(
levels,
targets,
w,
h,
mchan,
predictors,
prices,
lambda,
&mut RoundingScratch::new(),
|selected, _| emit(selected),
);
}
#[allow(clippy::too_many_arguments)]
fn refine_channel_with_scratch(
levels: &mut [i16],
targets: &[f32],
w: usize,
h: usize,
mchan: usize,
predictors: &DcPredictorChoice,
prices: &Prices,
lambda: f32,
scratch: &mut RoundingScratch,
mut emit: impl FnMut(Token, Token),
) {
const NATIVE_ROUND: bool =
!cfg!(any(target_arch = "x86", target_arch = "x86_64")) || cfg!(target_feature = "sse4.1");
if w == 0 || h == 0 {
return;
}
let RoundingScratch {
wp,
base,
older,
previous,
column,
history,
starts,
predictions,
} = scratch;
wp.reset(w, WpParams::DEFAULT);
let channel_bits = &prices.bits[mchan * K_NUM_DC_CONTEXTS..(mchan + 1) * K_NUM_DC_CONTEXTS];
for y in 0..h {
wp.save_row_base(y, base);
history.clear();
starts.clear();
older.clear();
previous.clear();
for x in 0..w {
let i = y * w + x;
let get = |x: usize, y: usize| i64::from(levels[y * w + x]);
let target = targets[i];
let current = i64::from(levels[i]);
let max_error = (target - current as f32).abs().max(DC_RDO_MAX_ERROR);
let forced = !target.is_finite() || max_error > 2.0 * f32::from(i16::MAX);
starts.push(history.len());
column.clear();
let push = |nodes: &mut Vec<TrellisNode>, level: i64| {
let level = level.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16;
if (forced && !nodes.is_empty())
|| (target - f32::from(level)).abs() > max_error
|| nodes.iter().any(|n| n.level == level)
{
return;
}
nodes.push(TrellisNode {
level,
cost: f32::INFINITY,
back: 0,
update: WpUpdate::default(),
});
};
push(column, current);
if target.is_finite() {
let [floor, ceil] = dc_rounding_bounds::<NATIVE_ROUND>(target);
for candidate in [floor, ceil, current - 1, current + 1] {
push(column, candidate);
}
}
predictions.clear();
for s in 0..previous.len().max(1) {
let (west_value, path_cost) = if x > 0 {
let node = previous[s];
let west2 = (x > 1).then(|| older[usize::from(node.back)].update);
wp.stage_path(x, y, base, Some(node.update), west2);
(i64::from(node.level), node.cost)
} else {
(0, 0.0)
};
let north = if y > 0 { get(x, y - 1) } else { west_value };
let west = if x == 0 { north } else { west_value };
let ne = if y > 0 {
get((x + 1).min(w - 1), y - 1)
} else {
west
};
let nw = if y > 0 && x > 0 {
get(x - 1, y - 1)
} else {
north
};
let nn = if y > 1 { get(x, y - 2) } else { north };
let prediction = wp.predict(x, y, north, west, ne, nw, nn);
let prop = (K_GRAD_RANGE_MID + wp.wp_prop).clamp(K_GRAD_RANGE_MIN, K_GRAD_RANGE_MAX)
as usize;
let context = K_GRADIENT_CONTEXT_LUT[prop] as usize;
let prediction = if predictors[context] {
(north + west - nw).clamp(north.min(west), north.max(west))
} else {
prediction
};
if target.is_finite() {
push(column, prediction);
}
predictions.push((prediction, context, wp.last_prediction(), path_cost));
}
for node in column.iter_mut() {
let error = if forced {
0.0
} else {
target - f32::from(node.level)
};
let distortion = error * error;
let mut winning_prediction = None;
for (back, &(prediction, context, _, path_cost)) in predictions.iter().enumerate() {
let (symbol, extra, _) =
uint_encode(pack_signed((i64::from(node.level) - prediction) as i32));
let cost = path_cost
+ distortion
+ lambda * (channel_bits[context][symbol as usize] + extra as f32);
if cost < node.cost {
node.cost = cost;
node.back = back as u16;
winning_prediction = Some(back);
}
}
if let Some(back) = winning_prediction {
node.update = WpState::update_for(predictions[back].2, i64::from(node.level));
}
history.push(TrellisStep {
level: node.level,
back: node.back,
});
}
std::mem::swap(older, previous);
std::mem::swap(previous, column);
}
let mut best = previous
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| a.cost.total_cmp(&b.cost))
.unwrap()
.0;
for x in (0..w).rev() {
let step = history[starts[x] + best];
levels[y * w + x] = step.level;
best = usize::from(step.back);
}
wp.restore_row_base(y, base);
for x in 0..w {
let get = |x: usize, y: usize| i64::from(levels[y * w + x]);
let west = if x > 0 { get(x - 1, y) } else { 0 };
let north = if y > 0 { get(x, y - 1) } else { west };
let west = if x == 0 { north } else { west };
let ne = if y > 0 {
get((x + 1).min(w - 1), y - 1)
} else {
west
};
let nw = if y > 0 && x > 0 {
get(x - 1, y - 1)
} else {
north
};
let nn = if y > 1 { get(x, y - 2) } else { north };
let weighted = wp.predict(x, y, north, west, ne, nw, nn);
let prop =
(K_GRAD_RANGE_MID + wp.wp_prop).clamp(K_GRAD_RANGE_MIN, K_GRAD_RANGE_MAX) as usize;
let context = K_GRADIENT_CONTEXT_LUT[prop] as usize;
let prediction = if predictors[context] {
(north + west - nw).clamp(north.min(west), north.max(west))
} else {
weighted
};
let level = get(x, y);
emit(
Token::new(context as u32, pack_signed((level - prediction) as i32)),
Token::new(context as u32, pack_signed((level - weighted) as i32)),
);
wp.update(level, x, y);
}
}
}
const X_DC_STEP_LAMBDA: f64 = 0.02;
const X_DC_STEP_SIGMA: f32 = 3.0;
const X_DC_STEP_FINEST: f32 = 0.5;
const X_DC_STEP_SPACING: f32 = 0.02;
pub(super) fn choose_x_dc_step(
groups: &mut [DcGroupData],
distp: &DistanceParams,
cfl: CflFrame,
quantize: crate::group::QuantizeDcCflFn,
pool: &crate::thread_pool::ThreadPool,
scratch: &mut crate::coder_scratch::CoderScratch,
) -> [f32; 3] {
let mut dc_step = distp.dc_step;
if groups.is_empty() || groups.iter().any(|dc| dc.source_dc_x.is_none()) {
return dc_step;
}
let dc_error = crate::dc_ops::selected_dc_error_row_fn();
let base = dc_step[0];
let kernel = gaussian_kernel(X_DC_STEP_SIGMA);
let cost = |m: f32, groups: &[DcGroupData], mut keep: Option<&mut Vec<Vec<i16>>>| -> f64 {
let mut price = DcPlanePrice::default();
let (mut levels, mut errors, mut blurred) = (Vec::new(), Vec::new(), Vec::new());
let mut step = dc_step;
step[0] = base * m;
let scale = INV_DC_QUANT[0] / step[0] * distp.scale_dc;
let factor = dc_cfl_factor_x(step, cfl.base_x);
let mut distortion = 0.0f64;
for dc in groups {
let source = dc.source_dc_x.as_ref().unwrap();
let (w, h) = (source.xsize(), source.ysize());
levels.resize(w * h, 0i16);
errors.resize(w * h, 0.0f32);
for y in 0..h {
let luma = dc.quant_dc.plane_row(1, y);
let row = &mut levels[y * w..][..w];
quantize(source.row(y), luma, scale, factor, row);
dc_error(
source.row(y),
luma,
row,
scale,
factor,
m,
&mut errors[y * w..][..w],
);
}
distortion += lowpass_energy(&errors, w, h, &kernel, &mut blurred);
price.add(&levels, w, h, w);
if let Some(keep) = keep.as_deref_mut() {
keep.push(levels[..w * h].to_vec());
}
}
distortion + X_DC_STEP_LAMBDA * price.bits()
};
let candidates = ((1.0 - X_DC_STEP_FINEST) / X_DC_STEP_SPACING).round() as usize;
let multipliers: Vec<f32> = std::iter::once(1.0)
.chain((1..=candidates).map(|k| {
let multiplier = 1.0 - X_DC_STEP_SPACING * k as f32;
crate::color_correlation::signaled_dc_step(0, base * multiplier) / base
}))
.collect();
let shared: &[DcGroupData] = groups;
let costs = pool.steal_map(scratch, multipliers.len(), |i, _| {
cost(multipliers[i], shared, None)
});
let mut best = (costs[0], 1.0f32);
for (&m, &candidate) in multipliers.iter().zip(&costs).skip(1) {
if candidate < best.0 {
best = (candidate, m);
}
}
if best.1 != 1.0 {
let mut levels = Vec::with_capacity(groups.len());
cost(best.1, groups, Some(&mut levels));
for (dc, levels) in groups.iter_mut().zip(&levels) {
let w = dc.source_dc_x.as_ref().unwrap().xsize();
for (y, row) in levels.chunks_exact(w).enumerate() {
dc.quant_dc.plane_row_mut(0, y).copy_from_slice(row);
}
}
dc_step[0] = base * best.1;
}
dc_step
}
fn gaussian_kernel(sigma: f32) -> Vec<f32> {
let radius = (3.0 * sigma).ceil() as i32;
let taps: Vec<f32> = (-radius..=radius)
.map(|i| (-(i * i) as f32 / (2.0 * sigma * sigma)).exp())
.collect();
let sum: f32 = taps.iter().sum();
taps.into_iter().map(|v| v / sum).collect()
}
#[inline]
fn mirror(mut i: isize, n: usize) -> usize {
let n = n as isize;
loop {
if i < 0 {
i = -i - 1;
} else if i >= n {
i = 2 * n - i - 1;
} else {
return i as usize;
}
if n == 1 {
return 0;
}
}
}
fn lowpass_energy(plane: &[f32], w: usize, h: usize, kernel: &[f32], tmp: &mut Vec<f32>) -> f64 {
let n = w * h;
if n == 0 || kernel.is_empty() {
tmp.resize(n, 0.0);
tmp.fill(0.0);
return 0.0;
}
let add_scaled = crate::dc_ops::selected_add_scaled_fn();
let r = kernel.len() / 2;
let padded_len = w + kernel.len() - 1;
tmp.resize(n + padded_len + w, 0.0);
let (horizontal, scratch) = tmp.split_at_mut(n);
let (padded, accum) = scratch.split_at_mut(padded_len);
for (src, dst) in plane[..n]
.chunks_exact(w)
.zip(horizontal.chunks_exact_mut(w))
{
let (left, rest) = padded.split_at_mut(r);
let (middle, right) = rest.split_at_mut(w);
middle.copy_from_slice(src);
for (i, value) in left.iter_mut().enumerate() {
*value = src[mirror(i as isize - r as isize, w)];
}
for (i, value) in right.iter_mut().enumerate() {
*value = src[mirror(w as isize + i as isize, w)];
}
dst.fill(0.0);
for (j, &tap) in kernel.iter().enumerate() {
add_scaled(dst, &padded[j..j + w], tap);
}
}
let mut energy = 0.0f64;
for y in 0..h {
accum.fill(0.0);
for (j, &tap) in kernel.iter().enumerate() {
let row = mirror(y as isize + j as isize - r as isize, h);
add_scaled(accum, &horizontal[row * w..][..w], tap);
}
energy = accum.iter().fold(energy, |sum, &value| {
let value = f64::from(value);
sum + value * value
});
}
tmp.truncate(n);
energy
}
pub(super) struct LearnedDcRates<'a> {
pub(super) tree: &'a crate::ma_tree::LearnedTree,
pub(super) leaf_ctx: &'a [u32],
pub(super) offsets: &'a [i32],
pub(super) code: &'a crate::entropy::OwnedEntropyCode,
pub(super) wp_params: WpParams,
}
struct LearnedCost {
tables: Vec<Vec<f32>>,
}
impl LearnedCost {
fn new(code: &crate::entropy::OwnedEntropyCode) -> Option<Self> {
if code.use_prefix_code {
return None;
}
let log_tab = f64::from(crate::entropy::ANS_LOG_TAB_SIZE);
Some(Self {
tables: code
.ans_histograms
.iter()
.map(|h| {
h.freqs
.iter()
.map(|&f| {
if f == 0 {
log_tab as f32
} else {
(log_tab - f_log2(f64::from(f))) as f32
}
})
.collect()
})
.collect(),
})
}
#[inline]
fn bits(&self, code: &crate::entropy::OwnedEntropyCode, ctx: u32, residual: i64) -> f32 {
let cl = code.context_map[ctx as usize] as usize;
let (sym, nbits, _) = crate::entropy::uint_encode_with_config(
pack_signed(residual as i32),
code.hybrid_uint_configs[cl],
);
let table = &self.tables[cl];
table[(sym as usize).min(table.len() - 1)] + nbits as f32
}
}
struct RefPlane<'a> {
pixels: &'a [i32],
width: usize,
}
#[allow(clippy::too_many_arguments)]
#[inline]
fn learned_props(
chan: i32,
stream: i32,
x: usize,
y: usize,
w: usize,
above: &dyn Fn(usize) -> i64,
above2: &dyn Fn(usize) -> i64,
west: i64,
west_west: i64,
wp_prop: i64,
refs: &[RefPlane<'_>],
props: &mut [i32; crate::ma_tree::NUM_MA_PROPS],
) -> crate::lossless::PredictorNeighbors {
let north = if y > 0 { above(x) } else { west };
let left = if x > 0 { west } else { north };
let top_left = if x > 0 && y > 0 { above(x - 1) } else { left };
let top_right = if x + 1 < w && y > 0 {
above(x + 1)
} else {
north
};
let top_top = if y > 1 { above2(x) } else { north };
let left_left = if x > 1 { west_west } else { left };
let top_right_right = if x + 2 < w && y > 0 {
above(x + 2)
} else {
top_right
};
let local_gradient = left + north - top_left;
let previous_local_gradient = if x == 0 {
0
} else {
let px = x - 1;
let p_west = west_west;
let p_north = if y > 0 { above(px) } else { p_west };
let p_left = if px > 0 { p_west } else { p_north };
let p_top_left = if px > 0 && y > 0 {
above(px - 1)
} else {
p_left
};
p_left + p_north - p_top_left
};
props[0] = chan;
props[1] = stream;
props[2] = y as i32;
props[3] = x as i32;
props[4] = north.abs() as i32;
props[5] = left.abs() as i32;
props[6] = north as i32;
props[7] = left as i32;
props[8] = (left - previous_local_gradient) as i32;
props[9] = local_gradient as i32;
props[10] = (left - top_left) as i32;
props[11] = (top_left - north) as i32;
props[12] = (north - top_right) as i32;
props[13] = (north - top_top) as i32;
props[14] = (left - left_left) as i32;
props[15] = wp_prop as i32;
for (r, out) in refs.iter().zip(props[16..].as_chunks_mut::<4>().0) {
let row = &r.pixels[y * r.width..];
let v = i64::from(row[x]);
let rl = if x > 0 { i64::from(row[x - 1]) } else { 0 };
let (rt, rtl) = if y > 0 {
let up = &r.pixels[(y - 1) * r.width..];
(
i64::from(up[x]),
if x > 0 { i64::from(up[x - 1]) } else { rl },
)
} else {
(rl, rl)
};
let g = (rl + rt - rtl).clamp(rl.min(rt), rl.max(rt));
let d = v - g;
*out = [v.abs() as i32, v as i32, d.abs() as i32, d as i32];
}
crate::lossless::PredictorNeighbors {
left,
top: north,
top_left,
top_right,
left_left,
top_top,
top_right_right,
}
}
pub(super) fn refine_dc_rounding_learned(
groups: &mut [DcGroupData],
distp: &DistanceParams,
ytob_dc: i32,
cfl: CflFrame,
compensate_b: bool,
rates: &LearnedDcRates<'_>,
) -> Result<bool, EncodeError> {
if groups
.iter()
.any(|dc| dc.source_dc_y.is_none() || dc.source_dc_x.is_none() || dc.source_dc_b.is_none())
{
return Ok(false);
}
let Some(cost) = LearnedCost::new(rates.code) else {
return Ok(false);
};
let inv: [f32; 3] =
std::array::from_fn(|c| INV_DC_QUANT[c] * distp.scale_dc / distp.dc_step[c]);
let factors = [
dc_cfl_factor_x(distp.dc_step, cfl.base_x),
0.0,
dc_cfl_factor(distp.dc_step, ytob_dc, cfl),
];
let compensation = BdcCompensation::new(distp);
let [d0, d1] = DC_RDO_LAMBDA_DISTANCE;
let t = ((distp.distance - d0) / (d1 - d0)).clamp(0.0, 1.0);
let lambda = DC_RDO_LAMBDA[0] + (DC_RDO_LAMBDA[1] - DC_RDO_LAMBDA[0]) * t;
let mut rounding = RoundingScratch::new();
let group_sizes: Vec<usize> = groups
.iter()
.map(|dc| dc.quant_dc.plane(0).as_slice().len())
.collect();
let mut previous = try_vec![0i16; group_sizes.iter().sum()]?;
let mut targets = try_vec![0.0f32; group_sizes.iter().copied().max().unwrap_or(0)]?;
let mut changed = false;
for (mchan, c, lambda) in [
(0usize, 1usize, lambda * DC_RDO_LUMA_LAMBDA_SCALE),
(1, 0, lambda * DC_RDO_X_LAMBDA_SCALE),
(2, 2, lambda),
] {
let mut offset = 0;
for (dc, &len) in groups.iter().zip(&group_sizes) {
previous[offset..offset + len].copy_from_slice(dc.quant_dc.plane(c).as_slice());
offset += len;
}
let (mut base_error, mut candidate_error) = (0.0f64, 0.0f64);
let (mut base_bits, mut candidate_bits) = (0.0f64, 0.0f64);
let mut offset = 0;
for (group, (dc, &len)) in groups.iter_mut().zip(&group_sizes).enumerate() {
let (w, h) = (dc.quant_dc.xsize(), dc.quant_dc.ysize());
let previous = &previous[offset..offset + len];
let targets = &mut targets[..len];
offset += len;
let source = match c {
0 => dc.source_dc_x.as_ref().unwrap(),
1 => dc.source_dc_y.as_ref().unwrap(),
_ => dc.source_dc_b.as_ref().unwrap(),
};
for (((target, &source), &yq), &sy) in targets
.iter_mut()
.zip(source.as_slice())
.zip(dc.quant_dc.plane(1).as_slice())
.zip(dc.source_dc_y.as_ref().unwrap().as_slice())
{
*target = fmla(source, inv[c], -f32::from(yq) * factors[c]);
if c == 2 && compensate_b {
*target = compensation.adjust(*target, sy, yq);
}
}
base_error += targets
.iter()
.zip(previous)
.map(|(&t, &q)| f64::from(t - f32::from(q)).powi(2))
.sum::<f64>();
let to_i32 = |p: &[i16]| -> Vec<i32> { p.iter().map(|&v| i32::from(v)).collect() };
let y_plane = to_i32(dc.quant_dc.plane(1).as_slice());
let x_plane = to_i32(dc.quant_dc.plane(0).as_slice());
let ref_planes: Vec<RefPlane<'_>> = match c {
1 => Vec::new(),
0 => vec![RefPlane {
pixels: &y_plane,
width: w,
}],
_ => vec![
RefPlane {
pixels: &x_plane,
width: w,
},
RefPlane {
pixels: &y_plane,
width: w,
},
],
};
let stream = 1 + group as i32;
base_bits += learned_plane_bits(
previous,
w,
h,
mchan as i32,
stream,
&ref_planes,
rates,
&cost,
);
candidate_bits += refine_channel_learned(
dc.quant_dc.plane_mut(c).as_mut_slice(),
targets,
w,
h,
mchan as i32,
stream,
&ref_planes,
rates,
&cost,
lambda,
&mut rounding,
);
let levels = dc.quant_dc.plane(c).as_slice();
candidate_error += targets
.iter()
.zip(levels)
.map(|(&t, &q)| f64::from(t - f32::from(q)).powi(2))
.sum::<f64>();
}
let lambda = f64::from(lambda);
if candidate_bits + DC_RDO_MIN_GAIN_BITS >= base_bits
|| candidate_error + lambda * candidate_bits >= base_error + lambda * base_bits
{
let mut offset = 0;
for (dc, &len) in groups.iter_mut().zip(&group_sizes) {
dc.quant_dc
.plane_mut(c)
.as_mut_slice()
.copy_from_slice(&previous[offset..offset + len]);
offset += len;
}
} else {
changed = true;
}
}
Ok(changed)
}
#[allow(clippy::too_many_arguments)]
fn learned_plane_bits(
levels: &[i16],
w: usize,
h: usize,
chan: i32,
stream: i32,
refs: &[RefPlane<'_>],
rates: &LearnedDcRates<'_>,
cost: &LearnedCost,
) -> f64 {
let mut wp = WpState::new(w);
wp.reset(w, rates.wp_params);
let mut props = [0i32; crate::ma_tree::NUM_MA_PROPS];
let mut bits = 0.0f64;
for y in 0..h {
let above = |x: usize| i64::from(levels[(y - 1) * w + x]);
let above2 = |x: usize| i64::from(levels[(y - 2) * w + x]);
for x in 0..w {
let west = if x > 0 {
i64::from(levels[y * w + x - 1])
} else {
0
};
let west_west = if x > 1 {
i64::from(levels[y * w + x - 2])
} else {
0
};
let north = if y > 0 { above(x) } else { west };
let left = if x > 0 { west } else { north };
let ne = if x + 1 < w && y > 0 {
above(x + 1)
} else {
north
};
let nw = if x > 0 && y > 0 { above(x - 1) } else { left };
let nn = if y > 1 { above2(x) } else { north };
let wp_pred = wp.predict(x, y, north, left, ne, nw, nn);
let n = learned_props(
chan, stream, x, y, w, &above, &above2, west, west_west, wp.wp_prop, refs,
&mut props,
);
let (node, pred) = rates.tree.lookup(&props);
let prediction = crate::lossless::predictor_value(pred, n, wp_pred)
+ i64::from(rates.offsets[node as usize]);
let level = i64::from(levels[y * w + x]);
bits += f64::from(cost.bits(
rates.code,
rates.leaf_ctx[node as usize],
level - prediction,
));
wp.update(level, x, y);
}
}
bits
}
#[allow(clippy::too_many_arguments)]
fn refine_channel_learned(
levels: &mut [i16],
targets: &[f32],
w: usize,
h: usize,
chan: i32,
stream: i32,
refs: &[RefPlane<'_>],
rates: &LearnedDcRates<'_>,
cost: &LearnedCost,
lambda: f32,
scratch: &mut RoundingScratch,
) -> f64 {
const NATIVE_ROUND: bool =
!cfg!(any(target_arch = "x86", target_arch = "x86_64")) || cfg!(target_feature = "sse4.1");
if w == 0 || h == 0 {
return 0.0;
}
let RoundingScratch {
wp,
base,
older,
previous,
column,
history,
starts,
predictions,
} = scratch;
wp.reset(w, rates.wp_params);
let mut props = [0i32; crate::ma_tree::NUM_MA_PROPS];
let mut total_bits = 0.0f64;
for y in 0..h {
wp.save_row_base(y, base);
history.clear();
starts.clear();
older.clear();
previous.clear();
for x in 0..w {
let i = y * w + x;
let above = |x: usize| i64::from(levels[(y - 1) * w + x]);
let above2 = |x: usize| i64::from(levels[(y - 2) * w + x]);
let target = targets[i];
let current = i64::from(levels[i]);
let max_error = (target - current as f32).abs().max(DC_RDO_MAX_ERROR);
let forced = !target.is_finite() || max_error > 2.0 * f32::from(i16::MAX);
starts.push(history.len());
column.clear();
let push = |nodes: &mut Vec<TrellisNode>, level: i64| {
let level = level.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16;
if (forced && !nodes.is_empty())
|| (target - f32::from(level)).abs() > max_error
|| nodes.iter().any(|n| n.level == level)
{
return;
}
nodes.push(TrellisNode {
level,
cost: f32::INFINITY,
back: 0,
update: WpUpdate::default(),
});
};
push(column, current);
if target.is_finite() {
let [floor, ceil] = dc_rounding_bounds::<NATIVE_ROUND>(target);
for candidate in [floor, ceil, current - 1, current + 1] {
push(column, candidate);
}
}
predictions.clear();
for s in 0..previous.len().max(1) {
let (west, west_west, path_cost) = if x > 0 {
let node = previous[s];
let older_node = (x > 1).then(|| older[usize::from(node.back)]);
wp.stage_path(x, y, base, Some(node.update), older_node.map(|n| n.update));
(
i64::from(node.level),
older_node.map_or(0, |n| i64::from(n.level)),
node.cost,
)
} else {
(0, 0, 0.0)
};
let north = if y > 0 { above(x) } else { west };
let left = if x > 0 { west } else { north };
let ne = if x + 1 < w && y > 0 {
above(x + 1)
} else {
north
};
let nw = if x > 0 && y > 0 { above(x - 1) } else { left };
let nn = if y > 1 { above2(x) } else { north };
let wp_pred = wp.predict(x, y, north, left, ne, nw, nn);
let n = learned_props(
chan, stream, x, y, w, &above, &above2, west, west_west, wp.wp_prop, refs,
&mut props,
);
let (node, pred) = rates.tree.lookup(&props);
let prediction = crate::lossless::predictor_value(pred, n, wp_pred)
+ i64::from(rates.offsets[node as usize]);
if target.is_finite() {
push(column, prediction);
}
predictions.push((
prediction,
rates.leaf_ctx[node as usize] as usize,
wp.last_prediction(),
path_cost,
));
}
for node in column.iter_mut() {
let error = if forced {
0.0
} else {
target - f32::from(node.level)
};
let distortion = error * error;
let mut winning_prediction = None;
for (back, &(prediction, ctx, _, path_cost)) in predictions.iter().enumerate() {
let bits =
cost.bits(rates.code, ctx as u32, i64::from(node.level) - prediction);
let c = path_cost + distortion + lambda * bits;
if c < node.cost {
node.cost = c;
node.back = back as u16;
winning_prediction = Some(back);
}
}
if let Some(back) = winning_prediction {
node.update = WpState::update_for(predictions[back].2, i64::from(node.level));
}
history.push(TrellisStep {
level: node.level,
back: node.back,
});
}
std::mem::swap(older, previous);
std::mem::swap(previous, column);
}
let mut best = previous
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| a.cost.total_cmp(&b.cost))
.unwrap()
.0;
for x in (0..w).rev() {
let step = history[starts[x] + best];
levels[y * w + x] = step.level;
best = usize::from(step.back);
}
wp.restore_row_base(y, base);
for x in 0..w {
let above = |x: usize| i64::from(levels[(y - 1) * w + x]);
let above2 = |x: usize| i64::from(levels[(y - 2) * w + x]);
let west = if x > 0 {
i64::from(levels[y * w + x - 1])
} else {
0
};
let west_west = if x > 1 {
i64::from(levels[y * w + x - 2])
} else {
0
};
let north = if y > 0 { above(x) } else { west };
let left = if x > 0 { west } else { north };
let ne = if x + 1 < w && y > 0 {
above(x + 1)
} else {
north
};
let nw = if x > 0 && y > 0 { above(x - 1) } else { left };
let nn = if y > 1 { above2(x) } else { north };
let wp_pred = wp.predict(x, y, north, left, ne, nw, nn);
let n = learned_props(
chan, stream, x, y, w, &above, &above2, west, west_west, wp.wp_prop, refs,
&mut props,
);
let (node, pred) = rates.tree.lookup(&props);
let prediction = crate::lossless::predictor_value(pred, n, wp_pred)
+ i64::from(rates.offsets[node as usize]);
let level = i64::from(levels[y * w + x]);
total_bits += f64::from(cost.bits(
rates.code,
rates.leaf_ctx[node as usize],
level - prediction,
));
wp.update(level, x, y);
}
}
total_bits
}
#[cfg(test)]
mod tests {
use super::*;
fn prices() -> Prices {
let mut bits = vec![[5.0; ALPHABET_SIZE]; 3 * K_NUM_DC_CONTEXTS];
for row in &mut bits {
row[0] = 0.0;
}
Prices { bits }
}
#[test]
fn streamed_prices_match_full_token_streams() {
let mut groups = Vec::new();
for (w, h) in [(64, 64), (17, 9), (1, 5), (3, 2)] {
let mut dc = DcGroupData::new(w, h).unwrap();
for c in 0..3 {
for (i, level) in dc
.quant_dc
.plane_mut(c)
.as_mut_slice()
.iter_mut()
.enumerate()
{
*level = if c == 0 {
((i / w + i % w) % 31) as i16
} else {
((i * 7919 + c * 2971) % 1001) as i16 - 500
};
}
}
groups.push(dc);
}
let (wp, grad): (Vec<_>, Vec<_>) = groups
.iter()
.map(|dc| {
collect_dc_tokens_with_gradient(
dc,
&DC_PREDICTOR_WEIGHTED,
&mut Vec::new(),
false,
true,
WpParams::DEFAULT,
)
})
.unzip();
let mut scratch = DcPredictorScratch::default();
let expected = super::super::dc_tokens::choose_dc_predictors(&wp, &grad, &mut scratch);
let (prices, predictors, base_prices) = Prices::new(&groups, &mut scratch).unwrap();
assert_eq!(predictors, expected);
assert!(expected.iter().any(|&gradient| gradient));
assert!(expected.iter().any(|&gradient| !gradient));
let mut counts = vec![[0u32; ALPHABET_SIZE]; 3 * K_NUM_DC_CONTEXTS];
for ((dc, wp), grad) in groups.iter().zip(wp).zip(grad) {
let mut offset = 0;
for (mchan, c) in [1, 0, 2].into_iter().enumerate() {
let end = offset + dc.quant_dc.plane(c).as_slice().len();
for (w, g) in wp[offset..end].iter().zip(&grad[offset..end]) {
let token = if expected[w.context as usize] { g } else { w };
let (symbol, _, _) = uint_encode(token.value);
counts[mchan * K_NUM_DC_CONTEXTS + token.context as usize][symbol as usize] +=
1;
}
offset = end;
}
}
for (counts, bits) in counts.iter().zip(&prices.bits) {
let total =
counts.iter().map(|&n| f64::from(n)).sum::<f64>() + 0.5 * ALPHABET_SIZE as f64;
for (&count, &bits) in counts.iter().zip(bits) {
let expected = f_log2(total / (f64::from(count) + 0.5)) as f32;
assert_eq!(bits.to_bits(), expected.to_bits());
}
}
for (c, actual) in [0, 2].into_iter().zip(base_prices) {
let mut expected = DcPlanePrice::default();
for dc in &groups {
let (w, h) = (dc.quant_dc.xsize(), dc.quant_dc.ysize());
expected.add(dc.quant_dc.plane(c).as_slice(), w, h, w);
}
assert_eq!(actual.counts, expected.counts);
assert_eq!(actual.raw_bits, expected.raw_bits);
assert_eq!(actual.bits().to_bits(), expected.bits().to_bits());
}
let (empty_prices, empty_predictors, _) = Prices::new(&[], &mut scratch).unwrap();
assert_eq!(empty_predictors, DC_PREDICTOR_WEIGHTED);
assert!(empty_prices.bits.iter().flatten().all(|&b| b == 7.0));
}
#[test]
fn rounding_scratch_handles_width_changes() {
let mut scratch = RoundingScratch::new();
let prices = prices();
for (w, h) in [(17, 4), (1, 7), (64, 5), (2, 3), (0, 3), (3, 0), (17, 4)] {
let original: Vec<i16> = (0..w * h)
.map(|i| ((i * 19 + i / w * 7) % 101) as i16 - 50)
.collect();
let targets: Vec<f32> = original.iter().map(|&q| f32::from(q) - 0.49).collect();
let mut expected = original.clone();
let mut actual = original;
let mut expected_tokens = Vec::new();
let mut actual_tokens = Vec::new();
refine_channel(
&mut expected,
&targets,
w,
h,
1,
&DC_PREDICTOR_WEIGHTED,
&prices,
0.06,
|t| expected_tokens.push((t.context, t.value)),
);
refine_channel_with_scratch(
&mut actual,
&targets,
w,
h,
1,
&DC_PREDICTOR_WEIGHTED,
&prices,
0.06,
&mut scratch,
|t, _| actual_tokens.push((t.context, t.value)),
);
assert_eq!(actual, expected);
assert_eq!(actual_tokens, expected_tokens);
}
}
#[test]
fn replay_prices_match_a_separate_weighted_pass() {
let (w, h) = (17, 9);
let mut levels: Vec<i16> = (0..w * h)
.map(|i| ((i * 19 + i / w * 7) % 101) as i16 - 50)
.collect();
let targets: Vec<f32> = levels.iter().map(|&q| f32::from(q) - 0.49).collect();
let mut actual = DcPlanePrice::default();
refine_channel_with_scratch(
&mut levels,
&targets,
w,
h,
1,
&std::array::from_fn(|i| i % 2 == 0),
&prices(),
0.06,
&mut RoundingScratch::new(),
|_, weighted| actual.add_token(weighted),
);
let mut expected = DcPlanePrice::default();
expected.add(&levels, w, h, w);
assert_eq!(actual.counts, expected.counts);
assert_eq!(actual.raw_bits, expected.raw_bits);
assert_eq!(actual.bits().to_bits(), expected.bits().to_bits());
}
#[test]
fn rounding_matches_frozen_trellis_decisions() {
let mut prices = prices();
for (context, row) in prices.bits.iter_mut().enumerate() {
for (symbol, bits) in row.iter_mut().enumerate() {
*bits = ((context * 17 + symbol * 13) % 53) as f32 * 0.125;
}
}
let mut hash = 0xcbf2_9ce4_8422_2325u64;
let mut record = |bytes: &[u8]| {
for &byte in bytes {
hash = (hash ^ u64::from(byte)).wrapping_mul(0x100_0000_01b3);
}
};
let mut random = 0x85eb_ca6bu32;
for (w, h) in [
(0, 3),
(3, 0),
(1, 1),
(1, 7),
(2, 7),
(3, 4),
(17, 9),
(64, 5),
] {
for pattern in 0..5 {
let original: Vec<i16> = (0..w * h)
.map(|i| {
random = random.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
match pattern {
0 => 20,
1 => (i % 37) as i16 - 18,
2 => (random % 101) as i16 - 50,
_ => random as i16,
}
})
.collect();
let targets: Vec<f32> = original
.iter()
.enumerate()
.map(|(i, &q)| {
if pattern == 4 {
match i % 13 {
0 => return f32::NAN,
1 => return f32::INFINITY,
2 => return f32::NEG_INFINITY,
3 => return 1e20,
4 => return -1e20,
5 => return f32::from(i16::MIN) - 0.75,
6 => return f32::from(i16::MAX) + 0.75,
_ => {}
}
}
let offset = [-0.5, 0.5, -0.49, 0.49, -0.6, 0.6, 0.0, 0.25][i % 8];
f32::from(q) + if pattern == 3 { offset * 12.0 } else { offset }
})
.collect();
for predictors in [
DC_PREDICTOR_WEIGHTED,
[true; K_NUM_DC_CONTEXTS],
std::array::from_fn(|i| i % 2 == 0),
] {
for mchan in [1, 2] {
for lambda in [0.0, 0.04, 0.225] {
let mut levels = original.clone();
refine_channel(
&mut levels,
&targets,
w,
h,
mchan,
&predictors,
&prices,
lambda,
|token| {
record(&token.context.to_le_bytes());
record(&token.value.to_le_bytes());
},
);
for level in levels {
record(&level.to_le_bytes());
}
}
}
}
}
}
assert_eq!(
hash, 0x3978_cea8_1d8d_f792,
"trellis decisions: {hash:016x}"
);
}
#[test]
#[ignore = "isolated DC rounding timing; run in release with --nocapture"]
fn benchmark_dc_rounding() {
let prices = prices();
for (name, w, h, texture) in [
("smooth", 256, 256, false),
("texture", 256, 256, true),
("narrow", 17, 256, true),
] {
let original: Vec<i16> = (0..w * h)
.map(|i| {
if texture {
((i * 7919 + i / w * 2971) % 1001) as i16 - 500
} else {
((i / w + i % w) / 13) as i16
}
})
.collect();
let targets: Vec<f32> = original
.iter()
.enumerate()
.map(|(i, &q)| f32::from(q) + [-0.49, 0.45, -0.6, 0.2, 0.55][i % 5])
.collect();
let mut levels = original.clone();
let start = std::time::Instant::now();
for _ in 0..30 {
levels.copy_from_slice(&original);
refine_channel(
&mut levels,
&targets,
w,
h,
1,
&DC_PREDICTOR_WEIGHTED,
&prices,
0.06,
|_| {},
);
std::hint::black_box(&levels);
}
eprintln!(
"{name}: {:.3} ms/pass",
start.elapsed().as_secs_f64() * 1e3 / 30.0
);
}
}
#[test]
fn truncated_bounds_match_clamped_floor_and_ceil() {
let check = |target: f32| {
let clamp = |v: i64| v.clamp(i64::from(i16::MIN), i64::from(i16::MAX));
assert_eq!(
dc_bounds_from_truncation(target),
[clamp(target.floor() as i64), clamp(target.ceil() as i64)],
"target={target:?}, bits={:08x}",
target.to_bits(),
);
};
for integer in -32770..=32770 {
for fraction in [-0.75, -0.25, 0.0, 0.25, 0.75] {
check(integer as f32 + fraction);
}
}
for value in [
f32::MIN,
f32::MAX,
f32::NEG_INFINITY,
f32::INFINITY,
f32::NAN,
f32::from_bits(1),
-f32::from_bits(1),
-0.0,
i64::MIN as f32,
i64::MAX as f32,
] {
check(value);
}
let mut state = 0x85eb_ca6bu32;
for _ in 0..100_000 {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
check(f32::from_bits(state));
}
}
#[test]
fn rounding_favors_prediction_near_a_quantization_boundary() {
let mut levels = [20, 21, 20];
refine_channel(
&mut levels,
&[20.0, 20.51, 20.3],
3,
1,
0,
&DC_PREDICTOR_WEIGHTED,
&prices(),
0.02,
|_| {},
);
assert_eq!(levels, [20, 20, 20]);
}
#[test]
fn chosen_residuals_match_the_dc_tokenizer_at_every_edge() {
for w in [1, 2, 3, 17] {
for h in [1, 2, 4] {
for predictors in [DC_PREDICTOR_WEIGHTED, std::array::from_fn(|i| i % 2 == 0)] {
let mut levels: Vec<i16> = (0..w * h)
.map(|i| ((i * 19 + i / w * 7) % 101) as i16 - 50)
.collect();
let targets: Vec<f32> = levels
.iter()
.enumerate()
.map(|(i, &q)| f32::from(q) + [-0.49, 0.45, -0.6, 0.2, 0.55][i % 5])
.collect();
let mut tokens = Vec::new();
refine_channel(
&mut levels,
&targets,
w,
h,
0,
&predictors,
&prices(),
0.04,
|t| tokens.push(t),
);
let mut expected = Vec::new();
super::super::dc_tokens::walk_dc_channel(
&levels,
w,
h,
w,
0,
&predictors,
&mut Vec::new(),
false,
false,
WpParams::DEFAULT,
&mut expected,
&mut Vec::new(),
);
assert_eq!(tokens.len(), expected.len());
for (a, b) in tokens.iter().zip(&expected) {
assert_eq!((a.context, a.value), (b.context, b.value), "w={w}, h={h}");
}
}
}
}
}
#[test]
fn a_run_is_rounded_as_a_whole() {
let targets = [20.0, 20.6, 20.4, 20.4, 20.4, 20.4];
let original = [20i16, 21, 20, 20, 20, 20];
let row_cost = |levels: &[i16]| {
let mut tokens = Vec::new();
super::super::dc_tokens::walk_dc_channel(
levels,
levels.len(),
1,
levels.len(),
0,
&DC_PREDICTOR_WEIGHTED,
&mut Vec::new(),
false,
false,
WpParams::DEFAULT,
&mut tokens,
&mut Vec::new(),
);
let prices = prices();
tokens
.iter()
.zip(levels)
.zip(targets)
.map(|((t, &q), target)| {
let (symbol, extra, _) = uint_encode(t.value);
(target - f32::from(q)).powi(2)
+ 0.03 * (prices.bits[t.context as usize][symbol as usize] + extra as f32)
})
.sum::<f32>()
};
let mut levels = original;
refine_channel(
&mut levels,
&targets,
6,
1,
0,
&DC_PREDICTOR_WEIGHTED,
&prices(),
0.03,
|_| {},
);
assert_eq!(levels, [20; 6]);
assert!(row_cost(&levels) < row_cost(&original));
}
#[test]
fn rounding_handles_saturated_and_nonfinite_targets() {
let mut levels = [i16::MAX, i16::MIN, 42, -7];
refine_channel(
&mut levels,
&[1e20, -1e20, f32::NAN, f32::INFINITY],
2,
2,
0,
&DC_PREDICTOR_WEIGHTED,
&prices(),
0.04,
|_| {},
);
assert_eq!(levels, [i16::MAX, i16::MIN, 42, -7]);
}
#[test]
fn missing_sources_leave_every_group_unchanged() {
let mut groups = [DcGroupData::new(3, 2).unwrap()];
groups[0].quant_dc.plane_mut(1).as_mut_slice().fill(123);
refine_dc_rounding(
&mut groups,
&super::super::quantization::compute_distance_params(4.0),
0,
CflFrame::XYB,
false,
&mut DcPredictorScratch::default(),
)
.unwrap();
assert!(
groups[0]
.quant_dc
.plane(1)
.as_slice()
.iter()
.all(|&q| q == 123)
);
}
#[test]
fn constant_chroma_keeps_its_levels_without_a_rate_gain() {
use crate::image::Plane;
let params = super::super::quantization::compute_distance_params(4.0);
let mut dc = DcGroupData::new(16, 8).unwrap();
dc.quant_dc.plane_mut(0).as_mut_slice().fill(10);
dc.source_dc_x = Some(Plane::new_fill(
16,
8,
10.75 * params.dc_step[0] / (INV_DC_QUANT[0] * params.scale_dc),
));
dc.source_dc_y = Some(Plane::new(16, 8));
dc.source_dc_b = Some(Plane::new(16, 8));
let mut groups = [dc];
refine_dc_rounding(
&mut groups,
¶ms,
0,
CflFrame::XYB,
false,
&mut DcPredictorScratch::default(),
)
.unwrap();
assert!(
groups[0]
.quant_dc
.plane(0)
.as_slice()
.iter()
.all(|&q| q == 10)
);
assert!(
groups[0]
.quant_dc
.plane(1)
.as_slice()
.iter()
.all(|&q| q == 0)
);
}
fn x_step_fixture(params: &DistanceParams, x_of: impl Fn(usize, usize) -> f32) -> DcGroupData {
use crate::image::Plane;
let (w, h) = (32, 32);
let mut dc = DcGroupData::new(w, h).unwrap();
let unit = params.dc_step[0] / (INV_DC_QUANT[0] * params.scale_dc);
let mut source = Plane::new(w, h);
for y in 0..h {
for x in 0..w {
source.row_mut(y)[x] = x_of(x, y) * unit;
}
}
dc.source_dc_x = Some(source);
dc
}
#[test]
fn x_step_moves_a_level_under_a_flat_offset() {
let params = super::super::quantization::compute_distance_params(7.0);
let mut groups = [x_step_fixture(¶ms, |_, _| 3.4)];
let step = choose_x_dc_step(
&mut groups,
¶ms,
CflFrame::XYB,
crate::group::quantize_dc_cfl_scalar,
&crate::thread_pool::ThreadPool::new(2),
&mut Default::default(),
);
assert!(step[0] < params.dc_step[0], "kept the default step");
assert_eq!([step[1], step[2]], [params.dc_step[1], params.dc_step[2]]);
let m = step[0] / params.dc_step[0];
let level = groups[0].quant_dc.plane(0).as_slice()[0];
assert!(
groups[0]
.quant_dc
.plane(0)
.as_slice()
.iter()
.all(|&q| q == level)
);
assert!((f32::from(level) * m - 3.4).abs() < 0.4 * 0.5);
}
#[test]
fn x_step_keeps_the_default_for_unbiased_texture() {
let params = super::super::quantization::compute_distance_params(7.0);
let mut state = 0x9e37_79b9u32;
let mut noise = vec![0.0f32; 32 * 32];
for v in &mut noise {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
*v = (state >> 8) as f32 / (1u32 << 24) as f32 * 40.0 - 20.0;
}
let mut groups = [x_step_fixture(¶ms, |x, y| noise[y * 32 + x])];
let step = choose_x_dc_step(
&mut groups,
¶ms,
CflFrame::XYB,
crate::group::quantize_dc_cfl_scalar,
&crate::thread_pool::ThreadPool::new(2),
&mut Default::default(),
);
assert_eq!(step, params.dc_step);
assert!(
groups[0]
.quant_dc
.plane(0)
.as_slice()
.iter()
.all(|&q| q == 0)
);
}
#[test]
fn x_step_needs_every_source() {
let params = super::super::quantization::compute_distance_params(7.0);
let mut groups = [
x_step_fixture(¶ms, |_, _| 3.4),
DcGroupData::new(4, 4).unwrap(),
];
let step = choose_x_dc_step(
&mut groups,
¶ms,
CflFrame::XYB,
crate::group::quantize_dc_cfl_scalar,
&crate::thread_pool::ThreadPool::new(2),
&mut Default::default(),
);
assert_eq!(step, params.dc_step);
assert!(
groups[0]
.quant_dc
.plane(0)
.as_slice()
.iter()
.all(|&q| q == 0)
);
}
#[test]
fn lowpass_keeps_offsets_and_averages_out_alternation() {
let kernel = gaussian_kernel(X_DC_STEP_SIGMA);
assert!((kernel.iter().sum::<f32>() - 1.0).abs() < 1e-5);
let (w, h) = (24, 20);
let mut tmp = Vec::new();
let flat = vec![0.5f32; w * h];
let energy = lowpass_energy(&flat, w, h, &kernel, &mut tmp);
assert!((energy - 0.25 * (w * h) as f64).abs() < 1e-3);
let checker: Vec<f32> = (0..w * h)
.map(|i| if (i % w + i / w) % 2 == 0 { 0.5 } else { -0.5 })
.collect();
assert!(lowpass_energy(&checker, w, h, &kernel, &mut tmp) < 1e-3 * energy);
assert!((lowpass_energy(&[0.5; 7], 7, 1, &kernel, &mut tmp) - 1.75).abs() < 1e-4);
assert!((lowpass_energy(&[0.5; 7], 1, 7, &kernel, &mut tmp) - 1.75).abs() < 1e-4);
}
#[test]
fn lowpass_matches_reference_with_reused_scratch() {
fn reference(plane: &[f32], w: usize, h: usize, kernel: &[f32], tmp: &mut Vec<f32>) -> f64 {
let r = (kernel.len() / 2) as isize;
tmp.resize(w * h, 0.0);
for y in 0..h {
for x in 0..w {
let mut acc = 0.0;
for (j, &tap) in kernel.iter().enumerate() {
acc += tap * plane[y * w + mirror(x as isize + j as isize - r, w)];
}
tmp[y * w + x] = acc;
}
}
let mut energy = 0.0f64;
for y in 0..h {
for x in 0..w {
let mut acc = 0.0;
for (j, &tap) in kernel.iter().enumerate() {
acc += tap * tmp[mirror(y as isize + j as isize - r, h) * w + x];
}
energy += f64::from(acc).powi(2);
}
}
energy
}
let mut tmp = vec![f32::NAN; 512];
let mut expected_tmp = Vec::new();
let kernels = [
Vec::new(),
vec![0.7],
vec![0.3, -0.2],
vec![-0.1, 0.2, 0.7, -0.4],
gaussian_kernel(X_DC_STEP_SIGMA),
];
for (w, h) in (0..12).flat_map(|w| (0..12).map(move |h| (w, h))).chain([
(64, 32),
(32, 64),
(256, 3),
(3, 256),
]) {
let plane: Vec<f32> = (0..w * h)
.map(|i| ((i * 73 % 251) as f32 - 125.0) / 19.0)
.collect();
for kernel in &kernels {
let expected = reference(&plane, w, h, kernel, &mut expected_tmp);
let actual = lowpass_energy(&plane, w, h, kernel, &mut tmp);
assert_eq!(actual.to_bits(), expected.to_bits(), "{w}x{h} {kernel:?}");
assert_eq!(tmp, expected_tmp, "{w}x{h} {kernel:?}");
}
}
}
}