oxideav-ac4 0.0.7

Pure-Rust Dolby AC-4 audio decoder foundation for oxideav — sync, TOC, presentation and substream parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! A-SPX envelope / noise Huffman codebooks (Annex A.2, Tables A.16..A.33).
//!
//! The 18 A-SPX Huffman codebooks are transcribed verbatim from the
//! normative accompaniment file `ts_103190_tables.c` that ETSI publishes
//! alongside TS 103 190-1. The tables are normative numeric constants —
//! they are the bits the decoder has to match against when peeling
//! `aspx_ec_data()` payloads.
//!
//! Each codebook exposes a pair of `&'static` slices:
//!
//! * `ASPX_HCB_*_LEN[N]` — codeword bit widths, indexed by symbol.
//! * `ASPX_HCB_*_CW[N]`  — MSB-first codewords, indexed by symbol.
//!
//! plus the matching `ASPX_HCB_*_META.codebook_length` header field
//! (surfaced in [`crate::aspx`]) and, for the DF / DT flavours, a
//! `cb_off` offset so the caller can recover the delta as
//! `symbol_index - cb_off`.
//!
//! Naming — deliberately mirroring the C accompaniment file so the
//! mapping is 1:1 and auditable:
//!
//! ```text
//!   ASPX_HCB_ENV_LEVEL_15_F0   ENV_LEVEL_15_DF   ENV_LEVEL_15_DT
//!   ASPX_HCB_ENV_BALANCE_15_F0 ENV_BALANCE_15_DF ENV_BALANCE_15_DT
//!   ASPX_HCB_ENV_LEVEL_30_F0   ENV_LEVEL_30_DF   ENV_LEVEL_30_DT
//!   ASPX_HCB_ENV_BALANCE_30_F0 ENV_BALANCE_30_DF ENV_BALANCE_30_DT
//!   ASPX_HCB_NOISE_LEVEL_F0    NOISE_LEVEL_DF    NOISE_LEVEL_DT
//!   ASPX_HCB_NOISE_BALANCE_F0  NOISE_BALANCE_DF  NOISE_BALANCE_DT
//! ```
//!
//! The widest codeword across all 18 tables is 23 bits
//! (`ASPX_HCB_ENV_LEVEL_30_DF`), so `u32` is more than enough.

// Table A.16: ASPX_HCB_ENV_LEVEL_15_F0 (codebook_length = 71, cb_off = 0).
pub static ASPX_HCB_ENV_LEVEL_15_F0_LEN: &[u8] = &[
    7, 9, 9, 9, 9, 8, 8, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5,
    5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 6, 6, 6, 7, 8, 9, 10, 11, 12, 14, 14, 16, 16, 17, 17, 18, 18,
    18, 18, 18, 18, 18, 18, 17, 17, 17, 17, 16,
];

pub static ASPX_HCB_ENV_LEVEL_15_F0_CW: &[u32] = &[
    0x0003e, 0x0007e, 0x0007f, 0x000be, 0x000bf, 0x0003e, 0x0005e, 0x00016, 0x00017, 0x0001e,
    0x0002e, 0x0005e, 0x00012, 0x0000a, 0x0000c, 0x0000e, 0x00013, 0x00016, 0x0001a, 0x0001b,
    0x00022, 0x00023, 0x00004, 0x00008, 0x0000c, 0x0000e, 0x00012, 0x00014, 0x00015, 0x0001a,
    0x00000, 0x00016, 0x00018, 0x0001e, 0x0001c, 0x00019, 0x0001d, 0x0001f, 0x00001, 0x0001b,
    0x00013, 0x00010, 0x0000a, 0x0002e, 0x0001e, 0x0000d, 0x0003f, 0x000be, 0x0017e, 0x002fe,
    0x005fe, 0x00bfe, 0x02ffe, 0x02ffc, 0x0bffc, 0x0bff6, 0x17ffa, 0x17ffb, 0x2fff8, 0x2fff9,
    0x2fffa, 0x2fffb, 0x2fffc, 0x2fffd, 0x2fffe, 0x2ffff, 0x17fe8, 0x17fe9, 0x17fea, 0x17feb,
    0x0bff7,
];

// Table A.17: ASPX_HCB_ENV_LEVEL_15_DF (codebook_length = 141, cb_off = 70).
pub static ASPX_HCB_ENV_LEVEL_15_DF_LEN: &[u8] = &[
    21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 17, 18, 19, 19, 21, 19,
    19, 19, 21, 20, 20, 20, 16, 17, 19, 19, 18, 18, 17, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13,
    13, 13, 13, 13, 12, 12, 12, 12, 12, 11, 10, 10, 9, 8, 8, 7, 6, 6, 5, 4, 3, 2, 2, 3, 4, 6, 7, 7,
    8, 9, 10, 11, 11, 12, 13, 13, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 17, 17, 18, 17, 17,
    16, 17, 18, 19, 20, 21, 19, 19, 18, 16, 19, 21, 16, 20, 20, 21, 21, 20, 20, 19, 20, 19, 17, 21,
    21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
];

