use crate::adaptive_quant::{dirty_log2f, fast_exp2};
use crate::coder_scratch::{
AcStrategyBandScratch, CachedQuantCost, CoderScratch, FineMergeRollback, MergeUpgrade,
MergeUpgradeCandidate, QuantRefinement, RerankDowngrade,
};
use crate::dc_group_data::{
AcStrategyImage, STRATEGY_AFV0, STRATEGY_AFV1, STRATEGY_AFV2, STRATEGY_AFV3, STRATEGY_DCT,
STRATEGY_DCT2X2, STRATEGY_DCT4X4, STRATEGY_DCT4X8, STRATEGY_DCT8X4, STRATEGY_DCT8X16,
STRATEGY_DCT8X32, STRATEGY_DCT16X8, STRATEGY_DCT16X16, STRATEGY_DCT16X32, STRATEGY_DCT32X8,
STRATEGY_DCT32X16, STRATEGY_DCT32X32, STRATEGY_DCT32X64, STRATEGY_DCT64X32, STRATEGY_DCT64X64,
STRATEGY_IDENTITY,
};
use crate::dct::{DctInput, fmla};
use crate::encoding_context::EncodingContext;
use crate::image::{Image3F, ImageB, ImageSB};
use crate::inflated_cost::{
Footprint, ReconCost, ReconDistInput, ReconQuantization, ReconScoring, ReconSource,
ReconTransform, channel_rd,
};
use crate::util::fast_exp;
mod matrix_overhead;
mod rate_prices;
mod selection;
mod selection_quantizer;
#[cfg(feature = "extraslow")]
pub(crate) use selection_quantizer::enabled as content_selection_enabled;
pub(crate) use matrix_overhead::account_matrix_headers;
pub(crate) use rate_prices::RatePrices;
use rate_prices::RatePricesScope;
pub(crate) use selection::{
Chosen32Cost, FineMosaicScratch, LayoutDpScratch, LeafChoice, Recon64Scratch, SavedChild,
aggregate_quant, fill_ac_strategy,
};
const DCT8_ONLY_MAX_DISTANCE: f32 = 0.056_713_393;
const FAST_RERANK_MAX_DISTANCE: f32 = 1.7090991223128462;
#[inline]
fn use_dct8_only(distance: f32) -> bool {
distance <= DCT8_ONLY_MAX_DISTANCE
}
#[inline]
fn use_dct8_only_for_content(ctx: &EncodingContext, distance: f32) -> bool {
use_dct8_only(distance) && !ctx.point_chroma()
}
const MERGE_BAND_D0: f32 = 0.5;
const MERGE_BAND_D1: f32 = 1.5;
#[derive(Clone, Copy)]
struct Banded {
hq: f32,
base: f32,
}
impl Banded {
const fn new(hq: f32, base: f32) -> Self {
Self { hq, base }
}
#[inline]
fn at(self, distance: f32) -> f32 {
let t = ((distance - MERGE_BAND_D0) / (MERGE_BAND_D1 - MERGE_BAND_D0)).clamp(0.0, 1.0);
fmla(t, self.base - self.hq, self.hq)
}
}
const META_R: f32 = 4.0;
pub(crate) const RD_LAMBDA: f32 = 0.080_867_17;
const MERGE_PRESCREEN_BITS: f32 = 600.0;
const RERANK_META_R: f32 = 2.6465739748323758;
const RERANK_LAMBDA_LO: f32 = 0.25;
const RERANK_LAMBDA_HI: f32 = 10.0;
const RERANK_LAMBDA_D0: f32 = 1.0;
const RERANK_LAMBDA_D1: f32 = 4.0;
const RERANK_LAMBDA_KNEE: f32 = 2.0;
const RERANK_LAMBDA_KNEE_POWER: f32 = 1.33;
const BIAS_RECT: Banded = Banded::new(1.026_200_7, 0.915);
const BIAS_16X16: Banded = Banded::new(1.205_927_5, 1.05);
const BIAS_32X32: Banded = Banded::new(1.205_927_5, 1.06);
const BIAS_32X32_LOOSEN: f32 = 0.94;
const BIAS_RECT32: Banded = Banded::new(1.326_520_2, 1.02);
const MERGE_MARGIN_PAIR: Banded = Banded::new(0.014_500_806, 0.0288);
const MERGE_MARGIN_16: Banded = Banded::new(0.192_438_88, 0.048);
const MERGE_MARGIN_32_RECT: Banded = Banded::new(0.161_916_72, 0.29);
const MERGE_MARGIN_32: Banded = Banded::new(0.213_985_08, 0.234);
const MERGE_MARGIN_LOWQ_FRACTION: f32 = 0.82;
const MERGE_MARGIN_FADE_START: f32 = 1.0;
const MERGE_MARGIN_FADE_END: f32 = 4.0;
const BIAS_4X4: f32 = 1.33;
const BIAS_4X8: f32 = 1.09;
const BIAS_AFV: Banded = Banded::new(1.15, 1.25);
const SUB8_MAX_DISTANCE: f32 = 2.0;
const AFV_MAX_DISTANCE: f32 = SUB8_MAX_DISTANCE;
const FINE_TRANSFORM_MAX_DISTANCE: f32 = 5.0;
const FINE_RECON_MARGIN: f32 = 0.98;
const FINE_ADMIT_RATE_CORRECTION_BITS: f32 = 24.0;
const FINE_ADMIT_RATE_CORRECTION_SHARE: f32 = 0.1;
const FINE_FAVOR_FULL_DISTANCE: f32 = 1.75;
#[inline]
fn fine_transform_bias(base: f32, distance: f32) -> f32 {
let favor = if distance < 5.0 {
let t = (5.0 - distance) * 0.2;
0.4 * t * t * (distance / FINE_FAVOR_FULL_DISTANCE).clamp(0.0, 1.0)
} else {
0.0
};
base / 0.8 - favor
}
#[inline]
fn merge_margin(distance: f32, margin: Banded) -> f32 {
let fade = ((distance - MERGE_MARGIN_FADE_START)
/ (MERGE_MARGIN_FADE_END - MERGE_MARGIN_FADE_START))
.clamp(0.0, 1.0);
margin.at(distance) * (1.0 - fade * (1.0 - MERGE_MARGIN_LOWQ_FRACTION))
}
#[inline]
fn merge_beats_dct8(candidate_cost: f32, dct8_cost: f32, accept: f32) -> bool {
candidate_cost < dct8_cost * accept
}
#[derive(Clone, Copy)]
pub(crate) struct MergeTuning {
pub(crate) bias_rect: f32,
pub(crate) bias_16x16: f32,
pub(crate) bias_32x32: f32,
pub(crate) bias_rect32: f32,
pub(crate) accept_pair: f32,
pub(crate) accept_16: f32,
pub(crate) accept_32_rect: f32,
pub(crate) accept_32: f32,
pub(crate) rerank_margin: f32,
pub(crate) risk_k: f32,
pub(crate) accept_64: f32,
pub(crate) accept_64_rect: f32,
pub(crate) site_k16: f32,
pub(crate) site_k32: f32,
}
const SITE_BIAS_K: f32 = 0.2;
const SITE_BIAS_V0: f32 = 0.003;
const SITE_BIAS_OCTAVES: f32 = 8.0;
fn site_variance(opsin: &Image3F, x0: usize, y0: usize, n: usize) -> f32 {
site_variance_rect(opsin, x0, y0, n, n)
}
fn site_variance_rect(opsin: &Image3F, x0: usize, y0: usize, w: usize, h: usize) -> f32 {
let (mut s1, mut s2, mut cnt) = (0f64, 0f64, 0usize);
for y in y0..(y0 + h).min(opsin.ysize()) {
let row = opsin.plane_row(1, y);
for &v in &row[x0..(x0 + w).min(opsin.xsize())] {
let v = f64::from(v);
s1 += v;
s2 += v * v;
cnt += 1;
}
}
if cnt == 0 {
return 0.0;
}
(s2 / cnt as f64 - (s1 / cnt as f64).powi(2)).max(0.0) as f32
}
const VLQ_D0: f32 = 4.0;
const VLQ_D1: f32 = 7.5;
const VLQ_MARGIN_PAIR: f32 = -0.002;
const VLQ_MARGIN_16: f32 = 0.067;
const VLQ_MARGIN_32_RECT: f32 = 0.363;
const VLQ_MARGIN_32: f32 = 0.029_25;
const VLQ_BIAS_16X16: f32 = 0.880;
const VLQ_BIAS_32X32: f32 = 0.867;
const VLQ_BIAS_RECT32: f32 = 1.180;
const VLQ_ACCEPT_64: f32 = 0.973;
const VLQ_ACCEPT_64_RECT: f32 = 0.736;
impl MergeTuning {
#[inline]
pub(crate) fn site_bias(&self, opsin: &Image3F, x0: usize, y0: usize, n: usize) -> f32 {
let k = if n == 16 {
self.site_k16
} else {
self.site_k32
};
if k == 0.0 {
return 1.0;
}
let var = site_variance(opsin, x0, y0, n).max(1e-9);
let s = (dirty_log2f(var / SITE_BIAS_V0) / SITE_BIAS_OCTAVES).clamp(-1.0, 1.0);
fast_exp(k * s)
}
pub(crate) fn new(distance: f32, arbiter: crate::Arbiter, speed: crate::Speed) -> Self {
let accept = |margin: Banded| 1.0 - merge_margin(distance, margin);
let site_k = if matches!(speed, crate::Speed::Fast | crate::Speed::Fastest) {
SITE_BIAS_K
} else {
0.0
};
let mut tuning = Self {
site_k16: site_k,
site_k32: site_k,
bias_rect: BIAS_RECT.at(distance),
bias_16x16: BIAS_16X16.at(distance),
bias_32x32: BIAS_32X32.at(distance),
bias_rect32: BIAS_RECT32.at(distance),
accept_pair: accept(MERGE_MARGIN_PAIR),
accept_16: accept(MERGE_MARGIN_16),
accept_32_rect: accept(MERGE_MARGIN_32_RECT),
accept_32: accept(MERGE_MARGIN_32),
rerank_margin: 1.0,
risk_k: if crate::adaptive_quant::aq_dampen(distance) > 0.0 {
MERGE_RISK_K
} else {
0.0
},
accept_64: dct64_accept(),
accept_64_rect: dct64_rect_accept(),
};
let vlq_t = ((distance - VLQ_D0) / (VLQ_D1 - VLQ_D0)).clamp(0.0, 1.0);
if vlq_t > 0.0 {
let lerp = |cur: f32, vlq: f32| fmla(vlq_t, vlq - cur, cur);
tuning.bias_rect = lerp(tuning.bias_rect, arbiter.tuning().vlq_bias_rect);
tuning.bias_16x16 = lerp(tuning.bias_16x16, VLQ_BIAS_16X16);
tuning.bias_32x32 = lerp(tuning.bias_32x32, VLQ_BIAS_32X32);
tuning.bias_rect32 = lerp(tuning.bias_rect32, VLQ_BIAS_RECT32);
tuning.accept_pair = lerp(tuning.accept_pair, 1.0 - VLQ_MARGIN_PAIR);
tuning.accept_16 = lerp(tuning.accept_16, 1.0 - VLQ_MARGIN_16);
tuning.accept_32_rect = lerp(tuning.accept_32_rect, 1.0 - VLQ_MARGIN_32_RECT);
tuning.accept_32 = lerp(tuning.accept_32, 1.0 - VLQ_MARGIN_32);
tuning.accept_64 = lerp(tuning.accept_64, VLQ_ACCEPT_64);
tuning.accept_64_rect = lerp(tuning.accept_64_rect, VLQ_ACCEPT_64_RECT);
}
if distance >= MERGE_BAND_D1 {
tuning.bias_32x32 *= BIAS_32X32_LOOSEN;
}
if !arbiter.tuning().rerank_downgrades {
tuning.rerank_margin = 0.0;
}
tuning
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) struct SearchScope {
rectangles: bool,
full_rerank: bool,
merge_32: bool,
busy_quads: bool,
}
impl SearchScope {
#[cfg(test)]
pub(crate) const SQUARES: Self = Self {
rectangles: false,
full_rerank: false,
merge_32: true,
busy_quads: true,
};
#[cfg(test)]
pub(crate) const FULL: Self = Self {
rectangles: true,
full_rerank: true,
merge_32: true,
busy_quads: true,
};
#[inline]
fn for_speed(speed: crate::Speed) -> Self {
let effort = speed.effort();
Self {
rectangles: effort.rectangles,
full_rerank: effort.full_rerank,
merge_32: effort.merge_32,
busy_quads: effort.merge_busy_quads,
}
}
#[inline]
fn rectangles(self) -> bool {
self.rectangles
}
#[inline]
fn merge_32(self) -> bool {
self.merge_32
}
#[inline]
fn merge_busy_quads(self) -> bool {
self.busy_quads
}
#[inline]
fn rerank(self, distance: f32) -> bool {
self.full_rerank || distance <= FAST_RERANK_MAX_DISTANCE
}
}
const MERGE_UPGRADE_MARGIN: f32 = 1.0;
const MERGE_UPGRADE_MIN_DISTANCE: f32 = 2.0;
const MERGE_UPGRADE_LOW_MARGIN: f32 = 1.05;
const MERGE_UPGRADE_LOW_DISTANCE: f32 = 4.0;
#[inline]
fn merge_upgrade_margin(distance: f32) -> f32 {
let t = ((distance - MERGE_UPGRADE_MIN_DISTANCE)
/ (MERGE_UPGRADE_LOW_DISTANCE - MERGE_UPGRADE_MIN_DISTANCE))
.clamp(0.0, 1.0);
fmla(
t,
MERGE_UPGRADE_LOW_MARGIN - MERGE_UPGRADE_MARGIN,
MERGE_UPGRADE_MARGIN,
)
}
const LEAF_FIRST_MAX_DISTANCE: f32 = 1.5;
const LEARNED_LEAF_FIRST_MAX_DISTANCE: f32 = FINE_TRANSFORM_MAX_DISTANCE;
#[derive(Clone, Copy)]
struct SuperBlockCost {
chosen: f32,
dct8: f32,
prescreened: bool,
}
#[inline]
pub(crate) fn gather_pixels(
plane: &crate::image::Plane<f32>,
px: usize,
py: usize,
width: usize,
height: usize,
dst: &mut [f32],
) {
let pw = plane.xsize();
let ph = plane.ysize();
if width <= pw.saturating_sub(px) && height <= ph.saturating_sub(py) {
for (v, dst) in dst.chunks_exact_mut(width).take(height).enumerate() {
dst.copy_from_slice(&plane.row(py + v)[px..px + width]);
}
return;
}
let safe_w = width.min(pw.saturating_sub(px));
let safe_h = height.min(ph.saturating_sub(py));
for v in 0..height {
let sy = if v < safe_h { py + v } else { ph - 1 };
let row = plane.row(sy);
let drow = &mut dst[v * width..v * width + width];
drow[..safe_w].copy_from_slice(&row[px..px + safe_w]);
if safe_w < width {
drow[safe_w..].fill(row[pw - 1]);
}
}
}
#[inline]
fn dct_input<'a, const W: usize, const H: usize>(
plane: &'a crate::image::Plane<f32>,
tmp: &'a mut [f32],
px: usize,
py: usize,
) -> DctInput<'a, W, H> {
if W <= plane.xsize().saturating_sub(px) && H <= plane.ysize().saturating_sub(py) {
let stride = plane.xsize();
DctInput::new(&plane.as_slice()[py * stride + px..], stride)
} else {
gather_pixels(plane, px, py, W, H, &mut tmp[..W * H]);
DctInput::new(&tmp[..W * H], W)
}
}
pub(crate) fn forward_transform(
ctx: &EncodingContext,
tmp: &mut [f32],
strategy: u8,
plane: &crate::image::Plane<f32>,
px: usize,
py: usize,
out: &mut [f32],
) -> (usize, usize) {
match strategy {
STRATEGY_DCT => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.dct8x8)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_IDENTITY => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.identity8x8)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_DCT2X2 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.dct2x2_8x8)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_DCT16X8 => {
let dst: &mut [f32; 128] = out.first_chunk_mut::<128>().unwrap();
(ctx.dct16x8)(dct_input(plane, tmp, px, py), dst);
(2, 1)
}
STRATEGY_DCT8X16 => {
let dst: &mut [f32; 128] = out.first_chunk_mut::<128>().unwrap();
(ctx.dct8x16)(dct_input(plane, tmp, px, py), dst);
(2, 1)
}
STRATEGY_DCT16X16 => {
let dst: &mut [f32; 256] = out.first_chunk_mut::<256>().unwrap();
(ctx.dct16x16)(dct_input(plane, tmp, px, py), dst);
(2, 2)
}
STRATEGY_DCT32X32 => {
let dst: &mut [f32; 1024] = out.first_chunk_mut::<1024>().unwrap();
(ctx.dct32x32)(dct_input(plane, tmp, px, py), dst);
(4, 4)
}
STRATEGY_DCT32X16 => {
let dst: &mut [f32; 512] = out.first_chunk_mut::<512>().unwrap();
(ctx.dct32x16)(dct_input(plane, tmp, px, py), dst);
(4, 2)
}
STRATEGY_DCT16X32 => {
let dst: &mut [f32; 512] = out.first_chunk_mut::<512>().unwrap();
(ctx.dct16x32)(dct_input(plane, tmp, px, py), dst);
(4, 2)
}
STRATEGY_DCT32X8 => {
let dst: &mut [f32; 256] = out.first_chunk_mut::<256>().unwrap();
(ctx.dct32x8)(dct_input(plane, tmp, px, py), dst);
(4, 1)
}
STRATEGY_DCT8X32 => {
let dst: &mut [f32; 256] = out.first_chunk_mut::<256>().unwrap();
(ctx.dct8x32)(dct_input(plane, tmp, px, py), dst);
(4, 1)
}
STRATEGY_DCT64X64 => {
let dst = out.first_chunk_mut::<4096>().unwrap();
(ctx.dct64x64)(dct_input(plane, tmp, px, py), dst);
(8, 8)
}
STRATEGY_DCT64X32 => {
let dst = out.first_chunk_mut::<2048>().unwrap();
(ctx.dct64x32)(dct_input(plane, tmp, px, py), dst);
(8, 4)
}
STRATEGY_DCT32X64 => {
let dst = out.first_chunk_mut::<2048>().unwrap();
(ctx.dct32x64)(dct_input(plane, tmp, px, py), dst);
(8, 4)
}
STRATEGY_DCT4X4 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.dct4x4)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_DCT4X8 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.dct4x8)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_DCT8X4 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.dct8x4)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_AFV0 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.afv0)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_AFV1 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.afv1)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_AFV2 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.afv2)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
STRATEGY_AFV3 => {
let dst: &mut [f32; 64] = out.first_chunk_mut::<64>().unwrap();
(ctx.afv3)(dct_input(plane, tmp, px, py), dst);
(1, 1)
}
_ => unreachable!("invalid strategy {strategy}"),
}
}
const DCT64_MIN_DISTANCE: f32 = 1.0;
const BIAS_64X64: f32 = 1.0;
const BIAS_64_RECT: f32 = 1.0;
const ACCEPT_64: f32 = 0.945;
const ACCEPT_64_RECT: f32 = 0.642;
const fn dct64_accept() -> f32 {
ACCEPT_64
}
const fn dct64_rect_accept() -> f32 {
ACCEPT_64_RECT
}
#[inline]
fn use_dct64(speed: crate::Speed, distance: f32) -> bool {
speed.effort().dct64 && distance >= DCT64_MIN_DISTANCE
}
#[allow(clippy::too_many_arguments)]
fn strategy_cost64(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
) -> f32 {
let CoderScratch {
strategy_coeffs: coeffs,
transform_gather: input,
recon,
rate_prices,
..
} = scratch;
let (cx, cy, size) =
prepare_strategy_coeffs(ctx, coeffs, input, strategy, opsin, px, py, cmap_factor);
if coefficient_rebase(rate_prices.as_deref(), strategy) {
rebase_cfl_coefficients(
ctx,
coeffs,
&mut recon[0],
strategy,
qac,
distance,
cx,
cy,
cmap_factor,
);
}
let visible = Footprint::of_strategy(opsin, px, py, strategy).visible_fraction();
if let Some((distortion, rate)) = rate_prices.as_deref().and_then(|prices| {
prices.coefficient_dist_and_rate(ctx, strategy, coeffs, qac, qm_mult_x, distance, cx, cy)
}) {
return distortion * visible + RD_LAMBDA * (rate + meta_r);
}
let mut distortion = 0.0f32;
let mut rate = 0.0f32;
for (c, coeff) in coeffs.iter().enumerate() {
let qm_mult = if c == 0 {
qm_mult_x
} else if c == 2 {
ctx.b_qm_mul()
} else {
1.0
};
let matrix: &[f32] = match strategy {
STRATEGY_DCT64X64 => &ctx.matrices().inv_matrix_64x64(c)[..],
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => &ctx.matrices().inv_matrix_64x32(c)[..],
_ => unreachable!(),
};
let (d, r) = channel_rd(
ctx.sse_and_rate,
ctx.rate_log2_lut,
&coeff[..size],
matrix,
c,
ctx.dequant_bias(c),
qac,
qm_mult,
distance,
cx,
cy,
);
distortion += ctx.channel_weight(c) * d;
rate += r;
}
distortion * visible + RD_LAMBDA * (rate + meta_r)
}
fn strategy_cost(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
) -> f32 {
strategy_cost_impl(
ctx,
scratch,
strategy,
opsin,
px,
py,
qac,
qm_mult_x,
meta_r,
distance,
cmap_factor,
DistortionModel::Coefficient,
)
}
#[derive(Clone, Copy)]
struct ReconStrategyCost {
cost: f32,
base: f32,
distortion: f32,
rate: f32,
hue_distortion: f32,
}
#[allow(clippy::too_many_arguments)]
fn reconstruction_strategy_cost_and_base(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
spatial_errors: Option<[&mut [f32]; 3]>,
) -> ReconStrategyCost {
reconstruction_strategy_cost_bounded(
ctx,
scratch,
strategy,
opsin,
px,
py,
qac,
qm_mult_x,
meta_r,
distance,
cmap_factor,
spatial_errors,
f32::INFINITY,
)
}
#[allow(clippy::too_many_arguments)]
fn reconstruction_strategy_cost_bounded(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
spatial_errors: Option<[&mut [f32]; 3]>,
limit: f32,
) -> ReconStrategyCost {
let mut qac = qac;
let original_qac = qac;
let CoderScratch {
strategy_coeffs: coeffs,
transform_gather,
recon,
rate_prices,
..
} = scratch;
let prices = rate_prices.as_deref();
let keep_spatial_errors = spatial_errors.is_some();
let infinite = |distortion: f32, rate: f32, bound: f32| ReconStrategyCost {
cost: f32::INFINITY,
base: bound,
distortion,
rate,
hue_distortion: 0.0,
};
let (cx, cy, cost) = if !limit.is_finite() {
let (cx, cy, _) = prepare_strategy_coeffs(
ctx,
coeffs,
transform_gather,
strategy,
opsin,
px,
py,
cmap_factor,
);
qac *= selection_quantizer::scale(ctx, strategy, &coeffs[1], qac, distance, meta_r);
let cost = reconstruction_dist_and_rate_with_class(
ctx,
recon,
coeffs,
strategy,
opsin,
px,
py,
qac,
original_qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
keep_spatial_errors,
prices,
);
(cx, cy, cost)
} else {
let table = prices.filter(|prices| prices.has_table(strategy));
let lambda = reconstruction_lambda(distance);
let coeffs: &mut [[f32; 4096]; 3] = coeffs;
let transform_gather: &mut [f32] = &mut transform_gather[..];
let (cx, cy) = forward_transform(
ctx,
transform_gather,
strategy,
opsin.plane(1),
px,
py,
&mut coeffs[1],
);
qac *= selection_quantizer::scale(ctx, strategy, &coeffs[1], qac, distance, meta_r);
let size = cx * cy * 64;
let y_bits = table.map(|prices| {
learned_channel_bits(
ctx,
prices,
strategy,
&coeffs[1],
1,
qac,
original_qac,
qm_mult_x,
distance,
cx,
cy,
)
});
let luma = {
let input = recon_input(
ctx,
coeffs,
strategy,
opsin,
px,
py,
qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
keep_spatial_errors,
table.is_none(),
);
(ctx.recon_luma_phase)(recon, &input, &ctx.recon_error_kernels)
};
let luma_distortion = luma.distortion;
let y_rate = match (table, y_bits) {
(Some(prices), Some(bits)) => prices.scaled_rate(strategy, distance, bits),
_ => luma.rate,
};
let bound = fmla(lambda, y_rate + meta_r, luma_distortion);
if bound > limit {
return infinite(luma_distortion, y_rate, bound);
}
for c in [0, 2] {
forward_transform(
ctx,
transform_gather,
strategy,
opsin.plane(c),
px,
py,
&mut coeffs[c],
);
}
{
let [x, y, b] = coeffs;
apply_cfl(ctx, CflXyb { x, y, b }, size, cmap_factor);
}
let bits = table.map(|prices| {
let mut bits = 0f32;
bits += chroma_bits(
ctx,
prices,
strategy,
coeffs,
recon,
0,
qac,
original_qac,
qm_mult_x,
distance,
cx,
cy,
cmap_factor,
);
bits += y_bits.expect("priced luma");
bits += chroma_bits(
ctx,
prices,
strategy,
coeffs,
recon,
2,
qac,
original_qac,
qm_mult_x,
distance,
cx,
cy,
cmap_factor,
);
bits
});
let rate = match (table, bits) {
(Some(prices), Some(bits)) => Some(prices.scaled_rate(strategy, distance, bits)),
_ => None,
};
if let Some(rate) = rate {
let bound = fmla(lambda, rate + meta_r, luma_distortion);
if bound > limit {
return infinite(luma_distortion, rate, bound);
}
}
let input = recon_input(
ctx,
coeffs,
strategy,
opsin,
px,
py,
qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
keep_spatial_errors,
table.is_none(),
);
let mut cost = (ctx.recon_chroma_phase)(recon, &input, &ctx.recon_error_kernels, luma);
if let Some(rate) = rate {
cost.rate = rate;
}
(cx, cy, cost)
};
let ReconCost {
distortion,
rate,
hue_distortion,
} = cost;
if let Some(output) = spatial_errors {
let n = cx * cy * 64;
for (c, plane) in output.into_iter().enumerate() {
plane.copy_from_slice(&recon[c][..n]);
}
}
ReconStrategyCost {
cost: rd_cost(
DistortionModel::Reconstruction,
distance,
meta_r,
distortion,
rate,
),
base: if qac == original_qac {
rd_cost(
DistortionModel::Reconstruction,
distance,
0.0,
distortion,
rate,
)
} else {
f32::NAN
},
distortion,
rate,
hue_distortion,
}
}
#[derive(Clone, Copy)]
enum DistortionModel {
Coefficient,
Reconstruction,
}
struct CflXyb<'a> {
x: &'a mut [f32],
y: &'a mut [f32],
b: &'a mut [f32],
}
pub(crate) type ApplyCflFn = fn(&mut [f32], &[f32], &mut [f32], [f32; 3]);
#[cfg(not(any(
all(target_arch = "aarch64", feature = "neon"),
all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm")
)))]
fn apply_cfl_scalar(x: &mut [f32], y: &[f32], b: &mut [f32], cmap_factor: [f32; 3]) {
assert_eq!(x.len(), y.len());
assert_eq!(x.len(), b.len());
let neg_cx = -cmap_factor[0];
let neg_cb = -cmap_factor[2];
for ((x, b), &y) in x.iter_mut().zip(b).zip(y) {
*x = fmla(neg_cx, y, *x);
*b = fmla(neg_cb, y, *b);
}
}
pub(crate) fn selected_apply_cfl_fn() -> ApplyCflFn {
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
{
|x, y, b, cmap_factor| unsafe { crate::neon::apply_cfl_neon(x, y, b, cmap_factor) }
}
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("fma") {
return |x, y, b, cmap_factor| unsafe { crate::avx::apply_cfl_avx2(x, y, b, cmap_factor) };
}
#[cfg(all(target_arch = "wasm32", feature = "wasm", target_feature = "simd128"))]
return |x, y, b, cmap_factor| crate::wasm::apply_cfl_wasm(x, y, b, cmap_factor);
#[cfg(not(any(
all(target_arch = "aarch64", feature = "neon"),
all(target_arch = "wasm32", target_feature = "simd128", feature = "wasm")
)))]
apply_cfl_scalar
}
fn apply_cfl(ctx: &EncodingContext, coeffs: CflXyb<'_>, size: usize, cmap_factor: [f32; 3]) {
let CflXyb { x, y, b } = coeffs;
assert!(size <= x.len() && size <= y.len() && size <= b.len());
(ctx.apply_cfl)(&mut x[..size], &y[..size], &mut b[..size], cmap_factor);
}
#[allow(clippy::too_many_arguments)]
fn luma_coefficient_error(
ctx: &EncodingContext,
strategy: u8,
y: &[f32],
qac: f32,
distance: f32,
cx: usize,
cy: usize,
error: &mut [f32],
) {
let width = cx * 8;
unsafe {
crate::inflated_cost::recon_quantize_error_fn()(
y,
inverse_matrix_for(ctx, strategy, 1),
matrix_for(ctx, strategy, 1),
qac,
ctx.dequant_bias(1),
&crate::group::quantize_ac_thresholds(1, cx, cy, distance),
width,
cy * 8,
width / 2,
cx,
cy,
error,
ctx.rate_log2_lut,
&[],
)
};
}
fn rebase_cfl(
ctx: &EncodingContext,
x: &mut [f32],
luma_error: &[f32],
b: &mut [f32],
cmap_factor: [f32; 3],
) {
(ctx.apply_cfl)(x, luma_error, b, [-cmap_factor[0], 0.0, -cmap_factor[2]]);
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn rebase_cfl_coefficients(
ctx: &EncodingContext,
coeffs: &mut [[f32; 4096]; 3],
error: &mut [f32],
strategy: u8,
qac: f32,
distance: f32,
cx: usize,
cy: usize,
cmap_factor: [f32; 3],
) {
let size = cx * cy * 64;
luma_coefficient_error(
ctx,
strategy,
&coeffs[1][..size],
qac,
distance,
cx,
cy,
&mut error[..size],
);
let [x, _, b] = coeffs;
rebase_cfl(
ctx,
&mut x[..size],
&error[..size],
&mut b[..size],
cmap_factor,
);
}
fn coefficient_rebase(prices: Option<&RatePrices>, strategy: u8) -> bool {
prices.is_some_and(|prices| prices.has_table(strategy))
}
#[inline]
fn inverse_matrix_for(ctx: &EncodingContext, strategy: u8, channel: usize) -> &[f32] {
let matrices = ctx.matrices();
match strategy {
STRATEGY_DCT => &matrices.inv_matrix(channel)[..],
STRATEGY_IDENTITY => &matrices.inv_matrix_identity(channel)[..],
STRATEGY_DCT2X2 => &matrices.inv_matrix_dct2x2(channel)[..],
STRATEGY_DCT4X4 => &matrices.inv_matrix_4x4(channel)[..],
STRATEGY_DCT4X8 | STRATEGY_DCT8X4 => &matrices.inv_matrix_4x8(channel)[..],
STRATEGY_AFV0..=STRATEGY_AFV3 => &matrices.inv_matrix_afv(channel)[..],
STRATEGY_DCT16X16 => &matrices.inv_matrix_16x16(channel)[..],
STRATEGY_DCT32X32 => &matrices.inv_matrix_32x32(channel)[..],
STRATEGY_DCT32X16 | STRATEGY_DCT16X32 => &matrices.inv_matrix_32x16(channel)[..],
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => &matrices.inv_matrix_8x32(channel)[..],
STRATEGY_DCT64X64 => &matrices.inv_matrix_64x64(channel)[..],
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => &matrices.inv_matrix_64x32(channel)[..],
_ => &matrices.inv_matrix_16x8(channel)[..],
}
}
#[inline]
fn matrix_for(ctx: &EncodingContext, strategy: u8, channel: usize) -> &[f32] {
let matrices = ctx.matrices();
match strategy {
STRATEGY_DCT => &matrices.matrix(channel)[..],
STRATEGY_IDENTITY => &matrices.matrix_identity(channel)[..],
STRATEGY_DCT2X2 => &matrices.matrix_dct2x2(channel)[..],
STRATEGY_DCT4X4 => &matrices.matrix_4x4(channel)[..],
STRATEGY_DCT4X8 | STRATEGY_DCT8X4 => &matrices.matrix_4x8(channel)[..],
STRATEGY_AFV0..=STRATEGY_AFV3 => &matrices.matrix_afv(channel)[..],
STRATEGY_DCT16X16 => &matrices.matrix_16x16(channel)[..],
STRATEGY_DCT32X32 => &matrices.matrix_32x32(channel)[..],
STRATEGY_DCT32X16 | STRATEGY_DCT16X32 => &matrices.matrix_32x16(channel)[..],
STRATEGY_DCT32X8 | STRATEGY_DCT8X32 => &matrices.matrix_8x32(channel)[..],
STRATEGY_DCT64X64 => &matrices.matrix_64x64(channel)[..],
STRATEGY_DCT64X32 | STRATEGY_DCT32X64 => &matrices.matrix_64x32(channel)[..],
_ => &matrices.matrix_16x8(channel)[..],
}
}
#[allow(clippy::too_many_arguments)]
#[inline]
fn coefficient_dist_and_rate(
ctx: &EncodingContext,
strategy: u8,
coeffs: &[[f32; 4096]; 3],
size: usize,
qac: f32,
class_qac: f32,
qm_mult_x: f32,
distance: f32,
cx: usize,
cy: usize,
prices: Option<&RatePrices>,
visible: f32,
) -> (f32, f32) {
if let Some((d, r)) = prices.and_then(|prices| {
prices.coefficient_dist_and_rate_with_class(
ctx, strategy, coeffs, qac, class_qac, qm_mult_x, distance, cx, cy,
)
}) {
return (d * visible, r);
}
let mut d_total = 0.0f32;
let mut r_total = 0.0f32;
for c in 0..3 {
let qm_mult = if c == 0 {
qm_mult_x
} else if c == 2 {
ctx.b_qm_mul()
} else {
1.0
};
let (d, r) = channel_rd(
ctx.sse_and_rate,
ctx.rate_log2_lut,
&coeffs[c][..size],
inverse_matrix_for(ctx, strategy, c),
c,
ctx.dequant_bias(c),
qac,
qm_mult,
distance,
cx,
cy,
);
d_total += ctx.channel_weight(c) * d;
r_total += r;
}
(d_total * visible, r_total)
}
#[allow(clippy::too_many_arguments)]
fn strategy_cost_impl(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
distortion_model: DistortionModel,
) -> f32 {
let CoderScratch {
strategy_coeffs: coeffs,
transform_gather,
recon,
rate_prices,
..
} = scratch;
let prices = rate_prices.as_deref();
let (cx, cy, size) = prepare_strategy_coeffs(
ctx,
coeffs,
transform_gather,
strategy,
opsin,
px,
py,
cmap_factor,
);
let class_qac = qac;
let qac = qac * selection_quantizer::scale(ctx, strategy, &coeffs[1], qac, distance, meta_r);
let (d_total, r_total) = match distortion_model {
DistortionModel::Reconstruction => reconstruction_dist_and_rate_with_class(
ctx,
recon,
coeffs,
strategy,
opsin,
px,
py,
qac,
class_qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
false,
prices,
)
.dist_and_rate(),
DistortionModel::Coefficient => {
if coefficient_rebase(prices, strategy) {
rebase_cfl_coefficients(
ctx,
coeffs,
&mut recon[0],
strategy,
qac,
distance,
cx,
cy,
cmap_factor,
);
}
coefficient_dist_and_rate(
ctx,
strategy,
coeffs,
size,
qac,
class_qac,
qm_mult_x,
distance,
cx,
cy,
prices,
Footprint::of_strategy(opsin, px, py, strategy).visible_fraction(),
)
}
};
rd_cost(distortion_model, distance, meta_r, d_total, r_total)
}
#[allow(clippy::too_many_arguments)]
fn prepare_strategy_coeffs(
ctx: &EncodingContext,
coeffs: &mut [[f32; 4096]; 3],
transform_gather: &mut [f32; 4096],
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
cmap_factor: [f32; 3],
) -> (usize, usize, usize) {
let mut cxy = (1usize, 1usize);
for c in 0..3 {
cxy = forward_transform(
ctx,
transform_gather,
strategy,
opsin.plane(c),
px,
py,
&mut coeffs[c],
);
}
let (cx, cy) = cxy;
let size = cx * cy * 64;
let [x, y, b] = coeffs;
apply_cfl(ctx, CflXyb { x, y, b }, size, cmap_factor);
(cx, cy, size)
}
#[allow(clippy::too_many_arguments)]
fn recon_input<'a>(
ctx: &'a EncodingContext,
coeffs: &'a [[f32; 4096]; 3],
strategy: u8,
opsin: &'a Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
distance: f32,
cmap_factor: [f32; 3],
cx: usize,
cy: usize,
keep_spatial_errors: bool,
compute_rate: bool,
) -> ReconDistInput<'a> {
ReconDistInput {
idct: ctx.idct,
quantization: ReconQuantization {
compute_rate,
rate_log2_lut: ctx.rate_log2_lut,
coeffs: [&coeffs[0], &coeffs[1], &coeffs[2]],
inverse_matrices: [
inverse_matrix_for(ctx, strategy, 0),
inverse_matrix_for(ctx, strategy, 1),
inverse_matrix_for(ctx, strategy, 2),
],
matrices: [
matrix_for(ctx, strategy, 0),
matrix_for(ctx, strategy, 1),
matrix_for(ctx, strategy, 2),
],
qac,
qm_mult_x,
qm_mult_b: ctx.b_qm_mul(),
distance,
dequant_bias: std::array::from_fn(|c| ctx.dequant_bias(c)),
},
transform: ReconTransform {
blocks_x: cx,
blocks_y: cy,
strategy,
},
source: ReconSource {
opsin,
x: px,
y: py,
},
scoring: ReconScoring {
factor_x: cmap_factor[0],
factor_b: cmap_factor[2],
channel_weights: ctx.channel_weights(),
xyb_matrix: ctx.xyb,
rgb_hue_alpha: rerank_rgb_hue_alpha(ctx, distance),
keep_spatial_errors,
},
}
}
#[allow(clippy::too_many_arguments)]
fn learned_channel_bits(
ctx: &EncodingContext,
prices: &RatePrices,
strategy: u8,
coeffs: &[f32],
c: usize,
qac: f32,
class_qac: f32,
qm_mult_x: f32,
distance: f32,
cx: usize,
cy: usize,
) -> f32 {
prices
.channel_rate_bits_with_class(
ctx, strategy, coeffs, c, qac, class_qac, qm_mult_x, distance, cx, cy,
)
.expect("priced table")
}
#[allow(clippy::too_many_arguments)]
fn chroma_bits(
ctx: &EncodingContext,
prices: &RatePrices,
strategy: u8,
coeffs: &[[f32; 4096]; 3],
recon: &mut [[f32; 4096]; 8],
c: usize,
qac: f32,
class_qac: f32,
qm_mult_x: f32,
distance: f32,
cx: usize,
cy: usize,
cmap_factor: [f32; 3],
) -> f32 {
let size = cx * cy * 64;
let [_, luma_error, _, _, _, rebased, ..] = recon;
(ctx.recon_error_kernels.combine)(
&coeffs[c][..size],
&luma_error[..size],
cmap_factor[c],
&mut rebased[..size],
);
learned_channel_bits(
ctx,
prices,
strategy,
&rebased[..size],
c,
qac,
class_qac,
qm_mult_x,
distance,
cx,
cy,
)
}
#[allow(clippy::too_many_arguments)]
fn reconstruction_dist_and_rate(
ctx: &EncodingContext,
recon: &mut [[f32; 4096]; 8],
coeffs: &[[f32; 4096]; 3],
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
distance: f32,
cmap_factor: [f32; 3],
cx: usize,
cy: usize,
keep_spatial_errors: bool,
prices: Option<&RatePrices>,
) -> ReconCost {
reconstruction_dist_and_rate_with_class(
ctx,
recon,
coeffs,
strategy,
opsin,
px,
py,
qac,
qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
keep_spatial_errors,
prices,
)
}
#[allow(clippy::too_many_arguments)]
fn reconstruction_dist_and_rate_with_class(
ctx: &EncodingContext,
recon: &mut [[f32; 4096]; 8],
coeffs: &[[f32; 4096]; 3],
strategy: u8,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
class_qac: f32,
qm_mult_x: f32,
distance: f32,
cmap_factor: [f32; 3],
cx: usize,
cy: usize,
keep_spatial_errors: bool,
prices: Option<&RatePrices>,
) -> ReconCost {
let table = prices.filter(|prices| prices.has_table(strategy));
let input = recon_input(
ctx,
coeffs,
strategy,
opsin,
px,
py,
qac,
qm_mult_x,
distance,
cmap_factor,
cx,
cy,
keep_spatial_errors,
table.is_none(),
);
let Some(prices) = table else {
return (ctx.recon_dist_and_rate)(recon, &input, &ctx.recon_error_kernels);
};
let luma = (ctx.recon_luma_phase)(recon, &input, &ctx.recon_error_kernels);
let mut bits = 0f32;
bits += chroma_bits(
ctx,
prices,
strategy,
coeffs,
recon,
0,
qac,
class_qac,
qm_mult_x,
distance,
cx,
cy,
cmap_factor,
);
bits += learned_channel_bits(
ctx, prices, strategy, &coeffs[1], 1, qac, class_qac, qm_mult_x, distance, cx, cy,
);
bits += chroma_bits(
ctx,
prices,
strategy,
coeffs,
recon,
2,
qac,
class_qac,
qm_mult_x,
distance,
cx,
cy,
cmap_factor,
);
let mut cost = (ctx.recon_chroma_phase)(recon, &input, &ctx.recon_error_kernels, luma);
cost.rate = prices.scaled_rate(strategy, distance, bits);
cost
}
const RERANK_RGB_HUE_ALPHA: f32 = 800.0;
fn rerank_rgb_hue_alpha(ctx: &EncodingContext, distance: f32) -> f32 {
if !ctx.x_heavy() {
return 0.0;
}
let fade_in = ((distance - 0.35) / 0.40).clamp(0.0, 1.0);
RERANK_RGB_HUE_ALPHA * fade_in
}
#[inline]
fn reconstruction_lambda(distance: f32) -> f32 {
const RECIP_RERANKING: f32 = 1. / (RERANK_LAMBDA_D1 - RERANK_LAMBDA_D0);
let ramp = ((distance - RERANK_LAMBDA_D0) * RECIP_RERANKING).clamp(0.0, 1.0);
let mut multiplier = fmla(ramp, RERANK_LAMBDA_HI - RERANK_LAMBDA_LO, RERANK_LAMBDA_LO);
if distance > RERANK_LAMBDA_D0 && distance < RERANK_LAMBDA_KNEE {
const KNEE_RAMP: f32 = (RERANK_LAMBDA_KNEE - RERANK_LAMBDA_D0) * RECIP_RERANKING;
let t = (distance - RERANK_LAMBDA_D0) / (RERANK_LAMBDA_KNEE - RERANK_LAMBDA_D0);
multiplier = fmla(
t.powf(RERANK_LAMBDA_KNEE_POWER),
KNEE_RAMP * (RERANK_LAMBDA_HI - RERANK_LAMBDA_LO),
RERANK_LAMBDA_LO,
);
}
RD_LAMBDA * multiplier
}
#[inline]
pub(crate) fn fine_mosaic_lambda(distance: f32) -> f32 {
reconstruction_lambda(distance).max(RD_LAMBDA)
}
#[inline]
fn rd_cost(
distortion_model: DistortionModel,
distance: f32,
meta_r: f32,
d_total: f32,
r_total: f32,
) -> f32 {
let lam = match distortion_model {
DistortionModel::Coefficient => RD_LAMBDA,
DistortionModel::Reconstruction => reconstruction_lambda(distance),
};
fmla(lam, r_total + meta_r, d_total)
}
#[derive(Clone, Copy)]
struct Sub8Costs {
dct8: f32,
identity: f32,
dct2x2: f32,
dct4x4: f32,
dct4x8: f32,
dct8x4: f32,
afv: [f32; 4],
}
#[inline]
fn forward_sub8_transform(
ctx: &EncodingContext,
strategy: u8,
input: &[f32; 64],
output: &mut [f32],
) {
let dst: &mut [f32; 64] = output.first_chunk_mut::<64>().unwrap();
let input = DctInput::from_flat(input);
match strategy {
STRATEGY_DCT => (ctx.dct8x8)(input, dst),
STRATEGY_IDENTITY => (ctx.identity8x8)(input, dst),
STRATEGY_DCT2X2 => (ctx.dct2x2_8x8)(input, dst),
STRATEGY_DCT4X4 => (ctx.dct4x4)(input, dst),
STRATEGY_DCT4X8 => (ctx.dct4x8)(input, dst),
STRATEGY_DCT8X4 => (ctx.dct8x4)(input, dst),
STRATEGY_AFV0 => (ctx.afv0)(input, dst),
STRATEGY_AFV1 => (ctx.afv1)(input, dst),
STRATEGY_AFV2 => (ctx.afv2)(input, dst),
STRATEGY_AFV3 => (ctx.afv3)(input, dst),
_ => unreachable!("non-sub8 strategy {strategy}"),
}
}
#[allow(clippy::too_many_arguments)]
fn sub8_strategy_costs(
ctx: &EncodingContext,
scratch: &mut CoderScratch,
opsin: &Image3F,
px: usize,
py: usize,
qac: f32,
qm_mult_x: f32,
meta_r: f32,
distance: f32,
cmap_factor: [f32; 3],
cached_dct8: Option<f32>,
with_dct4: bool,
with_fine: bool,
) -> Sub8Costs {
let mut pixels = [[0.0f32; 64]; 3];
for (c, input) in pixels.iter_mut().enumerate() {
gather_pixels(opsin.plane(c), px, py, 8, 8, input);
}
let visible = Footprint::new(opsin.xsize(), opsin.ysize(), px, py, 8, 8).visible_fraction();
let prices = scratch.rate_prices.as_deref();
let coeffs = &mut scratch.strategy_coeffs;
let mut evaluate = |strategy| {
for c in 0..3 {
forward_sub8_transform(ctx, strategy, &pixels[c], &mut coeffs[c]);
}
{
let [x, y, b] = &mut ***coeffs;
apply_cfl(ctx, CflXyb { x, y, b }, 64, cmap_factor);
}
if coefficient_rebase(prices, strategy) {
let mut error = [0.0f32; 64];
rebase_cfl_coefficients(
ctx,
coeffs,
&mut error,
strategy,
qac,
distance,
1,
1,
cmap_factor,
);
}
let (distortion, rate) = coefficient_dist_and_rate(
ctx, strategy, coeffs, 64, qac, qac, qm_mult_x, distance, 1, 1, prices, visible,
);
fmla(RD_LAMBDA, rate + meta_r, distortion)
};
let dct8 = if let Some(cost) = cached_dct8 {
cost
} else {
evaluate(STRATEGY_DCT)
};
let identity = if with_fine {
evaluate(STRATEGY_IDENTITY)
} else {
f32::INFINITY
};
let dct2x2 = if with_fine {
evaluate(STRATEGY_DCT2X2)
} else {
f32::INFINITY
};
let mut evaluate_dct4 = |strategy| {
if with_dct4 {
evaluate(strategy)
} else {
f32::INFINITY
}
};
Sub8Costs {
dct8,
identity,
dct2x2,
dct4x4: evaluate_dct4(STRATEGY_DCT4X4),
dct4x8: evaluate_dct4(STRATEGY_DCT4X8),
dct8x4: evaluate_dct4(STRATEGY_DCT8X4),
afv: if with_dct4 || distance <= AFV_MAX_DISTANCE {
std::array::from_fn(|kind| evaluate(STRATEGY_AFV0 + kind as u8))
} else {
[f32::INFINITY; 4]
},
}
}
#[inline]
fn cmap_factors(
cfl: crate::color_correlation::CflFrame,
ytox_map: &ImageSB,
ytob_map: &ImageSB,
bx: usize,
by: usize,
) -> [f32; 3] {
let tx = (bx / 8).min(ytox_map.xsize() - 1);
let ty = (by / 8).min(ytox_map.ysize() - 1);
[
crate::color_correlation::y_to_x_ratio(cfl, ytox_map.row(ty)[tx]),
0.0,
crate::color_correlation::y_to_b_ratio(cfl, ytob_map.row(ty)[tx]),
]
}
const MERGE_RISK_K: f32 = std::f32::consts::LOG2_E;
#[inline]
fn risk_gated(k: f32, accept: f32, q_min: f32, q_max: f32, area_scale: f32) -> f32 {
if q_min <= 0.0 || k <= 0.0 {
return accept;
}
let spread = q_max / q_min - 1.0;
accept * fast_exp2(-k * area_scale * spread)
}