use crate::adaptive_quant::dirty_log2f;
use crate::entropy::{OwnedEntropyCode, Token};
pub(crate) fn estimate_ac_plain_bits<'a, I>(streams: I, code: &OwnedEntropyCode) -> u64
where
I: IntoIterator<Item = &'a [Token]>,
{
estimate_ac_plain_bits_shared(&mut streams.into_iter(), code)
}
fn estimate_ac_plain_bits_shared(
streams: &mut dyn Iterator<Item = &[Token]>,
code: &OwnedEntropyCode,
) -> u64 {
if !code.use_prefix_code {
let cost: Vec<Vec<f32>> = code
.ans_histograms
.iter()
.map(|histogram| {
histogram
.freqs
.iter()
.map(|&f| {
if f == 0 {
crate::entropy::ANS_LOG_TAB_SIZE as f32
} else {
crate::entropy::ANS_LOG_TAB_SIZE as f32 - dirty_log2f(f as f32)
}
})
.collect()
})
.collect();
let mut bits = 0.0f64;
for tokens in streams {
for t in tokens {
let cl = code.context_map[t.context as usize] as usize;
let (sym, nbits, _) =
crate::entropy::uint_encode_with_config(t.value, code.hybrid_uint_configs[cl]);
bits += cost[cl][sym as usize] as f64 + nbits as f64;
}
}
return bits as u64;
}
let mut bits: u64 = 0;
for tokens in streams {
for t in tokens {
let cl = code.context_map[t.context as usize] as usize;
let (sym, nbits, _) =
crate::entropy::uint_encode_with_config(t.value, code.hybrid_uint_configs[cl]);
bits += code.prefix_codes[cl].depths[sym as usize] as u64 + nbits as u64;
}
}
bits
}
#[cfg(test)]
mod tests {
use super::estimate_ac_plain_bits;
use crate::ac_context::K_NUM_AC_CONTEXTS;
use crate::entropy::{Token, optimize_entropy_code_ac};
#[test]
fn plain_estimate_prices_the_ans_path_below_the_huffman_floor() {
let tokens: Vec<Token> = (0..20_000)
.map(|i| Token {
context: 0,
value: u32::from(i % 100 == 0),
})
.collect();
let mut scratch = crate::coder_scratch::CoderScratch::default();
let code = optimize_entropy_code_ac(&tokens, K_NUM_AC_CONTEXTS, &mut scratch.huffman_pool);
assert!(
!code.use_prefix_code,
"expected the rANS path for this stream"
);
let estimate = estimate_ac_plain_bits(std::iter::once(tokens.as_slice()), &code);
let huffman_floor = tokens.len() as u64;
assert!(
estimate < huffman_floor / 2,
"ANS estimate {estimate} should be far below the {huffman_floor}-bit Huffman floor"
);
}
}