pub static ASPX_HCB_ENV_LEVEL_15_DF_CW: &[u32] = &[
    0xfeff2, 0xfeff3, 0xfeff4, 0xfeff5, 0xfeff6, 0xfeff7, 0xfeff8, 0xfeff9, 0xfeffa, 0xfeffb,
    0xfeffc, 0xfeffd, 0xfeffe, 0xfefff, 0xff5e4, 0xff5e5, 0xff5e6, 0xff5e7, 0x0fef4, 0x1fefe,
    0x3fdea, 0x3ffde, 0xff7b6, 0x3fdec, 0x3fdee, 0x3fd78, 0xff5ee, 0x7f798, 0x7faf6, 0x7f799,
    0x07ffa, 0x0fef6, 0x3fdef, 0x3ffdf, 0x1fdee, 0x1ffee, 0x0ff7e, 0x03d7e, 0x03fba, 0x03fbe,
    0x03ffe, 0x01ebe, 0x01fdc, 0x01fee, 0x01ff6, 0x00f5c, 0x00f5d, 0x00f7e, 0x00f7f, 0x00ff4,
    0x00ff6, 0x00ffe, 0x007bc, 0x007bd, 0x007f6, 0x007fc, 0x007fe, 0x003fa, 0x001ea, 0x001fc,
    0x000f6, 0x00076, 0x0007e, 0x0003c, 0x0001c, 0x0003e, 0x0001e, 0x0000e, 0x00006, 0x00002,
    0x00000, 0x00002, 0x00006, 0x0003f, 0x0003e, 0x0003a, 0x00077, 0x000f4, 0x001ee, 0x003fc,
    0x003d6, 0x007be, 0x00ffa, 0x00f5e, 0x01ff7, 0x01fea, 0x03ffc, 0x03fd6, 0x03fbb, 0x07fbe,
    0x07fae, 0x07f7e, 0x0fff6, 0x0fffe, 0x0fef5, 0x0fef2, 0x0f5fe, 0x1fffe, 0x0ff78, 0x0fefe,
    0x07afe, 0x0ff79, 0x1fef4, 0x3fd7a, 0x7f79a, 0xff5ef, 0x3fdfe, 0x3fffe, 0x1fdef, 0x07ffe,
    0x3fdff, 0xff5f2, 0x07f78, 0x7f79b, 0x7f79e, 0xff5f3, 0xff5f4, 0x7f79f, 0x7faf8, 0x3ffff,
    0x7f7f8, 0x3fbce, 0x0f5ff, 0xff5f5, 0xff5f6, 0xff5f7, 0xff5f8, 0xff5f9, 0xff5fa, 0xff5fb,
    0xff5fc, 0xff5fd, 0xff5fe, 0xff5ff, 0xff7ac, 0xff7ad, 0xff7ae, 0xff7af, 0xff7b4, 0xff7b5,
    0xff7b7,
];

// Table A.18: ASPX_HCB_ENV_LEVEL_15_DT (codebook_length = 141, cb_off = 70).
pub static ASPX_HCB_ENV_LEVEL_15_DT_LEN: &[u8] = &[
    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 18,
    17, 19, 17, 16, 17, 17, 17, 16, 15, 16, 15, 15, 14, 14, 14, 13, 12, 10, 8, 6, 4, 2, 1, 3, 5, 7,
    10, 11, 12, 14, 14, 15, 15, 16, 16, 17, 16, 17, 17, 18, 16, 17, 18, 18, 19, 18, 19, 19, 17, 18,
    18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
];

pub static ASPX_HCB_ENV_LEVEL_15_DT_CW: &[u32] = &[
    0x7ffa0, 0x7ffa1, 0x7ffa2, 0x7ffa3, 0x7ffa4, 0x7ffa5, 0x7ffa6, 0x7ffa7, 0x7ffa8, 0x7ffa9,
    0x7ffaa, 0x7ffab, 0x7ffac, 0x7ffad, 0x7ffae, 0x7ffaf, 0x7ffb0, 0x7ffb1, 0x7ffb2, 0x7ffb3,
    0x7ffb4, 0x7ffb5, 0x7ffb6, 0x7ffb7, 0x7ffb8, 0x7ffb9, 0x7ffba, 0x7ffbb, 0x7ffbc, 0x7ffbd,
    0x7ffbe, 0x7ffbf, 0x7ffc0, 0x7ffc1, 0x7ffc2, 0x7ffc3, 0x7ffc4, 0x7ffc5, 0x7ffc6, 0x7ffc7,
    0x7ffcc, 0x7ffcd, 0x7ffce, 0x3ff16, 0x3ff17, 0x3ff1e, 0x3ff1f, 0x3ff3c, 0x1fefa, 0x7ffcf,
    0x1ffb6, 0x0ff7c, 0x1ff8e, 0x1ffbe, 0x1fefb, 0x0ffc4, 0x07fba, 0x0ffc6, 0x07fbb, 0x07fee,
    0x03fdc, 0x03ff0, 0x03ff2, 0x01ffa, 0x00ffe, 0x003fc, 0x000fe, 0x0003e, 0x0000e, 0x00002,
    0x00000, 0x00006, 0x0001e, 0x0007e, 0x003fe, 0x007fa, 0x00ff6, 0x03ffc, 0x03fde, 0x07fe6,
    0x07fec, 0x0ffda, 0x0ffde, 0x1fefe, 0x0ffce, 0x1fff2, 0x1ffbf, 0x3ff3d, 0x0ff7e, 0x1feff,
    0x3ff3e, 0x3ff3f, 0x7ffd2, 0x3ffe8, 0x7ffd3, 0x7ffd4, 0x1ff8a, 0x3ff6e, 0x3ff6f, 0x7ffd5,
    0x7ffd6, 0x7ffd7, 0x7ffd8, 0x7ffd9, 0x7ffda, 0x7ffdb, 0x7ffde, 0x7ffdf, 0x3ffee, 0x7ffe0,
    0x7ffe1, 0x7ffe2, 0x7ffe3, 0x7ffe4, 0x7ffe5, 0x7ffe6, 0x7ffe7, 0x7ffe8, 0x7ffe9, 0x7ffea,
    0x7ffeb, 0x7ffec, 0x7ffed, 0x7ffee, 0x7ffef, 0x7fff0, 0x7fff1, 0x7fff2, 0x7fff3, 0x7fff4,
    0x7fff5, 0x7fff6, 0x7fff7, 0x7fff8, 0x7fff9, 0x7fffa, 0x7fffb, 0x7fffc, 0x7fffd, 0x7fffe,
    0x7ffff,
];

