From 0c85b73627caa5ccc41f402691d314213c1bea3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:12:15 +0300 Subject: [PATCH] S10: aac PS books decode through huffman::Tree; same codes and same bit reads, one codeword walk instead of a linear scan per bit --- crates/ec-aac/src/ps_data.rs | 64 +++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/crates/ec-aac/src/ps_data.rs b/crates/ec-aac/src/ps_data.rs index 81aae92e..35c79a0c 100644 --- a/crates/ec-aac/src/ps_data.rs +++ b/crates/ec-aac/src/ps_data.rs @@ -8,6 +8,7 @@ //! its handling of stale fields, so that the decoded parameters match the //! reference decoder frame for frame. +use crate::huffman::Tree; use crate::ps_huff; use ec_core::{BitReader, Error, Result}; use std::sync::OnceLock; @@ -93,10 +94,21 @@ impl Default for PsCommon { } } -/// One prefix-code table: every codeword as (length, code, parameter value). +/// One prefix-code table: the walkable code tree, and the parameter value of +/// each symbol (the tree's symbol is the codeword's index in the table). struct Vlc { - codes: Vec<(u32, u32, i32)>, - max_len: u32, + tree: Tree, + values: Vec, +} + +impl Vlc { + fn new(codes: &[(u32, u32, i32)]) -> Vlc { + let lens_codes: Vec<(u8, u32)> = codes.iter().map(|&(len, code, _)| (len as u8, code)).collect(); + Vlc { + tree: Tree::new(&lens_codes), + values: codes.iter().map(|&(_, _, value)| value).collect(), + } + } } /// The ten PS Huffman tables, with canonical codes assigned from the lengths. @@ -106,7 +118,7 @@ fn vlcs() -> &'static [Vlc] { ps_huff::TABLES .iter() .zip(ps_huff::OFFSETS) - .map(|(table, offset)| build_codes(table, offset)) + .map(|(table, offset)| Vlc::new(&build_codes(table, offset))) .collect() }) } @@ -117,36 +129,23 @@ fn vlcs() -> &'static [Vlc] { /// lengths alone do not fix the codes, which is why sorting them canonically /// misreads every fine-IID and ICC frame. Every table in the standard is a /// complete prefix code, which the unit tests check. -fn build_codes(table: &[(u8, u8)], offset: i32) -> Vlc { +fn build_codes(table: &[(u8, u8)], offset: i32) -> Vec<(u32, u32, i32)> { let mut codes = Vec::with_capacity(table.len()); // Position in the code space, in units of 2^-32 (a 64-bit sum: no overflow). let mut next: u64 = 0; - let mut max_len = 0; for &(value, len) in table { let len = u32::from(len).clamp(1, 32); let code = (next >> (32 - len)) as u32; codes.push((len, code, i32::from(value) + offset)); next += 1u64 << (32 - len); - max_len = max_len.max(len); } - Vlc { codes, max_len } + codes } /// Reads one codeword of table `which` and returns its parameter value. fn read_vlc(r: &mut BitReader<'_>, which: usize) -> Result { - let table = &vlcs()[which]; - let mut code: u32 = 0; - for len in 1..=table.max_len { - code = (code << 1) | u32::from(r.read_bit()?); - if let Some(&(_, _, value)) = table - .codes - .iter() - .find(|&&(l, c, _)| l == len && c == code) - { - return Ok(value); - } - } - Err(Error::corrupt("ps: no codeword matches the bitstream")) + let vlc = &vlcs()[which]; + Ok(vlc.values[vlc.tree.decode(r)?]) } /// Reads one IID/ICC/IPD/OPD parameter row (FFmpeg's `READ_PAR_DATA`). @@ -458,17 +457,28 @@ mod tests { // length 4) and (value +2, length 4), so they get codes 0000 and 0001, // not the canonical order a length sort would give. let v = build_codes(ps_huff::TABLES[HUFF_IID_DF1], ps_huff::OFFSETS[HUFF_IID_DF1]); - assert_eq!(v.codes[0], (4, 0b0000, -2)); - assert_eq!(v.codes[1], (4, 0b0001, 2)); - assert_eq!(v.codes[2], (3, 0b001, -1)); + assert_eq!(v[0], (4, 0b0000, -2)); + assert_eq!(v[1], (4, 0b0001, 2)); + assert_eq!(v[2], (3, 0b001, -1)); + } + + #[test] + fn every_ps_codeword_decodes_to_its_own_symbol() { + for (i, table) in ps_huff::TABLES.iter().enumerate() { + let codes: Vec<(u8, u32)> = build_codes(table, ps_huff::OFFSETS[i]) + .iter() + .map(|&(len, code, _)| (len as u8, code)) + .collect(); + crate::huffman::assert_each_codeword_decodes_to_itself(&codes); + } } #[test] fn codes_are_prefix_free_and_values_cover_the_range() { for (i, table) in ps_huff::TABLES.iter().enumerate() { let v = build_codes(table, ps_huff::OFFSETS[i]); - for (a, &(la, ca, _)) in v.codes.iter().enumerate() { - for &(lb, cb, _) in v.codes.iter().skip(a + 1) { + for (a, &(la, ca, _)) in v.iter().enumerate() { + for &(lb, cb, _) in v.iter().skip(a + 1) { let (short, long, cs, cl) = if la <= lb { (la, lb, ca, cb) } else { (lb, la, cb, ca) }; assert!(cl >> (long - short) != cs, "table {i} codes collide"); } @@ -498,7 +508,7 @@ mod tests { push(&mut bits, 0, 1); // dt = 0 for the envelope let code_for_zero = { let v = build_codes(ps_huff::TABLES[HUFF_IID_DF0], ps_huff::OFFSETS[HUFF_IID_DF0]); - *v.codes.iter().find(|c| c.2 == 0).expect("zero is a codeword") + *v.iter().find(|c| c.2 == 0).expect("zero is a codeword") }; for _ in 0..10 { push(&mut bits, code_for_zero.1, code_for_zero.0);