use crate::afv;
use crate::quant_weights::DequantMatrices;
use crate::simd::scaled_add::{AddScaledFn, selected_add_scaled_fn};
use crate::thread_pool::ThreadPool;
use crate::{
Speed, ac_strategy, adaptive_quant, color_correlation, dct, frame, group, inflated_cost, xyb,
};
#[inline]
fn channel_weights_for_bias(bias: f32, distance: f32) -> [f32; 3] {
let matrix_mix = ((bias - xyb::B_BIAS) / (0.85 - xyb::B_BIAS)).clamp(0.0, 1.0);
let coarse = crate::yellow_opsin::coarse_weight_strength(distance)
* ((bias - 0.70) / 0.15).clamp(0.0, 1.0);
[
0.30 + (0.10 - 0.30) * matrix_mix,
1.0,
0.28 + (0.83 - 0.28) * matrix_mix * (1.0 - coarse),
]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub(crate) enum FrameRole {
#[default]
Last,
SavedFirst,
LastExtraAdd,
}
pub(crate) struct EncodingContext {
pub(crate) thread_pool: ThreadPool,
pub(crate) speed: Speed,
pub(crate) frame_role: FrameRole,
pub(crate) crop_base: bool,
pub(crate) lossy_modular: crate::LossyModular,
pub(crate) modular_vertical_first: bool,
pub(crate) progressive_dc: bool,
pub(crate) progressive_dc_levels: Option<u32>,
pub(crate) group_order: crate::GroupOrder,
pub(crate) arbiter: crate::Arbiter,
pub(crate) grain: crate::Grain,
#[cfg(feature = "splines")]
pub(crate) splines: bool,
#[cfg(feature = "splines")]
pub(crate) edge_splines: bool,
#[cfg(feature = "splines")]
pub(crate) dots: bool,
#[cfg(feature = "splines")]
pub(crate) spline_kernels: &'static crate::splines::SplinesContext,
pub(crate) xyb: xyb::XybMatrix,
pub(crate) coding: crate::coding::CodingTransform,
dequant_bias: [group::DequantBias; 3],
pub(crate) epf_channel_scale: Option<[f32; 3]>,
plain_matrices: std::sync::OnceLock<&'static DequantMatrices>,
plain_distance: f32,
cfl_frame: [std::sync::atomic::AtomicU32; 3],
pub(crate) extra_step: u32,
pub(crate) extra_black: bool,
pub(crate) extra_squeeze_distance: f32,
pub(crate) extra_learned_trees: bool,
pub(crate) extra_plane_tree: Option<crate::lossless::PlaneTreeBudget>,
pub(crate) extra_context_trees: bool,
pub(crate) alpha_patches: bool,
pub(crate) patch_epf: bool,
pub(crate) epf_overlay: Option<crate::epf_filter::EpfOverlay>,
pub(crate) glyph_templates: bool,
pub(crate) patch_lite: bool,
pub(crate) template_probe: Option<std::sync::Arc<std::sync::atomic::AtomicUsize>>,
pub(crate) function_fields: bool,
pub(crate) function_fields_layered: bool,
pub(crate) function_fields_exact: bool,
pub(crate) function_fields_portable: bool,
pub(crate) aq_source: std::sync::OnceLock<crate::image::Image3F>,
pub(crate) alpha_aq: std::sync::OnceLock<(usize, Vec<f32>)>,
#[cfg(feature = "splines")]
pub(crate) overlay_coverage: Option<Vec<f32>>,
channel_weights: [f32; 3],
pub(crate) merge: ac_strategy::MergeTuning,
pub(crate) learned_rate: bool,
base_matrices: &'static DequantMatrices,
point_chroma_hq_matrices: Option<&'static DequantMatrices>,
sat_matrices: &'static DequantMatrices,
pair_b_matrices: &'static DequantMatrices,
sat_pair_b_matrices: &'static DequantMatrices,
x_heavy_matrices: &'static DequantMatrices,
chroma_heavy: std::sync::atomic::AtomicBool,
luma_texture: std::sync::atomic::AtomicU32,
#[cfg(feature = "extraslow")]
selection_tables: std::sync::atomic::AtomicPtr<[&'static DequantMatrices; 4]>,
point_chroma: std::sync::atomic::AtomicBool,
x_heavy: std::sync::atomic::AtomicBool,
pair_b_fine: std::sync::atomic::AtomicBool,
b_heavy: std::sync::atomic::AtomicBool,
screen_content: std::sync::atomic::AtomicBool,
b_qm_scale: std::sync::atomic::AtomicU32,
x_qm_scale_floor: std::sync::atomic::AtomicU32,
pub(crate) to_xyb_band: xyb::ToXybBandFn,
pub(crate) round_lattice_pow2: xyb::RoundLatticePow2Fn,
pub(crate) fill_quant_field: adaptive_quant::FillQuantFieldFn,
pub(crate) sse_and_rate: inflated_cost::SseAndRateFn,
pub(crate) sse_and_quantize: inflated_cost::SseAndQuantizeFn,
pub(crate) recon_dist_and_rate: inflated_cost::ReconDistAndRateFn,
pub(crate) recon_luma_phase: inflated_cost::ReconLumaPhaseFn,
pub(crate) recon_chroma_phase: inflated_cost::ReconChromaPhaseFn,
pub(crate) recon_error_kernels: inflated_cost::ReconErrorKernels,
pub(crate) rate_log2_lut: &'static inflated_cost::RateLog2Lut,
pub(crate) quantize_block_ac: group::QuantizeBlockAcFn,
pub(crate) quantize_dc: group::QuantizeDcFn,
pub(crate) quantize_dc_cfl: group::QuantizeDcCflFn,
pub(crate) quantize_dc_i32: group::QuantizeDcI32Fn,
pub(crate) quantize_dc_cfl_i32: group::QuantizeDcCflI32Fn,
pub(crate) dc_rows: crate::dc_ops::DcRowKernels,
pub(crate) add_scaled: AddScaledFn,
pub(crate) apply_cfl: ac_strategy::ApplyCflFn,
pub(crate) mosaic_seam_stats: crate::mosaic_seam::MosaicSeamStatsFn,
pub(crate) cfl_regression: color_correlation::CflRegressionFn,
pub(crate) cfl_rdo_block: color_correlation::CflRdoBlockFn,
pub(crate) cfl_rdo_refill: color_correlation::CflRdoRefillFn,
pub(crate) cfl_rdo_stats: color_correlation::CflRdoStatsFn,
pub(crate) cfl_rd_costs: color_correlation::CflRdCostsFn,
pub(crate) noise_kernels: crate::noise::NoiseKernels,
pub(crate) chroma_gradient_sums: frame::ChromaGradientSumsFn,
pub(crate) fill_ytob_row: color_correlation::FillYtobRowFn,
pub(crate) accumulate_ytob_weights: color_correlation::AccumulateYtobWeightsFn,
pub(crate) fill_ytob_residuals: color_correlation::FillYtobResidualsFn,
pub(crate) idct: &'static dct::IdctMethods,
pub(crate) identity8x8: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct2x2_8x8: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct8x8: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct8x16: &'static dct::DctFn<16, 8, 128>,
pub(crate) dct16x8: &'static dct::DctFn<8, 16, 128>,
pub(crate) dct16x16: &'static dct::DctFn<16, 16, 256>,
pub(crate) dct4x4: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct4x8: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct8x4: &'static dct::DctFn<8, 8, 64>,
pub(crate) dct32x32: &'static dct::DctFn<32, 32, 1024>,
pub(crate) dct64x64: &'static dct::DctFn<64, 64, 4096>,
pub(crate) dct64x32: &'static dct::DctFn<32, 64, 2048>,
pub(crate) dct32x64: &'static dct::DctFn<64, 32, 2048>,
#[cfg(feature = "extraslow")]
pub(crate) dct128x128: &'static dct::DctFn<128, 128, 16384>,
#[cfg(feature = "extraslow")]
pub(crate) dct128x64: &'static dct::DctFn<64, 128, 8192>,
#[cfg(feature = "extraslow")]
pub(crate) dct64x128: &'static dct::DctFn<128, 64, 8192>,
pub(crate) dct32x16: &'static dct::DctFn<16, 32, 512>,
pub(crate) dct16x32: &'static dct::DctFn<32, 16, 512>,
pub(crate) dct32x8: &'static dct::DctFn<8, 32, 256>,
pub(crate) dct8x32: &'static dct::DctFn<32, 8, 256>,
pub(crate) dc_from_dct8x32: dct::DcFromDct8x32Fn,
pub(crate) dc_from_dct32x32: dct::DcFromDct32x32Fn,
pub(crate) dc_from_dct32x16: dct::DcFromDct32x16Fn,
pub(crate) dc_from_dct16x32: dct::DcFromDct16x32Fn,
pub(crate) dc_from_dct64x64: dct::DcFromDct64x64Fn,
pub(crate) dc_from_dct64x32: dct::DcFromDct64x32Fn,
pub(crate) dc_from_dct32x64: dct::DcFromDct32x64Fn,
#[cfg(feature = "extraslow")]
pub(crate) dc_from_dct128x128: dct::DcFromDct128x128Fn,
#[cfg(feature = "extraslow")]
pub(crate) dc_from_dct128x64: dct::DcFromDct128x64Fn,
#[cfg(feature = "extraslow")]
pub(crate) dc_from_dct64x128: dct::DcFromDct64x128Fn,
pub(crate) afv0: afv::AfvFn,
pub(crate) afv1: afv::AfvFn,
pub(crate) afv2: afv::AfvFn,
pub(crate) afv3: afv::AfvFn,
}
impl EncodingContext {
#[inline]
pub(crate) fn matrices(&self) -> &'static DequantMatrices {
use std::sync::atomic::Ordering::Relaxed;
let plain = self.plain_matrices.get();
debug_assert!(
plain.is_some() || self.coding.is_xyb(),
"non-XYB tables are resolved per frame"
);
if let Some(&plain) = plain {
plain
} else if let Some(sets) = self.selection_matrices()
&& !self.x_heavy.load(Relaxed)
&& self.point_chroma_hq_matrices().is_none()
{
sets[usize::from(self.chroma_heavy.load(Relaxed))
+ 2 * usize::from(self.pair_b_fine.load(Relaxed))]
} else if self.x_heavy.load(Relaxed) {
self.x_heavy_matrices
} else if let Some(matrices) = self.point_chroma_hq_matrices() {
matrices
} else {
match (
self.chroma_heavy.load(Relaxed),
self.pair_b_fine.load(Relaxed),
) {
(true, true) => self.sat_pair_b_matrices,
(true, false) => self.sat_matrices,
(false, true) => self.pair_b_matrices,
(false, false) => self.base_matrices,
}
}
}
#[inline]
fn selection_matrices(&self) -> Option<&'static [&'static DequantMatrices; 4]> {
#[cfg(feature = "extraslow")]
{
let sets = self
.selection_tables
.load(std::sync::atomic::Ordering::Acquire);
unsafe { sets.as_ref() }
}
#[cfg(not(feature = "extraslow"))]
{
None
}
}
pub(crate) fn set_point_chroma(&self, point_chroma: bool) {
self.point_chroma
.store(point_chroma, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn point_chroma(&self) -> bool {
use std::sync::atomic::Ordering::Relaxed;
self.speed.effort().chroma_policy
&& self.point_chroma.load(Relaxed)
&& !self.x_heavy.load(Relaxed)
}
pub(crate) fn point_chroma_hq_matrices(&self) -> Option<&'static DequantMatrices> {
if self.point_chroma() {
self.point_chroma_hq_matrices
} else {
None
}
}
pub(crate) fn set_pair_b_fine(&self, fine: bool) {
self.pair_b_fine
.store(fine, std::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "extraslow")]
pub(crate) fn set_selection_family(&self, distance: f32, family: Option<f32>) {
let effort = self.speed.effort();
let sets = family.filter(|&f| f != 1.0).map(|f| {
DequantMatrices::selection_sets(
distance,
self.arbiter,
effort.rectangles,
effort.layout_retile && !effort.layout_loop(),
f,
)
});
self.selection_tables.store(
sets.map_or(std::ptr::null_mut(), |sets| {
std::ptr::from_ref(sets).cast_mut()
}),
std::sync::atomic::Ordering::Release,
);
}
pub(crate) fn set_luma_texture(&self, stat: f32) {
self.luma_texture
.store(stat.to_bits(), std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn luma_texture(&self) -> f32 {
f32::from_bits(self.luma_texture.load(std::sync::atomic::Ordering::Relaxed))
}
pub(crate) fn chroma_heavy(&self) -> bool {
self.chroma_heavy.load(std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn set_chroma_heavy(&self, heavy: bool) {
self.chroma_heavy
.store(heavy, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn x_heavy(&self) -> bool {
self.x_heavy.load(std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn set_x_heavy(&self, heavy: bool) {
self.x_heavy
.store(heavy, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn b_heavy(&self) -> bool {
self.b_heavy.load(std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn set_b_heavy(&self, heavy: bool) {
self.b_heavy
.store(heavy, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn screen_content(&self) -> bool {
self.screen_content
.load(std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn set_screen_content(&self, screen: bool) {
self.screen_content
.store(screen, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn raise_x_qm_scale_floor(&self, scale: u32) {
self.x_qm_scale_floor
.fetch_max(scale.clamp(2, 7), std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn x_qm_scale_floor(&self) -> u32 {
self.x_qm_scale_floor
.load(std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn raise_b_qm_scale(&self, scale: u32) {
self.b_qm_scale
.fetch_max(scale.clamp(2, 7), std::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "extraslow")]
pub(crate) fn set_b_qm_scale(&self, scale: u32) {
self.b_qm_scale
.store(scale.clamp(2, 7), std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn b_qm_scale(&self) -> u32 {
self.b_qm_scale.load(std::sync::atomic::Ordering::Relaxed)
}
#[inline]
pub(crate) fn b_qm_mul(&self) -> f32 {
static MULTIPLIERS: [f32; 6] = [1.0, 1.25, 1.5625, 1.953125, 2.441_406_2, 3.051_757_8];
MULTIPLIERS[(self.b_qm_scale().clamp(2, 7) - 2) as usize]
}
pub(crate) fn set_coding(&mut self, coding: crate::coding::CodingTransform, distance: f32) {
self.coding = coding;
self.refresh_dequant_bias();
self.set_cfl_frame(crate::color_correlation::CflFrame {
base_x: coding.base_correlation_x(),
base_b: coding.base_correlation_b(),
color_factor: crate::color_correlation::K_COLOR_FACTOR,
});
self.epf_channel_scale = match (coding, self.speed) {
(crate::coding::CodingTransform::Rgb, Speed::Slow | Speed::ExtraSlow) => {
Some([5.0, 5.0, 2.5])
}
_ => coding.epf_channel_scale(),
};
self.plain_matrices = std::sync::OnceLock::new();
self.plain_distance = distance;
if !coding.is_xyb() {
self.channel_weights = coding.channel_weights();
#[cfg(feature = "splines")]
{
self.splines = false;
self.edge_splines = false;
self.dots = false;
}
}
}
pub(crate) fn set_plain_matrices(&self, coded: &crate::image::Image3F) {
self.plain_matrices.get_or_init(|| {
let effort = self.speed.effort();
DequantMatrices::new_plain(
self.plain_distance,
effort.rectangles,
self.arbiter,
self.coding,
self.coding.plane_scale(coded),
effort.layout_retile && !effort.layout_loop(),
)
});
}
#[inline]
pub(crate) fn cfl_frame(&self) -> color_correlation::CflFrame {
use std::sync::atomic::Ordering::Relaxed;
let [x, b, f] = self
.cfl_frame
.each_ref()
.map(|v| f32::from_bits(v.load(Relaxed)));
color_correlation::CflFrame {
base_x: x,
base_b: b,
color_factor: f,
}
}
#[inline]
pub(crate) fn cfl_base_x(&self) -> f32 {
self.cfl_frame().base_x
}
#[inline]
pub(crate) fn cfl_base_b(&self) -> f32 {
self.cfl_frame().base_b
}
pub(crate) fn set_cfl_frame(&self, cfl: crate::color_correlation::CflFrame) {
use std::sync::atomic::Ordering::Relaxed;
for (slot, v) in self
.cfl_frame
.iter()
.zip([cfl.base_x, cfl.base_b, cfl.color_factor])
{
slot.store(v.to_bits(), Relaxed);
}
}
#[inline]
pub(crate) fn set_xyb_matrix(
&mut self,
matrix: xyb::XybMatrix,
distance: f32,
default_weights: bool,
) {
self.xyb = matrix;
self.refresh_dequant_bias();
let weight_bias = if default_weights {
xyb::B_BIAS
} else {
matrix.fwd[8]
};
self.channel_weights = channel_weights_for_bias(weight_bias, distance);
}
fn refresh_dequant_bias(&mut self) {
let explicit = self.coding.is_xyb() && !self.xyb.is_decoder_default();
self.dequant_bias = crate::group::DequantBias::channels(explicit);
}
#[inline]
pub(crate) fn dequant_bias(&self, channel: usize) -> crate::group::DequantBias {
self.dequant_bias[channel]
}
#[inline]
pub(crate) fn channel_weights(&self) -> [f32; 3] {
self.channel_weights
}
#[inline]
pub(crate) fn channel_weight(&self, channel: usize) -> f32 {
self.channel_weights()[channel]
}
pub(crate) fn new(
speed: Speed,
arbiter: crate::Arbiter,
xyb: xyb::XybMatrix,
distance: f32,
num_threads: usize,
) -> Self {
let quantize_dc = group::selected_quantize_dc_methods();
let dc_from_dct = dct::selected_dc_from_dct_methods();
let afv = afv::selected_afv_methods();
let effort = speed.effort();
let retile = effort.layout_retile && !effort.layout_loop();
let base_matrices = if effort.rectangles {
DequantMatrices::new(distance, arbiter, retile)
} else {
DequantMatrices::new_fast(distance, arbiter)
};
Self {
thread_pool: ThreadPool::new(num_threads),
speed,
frame_role: FrameRole::Last,
crop_base: false,
lossy_modular: crate::LossyModular::Off,
modular_vertical_first: false,
progressive_dc: false,
progressive_dc_levels: None,
group_order: crate::GroupOrder::Scanline,
arbiter,
grain: crate::Grain::Off,
#[cfg(feature = "splines")]
splines: false,
#[cfg(feature = "splines")]
edge_splines: false,
#[cfg(feature = "splines")]
dots: false,
xyb,
coding: crate::coding::CodingTransform::Xyb,
dequant_bias: group::DequantBias::channels(!xyb.is_decoder_default()),
epf_channel_scale: None,
plain_matrices: std::sync::OnceLock::new(),
plain_distance: 0.0,
cfl_frame: [
std::sync::atomic::AtomicU32::new(0.0f32.to_bits()),
std::sync::atomic::AtomicU32::new(1.0f32.to_bits()),
std::sync::atomic::AtomicU32::new(84.0f32.to_bits()),
],
aq_source: std::sync::OnceLock::new(),
alpha_aq: std::sync::OnceLock::new(),
#[cfg(feature = "splines")]
overlay_coverage: None,
extra_step: 1,
extra_black: false,
extra_squeeze_distance: 0.0,
extra_learned_trees: false,
extra_plane_tree: None,
extra_context_trees: false,
alpha_patches: false,
patch_epf: false,
epf_overlay: None,
glyph_templates: false,
patch_lite: false,
template_probe: None,
function_fields: false,
function_fields_layered: false,
function_fields_exact: false,
function_fields_portable: false,
channel_weights: channel_weights_for_bias(xyb.fwd[8], distance),
merge: ac_strategy::MergeTuning::new(distance, arbiter, speed),
learned_rate: false,
base_matrices,
point_chroma_hq_matrices: (effort.chroma_policy
&& distance < crate::quant_weights::QM_FLAT_B8_MIN_DISTANCE)
.then(|| {
DequantMatrices::new(
crate::quant_weights::QM_FLAT_B8_MIN_DISTANCE,
arbiter,
retile,
)
}),
sat_matrices: DequantMatrices::new_saturated(distance, arbiter, retile),
pair_b_matrices: if effort.chroma_policy {
DequantMatrices::new_pair_b(distance, arbiter, retile)
} else {
base_matrices
},
sat_pair_b_matrices: DequantMatrices::new_saturated_pair_b(distance, arbiter, retile),
x_heavy_matrices: if effort.chroma_policy {
DequantMatrices::new_x_heavy(distance, arbiter)
} else {
DequantMatrices::new(0.0, arbiter, retile)
},
chroma_heavy: std::sync::atomic::AtomicBool::new(false),
#[cfg(feature = "extraslow")]
selection_tables: std::sync::atomic::AtomicPtr::new(std::ptr::null_mut()),
luma_texture: std::sync::atomic::AtomicU32::new(0),
point_chroma: std::sync::atomic::AtomicBool::new(false),
x_heavy: std::sync::atomic::AtomicBool::new(false),
pair_b_fine: std::sync::atomic::AtomicBool::new(false),
b_heavy: std::sync::atomic::AtomicBool::new(false),
screen_content: std::sync::atomic::AtomicBool::new(false),
b_qm_scale: std::sync::atomic::AtomicU32::new(2),
x_qm_scale_floor: std::sync::atomic::AtomicU32::new(2),
#[cfg(feature = "splines")]
spline_kernels: crate::splines::SplinesContext::get(),
to_xyb_band: xyb::selected_to_xyb_band_fn(),
round_lattice_pow2: xyb::selected_round_lattice_pow2_fn(),
fill_quant_field: adaptive_quant::selected_fill_quant_field_fn(),
sse_and_rate: inflated_cost::selected_sse_and_rate_fn(),
sse_and_quantize: inflated_cost::selected_sse_and_quantize_fn(),
recon_dist_and_rate: inflated_cost::select_recon_dist_and_rate_fn(),
recon_luma_phase: inflated_cost::select_recon_luma_phase_fn(),
recon_chroma_phase: inflated_cost::select_recon_chroma_phase_fn(),
recon_error_kernels: inflated_cost::ReconErrorKernels {
combine: inflated_cost::select_combine_error_fn(),
rgb_hue_chroma_edge_loss: inflated_cost::select_rgb_hue_chroma_edge_loss_fn(),
},
rate_log2_lut: inflated_cost::rate_log2_lut(),
quantize_block_ac: group::selected_quantize_block_ac_fn(),
quantize_dc: quantize_dc.quantize,
quantize_dc_cfl: quantize_dc.quantize_cfl,
quantize_dc_i32: quantize_dc.quantize_i32,
quantize_dc_cfl_i32: quantize_dc.quantize_cfl_i32,
dc_rows: crate::dc_ops::selected_dc_row_kernels(),
add_scaled: selected_add_scaled_fn(),
apply_cfl: ac_strategy::selected_apply_cfl_fn(),
mosaic_seam_stats: crate::mosaic_seam::select_mosaic_seam_stats_fn(),
cfl_regression: color_correlation::selected_cfl_regression_fn(),
cfl_rdo_block: color_correlation::selected_cfl_rdo_block_fn(),
cfl_rdo_refill: color_correlation::selected_cfl_rdo_refill_fn(),
cfl_rdo_stats: color_correlation::selected_cfl_rdo_stats_fn(),
cfl_rd_costs: color_correlation::selected_cfl_rd_costs_fn(),
noise_kernels: crate::noise::selected_noise_kernels(),
chroma_gradient_sums: frame::selected_chroma_gradient_sums_fn(),
fill_ytob_row: color_correlation::selected_fill_ytob_row_fn(),
accumulate_ytob_weights: color_correlation::selected_accumulate_ytob_weights_fn(),
fill_ytob_residuals: color_correlation::selected_fill_ytob_residuals_fn(),
idct: dct::selected_idct_methods(),
identity8x8: dct::selected_identity8x8(),
dct2x2_8x8: dct::selected_dct2x2_8x8(),
dct8x8: dct::selected_dct8x8(),
dct8x16: dct::selected_dct8x16(),
dct16x8: dct::selected_dct16x8(),
dct16x16: dct::selected_dct16x16(),
dct4x4: dct::selected_dct4x4(),
dct4x8: dct::selected_dct4x8(),
dct8x4: dct::selected_dct8x4(),
dct32x32: dct::selected_dct32x32(),
dct64x64: dct::selected_dct64x64(),
dct64x32: dct::selected_dct64x32(),
dct32x64: dct::selected_dct32x64(),
#[cfg(feature = "extraslow")]
dct128x128: crate::dct128::selected_dct128x128(),
#[cfg(feature = "extraslow")]
dct128x64: crate::dct128::selected_dct128x64(),
#[cfg(feature = "extraslow")]
dct64x128: crate::dct128::selected_dct64x128(),
dct32x16: dct::selected_dct32x16(),
dct16x32: dct::selected_dct16x32(),
dct32x8: dct::selected_dct32x8(),
dct8x32: dct::selected_dct8x32(),
dc_from_dct8x32: dc_from_dct.dct8x32,
dc_from_dct32x32: dc_from_dct.dct32x32,
dc_from_dct32x16: dc_from_dct.dct32x16,
dc_from_dct16x32: dc_from_dct.dct16x32,
dc_from_dct64x64: dc_from_dct.dct64x64,
dc_from_dct64x32: dc_from_dct.dct64x32,
dc_from_dct32x64: dc_from_dct.dct32x64,
#[cfg(feature = "extraslow")]
dc_from_dct128x128: dc_from_dct.dct128x128,
#[cfg(feature = "extraslow")]
dc_from_dct128x64: dc_from_dct.dct128x64,
#[cfg(feature = "extraslow")]
dc_from_dct64x128: dc_from_dct.dct64x128,
afv0: afv.afv0,
afv1: afv.afv1,
afv2: afv.afv2,
afv3: afv.afv3,
}
}
}
impl Default for EncodingContext {
#[inline]
fn default() -> Self {
Self::new(
Speed::Fast,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
1.0,
1,
)
}
}
#[cfg(test)]
mod tests {
use super::channel_weights_for_bias;
#[cfg(feature = "extraslow")]
#[test]
fn selection_family_switches_and_restores_each_content_class() {
use super::{DequantMatrices, EncodingContext, Speed, xyb};
let distance = 1.0;
let ctx = EncodingContext::new(
Speed::ExtraSlow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
distance,
2,
);
let effort = ctx.speed.effort();
for (class, (chroma, pair_b)) in
[(false, false), (true, false), (false, true), (true, true)]
.into_iter()
.enumerate()
{
ctx.set_chroma_heavy(chroma);
ctx.set_pair_b_fine(pair_b);
let coding = ctx.matrices();
for family in [Some(1.25), Some(0.85), None, Some(1.25), Some(1.0)] {
let expected = match family {
Some(scale) if scale != 1.0 => DequantMatrices::selection_sets(
distance,
ctx.arbiter,
effort.rectangles,
effort.layout_retile && !effort.layout_loop(),
scale,
)[class],
_ => coding,
};
ctx.set_selection_family(distance, family);
ctx.thread_pool.map(4, |_| {
assert!(std::ptr::eq(ctx.matrices(), expected));
});
}
}
}
#[cfg(feature = "extraslow")]
#[test]
fn selection_family_preserves_point_and_x_overrides() {
use super::{EncodingContext, Speed, xyb};
let distance = 0.295;
let ctx = EncodingContext::new(
Speed::ExtraSlow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
distance,
1,
);
ctx.set_point_chroma(true);
let point = ctx.matrices();
ctx.set_x_heavy(true);
let x = ctx.matrices();
assert!(!std::ptr::eq(point, x));
ctx.set_selection_family(distance, Some(1.25));
assert!(std::ptr::eq(ctx.matrices(), x));
ctx.set_x_heavy(false);
assert!(std::ptr::eq(ctx.matrices(), point));
ctx.set_point_chroma(false);
let selection = ctx.matrices();
assert!(!std::ptr::eq(selection, point));
assert!(!std::ptr::eq(selection, x));
ctx.set_selection_family(distance, None);
assert!(!std::ptr::eq(ctx.matrices(), selection));
}
#[test]
fn point_chroma_tables_preserve_existing_class_precedence() {
use super::{DequantMatrices, EncodingContext, Speed, xyb};
for distance in [0.295, 0.299, 0.3, 1.25, 2.25] {
let ctx = EncodingContext::new(
Speed::Slow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
distance,
1,
);
ctx.set_chroma_heavy(true);
ctx.set_pair_b_fine(true);
let previous = ctx.matrices();
ctx.set_point_chroma(true);
let expected = if distance < 0.3 {
DequantMatrices::new(0.3, crate::Arbiter::Ssimulacra2, false)
} else {
previous
};
assert!(std::ptr::eq(ctx.matrices(), expected));
ctx.set_x_heavy(true);
assert!(std::ptr::eq(
ctx.matrices(),
DequantMatrices::new_x_heavy(distance, crate::Arbiter::Ssimulacra2)
));
assert!(ctx.point_chroma_hq_matrices().is_none());
ctx.set_x_heavy(false);
ctx.set_point_chroma(false);
assert!(std::ptr::eq(ctx.matrices(), previous));
}
let ctx = EncodingContext::new(
Speed::Fast,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
0.295,
1,
);
let previous = ctx.matrices();
ctx.set_point_chroma(true);
assert!(std::ptr::eq(ctx.matrices(), previous));
assert!(ctx.point_chroma_hq_matrices().is_none());
}
#[test]
fn hue_tables_require_the_frame_content_gate() {
use super::{DequantMatrices, EncodingContext, Speed, xyb};
let ctx = EncodingContext::new(
Speed::Slow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
1.0,
1,
);
assert!(std::ptr::eq(
ctx.matrices(),
DequantMatrices::new(1.0, crate::Arbiter::Ssimulacra2, false)
));
ctx.set_chroma_heavy(true);
assert!(std::ptr::eq(
ctx.matrices(),
DequantMatrices::new_saturated(1.0, crate::Arbiter::Ssimulacra2, false)
));
ctx.set_x_heavy(true);
assert!(std::ptr::eq(
ctx.matrices(),
DequantMatrices::new_x_heavy(1.0, crate::Arbiter::Ssimulacra2)
));
ctx.set_x_heavy(false);
assert!(std::ptr::eq(
ctx.matrices(),
DequantMatrices::new_saturated(1.0, crate::Arbiter::Ssimulacra2, false)
));
}
#[test]
fn channel_weights_follow_the_opsin_b_row() {
assert_eq!(
channel_weights_for_bias(crate::xyb::B_BIAS, 1.0),
[0.30, 1.0, 0.28]
);
let strong = channel_weights_for_bias(0.85, 1.0);
assert!((strong[0] - 0.10).abs() < 1e-6);
assert_eq!(strong[1], 1.0);
assert!((strong[2] - 0.83).abs() < 1e-6);
let mid = channel_weights_for_bias((crate::xyb::B_BIAS + 0.85) * 0.5, 1.0);
assert!((mid[0] - 0.20).abs() < 1e-6);
assert!((mid[2] - 0.555).abs() < 1e-6);
}
#[test]
fn adaptive_matrix_updates_cached_coarse_weights() {
use super::{EncodingContext, Speed, xyb};
let mut ctx = EncodingContext::new(
Speed::Slow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
2.0,
1,
);
assert_eq!(ctx.channel_weights(), [0.30, 1.0, 0.28]);
let matrix = crate::yellow_opsin::matrix_for_bias(0.90);
ctx.set_xyb_matrix(matrix, 2.0, false);
assert_eq!(ctx.xyb, matrix);
let coarse = ctx.channel_weights();
assert!((coarse[0] - 0.10).abs() < 1e-6);
assert_eq!(coarse[1], 1.0);
assert_eq!(coarse[2], 0.28);
assert_eq!(
channel_weights_for_bias(0.70, 2.0),
channel_weights_for_bias(0.70, 1.0)
);
ctx.set_xyb_matrix(crate::yellow_opsin::matrix_for_bias(0.85), 1.0, false);
assert_eq!(ctx.channel_weights(), channel_weights_for_bias(0.85, 1.0));
let mild = crate::yellow_opsin::matrix_for_bias(0.65);
ctx.set_xyb_matrix(mild, 2.0, true);
assert_eq!(ctx.xyb, mild);
assert_eq!(ctx.channel_weights(), [0.30, 1.0, 0.28]);
}
#[test]
fn dequant_bias_follows_the_signaled_bundle() {
use super::{EncodingContext, Speed, xyb};
use crate::group::{DequantBias, EXPLICIT_QUANT_BIASES};
let mut ctx = EncodingContext::new(
Speed::Slow,
crate::Arbiter::Ssimulacra2,
xyb::XybMatrix::SPEC,
1.0,
1,
);
let biases = |ctx: &EncodingContext| std::array::from_fn(|c| ctx.dequant_bias(c));
assert_eq!(biases(&ctx), DequantBias::DEFAULT);
ctx.set_xyb_matrix(crate::yellow_opsin::matrix_for_bias(0.85), 1.0, false);
let explicit: [DequantBias; 3] = biases(&ctx);
assert_eq!(explicit[2].one, 1.0);
for (c, bias) in explicit.iter().enumerate() {
let f16 = crate::util::f32_to_f16_bits(EXPLICIT_QUANT_BIASES[c]);
assert_eq!(bias.one, crate::util::f16_bits_to_f32(f16));
assert!((bias.numerator - 0.145).abs() < 1e-4);
}
ctx.set_coding(crate::coding::CodingTransform::YCbCr, 1.0);
assert_eq!(biases(&ctx), DequantBias::DEFAULT);
}
}