// Table A.19: ASPX_HCB_ENV_BALANCE_15_F0 (codebook_length = 25, cb_off = 0).
pub static ASPX_HCB_ENV_BALANCE_15_F0_LEN: &[u8] = &[
    13, 16, 15, 15, 13, 12, 11, 9, 8, 7, 5, 3, 1, 2, 4, 7, 8, 9, 10, 10, 11, 12, 13, 15, 16,
];

pub static ASPX_HCB_ENV_BALANCE_15_F0_CW: &[u32] = &[
    0x01ffe, 0x0ffea, 0x07ff6, 0x07ff7, 0x01fff, 0x00ffc, 0x007de, 0x001f6, 0x000fa, 0x0007c,
    0x0001e, 0x00006, 0x00000, 0x00002, 0x0000e, 0x0007e, 0x000fe, 0x001fe, 0x003fe, 0x003ee,
    0x007df, 0x00ffd, 0x01ffc, 0x07ff4, 0x0ffeb,
];

// Table A.20: ASPX_HCB_ENV_BALANCE_15_DF (codebook_length = 49, cb_off = 24).
pub static ASPX_HCB_ENV_BALANCE_15_DF_LEN: &[u8] = &[
    19, 19, 19, 19, 19, 18, 18, 17, 16, 16, 16, 15, 13, 13, 12, 12, 12, 11, 10, 9, 8, 7, 5, 3, 1,
    2, 4, 7, 8, 10, 10, 11, 12, 12, 13, 13, 14, 15, 16, 18, 18, 17, 18, 18, 18, 19, 19, 18, 19,
];

pub static ASPX_HCB_ENV_BALANCE_15_DF_CW: &[u32] = &[
    0x7def6, 0x7def7, 0x7defa, 0x7defb, 0x7dffe, 0x3ef7c, 0x3ef7e, 0x1f7b2, 0x0fbd8, 0x0fbda,
    0x0fbfe, 0x07dfe, 0x01f6e, 0x01f7a, 0x00f9e, 0x00fb6, 0x00fbc, 0x007da, 0x003ec, 0x001f2,
    0x000fa, 0x0007e, 0x0001e, 0x00006, 0x00000, 0x00002, 0x0000e, 0x0007f, 0x000f8, 0x003ee,
    0x003e6, 0x007ce, 0x00fbe, 0x00f9f, 0x01f7e, 0x01f6f, 0x03efe, 0x07dee, 0x0fbdb, 0x3ef78,
    0x3ef7f, 0x1f7b3, 0x3effc, 0x3effd, 0x3ef79, 0x7dffc, 0x7dffd, 0x3ef7a, 0x7dfff,
];

// Table A.21: ASPX_HCB_ENV_BALANCE_15_DT (codebook_length = 49, cb_off = 24).
pub static ASPX_HCB_ENV_BALANCE_15_DT_LEN: &[u8] = &[
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 14, 13, 13, 12, 12, 11, 12, 11, 9, 8, 7, 4, 2, 1,
    3, 5, 7, 9, 9, 10, 11, 11, 12, 12, 12, 12, 13, 15, 14, 15, 14, 15, 15, 15, 14, 14, 15, 15,
];

pub static ASPX_HCB_ENV_BALANCE_15_DT_CW: &[u32] = &[
    0x07dce, 0x07dcf, 0x07ddc, 0x07ddd, 0x07dde, 0x07ddf, 0x07dec, 0x07ded, 0x07dee, 0x07def,
    0x07df8, 0x03ebe, 0x01f5e, 0x01f7a, 0x00fb8, 0x00fba, 0x007cc, 0x00fbe, 0x007d6, 0x001f2,
    0x000f8, 0x0007e, 0x0000e, 0x00002, 0x00000, 0x00006, 0x0001e, 0x0007f, 0x001f6, 0x001f4,
    0x003ea, 0x007cd, 0x007ce, 0x00f9e, 0x00fae, 0x00fbc, 0x00f9f, 0x01f76, 0x07df9, 0x03ebf,
    0x07dfa, 0x03ee4, 0x07dfb, 0x07dfc, 0x07dfd, 0x03ee5, 0x03ee6, 0x07dfe, 0x07dff,
];

