forge
Explore
Log in

Don't you dare go Hollow

Enhanced AC-3 — Annex E syntax, spectral extension and AHT

Browse files at this commit

7fa403863b93ed726f190aa2b1017915f2b040f1Ahmet Kaan GümüşParent: 5bc2be2
  • feat
    • Enhanced AC-3 — Annex E syntax, spectral extension and AHT
Show full message
feat(ac3): Enhanced AC-3 — Annex E syntax, spectral extension and AHT

Annex E bsi/audfrm/audblk, driving the same bit allocation and transform
as AC-3. Spectral extension synthesises the high band by translation,
noise blending and envelope scaling; the adaptive hybrid transform brings
its six-point DCT with the vector and gain-adaptive quantizers.

Two details the published document states ambiguously, resolved against a
real Dolby Digital Plus stream: the coupling banding array is indexed by
absolute sub-band (like the spectral extension one), and the AHT inverse
DCT carries sqrt(2), not the 2 its printed equation shows -- with 2 every
AHT channel comes out exactly sqrt(2) too loud.

Enhanced coupling and dependent substreams above 5.1 are refused by name.
crates/ec-ac3/src/aht.rsadded+270 -0
@@ -0,0 +1,270 @@
1+//! The adaptive hybrid transform of A/52 Annex E §3.4.
2+//!
3+//! AHT is E-AC-3's second transform stage: for a frame whose exponents never
4+//! change, one bin's six block-mantissas are themselves DCT-coded, and the
5+//! whole set arrives in the first block. That buys two quantizers the AC-3
6+//! layer does not have — a six-dimensional vector quantizer for the coarse
7+//! allocations and a gain-adaptive quantizer for the fine ones — so this module
8+//! reads a channel's entire frame of mantissas at once and hands back the six
9+//! blocks after inverting the DCT.
10+
11+use ec_core::{BitReader, Result};
12+
13+use crate::aht_tables::{GAQ_BITS, REMAP, VQ_BITS, vq_table};
14+use crate::decode::COEFFS;
15+use crate::mantissa::Dither;
16+
17+/// Blocks an AHT frame always has.
18+pub(crate) const AHT_BLOCKS: usize = 6;
19+
20+/// One channel's mantissas for a whole frame: `[block * COEFFS + bin]`.
21+pub(crate) type Frame = [f32];
22+
23+/// The `Gk` gain of a GAQ block (Table E3.3).
24+fn gain_of(gaqmod: u32, mapped: u32) -> u32 {
25+ match (gaqmod, mapped) {
26+ (_, 0) => 1,
27+ (1, _) | (3, 1) => 2,
28+ _ => 4,
29+ }
30+}
31+
32+/// §3.4.4.2 remapping, `y = x + a*x + b`, with `a` and `b` as 16-bit fractions.
33+fn remap(hebap: u8, gain: u32, x: f32) -> f32 {
34+ let row = (hebap as usize).saturating_sub(8).min(11);
35+ let column = match gain {
36+ 1 => 0,
37+ 2 => 1,
38+ _ => 2,
39+ };
40+ let (a, b) = REMAP[row][column][usize::from(x < 0.0)];
41+ x + x * (f32::from(a) / 32768.0) + f32::from(b) / 32768.0
42+}
43+
44+/// One GAQ mantissa (§3.4.4.2 and Table E3.5).
45+fn read_gaq(r: &mut BitReader<'_>, hebap: u8, gain: u32) -> Result<f32> {
46+ let m = GAQ_BITS[(hebap as usize).min(19)];
47+ if gain == 1 {
48+ let code = r.read_signed(m)?;
49+ return Ok(remap(hebap, 1, code as f32 / (1u32 << (m - 1)) as f32));
50+ }
51+ let small = if gain == 2 { m - 1 } else { m - 2 };
52+ let code = r.read_signed(small)?;
53+ // The full-scale negative symbol is the tag that says "large mantissa
54+ // follows"; every other symbol is a small one, attenuated by 1/Gk.
55+ if code == -(1i32 << (small - 1)) {
56+ let large = if gain == 2 { m - 1 } else { m };
57+ let value = r.read_signed(large)? as f32 / (1u32 << (large - 1)) as f32;
58+ Ok(remap(hebap, gain, value))
59+ } else {
60+ Ok(code as f32 / (1u32 << (small - 1)) as f32 / gain as f32)
61+ }
62+}
63+
64+/// Read one AHT channel's whole frame of mantissas and invert the DCT.
65+///
66+/// `hebap` is the high-efficiency allocation for this channel, `range` its
67+/// mantissa bins. `out` receives six blocks of `COEFFS` mantissas, exponents
68+/// not yet applied — §3.4.5 inverts the DCT first.
69+#[allow(clippy::too_many_arguments)]
70+pub(crate) fn read_channel(
71+ r: &mut BitReader<'_>,
72+ hebap: &[u8],
73+ range: (usize, usize),
74+ dither: &mut Dither,
75+ dither_on: bool,
76+ out: &mut Frame,
77+) -> Result<()> {
78+ let (start, end) = range;
79+ let gaqmod = r.read_bits(2)?;
80+ let endbap = if gaqmod < 2 { 12 } else { 17 };
81+
82+ // §3.4.2: which bins carry a gain word, and how many gain sections that
83+ // makes.
84+ let active = hebap[start..end]
85+ .iter()
86+ .filter(|&&h| h > 7 && u32::from(h) < endbap)
87+ .count();
88+ let sections = match gaqmod {
89+ 0 => 0,
90+ 1 | 2 => active,
91+ _ => active.div_ceil(3),
92+ };
93+ let mut gains = Vec::with_capacity(sections * 3);
94+ for _ in 0..sections {
95+ if gaqmod == 3 {
96+ // Three 3-state gains in one 5-bit word, unpacked like exponents.
97+ let word = r.read_bits(5)?.min(26);
98+ gains.push(word / 9);
99+ gains.push((word % 9) / 3);
100+ gains.push((word % 9) % 3);
101+ } else {
102+ gains.push(r.read_bits(1)?);
103+ }
104+ }
105+
106+ let mut pre = [0.0f32; AHT_BLOCKS];
107+ let mut next_gain = 0usize;
108+ for bin in start..end {
109+ let h = hebap[bin];
110+ let mut zero_bits = false;
111+ if h == 0 {
112+ pre = [0.0; AHT_BLOCKS];
113+ zero_bits = true;
114+ } else if h <= 7 {
115+ let index = r.read_bits(VQ_BITS[h as usize])? as usize;
116+ let table = vq_table(h);
117+ let vector = table[index.min(table.len() - 1)];
118+ for (slot, v) in pre.iter_mut().zip(vector) {
119+ *slot = f32::from(v) / 32768.0;
120+ }
121+ } else {
122+ let gain = if gaqmod > 0 && u32::from(h) < endbap {
123+ let mapped = gains.get(next_gain).copied().unwrap_or(0);
124+ next_gain += 1;
125+ gain_of(gaqmod, mapped)
126+ } else {
127+ 1
128+ };
129+ for slot in &mut pre {
130+ *slot = read_gaq(r, h, gain)?;
131+ }
132+ }
133+
134+ if zero_bits {
135+ // A zero-bit AHT bin is the same silence AC-3 fills with dither.
136+ for blk in 0..AHT_BLOCKS {
137+ out[blk * COEFFS + bin] = if dither_on { dither.value() } else { 0.0 };
138+ }
139+ continue;
140+ }
141+ // §3.4.5: invert the six-point DCT across blocks.
142+ for (blk, slot) in idct6(&pre).into_iter().enumerate() {
143+ out[blk * COEFFS + bin] = slot;
144+ }
145+ }
146+ Ok(())
147+}
148+
149+/// The inverse of §3.4.5's DCT: six AHT coefficients into six block mantissas.
150+fn idct6(x: &[f32; AHT_BLOCKS]) -> [f32; AHT_BLOCKS] {
151+ // cos(j(2m+1)pi/12) for j, m in 0..6, with the 1/sqrt(2) weight on j = 0
152+ // folded in. The leading factor is sqrt(2), not the 2 the standard's
153+ // printed equation shows: measured against ffmpeg's decode of a real DDP
154+ // stream, a factor of 2 makes every AHT channel exactly sqrt(2) too loud
155+ // (per-frame RMS ratio 1.4142 across 3 649 frames). The radical is lost in
156+ // the published equation's typesetting.
157+ const COS: [[f32; AHT_BLOCKS]; AHT_BLOCKS] = cos_table();
158+ let mut out = [0.0f32; AHT_BLOCKS];
159+ for (m, slot) in out.iter_mut().enumerate() {
160+ let mut acc = 0.0;
161+ for (j, &v) in x.iter().enumerate() {
162+ acc += v * COS[j][m];
163+ }
164+ *slot = std::f32::consts::SQRT_2 * acc;
165+ }
166+ out
167+}
168+
169+/// Build the cosine table at compile time from a small polynomial-free form:
170+/// the angles are multiples of 15 degrees, so every entry is one of six exact
171+/// values.
172+const fn cos_table() -> [[f32; AHT_BLOCKS]; AHT_BLOCKS] {
173+ // cos(k * pi / 12) for k = 0..24, the only angles the transform uses.
174+ use std::f32::consts::FRAC_1_SQRT_2;
175+ const C: [f32; 25] = [
176+ 1.0,
177+ 0.965_925_8,
178+ 0.866_025_4,
179+ FRAC_1_SQRT_2,
180+ 0.5,
181+ 0.258_819_04,
182+ 0.0,
183+ -0.258_819_04,
184+ -0.5,
185+ -FRAC_1_SQRT_2,
186+ -0.866_025_4,
187+ -0.965_925_8,
188+ -1.0,
189+ -0.965_925_8,
190+ -0.866_025_4,
191+ -FRAC_1_SQRT_2,
192+ -0.5,
193+ -0.258_819_04,
194+ 0.0,
195+ 0.258_819_04,
196+ 0.5,
197+ FRAC_1_SQRT_2,
198+ 0.866_025_4,
199+ 0.965_925_8,
200+ 1.0,
201+ ];
202+ let mut table = [[0.0f32; AHT_BLOCKS]; AHT_BLOCKS];
203+ let mut j = 0;
204+ while j < AHT_BLOCKS {
205+ let mut m = 0;
206+ while m < AHT_BLOCKS {
207+ let k = (j * (2 * m + 1)) % 24;
208+ let value = C[k];
209+ // R_j: 1/sqrt(2) for the DC term, 1 otherwise.
210+ table[j][m] = if j == 0 { value * FRAC_1_SQRT_2 } else { value };
211+ m += 1;
212+ }
213+ j += 1;
214+ }
215+ table
216+}
217+
218+#[cfg(test)]
219+mod tests {
220+ use super::*;
221+
222+ #[test]
223+ fn idct_of_a_dc_only_vector_is_flat() {
224+ let mut x = [0.0f32; AHT_BLOCKS];
225+ x[0] = 1.0;
226+ let out = idct6(&x);
227+ for v in out {
228+ assert!((v - out[0]).abs() < 1e-5, "{out:?}");
229+ }
230+ assert!(out[0] > 0.0);
231+ }
232+
233+ #[test]
234+ fn idct_matches_the_direct_sum() {
235+ // The table-driven transform against the standard's equation with the
236+ // measured sqrt(2) scale, computed the slow way in f64.
237+ let x = [0.3f32, -0.7, 0.1, 0.9, -0.2, 0.5];
238+ let got = idct6(&x);
239+ for (m, &g) in got.iter().enumerate() {
240+ let mut acc = 0.0f64;
241+ for (j, &v) in x.iter().enumerate() {
242+ let r = if j == 0 { 1.0 / 2f64.sqrt() } else { 1.0 };
243+ let angle = j as f64 * (2 * m + 1) as f64 * std::f64::consts::PI / 12.0;
244+ acc += r * f64::from(v) * angle.cos();
245+ }
246+ assert!((f64::from(g) - 2f64.sqrt() * acc).abs() < 1e-5, "m = {m}");
247+ }
248+ }
249+
250+ #[test]
251+ fn gaq_gains_follow_table_e3_3() {
252+ assert_eq!(gain_of(1, 0), 1);
253+ assert_eq!(gain_of(1, 1), 2);
254+ assert_eq!(gain_of(2, 1), 4);
255+ assert_eq!(gain_of(3, 1), 2);
256+ assert_eq!(gain_of(3, 2), 4);
257+ }
258+
259+ #[test]
260+ fn remap_reproduces_the_step_sizes_of_table_e3_5() {
261+ // hebap 8, Gk = 1: seven points of step 2/(2^3 - 1) = 2/7.
262+ let a = remap(8, 1, 1.0 / 4.0);
263+ let b = remap(8, 1, 2.0 / 4.0);
264+ assert!((b - a - 2.0 / 7.0).abs() < 1e-4, "{a} {b}");
265+ // hebap 8, Gk = 2 large: four points of step 1/(2^2 - 1) = 1/3.
266+ let a = remap(8, 2, 0.0);
267+ let b = remap(8, 2, 0.5);
268+ assert!((b - a - 1.0 / 3.0).abs() < 1e-4, "{a} {b}");
269+ }
270+}
crates/ec-ac3/src/aht_tables.rsadded+1070 -0
@@ -0,0 +1,1070 @@
1+//! Adaptive hybrid transform tables from A/52 Annex E (§E3.4, §E4).
2+//!
3+//! The vector quantizer tables are the only bulk data in this crate: 956
4+//! six-dimensional vectors, transcribed as the 16-bit two's complement values
5+//! the standard prints and converted to fractions where they are used.
6+
7+/// High efficiency bit allocation pointers, `hebaptab[]` (Table E3.1).
8+pub const HEBAPTAB: [u8; 64] = [
9+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
10+ 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
11+ 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19,
12+];
13+
14+/// Mantissa bits by `hebap` for the GAQ range 8..=19 (Table E3.2).
15+pub const GAQ_BITS: [u32; 20] = [
16+ 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16,
17+];
18+
19+/// Index width in bits by `hebap` for the VQ range 1..=7 (Table E3.2).
20+pub const VQ_BITS: [u32; 8] = [0, 2, 3, 4, 5, 7, 8, 9];
21+
22+/// VQ table for `hebap` 1 (Table E4.1).
23+pub const VQ1: [[i16; 6]; 4] = [
24+ [7167, 4739, 1106, 4269, 10412, 4820],
25+ [-5702, -3187, -14483, -1392, -2027, 849],
26+ [633, 6199, 7009, -12779, -2306, -2636],
27+ [-1468, -7031, 7592, 10617, -5946, -3062],
28+];
29+
30+/// VQ table for `hebap` 2 (Table E4.2).
31+pub const VQ2: [[i16; 6]; 8] = [
32+ [-12073, 608, -7019, 590, 4000, 869],
33+ [6692, 15689, -6178, -9239, -74, 133],
34+ [1855, -989, 20596, -2920, -4475, 225],
35+ [-1194, -3901, -821, -6566, -875, -20298],
36+ [-2762, -3181, -4094, -5623, -16945, 9765],
37+ [1547, 6839, 1980, 20233, -1071, -4986],
38+ [6221, -17915, -5516, 6266, 358, 1162],
39+ [3753, -1066, 4283, -3227, 15928, 10186],
40+];
41+
42+/// VQ table for `hebap` 3 (Table E4.3).
43+pub const VQ3: [[i16; 6]; 16] = [
44+ [-10028, 20779, 10982, -4560, 798, -68],
45+ [11050, 20490, -6617, -5342, -1797, -1631],
46+ [3977, -542, 7118, -1166, 18844, 14678],
47+ [-4320, -96, -7295, -492, -22050, -4277],
48+ [2692, 5856, 5530, 21862, -7212, -5325],
49+ [-135, -23391, 962, 8115, -644, 382],
50+ [-1563, 3400, -3299, 4693, -6892, 22398],
51+ [3535, 3030, 7296, 6214, 20476, -12099],
52+ [57, -6823, 1848, -22349, -5919, 6823],
53+ [-821, -3655, -387, -6253, -1735, -22373],
54+ [-6046, 1586, -18890, -14392, 9214, 705],
55+ [-5716, 264, -17964, 14618, 7921, -337],
56+ [-110, 108, 8, 74, -89, -50],
57+ [6612, -1517, 21687, -1658, -7949, -246],
58+ [21667, -6335, -8290, -101, -1349, -22],
59+ [-22003, -6476, 7974, 648, 2054, -331],
60+];
61+
62+/// VQ table for `hebap` 4 (Table E4.4).
63+pub const VQ4: [[i16; 6]; 32] = [
64+ [22787, 5568, -5658, -156, -506, -33],
65+ [6636, -4593, 14173, -17297, -16523, 864],
66+ [3658, 22540, 104, -1763, -84, 6],
67+ [21580, -17815, -7282, -1575, -2078, -320],
68+ [-2233, 10017, -2728, 14938, -13640, -17659],
69+ [-1564, -17738, -19161, 13735, 2757, 2951],
70+ [4520, 5510, 7393, 10799, 19231, -13770],
71+ [399, 2976, -1099, 5013, -1159, 22095],
72+ [3624, -2359, 4680, -2238, 22702, 3765],
73+ [-4201, -8285, -6810, -12390, -18414, 15382],
74+ [-5198, -6869, -10047, -8364, -16022, -20562],
75+ [-142, -22671, -368, 4391, -464, -13],
76+ [814, -1118, -1089, -22019, 74, 1553],
77+ [-1618, 19222, -17642, -13490, 842, -2309],
78+ [4689, 16490, 20813, -15387, -4164, -3968],
79+ [-3308, 11214, -13542, 13599, -19473, 13770],
80+ [1817, 854, 21225, -966, -1643, -268],
81+ [-2587, -107, -20154, 376, 1174, -304],
82+ [-2919, 453, -5390, 750, -22034, -978],
83+ [-19012, 16839, 10000, -3580, 2211, 1459],
84+ [1363, -2658, -33, -4067, 1165, -21985],
85+ [-8592, -2760, -17520, -15985, 14897, 1323],
86+ [652, -9331, 3253, -14622, 12181, 19692],
87+ [-6361, 5773, -15395, 17291, 16590, -2922],
88+ [-661, -601, 1609, 22610, 992, -1045],
89+ [4961, 9107, 11225, 7829, 16320, 18627],
90+ [-21872, -1433, 138, 1470, -1891, -196],
91+ [-19499, -18203, 11056, -516, 2543, -2249],
92+ [-1196, -17574, 20150, 11462, -401, 2619],
93+ [4638, -8154, 11891, -15759, 17615, -14955],
94+ [-83, 278, 323, 55, -154, 232],
95+ [7788, 1462, 18395, 15296, -15763, -1131],
96+];
97+
98+/// VQ table for `hebap` 5 (Table E4.5).
99+pub const VQ5: [[i16; 6]; 128] = [
100+ [-3394, -19730, 2963, 9590, 4660, 19673],
101+ [-15665, -6405, 17671, 3860, -8232, -19429],
102+ [4467, 412, -17873, -8037, 691, -17307],
103+ [3580, 2363, 6886, 3763, 6379, -20522],
104+ [-17230, -14133, -1396, -23939, 8373, -12537],
105+ [-8073, -21469, -15638, 3214, 8105, -5965],
106+ [4343, 5169, 2683, -16822, -5146, -16558],
107+ [6348, -10668, 12995, -25500, -22090, 4091],
108+ [-2880, -8366, -5968, -17158, -2638, 23132],
109+ [-5095, -14281, -22371, 21741, 3689, 2961],
110+ [-2443, -17739, 25155, 2707, 1594, 7],
111+ [-18379, 9010, 4270, 731, -426, -640],
112+ [-23695, 24732, 5642, 612, -308, -964],
113+ [-767, 1268, 225, 1635, 173, 916],
114+ [5455, 6493, 4902, 10560, 23041, -17140],
115+ [17219, -21054, -18716, 4936, -3420, 3357],
116+ [-1390, 15488, -21946, -14611, 1339, 542],
117+ [-6866, -2254, -12070, -3075, -19981, -20622],
118+ [-1803, 11775, 1343, 8917, 693, 24497],
119+ [-21610, 9462, 4681, 9254, -7815, 15904],
120+ [-5559, -3018, -9169, -1347, -22547, 12868],
121+ [-366, 5076, -1727, 20427, -283, -2923],
122+ [-1886, -6313, -939, -2081, -1399, 3513],
123+ [-3161, -537, -5075, 11268, 19396, 989],
124+ [2345, 4153, 5769, -4273, 233, -399],
125+ [-21894, -1138, -16474, 5902, 5488, -3211],
126+ [10007, -12530, 18829, 20932, -1158, 1790],
127+ [-1165, 5014, -1199, 6415, -8418, -21038],
128+ [1892, -3534, 3815, -5846, 16427, 20288],
129+ [-2664, -11627, -4147, -18311, -22710, 14848],
130+ [17256, 10419, 7764, 12040, 18956, 2525],
131+ [-21419, -18685, -10897, 4368, -7051, 4539],
132+ [-1574, 2050, 5760, 24756, 15983, 17678],
133+ [-538, -22867, 11067, 10301, 385, 528],
134+ [-8465, -3025, -16357, -23237, 16491, 3654],
135+ [5840, 575, 11890, 1947, 25157, 6653],
136+ [6625, -3516, -1964, 3850, -390, -116],
137+ [18005, 20900, 14323, -7621, -10922, 11802],
138+ [-4857, -2932, -13334, -7815, 21622, 2267],
139+ [-579, -9431, -748, -21321, 12367, 8265],
140+ [-8317, 1375, -17847, 2921, 9062, 22046],
141+ [18398, 8635, -1503, -2418, -18295, -14734],
142+ [-2987, 15129, -3331, 22300, 13878, -13639],
143+ [5874, -19026, 15587, 11350, -20738, 1971],
144+ [1581, -6955, -21440, 2455, 65, 414],
145+ [515, -4468, -665, -4672, 125, -19222],
146+ [21495, -20301, -1872, -1926, -211, -1022],
147+ [5189, -12250, -1775, -23550, -4546, 5813],
148+ [321, -6331, 14646, 6975, -1773, 867],
149+ [-13814, 3180, 7927, 444, 19552, 3146],
150+ [-6660, 12252, -1972, 17408, -24280, -12956],
151+ [-745, 14356, -1107, 23742, -9631, -18344],
152+ [18284, -7909, -7531, 19118, 7721, -12659],
153+ [1926, 15101, -12848, 2153, 21631, 1864],
154+ [-2130, 23416, 17056, -15597, -1544, 87],
155+ [8314, -11824, 14581, -20591, 7891, -2099],
156+ [19600, 22814, -17304, -2040, 285, -3863],
157+ [-8214, -18322, 10724, -13744, -13469, -1666],
158+ [14351, 4880, -20034, 964, -4221, -180],
159+ [-24598, -16635, 19724, 5925, 4777, 4414],
160+ [-2495, 23493, -16141, 2918, -1038, -2010],
161+ [18974, -2540, 13343, 1405, -6194, -1136],
162+ [2489, 13670, 22638, -7311, -129, -2792],
163+ [-13962, 16775, 23012, 728, 3397, 162],
164+ [3038, 993, 8774, -21969, -6609, 910],
165+ [-12444, -22386, -2626, -5295, 19520, 9872],
166+ [-1911, -18274, -18506, -14962, 4760, 7119],
167+ [8298, -2978, 25886, 7660, -7897, 1020],
168+ [6132, 15127, 18757, -24370, -6529, -6627],
169+ [7924, 12125, -9459, -23962, 5502, 937],
170+ [-17056, -5373, 2522, 327, 1129, -390],
171+ [15774, 19955, -10380, 11172, -3107, 14853],
172+ [-11904, -8091, -17928, -22287, -17237, -6803],
173+ [-12862, -2172, -6509, 5927, 12458, -22355],
174+ [-497, 322, 1038, -6643, -5404, 20311],
175+ [1083, -22984, -8494, 12130, -762, 2623],
176+ [5067, 19712, -1901, -30, -325, 85],
177+ [987, -5830, 4212, -9030, 9121, -25038],
178+ [-7868, 7284, -12292, 12914, -21592, 20941],
179+ [-1630, -7694, -2187, -8525, -5604, -25196],
180+ [-6668, 388, -22535, 1526, 9082, 193],
181+ [-7867, -22308, 5163, 362, 944, -259],
182+ [3824, -11850, 7591, -23176, 25342, 23771],
183+ [-10504, 4123, -21111, 21173, 22439, -838],
184+ [-4723, 21795, 6184, -122, 1642, -717],
185+ [24504, 19887, -2043, 986, 7, -55],
186+ [-27313, -135, 2437, 259, 89, 307],
187+ [24446, -3873, -5391, -820, -2387, 361],
188+ [5529, 5784, 18682, 242, -21896, -4003],
189+ [22304, 4483, 722, -12242, 7570, 15448],
190+ [8673, 3009, 20437, 21108, -21100, -3080],
191+ [-1132, 2705, -1825, 5420, -785, 18532],
192+ [16932, -13517, -16509, -14858, -20327, -14221],
193+ [2219, 1380, 21474, -1128, 327, 83],
194+ [-2177, 21517, -3856, -14180, -204, -2191],
195+ [953, -9426, 15874, -10710, -3231, 21030],
196+ [-421, -1377, 640, -8239, -20976, 2174],
197+ [4309, 18514, -9100, -18319, -15518, 3704],
198+ [-5943, 449, -8387, 1075, -22210, -4992],
199+ [2953, 12788, 18285, 1430, 14937, 21731],
200+ [-2913, 401, -4739, -20105, 1699, -1147],
201+ [3449, 5241, 8853, 22134, -7547, 1451],
202+ [-2154, 8584, 18120, -15614, 19319, -5991],
203+ [3501, 2841, 5897, 6397, 8630, 23018],
204+ [2467, 2956, 379, 5703, -22047, -2189],
205+ [-16963, -594, 18822, -5295, 1640, 774],
206+ [2896, -1424, 3586, -2292, 19910, -1822],
207+ [-18575, 21219, -14001, -12573, 16466, 635],
208+ [-1998, -19314, -16527, 12208, -16576, -7854],
209+ [-9674, 1012, -21645, 2883, -12712, 2321],
210+ [-1005, 471, -3629, 8045, -11087, 25533],
211+ [4141, -21472, -2673, 756, -663, -523],
212+ [6490, 8531, 19289, 18949, 6092, -9347],
213+ [16965, 24599, 14024, 10072, -536, -10438],
214+ [-8147, 2145, -23028, -17073, 5451, -4401],
215+ [-14873, 20520, -18303, -9717, -11885, -17831],
216+ [-2290, -14120, 2070, 22467, 1671, 725],
217+ [-8538, 14629, 3521, -20577, 6673, 8200],
218+ [20248, 4410, -1366, -585, 1229, -2449],
219+ [7467, -7148, 13667, -8246, 22392, -17320],
220+ [-1932, 3875, -9064, -3812, 958, 265],
221+ [-4399, 2959, -15911, 19598, 4954, -1105],
222+ [18009, -9923, -18137, -3862, 11178, 5821],
223+ [-14596, -1227, 9660, 21619, 11228, -11721],
224+ [-721, -1700, 109, -2142, 61, -6772],
225+ [-24619, -22520, 5608, -1957, -1761, -1012],
226+ [-23728, -4451, -2688, -14679, -4266, 9919],
227+ [8495, -894, 20438, -13820, -17267, 139],
228+];
229+
230+/// VQ table for `hebap` 6 (Table E4.6).
231+pub const VQ6: [[i16; 6]; 256] = [
232+ [10154, 7365, 16861, 18681, -22893, -3636],
233+ [-2619, -3788, -5529, -5192, -9009, -20298],
234+ [-5583, -22800, 21297, 7012, 745, 720],
235+ [428, -1459, 109, -3082, 361, -8403],
236+ [8161, 22401, 241, 1755, -874, -2824],
237+ [1140, 12643, 2306, 22263, -25146, -17557],
238+ [-2609, 3379, 10337, -19730, -15468, -23944],
239+ [-4040, -12796, -25772, 13096, 3905, 1315],
240+ [4624, -23799, 13608, 25317, -1175, 2173],
241+ [-97, 13747, -5122, 23255, 4214, -22145],
242+ [6878, -322, 18264, -854, -11916, -733],
243+ [17280, -12669, -9693, 23563, -16240, -1309],
244+ [5802, -4968, 19526, -21194, -24622, -183],
245+ [5851, -16137, 15229, -9496, -1538, 377],
246+ [14096, 25057, 13419, 8290, 23320, 16818],
247+ [-7261, 118, -15867, 19097, 9781, -277],
248+ [-4288, 21589, -13288, -16259, 16633, -4862],
249+ [4909, -19217, 23411, 14705, -722, 125],
250+ [19462, -4732, -1928, -11527, 20770, 5425],
251+ [-27562, -2881, -4331, 384, -2103, 1367],
252+ [-266, -9175, 5441, 26333, -1924, 4221],
253+ [-2970, -20170, -21816, 5450, -7426, 5344],
254+ [-221, -6696, 603, -9140, 1308, -27506],
255+ [9621, -8380, -1967, 9403, -1651, 22817],
256+ [7566, -5250, -4165, 1385, -990, 560],
257+ [-1262, 24738, -19057, 10741, 7585, -7098],
258+ [451, 20130, -9949, -6015, -2188, -1458],
259+ [22249, 9380, 9096, 10959, -2365, -3724],
260+ [18668, -650, -1234, 11092, 7678, 5969],
261+ [19207, -1485, -1076, -731, -684, 43],
262+ [-4973, 13430, 20139, 60, 476, -935],
263+ [-20029, 8710, 2499, 1016, -1158, 335],
264+ [-26413, 18598, -2201, -669, 3409, 793],
265+ [-4726, 8875, -24607, -9646, 3643, -283],
266+ [13303, -21404, -3691, -1184, -1970, 1612],
267+ [173, 60, 919, 1229, 6942, -665],
268+ [16377, 16991, 5341, -14015, -2304, -20390],
269+ [25334, -10609, 11947, -7653, -6363, 14058],
270+ [23929, -13259, -7226, -937, 234, -187],
271+ [6311, -1877, 12506, -1879, 18751, -23341],
272+ [621, 6445, 3354, -24274, 8406, 5315],
273+ [-3297, -5034, -4704, -5080, -25730, 5347],
274+ [-1275, -13295, -965, -23318, 1214, 26259],
275+ [-6252, 10035, -20105, 15301, -16073, 5136],
276+ [9562, -3911, -19510, 4745, 22270, -4171],
277+ [7978, -19600, 14024, -5745, -20855, 8939],
278+ [7, -4039, 991, -6065, 52, -19423],
279+ [3485, 2969, 7732, 7786, 25312, 6206],
280+ [-959, -12812, -1840, -22743, 7324, 10830],
281+ [-4686, 1678, -10172, -5205, 4294, -1271],
282+ [3889, 1302, 7450, 638, 20374, -3133],
283+ [-12496, -9123, 18463, -12343, -7238, 18552],
284+ [-6185, 8649, -6903, -895, 17109, 16604],
285+ [-9896, 28579, 2845, 1640, 2925, -298],
286+ [14968, -25988, 14878, -24012, 1815, -6474],
287+ [26107, 5166, 21225, 15873, 21617, 14825],
288+ [-21684, 16438, 20504, -14346, -7114, -4162],
289+ [28647, 90, -1572, 789, -902, -75],
290+ [-1479, 2471, -4061, 3612, -2240, 10914],
291+ [8616, 17491, 17255, -17456, 17022, -16357],
292+ [-20722, -18597, 25274, 17720, -3573, 1695],
293+ [-997, 6129, -6303, 11250, -11359, -19739],
294+ [-74, -4001, -1584, 13384, 162, -144],
295+ [-529, 21068, 7923, -11396, 422, -26],
296+ [7102, -13531, -20055, 2629, -178, -429],
297+ [9201, 1368, -22238, 2623, -20499, 24889],
298+ [-432, 6675, -266, 8723, 80, 28024],
299+ [19493, -3108, -9261, 1910, -21777, 5345],
300+ [14079, -11489, 12604, 6079, 19877, 1315],
301+ [10947, 9837, -18612, 15742, 4792, 605],
302+ [-1777, 3758, -4087, 21696, 6024, -576],
303+ [3567, -3578, 16379, 2680, -1752, 716],
304+ [-5049, -1399, -4550, -652, -17721, -3366],
305+ [-3635, -4372, -6522, -22152, 7382, 1458],
306+ [12242, 19190, 5646, -7815, -20289, 21344],
307+ [-7508, 19952, 23542, -9753, 5669, -1990],
308+ [-2275, 15438, 10907, -17879, 6497, 13582],
309+ [-15894, -15646, -4716, 6019, 24250, -6179],
310+ [-2049, -6856, -1208, 918, 17735, -69],
311+ [-3721, 9099, -16065, -23621, 5981, -2344],
312+ [7862, -8918, 24033, 25508, -11033, -741],
313+ [-12588, 19468, 14649, 15451, -21226, 1171],
314+ [2102, 1147, 2789, 4096, 2179, 8750],
315+ [-18214, -17758, -10366, -5203, -1066, -3541],
316+ [-2819, -19958, -11921, 6032, 8315, 10374],
317+ [-9078, -2100, 19431, -17, 732, -689],
318+ [-14512, -19224, -7095, 18727, 1870, 22906],
319+ [3912, 659, 25597, -4006, 9619, 877],
320+ [2616, 22695, -5770, 17920, 3812, 20220],
321+ [2561, 26847, -5245, -10908, 2256, -517],
322+ [-4974, 198, -21983, -3608, 22174, -18924],
323+ [21308, -1211, 19144, 16691, -1588, 11390],
324+ [-1790, 3959, -3488, 7003, -7107, 20877],
325+ [-6108, -17955, -18722, 24763, 16508, 3211],
326+ [20462, -24987, -20361, 4484, -5111, -478],
327+ [-6378, -1998, -10229, -561, -22039, -22339],
328+ [3047, -18850, 7586, 14743, -19862, 6351],
329+ [-5047, 1405, -9672, 1055, -21881, 11170],
330+ [3481, -9699, 6526, -16655, 22813, 21907],
331+ [-18570, 17501, 14664, 1291, 5026, 19676],
332+ [16134, -19810, -16956, -17939, -16933, 5800],
333+ [-8224, 4908, 8935, 2272, -1140, -23217],
334+ [1572, 2753, -1598, 2143, -3346, -21926],
335+ [-9832, -1060, -27818, 1214, 7289, 150],
336+ [98, 1538, 535, 17429, -23198, -901],
337+ [21340, -20146, 3297, -1744, -8207, -21462],
338+ [-4166, -4633, -17902, 5478, 1285, 136],
339+ [18713, 21003, 24818, 11421, 1282, -4618],
340+ [-3535, 7636, -265, 2141, -829, -2035],
341+ [-3184, 19713, 2775, -2, 1090, 104],
342+ [-6771, -20185, 2938, -2125, -36, 1268],
343+ [9560, 9430, 9586, 22100, 13827, 6296],
344+ [-535, -20018, 4276, -1868, -448, -17183],
345+ [-24352, 14244, -13647, -21040, 2271, 11555],
346+ [-2646, 15437, -4589, 18638, -4299, -622],
347+ [-20064, 4169, 18115, -1404, 13722, -1825],
348+ [-16359, 9080, 744, 22021, 125, 10794],
349+ [9644, -14607, -18479, -14714, 11174, -20754],
350+ [-326, -23762, 6144, 7909, 602, 1540],
351+ [-6650, 6634, -12683, 21396, 20785, -6839],
352+ [4252, -21043, 5628, 18687, 23860, 8328],
353+ [17986, 5704, -5245, -18093, -555, 3219],
354+ [6091, 14232, -5117, -17456, -19452, -11649],
355+ [-21586, 11302, 15434, 25590, 6777, -26683],
356+ [21355, -8244, 5877, -3540, 6079, -2567],
357+ [2603, -2455, 5421, -12286, -19100, 5574],
358+ [-1721, -26393, -23664, 22904, -349, 3787],
359+ [2189, -1203, 5340, 3249, -22617, 104],
360+ [-1664, -11020, -2857, -20723, -24049, 19900],
361+ [22873, -7345, -18481, -14616, -8400, -12965],
362+ [3777, 3958, 8239, 20494, -6991, -1201],
363+ [-160, -1613, -793, -8681, 573, 776],
364+ [4297, -3786, 20373, 6082, -5321, -18400],
365+ [18745, 2463, 12546, -7749, -7734, -2183],
366+ [11074, -4720, 22119, 1825, -24351, 4080],
367+ [1503, -19178, -1569, 13, -313, 375],
368+ [318, -575, 2544, 178, 102, 40],
369+ [-15996, -26897, 5008, 3320, 686, 1159],
370+ [25755, 26886, 574, -5930, -3916, 1407],
371+ [-9148, -7665, -2875, -8384, -18663, 26400],
372+ [-7445, -18040, -18396, 8802, -2252, -21886],
373+ [7851, 11773, 27485, -12847, -1410, 19590],
374+ [2240, 5947, 11247, 15980, -6499, 24280],
375+ [21673, -18515, 9771, 6550, -2730, 334],
376+ [-4149, 1576, -11010, 89, -24429, -5710],
377+ [7720, 1478, 21412, -25025, -8385, 9],
378+ [-2448, 10218, -12756, -16079, 1161, -21284],
379+ [-8757, -14429, -22918, -14812, 2629, 13844],
380+ [-7252, 2843, -9639, 2882, -14625, 24497],
381+ [-674, -6530, 414, -23333, -21343, 454],
382+ [2104, -6312, 10887, 18087, -1199, 175],
383+ [-493, -562, -2739, 118, -1074, 93],
384+ [-10011, -4075, -28071, 22180, 15077, -636],
385+ [-4637, -16408, -9003, -20418, -11608, -20932],
386+ [4815, 15892, 24238, -13634, -3074, -1059],
387+ [-6724, 4610, -18772, -15283, -16685, 23988],
388+ [15349, -674, -3682, 21679, 4475, -12088],
389+ [4756, 2593, 5354, 6001, 15063, 26490],
390+ [-23815, -17251, 6944, 378, 694, 670],
391+ [23392, -8839, -14713, 7544, -876, 11088],
392+ [3640, 3336, 22593, -3495, -2328, -113],
393+ [284, 6914, 3097, 10171, 6638, -18621],
394+ [2472, 5976, 11054, -11936, -603, -663],
395+ [16175, 16441, 13164, -4043, 4667, 7431],
396+ [19338, 15534, -6533, 1681, -4857, 17048],
397+ [17027, 532, -19064, -1441, -5130, 1085],
398+ [-12617, -17609, 2062, -25332, 19009, -16121],
399+ [10056, -21000, -13634, -2949, 15367, 19934],
400+ [-648, -1605, 10046, -1592, 13296, 19808],
401+ [-1054, 10744, 538, 24938, 9630, -9052],
402+ [-10099, 3042, -25076, -24052, 13971, 100],
403+ [6547, 6907, 7031, 10348, 23775, -17886],
404+ [-22793, -1984, -1393, -3330, 9267, 14317],
405+ [-14346, -3967, 3042, 16254, -17303, 9646],
406+ [-21393, 23628, 16773, 716, 2663, 114],
407+ [-19016, -3038, 1574, -245, 1463, -793],
408+ [22410, 23441, -14637, -530, 17310, 13617],
409+ [-11582, 7935, -13954, 23465, -24628, 26550],
410+ [-1045, 3679, -2218, 10572, 20999, -3702],
411+ [-15513, 197, 16718, -24603, 4945, 5],
412+ [10781, 4335, 26790, -9059, -16152, -2840],
413+ [16075, -24100, -3933, -6833, 12645, -7029],
414+ [2096, -25572, -8370, 6814, 11, 1178],
415+ [-11848, -583, -8889, -20543, -10471, -380],
416+ [-2487, 24777, -21639, -19341, 1660, -732],
417+ [2313, 13679, 4085, 24549, 24691, -21179],
418+ [-2366, -504, -4130, -10570, 23668, 1961],
419+ [20379, 17809, -9506, 3733, -18954, -6292],
420+ [-3856, 16802, -929, -20310, -17739, 6797],
421+ [12431, 6078, -11272, -14450, 6913, 23476],
422+ [7636, -1655, 23017, 10719, -8292, 838],
423+ [-8559, -1235, -18096, 3897, 16093, 1490],
424+ [-3586, 8276, 15165, -3791, -21149, 1741],
425+ [-4497, 21739, 2366, -278, -4792, 15549],
426+ [-23122, -13708, 7668, 16232, 24120, 15025],
427+ [-20043, 12821, -20160, 16691, -11655, -16081],
428+ [-12601, 20239, 3496, -2549, -6745, -11850],
429+ [4441, 7812, 20783, 17080, 11523, -9643],
430+ [24766, 8494, -23298, -3262, 11101, -7120],
431+ [-10107, -7623, -22152, -18303, 26645, 9550],
432+ [-25549, 477, 7874, -1538, 1123, -168],
433+ [470, 9834, -347, 23945, -10381, -9467],
434+ [-4096, -9702, -6856, -21544, 20845, 7174],
435+ [5370, 9748, -23765, -1190, 512, -1538],
436+ [-1006, -10046, -12649, 19234, -1790, -890],
437+ [15108, 23620, -15646, -2522, -1203, -1325],
438+ [-7406, -2605, 1095, -247, -473, 177],
439+ [8089, 4, 12424, -22284, 10405, -7728],
440+ [22196, 10775, -5043, 690, 534, -212],
441+ [-3153, -1418, -16835, 18426, 15821, 22956],
442+ [5681, -2229, 3196, -3414, -21817, -14807],
443+ [19, 787, 1032, 170, -8295, -645],
444+ [-882, -2319, -27105, 432, -4392, 1499],
445+ [-1354, -11819, -76, -20380, -10293, 11328],
446+ [211, -4753, -4675, -6933, -13538, 14479],
447+ [6043, 5260, -459, -462, 143, -65],
448+ [-2572, 7256, -3317, 9212, -23184, -9990],
449+ [-24882, -9532, 18874, 6101, 2429, -14482],
450+ [8314, 2277, 14192, 3512, 25881, 22000],
451+ [208, 20218, -281, -24778, -63, -1183],
452+ [1095, -6034, 2706, -21935, -2655, 563],
453+ [23, -5930, 243, -8989, 5345, 20558],
454+ [-15466, 12699, 4160, 11087, 20621, -10416],
455+ [20995, -85, -8468, 194, 1003, -9515],
456+ [-19637, -3335, -14081, 3574, -23381, -667],
457+ [-2076, 3489, -3192, -19367, 539, -1530],
458+ [7352, -15213, 22596, 19369, 1043, 16627],
459+ [-1872, -413, 1235, -5276, -3550, 21903],
460+ [7931, -2008, 16968, -6799, 29393, -2475],
461+ [-13589, 8389, -23636, -22091, -14178, -14297],
462+ [-11575, -20090, 16056, -1848, 15721, 4500],
463+ [3849, -16581, 20161, -21155, 7778, 11864],
464+ [-6547, -1273, -18837, -11218, 11636, 1044],
465+ [2528, -6691, -17917, -11362, -4894, -1008],
466+ [1241, 4260, 2319, 6111, 3485, 20209],
467+ [3014, -3048, 5316, -4539, 20831, 8702],
468+ [-1790, -14683, 278, 13956, -10065, -10547],
469+ [-22732, -7957, -1154, 13821, -1484, -1247],
470+ [-7317, -615, 13094, 18927, 9897, 1452],
471+ [2552, -2338, 3424, -4630, 11124, -19584],
472+ [-11125, -20553, -10855, -10783, -20767, 6833],
473+ [984, -15095, 5775, 25125, 5377, -19799],
474+ [517, 13272, -7458, -1711, 20612, -6013],
475+ [-21417, 13251, -20795, 13449, 17281, 13104],
476+ [-15811, -16248, 23093, -4037, -8195, 871],
477+ [582, 12571, -21129, -14766, -9187, 5685],
478+ [4318, -1776, 11425, -17763, -9921, 577],
479+ [6013, 16830, 17655, -25766, -4400, -3550],
480+ [-13744, -16541, 3636, -3330, -21091, -15886],
481+ [6565, -11147, 8649, -13114, 23345, -13565],
482+ [-2542, -9046, -7558, 29240, 3701, -383],
483+ [-10612, 24995, 1893, -8210, 20920, -16210],
484+ [5276, 16726, 10659, 19940, -4799, -19324],
485+ [-532, -9300, 27856, 4965, -241, 536],
486+ [-765, -20706, -3412, 18870, 2765, 1420],
487+ [-3059, 2708, -19022, -331, 3537, 116],
488+];
489+
490+/// VQ table for `hebap` 7 (Table E4.7).
491+pub const VQ7: [[i16; 6]; 512] = [
492+ [-21173, 21893, 10390, 13646, 10718, -9177],
493+ [-22519, -8193, 18328, -6629, 25518, -10848],
494+ [6800, -13758, -13278, 22418, 14667, -20938],
495+ [2347, 10516, 1125, -3455, 5569, 27136],
496+ [-6617, 11851, -24524, 22937, 20362, -6019],
497+ [-21768, 10681, -19615, -15021, -8478, -2081],
498+ [-2745, 8684, -4895, 27739, 7554, -11961],
499+ [-1020, 2460, -954, 4754, -627, -16368],
500+ [-19702, 23097, 75, -13684, -2644, 2108],
501+ [4049, -2872, 5851, -4459, 22150, 12560],
502+ [-21304, -17129, -730, 7419, -11658, -10523],
503+ [11332, 1792, 26666, 23518, -19561, -491],
504+ [-17827, -16777, -13606, -14389, -22029, -2464],
505+ [1091, -5967, -7975, -16977, -20432, -21931],
506+ [18388, -1103, 1933, 13342, -17463, 18114],
507+ [22646, 17345, -9966, 17919, 18274, 698],
508+ [1484, 20297, -5754, -26515, 4941, -22263],
509+ [-2603, 4587, -5842, 18464, 8767, -2568],
510+ [-2797, -1602, 21713, 3099, -25683, 3224],
511+ [-19027, 4693, -5007, 6060, 1972, -15095],
512+ [-2189, 9516, -530, 20669, -4662, -8301],
513+ [-22325, -8887, 2529, -11352, 5476, 998],
514+ [22100, -5052, 1651, -2657, 4615, 2319],
515+ [20855, -3078, -3330, 4105, 13470, 3069],
516+ [85, 17289, 10264, -14752, 214, 90],
517+ [-26365, -18849, -19352, 19244, -10218, 9909],
518+ [-9739, 20497, -6579, -6983, 2891, -738],
519+ [20575, -15860, -22913, 6870, 76, 327],
520+ [8744, -12877, -22945, -2372, -19424, -9771],
521+ [-12886, 16183, 21084, 3821, 749, -13792],
522+ [-15995, 18399, 2391, -17661, 19484, -6018],
523+ [1423, 11734, 4051, 19290, 6857, -19681],
524+ [-5200, 9766, 18246, 2463, 18764, -4852],
525+ [-597, 19498, 1323, -9096, -308, -1104],
526+ [-3099, -25731, -15665, 25332, 4634, 2635],
527+ [19623, -2384, -7913, 11796, -9333, -14084],
528+ [2642, 26453, -21091, -10354, -1693, -1711],
529+ [22031, 21625, 11580, -22915, -4141, 129],
530+ [-6122, 3542, 915, -261, -17, -383],
531+ [1696, 6704, -1425, 20838, 857, -4416],
532+ [1423, -15280, -8550, -9667, 5210, 5687],
533+ [-4520, -613, -11683, 5618, 4230, 619],
534+ [937, -4963, -14102, -17104, -6906, -5952],
535+ [-15068, -481, -7237, -14894, 18876, 21673],
536+ [-25658, 2910, 1143, -327, -458, -995],
537+ [-9656, -819, -24900, 2804, 20225, 1083],
538+ [-1111, -3682, -1788, -19492, 966, 821],
539+ [7293, -21759, 10790, -7059, -23293, -1723],
540+ [-282, -11093, 170, -20950, -28926, 12615],
541+ [17938, 3713, -1563, 885, 5, 564],
542+ [6116, 22696, 2242, -6951, 9975, -6132],
543+ [4338, 26808, -3705, 1976, -1079, -2570],
544+ [-661, -7901, -2668, -15194, 17722, 4375],
545+ [-4174, -11053, 717, -22506, 1562, 12252],
546+ [-6405, 18334, 6103, 6983, 5956, 18195],
547+ [9851, 5370, 23604, -6861, -6569, -62],
548+ [21964, 13359, -683, 3785, 2168, 209],
549+ [-3569, -1127, -19724, -1544, 1308, -803],
550+ [-3083, 16049, -13791, -3077, 4294, 23713],
551+ [-9999, 9943, -15872, 12934, -23631, 21699],
552+ [9722, 22837, 12192, 15091, 5533, 4837],
553+ [2243, 2099, 1243, 4089, 4748, 12956],
554+ [4007, -2468, 3353, -3092, 8843, 17024],
555+ [4330, 6127, 5549, 9249, 11226, 28592],
556+ [-9586, -8825, 236, 1009, 455, -964],
557+ [6829, 19290, -1018, 200, 1821, 578],
558+ [5196, 957, 10372, 3330, -12800, -127],
559+ [-3022, -8193, -14557, 22061, 5920, 1053],
560+ [10982, 25942, -24546, -23278, -11905, -6789],
561+ [22667, -11010, 5736, 2567, 23705, -10253],
562+ [-3343, -4233, -5458, 20667, -10843, -3605],
563+ [-4131, -3612, 4575, -829, -350, -847],
564+ [-3303, 3451, -7398, -11604, 3023, 455],
565+ [3200, -9547, 3202, -22893, 11184, -26466],
566+ [-14093, -4117, 15382, 14295, -10915, -20377],
567+ [3807, -11016, 22052, 14370, -15328, -7733],
568+ [-6291, -17719, -1560, 12048, -19805, -443],
569+ [-6147, -4234, -160, 8363, 22638, 11911],
570+ [19197, 1175, 7422, -9875, -4136, 4704],
571+ [-72, -7652, -112, -11955, -3230, 27175],
572+ [3274, 5963, 7501, -17019, 866, -25452],
573+ [737, 1861, 1833, 2022, 2384, 4755],
574+ [-5217, 7512, 3323, 2715, 3065, -1606],
575+ [4247, 565, 5629, 2497, 18019, -4920],
576+ [-2833, -17920, -8062, 15738, -1018, 2136],
577+ [3050, -19483, 16930, 29835, -10222, 15153],
578+ [-11346, 118, -25796, -13761, 15320, -468],
579+ [-4824, 4960, -4263, 1575, -10593, 19561],
580+ [-8203, -1409, -763, -1139, -607, 1408],
581+ [-2203, -11415, 2021, -6388, -2600, 711],
582+ [-413, -2511, -216, -3519, -28267, 1719],
583+ [-14446, 17050, 13917, 13499, -25762, -16121],
584+ [19228, 7341, -12301, 682, -3791, -199],
585+ [-4193, 20746, -15651, 11349, 5860, -824],
586+ [-21490, -3546, -3, -1705, -3959, 9213],
587+ [15445, -1876, 2012, -19627, 16228, -4845],
588+ [-2867, -3733, -7354, -175, -20119, 11174],
589+ [-3571, -24587, 19700, 6654, 979, -654],
590+ [21820, -7430, -6639, -10767, -8362, 15543],
591+ [14827, 17977, -7204, -3409, 1906, -17288],
592+ [3525, -3947, -1415, -2798, 17648, 2082],
593+ [-6580, -15255, -17913, 1337, 15338, 21158],
594+ [6210, 9698, 15155, -24666, -22507, -3999],
595+ [-1740, -593, 1095, -7779, 25058, 5601],
596+ [21415, -432, -1658, -6898, -1438, -14454],
597+ [-6943, 700, -12139, -745, -24187, 22466],
598+ [6287, 3283, 11006, 3844, 19184, 14781],
599+ [-22502, 15274, 5443, -2808, -970, -3343],
600+ [3257, -3708, 4744, -8301, 22814, -10208],
601+ [24346, -20970, 19846, 987, -11958, -6277],
602+ [3906, -19701, 13060, -1609, 18641, 7466],
603+ [-26409, -22549, 16305, 2014, 10975, 18032],
604+ [-7039, 4655, -14818, 18739, 15789, 1296],
605+ [9310, -1681, 14667, -3326, 26535, -11853],
606+ [5728, 5917, 13400, 10020, -2236, -24704],
607+ [1741, -6727, 12695, -22009, 4080, 5450],
608+ [-2621, 9393, 21143, -25938, -3162, -2529],
609+ [20672, 18894, -13939, 6990, -8260, 15811],
610+ [-23818, 11183, -13639, 11868, 16045, 2630],
611+ [18361, -10220, 829, 856, -1010, 157],
612+ [14400, -4678, 5153, -13290, -27434, -11028],
613+ [21613, 11256, 17453, 7604, 13130, -484],
614+ [7, 1236, 573, 4214, 5576, -3081],
615+ [916, -9092, 1285, -8958, 1185, -28699],
616+ [21587, 23695, 19116, -2885, -14282, -8438],
617+ [23414, -6161, 12978, 3061, -9351, 2236],
618+ [-3070, -7344, -20140, 5788, 582, -551],
619+ [-3993, 315, -7773, 8224, -28082, -12465],
620+ [13766, -15357, 19205, -20624, 13043, -19247],
621+ [3777, -177, 8029, -1001, 17812, 5162],
622+ [-7308, -4327, -18096, -620, -1350, 14932],
623+ [14756, -1221, -12819, -14922, -547, 27125],
624+ [2234, 1708, 2764, 5416, 7986, -25163],
625+ [2873, 3636, 3992, 5344, 10142, 21259],
626+ [1158, 5379, 508, -10514, 290, -1615],
627+ [1114, 24789, 16575, -25168, -298, -2832],
628+ [-1107, -6144, -1918, -7791, -2971, -23276],
629+ [4016, 10793, 17317, -4342, -20982, -3383],
630+ [-4494, -207, -9951, -3575, 7947, 1154],
631+ [-7576, 8117, -14047, 16982, -26457, -27540],
632+ [-15164, 16096, -16844, -8886, -23720, 15906],
633+ [24922, 5680, -1874, 420, 132, 117],
634+ [-506, -19310, -198, 412, -311, 752],
635+ [-1906, 3981, -7688, 16566, -19291, -14722],
636+ [-399, -729, -3807, -4196, -12395, 7639],
637+ [3368, 2330, 9092, 23686, -10290, -1705],
638+ [-3148, 2596, -7986, 14602, -4807, 16627],
639+ [8057, 1481, 49, 17205, 24869, 7474],
640+ [-19304, -513, 11905, 2346, 5588, 3365],
641+ [-5063, -21812, 11370, 10896, 4881, 261],
642+ [4794, 20577, 5109, -6025, -8049, -1521],
643+ [8125, -14756, 20639, -14918, 23941, -3650],
644+ [12451, 1381, 3613, 8687, -24002, 4848],
645+ [6726, 10643, 10086, 25217, -25159, -1065],
646+ [6561, 13977, 2911, 21737, 16465, -26050],
647+ [-1776, 2575, -19606, -16800, 3032, 6679],
648+ [15012, -17910, -8438, -21554, -27111, 11808],
649+ [3448, -924, -15913, -1135, 5126, -20613],
650+ [7720, 2226, 17463, 5434, 28942, 17552],
651+ [1246, 15614, -11743, 24618, -17539, 3272],
652+ [3215, 17950, 2783, -722, -22672, 5979],
653+ [-5678, -3184, -26087, 26034, 6583, 3302],
654+ [20310, -3555, -2715, -444, -1487, 1526],
655+ [-20640, -21970, -12207, -25793, 8863, -1036],
656+ [17888, 570, -16102, 8329, -2553, 15275],
657+ [-2677, 9950, -1879, 16477, -12762, -29007],
658+ [-120, -2221, 219, 97, 365, 35],
659+ [1270, -718, 1480, -2689, 1930, -7527],
660+ [1896, 8750, 1906, 18235, -12692, -6174],
661+ [-3733, 13713, -9882, -15960, -1376, -7146],
662+ [-10600, 8496, 15967, -8792, 7532, 20439],
663+ [3041, -13457, 1032, -26952, 5787, 24984],
664+ [-4590, -8220, -9322, -6112, -17243, 25745],
665+ [-17808, 6970, 3752, 626, -114, 2178],
666+ [4449, -4862, 7054, -5404, 4738, -2827],
667+ [4922, -651, 18939, -9866, 848, 1886],
668+ [-336, -5410, 7234, 20444, -9583, -600],
669+ [781, -19474, -12648, 6634, 1414, 450],
670+ [-3399, -16770, 11107, 13200, -5498, 21663],
671+ [-3265, 4859, -5961, 7530, -10837, 28086],
672+ [10350, -12901, 25699, 25640, -639, 351],
673+ [1163, 18763, -5466, -15087, -145, -1377],
674+ [-14477, 27229, -31383, -32653, 21439, -2894],
675+ [15420, 18823, 22128, 19398, 22583, 13587],
676+ [-10674, 10710, 5089, -4756, 909, -20760],
677+ [-12948, -20660, 7410, 2722, 3427, 11585],
678+ [-1105, 18374, 19731, -9650, 22442, 19634],
679+ [-296, -6798, -14677, 21603, 19796, 21399],
680+ [-19350, -7501, 25446, 13144, 8588, -25298],
681+ [3092, -10618, 20896, 9249, -3326, 1796],
682+ [-811, 1449, 3106, 4748, 12073, -14262],
683+ [-20720, 14275, -4332, -25838, -5781, -21149],
684+ [-5132, 10554, -14020, -22150, 2840, -554],
685+ [25533, 17648, 14886, -21074, 2459, 25142],
686+ [-9370, -1788, -12862, -5870, -25811, -11023],
687+ [6698, 819, 10313, 166, 27581, 523],
688+ [101, -19388, 3413, 9638, 64, 806],
689+ [-2742, -17931, -2576, 22818, 8553, 1126],
690+ [2972, 15203, 1792, 25434, -5728, -17265],
691+ [-1419, 1604, 4398, 11452, 1731, 23787],
692+ [-5136, 4625, -10653, 27981, 9897, -2510],
693+ [-10528, -28033, 2999, -1530, -832, -830],
694+ [-11133, -12511, 22206, -7243, -23578, -21698],
695+ [16935, -21892, 1861, -9606, 9432, 19026],
696+ [10277, 9516, 26815, 2010, -4943, -9080],
697+ [5547, -2210, 14270, -15300, -19316, 1822],
698+ [-4850, -783, -8959, -3076, -20056, -3197],
699+ [8232, -2794, -17752, 13308, 3229, -991],
700+ [-12237, -6581, 10315, -9552, 2260, -20648],
701+ [-7000, 5529, -7553, -7490, -10342, -10266],
702+ [3641, 19479, -5972, -19097, -18570, 12805],
703+ [1283, -4164, 4198, -28473, -2498, 1866],
704+ [16047, 26826, -13053, -6316, 985, -1597],
705+ [-403, 13680, 6457, 25070, 27124, -20710],
706+ [-18070, -1790, -24986, 5953, -954, 26600],
707+ [-24224, -15383, 24788, 1953, -1136, 187],
708+ [-2289, 12505, -20738, -904, 18324, 21258],
709+ [2658, -6140, 16179, 22276, -556, 2154],
710+ [-6087, 13950, -25682, -27713, 4049, -4795],
711+ [-21452, 26473, 19435, -9124, 895, 303],
712+ [-22200, -26177, -6026, 24729, -22926, -9030],
713+ [-14276, -15982, 23732, -22851, 9268, -3841],
714+ [29482, 21923, -6213, 1679, -2059, -1120],
715+ [-435, 9802, -3891, 12359, -4288, -18971],
716+ [19768, -86, 2467, 1990, -1021, -5354],
717+ [20986, -8783, -5329, -23562, -4730, 2673],
718+ [-5095, 5605, -4629, 19150, 26037, -12259],
719+ [972, 6858, 4551, 27949, -4025, -2272],
720+ [6075, -3260, -4989, -373, -1571, -3730],
721+ [-7256, -12992, -8820, -5109, 23054, 5054],
722+ [920, 2615, 7912, -7353, -4905, 20186],
723+ [-250, 5454, 3140, 6928, -18723, -2051],
724+ [-10299, -4372, 19608, 4879, -661, -1885],
725+ [14816, -8603, -19815, 6135, -21210, 14108],
726+ [-11945, -2223, 5018, 11892, 22741, 406],
727+ [-13184, -2613, -13256, -22433, -12482, -8380],
728+ [17066, 25267, -2273, 5056, -342, 145],
729+ [8401, -17683, 19112, 10615, -19453, 17083],
730+ [20821, -5700, 12298, -25598, 10391, 7692],
731+ [4550, 15779, 17338, -19379, -4768, 1206],
732+ [-7723, 10836, -27164, -11439, 6835, -1776],
733+ [2542, 3199, 4442, 17513, -3711, -914],
734+ [20960, -16774, -5814, 11087, -70, 22961],
735+ [3305, 2919, 6256, -4800, -20966, -3230],
736+ [5924, -16547, 2183, 2733, 3446, -23306],
737+ [-6061, -194, -13852, -10971, 19488, 1029],
738+ [4467, -5964, -19004, 1519, -359, 855],
739+ [-1581, -7607, 22070, -11580, -10032, 17102],
740+ [-12412, 2553, 4324, 22500, 5751, 12170],
741+ [-25127, 17996, -6384, 1180, 1182, 9622],
742+ [23462, -8471, -4392, -2669, 7638, -16835],
743+ [-5511, -2887, -10757, -20883, 7246, 1053],
744+ [2703, -20602, -7554, 7516, -7740, 5868],
745+ [20670, 21901, 457, 14969, -17657, -11921],
746+ [3603, -1595, -2177, -157, -43, 605],
747+ [2513, 8954, 10527, 22559, -16100, -16041],
748+ [6002, 4951, 6795, -4862, -22400, 18849],
749+ [7590, -1693, -24688, -3404, 14169, 1214],
750+ [-4398, -6663, -6870, -10083, -24596, 9253],
751+ [10468, 17751, -7748, 147, -6314, 4419],
752+ [16187, -16557, -4119, 4302, 7625, 5409],
753+ [3303, 2735, 7458, -19902, -2254, -3702],
754+ [-2077, 21609, 14870, 12545, -6081, -1764],
755+ [4678, 11740, 2859, 6953, 1919, -3871],
756+ [3522, -21853, -2469, -10453, 18893, -10742],
757+ [3759, -10191, -4866, -2659, -17831, -1242],
758+ [14991, 9351, 11870, -1573, -4848, 22549],
759+ [9509, -27152, 10734, 20851, -26185, -17878],
760+ [-7170, -1392, -19495, 12746, 8198, -1988],
761+ [1883, 28158, -846, -7235, 249, 233],
762+ [-7200, 669, -371, -2948, 23234, -5635],
763+ [3141, 288, 3223, -1258, -98, -27607],
764+ [17373, -23235, 5110, -11199, -2574, -11487],
765+ [-4928, 1518, -5456, 670, -18278, 1951],
766+ [10334, -19865, -4649, 361, -160, -923],
767+ [18732, 14264, -3155, -7485, -3328, 5959],
768+ [-3614, 21077, 7276, 3536, 8121, -1528],
769+ [-8422, 500, -19182, 18929, 26392, -1039],
770+ [15639, 25668, 8375, 1903, 1945, -11979],
771+ [-2716, 3389, 26850, -4587, 1803, 22],
772+ [1177, -655, 1233, -2128, 7844, 1767],
773+ [-761, 8209, -19290, -4593, 1923, -343],
774+ [-689, -3530, -3267, -3804, -2753, 18566],
775+ [-2110, 1962, -1353, 16643, 2765, -23102],
776+ [-433, 4905, 302, 13016, 15933, -5905],
777+ [3203, 4126, 11181, -5496, -2529, -1160],
778+ [-1091, -6469, -1415, 5682, -268, 583],
779+ [-9405, -19572, 6216, 1658, 993, -75],
780+ [-1695, -4504, -2289, -4088, -6556, -16577],
781+ [4760, -892, -10902, 6516, 24199, -6011],
782+ [-253, 1000, 63, -81, -115, -382],
783+ [-1333, 24224, -698, -4667, -2801, -19144],
784+ [-876, -28866, -21873, 12677, -6344, 3235],
785+ [16847, 21145, -26172, -3183, -396, 230],
786+ [18296, -7790, -12857, -679, -1473, 5],
787+ [-10488, 11429, 25805, -1122, 1401, -438],
788+ [3782, -7429, 26720, 17567, 19257, 12542],
789+ [6332, -746, 12789, 9316, -22542, -5354],
790+ [3418, -22728, 26978, 18303, 1076, 956],
791+ [-27315, -2988, 920, 235, 2233, 81],
792+ [6199, 5296, 16093, 14768, -8429, -1112],
793+ [-6432, 19244, 9921, -3253, 1278, -954],
794+ [24213, 2049, -22931, 2585, -2410, -4216],
795+ [9286, 14282, -19735, -3985, -2344, 1028],
796+ [-20128, 17993, -9458, 23012, -16983, 8625],
797+ [-6896, -20730, 3762, 17415, 22341, 19024],
798+ [842, 24181, 25062, -5839, -78, 937],
799+ [-621, 19722, -24204, -1962, -14854, -56],
800+ [22766, -5119, 17365, 23868, -19480, -6558],
801+ [-2158, 17490, -21435, 3340, -12819, -20295],
802+ [-9621, 17325, 715, 2265, -4123, -492],
803+ [9156, 12947, 27303, -21175, -6072, -9457],
804+ [-13164, -23269, -14006, -4184, 6978, 2],
805+ [938, -13381, 3520, -24297, 22902, 19589],
806+ [-4911, -19774, 19764, -9310, -12650, 3819],
807+ [-5462, -4249, -6987, -6260, -13943, -25150],
808+ [9341, 10369, -13862, -6704, 22556, -519],
809+ [6651, 18768, -4855, 12570, 14730, -10209],
810+ [-823, 18119, 398, -1582, -116, -363],
811+ [-6935, -12694, -28392, 8552, 6961, -239],
812+ [-2602, -4704, -1021, 2015, 5129, 23670],
813+ [-12559, -8190, -25028, 18544, 14179, 1663],
814+ [3813, 21036, -9620, -5051, -1800, -1087],
815+ [-22057, 16675, 14960, 9459, 2786, 16991],
816+ [-26040, -19318, -6414, 1104, 5798, -18039],
817+ [-1737, 24825, 10417, -11087, 896, -5273],
818+ [-1855, 11661, -2803, 24809, -21435, -19792],
819+ [-23473, -16729, -5782, 5643, 2636, 4940],
820+ [-1724, 4388, -26673, -13695, 10570, -25895],
821+ [15358, -19496, 26242, -18493, 1736, 8054],
822+ [5684, 20890, 4091, -19100, -14588, -10468],
823+ [17260, -16291, 14859, -17711, -19174, 12435],
824+ [-27185, -12573, 6743, -562, 976, -257],
825+ [12395, -8618, -22248, -19843, 11013, 7762],
826+ [3799, 11853, -27622, -8473, 1089, -1495],
827+ [4141, -2182, -26720, -735, -774, 1469],
828+ [3125, 13762, 4606, 29257, 18771, -9958],
829+ [-17465, -9445, -17562, -2530, -6435, -3726],
830+ [-1742, 4351, -6841, -19773, 9627, -10654],
831+ [7251, 3525, 10835, 5601, 25198, -23348],
832+ [-10300, -17830, 631, 11640, 2044, -20878],
833+ [-873, -8502, -1063, -15674, -10693, 14934],
834+ [-15957, 28137, 5268, 477, -1053, 1158],
835+ [-1495, -8814, -5764, -24965, 25988, 7907],
836+ [-1038, -114, -2308, -1319, -6480, 1472],
837+ [4895, -17897, -25850, 5301, -188, 1581],
838+ [3200, 17225, 4346, 22101, -18543, 22028],
839+ [-10250, 545, -10932, 2276, -28070, 8118],
840+ [15343, 2329, 9316, 20537, 14908, 21021],
841+ [6329, 6130, -24508, 837, -8637, -5844],
842+ [7386, -501, 10503, 20131, 11435, -4755],
843+ [-2745, 24174, -9274, 15273, -8389, -5835],
844+ [2992, -2864, 6048, -7473, 11687, -19996],
845+ [-883, -11954, -9976, -21829, -4436, -27178],
846+ [3458, 19626, 1280, 2597, 19849, 5255],
847+ [-5315, 19133, -14518, -8946, 13749, -1352],
848+ [18642, 17655, 11001, 6817, -18418, 6336],
849+ [-1697, 2244, -4640, 3948, -12890, -5273],
850+ [20428, 10542, 4170, -1012, 19439, 21691],
851+ [-2943, -19735, -4208, 1320, 909, -8897],
852+ [9351, -8066, -2618, -12933, 26582, 3507],
853+ [9705, -22628, 8311, 8167, -13293, 5608],
854+ [3222, 3749, -1508, 165, -52, -196],
855+ [102, -22744, -8832, 903, -11421, -14662],
856+ [-120, 5998, 19765, 13401, 3628, 5197],
857+ [8528, 5827, -1066, 774, -39, -166],
858+ [9411, -9476, 9581, -13004, 24456, 24900],
859+ [17878, 2235, -21639, 20478, 4716, -7190],
860+ [-2482, 9511, 1611, -21943, 14230, -1289],
861+ [9288, -2291, 23215, -3452, -10842, 11],
862+ [9496, 3041, 5130, -3890, -21219, -22589],
863+ [14262, -9838, 20195, 14019, 91, -17200],
864+ [-18591, 980, 17, 821, 120, -574],
865+ [12285, -19269, 13742, 16373, -161, 6025],
866+ [-3364, 1530, -4005, 2454, -10872, -23839],
867+ [105, 5085, -260, 5790, -588, 19170],
868+ [4121, 4169, 13439, 14644, 20899, 7434],
869+ [-175, 13101, -3704, 23233, 3907, 10106],
870+ [-6101, 23467, 5204, -1341, 1599, 13174],
871+ [-3217, -3494, 15117, -8387, -11762, -4750],
872+ [1146, 4675, -19378, 14917, -5091, 249],
873+ [-21506, 10136, -16473, -13305, 18382, -8601],
874+ [628, 2447, 3344, 3130, -5115, 119],
875+ [17900, -22422, -17633, 21967, -16293, -7676],
876+ [16863, 24214, 5612, -3858, -809, 3822],
877+ [-2291, 10091, -2360, -25109, -1226, 312],
878+ [2957, 11256, 26745, -13266, -3455, -1128],
879+ [-19762, -2708, 4604, 6355, 1638, 25501],
880+ [-19593, -7753, 3159, -85, -489, -1855],
881+ [814, 12510, 19077, -4681, -2610, -1474],
882+ [-23408, -19027, 8137, 19878, 7912, -282],
883+ [839, -19652, 11927, 27278, -3211, 2266],
884+ [4020, -1110, 8226, -1274, 20922, 25060],
885+ [26576, 325, -8693, -232, -2218, -699],
886+ [-11293, -4200, 1805, -6673, -22940, -1339],
887+ [-2005, -15886, -1047, -27687, -13235, 14370],
888+ [-22073, 1949, 13175, -15656, -1846, 8055],
889+ [3039, 12025, 7132, -24632, 413, -2347],
890+ [-24048, -206, 12459, -6654, -417, -10091],
891+ [18179, -23688, -20515, -16396, 7230, 763],
892+ [5659, -5085, 13878, -23729, -11077, -19587],
893+ [11340, 501, 25040, 7616, -19658, 1605],
894+ [-26650, 8878, 10544, 417, 1299, 261],
895+ [14460, 11369, -3263, 9990, 8194, 18111],
896+ [1355, -20838, -9196, -16060, -8559, -730],
897+ [-1918, -20937, -18293, -2461, -2651, 4316],
898+ [-2810, 24521, -10996, -25721, 308, -1234],
899+ [-9075, -17280, -1833, -29342, -24213, -16631],
900+ [-2843, 10165, -5339, -2888, 21858, -21340],
901+ [-15832, 14849, -23780, 5184, 10113, -20639],
902+ [-19535, -11361, 8413, 1486, -23658, -5759],
903+ [-7512, 1027, -20794, 13732, 19892, -21934],
904+ [-12132, -7022, -19175, -8840, 22125, -16490],
905+ [1937, 5210, -6318, -23788, 13141, 11082],
906+ [-205, 6036, -380, 8658, -233, 28020],
907+ [-5523, 7477, 7635, 23595, 9763, -2590],
908+ [21658, -28313, -3086, -300, -1032, 1744],
909+ [-22352, 16646, 208, 6665, -17400, -3028],
910+ [18482, 9336, -2737, -19372, 407, -4389],
911+ [-4913, -17370, 18819, -17654, 13416, 15232],
912+ [7749, 6368, 23135, -18174, 7584, -4248],
913+ [-1489, -6523, 586, -10157, 14964, 25568],
914+ [3844, -6156, 4897, -13045, -22526, 5647],
915+ [-8491, -2105, -24774, 905, -9326, 1456],
916+ [-3040, -1476, 1166, -4428, 11236, 9204],
917+ [3397, -1451, 13598, -15841, 24540, 5819],
918+ [8483, -2993, 21547, -16916, 7741, 24018],
919+ [-14932, -23758, -5332, -6664, -4497, 13267],
920+ [19379, 12916, -2142, -737, 21100, -22101],
921+ [3393, -4629, 5735, -18913, -6969, 2687],
922+ [1148, -16147, -21433, -28095, -630, -14449],
923+ [7300, 672, 18530, -17452, -10149, 351],
924+ [11356, -10974, 17212, 4624, 145, 17791],
925+ [-711, -3479, -2238, 15887, 2027, 0],
926+ [-28048, 1794, -593, -2758, -21852, 11535],
927+ [-19683, 4937, 22004, 21523, -3148, 1790],
928+ [813, 8231, 2633, 11981, -3043, 22201],
929+ [8952, -24760, -690, 14873, -2366, -5372],
930+ [8406, -5439, -274, -642, -145, 778],
931+ [-6605, 7258, 20780, -23507, -18625, 22782],
932+ [-22896, -25488, 10020, -1614, 1508, -1393],
933+ [7607, 407, -24678, -16385, -1804, -4699],
934+ [-10592, -19139, 10462, -3747, 8721, -6919],
935+ [13010, 5292, -6230, -4884, -20904, -1797],
936+ [16891, -13770, -465, 19343, -10741, -12959],
937+ [25193, -14799, -5681, -521, -321, -1211],
938+ [6917, -3093, 20183, -26903, -12026, 1295],
939+ [305, 1992, 19457, -985, 25, -521],
940+ [6707, -3698, 8365, -8687, 21921, -27166],
941+ [4668, 5997, 7117, 11696, 24401, -10794],
942+ [744, -9416, 19893, 1963, 7922, -9824],
943+ [3430, 21282, -1736, 10844, 8821, 27015],
944+ [-8813, 1521, -24038, 1651, 7838, -1208],
945+ [3911, -11221, 3273, -12541, 7168, 18402],
946+ [21642, 9117, -11536, -5256, 7077, 2382],
947+ [100, 3817, -6713, 1244, 1518, -321],
948+ [7946, -18670, 10667, -4866, 727, 776],
949+ [-15883, -8150, -2087, 22739, 1567, -3482],
950+ [4380, -2735, 8469, -7025, -11424, 1317],
951+ [26970, 4393, 7665, 17561, -714, 650],
952+ [-16191, -835, 8365, 1795, -14314, 16297],
953+ [4504, -10048, 7662, -26690, -17428, 2580],
954+ [48, -3984, 564, -5871, 2658, -18658],
955+ [12579, -26016, -15642, 2672, -1347, -887],
956+ [-4950, 4208, -6811, 2569, -20621, -8658],
957+ [-1836, -14818, -5571, -23322, -14800, 25867],
958+ [5434, -28139, -2357, -2883, -570, 2431],
959+ [13096, -2771, 24994, -12496, -24723, -1025],
960+ [-5676, -4339, 1908, 18628, -21323, 17366],
961+ [27660, -27897, -15409, 1436, -7112, -2241],
962+ [8019, 3847, 24568, -469, 9674, 10683],
963+ [-903, -10149, 1801, -21260, 4795, -8751],
964+ [1122, -9582, 2625, 22791, 956, 882],
965+ [7876, 19075, -9900, -24266, 7496, 9277],
966+ [980, -26764, -5386, 5396, 1086, 1648],
967+ [28838, -1270, -447, 5, -429, -20],
968+ [-15283, 6132, 22812, 1252, -9963, 511],
969+ [851, 7925, -457, -12210, 4261, 7579],
970+ [-4530, 8452, -1246, 14501, -24951, -5760],
971+ [-17814, -10727, 9887, -23929, -13432, 1878],
972+ [-15049, 10165, 16491, -14603, -11712, -21156],
973+ [-3317, 840, -5683, 22413, 1994, 586],
974+ [23158, -5788, -15043, -10372, -9271, -13523],
975+ [-773, -9509, -3993, -24264, 8463, 5804],
976+ [-8545, -703, -12440, -3985, -25122, -28147],
977+ [-16659, 16001, 2746, 1611, 5097, -1043],
978+ [41, -7181, 19903, 31555, -32237, 13927],
979+ [-5658, 845, -12774, 5705, 16695, -86],
980+ [5282, 14875, 27026, 21124, 15776, -10477],
981+ [14712, 19648, -11487, -13361, -20196, -15229],
982+ [8597, -9138, -626, 10891, -6015, 6346],
983+ [-1488, -1272, -1479, -1303, -3704, -5485],
984+ [-3370, 17871, -6604, 24930, 25886, -3127],
985+ [8416, 27783, -1385, 5350, -4260, 19993],
986+ [5688, 362, 17246, 3809, -3246, 1088],
987+ [-105, -29607, 2747, 15223, -167, 3722],
988+ [3502, -3195, 8602, 7772, -1566, -915],
989+ [-491, 3257, -2423, 5522, 20606, -100],
990+ [-13948, -11368, -15375, -21866, -8520, 12221],
991+ [-616, 2424, -2023, 4398, -3805, 8108],
992+ [-7204, 21043, 21211, -9395, -19391, 896],
993+ [-5737, -15160, -21298, 17066, -1006, -366],
994+ [6261, 3240, -11937, -16213, -15820, 6581],
995+ [-3155, 24796, 2733, -1257, -875, -1597],
996+ [-20469, 11094, 24071, -8987, 14136, 2220],
997+ [-14106, 11959, -22495, 4135, -1055, -5420],
998+ [801, -2655, 60, -5324, -790, 5937],
999+ [-7372, -1764, -22433, -26060, 21707, 4178],
1000+ [-5715, -6648, -14908, 1325, -24044, 1493],
1001+ [-6024, -12488, 23930, 2950, 1601, 1173],
1002+ [19067, 17630, 17929, -10654, 10928, -4958],
1003+ [3231, -3284, 27336, 4174, -1683, 497],
1004+];
1005+
1006+/// The vector table for a VQ-coded `hebap`.
1007+pub fn vq_table(hebap: u8) -> &'static [[i16; 6]] {
1008+ match hebap {
1009+ 1 => &VQ1,
1010+ 2 => &VQ2,
1011+ 3 => &VQ3,
1012+ 4 => &VQ4,
1013+ 5 => &VQ5,
1014+ 6 => &VQ6,
1015+ _ => &VQ7,
1016+ }
1017+}
1018+
1019+/// Large-mantissa remapping constants `(a, b)` (Table E3.6), indexed by
1020+/// `hebap - 8`, then gain (1, 2, 4), then sign (`x >= 0`, `x < 0`).
1021+pub const REMAP: [[[(i16, i16); 2]; 3]; 12] = [
1022+ [
1023+ [(4681, 0), (4681, 0)],
1024+ [(-10923, 16384), (-10923, -5461)],
1025+ [(-4681, 8192), (-4681, -1170)],
1026+ ],
1027+ [
1028+ [(2185, 0), (2185, 0)],
1029+ [(-14043, 16384), (-14043, -11703)],
1030+ [(-6554, 8192), (-6554, -4915)],
1031+ ],
1032+ [
1033+ [(1057, 0), (1057, 0)],
1034+ [(-15292, 16384), (-15292, -14199)],
1035+ [(-7399, 8192), (-7399, -6606)],
1036+ ],
1037+ [
1038+ [(520, 0), (520, 0)],
1039+ [(-15855, 16384), (-15855, -15327)],
1040+ [(-7802, 8192), (-7802, -7412)],
1041+ ],
1042+ [
1043+ [(258, 0), (258, 0)],
1044+ [(-16124, 16384), (-16124, -15864)],
1045+ [(-7998, 8192), (-7998, -7805)],
1046+ ],
1047+ [
1048+ [(129, 0), (129, 0)],
1049+ [(-16255, 16384), (-16255, -16126)],
1050+ [(-8096, 8192), (-8096, -7999)],
1051+ ],
1052+ [
1053+ [(64, 0), (64, 0)],
1054+ [(-16320, 16384), (-16320, -16255)],
1055+ [(-8144, 8192), (-8144, -8096)],
1056+ ],
1057+ [
1058+ [(32, 0), (32, 0)],
1059+ [(-16352, 16384), (-16352, -16320)],
1060+ [(-8168, 8192), (-8168, -8144)],
1061+ ],
1062+ [
1063+ [(16, 0), (16, 0)],
1064+ [(-16368, 16384), (-16368, -16352)],
1065+ [(-8180, 8192), (-8180, -8168)],
1066+ ],
1067+ [[(8, 0), (8, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)]],
1068+ [[(2, 0), (2, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)]],
1069+ [[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)]],
1070+];
crates/ec-ac3/src/eac3.rsadded+1209 -0
@@ -0,0 +1,1209 @@
1+//! Enhanced AC-3 (A/52 Annex E): the header, the frame-level strategy fields
2+//! and the audio block syntax that differ from AC-3.
3+//!
4+//! Everything after parsing — bit allocation, mantissas, coupling, the inverse
5+//! transform — is the AC-3 machinery in [`crate::decode`], because Annex E
6+//! changes how the parameters are *sent*, not what they mean. Of what Annex E
7+//! adds on top, the adaptive hybrid transform lives in [`crate::aht`] and
8+//! spectral extension in this module; enhanced coupling is refused by name
9+//! until a stream that uses it turns up.
10+
11+use ec_core::{BitReader, Error, Result};
12+
13+use crate::bitalloc::{BitAllocParams, DeltaBa};
14+use crate::bsi::{Acmod, Bsi};
15+use crate::decode::{CHANNELS, COEFFS, CPL, Core, LFE, Syntax};
16+use crate::exps::{self, Strategy};
17+
18+pub mod bsi {
19+ //! `bsi()` of Annex E, shaped like [`crate::bsi`] so a caller can reach
20+ //! either one the same way.
21+ pub use super::{Eac3Bsi, parse, parse_from};
22+}
23+
24+/// Blocks per syncframe by `numblkscod` (Table E2.4).
25+const BLOCKS: [usize; 4] = [1, 2, 3, 6];
26+
27+/// Reduced sample rates by `fscod2` (Table E2.3).
28+const HALF_RATE: [u32; 4] = [24_000, 22_050, 16_000, 0];
29+
30+/// Default coupling banding structure, `defcplbndstrc[]` (Table E2.12).
31+const DEFAULT_CPLBNDSTRC: [bool; 18] = [
32+ false, false, false, false, false, false, false, false, true, false, true, true, false, true,
33+ true, true, true, true,
34+];
35+
36+/// Default spectral extension banding structure, `defspxbndstrc[]` (Table
37+/// E2.11): every other sub-band from 8 upwards joins the band below it.
38+const DEFAULT_SPXBNDSTRC: [bool; 18] = [
39+ false, false, false, false, false, false, false, false, true, false, true, false, true, false,
40+ true, false, true, false,
41+];
42+
43+/// Spectral extension attenuation, `spxattentab[]` (Table E3.14). Only the
44+/// first three taps of the symmetric 5-tap notch are stored.
45+const SPXATTENTAB: [[f32; 3]; 32] = [
46+ [0.9548416, 0.9117225, 0.8705506],
47+ [0.9117225, 0.8312379, 0.7578583],
48+ [0.8705506, 0.7578583, 0.659754],
49+ [0.8312379, 0.6909564, 0.5743492],
50+ [0.7937005, 0.6299605, 0.5],
51+ [0.7578583, 0.5743492, 0.4352753],
52+ [0.7236346, 0.5236471, 0.3789291],
53+ [0.6909564, 0.4774208, 0.329877],
54+ [0.659754, 0.4352753, 0.2871746],
55+ [0.6299605, 0.3968503, 0.25],
56+ [0.6015125, 0.3618173, 0.2176376],
57+ [0.5743492, 0.329877, 0.1894646],
58+ [0.5484125, 0.3007563, 0.1649385],
59+ [0.5236471, 0.2742062, 0.1435873],
60+ [0.5, 0.25, 0.125],
61+ [0.4774208, 0.2279306, 0.1088188],
62+ [0.4558612, 0.2078095, 0.0947323],
63+ [0.4352753, 0.1894646, 0.0824692],
64+ [0.4156189, 0.1727391, 0.0717936],
65+ [0.3968503, 0.1574901, 0.0625],
66+ [0.3789291, 0.1435873, 0.0544094],
67+ [0.3618173, 0.1309118, 0.0473661],
68+ [0.3454782, 0.1193552, 0.0412346],
69+ [0.329877, 0.1088188, 0.0358968],
70+ [0.3149803, 0.0992126, 0.03125],
71+ [0.3007563, 0.0904543, 0.0272047],
72+ [0.2871746, 0.0824692, 0.0236831],
73+ [0.2742062, 0.0751891, 0.0206173],
74+ [0.2618235, 0.0685516, 0.0179484],
75+ [0.25, 0.0625, 0.015625],
76+ [0.2387104, 0.0569827, 0.0136024],
77+ [0.2279306, 0.0519524, 0.0118415],
78+];
79+
80+/// First transform coefficient of spectral extension sub-band `sbnd`
81+/// (Table E3.13): 17 sub-bands of 12 coefficients from #25.
82+fn spx_band_start(sbnd: usize) -> usize {
83+ 25 + 12 * sbnd
84+}
85+
86+/// Frame exponent strategy combinations (Table E2.10), as strategy codes.
87+const FRAME_EXP_STRATEGY: [[u8; 6]; 32] = [
88+ [1, 0, 0, 0, 0, 0],
89+ [1, 0, 0, 0, 0, 3],
90+ [1, 0, 0, 0, 2, 0],
91+ [1, 0, 0, 0, 3, 3],
92+ [2, 0, 0, 2, 0, 0],
93+ [2, 0, 0, 2, 0, 3],
94+ [2, 0, 0, 3, 2, 0],
95+ [2, 0, 0, 3, 3, 3],
96+ [2, 0, 1, 0, 0, 0],
97+ [2, 0, 2, 0, 0, 3],
98+ [2, 0, 2, 0, 2, 0],
99+ [2, 0, 2, 0, 3, 3],
100+ [2, 0, 3, 2, 0, 0],
101+ [2, 0, 3, 2, 0, 3],
102+ [2, 0, 3, 3, 2, 0],
103+ [2, 0, 3, 3, 3, 3],
104+ [3, 1, 0, 0, 0, 0],
105+ [3, 1, 0, 0, 0, 3],
106+ [3, 2, 0, 0, 2, 0],
107+ [3, 2, 0, 0, 3, 3],
108+ [3, 2, 0, 2, 0, 0],
109+ [3, 2, 0, 2, 0, 3],
110+ [3, 2, 0, 3, 2, 0],
111+ [3, 2, 0, 3, 3, 3],
112+ [3, 3, 1, 0, 0, 0],
113+ [3, 3, 2, 0, 0, 3],
114+ [3, 3, 2, 0, 2, 0],
115+ [3, 3, 2, 0, 3, 3],
116+ [3, 3, 3, 2, 0, 0],
117+ [3, 3, 3, 2, 0, 3],
118+ [3, 3, 3, 3, 2, 0],
119+ [3, 3, 3, 3, 3, 3],
120+];
121+
122+/// Which substream a frame belongs to (Table E2.1).
123+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
124+pub enum StreamType {
125+ /// Independent substream: decodable on its own.
126+ Independent,
127+ /// Dependent substream: channel extensions for the independent one before
128+ /// it.
129+ Dependent,
130+ /// An AC-3 stream carried in Annex E framing.
131+ Ac3Converted,
132+ /// Reserved.
133+ Reserved,
134+}
135+
136+impl StreamType {
137+ fn from_code(code: u32) -> StreamType {
138+ match code & 3 {
139+ 0 => StreamType::Independent,
140+ 1 => StreamType::Dependent,
141+ 2 => StreamType::Ac3Converted,
142+ _ => StreamType::Reserved,
143+ }
144+ }
145+}
146+
147+/// Everything the Annex E `bsi()` states.
148+#[derive(Debug, Clone, PartialEq)]
149+pub struct Eac3Bsi {
150+ /// Substream type.
151+ pub strmtyp: StreamType,
152+ /// Substream identifier, 0..=7.
153+ pub substreamid: u8,
154+ /// Frame size code: the frame is `(frmsiz + 1) * 2` bytes.
155+ pub frmsiz: u16,
156+ /// Frame size in bytes.
157+ pub frame_size: usize,
158+ /// Sample rate code; 3 means the reduced rates of `fscod2`.
159+ pub fscod: u8,
160+ /// Reduced sample rate code, when `fscod == 3`.
161+ pub fscod2: Option<u8>,
162+ /// Sample rate in Hz.
163+ pub sample_rate: u32,
164+ /// Number of blocks per syncframe code.
165+ pub numblkscod: u8,
166+ /// Blocks in this syncframe: 1, 2, 3 or 6.
167+ pub nblocks: usize,
168+ /// Custom channel map of a dependent substream (Table E2.5).
169+ pub chanmap: Option<u16>,
170+ /// The fields Annex E shares with AC-3, so one caller can read either.
171+ pub bsi: Bsi,
172+ /// Full-bandwidth channels, LFE excluded — mirrors `bsi.nfchans`.
173+ pub nfchans: usize,
174+}
175+
176+/// Parse the Annex E `bsi()` from the bytes after the 16-bit sync word.
177+///
178+/// `data` starts at `strmtyp`, which is how a caller has it after matching the
179+/// sync word: `eac3::bsi::parse(&frame[2..])`.
180+pub fn parse(data: &[u8]) -> Result<Eac3Bsi> {
181+ // The frame-size field is relative to the sync word, so a reader that
182+ // starts after it has to be told where zero was: parse against a prefixed
183+ // buffer only if the caller kept the sync word. Here the caller did not,
184+ // so nothing in this function depends on absolute bit position.
185+ parse_after_syncword(&mut BitReader::new(data))
186+}
187+
188+/// [`parse`] against a reader positioned at the sync word.
189+pub fn parse_from(r: &mut BitReader<'_>) -> Result<Eac3Bsi> {
190+ let syncword = r.read_bits(16)? as u16;
191+ if syncword != crate::syncinfo::SYNCWORD {
192+ return Err(Error::corrupt(format!(
193+ "E-AC-3 bsi: syncword {syncword:#06x}, expected 0x0b77"
194+ )));
195+ }
196+ parse_after_syncword(r)
197+}
198+
199+fn parse_after_syncword(r: &mut BitReader<'_>) -> Result<Eac3Bsi> {
200+ let strmtyp = StreamType::from_code(r.read_bits(2)?);
201+ let substreamid = r.read_bits(3)? as u8;
202+ let frmsiz = r.read_bits(11)? as u16;
203+ let fscod = r.read_bits(2)? as u8;
204+ let (fscod2, numblkscod) = if fscod == 3 {
205+ (Some(r.read_bits(2)? as u8), 3)
206+ } else {
207+ (None, r.read_bits(2)? as u8)
208+ };
209+ let sample_rate = match fscod2 {
210+ Some(code) => HALF_RATE[code as usize],
211+ None => crate::tables::SAMPLE_RATE[fscod as usize],
212+ };
213+ if sample_rate == 0 {
214+ return Err(Error::corrupt("E-AC-3 bsi: reserved sample rate code"));
215+ }
216+ let acmod = Acmod::from_code(r.read_bits(3)? as u8);
217+ let lfeon = r.read_bit()?;
218+ let bsid = r.read_bits(5)? as u8;
219+ if !(11..=16).contains(&bsid) {
220+ return Err(Error::unsupported(
221+ format!("E-AC-3 bsi: bsid = {bsid}"),
222+ "Annex E defines bsid 11 to 16; below that the stream is AC-3",
223+ ));
224+ }
225+ let dialnorm = r.read_bits(5)? as u8;
226+ let compr = read_optional(r, 8)?;
227+ let (mut dialnorm2, mut compr2) = (None, None);
228+ if acmod == Acmod::DualMono {
229+ dialnorm2 = Some(r.read_bits(5)? as u8);
230+ compr2 = read_optional(r, 8)?;
231+ }
232+ let mut chanmap = None;
233+ if strmtyp == StreamType::Dependent && r.read_bit()? {
234+ chanmap = Some(r.read_bits(16)? as u16);
235+ }
236+
237+ let mut bsi = Bsi {
238+ bsid,
239+ bsmod: 0,
240+ acmod,
241+ cmixlev: None,
242+ surmixlev: None,
243+ dsurmod: None,
244+ lfeon,
245+ dialnorm,
246+ compr,
247+ langcod: None,
248+ mixlevel: None,
249+ roomtyp: None,
250+ dialnorm2,
251+ compr2,
252+ copyrightb: false,
253+ origbs: false,
254+ dmixmod: None,
255+ ltrt_mixlev: None,
256+ loro_mixlev: None,
257+ dsurexmod: None,
258+ nfchans: acmod.nfchans(),
259+ channels: acmod.nfchans() + usize::from(lfeon),
260+ };
261+ let nblocks = BLOCKS[numblkscod as usize];
262+
263+ // Mixing metadata.
264+ if r.read_bit()? {
265+ if acmod.code() > 2 {
266+ bsi.dmixmod = Some(r.read_bits(2)? as u8);
267+ }
268+ if acmod.code() & 1 != 0 && acmod.code() > 2 {
269+ let ltrtcmixlev = r.read_bits(3)? as u8;
270+ let lorocmixlev = r.read_bits(3)? as u8;
271+ bsi.ltrt_mixlev = Some((ltrtcmixlev, 0));
272+ bsi.loro_mixlev = Some((lorocmixlev, 0));
273+ }
274+ if acmod.code() & 4 != 0 {
275+ let ltrtsurmixlev = r.read_bits(3)? as u8;
276+ let lorosurmixlev = r.read_bits(3)? as u8;
277+ bsi.ltrt_mixlev = Some((bsi.ltrt_mixlev.map_or(0, |v| v.0), ltrtsurmixlev));
278+ bsi.loro_mixlev = Some((bsi.loro_mixlev.map_or(0, |v| v.0), lorosurmixlev));
279+ }
280+ if lfeon && r.read_bit()? {
281+ let _lfemixlevcod = r.read_bits(5)?;
282+ }
283+ if strmtyp == StreamType::Independent {
284+ if r.read_bit()? {
285+ let _pgmscl = r.read_bits(6)?;
286+ }
287+ if acmod == Acmod::DualMono && r.read_bit()? {
288+ let _pgmscl2 = r.read_bits(6)?;
289+ }
290+ if r.read_bit()? {
291+ let _extpgmscl = r.read_bits(6)?;
292+ }
293+ match r.read_bits(2)? {
294+ 1 => {
295+ let _premix = r.read_bits(5)?;
296+ }
297+ 2 => {
298+ let _mixdata = r.read_bits(12)?;
299+ }
300+ 3 => read_mixdata_option4(r, nblocks)?,
301+ _ => {}
302+ }
303+ if acmod.code() < 2 {
304+ if r.read_bit()? {
305+ let _panmean = r.read_bits(8)?;
306+ let _paninfo = r.read_bits(6)?;
307+ }
308+ if acmod == Acmod::DualMono && r.read_bit()? {
309+ let _panmean2 = r.read_bits(8)?;
310+ let _paninfo2 = r.read_bits(6)?;
311+ }
312+ }
313+ if r.read_bit()? {
314+ if numblkscod == 0 {
315+ let _blkmixcfginfo = r.read_bits(5)?;
316+ } else {
317+ for _ in 0..nblocks {
318+ if r.read_bit()? {
319+ let _blkmixcfginfo = r.read_bits(5)?;
320+ }
321+ }
322+ }
323+ }
324+ }
325+ }
326+
327+ // Informational metadata.
328+ if r.read_bit()? {
329+ bsi.bsmod = r.read_bits(3)? as u8;
330+ bsi.copyrightb = r.read_bit()?;
331+ bsi.origbs = r.read_bit()?;
332+ if acmod == Acmod::Stereo {
333+ bsi.dsurmod = Some(r.read_bits(2)? as u8);
334+ let _dheadphonmod = r.read_bits(2)?;
335+ }
336+ if acmod.code() >= 6 {
337+ bsi.dsurexmod = Some(r.read_bits(2)? as u8);
338+ }
339+ if r.read_bit()? {
340+ bsi.mixlevel = Some(r.read_bits(5)? as u8);
341+ bsi.roomtyp = Some(r.read_bits(2)? as u8);
342+ let _adconvtyp = r.read_bit()?;
343+ }
344+ if acmod == Acmod::DualMono && r.read_bit()? {
345+ let _mixlevel2 = r.read_bits(5)?;
346+ let _roomtyp2 = r.read_bits(2)?;
347+ let _adconvtyp2 = r.read_bit()?;
348+ }
349+ if fscod < 3 {
350+ let _sourcefscod = r.read_bit()?;
351+ }
352+ }
353+ if strmtyp == StreamType::Independent && numblkscod != 3 {
354+ let _convsync = r.read_bit()?;
355+ }
356+ if strmtyp == StreamType::Ac3Converted {
357+ let blkid = if numblkscod == 3 { true } else { r.read_bit()? };
358+ if blkid {
359+ let _frmsizecod = r.read_bits(6)?;
360+ }
361+ }
362+ if r.read_bit()? {
363+ let addbsil = r.read_bits(6)? as u64;
364+ r.skip_bits((addbsil + 1) * 8)?;
365+ }
366+
367+ Ok(Eac3Bsi {
368+ strmtyp,
369+ substreamid,
370+ frmsiz,
371+ frame_size: (usize::from(frmsiz) + 1) * 2,
372+ fscod,
373+ fscod2,
374+ sample_rate,
375+ numblkscod,
376+ nblocks,
377+ chanmap,
378+ nfchans: bsi.nfchans,
379+ bsi,
380+ })
381+}
382+
383+fn read_mixdata_option4(r: &mut BitReader<'_>, _nblocks: usize) -> Result<()> {
384+ let mixdeflen = r.read_bits(5)? as u64;
385+ let start = r.bit_position();
386+ if r.read_bit()? {
387+ let _premixcmpsel = r.read_bit()?;
388+ let _drcsrc = r.read_bit()?;
389+ let _premixcmpscl = r.read_bits(3)?;
390+ for _ in 0..7 {
391+ if r.read_bit()? {
392+ let _scale = r.read_bits(4)?;
393+ }
394+ }
395+ if r.read_bit()? {
396+ for _ in 0..2 {
397+ if r.read_bit()? {
398+ let _aux = r.read_bits(4)?;
399+ }
400+ }
401+ }
402+ }
403+ if r.read_bit()? {
404+ let _spchdat = r.read_bits(5)?;
405+ if r.read_bit()? {
406+ let _spchdat1 = r.read_bits(5)?;
407+ let _spchan1att = r.read_bits(2)?;
408+ if r.read_bit()? {
409+ let _spchdat2 = r.read_bits(5)?;
410+ let _spchan2att = r.read_bits(3)?;
411+ }
412+ }
413+ }
414+ // The field is a fixed-length block: whatever the sub-fields did not use is
415+ // padding, so seek to its stated end rather than trusting the parse.
416+ let used = r.bit_position() - start;
417+ let total = 8 * (mixdeflen + 2);
418+ if used > total {
419+ return Err(Error::corrupt(
420+ "E-AC-3 bsi: mixdata sub-fields overran mixdeflen",
421+ ));
422+ }
423+ r.skip_bits(total - used)?;
424+ Ok(())
425+}
426+
427+fn read_optional(r: &mut BitReader<'_>, n: u32) -> Result<Option<u8>> {
428+ if r.read_bit()? {
429+ Ok(Some(r.read_bits(n)? as u8))
430+ } else {
431+ Ok(None)
432+ }
433+}
434+
435+/// The frame-level strategy fields of `audfrm()` (Table E1.3).
436+#[derive(Debug, Clone)]
437+struct AudFrm {
438+ expstre: bool,
439+ ahte: bool,
440+ snroffststr: u32,
441+ transproce: bool,
442+ blkswe: bool,
443+ dithflage: bool,
444+ bamode: bool,
445+ frmfgaincode: bool,
446+ dbaflde: bool,
447+ skipflde: bool,
448+ spxattene: bool,
449+ cplstre: [bool; 6],
450+ cplinu: [bool; 6],
451+ cplexpstr: [Strategy; 6],
452+ chexpstr: [[Strategy; 5]; 6],
453+ lfeexpstr: [Strategy; 6],
454+ frmcsnroffst: u8,
455+ frmfsnroffst: u8,
456+ /// Per-channel spectral extension attenuation, from `spxattene`.
457+ chinspxatten: [bool; 5],
458+ spxattencod: [u8; 5],
459+ /// Which channels use the adaptive hybrid transform this frame.
460+ cplahtinu: bool,
461+ chahtinu: [bool; 5],
462+ lfeahtinu: bool,
463+}
464+
465+fn parse_audfrm(r: &mut BitReader<'_>, hdr: &Eac3Bsi) -> Result<AudFrm> {
466+ let nfchans = hdr.nfchans;
467+ let nblocks = hdr.nblocks;
468+ let acmod = hdr.bsi.acmod;
469+ let (expstre, ahte) = if hdr.numblkscod == 3 {
470+ (r.read_bit()?, r.read_bit()?)
471+ } else {
472+ (true, false)
473+ };
474+ let mut frm = AudFrm {
475+ expstre,
476+ ahte,
477+ snroffststr: r.read_bits(2)?,
478+ transproce: r.read_bit()?,
479+ blkswe: r.read_bit()?,
480+ dithflage: r.read_bit()?,
481+ bamode: r.read_bit()?,
482+ frmfgaincode: r.read_bit()?,
483+ dbaflde: r.read_bit()?,
484+ skipflde: r.read_bit()?,
485+ spxattene: r.read_bit()?,
486+ cplstre: [false; 6],
487+ cplinu: [false; 6],
488+ cplexpstr: [Strategy::Reuse; 6],
489+ chexpstr: [[Strategy::Reuse; 5]; 6],
490+ lfeexpstr: [Strategy::Reuse; 6],
491+ frmcsnroffst: 0,
492+ frmfsnroffst: 0,
493+ chinspxatten: [false; 5],
494+ spxattencod: [0; 5],
495+ cplahtinu: false,
496+ chahtinu: [false; 5],
497+ lfeahtinu: false,
498+ };
499+
500+ frm.cplstre[0] = true;
501+ if acmod.code() > 1 {
502+ frm.cplinu[0] = r.read_bit()?;
503+ for blk in 1..nblocks {
504+ frm.cplstre[blk] = r.read_bit()?;
505+ frm.cplinu[blk] = if frm.cplstre[blk] {
506+ r.read_bit()?
507+ } else {
508+ frm.cplinu[blk - 1]
509+ };
510+ }
511+ }
512+
513+ if frm.expstre {
514+ for blk in 0..nblocks {
515+ if frm.cplinu[blk] {
516+ frm.cplexpstr[blk] = Strategy::from_code(r.read_bits(2)?);
517+ }
518+ for ch in 0..nfchans {
519+ frm.chexpstr[blk][ch] = Strategy::from_code(r.read_bits(2)?);
520+ }
521+ }
522+ } else {
523+ let ncplblks = frm.cplinu[..nblocks].iter().filter(|&&v| v).count();
524+ if acmod.code() > 1 && ncplblks > 0 {
525+ let code = r.read_bits(5)? as usize;
526+ for blk in 0..nblocks {
527+ frm.cplexpstr[blk] = Strategy::from_code(u32::from(FRAME_EXP_STRATEGY[code][blk]));
528+ }
529+ }
530+ for ch in 0..nfchans {
531+ let code = r.read_bits(5)? as usize;
532+ for blk in 0..nblocks {
533+ frm.chexpstr[blk][ch] =
534+ Strategy::from_code(u32::from(FRAME_EXP_STRATEGY[code][blk]));
535+ }
536+ }
537+ }
538+ if hdr.bsi.lfeon {
539+ for blk in 0..nblocks {
540+ frm.lfeexpstr[blk] = if r.read_bit()? {
541+ Strategy::D15
542+ } else {
543+ Strategy::Reuse
544+ };
545+ }
546+ }
547+ if hdr.strmtyp == StreamType::Independent {
548+ let convexpstre = if hdr.numblkscod != 3 {
549+ r.read_bit()?
550+ } else {
551+ true
552+ };
553+ if convexpstre {
554+ for _ in 0..nfchans {
555+ let _convexpstr = r.read_bits(5)?;
556+ }
557+ }
558+ }
559+ if frm.ahte {
560+ // The AHT flags are only sent where a single exponent region covers
561+ // the frame; counting the regions is how the standard states it.
562+ let ncplblks = frm.cplinu[..nblocks].iter().filter(|&&v| v).count();
563+ let ncplregs = (0..nblocks)
564+ .filter(|&b| frm.cplstre[b] || frm.cplexpstr[b] != Strategy::Reuse)
565+ .count();
566+ let cplahtinu = if ncplblks == 6 && ncplregs == 1 {
567+ r.read_bit()?
568+ } else {
569+ false
570+ };
571+ let mut chahtinu = [false; 5];
572+ for (ch, slot) in chahtinu.iter_mut().enumerate().take(nfchans) {
573+ let nchregs = (0..nblocks)
574+ .filter(|&b| frm.chexpstr[b][ch] != Strategy::Reuse)
575+ .count();
576+ if nchregs == 1 {
577+ *slot = r.read_bit()?;
578+ }
579+ }
580+ let mut lfeahtinu = false;
581+ let nlferegs = (0..nblocks)
582+ .filter(|&b| frm.lfeexpstr[b] != Strategy::Reuse)
583+ .count();
584+ if hdr.bsi.lfeon && nlferegs == 1 {
585+ lfeahtinu = r.read_bit()?;
586+ }
587+ frm.cplahtinu = cplahtinu;
588+ frm.chahtinu = chahtinu;
589+ frm.lfeahtinu = lfeahtinu;
590+ }
591+ if frm.snroffststr == 0 {
592+ frm.frmcsnroffst = r.read_bits(6)? as u8;
593+ frm.frmfsnroffst = r.read_bits(4)? as u8;
594+ }
595+ if frm.transproce {
596+ for _ in 0..nfchans {
597+ if r.read_bit()? {
598+ let _transprocloc = r.read_bits(10)?;
599+ let _transproclen = r.read_bits(8)?;
600+ }
601+ }
602+ }
603+ if frm.spxattene {
604+ for ch in 0..nfchans {
605+ frm.chinspxatten[ch] = r.read_bit()?;
606+ if frm.chinspxatten[ch] {
607+ frm.spxattencod[ch] = r.read_bits(5)? as u8;
608+ }
609+ }
610+ }
611+ if hdr.numblkscod != 0 && r.read_bit()? {
612+ // §E2.3.2.27: the field is sized from the frame size.
613+ // §E2.3.2.27: (numblks - 1) * (4 + ceil(log2(frmsiz + 1))).
614+ let words = u32::from(hdr.frmsiz) + 1;
615+ let nblkstrtbits = (nblocks as u32 - 1) * (4 + words.next_power_of_two().trailing_zeros());
616+ r.skip_bits(u64::from(nblkstrtbits))?;
617+ }
618+ Ok(frm)
619+}
620+
621+/// Decode one Annex E syncframe: header already parsed, reader positioned at
622+/// `audfrm()`.
623+pub(crate) fn decode_frame(
624+ core: &mut Core,
625+ r: &mut BitReader<'_>,
626+ hdr: &Eac3Bsi,
627+ out: &mut Vec<f32>,
628+) -> Result<()> {
629+ let frm = parse_audfrm(r, hdr)?;
630+ // Reduced rates index the hearing threshold table by the full rate they
631+ // halve, which is what fscod2 numbers.
632+ let fscod = hdr.fscod2.map_or(hdr.fscod as usize, |c| c as usize);
633+ core.start_frame(Syntax::Eac3, fscod, &hdr.bsi);
634+ // §E1.3, end of audfrm(): the coupling coordinates, the spectral extension
635+ // coordinates and the leak state are all sent afresh in the first block of
636+ // every frame that uses them.
637+ core.first_cplco = [true; 5];
638+ core.first_spxco = [true; 5];
639+ core.first_cplleak = true;
640+ core.chinspxatten = frm.chinspxatten;
641+ core.spxattencod = frm.spxattencod;
642+ core.aht = [false; CHANNELS];
643+ core.aht[CPL] = frm.cplahtinu;
644+ core.aht[LFE] = frm.lfeahtinu;
645+ core.aht[..5].copy_from_slice(&frm.chahtinu);
646+ for blk in 0..hdr.nblocks {
647+ parse_block(core, r, &frm, hdr, blk)?;
648+ // §3.6: synthesis sits between decoupling and rematrixing — it fills
649+ // the band above the coded one, which rematrixing never reaches.
650+ core.decouple();
651+ if core.spxinu {
652+ for ch in 0..core.nfchans {
653+ if core.chinspx[ch] {
654+ spectral_extension(core, ch);
655+ }
656+ }
657+ }
658+ core.rematrix();
659+ core.apply_gain();
660+ core.transform(out);
661+ }
662+ Ok(())
663+}
664+
665+#[allow(clippy::too_many_lines)]
666+fn parse_block(
667+ core: &mut Core,
668+ r: &mut BitReader<'_>,
669+ frm: &AudFrm,
670+ hdr: &Eac3Bsi,
671+ blk: usize,
672+) -> Result<()> {
673+ let nfchans = core.nfchans;
674+ let acmod = core.acmod;
675+
676+ for ch in 0..nfchans {
677+ core.blksw[ch] = if frm.blkswe { r.read_bit()? } else { false };
678+ }
679+ for ch in 0..nfchans {
680+ core.dithflag[ch] = if frm.dithflage { r.read_bit()? } else { true };
681+ }
682+ if r.read_bit()? {
683+ core.dynrng[0] = core.gain(r.read_bits(8)? as u8);
684+ } else if blk == 0 {
685+ core.dynrng[0] = 1.0;
686+ }
687+ if acmod == Acmod::DualMono {
688+ if r.read_bit()? {
689+ core.dynrng[1] = core.gain(r.read_bits(8)? as u8);
690+ } else if blk == 0 {
691+ core.dynrng[1] = 1.0;
692+ }
693+ }
694+
695+ // Spectral extension strategy (§E2.2.4).
696+ let spxstre = blk == 0 || r.read_bit()?;
697+ if spxstre {
698+ core.spxinu = r.read_bit()?;
699+ if core.spxinu {
700+ if acmod == Acmod::Mono {
701+ core.chinspx[0] = true;
702+ } else {
703+ for ch in 0..nfchans {
704+ core.chinspx[ch] = r.read_bit()?;
705+ }
706+ }
707+ core.spxstrtf = r.read_bits(2)? as usize;
708+ let spxbegf = r.read_bits(3)? as usize;
709+ let spxendf = r.read_bits(3)? as usize;
710+ core.spxbegf = spxbegf;
711+ core.spx_begin = if spxbegf < 6 {
712+ spxbegf + 2
713+ } else {
714+ spxbegf * 2 - 3
715+ };
716+ core.spx_end = if spxendf < 3 {
717+ spxendf + 5
718+ } else {
719+ spxendf * 2 + 3
720+ };
721+ if core.spx_end <= core.spx_begin || core.spx_end > 17 {
722+ return Err(Error::corrupt(format!(
723+ "E-AC-3 spectral extension: sub-bands {}..{}",
724+ core.spx_begin, core.spx_end
725+ )));
726+ }
727+ if r.read_bit()? {
728+ for bnd in core.spx_begin + 1..core.spx_end {
729+ core.spxbndstrc[bnd] = r.read_bit()?;
730+ }
731+ } else if blk == 0 {
732+ core.spxbndstrc = DEFAULT_SPXBNDSTRC;
733+ }
734+ // §3.6.2: sub-bands merge into bands as spxbndstrc says.
735+ core.nspxbnds = 1;
736+ core.spxbndsz[0] = 12;
737+ for bnd in core.spx_begin + 1..core.spx_end {
738+ if core.spxbndstrc[bnd] {
739+ core.spxbndsz[core.nspxbnds - 1] += 12;
740+ } else {
741+ core.spxbndsz[core.nspxbnds] = 12;
742+ core.nspxbnds += 1;
743+ }
744+ }
745+ } else {
746+ core.chinspx = [false; 5];
747+ core.first_spxco = [true; 5];
748+ }
749+ }
750+
751+ // Spectral extension coordinates.
752+ if core.spxinu {
753+ for ch in 0..nfchans {
754+ if !core.chinspx[ch] {
755+ core.first_spxco[ch] = true;
756+ continue;
757+ }
758+ let spxcoe = if core.first_spxco[ch] {
759+ core.first_spxco[ch] = false;
760+ true
761+ } else {
762+ r.read_bit()?
763+ };
764+ if !spxcoe {
765+ continue;
766+ }
767+ let spxblnd = r.read_bits(5)? as f32 / 32.0;
768+ let mstrspxco = r.read_bits(2)? as i32;
769+ let mut spxmant = spx_band_start(core.spx_begin) as f32;
770+ let end = spx_band_start(core.spx_end) as f32;
771+ for bnd in 0..core.nspxbnds {
772+ let exp = r.read_bits(4)? as i32;
773+ let mant = r.read_bits(2)? as f32;
774+ let value = if exp == 15 {
775+ mant / 4.0
776+ } else {
777+ (mant + 4.0) / 8.0
778+ };
779+ core.spxco[ch][bnd] =
780+ value * crate::mantissa::scale((exp + 3 * mstrspxco).min(24) as u8);
781+ // §3.6.4.2.1: the blend follows the band's frequency midpoint.
782+ let bandsize = core.spxbndsz[bnd] as f32;
783+ let nratio = ((spxmant + 0.5 * bandsize) / end - spxblnd).clamp(0.0, 1.0);
784+ core.nblend[ch][bnd] = nratio.sqrt();
785+ core.sblend[ch][bnd] = (1.0 - nratio).sqrt();
786+ spxmant += bandsize;
787+ }
788+ }
789+ }
790+
791+ // Coupling strategy.
792+ // `cplstre[blk]` and `cplinu[blk]` were read in audfrm(), not here.
793+ if frm.cplstre[blk] {
794+ if frm.cplinu[blk] {
795+ let ecplinu = r.read_bit()?;
796+ if ecplinu {
797+ return Err(Error::unsupported(
798+ "E-AC-3 enhanced coupling (ecplinu)",
799+ "the amplitude/angle/chaos parameterisation is not \
800+ implemented in this build",
801+ ));
802+ }
803+ if acmod == Acmod::Stereo {
804+ core.chincpl[0] = true;
805+ core.chincpl[1] = true;
806+ } else {
807+ for ch in 0..nfchans {
808+ core.chincpl[ch] = r.read_bit()?;
809+ }
810+ }
811+ if acmod == Acmod::Stereo {
812+ core.phsflginu = r.read_bit()?;
813+ }
814+ core.cplbegf = r.read_bits(4)? as usize;
815+ // §E3.3.1: with spectral extension the coupling end is derived
816+ // from spxbegf rather than sent, and may go negative.
817+ let cplendf: i32 = if core.spxinu {
818+ if core.spxbegf < 6 {
819+ core.spxbegf as i32 - 2
820+ } else {
821+ core.spxbegf as i32 * 2 - 7
822+ }
823+ } else {
824+ r.read_bits(4)? as i32
825+ };
826+ let ncplsubnd = 3 + cplendf - core.cplbegf as i32;
827+ if !(1..=18).contains(&ncplsubnd) {
828+ return Err(Error::corrupt(format!(
829+ "E-AC-3 coupling: cplbegf {} with cplendf {cplendf}",
830+ core.cplbegf
831+ )));
832+ }
833+ core.ncplsubnd = ncplsubnd as usize;
834+ core.strtmant[CPL] = 37 + 12 * core.cplbegf;
835+ core.endmant[CPL] = (37 + 12 * (cplendf + 3)) as usize;
836+ // §E2.3.3.14: the banding array is indexed by *absolute* coupling
837+ // sub-band, like the spectral extension one — which is what makes
838+ // the default of Table E2.12 mean the same thing wherever coupling
839+ // starts. `cplbndstrce == 0` in the first block using coupling
840+ // means the default; in a later block it means "reuse".
841+ if r.read_bit()? {
842+ for bnd in 1..core.ncplsubnd {
843+ core.cplbndstrc[core.cplbegf + bnd] = r.read_bit()?;
844+ }
845+ } else if blk == 0 {
846+ core.cplbndstrc = DEFAULT_CPLBNDSTRC;
847+ }
848+ core.cplband[0] = 0;
849+ core.ncplbnd = 1;
850+ for sbnd in 1..core.ncplsubnd {
851+ if !core.cplbndstrc[core.cplbegf + sbnd] {
852+ core.ncplbnd += 1;
853+ }
854+ core.cplband[sbnd] = core.ncplbnd - 1;
855+ }
856+ } else {
857+ core.chincpl = [false; 5];
858+ core.phsflginu = false;
859+ }
860+ }
861+ core.cplinu = frm.cplinu[blk];
862+
863+ // Coupling coordinates. Annex E sends them once per channel per coupling
864+ // run: the first block of the run has them implicitly.
865+ if core.cplinu {
866+ let mut cplcoe = [false; 5];
867+ for ch in 0..nfchans {
868+ if !core.chincpl[ch] {
869+ core.first_cplco[ch] = true;
870+ continue;
871+ }
872+ cplcoe[ch] = if core.first_cplco[ch] {
873+ core.first_cplco[ch] = false;
874+ true
875+ } else {
876+ r.read_bit()?
877+ };
878+ if cplcoe[ch] {
879+ let mstrcplco = r.read_bits(2)? as i32;
880+ for bnd in 0..core.ncplbnd {
881+ let cplcoexp = r.read_bits(4)? as i32;
882+ let cplcomant = r.read_bits(4)? as f32;
883+ let mant = if cplcoexp == 15 {
884+ cplcomant / 16.0
885+ } else {
886+ (cplcomant + 16.0) / 32.0
887+ };
888+ core.cplco[ch][bnd] =
889+ mant * crate::mantissa::scale((cplcoexp + 3 * mstrcplco).min(24) as u8);
890+ }
891+ }
892+ }
893+ if acmod == Acmod::Stereo && core.phsflginu && (cplcoe[0] || cplcoe[1]) {
894+ for bnd in 0..core.ncplbnd {
895+ core.phsflg[bnd] = r.read_bit()?;
896+ }
897+ }
898+ }
899+
900+ // Rematrixing.
901+ if acmod == Acmod::Stereo {
902+ let rematstr = blk == 0 || r.read_bit()?;
903+ if rematstr {
904+ // §E3.3.2.
905+ core.nrematbnd = if core.cplinu {
906+ match core.cplbegf {
907+ 0 => 2,
908+ 1 | 2 => 3,
909+ _ => 4,
910+ }
911+ } else if core.spxinu {
912+ if core.spxbegf < 2 { 3 } else { 4 }
913+ } else {
914+ 4
915+ };
916+ for bnd in 0..core.nrematbnd {
917+ core.rematflg[bnd] = r.read_bit()?;
918+ }
919+ }
920+ }
921+
922+ // Bandwidth codes and exponents.
923+ core.expstr = [Strategy::Reuse; CHANNELS];
924+ core.expstr[CPL] = frm.cplexpstr[blk];
925+ for ch in 0..nfchans {
926+ core.expstr[ch] = frm.chexpstr[blk][ch];
927+ }
928+ core.expstr[LFE] = frm.lfeexpstr[blk];
929+ for ch in 0..nfchans {
930+ if core.expstr[ch] != Strategy::Reuse {
931+ if core.chincpl[ch] && core.cplinu {
932+ core.endmant[ch] = core.strtmant[CPL];
933+ } else if core.chinspx[ch] && core.spxinu {
934+ // §E3.3.3: the coded band ends where synthesis begins.
935+ core.endmant[ch] = spx_band_start(core.spx_begin);
936+ } else {
937+ let chbwcod = r.read_bits(6)? as usize;
938+ if chbwcod > 60 {
939+ return Err(Error::corrupt(format!(
940+ "E-AC-3 audblk: chbwcod = {chbwcod} > 60"
941+ )));
942+ }
943+ core.endmant[ch] = (chbwcod + 12) * 3 + 37;
944+ }
945+ }
946+ }
947+ if core.cplinu && core.expstr[CPL] != Strategy::Reuse {
948+ let absexp = (r.read_bits(4)? << 1) as u8;
949+ let (start, end) = (core.strtmant[CPL], core.endmant[CPL]);
950+ let ngrps = core.expstr[CPL].coupling_groups(start, end);
951+ exps::decode(
952+ r,
953+ core.expstr[CPL],
954+ ngrps,
955+ absexp,
956+ start,
957+ &mut core.exps[CPL],
958+ )?;
959+ }
960+ for ch in 0..nfchans {
961+ if core.expstr[ch] == Strategy::Reuse {
962+ continue;
963+ }
964+ let absexp = r.read_bits(4)? as u8;
965+ core.exps[ch][0] = absexp;
966+ let ngrps = core.expstr[ch].fbw_groups(core.endmant[ch]);
967+ exps::decode(r, core.expstr[ch], ngrps, absexp, 1, &mut core.exps[ch])?;
968+ let _gainrng = r.read_bits(2)?;
969+ }
970+ if core.lfeon && core.expstr[LFE] != Strategy::Reuse {
971+ let absexp = r.read_bits(4)? as u8;
972+ core.exps[LFE][0] = absexp;
973+ exps::decode(r, Strategy::D15, 2, absexp, 1, &mut core.exps[LFE])?;
974+ }
975+
976+ // Bit allocation parameters.
977+ if frm.bamode {
978+ if r.read_bit()? {
979+ core.ba = BitAllocParams {
980+ sdcycod: r.read_bits(2)? as u8,
981+ fdcycod: r.read_bits(2)? as u8,
982+ sgaincod: r.read_bits(2)? as u8,
983+ dbpbcod: r.read_bits(2)? as u8,
984+ floorcod: r.read_bits(3)? as u8,
985+ };
986+ }
987+ } else {
988+ core.ba = BitAllocParams {
989+ sdcycod: 2,
990+ fdcycod: 1,
991+ sgaincod: 1,
992+ dbpbcod: 2,
993+ floorcod: 7,
994+ };
995+ }
996+
997+ // SNR offsets.
998+ if frm.snroffststr == 0 {
999+ core.csnroffst = frm.frmcsnroffst;
1000+ core.fsnroffst = [frm.frmfsnroffst; CHANNELS];
1001+ } else {
1002+ let snroffste = blk == 0 || r.read_bit()?;
1003+ if snroffste {
1004+ core.csnroffst = r.read_bits(6)? as u8;
1005+ if frm.snroffststr == 1 {
1006+ let blkfsnroffst = r.read_bits(4)? as u8;
1007+ core.fsnroffst = [blkfsnroffst; CHANNELS];
1008+ } else {
1009+ if core.cplinu {
1010+ core.fsnroffst[CPL] = r.read_bits(4)? as u8;
1011+ }
1012+ for ch in 0..nfchans {
1013+ core.fsnroffst[ch] = r.read_bits(4)? as u8;
1014+ }
1015+ if core.lfeon {
1016+ core.fsnroffst[LFE] = r.read_bits(4)? as u8;
1017+ }
1018+ }
1019+ }
1020+ }
1021+
1022+ // Fast gain codes.
1023+ let fgaincode = frm.frmfgaincode && r.read_bit()?;
1024+ if fgaincode {
1025+ if core.cplinu {
1026+ core.fgaincod[CPL] = r.read_bits(3)? as u8;
1027+ }
1028+ for ch in 0..nfchans {
1029+ core.fgaincod[ch] = r.read_bits(3)? as u8;
1030+ }
1031+ if core.lfeon {
1032+ core.fgaincod[LFE] = r.read_bits(3)? as u8;
1033+ }
1034+ } else {
1035+ core.fgaincod = [4; CHANNELS];
1036+ }
1037+ if hdr.strmtyp == StreamType::Independent && r.read_bit()? {
1038+ let _convsnroffst = r.read_bits(10)?;
1039+ }
1040+ if core.cplinu {
1041+ let cplleake = if core.first_cplleak {
1042+ core.first_cplleak = false;
1043+ true
1044+ } else {
1045+ r.read_bit()?
1046+ };
1047+ if cplleake {
1048+ core.cplleak = (
1049+ ((r.read_bits(3)? as i32) << 8) + 768,
1050+ ((r.read_bits(3)? as i32) << 8) + 768,
1051+ );
1052+ }
1053+ }
1054+
1055+ // Delta bit allocation.
1056+ if frm.dbaflde && r.read_bit()? {
1057+ let mut deltbae = [0u32; CHANNELS];
1058+ if core.cplinu {
1059+ deltbae[CPL] = r.read_bits(2)?;
1060+ }
1061+ for ch in 0..nfchans {
1062+ deltbae[ch] = r.read_bits(2)?;
1063+ }
1064+ let mut order = Vec::with_capacity(CHANNELS);
1065+ if core.cplinu {
1066+ order.push(CPL);
1067+ }
1068+ order.extend(0..nfchans);
1069+ for ch in order {
1070+ match deltbae[ch] {
1071+ 0 => {}
1072+ 1 => {
1073+ core.deltba_on[ch] = true;
1074+ let nseg = r.read_bits(3)? as usize + 1;
1075+ let mut dba = DeltaBa {
1076+ nseg,
1077+ ..DeltaBa::default()
1078+ };
1079+ for seg in 0..nseg {
1080+ dba.offset[seg] = r.read_bits(5)? as u8;
1081+ dba.length[seg] = r.read_bits(4)? as u8;
1082+ dba.delta[seg] = r.read_bits(3)? as u8;
1083+ }
1084+ core.deltba[ch] = dba;
1085+ }
1086+ _ => core.deltba_on[ch] = false,
1087+ }
1088+ }
1089+ }
1090+
1091+ if frm.skipflde && r.read_bit()? {
1092+ let skipl = r.read_bits(9)? as u64;
1093+ r.skip_bits(skipl * 8)?;
1094+ }
1095+
1096+ core.allocate();
1097+ core.read_mantissas(r, blk)
1098+}
1099+
1100+/// §3.6.4: synthesise one channel's high band — translate, blend with noise,
1101+/// and scale to the transmitted envelope.
1102+fn spectral_extension(core: &mut Core, ch: usize) {
1103+ let copystart = spx_band_start(core.spxstrtf);
1104+ let copyend = spx_band_start(core.spx_begin);
1105+ let nbnds = core.nspxbnds;
1106+
1107+ // §3.6.4.1: translation, wrapping the copy region as it runs out.
1108+ let mut wrapflag = [false; 18];
1109+ let mut copyindex = copystart;
1110+ let mut insertindex = copyend;
1111+ for bnd in 0..nbnds {
1112+ let bandsize = core.spxbndsz[bnd];
1113+ if copyindex + bandsize > copyend {
1114+ copyindex = copystart;
1115+ wrapflag[bnd] = true;
1116+ }
1117+ for _ in 0..bandsize {
1118+ if copyindex == copyend {
1119+ copyindex = copystart;
1120+ }
1121+ if insertindex >= COEFFS {
1122+ break;
1123+ }
1124+ core.coeffs[ch][insertindex] = core.coeffs[ch][copyindex];
1125+ insertindex += 1;
1126+ copyindex += 1;
1127+ }
1128+ }
1129+
1130+ // §3.6.4.2.2: banded RMS energy of what was just translated.
1131+ let mut rms = [0.0f32; 18];
1132+ let mut spxmant = copyend;
1133+ for bnd in 0..nbnds {
1134+ let bandsize = core.spxbndsz[bnd];
1135+ let mut accum = 0.0f32;
1136+ for _ in 0..bandsize {
1137+ if spxmant >= COEFFS {
1138+ break;
1139+ }
1140+ let v = core.coeffs[ch][spxmant];
1141+ accum += v * v;
1142+ spxmant += 1;
1143+ }
1144+ rms[bnd] = (accum / bandsize as f32).sqrt();
1145+ }
1146+
1147+ // §3.6.4.2.3: a 5-tap notch across the baseband border and every wrap.
1148+ if core.chinspxatten[ch] {
1149+ let tab = SPXATTENTAB[(core.spxattencod[ch] as usize).min(31)];
1150+ let taps = [tab[0], tab[1], tab[2], tab[1], tab[0]];
1151+ let mut filtbin = copyend.saturating_sub(2);
1152+ for tap in taps {
1153+ if filtbin < COEFFS {
1154+ core.coeffs[ch][filtbin] *= tap;
1155+ }
1156+ filtbin += 1;
1157+ }
1158+ filtbin += core.spxbndsz[0];
1159+ for bnd in 1..nbnds {
1160+ if wrapflag[bnd] {
1161+ filtbin -= 5;
1162+ for tap in taps {
1163+ if filtbin < COEFFS {
1164+ core.coeffs[ch][filtbin] *= tap;
1165+ }
1166+ filtbin += 1;
1167+ }
1168+ }
1169+ filtbin += core.spxbndsz[bnd];
1170+ }
1171+ }
1172+
1173+ // §3.6.4.2.4 and §3.6.4.3: blend in energy-matched noise, then scale to
1174+ // the transmitted envelope.
1175+ let mut spxmant = copyend;
1176+ for bnd in 0..nbnds {
1177+ let bandsize = core.spxbndsz[bnd];
1178+ let nscale = rms[bnd] * core.nblend[ch][bnd];
1179+ let sscale = core.sblend[ch][bnd];
1180+ let coord = core.spxco[ch][bnd] * 32.0;
1181+ for _ in 0..bandsize {
1182+ if spxmant >= COEFFS {
1183+ break;
1184+ }
1185+ let noise = core.spx_noise.unit_variance();
1186+ core.coeffs[ch][spxmant] = (core.coeffs[ch][spxmant] * sscale + noise * nscale) * coord;
1187+ spxmant += 1;
1188+ }
1189+ }
1190+}
1191+
1192+#[cfg(test)]
1193+mod tests {
1194+ use super::*;
1195+
1196+ #[test]
1197+ fn frame_exponent_strategy_table_is_the_printed_one() {
1198+ // Table E2.10 corners: code 0 is D15 then all reuse, code 31 is D45
1199+ // in every block, and code 16 opens D45, D15.
1200+ assert_eq!(FRAME_EXP_STRATEGY[0], [1, 0, 0, 0, 0, 0]);
1201+ assert_eq!(FRAME_EXP_STRATEGY[31], [3, 3, 3, 3, 3, 3]);
1202+ assert_eq!(FRAME_EXP_STRATEGY[16], [3, 1, 0, 0, 0, 0]);
1203+ // Every combination starts by sending exponents, never by reusing
1204+ // exponents that do not exist yet.
1205+ for row in FRAME_EXP_STRATEGY {
1206+ assert_ne!(row[0], 0);
1207+ }
1208+ }
1209+}