use super::frame::keep_smaller_writer;
use super::groups::{DeepLzScratchPool, group_lz_threads};
use super::lz77::{
ConstantTail, LzToken, build_lz_pixel_code_tails, estimate_coded_bits,
estimate_literal_and_run_bits_single, lz77_compress_for_speed_with_depth, lz77_run_count,
write_lz_section_tail, write_tree_lz77,
};
use super::ma_encoding::tokenize_ma_rect;
use super::ma_learning::{finish_ma_candidate, ma_leaf_price, ma_learn_params, with_ma_prune};
use super::ma_sampling::{ma_channel_sample_count, sample_channel_ma};
use super::ma_source::{
LearnedCandidate, MA_MAX_LEAVES, MA_MIN_NODE_SAMPLES, MaPlacement, MaSource, MaTransform,
ac_stream_id, with_ma_pixels,
};
use super::ma_streams::{
LZ_LITERAL_ALTERNATIVE_MAX_RATIO, VARIANT_WRITE_TOLERANCE, priced_tree_streams,
};
use super::tokens::RawTokens;
use super::{GroupLayout, lz77};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::entropy::Token;
use crate::ma_tree::{MaSamples, learn_ma_tree_in_place};
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::{WpParams, write_wp_header};
const LOCAL_TREE_TARGET_SAMPLES: usize = 1 << 20;
const LOCAL_TREE_LEARN_LANES: usize = 4;
pub(super) const LOCAL_TREE_MIN_GLOBAL_LEAVES: usize = MA_MAX_LEAVES / 2;
pub(super) const LOCAL_TREE_MAX_BITS_PER_VALUE: f64 = 0.5;
#[allow(clippy::too_many_arguments)]
pub(super) fn write_local_tree_sections(
speed: crate::Speed,
source: &MaSource<'_>,
placement: &[MaPlacement],
xsize: usize,
ysize: usize,
layout: GroupLayout,
num_dc_groups: usize,
min_symbol: u32,
wp_params: WpParams,
use_wp: bool,
dictionary: Option<&BitWriter>,
pool: &ThreadPool,
scratch: &mut CoderScratch,
) -> Option<Vec<BitWriter>> {
let gdim = layout.dim();
let xsize_groups = xsize.div_ceil(gdim);
let ysize_groups = ysize.div_ceil(gdim);
let num_ac_groups = xsize_groups * ysize_groups;
if num_ac_groups < 2 || placement.iter().any(|p| p.global) {
return None;
}
let target = LOCAL_TREE_TARGET_SAMPLES;
let max_candidates = source.max_candidates();
let leaf_offsets = matches!(source.transform, MaTransform::Rct(_));
let group_rect = |group_index: usize| {
let x0 = (group_index % xsize_groups) * gdim;
let y0 = (group_index / xsize_groups) * gdim;
(x0, y0)
};
let cands: Vec<LearnedCandidate> = pool.steal_map_with_threads(
scratch,
num_ac_groups,
LOCAL_TREE_LEARN_LANES,
|group_index, scratch| {
let (x0, y0) = group_rect(group_index);
let stream = ac_stream_id(num_dc_groups, group_index);
let sampled = || {
placement
.iter()
.filter(|p| source.channels[p.channel].constant.is_none())
};
let group_values: usize = sampled()
.map(|p| {
let ch = &source.channels[p.channel];
gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
})
.sum();
let mut stride = group_values.div_ceil(target).max(1);
if stride > 1 && stride.is_multiple_of(2) {
stride += 1;
}
let capacity = sampled()
.map(|p| {
let ch = &source.channels[p.channel];
ma_channel_sample_count(gdim.min(ch.w - x0), gdim.min(ch.h - y0), stride)
})
.sum();
let mut samples = MaSamples::with_capacity(capacity);
for p in sampled() {
let ch = &source.channels[p.channel];
let gw = gdim.min(ch.w - x0);
let gh = gdim.min(ch.h - y0);
let refs = source.ref_planes(placement, p, x0, y0);
with_ma_pixels!(ch, |pixels| sample_channel_ma(
|y| &pixels[(y0 + y) * ch.w + x0..][..gw],
gw,
gh,
p.chan_id,
stream,
&refs,
wp_params,
use_wp,
stride,
|props, tok, val| samples.push_val(props, tok, val),
));
}
let max_leaves = if samples.len() < 4 * MA_MIN_NODE_SAMPLES {
1
} else {
source.max_leaves()
};
let offsets = (leaf_offsets && max_leaves > 1).then(|| MaSamples {
props: samples.props.sampled(4),
tok: samples.tok.iter().step_by(4).copied().collect(),
val: Vec::new(),
});
let params = with_ma_prune(
ma_learn_params(
min_symbol,
max_leaves,
max_candidates,
stride as f64,
use_wp,
!leaf_offsets,
),
stride as f64,
);
let leaf_price = ma_leaf_price(¶ms, stride as f64);
let tree = learn_ma_tree_in_place(&mut samples, params, pool, scratch);
drop(samples);
let mut cand = finish_ma_candidate(tree, stride as f64, leaf_price);
if let Some(offsets) = offsets {
cand.set_leaf_offsets(&offsets, 1);
}
cand
},
);
let constant = source.constant_placement(placement);
let cands: Vec<LearnedCandidate> = match constant {
Some((p, value)) => cands
.iter()
.map(|cand| cand.with_constant_channel(p.chan_id, value))
.collect(),
None => cands,
};
let cands_ref = &cands;
let deep_lz_max_ratio = source.deep_lz_max_ratio();
let deep_lz = DeepLzScratchPool::new(group_lz_threads(speed, pool));
let group_sections: Vec<BitWriter> = pool.steal_map_with_threads(
scratch,
num_ac_groups,
group_lz_threads(speed, pool),
|group_index, scratch| {
let (x0, y0) = group_rect(group_index);
let num_values = placement
.iter()
.map(|p| {
let ch = &source.channels[p.channel];
gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
})
.sum();
let mut toks = RawTokens::with_capacity(num_values);
let mut tail = ConstantTail::NONE;
for p in placement {
let ch = &source.channels[p.channel];
if ch.constant.is_some() {
tail = cands_ref[group_index]
.constant_tail(gdim.min(ch.w - x0), gdim.min(ch.h - y0));
continue;
}
tokenize_ma_rect(
source,
placement,
p,
ac_stream_id(num_dc_groups, group_index),
x0,
y0,
gdim.min(ch.w - x0),
gdim.min(ch.h - y0),
&cands_ref[group_index],
wp_params,
use_wp,
&mut toks,
);
}
let cand = &cands_ref[group_index];
match &toks {
RawTokens::Compact(tokens) => write_local_tree_group(
speed,
tokens,
tail,
cand,
min_symbol,
wp_params,
deep_lz_max_ratio,
&deep_lz,
scratch,
),
RawTokens::Wide(tokens) => write_local_tree_group(
speed,
tokens,
tail,
cand,
min_symbol,
wp_params,
deep_lz_max_ratio,
&deep_lz,
scratch,
),
}
},
);
let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();
if let Some(dictionary) = dictionary {
sections[0].append(dictionary);
}
sections[0].write(1, 1); sections[0].write(1, 0); sections[0].write(1, 0); write_wp_header(wp_params, &mut sections[0]);
source.write_transforms(&mut sections[0]);
let empty_code = crate::modular::build_pixel_code(&[Token::new(0, 0)], scratch);
crate::modular::write_tree_and_pixel_histograms(&empty_code, scratch, &mut sections[0]);
sections[0].zero_pad_to_byte();
for section in sections[1..num_dc_groups + 2].iter_mut() {
section.write(1, 0); write_wp_header(wp_params, section);
section.write(2, 0); section.zero_pad_to_byte();
}
for (group_index, section) in group_sections.into_iter().enumerate() {
sections[2 + num_dc_groups + group_index] = section;
}
Some(sections)
}
fn write_local_tree_group<T: lz77::LiteralToken>(
speed: crate::Speed,
tokens: &[T],
tail: ConstantTail,
cand: &LearnedCandidate,
min_symbol: u32,
wp_params: WpParams,
deep_lz_max_ratio: f64,
deep_lz: &DeepLzScratchPool,
scratch: &mut CoderScratch,
) -> BitWriter {
let num_ctx = cand.num_ctx;
let distance_ctx = num_ctx;
let run_count = lz77_run_count(tokens);
let mut best: Option<BitWriter> = None;
let mut best_estimate = f64::INFINITY;
let deep = |scratch: &mut CoderScratch| -> Vec<LzToken> {
deep_lz.with_depth(|depth| {
lz77_compress_for_speed_with_depth(
tokens,
distance_ctx,
speed,
depth,
Some(run_count),
scratch,
)
})
};
if (run_count as f64) >= LZ_LITERAL_ALTERNATIVE_MAX_RATIO * tokens.len() as f64 {
let lz = deep(scratch);
write_local_tree_variant(
speed,
&lz,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
} else {
let (e_lit, e_run) =
estimate_literal_and_run_bits_single(tokens, num_ctx as usize + 1, min_symbol);
if e_run > e_lit * deep_lz_max_ratio {
write_local_tree_variant(
speed,
tokens,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
} else if e_run < e_lit {
let lz = deep(scratch);
write_local_tree_variant(
speed,
&lz,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
write_local_tree_variant(
speed,
tokens,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
} else {
write_local_tree_variant(
speed,
tokens,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
let lz = deep(scratch);
write_local_tree_variant(
speed,
&lz,
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
}
}
if speed.effort().lz_priced_search() {
let (e_lit, _) =
estimate_literal_and_run_bits_single(tokens, num_ctx as usize + 1, min_symbol);
if let Some(streams) = priced_tree_streams(
speed,
&[tokens],
e_lit,
num_ctx as usize + 1,
min_symbol,
&ThreadPool::new_lossless(1),
scratch,
) {
write_local_tree_variant(
speed,
&streams[0],
tail,
cand,
min_symbol,
wp_params,
scratch,
&mut best,
&mut best_estimate,
);
}
}
best.expect("local-tree group section")
}
fn write_local_tree_variant<T: lz77::LzTokenSource>(
speed: crate::Speed,
stream: &[T],
tail: ConstantTail,
cand: &LearnedCandidate,
min_symbol: u32,
wp_params: WpParams,
scratch: &mut CoderScratch,
best: &mut Option<BitWriter>,
best_estimate: &mut f64,
) {
let num_ctx = cand.num_ctx;
let distance_ctx = num_ctx;
let code = build_lz_pixel_code_tails(
speed.effort().lz_exhaustive_entropy(),
&[stream],
&[tail],
num_ctx as usize,
min_symbol,
true,
true,
None,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
let estimate = estimate_coded_bits(&[stream], distance_ctx, &code, min_symbol, None);
if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
return;
}
*best_estimate = best_estimate.min(estimate);
let mut section = BitWriter::new();
section.write(1, 0); write_wp_header(wp_params, &mut section);
section.write(2, 0); write_tree_lz77(
&cand.tree_tokens,
&code,
min_symbol,
&mut scratch.huffman_pool,
&mut section,
);
write_lz_section_tail(stream, tail, distance_ctx, &code, min_symbol, &mut section);
section.zero_pad_to_byte();
keep_smaller_writer(best, section);
}