// Table A.22: ASPX_HCB_ENV_LEVEL_30_F0 (codebook_length = 36, cb_off = 0).
pub static ASPX_HCB_ENV_LEVEL_30_F0_LEN: &[u8] = &[
    11, 13, 11, 11, 10, 10, 9, 8, 8, 7, 7, 7, 6, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 4, 5, 6, 7, 9,
    12, 14, 16, 17, 16, 17, 16,
];

pub static ASPX_HCB_ENV_LEVEL_30_F0_CW: &[u32] = &[
    0x007fe, 0x01ffe, 0x001de, 0x001df, 0x000ee, 0x003fe, 0x001fe, 0x0003a, 0x000fe, 0x0001c,
    0x0001e, 0x0007e, 0x0002e, 0x0003e, 0x00006, 0x00016, 0x00002, 0x0000a, 0x0000c, 0x0000e,
    0x00002, 0x00003, 0x00004, 0x00000, 0x0000d, 0x0001e, 0x0002f, 0x0001f, 0x00076, 0x00ffe,
    0x03ffe, 0x0fffc, 0x1fffe, 0x0fffe, 0x1ffff, 0x0fffd,
];

// Table A.23: ASPX_HCB_ENV_LEVEL_30_DF (codebook_length = 71, cb_off = 35).
pub static ASPX_HCB_ENV_LEVEL_30_DF_LEN: &[u8] = &[
    23, 23, 23, 23, 22, 22, 22, 22, 20, 19, 19, 20, 19, 18, 17, 16, 16, 15, 14, 14, 14, 13, 13, 13,
    13, 12, 12, 12, 11, 10, 9, 8, 5, 4, 2, 1, 3, 6, 8, 9, 11, 12, 14, 14, 15, 16, 17, 17, 17, 18,
    18, 19, 21, 19, 17, 20, 19, 20, 20, 20, 19, 19, 19, 22, 22, 22, 22, 22, 22, 22, 22,
];

pub static ASPX_HCB_ENV_LEVEL_30_DF_CW: &[u32] = &[
    0x7ffbfc, 0x7ffbfd, 0x7ffbfe, 0x7ffbff, 0x3f7f6c, 0x3f7f6d, 0x3f7f6e, 0x3f7f6f, 0x0fdfee,
    0x07effe, 0x07efec, 0x0fff7e, 0x07ffbe, 0x03f7fe, 0x01fbfe, 0x00fdfc, 0x00fff6, 0x007ffa,
    0x003f7e, 0x003ffc, 0x003ffe, 0x001fba, 0x001fbe, 0x001ffa, 0x001ffb, 0x000fdc, 0x000ffc,
    0x000ffe, 0x0007fc, 0x0003f6, 0x0001fe, 0x0000fe, 0x00001e, 0x00000e, 0x000002, 0x000000,
    0x000006, 0x00003e, 0x0000fc, 0x0001fa, 0x0007fd, 0x000fde, 0x003fff, 0x003f76, 0x007eee,
    0x00fdde, 0x01ffee, 0x01fbfc, 0x01fbfa, 0x03ffde, 0x03f77e, 0x07efee, 0x1ffefe, 0x07eefe,
    0x01fbbe, 0x0fdfea, 0x07eff6, 0x0fdfda, 0x0fdfde, 0x0fdfef, 0x07eff4, 0x07efff, 0x07eeff,
    0x3f7fae, 0x3f7f7c, 0x3f7f7d, 0x3f7f7e, 0x3f7f7f, 0x3f7fac, 0x3f7fad, 0x3f7faf,
];

// Table A.24: ASPX_HCB_ENV_LEVEL_30_DT (codebook_length = 71, cb_off = 35).
pub static ASPX_HCB_ENV_LEVEL_30_DT_LEN: &[u8] = &[
    20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 19, 20, 20, 18, 16, 16, 15, 16, 15, 14, 14, 14, 13, 13,
    13, 12, 12, 11, 11, 10, 10, 8, 6, 4, 2, 1, 3, 5, 8, 9, 11, 11, 12, 13, 13, 14, 14, 14, 15, 15,
    16, 17, 18, 19, 18, 18, 18, 18, 20, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19,
];

