use super::lz77::{
LZ77_MIN_SYMBOL, LzToken, build_lz_pixel_code, build_lz_pixel_code_opts,
lz77_compress_for_speed, lz77_compress_for_speed_with_depth, lz77_literals,
write_local_tree_lz77, write_lz_section, write_tree_lz77,
};
use super::predictor::{
GradPackInteriorFn, PREDICTOR_GRADIENT, SqueezePredictorCost, channel_to_context,
choose_predictor_for_plane, fixed_predictor, predictor_value, selected_grad_pack_interior_fn,
tokenize_plane_rows,
};
use super::rct::write_rct_transform;
use super::{
DeepLzScratchPool, GROUP_DIM, GroupLayout, LF_GROUP_DIM, MA_MIN_NODE_SAMPLES,
MA_PHOTO_CANDIDATES, MA_SPLIT_COST_BITS, build_balanced_tree_tokens_mul, group_lz_threads,
push_leaf_mul, push_split, sample_channel_ma, walk_channel_ma, write_frame_header_modular,
write_lz_groups, write_toc_entry, write_u64,
};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::{CoderScratch, LZ77_MAX_CONTEXTS};
const LM_MAX_CONTEXTS: usize = 1024;
const _: () = assert!(LM_MAX_CONTEXTS <= LZ77_MAX_CONTEXTS);
use crate::encode_image::AlphaPlane;
use crate::entropy::{Token, pack_signed};
use crate::image::Image3Si;
use crate::ma_tree::{LearnedTree, MaLearnParams, MaNode, MaSamples, learn_ma_tree};
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::WpParams;
fn write_modular_transforms_rct_squeeze(steps: &[crate::squeeze::SqueezeStep], w: &mut BitWriter) {
w.write(2, 0b10);
w.write(4, 0);
write_rct_transform(6, w);
write_squeeze_transform_entry(steps, w);
}
fn write_modular_transforms_squeeze_only(steps: &[crate::squeeze::SqueezeStep], w: &mut BitWriter) {
w.write(2, 0b01); write_squeeze_transform_entry(steps, w);
}
fn write_squeeze_transform_entry(steps: &[crate::squeeze::SqueezeStep], w: &mut BitWriter) {
w.write(2, 0b10); let n = steps.len() as u32;
if n == 0 {
w.write(2, 0b00);
} else if n <= 16 {
w.write(2, 0b01);
w.write(4, (n - 1) as u64);
} else if n <= 72 {
w.write(2, 0b10);
w.write(6, (n - 9) as u64);
} else {
w.write(2, 0b11);
w.write(8, (n - 41) as u64);
}
for s in steps {
w.write(1, if s.horizontal { 1 } else { 0 });
w.write(1, if s.in_place { 1 } else { 0 });
debug_assert!(s.begin_c < 8);
w.write(2, 0b00);
w.write(3, s.begin_c as u64);
match s.num_c {
1 => w.write(2, 0b00),
2 => w.write(2, 0b01),
3 => w.write(2, 0b10),
n => {
w.write(2, 0b11);
w.write(4, (n - 4) as u64);
}
}
}
}
pub(super) fn encode_squeeze_single_group(
linear: &Image3Si,
alpha: Option<&AlphaPlane>,
xsize: usize,
ysize: usize,
min_symbol: u32,
grad_pack_fn: GradPackInteriorFn,
speed: crate::Speed,
use_wp: bool,
pool: &ThreadPool,
scratch: &mut CoderScratch,
writer: &mut BitWriter,
) {
use crate::squeeze::{Channel, apply_step_forward, default_squeeze_steps};
let num_c = if alpha.is_some() { 4 } else { 3 };
let mut channels: Vec<Channel> = Vec::with_capacity(num_c);
for c in 0..3usize {
let mut ch = Channel::new(xsize, ysize);
for y in 0..ysize {
let row = linear.plane_row(c, y);
ch.data[y * xsize..y * xsize + xsize].copy_from_slice(&row[..xsize]);
}
channels.push(ch);
}
if let Some(a) = alpha {
let mut ch = Channel::new(xsize, ysize);
match a {
AlphaPlane::U8(data) => {
for (row, src_row) in ch
.data
.chunks_exact_mut(xsize)
.zip(data.chunks_exact(xsize))
{
for (dst, &src) in row[..xsize].iter_mut().zip(src_row.iter()) {
*dst = src as i32;
}
}
}
AlphaPlane::U16 { data, bits: _ } => {
for (row, src_row) in ch
.data
.chunks_exact_mut(xsize)
.zip(data.chunks_exact(xsize))
{
for (dst, &src) in row[..xsize].iter_mut().zip(src_row.iter()) {
*dst = src as i32;
}
}
}
AlphaPlane::F32(data) => {
for (row, src_row) in ch
.data
.chunks_exact_mut(xsize)
.zip(data.chunks_exact(xsize))
{
row[..xsize].copy_from_slice(&src_row[..xsize]);
}
}
}
channels.push(ch);
}
let steps = default_squeeze_steps(xsize, ysize, num_c);
for s in &steps {
apply_step_forward(&mut channels, s);
}
let nb = channels.len();
let predictors: Vec<u32> = if speed == crate::Speed::Slow {
pool.steal_map(scratch, channels.len(), |c, _scratch| {
let ch = &channels[c];
let data = &ch.data;
let w = ch.w;
let get = move |gx: usize, gy: usize| data[gy * w + gx];
choose_predictor_for_plane(get, ch.w, ch.h, use_wp)
})
} else {
vec![fixed_predictor(use_wp); nb]
};
let channel_tokens = pool.steal_map(scratch, nb, |c, scratch| {
let ch = &channels[c];
let mut tokens = Vec::with_capacity(ch.w * ch.h);
tokenize_plane_rows(
channel_to_context(c, nb),
|y| &ch.data[y * ch.w..][..ch.w],
ch.w,
ch.h,
predictors[c],
grad_pack_fn,
&mut scratch.gradient,
&mut tokens,
);
tokens
});
let mut tokens: Vec<Token> = Vec::new();
for channel in channel_tokens {
tokens.extend(channel);
}
write_frame_header_modular(alpha.is_some(), GroupLayout::DEFAULT, writer);
let mut section = BitWriter::new();
section.write(1, 1); section.write(1, 0); section.write(1, 0); section.write(1, 1); write_modular_transforms_rct_squeeze(&steps, &mut section);
let distance_ctx = nb as u32;
let lz_tokens = lz77_compress_for_speed(&tokens, distance_ctx, speed, scratch);
let code = build_lz_pixel_code(
std::iter::once(lz_tokens.as_slice()),
nb,
min_symbol,
speed == crate::Speed::Slow,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
write_local_tree_lz77(
&predictors,
&code,
min_symbol,
&mut scratch.huffman_pool,
&mut section,
);
write_lz_section(&lz_tokens, distance_ctx, &code, min_symbol, &mut section);
section.zero_pad_to_byte();
writer.write(1, 0); writer.zero_pad_to_byte();
write_toc_entry(section.bits_written() / 8, writer);
writer.zero_pad_to_byte();
writer.append_byte_aligned(std::slice::from_mut(&mut section));
writer.zero_pad_to_byte();
}
#[allow(clippy::too_many_arguments)]
fn for_each_squeeze_group_crop(
channels: &[crate::squeeze::Channel],
split: usize,
gdim: usize,
gx: usize,
gy: usize,
minsh: i32,
maxsh: i32,
mut visit: impl FnMut(usize, usize, usize, usize, usize, usize),
) {
let mut within = 0usize;
for (c, ch) in channels.iter().enumerate().skip(split) {
let msh = ch.hshift.min(ch.vshift);
if msh < minsh || msh > maxsh {
continue;
}
let hs = ch.hshift as usize;
let vs = ch.vshift as usize;
let rx0 = (gx * gdim) >> hs;
let ry0 = (gy * gdim) >> vs;
if rx0 >= ch.w || ry0 >= ch.h {
continue;
}
let rw = (gdim >> hs).min(ch.w - rx0);
let rh = (gdim >> vs).min(ch.h - ry0);
if rw == 0 || rh == 0 {
continue;
}
visit(within, c, rx0, ry0, rw, rh);
within += 1;
}
}
#[allow(clippy::too_many_arguments)]
pub(super) fn encode_squeeze_multigroup(
linear: &Image3Si,
alpha: Option<&AlphaPlane>,
xsize: usize,
ysize: usize,
min_symbol: u32,
xsize_groups: usize,
ysize_groups: usize,
xsize_dc_groups: usize,
ysize_dc_groups: usize,
num_dc_groups: usize,
num_ac_groups: usize,
grad_pack_fn: GradPackInteriorFn,
speed: crate::Speed,
use_wp: bool,
pool: &ThreadPool,
scratch: &mut CoderScratch,
writer: &mut BitWriter,
) -> bool {
use crate::squeeze::{Channel, apply_step_forward, default_squeeze_steps};
let num_c = if alpha.is_some() { 4 } else { 3 };
let mut channels: Vec<Channel> = Vec::with_capacity(num_c);
for c in 0..3usize {
let mut ch = Channel::new(xsize, ysize);
for y in 0..ysize {
let row = linear.plane_row(c, y);
ch.data[y * xsize..y * xsize + xsize].copy_from_slice(&row[..xsize]);
}
channels.push(ch);
}
if let Some(a) = alpha {
let mut ch = Channel::new(xsize, ysize);
for (i, value) in ch.data.iter_mut().enumerate() {
*value = a.get_i32(i);
}
channels.push(ch);
}
let steps = default_squeeze_steps(xsize, ysize, num_c);
for s in &steps {
apply_step_forward(&mut channels, s);
}
let nb = channels.len();
let split = channels
.iter()
.position(|c| c.w > GROUP_DIM || c.h > GROUP_DIM)
.unwrap_or(nb);
let predictors: Vec<u32> = if speed == crate::Speed::Slow {
let mut costs: Vec<SqueezePredictorCost> =
(0..nb).map(|_| SqueezePredictorCost::default()).collect();
for (cost, ch) in costs[..split].iter_mut().zip(&channels[..split]) {
cost.add_crop(|x, y| ch.data[y * ch.w + x], ch.w, ch.h, use_wp);
}
for gy in 0..ysize_dc_groups {
for gx in 0..xsize_dc_groups {
for_each_squeeze_group_crop(
&channels,
split,
LF_GROUP_DIM,
gx,
gy,
3,
1000,
|within, c, x0, y0, w, h| {
let ch = &channels[c];
costs[within].add_crop(
|x, y| ch.data[(y0 + y) * ch.w + x0 + x],
w,
h,
use_wp,
);
},
);
}
}
for gy in 0..ysize_groups {
for gx in 0..xsize_groups {
for_each_squeeze_group_crop(
&channels,
split,
GROUP_DIM,
gx,
gy,
0,
2,
|within, c, x0, y0, w, h| {
let ch = &channels[c];
costs[within].add_crop(
|x, y| ch.data[(y0 + y) * ch.w + x0 + x],
w,
h,
use_wp,
);
},
);
}
}
costs.iter().map(|c| c.predictor(use_wp)).collect()
} else {
vec![fixed_predictor(use_wp); nb]
};
let distance_ctx = nb as u32;
let global_channel_tokens = pool.steal_map(scratch, split, |c, scratch| {
let ch = &channels[c];
let ctx = channel_to_context(c, nb);
let mut tokens = Vec::with_capacity(ch.w * ch.h);
tokenize_plane_rows(
ctx,
|y| &ch.data[y * ch.w..][..ch.w],
ch.w,
ch.h,
predictors[c],
grad_pack_fn,
&mut scratch.gradient,
&mut tokens,
);
tokens
});
let mut global_tokens: Vec<Token> = Vec::new();
for channel in global_channel_tokens {
global_tokens.extend(channel);
}
let global_lz = lz77_compress_for_speed(&global_tokens, distance_ctx, speed, scratch);
let (dc_group_lz, ac_group_lz) = {
let deep_lz = (speed == crate::Speed::Slow)
.then(|| DeepLzScratchPool::new(group_lz_threads(speed, pool)));
let crop_group = |gdim: usize,
gx: usize,
gy: usize,
minsh: i32,
maxsh: i32,
scratch: &mut CoderScratch|
-> Vec<LzToken> {
let mut gtok: Vec<Token> = Vec::new();
for_each_squeeze_group_crop(
&channels,
split,
gdim,
gx,
gy,
minsh,
maxsh,
|within, c, rx0, ry0, rw, rh| {
let ch = &channels[c];
let ctx = channel_to_context(within, nb);
let pred = predictors[within];
tokenize_plane_rows(
ctx,
|y| &ch.data[(ry0 + y) * ch.w + rx0..][..rw],
rw,
rh,
pred,
grad_pack_fn,
&mut scratch.gradient,
&mut gtok,
);
},
);
if let Some(deep_lz) = &deep_lz {
deep_lz.with_depth(|depth| {
lz77_compress_for_speed_with_depth(
>ok,
distance_ctx,
speed,
depth,
None,
scratch,
)
})
} else {
lz77_compress_for_speed(>ok, distance_ctx, speed, scratch)
}
};
let dc_group_lz = pool.steal_map_with_threads(
scratch,
num_dc_groups,
group_lz_threads(speed, pool),
|group_index, scratch| {
let gx = group_index % xsize_dc_groups;
let gy = group_index / xsize_dc_groups;
crop_group(LF_GROUP_DIM, gx, gy, 3, 1000, scratch)
},
);
let ac_group_lz = pool.steal_map_with_threads(
scratch,
num_ac_groups,
group_lz_threads(speed, pool),
|group_index, scratch| {
let gx = group_index % xsize_groups;
let gy = group_index / xsize_groups;
crop_group(GROUP_DIM, gx, gy, 0, 2, scratch)
},
);
(dc_group_lz, ac_group_lz)
};
let code = build_lz_pixel_code(
std::iter::once(global_lz.as_slice())
.chain(dc_group_lz.iter().map(Vec::as_slice))
.chain(ac_group_lz.iter().map(Vec::as_slice)),
nb,
min_symbol,
speed == crate::Speed::Slow,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
write_frame_header_modular(alpha.is_some(), GroupLayout::DEFAULT, writer);
let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();
sections[0].write(1, 1); sections[0].write(1, 1); write_local_tree_lz77(
&predictors,
&code,
min_symbol,
&mut scratch.huffman_pool,
&mut sections[0],
);
sections[0].write(1, 1); sections[0].write(1, 1); write_modular_transforms_rct_squeeze(&steps, &mut sections[0]);
write_lz_section(
&global_lz,
distance_ctx,
&code,
min_symbol,
&mut sections[0],
);
sections[0].zero_pad_to_byte();
write_lz_groups(
&dc_group_lz,
&[],
&code,
distance_ctx,
min_symbol,
WpParams::DEFAULT,
pool,
&mut sections[1..num_dc_groups + 1],
);
let ac_global_idx = 1 + num_dc_groups;
sections[ac_global_idx].write(1, 1);
sections[ac_global_idx].write(1, 1);
sections[ac_global_idx].zero_pad_to_byte();
write_lz_groups(
&ac_group_lz,
&[],
&code,
distance_ctx,
min_symbol,
WpParams::DEFAULT,
pool,
&mut sections[2 + num_dc_groups..],
);
writer.write(1, 0); writer.zero_pad_to_byte();
for s in §ions {
write_toc_entry(s.bits_written() / 8, writer);
}
writer.zero_pad_to_byte();
writer.append_byte_aligned(&mut sections);
writer.zero_pad_to_byte();
true
}
static SQUEEZE_XYB_QTABLE: [[f32; 16]; 3] = [
[
163.84, 81.92, 40.96, 20.48, 10.24, 5.12, 2.56, 1.28, 0.64, 0.32, 0.16, 0.08, 0.04, 0.02,
0.01, 0.005,
], [
1024.0, 512.0, 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5, 0.5, 0.5, 0.5, 0.5,
], [
2048.0, 1024.0, 512.0, 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5, 0.5, 0.5,
0.5,
], ];
static SQUEEZE_QUALITY_FACTOR_XYB: f32 = 4.8;
static LM_ROW_MUL: [f32; 3] = [1.265, 1.704, 0.546];
static LOSSY_MODULAR_XYB_SCALE: [f32; 3] = [65536.0, 4096.0, 4096.0];
fn lossy_modular_q(color: usize, hshift: i32, vshift: i32, distance: f32) -> i32 {
let mut shift = (hshift + vshift).min(16);
if shift > 0 {
shift -= 1;
}
let quantizer = 0.25f32 * distance;
let q = quantizer
* SQUEEZE_QUALITY_FACTOR_XYB
* SQUEEZE_XYB_QTABLE[color][shift as usize]
* LM_ROW_MUL[color];
(q as i32).max(1)
}
pub(crate) fn lm_calibrated_distance(d: f32) -> f32 {
static KNOTS: [(f32, f32); 4] = [(1.0, 1.255), (1.5, 1.532), (2.0, 1.888), (3.0, 2.238)];
let knots = KNOTS;
let k = if d <= knots[0].0 {
knots[0].1
} else if d >= knots[knots.len() - 1].0 {
knots[knots.len() - 1].1
} else {
let mut k = knots[0].1;
for w in knots.array_windows::<2>() {
let [(d0, k0), (d1, k1)] = [w[0], w[1]];
if d >= d0 && d <= d1 {
k = k0 + (k1 - k0) * (d - d0) / (d1 - d0);
break;
}
}
k
};
d * k
}
const LM_SAMPLE_TARGET: usize = 1 << 16;
const LM_BUDGET_DIV: usize = 1024;
const LM_BUDGET_CAP: usize = 256;
const LM_SAFE_PRED_MASK: u16 =
(1 << 0) | (1 << 1) | (1 << 2) | (1 << 4) | (1 << 5) | (1 << 7) | (1 << 8) | (1 << 9);
fn scale_ma_value_props(samples: &mut MaSamples, q: i32) {
if q <= 1 {
return;
}
for p in samples.props.iter_mut() {
for v in &mut p[4..=14] {
*v *= q;
}
}
}
#[allow(clippy::too_many_arguments)]
fn tokenize_channel_ma_mult<'a>(
get_row: impl Fn(usize) -> &'a [i32],
gw: usize,
gh: usize,
chan: u32,
q: i32,
tree: &LearnedTree,
leaf_ctx: &[u32],
base: u32,
out: &mut Vec<Token>,
) {
walk_channel_ma(
get_row,
gw,
gh,
chan,
0,
&[],
WpParams::DEFAULT,
false,
|_x, _y, v, p, n, _wp_pred| {
let (node, pred) = if q > 1 {
let mut sp = *p;
for s in &mut sp[4..=14] {
*s *= q;
}
tree.lookup(&sp)
} else {
tree.lookup(p)
};
let pv = predictor_value(pred, n, 0);
out.push(Token::new(
leaf_ctx[(base + node) as usize],
pack_signed((v - pv) as i32),
));
},
);
}
fn emit_lm_forest(
trees: &[LearnedTree],
tag_mults: &[u32],
split: usize,
n_dc_slots: usize,
num_dc_groups: usize,
) -> (Vec<Token>, Vec<u32>, Vec<u32>, u32) {
use std::collections::VecDeque;
let n_tags = trees.len();
let n_ac_slots = n_tags - split - n_dc_slots;
debug_assert!(split > 0 && n_ac_slots > 0);
let mut nodes: Vec<MaNode> = Vec::new();
let mut mult_of: Vec<u32> = Vec::new();
let mut tag_base = vec![0u32; n_tags];
let append_tree = |nodes: &mut Vec<MaNode>, mult_of: &mut Vec<u32>, t: usize| -> u32 {
let base = nodes.len() as u32;
for n in &trees[t].nodes {
nodes.push(match *n {
MaNode::Split { prop, val, gt, le } => MaNode::Split {
prop,
val,
gt: gt + base,
le: le + base,
},
MaNode::Leaf { pred } => MaNode::Leaf { pred },
});
mult_of.push(tag_mults[t]);
}
base
};
let build_chain = |nodes: &mut Vec<MaNode>,
mult_of: &mut Vec<u32>,
tag_base: &mut [u32],
lo: usize,
n: usize|
-> u32 {
let base0 = append_tree(nodes, mult_of, lo);
tag_base[lo] = base0;
let mut acc = base0;
for k in 1..n {
let sub = append_tree(nodes, mult_of, lo + k);
tag_base[lo + k] = sub;
nodes.push(MaNode::Split {
prop: 0,
val: (k - 1) as i32,
gt: sub,
le: acc,
});
mult_of.push(1);
acc = (nodes.len() - 1) as u32;
}
acc
};
let global_root = build_chain(&mut nodes, &mut mult_of, &mut tag_base, 0, split);
let ac_root = build_chain(
&mut nodes,
&mut mult_of,
&mut tag_base,
split + n_dc_slots,
n_ac_slots,
);
let low_root = if n_dc_slots > 0 {
let dc_root = build_chain(&mut nodes, &mut mult_of, &mut tag_base, split, n_dc_slots);
nodes.push(MaNode::Split {
prop: 1,
val: 0,
gt: dc_root,
le: global_root,
});
mult_of.push(1);
(nodes.len() - 1) as u32
} else {
global_root
};
nodes.push(MaNode::Split {
prop: 1,
val: (2 * num_dc_groups) as i32,
gt: ac_root,
le: low_root,
});
mult_of.push(1);
let root = (nodes.len() - 1) as u32;
let mut tokens = Vec::new();
let mut leaf_ctx = vec![u32::MAX; nodes.len()];
let mut queue: VecDeque<u32> = VecDeque::new();
queue.push_back(root);
let mut ctx = 0u32;
while let Some(i) = queue.pop_front() {
match nodes[i as usize] {
MaNode::Split { prop, val, gt, le } => {
push_split(&mut tokens, prop as u32, val);
queue.push_back(gt);
queue.push_back(le);
}
MaNode::Leaf { pred } => {
push_leaf_mul(&mut tokens, pred, mult_of[i as usize]);
leaf_ctx[i as usize] = ctx;
ctx += 1;
}
}
}
(tokens, leaf_ctx, tag_base, ctx)
}
fn quantize_channel_divide(ch: &mut crate::squeeze::Channel, q: i32) {
if q <= 1 {
return;
}
for v in ch.data.iter_mut() {
let a = *v;
*v = if a < 0 {
-((-a + q / 2) / q)
} else {
(a + q / 2) / q
};
}
}
fn write_frame_header_modular_xyb_regular(w: &mut BitWriter) {
w.write(1, 0); w.write(2, 0b00); w.write(1, 1); write_u64(0, w); w.write(2, 0b00); w.write(2, GroupLayout::DEFAULT.shift as u64);
w.write(2, 0b00); w.write(1, 0); w.write(2, 0b00); w.write(1, 1); w.write(2, 0b00); w.write(1, 0); w.write(1, 0); w.write(2, 0); w.write(2, 0b00); w.write(2, 0b00); }
fn write_lossy_modular_dc_quant(w: &mut BitWriter) {
use crate::util::f32_to_f16_bits;
w.write(1, 0); for scale in LOSSY_MODULAR_XYB_SCALE {
let step_x128 = 128.0 / scale;
w.write(16, f32_to_f16_bits(step_x128) as u64);
}
}
pub(crate) struct LmQuantized {
channels: Vec<crate::squeeze::Channel>,
quants: Vec<u32>,
steps: Vec<crate::squeeze::SqueezeStep>,
}
pub(crate) fn lm_quantize(
xyb: &crate::image::Image3F,
effective_distance: f32,
) -> Option<LmQuantized> {
use crate::squeeze::{Channel, apply_step_forward, default_squeeze_steps, lossy_squeeze_steps};
let xsize = xyb.xsize();
let ysize = xyb.ysize();
let num_c = 3usize;
if default_squeeze_steps(xsize, ysize, num_c).is_empty() {
return None;
}
let steps = lossy_squeeze_steps(xsize, ysize, num_c);
let chroma_pre = steps.len() >= 2 && !steps[0].in_place;
let n = xsize * ysize;
let input = [
&xyb.plane_data(0)[..n],
&xyb.plane_data(1)[..n],
&xyb.plane_data(2)[..n],
];
let mut planes = [vec![0i32; n], vec![0i32; n], vec![0i32; n]];
{
let [dst_y, dst_x, dst_b] = &mut planes;
let scales = [
LOSSY_MODULAR_XYB_SCALE[0],
LOSSY_MODULAR_XYB_SCALE[1],
LOSSY_MODULAR_XYB_SCALE[2],
];
unsafe {
crate::xyb::selected_quantize_xyb_channels_fn()(input, [dst_y, dst_x, dst_b], scales);
}
}
let mut channels: Vec<Channel> = planes
.into_iter()
.map(|data| Channel {
data,
w: xsize,
h: ysize,
hshift: 0,
vshift: 0,
})
.collect();
for s in &steps {
apply_step_forward(&mut channels, s);
}
let nb = channels.len();
let color_of = |i: usize| {
if chroma_pre && i >= nb - 4 {
1 } else {
i % num_c
}
};
let mut quants: Vec<u32> = Vec::with_capacity(nb);
for (i, ch) in channels.iter_mut().enumerate() {
let q = lossy_modular_q(color_of(i), ch.hshift, ch.vshift, effective_distance);
quantize_channel_divide(ch, q);
quants.push(q as u32);
}
Some(LmQuantized {
channels,
quants,
steps,
})
}
pub(crate) fn encode_frame_lossy_modular_squeeze(
xyb: &crate::image::Image3F,
effective_distance: f32,
speed: crate::Speed,
pool: &ThreadPool,
scratch: &mut CoderScratch,
writer: &mut BitWriter,
) -> bool {
let xsize = xyb.xsize();
let ysize = xyb.ysize();
let Some(LmQuantized {
channels,
quants,
steps,
}) = lm_quantize(xyb, effective_distance)
else {
return false;
};
let nb = channels.len();
let max_abs = channels
.iter()
.flat_map(|ch| ch.data.iter())
.map(|v| v.unsigned_abs())
.max()
.unwrap_or(0);
let value_bits = 33 - (2 * max_abs).max(1).leading_zeros();
let min_symbol = if value_bits <= 13 {
LZ77_MIN_SYMBOL
} else {
4 * value_bits + 24
};
let use_wp = true;
let grad_pack_fn = selected_grad_pack_interior_fn();
let xsize_groups = xsize.div_ceil(GROUP_DIM);
let ysize_groups = ysize.div_ceil(GROUP_DIM);
let num_ac_groups = xsize_groups * ysize_groups;
let xsize_dc_groups = xsize.div_ceil(LF_GROUP_DIM);
let ysize_dc_groups = ysize.div_ceil(LF_GROUP_DIM);
let num_dc_groups = xsize_dc_groups * ysize_dc_groups;
if num_ac_groups == 1 {
let predictors: Vec<u32> = if speed == crate::Speed::Slow {
let quants_ref = &quants;
pool.steal_map(scratch, nb, |c, _scratch| {
let ch = &channels[c];
let data = &ch.data;
let w = ch.w;
let get = move |gx: usize, gy: usize| data[gy * w + gx];
if quants_ref[c] > 1 {
let mut costs = SqueezePredictorCost::with_zero();
costs.add_crop(get, ch.w, ch.h, use_wp);
costs.safe_predictor()
} else {
choose_predictor_for_plane(get, ch.w, ch.h, use_wp)
}
})
} else {
(0..nb)
.map(|i| {
if quants[i] > 1 {
PREDICTOR_GRADIENT
} else {
fixed_predictor(use_wp)
}
})
.collect()
};
let channel_tokens = pool.steal_map(scratch, nb, |c, scratch| {
let ch = &channels[c];
let mut tokens = Vec::with_capacity(ch.w * ch.h);
tokenize_plane_rows(
channel_to_context(c, nb),
|y| &ch.data[y * ch.w..][..ch.w],
ch.w,
ch.h,
predictors[c],
grad_pack_fn,
&mut scratch.gradient,
&mut tokens,
);
tokens
});
let mut tokens: Vec<Token> = Vec::new();
for channel in channel_tokens {
tokens.extend(channel);
}
write_frame_header_modular_xyb_regular(writer);
let mut section = BitWriter::new();
write_lossy_modular_dc_quant(&mut section);
section.write(1, 0); section.write(1, 0); section.write(1, 1); write_modular_transforms_squeeze_only(&steps, &mut section);
let distance_ctx = nb as u32;
let lz_tokens = lz77_compress_for_speed(&tokens, distance_ctx, speed, scratch);
let code = build_lz_pixel_code(
std::iter::once(lz_tokens.as_slice()),
nb,
min_symbol,
speed == crate::Speed::Slow,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
let leaves: Vec<(u32, u32)> = predictors.iter().copied().zip(quants).collect();
write_tree_lz77(
&build_balanced_tree_tokens_mul(&leaves),
&code,
min_symbol,
&mut scratch.huffman_pool,
&mut section,
);
write_lz_section(&lz_tokens, distance_ctx, &code, min_symbol, &mut section);
section.zero_pad_to_byte();
writer.write(1, 0); writer.zero_pad_to_byte();
write_toc_entry(section.bits_written() / 8, writer);
writer.zero_pad_to_byte();
writer.append_byte_aligned(std::slice::from_mut(&mut section));
writer.zero_pad_to_byte();
return true;
}
let split = channels
.iter()
.position(|c| c.w > GROUP_DIM || c.h > GROUP_DIM)
.unwrap_or(nb);
let dc_slot_channels: Vec<usize> = (split..nb)
.filter(|&c| channels[c].hshift.min(channels[c].vshift) >= 3)
.collect();
let ac_slot_channels: Vec<usize> = (split..nb)
.filter(|&c| {
let msh = channels[c].hshift.min(channels[c].vshift);
(0..=2).contains(&msh)
})
.collect();
let n_dc_slots = dc_slot_channels.len();
let n_ac_slots = ac_slot_channels.len();
let _ = n_ac_slots;
let n_tags = split + n_dc_slots + n_ac_slots;
let dc_tag_base = split;
let ac_tag_base = split + n_dc_slots;
let tag_channel: Vec<usize> = (0..split)
.chain(dc_slot_channels.iter().copied())
.chain(ac_slot_channels.iter().copied())
.collect();
let tag_quants: Vec<u32> = tag_channel.iter().map(|&c| quants[c]).collect();
let learned = {
let tag_samples: Vec<MaSamples> = pool.steal_map(scratch, n_tags, |t, _scratch| {
let ch = &channels[tag_channel[t]];
let px = ch.w * ch.h;
let mut stride = px.div_ceil(LM_SAMPLE_TARGET).max(1);
if stride > 1 && stride.is_multiple_of(2) {
stride += 1;
}
let chan = if t < split {
t
} else if t < ac_tag_base {
t - dc_tag_base
} else {
t - ac_tag_base
} as u32;
let mut samples = MaSamples::with_capacity(px / stride + 1);
let data = &ch.data;
let w = ch.w;
sample_channel_ma(
|y| &data[y * w..][..w],
ch.w,
ch.h,
chan,
0,
&[],
WpParams::DEFAULT,
false,
stride,
|props, tok| samples.push(props, tok),
);
scale_ma_value_props(&mut samples, tag_quants[t] as i32);
samples
});
let budgets: Vec<usize> = (0..n_tags)
.map(|t| {
let ch = &channels[tag_channel[t]];
((ch.w * ch.h) / LM_BUDGET_DIV).clamp(1, LM_BUDGET_CAP)
})
.collect();
let budget_cap = LM_MAX_CONTEXTS - 64;
let mut learned: Vec<LearnedTree> = Vec::with_capacity(n_tags);
for t in 0..n_tags {
let ch = &channels[tag_channel[t]];
let px = ch.w * ch.h;
let mut stride = px.div_ceil(LM_SAMPLE_TARGET).max(1);
if stride > 1 && stride.is_multiple_of(2) {
stride += 1;
}
let params = |max_leaves: usize| MaLearnParams {
alphabet: min_symbol as usize,
max_leaves,
split_cost_bits: MA_SPLIT_COST_BITS / stride as f32,
min_node: MA_MIN_NODE_SAMPLES,
allow_wp: false,
allowed_preds: LM_SAFE_PRED_MASK,
side_preds: 1,
max_candidates: MA_PHOTO_CANDIDATES,
};
let samples = &tag_samples[t];
if samples.len() < 4 * MA_MIN_NODE_SAMPLES {
learned.push(learn_ma_tree(samples, params(1), pool, scratch));
continue;
}
let budget = budgets[t];
let tree = learn_ma_tree(samples, params(budget), pool, scratch);
let n_leaves = tree.nodes.len().div_ceil(2);
let overhead_bits = tree.nodes.len() as f64 * 10.0 + n_leaves as f64 * 200.0;
let est_real = tree.est_bits * stride as f64 + overhead_bits;
let flat_real = tree.flat_bits * stride as f64;
if est_real < flat_real {
learned.push(tree);
} else {
learned.push(learn_ma_tree(samples, params(1), pool, scratch));
}
}
let leaves_of = |t: &LearnedTree| t.nodes.len().div_ceil(2);
let mut total_leaves: usize = learned.iter().map(leaves_of).sum();
while total_leaves > budget_cap {
let (worst, _) = learned
.iter()
.enumerate()
.max_by_key(|(_, t)| leaves_of(t))
.unwrap();
let target = (leaves_of(&learned[worst]) / 2).max(1);
let ch = &channels[tag_channel[worst]];
let px = ch.w * ch.h;
let mut stride = px.div_ceil(LM_SAMPLE_TARGET).max(1);
if stride > 1 && stride.is_multiple_of(2) {
stride += 1;
}
let params = MaLearnParams {
alphabet: min_symbol as usize,
max_leaves: target,
split_cost_bits: MA_SPLIT_COST_BITS / stride as f32,
min_node: MA_MIN_NODE_SAMPLES,
allow_wp: false,
allowed_preds: LM_SAFE_PRED_MASK,
side_preds: 1,
max_candidates: MA_PHOTO_CANDIDATES,
};
learned[worst] = learn_ma_tree(&tag_samples[worst], params, pool, scratch);
total_leaves = learned.iter().map(leaves_of).sum();
}
learned
};
let (tree_tokens, leaf_ctx, tag_base, num_ctx) =
emit_lm_forest(&learned, &tag_quants, split, n_dc_slots, num_dc_groups);
let num_ctx = num_ctx as usize;
let distance_ctx = num_ctx as u32;
let global_channel_tokens = pool.steal_map(scratch, split, |c, _scratch| {
let ch = &channels[c];
let data = &ch.data;
let w = ch.w;
let get_row = |y| &data[y * w..][..w];
let mut tokens = Vec::with_capacity(ch.w * ch.h);
tokenize_channel_ma_mult(
get_row,
ch.w,
ch.h,
c as u32,
tag_quants[c] as i32,
&learned[c],
&leaf_ctx,
tag_base[c],
&mut tokens,
);
tokens
});
let mut global_tokens: Vec<Token> = Vec::new();
for channel in global_channel_tokens {
global_tokens.extend(channel);
}
let global_lz = lz77_literals(&global_tokens);
let crop_group = |gdim: usize,
gx: usize,
gy: usize,
minsh: i32,
maxsh: i32,
slot_tag_base: usize,
_scratch: &mut CoderScratch|
-> Vec<LzToken> {
let mut gtok: Vec<Token> = Vec::new();
for_each_squeeze_group_crop(
&channels,
split,
gdim,
gx,
gy,
minsh,
maxsh,
|within, c, rx0, ry0, rw, rh| {
let ch = &channels[c];
let tag = slot_tag_base + within;
let data = &ch.data;
let w = ch.w;
let get_row = |y| &data[(ry0 + y) * w + rx0..][..rw];
tokenize_channel_ma_mult(
get_row,
rw,
rh,
within as u32,
tag_quants[tag] as i32,
&learned[tag],
&leaf_ctx,
tag_base[tag],
&mut gtok,
);
},
);
lz77_literals(>ok)
};
let dc_group_lz = pool.steal_map_with_threads(
scratch,
num_dc_groups,
group_lz_threads(speed, pool),
|group_index, scratch| {
let gx = group_index % xsize_dc_groups;
let gy = group_index / xsize_dc_groups;
crop_group(LF_GROUP_DIM, gx, gy, 3, 1000, dc_tag_base, scratch)
},
);
let ac_group_lz = pool.steal_map_with_threads(
scratch,
num_ac_groups,
group_lz_threads(speed, pool),
|group_index, scratch| {
let gx = group_index % xsize_groups;
let gy = group_index / xsize_groups;
crop_group(GROUP_DIM, gx, gy, 0, 2, ac_tag_base, scratch)
},
);
let code = build_lz_pixel_code_opts(
std::iter::once(global_lz.as_slice())
.chain(dc_group_lz.iter().map(Vec::as_slice))
.chain(ac_group_lz.iter().map(Vec::as_slice)),
num_ctx,
min_symbol,
speed == crate::Speed::Slow,
true,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
write_frame_header_modular_xyb_regular(writer);
let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();
write_lossy_modular_dc_quant(&mut sections[0]);
sections[0].write(1, 1); write_tree_lz77(
&tree_tokens,
&code,
min_symbol,
&mut scratch.huffman_pool,
&mut sections[0],
);
sections[0].write(1, 1); sections[0].write(1, 1); write_modular_transforms_squeeze_only(&steps, &mut sections[0]);
write_lz_section(
&global_lz,
distance_ctx,
&code,
min_symbol,
&mut sections[0],
);
sections[0].zero_pad_to_byte();
write_lz_groups(
&dc_group_lz,
&[],
&code,
distance_ctx,
min_symbol,
WpParams::DEFAULT,
pool,
&mut sections[1..num_dc_groups + 1],
);
let ac_global_idx = 1 + num_dc_groups;
sections[ac_global_idx].write(1, 1);
sections[ac_global_idx].write(1, 1);
sections[ac_global_idx].zero_pad_to_byte();
write_lz_groups(
&ac_group_lz,
&[],
&code,
distance_ctx,
min_symbol,
WpParams::DEFAULT,
pool,
&mut sections[2 + num_dc_groups..],
);
writer.write(1, 0); writer.zero_pad_to_byte();
for s in §ions {
write_toc_entry(s.bits_written() / 8, writer);
}
writer.zero_pad_to_byte();
writer.append_byte_aligned(&mut sections);
writer.zero_pad_to_byte();
true
}