use super::compress::{
BLOCK_HEADER_SIZE, FrameCompressor, LONGNBSEQ, RLE_MAX_LENGTH, SeqChunk, SeqStats,
build_sequences_statistics, entropy_compress_seq_store, is_rle,
};
use super::fse_enc::FseCTable;
use super::hist::{hist_count, hist_count_codes};
use super::huf_enc::{
HUF_FLAGS_OPTIMAL_DEPTH, HufCTable, HufRepeat, huf_build_ctable, huf_estimate_compressed_size,
huf_optimal_table_log, huf_validate_ctable, huf_write_ctable,
};
use super::literals::{HufCTables, STRAT_BTULTRA};
use super::seq_store::{
LongLengthType, MINMATCH, REP_NUM, SeqStore, offbase_is_repcode, update_rep,
};
use super::sequences::{
COST_ERROR, FseRepeat, SET_BASIC, SET_COMPRESSED, SET_REPEAT, SET_RLE, cross_entropy_cost,
fse_bit_cost,
};
use crate::error::{Error, Result};
use crate::fse::{
LL_BITS, LL_DEFAULT_NORM, MAX_LL, MAX_ML, MAX_OFF, ML_BITS, ML_DEFAULT_NORM, OF_DEFAULT_NORM,
};
const MIN_SEQUENCES_BLOCK_SPLITTING: usize = 300;
const MAX_NB_BLOCK_SPLITS: usize = 196;
const MAX_HUF_HEADER_SIZE: usize = 128;
const MAX_FSE_HEADERS_SIZE: usize =
((MAX_ML + 1) * 9 + (MAX_LL + 1) * 9 + (MAX_OFF + 1) * 8).div_ceil(8);
const LIT_HUF_LOG: u32 = 11;
const COMPRESS_LITERALS_SIZE_MIN: usize = 63;
fn count_literals_bytes(ss: &SeqStore, ch: &SeqChunk) -> usize {
let mut n = 0usize;
for (i, s) in ss.sequences[ch.seq_start..ch.seq_end].iter().enumerate() {
n += usize::from(s.lit_length);
if i == ch.long_length_pos as usize && ch.long_length_type == LongLengthType::LiteralLength
{
n += 0x10000;
}
}
n
}
fn count_match_bytes(ss: &SeqStore, ch: &SeqChunk) -> usize {
let mut n = 0usize;
for (i, s) in ss.sequences[ch.seq_start..ch.seq_end].iter().enumerate() {
n += usize::from(s.ml_base) + MINMATCH as usize;
if i == ch.long_length_pos as usize && ch.long_length_type == LongLengthType::MatchLength {
n += 0x10000;
}
}
n
}
fn derive_chunk(ss: &SeqStore, start_idx: usize, end_idx: usize) -> SeqChunk {
let full = SeqChunk::full(ss);
let mut lit_start = 0;
if start_idx > 0 {
lit_start = count_literals_bytes(
ss,
&SeqChunk {
seq_end: start_idx,
..full
},
);
}
let (mut llt, mut llpos) = (full.long_length_type, full.long_length_pos);
if llt != LongLengthType::None {
if (llpos as usize) < start_idx || (llpos as usize) >= end_idx {
llt = LongLengthType::None;
} else {
llpos -= start_idx as u32;
}
}
let mut ch = SeqChunk {
seq_start: start_idx,
seq_end: end_idx,
lit_start,
lit_end: full.lit_end,
long_length_type: llt,
long_length_pos: llpos,
};
if end_idx != ss.nb_seq {
ch.lit_end = lit_start + count_literals_bytes(ss, &ch);
}
ch
}
fn resolve_repcode_to_raw_offset(rep: &[u32; 3], off_base: u32, ll0: bool) -> u32 {
debug_assert!(offbase_is_repcode(off_base));
let adjusted_rep_code = off_base - 1 + u32::from(ll0); if adjusted_rep_code == REP_NUM {
debug_assert!(ll0);
return rep[0].wrapping_sub(1);
}
rep[adjusted_rep_code as usize]
}
fn resolve_off_codes(d_rep: &mut [u32; 3], c_rep: &mut [u32; 3], ss: &mut SeqStore, ch: &SeqChunk) {
let nb_seq = ch.nb_seq();
let long_lit_len_idx = if ch.long_length_type == LongLengthType::LiteralLength {
ch.long_length_pos as usize
} else {
nb_seq
};
for (idx, seq) in ss.sequences[ch.seq_start..ch.seq_end]
.iter_mut()
.enumerate()
{
let ll0 = seq.lit_length == 0 && idx != long_lit_len_idx;
let off_base = seq.off_base;
debug_assert!(off_base > 0);
if offbase_is_repcode(off_base) {
let d_raw_offset = resolve_repcode_to_raw_offset(d_rep, off_base, ll0);
let c_raw_offset = resolve_repcode_to_raw_offset(c_rep, off_base, ll0);
if d_raw_offset != c_raw_offset {
seq.off_base = c_raw_offset.wrapping_add(REP_NUM);
}
}
update_rep(d_rep, seq.off_base, ll0);
update_rep(c_rep, off_base, ll0);
}
}
fn no_compress_block(dst: &mut [u8], src: &[u8], last: u32) -> Result<usize> {
let n = src.len();
if n + BLOCK_HEADER_SIZE > dst.len() {
return Err(Error::OutputTooSmall);
}
let h = last + ((n as u32) << 3);
dst[..3].copy_from_slice(&h.to_le_bytes()[..3]);
dst[3..3 + n].copy_from_slice(src);
Ok(n + BLOCK_HEADER_SIZE)
}
fn rle_compress_block(dst: &mut [u8], byte: u8, src_size: usize, last: u32) -> Result<usize> {
if dst.len() < 4 {
return Err(Error::OutputTooSmall);
}
let h = last + (1 << 1) + ((src_size as u32) << 3);
dst[..3].copy_from_slice(&h.to_le_bytes()[..3]);
dst[3] = byte;
Ok(4)
}
fn build_entropy_stats_literals(
src: &[u8],
prev: &HufCTables,
next: &mut HufCTables,
optimal_depth: bool,
) -> Result<(u32, usize)> {
let src_size = src.len();
let mut max_symbol_value = 255u32;
let mut repeat = prev.repeat;
next.clone_from(prev);
let min_lit_size = if prev.repeat == HufRepeat::Valid {
6
} else {
COMPRESS_LITERALS_SIZE_MIN
};
if src_size <= min_lit_size {
return Ok((SET_BASIC, 0));
}
let mut count = [0u32; 256];
let largest = hist_count(&mut count, &mut max_symbol_value, src)? as usize;
if largest == src_size {
return Ok((SET_RLE, 0)); }
if largest <= (src_size >> 7) + 4 {
return Ok((SET_BASIC, 0)); }
if repeat == HufRepeat::Check && !huf_validate_ctable(&prev.table, &count, max_symbol_value) {
repeat = HufRepeat::None;
}
next.table = HufCTable::new();
let flags = if optimal_depth {
HUF_FLAGS_OPTIMAL_DEPTH
} else {
0
};
let huff_log = huf_optimal_table_log(
LIT_HUF_LOG,
src_size,
max_symbol_value,
&mut next.table,
&count,
flags,
);
debug_assert!(huff_log <= LIT_HUF_LOG);
let huff_log = huf_build_ctable(&mut next.table, &count, max_symbol_value, huff_log)?;
let new_c_size = huf_estimate_compressed_size(&next.table, &count, max_symbol_value);
let mut des = [0u8; MAX_HUF_HEADER_SIZE];
let h_size = huf_write_ctable(&mut des, &next.table, max_symbol_value, huff_log)?;
if repeat != HufRepeat::None {
let old_c_size = huf_estimate_compressed_size(&prev.table, &count, max_symbol_value);
if old_c_size < src_size && (old_c_size <= h_size + new_c_size || h_size + 12 >= src_size) {
next.clone_from(prev);
return Ok((SET_REPEAT, 0));
}
}
if new_c_size + h_size >= src_size {
next.clone_from(prev);
return Ok((SET_BASIC, 0));
}
next.repeat = HufRepeat::Check;
Ok((SET_COMPRESSED, h_size))
}
fn estimate_literals_size(
literals: &[u8],
huf: &HufCTable,
h_type: u32,
huf_des_size: usize,
write_entropy: bool,
) -> usize {
let lit_size = literals.len();
let header_size = 3 + usize::from(lit_size >= 1024) + usize::from(lit_size >= 16 * 1024);
let single_stream = lit_size < 256;
match h_type {
SET_BASIC => lit_size,
SET_RLE => 1,
_ => {
let mut count = [0u32; 256];
let mut max_symbol_value = 255u32;
if hist_count(&mut count, &mut max_symbol_value, literals).is_err() {
return lit_size;
}
let mut estimate = huf_estimate_compressed_size(huf, &count, max_symbol_value);
if write_entropy {
estimate += huf_des_size;
}
if !single_stream {
estimate += 6; }
estimate + header_size
}
}
}
#[allow(clippy::too_many_arguments)]
fn estimate_symbol_type_size<const N: usize, const S: usize>(
encoding: u32,
codes: &[u8],
max_code: u32,
ct: &FseCTable<N, S>,
additional_bits: Option<&[u8]>,
default_norm: &[i16],
default_norm_log: u32,
) -> usize {
let nb_seq = codes.len();
let mut count = [0u32; 256];
let mut max = max_code;
hist_count_codes(&mut count, &mut max, codes);
let mut bits = match encoding {
SET_BASIC => cross_entropy_cost(default_norm, default_norm_log, &count, max),
SET_RLE => 0,
_ => fse_bit_cost(ct, &count, max),
};
if bits == COST_ERROR {
return nb_seq * 10;
}
match additional_bits {
Some(ab) => {
bits += codes
.iter()
.map(|&c| usize::from(ab[c as usize]))
.sum::<usize>()
}
None => bits += codes.iter().map(|&c| usize::from(c)).sum::<usize>(),
}
bits >> 3
}
impl FrameCompressor {
fn estimate_sub_block_size(&mut self, ch: &SeqChunk) -> Option<usize> {
let strategy = self.cp.strategy as u32;
let (h_type, huf_des_size) = build_entropy_stats_literals(
&self.ss.lit_buf[ch.lit_start..ch.lit_end],
&self.prev.huf,
&mut self.next.huf,
strategy >= STRAT_BTULTRA,
)
.ok()?;
let nb_seq = ch.nb_seq();
let stats = if nb_seq != 0 {
let mut fse_buf = [0u8; MAX_FSE_HEADERS_SIZE];
build_sequences_statistics(
&mut fse_buf,
&mut self.ss,
ch,
&self.prev.fse,
&mut self.next.fse,
strategy,
)
.ok()?
} else {
self.next.fse.ll_repeat = FseRepeat::None;
self.next.fse.of_repeat = FseRepeat::None;
self.next.fse.ml_repeat = FseRepeat::None;
SeqStats {
ll_type: SET_BASIC,
of_type: SET_BASIC,
ml_type: SET_BASIC,
size: 0,
last_count_size: 0,
}
};
let literals_size = estimate_literals_size(
&self.ss.lit_buf[ch.lit_start..ch.lit_end],
&self.next.huf.table,
h_type,
huf_des_size,
h_type == SET_COMPRESSED,
);
let (s, e) = (ch.seq_start, ch.seq_end);
let fse = &self.next.fse;
let mut seq_size = 1 + 1 + usize::from(nb_seq >= 128) + usize::from(nb_seq >= LONGNBSEQ);
seq_size += estimate_symbol_type_size(
stats.of_type,
&self.ss.of_code[s..e],
MAX_OFF as u32,
&fse.of,
None,
&OF_DEFAULT_NORM,
5,
);
seq_size += estimate_symbol_type_size(
stats.ll_type,
&self.ss.ll_code[s..e],
MAX_LL as u32,
&fse.ll,
Some(&LL_BITS),
&LL_DEFAULT_NORM,
6,
);
seq_size += estimate_symbol_type_size(
stats.ml_type,
&self.ss.ml_code[s..e],
MAX_ML as u32,
&fse.ml,
Some(&ML_BITS),
&ML_DEFAULT_NORM,
6,
);
seq_size += stats.size; Some(seq_size + literals_size + BLOCK_HEADER_SIZE)
}
fn derive_block_splits_helper(
&mut self,
splits: &mut [u32; MAX_NB_BLOCK_SPLITS + 1],
nb_splits: &mut usize,
start_idx: usize,
end_idx: usize,
) {
let mid_idx = (start_idx + end_idx) / 2;
debug_assert!(end_idx >= start_idx);
if end_idx - start_idx < MIN_SEQUENCES_BLOCK_SPLITTING || *nb_splits >= MAX_NB_BLOCK_SPLITS
{
return; }
let full = derive_chunk(&self.ss, start_idx, end_idx);
let first_half = derive_chunk(&self.ss, start_idx, mid_idx);
let second_half = derive_chunk(&self.ss, mid_idx, end_idx);
let original = self.estimate_sub_block_size(&full);
let first = self.estimate_sub_block_size(&first_half);
let second = self.estimate_sub_block_size(&second_half);
let (Some(original), Some(first), Some(second)) = (original, first, second) else {
return;
};
if first + second < original {
self.derive_block_splits_helper(splits, nb_splits, start_idx, mid_idx);
if *nb_splits >= MAX_NB_BLOCK_SPLITS {
return;
}
splits[*nb_splits] = mid_idx as u32;
*nb_splits += 1;
self.derive_block_splits_helper(splits, nb_splits, mid_idx, end_idx);
}
}
fn derive_block_splits(
&mut self,
partitions: &mut [u32; MAX_NB_BLOCK_SPLITS + 1],
nb_seq: usize,
) -> usize {
if nb_seq <= 4 {
return 0; }
let mut nb_splits = 0;
self.derive_block_splits_helper(partitions, &mut nb_splits, 0, nb_seq);
debug_assert!(nb_splits <= MAX_NB_BLOCK_SPLITS);
partitions[nb_splits] = nb_seq as u32;
nb_splits
}
#[allow(clippy::too_many_arguments)]
fn compress_seq_store_single_block(
&mut self,
ch: &SeqChunk,
d_rep: &mut [u32; 3],
c_rep: &mut [u32; 3],
dst: &mut [u8],
src: &[u8],
last: u32,
is_partition: bool,
next_rep: &[u32; 3],
) -> Result<usize> {
let d_rep_original = *d_rep;
if is_partition {
resolve_off_codes(d_rep, c_rep, &mut self.ss, ch);
}
if dst.len() < BLOCK_HEADER_SIZE {
return Err(Error::OutputTooSmall);
}
let mut c_seqs_size = entropy_compress_seq_store(
&mut dst[BLOCK_HEADER_SIZE..],
&mut self.ss,
ch,
&self.prev,
&mut self.next,
&self.cp,
src.len(),
)?;
if !self.first_block && c_seqs_size < RLE_MAX_LENGTH && is_rle(src) {
c_seqs_size = 1;
}
let c_size = if c_seqs_size == 0 {
*d_rep = d_rep_original;
no_compress_block(dst, src, last)?
} else if c_seqs_size == 1 {
*d_rep = d_rep_original;
rle_compress_block(dst, src[0], src.len(), last)?
} else {
core::mem::swap(&mut self.prev, &mut self.next);
self.rep = *next_rep;
let h = last + (2 << 1) + ((c_seqs_size as u32) << 3);
dst[..3].copy_from_slice(&h.to_le_bytes()[..3]);
BLOCK_HEADER_SIZE + c_seqs_size
};
if self.prev.fse.of_repeat == FseRepeat::Valid {
self.prev.fse.of_repeat = FseRepeat::Check;
}
Ok(c_size)
}
pub(super) fn compress_block_split(
&mut self,
dst: &mut [u8],
src: &[u8],
start: usize,
end: usize,
last: u32,
) -> Result<usize> {
let mut next_rep = self.rep;
if !self.build_block_seq_store(&mut next_rep, src, start, end)? {
if self.prev.fse.of_repeat == FseRepeat::Valid {
self.prev.fse.of_repeat = FseRepeat::Check;
}
return no_compress_block(dst, &src[start..end], last);
}
let nb_seq = self.ss.nb_seq;
self.split_block_internal(dst, &src[start..end], last, nb_seq, &next_rep)
}
fn split_block_internal(
&mut self,
dst: &mut [u8],
block: &[u8],
last: u32,
nb_seq: usize,
next_rep: &[u32; 3],
) -> Result<usize> {
let block_size = block.len();
let mut partitions = [0u32; MAX_NB_BLOCK_SPLITS + 1]; let num_splits = self.derive_block_splits(&mut partitions, nb_seq);
let mut d_rep = self.rep;
let mut c_rep = self.rep;
if num_splits == 0 {
let ch = SeqChunk::full(&self.ss);
return self.compress_seq_store_single_block(
&ch, &mut d_rep, &mut c_rep, dst, block, last, false, next_rep,
);
}
let mut c_size = 0usize;
let mut ip = 0usize;
let mut src_bytes_total = 0usize;
let mut curr = derive_chunk(&self.ss, 0, partitions[0] as usize);
for i in 0..=num_splits {
let last_partition = i == num_splits;
let mut last_block_entire_src = 0;
let mut src_bytes =
count_literals_bytes(&self.ss, &curr) + count_match_bytes(&self.ss, &curr);
src_bytes_total += src_bytes;
let next = if last_partition {
src_bytes += block_size - src_bytes_total;
last_block_entire_src = last;
curr
} else {
derive_chunk(&self.ss, partitions[i] as usize, partitions[i + 1] as usize)
};
let c_size_chunk = self.compress_seq_store_single_block(
&curr,
&mut d_rep,
&mut c_rep,
&mut dst[c_size..],
&block[ip..ip + src_bytes],
last_block_entire_src,
true,
next_rep,
)?;
ip += src_bytes;
c_size += c_size_chunk;
curr = next;
}
self.rep = d_rep;
Ok(c_size)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::enc::seq_store::{REP_START_VALUE, SeqDef};
fn store(seqs: &[(u32, usize, usize)], last_lits: usize) -> SeqStore {
let n: usize = seqs.iter().map(|s| s.1 + s.2).sum::<usize>() + last_lits;
let src: alloc::vec::Vec<u8> = (0..n + 64).map(|i| i as u8).collect();
let mut ss = SeqStore::new(1 << 17, 3);
let mut pos = 0;
for &(off_base, ll, ml) in seqs {
ss.store_seq(&src, pos, ll, n, off_base, ml);
pos += ll + ml;
}
ss.store_last_literals(&src[pos..pos + last_lits]);
ss
}
#[test]
fn chunks() {
let ss = store(&[(10, 5, 4), (1, 0, 3), (20, 70_000, 5), (2, 7, 3)], 9);
assert_eq!(ss.long_length_type, LongLengthType::LiteralLength);
assert_eq!(ss.long_length_pos, 2);
let a = derive_chunk(&ss, 0, 2);
assert_eq!((a.lit_start, a.lit_end), (0, 5));
assert_eq!(a.long_length_type, LongLengthType::None);
assert_eq!(count_match_bytes(&ss, &a), 7);
let b = derive_chunk(&ss, 2, 4);
assert_eq!((b.lit_start, b.lit_end), (5, 5 + 70_007 + 9));
assert_eq!(b.long_length_pos, 0);
assert_eq!(count_literals_bytes(&ss, &b), 70_007);
let c = derive_chunk(&ss, 3, 4);
assert_eq!(c.long_length_type, LongLengthType::None);
assert_eq!((c.lit_start, c.lit_end), (70_005, 70_005 + 7 + 9));
}
#[test]
fn resolve_offcodes() {
let mut ss = store(&[(1, 0, 4), (2, 3, 4), (3, 0, 4)], 0);
let ch = SeqChunk::full(&ss);
let (mut d, mut c) = (REP_START_VALUE, REP_START_VALUE);
resolve_off_codes(&mut d, &mut c, &mut ss, &ch);
assert_eq!(d, c);
assert_eq!(ss.sequences[0].off_base, 1);
let mut ss = store(&[(1, 0, 4), (2, 3, 4), (3, 0, 4)], 0);
let mut d = [1, 4, 8];
let mut c = [100, 200, 300];
resolve_off_codes(&mut d, &mut c, &mut ss, &ch);
let offs: alloc::vec::Vec<u32> =
ss.sequences().iter().map(|s: &SeqDef| s.off_base).collect();
assert_eq!(offs, [203, 103, 3]);
assert_eq!(d, c);
assert_eq!(c, [99, 100, 200]);
}
#[test]
fn long_length_chunk_boundaries() {
for llt in [LongLengthType::LiteralLength, LongLengthType::MatchLength] {
let mut ss = store(&[(4, 4, 4); 4], 0);
ss.long_length_type = llt;
ss.long_length_pos = 2;
for start in 0..=4 {
for end in start..=4 {
let ch = derive_chunk(&ss, start, end);
if (start..end).contains(&2) {
assert_eq!(ch.long_length_type, llt);
assert_eq!(ch.long_length_pos as usize, 2 - start);
} else {
assert_eq!(ch.long_length_type, LongLengthType::None);
}
ss.ll_code[..5].fill(0xA5);
ss.ml_code[..5].fill(0xA5);
ss.of_code[..5].fill(0xA5);
crate::enc::compress::chunk_to_codes(&mut ss, &ch);
for i in 0..5 {
let long = i == 2;
let expected = if !(start..end).contains(&i) {
(0xA5, 0xA5, 0xA5)
} else if long && llt == LongLengthType::LiteralLength {
(MAX_LL as u8, 1, 2)
} else if long {
(4, MAX_ML as u8, 2)
} else {
(4, 1, 2)
};
assert_eq!(
(ss.ll_code[i], ss.ml_code[i], ss.of_code[i]),
expected,
"{llt:?} {start}..{end} at {i}"
);
}
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "38,400 sequences at level 19: over 10 minutes; the splitter has no unsafe"
)]
fn split_limit() {
fn hash(mut x: u32) -> u32 {
x ^= x >> 16;
x = x.wrapping_mul(0x7feb_352d);
x ^= x >> 15;
x = x.wrapping_mul(0x846c_a68b);
x ^ (x >> 16)
}
const SRC_SIZE: usize = 128_000;
const NB_SEQ: usize = 38_400;
let src = alloc::vec![0u8; SRC_SIZE];
let mut fc = FrameCompressor::new(19, SRC_SIZE);
let mut next_rep = fc.rep;
let mut pos = 0;
for i in 0..NB_SEQ {
let code = 3 + hash((i / 150) as u32) % 10;
let off_base = crate::enc::seq_store::offset_to_offbase((1 << code) + (i % 4) as u32);
let ll = if i == 0 { 12_800 } else { 0 };
fc.ss.store_seq(&src, pos, ll, SRC_SIZE, off_base, 3);
update_rep(&mut next_rep, off_base, ll == 0);
pos += ll + 3;
}
assert_eq!(pos, SRC_SIZE);
assert_eq!(fc.ss.nb_seq, NB_SEQ);
let mut probe = FrameCompressor::new(19, SRC_SIZE);
core::mem::swap(&mut probe.ss, &mut fc.ss);
let mut partitions = [0u32; MAX_NB_BLOCK_SPLITS + 1];
assert_eq!(
probe.derive_block_splits(&mut partitions, NB_SEQ),
MAX_NB_BLOCK_SPLITS
);
assert_eq!(partitions[MAX_NB_BLOCK_SPLITS], NB_SEQ as u32);
core::mem::swap(&mut probe.ss, &mut fc.ss);
let mut frame = alloc::vec![0x28, 0xB5, 0x2F, 0xFD, 0x00, (17 - 10) << 3];
let hdr = frame.len();
frame.resize(hdr + SRC_SIZE + 1024, 0);
let c_size = fc
.split_block_internal(&mut frame[hdr..], &src, 1, NB_SEQ, &next_rep)
.unwrap();
frame.truncate(hdr + c_size);
let (mut p, mut nb_blocks) = (hdr, 0);
loop {
let bh = u32::from_le_bytes([frame[p], frame[p + 1], frame[p + 2], 0]);
p += BLOCK_HEADER_SIZE;
p += if (bh >> 1) & 3 == 1 {
1
} else {
(bh >> 3) as usize
};
nb_blocks += 1;
if bh & 1 != 0 {
break;
}
}
assert_eq!(p, frame.len());
assert_eq!(nb_blocks, MAX_NB_BLOCK_SPLITS + 1);
assert_eq!(crate::decompress(&frame).unwrap(), src);
}
}