pub static ASPX_HCB_ENV_LEVEL_30_DT_CW: &[u32] = &[
    0x0ff7d6, 0x0ff7d7, 0x0ff7d8, 0x0ff7d9, 0x0ff7da, 0x0ff7db, 0x0ff7dc, 0x0ff7dd, 0x0ff7de,
    0x07fbfe, 0x07ed7a, 0x0ff7df, 0x0ff7f2, 0x03fdf4, 0x00ff7c, 0x00fdae, 0x007ede, 0x00ff7e,
    0x007ed2, 0x003f68, 0x003f6e, 0x003ffa, 0x001fb6, 0x001ffe, 0x001fff, 0x000fde, 0x000ff6,
    0x0007ec, 0x0007fa, 0x0003fc, 0x0003fe, 0x0000fc, 0x00003e, 0x00000e, 0x000002, 0x000000,
    0x000006, 0x00001e, 0x0000fe, 0x0001fa, 0x0007fe, 0x0007ee, 0x000fdf, 0x001ffc, 0x001fee,
    0x003ffb, 0x003fde, 0x003f6a, 0x007ed6, 0x007ed3, 0x00fdbe, 0x01fb7e, 0x03f6bc, 0x07fbf8,
    0x03f6be, 0x03f6bf, 0x03f6fe, 0x03f6ff, 0x0ff7f3, 0x07fbea, 0x0ff7f4, 0x0ff7f5, 0x0ff7f6,
    0x0ff7f7, 0x0ff7f8, 0x0ff7f9, 0x0ff7fa, 0x0ff7fb, 0x0ff7fe, 0x0ff7ff, 0x07ed7b,
];

// Table A.25: ASPX_HCB_ENV_BALANCE_30_F0 (codebook_length = 13, cb_off = 0).
pub static ASPX_HCB_ENV_BALANCE_30_F0_LEN: &[u8] = &[12, 11, 9, 7, 4, 3, 1, 2, 5, 6, 8, 10, 12];

pub static ASPX_HCB_ENV_BALANCE_30_F0_CW: &[u32] = &[
    0x000ffe, 0x0007fe, 0x0001fe, 0x00007e, 0x00000e, 0x000006, 0x000000, 0x000002, 0x00001e,
    0x00003e, 0x0000fe, 0x0003fe, 0x000fff,
];

// Table A.26: ASPX_HCB_ENV_BALANCE_30_DF (codebook_length = 25, cb_off = 12).
pub static ASPX_HCB_ENV_BALANCE_30_DF_LEN: &[u8] = &[
    17, 17, 17, 15, 14, 12, 9, 9, 8, 7, 5, 2, 1, 3, 4, 7, 8, 9, 10, 11, 14, 15, 16, 16, 17,
];

pub static ASPX_HCB_ENV_BALANCE_30_DF_CW: &[u32] = &[
    0x01fffc, 0x01fffd, 0x01fffe, 0x007ffa, 0x003ffc, 0x000ffe, 0x0001fc, 0x0001fe, 0x0000fa,
    0x00007e, 0x00001e, 0x000002, 0x000000, 0x000006, 0x00000e, 0x00007c, 0x0000fb, 0x0001fd,
    0x0003fe, 0x0007fe, 0x003ffe, 0x007ffb, 0x00fffc, 0x00fffd, 0x01ffff,
];

// Table A.27: ASPX_HCB_ENV_BALANCE_30_DT (codebook_length = 25, cb_off = 12).
pub static ASPX_HCB_ENV_BALANCE_30_DT_LEN: &[u8] = &[
    15, 15, 15, 14, 13, 13, 10, 8, 8, 7, 4, 2, 1, 3, 5, 7, 9, 9, 9, 12, 14, 13, 14, 15, 14,
];

pub static ASPX_HCB_ENV_BALANCE_30_DT_CW: &[u32] = &[
    0x007ff2, 0x007ff3, 0x007ff6, 0x003ff6, 0x001ffe, 0x001fff, 0x0003fe, 0x0000fa, 0x0000fe,
    0x00007e, 0x00000e, 0x000002, 0x000000, 0x000006, 0x00001e, 0x00007c, 0x0001fe, 0x0001f6,
    0x0001f7, 0x000ffc, 0x003ffa, 0x001ffa, 0x003ff7, 0x007ff7, 0x003ff8,
];

// Table A.28: ASPX_HCB_NOISE_LEVEL_F0 (codebook_length = 30, cb_off = 0).
pub static ASPX_HCB_NOISE_LEVEL_F0_LEN: &[u8] = &[
    12, 12, 9, 7, 6, 6, 5, 1, 2, 3, 5, 7, 8, 8, 9, 10, 11, 12, 12, 12, 13, 13, 14, 12, 14, 14, 11,
    16, 16, 15,
];

pub static ASPX_HCB_NOISE_LEVEL_F0_CW: &[u32] = &[
    0x000efe, 0x000eff, 0x0001fe, 0x000076, 0x00003a, 0x00003e, 0x00001e, 0x000000, 0x000002,
    0x000006, 0x00001c, 0x00007e, 0x0000fe, 0x0000ee, 0x0001ff, 0x0003bc, 0x00077c, 0x000efa,
    0x000ef4, 0x000ef6, 0x001df6, 0x001dee, 0x003bee, 0x000ef5, 0x003bde, 0x003bef, 0x00077e,
    0x00ef7e, 0x00ef7f, 0x0077be,
];

// Table A.29: ASPX_HCB_NOISE_LEVEL_DF (codebook_length = 59, cb_off = 29).
pub static ASPX_HCB_NOISE_LEVEL_DF_LEN: &[u8] = &[
    17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 13, 14, 16, 16, 15, 15, 16, 15, 14, 14, 14, 14, 13, 11,
    10, 9, 8, 7, 3, 1, 2, 4, 5, 7, 8, 9, 10, 12, 13, 13, 14, 16, 17, 17, 17, 17, 17, 17, 16, 17,
    17, 17, 17, 17, 17, 17, 17, 17, 17,
];

