use super::cache::{CacheKey, TreeCache};
use super::forest::emit_forest;
use super::pyramid::Pyramid;
use super::tags::{Grid, References, Tags};
use super::tokens::ForestContexts;
use super::{single, tokens, trees, write};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::lossless::kit::{LZ77_MIN_SYMBOL, build_lz_pixel_code_ans};
use crate::thread_pool::ThreadPool;
pub(super) fn write_frame(
p: &Pyramid,
speed: crate::Speed,
pool: &ThreadPool,
scratch: &mut CoderScratch,
writer: &mut BitWriter,
cache: Option<&mut TreeCache>,
) {
let min_symbol = min_symbol(p);
let grid = Grid::new(p);
if grid.num_ac_groups == 1 && !p.dc_frame {
if speed < crate::Speed::Fast {
single::write_frame(p, min_symbol, speed, pool, scratch, writer);
return;
}
if speed >= crate::Speed::Slow {
let mut single_writer = writer.clone();
single::write_frame(p, min_symbol, speed, pool, scratch, &mut single_writer);
write_grouped(p, speed, pool, scratch, writer, cache, min_symbol, &grid);
if single_writer.bits_written() <= writer.bits_written() {
*writer = single_writer;
}
return;
}
}
write_grouped(p, speed, pool, scratch, writer, cache, min_symbol, &grid);
}
#[allow(clippy::too_many_arguments)]
fn write_grouped(
p: &Pyramid,
speed: crate::Speed,
pool: &ThreadPool,
scratch: &mut CoderScratch,
writer: &mut BitWriter,
mut cache: Option<&mut TreeCache>,
min_symbol: u32,
grid: &Grid,
) {
let tags = Tags::new(p, grid);
let refs = References::new(p, tags.split);
let keys: Vec<Option<CacheKey>> = (0..tags.len())
.map(|t| {
cache
.as_deref()
.and_then(|cache| cache.key(p, tags.channel[t], tags.stream(t)))
})
.collect();
let trees = trees::learn(
p,
&tags,
&refs,
&keys,
min_symbol,
pool,
scratch,
cache.as_deref_mut(),
);
let (tree_tokens, leaf_ctx, tag_base, num_ctx) = emit_forest(
&trees,
&tags.quants,
tags.split,
tags.n_dc_slots,
grid.num_dc_groups,
);
let num_ctx = num_ctx as usize;
let distance_ctx = num_ctx as u32;
if let Some(cache) = cache.as_deref_mut() {
cache.reserve_frame(p, &tags, &keys);
}
let (streams, pending) = tokens::tokenize(
p,
&tags,
&refs,
&trees,
ForestContexts {
leaf_ctx: &leaf_ctx,
tag_base: &tag_base,
},
&keys,
cache.as_deref(),
speed,
pool,
scratch,
);
if let Some(cache) = cache {
for block in pending {
cache.retain(block);
}
}
let dictionary = p.patch_dictionary(scratch);
let code = build_lz_pixel_code_ans(
speed.effort().lz_exhaustive_entropy(),
std::iter::once(streams.global.as_slice())
.chain(streams.dc_groups.iter().map(Vec::as_slice))
.chain(streams.ac_groups.iter().map(Vec::as_slice)),
num_ctx,
min_symbol,
speed.effort().lz_refined_entropy,
true,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
write::write_frame(
p,
&tree_tokens,
&code,
min_symbol,
distance_ctx,
&streams,
dictionary,
pool,
&mut scratch.huffman_pool,
writer,
);
}
fn min_symbol(p: &Pyramid) -> u32 {
let max_abs = p
.channels
.iter()
.flat_map(|ch| ch.data.iter())
.map(|v| v.unsigned_abs())
.max()
.unwrap_or(0);
let value_bits = 33 - max_abs.saturating_mul(2).max(1).leading_zeros();
if value_bits <= 13 {
LZ77_MIN_SYMBOL
} else {
4 * value_bits + 24
}
}