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 @@
8 //! its handling of stale fields, so that the decoded parameters match the8 //! its handling of stale fields, so that the decoded parameters match the
9 //! reference decoder frame for frame.9 //! reference decoder frame for frame.
10 10
11+use crate::huffman::Tree;
11 use crate::ps_huff;12 use crate::ps_huff;
12 use ec_core::{BitReader, Error, Result};13 use ec_core::{BitReader, Error, Result};
13 use std::sync::OnceLock;14 use std::sync::OnceLock;
@@ -93,10 +94,21 @@ impl Default for PsCommon {
93 }94 }
94 }95 }
95 96
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).
97 struct Vlc {99 struct Vlc {
98- codes: Vec<(u32, u32, i32)>,100+ tree: Tree,
99- max_len: u32,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+ }
100 }112 }
101 113
102 /// The ten PS Huffman tables, with canonical codes assigned from the lengths.114 /// The ten PS Huffman tables, with canonical codes assigned from the lengths.
@@ -106,7 +118,7 @@ fn vlcs() -> &'static [Vlc] {
106 ps_huff::TABLES118 ps_huff::TABLES
107 .iter()119 .iter()
108 .zip(ps_huff::OFFSETS)120 .zip(ps_huff::OFFSETS)
109- .map(|(table, offset)| build_codes(table, offset))121+ .map(|(table, offset)| Vlc::new(&build_codes(table, offset)))
110 .collect()122 .collect()
111 })123 })
112 }124 }
@@ -117,36 +129,23 @@ fn vlcs() -> &'static [Vlc] {
117 /// lengths alone do not fix the codes, which is why sorting them canonically129 /// lengths alone do not fix the codes, which is why sorting them canonically
118 /// misreads every fine-IID and ICC frame. Every table in the standard is a130 /// misreads every fine-IID and ICC frame. Every table in the standard is a
119 /// complete prefix code, which the unit tests check.131 /// 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)> {
121 let mut codes = Vec::with_capacity(table.len());133 let mut codes = Vec::with_capacity(table.len());
122 // Position in the code space, in units of 2^-32 (a 64-bit sum: no overflow).134 // Position in the code space, in units of 2^-32 (a 64-bit sum: no overflow).
123 let mut next: u64 = 0;135 let mut next: u64 = 0;
124- let mut max_len = 0;
125 for &(value, len) in table {136 for &(value, len) in table {
126 let len = u32::from(len).clamp(1, 32);137 let len = u32::from(len).clamp(1, 32);
127 let code = (next >> (32 - len)) as u32;138 let code = (next >> (32 - len)) as u32;
128 codes.push((len, code, i32::from(value) + offset));139 codes.push((len, code, i32::from(value) + offset));
129 next += 1u64 << (32 - len);140 next += 1u64 << (32 - len);
130- max_len = max_len.max(len);
131 }141 }
132- Vlc { codes, max_len }142+ codes
133 }143 }
134 144
135 /// Reads one codeword of table `which` and returns its parameter value.145 /// Reads one codeword of table `which` and returns its parameter value.
136 fn read_vlc(r: &mut BitReader<'_>, which: usize) -> Result<i32> {146 fn read_vlc(r: &mut BitReader<'_>, which: usize) -> Result<i32> {
137- let table = &vlcs()[which];147+ let vlc = &vlcs()[which];
138- let mut code: u32 = 0;148+ Ok(vlc.values[vlc.tree.decode(r)?])
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"))
150 }149 }
151 150
152 /// Reads one IID/ICC/IPD/OPD parameter row (FFmpeg's `READ_PAR_DATA`).151 /// Reads one IID/ICC/IPD/OPD parameter row (FFmpeg's `READ_PAR_DATA`).
@@ -458,17 +457,28 @@ mod tests {
458 // length 4) and (value +2, length 4), so they get codes 0000 and 0001,457 // length 4) and (value +2, length 4), so they get codes 0000 and 0001,
459 // not the canonical order a length sort would give.458 // not the canonical order a length sort would give.
460 let v = build_codes(ps_huff::TABLES[HUFF_IID_DF1], ps_huff::OFFSETS[HUFF_IID_DF1]);459 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));460+ assert_eq!(v[0], (4, 0b0000, -2));
462- assert_eq!(v.codes[1], (4, 0b0001, 2));461+ assert_eq!(v[1], (4, 0b0001, 2));
463- assert_eq!(v.codes[2], (3, 0b001, -1));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+ }
464 }474 }
465 475
466 #[test]476 #[test]
467 fn codes_are_prefix_free_and_values_cover_the_range() {477 fn codes_are_prefix_free_and_values_cover_the_range() {
468 for (i, table) in ps_huff::TABLES.iter().enumerate() {478 for (i, table) in ps_huff::TABLES.iter().enumerate() {
469 let v = build_codes(table, ps_huff::OFFSETS[i]);479 let v = build_codes(table, ps_huff::OFFSETS[i]);
470- for (a, &(la, ca, _)) in v.codes.iter().enumerate() {480+ for (a, &(la, ca, _)) in v.iter().enumerate() {
471- for &(lb, cb, _) in v.codes.iter().skip(a + 1) {481+ for &(lb, cb, _) in v.iter().skip(a + 1) {
472 let (short, long, cs, cl) = if la <= lb { (la, lb, ca, cb) } else { (lb, la, cb, ca) };482 let (short, long, cs, cl) = if la <= lb { (la, lb, ca, cb) } else { (lb, la, cb, ca) };
473 assert!(cl >> (long - short) != cs, "table {i} codes collide");483 assert!(cl >> (long - short) != cs, "table {i} codes collide");
474 }484 }
@@ -498,7 +508,7 @@ mod tests {
498 push(&mut bits, 0, 1); // dt = 0 for the envelope508 push(&mut bits, 0, 1); // dt = 0 for the envelope
499 let code_for_zero = {509 let code_for_zero = {
500 let v = build_codes(ps_huff::TABLES[HUFF_IID_DF0], ps_huff::OFFSETS[HUFF_IID_DF0]);510 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")
502 };512 };
503 for _ in 0..10 {513 for _ in 0..10 {
504 push(&mut bits, code_for_zero.1, code_for_zero.0);514 push(&mut bits, code_for_zero.1, code_for_zero.0);