pub static ASPX_HCB_NOISE_LEVEL_DF_CW: &[u32] = &[
    0x01f19e, 0x01f19f, 0x01f1dc, 0x01f1dd, 0x01f1de, 0x01f1df, 0x01f1e8, 0x01f1e9, 0x01f1ea,
    0x01f1eb, 0x001f1a, 0x003e7e, 0x00f9fe, 0x00f8f6, 0x007c76, 0x007c66, 0x00f8f7, 0x007c7c,
    0x003e3c, 0x003e36, 0x003e37, 0x003e3a, 0x001f3e, 0x0007ce, 0x0003e6, 0x0001f0, 0x0000fa,
    0x00007e, 0x000006, 0x000000, 0x000002, 0x00000e, 0x00001e, 0x00007f, 0x0000fb, 0x0001f2,
    0x0003e2, 0x000f9e, 0x001f1c, 0x001f18, 0x003e32, 0x00f8fa, 0x01f3fe, 0x01f1f6, 0x01f1f7,
    0x01f1f8, 0x01f1f9, 0x01f1fa, 0x00f8ce, 0x01f1fb, 0x01f1fc, 0x01f1fd, 0x01f1fe, 0x01f1ff,
    0x01f3f8, 0x01f3f9, 0x01f3fa, 0x01f3fb, 0x01f3ff,
];

// Table A.30: ASPX_HCB_NOISE_LEVEL_DT (codebook_length = 59, cb_off = 29).
pub static ASPX_HCB_NOISE_LEVEL_DT_LEN: &[u8] = &[
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 14, 16, 13,
    11, 8, 6, 4, 3, 1, 2, 5, 7, 10, 10, 12, 13, 13, 14, 15, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16,
];

pub static ASPX_HCB_NOISE_LEVEL_DT_CW: &[u32] = &[
    0x00ff70, 0x00ff71, 0x00ff72, 0x00ff73, 0x00ff74, 0x00ff75, 0x00ff76, 0x00ff77, 0x00ff78,
    0x00ff79, 0x00ff7a, 0x00ff7b, 0x00ff7c, 0x00ff7d, 0x00ff7e, 0x00ff7f, 0x00ffe0, 0x00ffe1,
    0x00ffe2, 0x00ffe3, 0x00ffe6, 0x003fd6, 0x00ffe7, 0x001ffe, 0x0007fe, 0x0000fe, 0x00003e,
    0x00000e, 0x000006, 0x000000, 0x000002, 0x00001e, 0x00007e, 0x0003fe, 0x0003fc, 0x000ff6,
    0x001fe8, 0x001fea, 0x003fd2, 0x007fae, 0x003fd3, 0x007ff2, 0x007faf, 0x00ffe8, 0x00ffe9,
    0x00ffea, 0x00ffeb, 0x00ffec, 0x00ffed, 0x00ffee, 0x00ffef, 0x00fff8, 0x00fff9, 0x00fffa,
    0x00fffb, 0x00fffc, 0x00fffd, 0x00fffe, 0x00ffff,
];

// Table A.31: ASPX_HCB_NOISE_BALANCE_F0 (codebook_length = 13, cb_off = 0).
pub static ASPX_HCB_NOISE_BALANCE_F0_LEN: &[u8] = &[10, 10, 9, 7, 5, 3, 2, 1, 4, 6, 9, 10, 10];

pub static ASPX_HCB_NOISE_BALANCE_F0_CW: &[u32] = &[
    0x0003fa, 0x0003fe, 0x0001fc, 0x00007e, 0x00001e, 0x000006, 0x000002, 0x000000, 0x00000e,
    0x00003e, 0x0001fe, 0x0003ff, 0x0003fb,
];

// Table A.32: ASPX_HCB_NOISE_BALANCE_DF (codebook_length = 25, cb_off = 12).
pub static ASPX_HCB_NOISE_BALANCE_DF_LEN: &[u8] = &[
    13, 13, 13, 13, 13, 12, 13, 10, 9, 7, 4, 2, 1, 3, 5, 6, 9, 11, 13, 13, 12, 12, 12, 12, 12,
];

pub static ASPX_HCB_NOISE_BALANCE_DF_CW: &[u32] = &[
    0x001fd8, 0x001fd9, 0x001fda, 0x001fdb, 0x001fdc, 0x000fea, 0x001fdd, 0x0003f8, 0x0001fe,
    0x00007e, 0x00000e, 0x000002, 0x000000, 0x000006, 0x00001e, 0x00003e, 0x0001ff, 0x0007f4,
    0x001fde, 0x001fdf, 0x000fe4, 0x000fe5, 0x000fe6, 0x000fe7, 0x000feb,
];

// Table A.33: ASPX_HCB_NOISE_BALANCE_DT (codebook_length = 25, cb_off = 12).
pub static ASPX_HCB_NOISE_BALANCE_DT_LEN: &[u8] = &[
    11, 11, 11, 11, 11, 11, 11, 10, 10, 6, 4, 3, 1, 2, 5, 8, 9, 11, 10, 11, 11, 11, 11, 11, 11,
];

