mod plane;
pub(crate) use plane::{GlobalPlaneTree, PlaneTreeBudget, StreamCrop, learn_global_plane_tree};
use super::groups::{DeepLzScratchPool, group_lz_threads, write_lz_groups};
use super::header::{write_frame_header_modular_kind, write_toc_entry};
use super::lz77::{
LzToken, build_lz_pixel_code, lz77_compress_for_speed, lz77_compress_for_speed_with_depth,
write_local_tree_lz77, write_lz_section,
};
use super::predictor::{
GradPackInteriorFn, SqueezePredictorCost, channel_to_context, choose_predictor_for_rows,
fixed_predictor, tokenize_plane_rows,
};
use super::rct::write_rct_transform;
use super::{GROUP_DIM, GroupLayout, LF_GROUP_DIM};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::encode_image::AlphaPlane;
use crate::entropy::Token;
use crate::image::Image3Si;
use crate::patches::ModularFrameKind;
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::WpParams;
pub(crate) 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);
}
pub(crate) fn write_modular_transforms_squeeze_only(
steps: &[crate::squeeze::SqueezeStep],
w: &mut BitWriter,
) {
w.write(2, 0b01); write_squeeze_transform_entry(steps, w);
}
pub(crate) fn write_modular_transforms_squeeze_palette(
rct: bool,
steps: &[crate::squeeze::SqueezeStep],
begin_c: u32,
nb_colors: u32,
w: &mut BitWriter,
) {
w.write(2, 0b10);
w.write(4, u64::from(rct));
if rct {
write_rct_transform(6, w);
}
write_squeeze_transform_entry(steps, w);
super::palette::write_palette_transform_entry(begin_c, 1, nb_colors, 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,
frame_kind: ModularFrameKind<'_>,
layout: GroupLayout,
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.effort().predictor_search {
pool.steal_map(scratch, channels.len(), |c, _scratch| {
let ch = &channels[c];
let data = &ch.data;
let w = ch.w;
choose_predictor_for_rows(|y| &data[y * w..][..w], 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_kind(alpha.is_some(), frame_kind, layout, 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(
speed.effort().lz_exhaustive_entropy(),
std::iter::once(lz_tokens.as_slice()),
nb,
min_symbol,
speed.effort().lz_refined_entropy,
&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)]
pub(crate) 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,
frame_kind: ModularFrameKind<'_>,
layout: GroupLayout,
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 {
channels.push(Channel {
data: a.to_i32_vec(xsize * ysize),
w: xsize,
h: ysize,
hshift: 0,
vshift: 0,
});
}
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.effort().predictor_search {
let mut costs: Vec<SqueezePredictorCost> =
(0..nb).map(|_| SqueezePredictorCost::default()).collect();
for (cost, ch) in costs[..split].iter_mut().zip(&channels[..split]) {
cost.add_rows(|y| &ch.data[y * ch.w..][..ch.w], 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_rows(
|y| &ch.data[(y0 + y) * ch.w + x0..][..w],
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_rows(
|y| &ch.data[(y0 + y) * ch.w + x0..][..w],
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
.effort()
.lz_deep
.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(
speed.effort().lz_exhaustive_entropy(),
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.effort().lz_refined_entropy,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
write_frame_header_modular_kind(alpha.is_some(), frame_kind, layout, 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
}