use super::lz77::{
ConstantTail, LZ77_MIN_SYMBOL, LzToken, RunLzWriter, build_lz_pixel_code,
write_local_tree_lz77, write_lz_section, write_lz_section_tail,
};
use super::predictor::PREDICTOR_GRADIENT;
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::entropy::{Token, uint_encode};
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::{WpParams, write_wp_header};
use std::sync::{Condvar, Mutex};
pub(super) const SLOW_DEEP_LZ_MAX_THREADS: usize = 8;
#[inline]
pub(super) fn group_lz_threads(speed: crate::Speed, pool: &ThreadPool) -> usize {
if speed.effort().lz_deep {
pool.num_threads().min(SLOW_DEEP_LZ_MAX_THREADS)
} else {
pool.num_threads()
}
}
pub(super) struct DeepLzScratchPool {
available: Mutex<Vec<Vec<u32>>>,
activity: Condvar,
}
impl DeepLzScratchPool {
pub(super) fn new(slots: usize) -> Self {
Self {
available: Mutex::new((0..slots.max(1)).map(|_| Vec::new()).collect()),
activity: Condvar::new(),
}
}
pub(super) fn with_depth<T>(&self, f: impl FnOnce(&mut Vec<u32>) -> T) -> T {
let mut guard = self.acquire();
f(guard.depth.as_mut().unwrap())
}
fn acquire(&self) -> DeepLzScratchGuard<'_> {
let depth = {
let mut available = self.available.lock().unwrap();
while available.is_empty() {
available = self.activity.wait(available).unwrap();
}
available.pop().unwrap()
};
DeepLzScratchGuard {
owner: self,
depth: Some(depth),
}
}
}
struct DeepLzScratchGuard<'a> {
owner: &'a DeepLzScratchPool,
depth: Option<Vec<u32>>,
}
impl Drop for DeepLzScratchGuard<'_> {
fn drop(&mut self) {
let depth = self.depth.take().unwrap();
self.owner.available.lock().unwrap().push(depth);
self.owner.activity.notify_one();
}
}
pub(crate) fn write_single_channel_lz77_section(
tokens: &[Token],
scratch: &mut CoderScratch,
w: &mut BitWriter,
) -> bool {
let max_literal = tokens
.iter()
.map(|t| uint_encode(t.value).0)
.max()
.unwrap_or(0);
let min_symbol = LZ77_MIN_SYMBOL.max(max_literal + 1);
if min_symbol > super::lossless_min_symbol(18) {
return false;
}
let mut runs = RunLzWriter::with_capacity(tokens.len());
for t in tokens {
runs.push(Token::new(0, t.value));
}
let lz = runs.finish();
let code = build_lz_pixel_code(
false,
std::iter::once(lz.as_slice()),
1,
min_symbol,
true,
&mut scratch.lz_entropy,
&mut scratch.huffman_pool,
);
w.write(1, 0);
w.write(1, 1);
w.write(2, 0);
write_local_tree_lz77(
&[PREDICTOR_GRADIENT],
&code,
min_symbol,
&mut scratch.huffman_pool,
w,
);
write_lz_section(&lz, 1, &code, min_symbol, w);
true
}
#[allow(clippy::too_many_arguments)]
pub(super) fn write_lz_groups(
groups: &[Vec<LzToken>],
tails: &[ConstantTail],
code: &crate::entropy::EntropyCode<'_>,
distance_context: u32,
min_symbol: u32,
wp_params: WpParams,
pool: &ThreadPool,
sections: &mut [BitWriter],
) {
write_lz_groups_with_header(
groups,
tails,
code,
distance_context,
min_symbol,
pool,
sections,
|_, section| {
section.write(1, 1);
write_wp_header(wp_params, section);
section.write(2, 0);
},
);
}
#[allow(clippy::too_many_arguments)]
pub(super) fn write_lz_groups_with_header(
groups: &[Vec<LzToken>],
tails: &[ConstantTail],
code: &crate::entropy::EntropyCode<'_>,
distance_context: u32,
min_symbol: u32,
pool: &ThreadPool,
sections: &mut [BitWriter],
write_header: impl Fn(usize, &mut BitWriter) + Sync,
) {
debug_assert_eq!(groups.len(), sections.len());
debug_assert!(tails.is_empty() || tails.len() == groups.len());
let max_staging = if code.use_prefix_code {
0
} else {
groups
.iter()
.map(|g| g.len().saturating_mul(8))
.max()
.unwrap_or(0)
};
#[allow(clippy::manual_clamp)]
let lanes = (32 * 1024 * 1024 / max_staging.max(1))
.max(1)
.min(SLOW_DEEP_LZ_MAX_THREADS)
.min(pool.num_threads())
.min(sections.len());
let write_group = |i: usize, section: &mut BitWriter| {
write_header(i, section);
let tail = tails.get(i).copied().unwrap_or(ConstantTail::NONE);
write_lz_section_tail(
&groups[i],
tail,
distance_context,
code,
min_symbol,
section,
);
section.zero_pad_to_byte();
};
if lanes <= 1 {
for (i, section) in sections.iter_mut().enumerate() {
write_group(i, section);
}
return;
}
let mut writer_scratch = CoderScratch::lossless();
pool.steal_for_each_mut_with_threads(&mut writer_scratch, sections, lanes, |i, section, _| {
write_group(i, section);
});
}