forge
Explore
Log in

Don't you dare go Hollow

S10: aac PS books decode through huffman::Tree; same codes and same bit reads, one codeword walk instead of a linear scan per bit

Browse files at this commit

0c85b73627caa5ccc41f402691d314213c1bea3aAhmet Kaan GümüşParent: 9794cb4
crates/ec-aac/src/ps_data.rsmodified+37 -27
@@ -8,6 +8,7 @@
88 //! its handling of stale fields, so that the decoded parameters match the
99 //! reference decoder frame for frame.
1010
11+use crate::huffman::Tree;
1112 use crate::ps_huff;
1213 use ec_core::{BitReader, Error, Result};
1314 use std::sync::OnceLock;
@@ -93,10 +94,21 @@ impl Default for PsCommon {
9394 }
9495 }
9596
96-/// One prefix-code table: every codeword as (length, code, parameter value).
97+/// One prefix-code table: the walkable code tree, and the parameter value of
98+/// each symbol (the tree's symbol is the codeword's index in the table).
9799 struct Vlc {
98- codes: Vec<(u32, u32, i32)>,
99- max_len: u32,
100+ tree: Tree,
101+ values: Vec<i32>,
102+}
103+
104+impl Vlc {
105+ fn new(codes: &[(u32, u32, i32)]) -> Vlc {
106+ let lens_codes: Vec<(u8, u32)> = codes.iter().map(|&(len, code, _)| (len as u8, code)).collect();
107+ Vlc {
108+ tree: Tree::new(&lens_codes),
109+ values: codes.iter().map(|&(_, _, value)| value).collect(),
110+ }
111+ }
100112 }
101113
102114 /// The ten PS Huffman tables, with canonical codes assigned from the lengths.
@@ -106,7 +118,7 @@ fn vlcs() -> &'static [Vlc] {
106118 ps_huff::TABLES
107119 .iter()
108120 .zip(ps_huff::OFFSETS)
109- .map(|(table, offset)| build_codes(table, offset))
121+ .map(|(table, offset)| Vlc::new(&build_codes(table, offset)))
110122 .collect()
111123 })
112124 }
@@ -117,36 +129,23 @@ fn vlcs() -> &'static [Vlc] {
117129 /// lengths alone do not fix the codes, which is why sorting them canonically
118130 /// misreads every fine-IID and ICC frame. Every table in the standard is a
119131 /// complete prefix code, which the unit tests check.
120-fn build_codes(table: &[(u8, u8)], offset: i32) -> Vlc {
132+fn build_codes(table: &[(u8, u8)], offset: i32) -> Vec<(u32, u32, i32)> {
121133 let mut codes = Vec::with_capacity(table.len());
122134 // Position in the code space, in units of 2^-32 (a 64-bit sum: no overflow).
123135 let mut next: u64 = 0;
124- let mut max_len = 0;
125136 for &(value, len) in table {
126137 let len = u32::from(len).clamp(1, 32);
127138 let code = (next >> (32 - len)) as u32;
128139 codes.push((len, code, i32::from(value) + offset));
129140 next += 1u64 << (32 - len);
130- max_len = max_len.max(len);
131141 }
132- Vlc { codes, max_len }
142+ codes
133143 }
134144
135145 /// Reads one codeword of table `which` and returns its parameter value.
136146 fn read_vlc(r: &mut BitReader<'_>, which: usize) -> Result<i32> {
137- let table = &vlcs()[which];
138- let mut code: u32 = 0;
139- for len in 1..=table.max_len {
140- code = (code << 1) | u32::from(r.read_bit()?);
141- if let Some(&(_, _, value)) = table
142- .codes
143- .iter()
144- .find(|&&(l, c, _)| l == len && c == code)
145- {
146- return Ok(value);
147- }
148- }
149- Err(Error::corrupt("ps: no codeword matches the bitstream"))
147+ let vlc = &vlcs()[which];
148+ Ok(vlc.values[vlc.tree.decode(r)?])
150149 }
151150
152151 /// Reads one IID/ICC/IPD/OPD parameter row (FFmpeg's `READ_PAR_DATA`).
@@ -458,17 +457,28 @@ mod tests {
458457 // length 4) and (value +2, length 4), so they get codes 0000 and 0001,
459458 // not the canonical order a length sort would give.
460459 let v = build_codes(ps_huff::TABLES[HUFF_IID_DF1], ps_huff::OFFSETS[HUFF_IID_DF1]);
461- assert_eq!(v.codes[0], (4, 0b0000, -2));
462- assert_eq!(v.codes[1], (4, 0b0001, 2));
463- assert_eq!(v.codes[2], (3, 0b001, -1));
460+ assert_eq!(v[0], (4, 0b0000, -2));
461+ assert_eq!(v[1], (4, 0b0001, 2));
462+ assert_eq!(v[2], (3, 0b001, -1));
463+ }
464+
465+ #[test]
466+ fn every_ps_codeword_decodes_to_its_own_symbol() {
467+ for (i, table) in ps_huff::TABLES.iter().enumerate() {
468+ let codes: Vec<(u8, u32)> = build_codes(table, ps_huff::OFFSETS[i])
469+ .iter()
470+ .map(|&(len, code, _)| (len as u8, code))
471+ .collect();
472+ crate::huffman::assert_each_codeword_decodes_to_itself(&codes);
473+ }
464474 }
465475
466476 #[test]
467477 fn codes_are_prefix_free_and_values_cover_the_range() {
468478 for (i, table) in ps_huff::TABLES.iter().enumerate() {
469479 let v = build_codes(table, ps_huff::OFFSETS[i]);
470- for (a, &(la, ca, _)) in v.codes.iter().enumerate() {
471- for &(lb, cb, _) in v.codes.iter().skip(a + 1) {
480+ for (a, &(la, ca, _)) in v.iter().enumerate() {
481+ for &(lb, cb, _) in v.iter().skip(a + 1) {
472482 let (short, long, cs, cl) = if la <= lb { (la, lb, ca, cb) } else { (lb, la, cb, ca) };
473483 assert!(cl >> (long - short) != cs, "table {i} codes collide");
474484 }
@@ -498,7 +508,7 @@ mod tests {
498508 push(&mut bits, 0, 1); // dt = 0 for the envelope
499509 let code_for_zero = {
500510 let v = build_codes(ps_huff::TABLES[HUFF_IID_DF0], ps_huff::OFFSETS[HUFF_IID_DF0]);
501- *v.codes.iter().find(|c| c.2 == 0).expect("zero is a codeword")
511+ *v.iter().find(|c| c.2 == 0).expect("zero is a codeword")
502512 };
503513 for _ in 0..10 {
504514 push(&mut bits, code_for_zero.1, code_for_zero.0);