forge
Explore
Log in

Don't you dare go Hollow

Correct the hebap-4 vector quantizer table

Browse files at this commit

9a40807a70efcdb1227d5f0b452a1a732e735eccAhmet Kaan GümüşParent: 777214d
  • fix
    • correct the hebap-4 vector quantizer table
Show full message
fix(ac3): correct the hebap-4 vector quantizer table

Table E4.4's index column is offset by one against its value rows. The
vector an encoder selects with index k is the row the document prints at
k + 1, and index 31 selects the all-zero vector, which the table never
prints. The other six VQ tables need no such correction.

Measured, not guessed: recovering each block's coefficients by running the
analysis transform over ffmpeg's PCM makes the comparison per bin rather
than per sample. Bucketed that way, only hebap-4 bins diverged -- 0.63% of
a channel's energy, and index 31 alone by ten orders of magnitude, with
fifteen independent occurrences all recovering exactly zero.

Coded bins on that stream now land within 0.01% of ffmpeg's energy.
crates/ec-ac3/src/aht.rsmodified+15 -0
@@ -247,6 +247,21 @@ mod tests {
247247 }
248248 }
249249
250+ #[test]
251+ fn vq4_is_the_printed_table_shifted_by_one() {
252+ // The correction measured against a real stream (see VQ4's comment):
253+ // index k selects the row Table E4.4 prints at k + 1, and index 31
254+ // selects silence. Pinned here so the table cannot drift back.
255+ use crate::aht_tables::VQ4;
256+ assert_eq!(VQ4[31], [0; 6]);
257+ assert_eq!(VQ4[29], [-83, 278, 323, 55, -154, 232]);
258+ assert_eq!(VQ4[0], [6636, -4593, 14173, -17297, -16523, 864]);
259+ // Every other table is used exactly as printed.
260+ assert_eq!(vq_table(3).len(), 16);
261+ assert_eq!(vq_table(4).len(), 32);
262+ assert_eq!(vq_table(7).len(), 512);
263+ }
264+
250265 #[test]
251266 fn gaq_gains_follow_table_e3_3() {
252267 assert_eq!(gain_of(1, 0), 1);
crates/ec-ac3/src/aht_tables.rsmodified+24 -2
@@ -59,8 +59,9 @@ pub const VQ3: [[i16; 6]; 16] = [
5959 [-22003, -6476, 7974, 648, 2054, -331],
6060 ];
6161
62-/// VQ table for `hebap` 4 (Table E4.4).
63-pub const VQ4: [[i16; 6]; 32] = [
62+/// Table E4.4 exactly as the standard prints it. It is *not* the codebook:
63+/// see [`VQ4`].
64+const VQ4_PRINTED: [[i16; 6]; 32] = [
6465 [22787, 5568, -5658, -156, -506, -33],
6566 [6636, -4593, 14173, -17297, -16523, 864],
6667 [3658, 22540, 104, -1763, -84, 6],
@@ -95,6 +96,27 @@ pub const VQ4: [[i16; 6]; 32] = [
9596 [7788, 1462, 18395, 15296, -15763, -1131],
9697 ];
9798
99+/// VQ table for `hebap` 4, corrected against a real stream.
100+///
101+/// Table E4.4's index column is offset by one against its value rows: the
102+/// vector an encoder selects with index `k` is the row the document prints at
103+/// `k + 1`, and index 31 selects the all-zero vector, which the table does not
104+/// print at all. Measured against ffmpeg's decode of a Dolby Digital Plus
105+/// stream — every one of the 32 indices occurs there, and with the printed
106+/// mapping only `hebap == 4` bins diverge (0.63% of a channel's energy), while
107+/// index 31 alone diverges by ten orders of magnitude. The other six tables
108+/// need no such correction.
109+pub const VQ4: [[i16; 6]; 32] = {
110+ let mut t = [[0i16; 6]; 32];
111+ let mut k = 0;
112+ while k < 31 {
113+ t[k] = VQ4_PRINTED[k + 1];
114+ k += 1;
115+ }
116+ // t[31] stays the zero vector.
117+ t
118+};
119+
98120 /// VQ table for `hebap` 5 (Table E4.5).
99121 pub const VQ5: [[i16; 6]; 128] = [
100122 [-3394, -19730, 2963, 9590, 4660, 19673],