pub static ASPX_HCB_NOISE_BALANCE_DT_CW: &[u32] = &[
    0x0007e6, 0x0007e7, 0x0007e8, 0x0007e9, 0x0007ea, 0x0007eb, 0x0007ec, 0x0003fe, 0x0003ff,
    0x00003e, 0x00000e, 0x000006, 0x000000, 0x000002, 0x00001e, 0x0000fe, 0x0001f8, 0x0007ed,
    0x0003f2, 0x0007ee, 0x0007ef, 0x0007f8, 0x0007f9, 0x0007fa, 0x0007fb,
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::huffman::huff_decode;
    use oxideav_core::bits::{BitReader, BitWriter};

    /// One row of the §A.2 sweep: `(name, len_table, cw_table, codebook_length)`.
    type AspxTableRow = (&'static str, &'static [u8], &'static [u32], usize);

    /// All 18 A-SPX codebooks paired up so we can sweep them with one helper.
    fn all_aspx_tables() -> Vec<AspxTableRow> {
        vec![
            (
                "ASPX_HCB_ENV_LEVEL_15_F0",
                ASPX_HCB_ENV_LEVEL_15_F0_LEN,
                ASPX_HCB_ENV_LEVEL_15_F0_CW,
                71,
            ),
            (
                "ASPX_HCB_ENV_LEVEL_15_DF",
                ASPX_HCB_ENV_LEVEL_15_DF_LEN,
                ASPX_HCB_ENV_LEVEL_15_DF_CW,
                141,
            ),
            (
                "ASPX_HCB_ENV_LEVEL_15_DT",
                ASPX_HCB_ENV_LEVEL_15_DT_LEN,
                ASPX_HCB_ENV_LEVEL_15_DT_CW,
                141,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_15_F0",
                ASPX_HCB_ENV_BALANCE_15_F0_LEN,
                ASPX_HCB_ENV_BALANCE_15_F0_CW,
                25,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_15_DF",
                ASPX_HCB_ENV_BALANCE_15_DF_LEN,
                ASPX_HCB_ENV_BALANCE_15_DF_CW,
                49,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_15_DT",
                ASPX_HCB_ENV_BALANCE_15_DT_LEN,
                ASPX_HCB_ENV_BALANCE_15_DT_CW,
                49,
            ),
            (
                "ASPX_HCB_ENV_LEVEL_30_F0",
                ASPX_HCB_ENV_LEVEL_30_F0_LEN,
                ASPX_HCB_ENV_LEVEL_30_F0_CW,
                36,
            ),
            (
                "ASPX_HCB_ENV_LEVEL_30_DF",
                ASPX_HCB_ENV_LEVEL_30_DF_LEN,
                ASPX_HCB_ENV_LEVEL_30_DF_CW,
                71,
            ),
            (
                "ASPX_HCB_ENV_LEVEL_30_DT",
                ASPX_HCB_ENV_LEVEL_30_DT_LEN,
                ASPX_HCB_ENV_LEVEL_30_DT_CW,
                71,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_30_F0",
                ASPX_HCB_ENV_BALANCE_30_F0_LEN,
                ASPX_HCB_ENV_BALANCE_30_F0_CW,
                13,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_30_DF",
                ASPX_HCB_ENV_BALANCE_30_DF_LEN,
                ASPX_HCB_ENV_BALANCE_30_DF_CW,
                25,
            ),
            (
                "ASPX_HCB_ENV_BALANCE_30_DT",
                ASPX_HCB_ENV_BALANCE_30_DT_LEN,
                ASPX_HCB_ENV_BALANCE_30_DT_CW,
                25,
            ),
            (
                "ASPX_HCB_NOISE_LEVEL_F0",
                ASPX_HCB_NOISE_LEVEL_F0_LEN,
                ASPX_HCB_NOISE_LEVEL_F0_CW,
                30,
            ),
            (
                "ASPX_HCB_NOISE_LEVEL_DF",
                ASPX_HCB_NOISE_LEVEL_DF_LEN,
                ASPX_HCB_NOISE_LEVEL_DF_CW,
                59,
            ),
            (
                "ASPX_HCB_NOISE_LEVEL_DT",
                ASPX_HCB_NOISE_LEVEL_DT_LEN,
                ASPX_HCB_NOISE_LEVEL_DT_CW,
                59,
            ),
            (
                "ASPX_HCB_NOISE_BALANCE_F0",
                ASPX_HCB_NOISE_BALANCE_F0_LEN,
                ASPX_HCB_NOISE_BALANCE_F0_CW,
                13,
            ),
            (
                "ASPX_HCB_NOISE_BALANCE_DF",
                ASPX_HCB_NOISE_BALANCE_DF_LEN,
                ASPX_HCB_NOISE_BALANCE_DF_CW,
                25,
            ),
            (
                "ASPX_HCB_NOISE_BALANCE_DT",
                ASPX_HCB_NOISE_BALANCE_DT_LEN,
                ASPX_HCB_NOISE_BALANCE_DT_CW,
                25,
            ),
        ]
    }

    /// Each table's len[]/cw[] pair must match its declared codebook_length.
    #[test]
    fn all_aspx_tables_have_declared_length() {
        for (name, lens, cws, n) in all_aspx_tables() {
            assert_eq!(lens.len(), n, "{name}: len-array length mismatch");
            assert_eq!(cws.len(), n, "{name}: cw-array length mismatch");
        }
    }

    /// Every codeword has to fit inside its declared bit-length.
    #[test]
    fn all_aspx_codewords_fit_in_declared_length() {
        for (name, lens, cws, _) in all_aspx_tables() {
            for (i, (&l, &c)) in lens.iter().zip(cws.iter()).enumerate() {
                assert!(
                    l > 0 && l <= 32,
                    "{name}[{i}]: declared length {l} out of supported range"
                );
                let max = if l == 32 { u32::MAX } else { (1u32 << l) - 1 };
                assert!(
                    c <= max,
                    "{name}[{i}]: codeword 0x{c:x} exceeds {l}-bit limit"
                );
            }
        }
    }

    /// Kraft equality: a complete prefix code satisfies Σ 2^(-len_i) == 1.
    /// 32-bit denominator is plenty for the widest 23-bit codeword
    /// (`ASPX_HCB_ENV_LEVEL_30_DF`).
    #[test]
    fn all_aspx_tables_kraft_sum_equals_one() {
        let denom: u128 = 1u128 << 32;
        for (name, lens, _, _) in all_aspx_tables() {
            let mut sum: u128 = 0;
            for &l in lens {
                sum += denom >> l as u128;
            }
            assert_eq!(sum, denom, "{name}: Kraft sum != 1");
        }
    }

    /// Pair-by-pair prefix-code check: equal-length codewords must be
    /// distinct, shorter codewords must not be a prefix of longer ones.
    #[test]
    fn all_aspx_tables_are_prefix_codes() {
        for (name, lens, cws, _) in all_aspx_tables() {
            for i in 0..lens.len() {
                let li = lens[i];
                let ci = cws[i];
                for j in (i + 1)..lens.len() {
                    let lj = lens[j];
                    let cj = cws[j];
                    if li == lj {
                        assert_ne!(
                            ci, cj,
                            "{name} collision at {i},{j}: same length {li} cw 0x{ci:x}"
                        );
                    } else {
                        let (sl, sc, ll, lc) = if li < lj {
                            (li, ci, lj, cj)
                        } else {
                            (lj, cj, li, ci)
                        };
                        let prefix = lc >> (ll - sl);
                        assert_ne!(
                            prefix, sc,
                            "{name} prefix conflict at {i},{j}: \
                             {sl}-bit cw 0x{sc:x} prefixes {ll}-bit cw 0x{lc:x}"
                        );
                    }
                }
            }
        }
    }

    /// End-to-end roundtrip: for every codebook, encode the shortest entry
    /// (the one that gets the 1-bit codeword in DF/DT codebooks, or the
    /// shortest in F0 codebooks) via `BitWriter`, then decode through
    /// `huff_decode` and verify the recovered symbol index matches.
    ///
    /// This is a single sweep across all 18 A-SPX codebooks rather than
    /// 18 individual tests — the loop body is identical in each case and
    /// the assertion message names the failing codebook.
    #[test]
    fn all_aspx_tables_decode_shortest_entry() {
        for (name, lens, cws, _) in all_aspx_tables() {
            // Pick the entry with the smallest length (most-frequent symbol).
            let (sym_idx, &min_len) = lens
                .iter()
                .enumerate()
                .min_by_key(|(_, &l)| l)
                .expect("non-empty codebook");
            let cw = cws[sym_idx];

            let mut bw = BitWriter::new();
            bw.write_u32(cw, min_len as u32);
            bw.align_to_byte();
            let bytes = bw.finish();
            let mut br = BitReader::new(&bytes);
            let got = huff_decode(&mut br, lens, cws)
                .unwrap_or_else(|e| panic!("{name}: decode failed for sym_idx={sym_idx}: {e:?}"));
            assert_eq!(
                got as usize, sym_idx,
                "{name}: decoded {got}, expected {sym_idx} (cw=0x{cw:x}, len={min_len})"
            );
        }
    }

    /// End-to-end roundtrip: for every codebook, encode and decode the
    /// **last** entry (often a long codeword at the codebook tail).
    /// Shortest + longest gives high-confidence coverage that the encoded
    /// bit pattern aligns with the decoder's MSB-first walk for every
    /// codebook width in the spec.
    #[test]
    fn all_aspx_tables_decode_last_entry() {
        for (name, lens, cws, _) in all_aspx_tables() {
            let last = lens.len() - 1;
            let l = lens[last];
            let cw = cws[last];

            let mut bw = BitWriter::new();
            bw.write_u32(cw, l as u32);
            bw.align_to_byte();
            let bytes = bw.finish();
            let mut br = BitReader::new(&bytes);
            let got = huff_decode(&mut br, lens, cws)
                .unwrap_or_else(|e| panic!("{name}: decode failed for last entry: {e:?}"));
            assert_eq!(
                got as usize, last,
                "{name}: decoded {got}, expected {last} (cw=0x{cw:x}, len={l})"
            );
        }
    }
}