| 1 | |
| 2 | |
| 3 | |
| 4 | import json |
| 5 | import sys |
| 6 | |
| 7 | SPEC = { |
| 8 | 1: (4, 1, False, False), |
| 9 | 2: (4, 1, False, False), |
| 10 | 3: (4, 2, True, False), |
| 11 | 4: (4, 2, True, False), |
| 12 | 5: (2, 4, False, False), |
| 13 | 6: (2, 4, False, False), |
| 14 | 7: (2, 7, True, False), |
| 15 | 8: (2, 7, True, False), |
| 16 | 9: (2, 12, True, False), |
| 17 | 10: (2, 12, True, False), |
| 18 | 11: (2, 16, True, True), |
| 19 | } |
| 20 | |
| 21 | |
| 22 | def index_of(t, dim, lav, unsigned): |
| 23 | span = lav + 1 if unsigned else 2 * lav + 1 |
| 24 | off = 0 if unsigned else lav |
| 25 | idx = 0 |
| 26 | for v in t: |
| 27 | idx = idx * span + (v + off) |
| 28 | return idx |
| 29 | |
| 30 | |
| 31 | def emit_codebook(cb): |
| 32 | dim, lav, unsigned, esc = SPEC[cb] |
| 33 | d = json.load(open(f"cb{cb}.json")) |
| 34 | span = lav + 1 if unsigned else 2 * lav + 1 |
| 35 | n = span ** dim |
| 36 | table = [None] * n |
| 37 | for bits, tup in d.items(): |
| 38 | i = index_of(tuple(tup), dim, lav, unsigned) |
| 39 | assert table[i] is None, (cb, tup) |
| 40 | table[i] = (len(bits), int(bits, 2)) |
| 41 | missing = [i for i, v in enumerate(table) if v is None] |
| 42 | assert not missing, (cb, missing[:5]) |
| 43 | body = ", ".join(f"({l},{c})" for l, c in table) |
| 44 | return f"/// Codebook {cb}: dim {dim}, lav {lav}, " \ |
| 45 | f"{'unsigned' if unsigned else 'signed'}{', escape' if esc else ''}.\n" \ |
| 46 | f"static HCB{cb}: [(u8, u32); {n}] = [{body}];\n" |
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | out = [] |
| 51 | out.append('''//! Normative AAC tables (ISO/IEC 14496-3 §4.6.2, §4.6.9, tbl 4.140). |
| 52 | //! |
| 53 | //! Every number here was derived by black-box probing of a reference decoder, |
| 54 | //! not copied from an implementation: `scripts/derive-aac-tables.py` synthesises |
| 55 | //! access units whose spectral payload is a chosen bit string, decodes them, and |
| 56 | //! reads the quantised spectrum back through the inverse MDCT. Each codebook |
| 57 | //! came out with a Kraft sum of exactly 1 and the entry count the standard |
| 58 | //! prescribes, which is what makes the derivation self-checking. |
| 59 | ''') |
| 60 | for cb in range(1, 12): |
| 61 | out.append(emit_codebook(cb)) |
| 62 | |
| 63 | sf = json.load(open("sfb.json")) |
| 64 | tab = [None] * 121 |
| 65 | for bits, v in sf.items(): |
| 66 | tab[v] = (len(bits), int(bits, 2)) |
| 67 | assert all(t is not None for t in tab) |
| 68 | out.append("/// Scalefactor codebook: index is the coded value, delta = index - 60.\n" |
| 69 | "static HCB_SF: [(u8, u32); 121] = [" |
| 70 | + ", ".join(f"({l},{c})" for l, c in tab) + "];\n") |
| 71 | |
| 72 | lng = json.load(open("swb.json")) |
| 73 | sht = json.load(open("swb_short.json")) |
| 74 | for name, src, cap in (("LONG", lng, "long"), ("SHORT", sht, None)): |
| 75 | rows = [] |
| 76 | for i in range(12): |
| 77 | e = src[str(i)]["long"] if cap else src[str(i)] |
| 78 | rows.append("&[0, " + ", ".join(str(v) for v in e) + "]") |
| 79 | out.append(f"/// Scalefactor-band offsets for {'1024' if cap else '128'}-line " |
| 80 | f"windows, by samplingFrequencyIndex.\n" |
| 81 | f"pub static SWB_{name}: [&[u16]; 12] = [\n " |
| 82 | + ",\n ".join(rows) + ",\n];\n") |
| 83 | |
| 84 | out.append(''' |
| 85 | /// One spectral codebook's shape. |
| 86 | #[derive(Clone, Copy, Debug)] |
| 87 | pub struct Codebook { |
| 88 | /// Coefficients per Huffman codeword (2 or 4). |
| 89 | pub dim: u8, |
| 90 | /// Largest absolute value the codebook can carry. |
| 91 | pub lav: u8, |
| 92 | /// True when magnitudes are coded and each non-zero one takes a sign bit. |
| 93 | pub unsigned: bool, |
| 94 | /// True for codebook 11, whose magnitude 16 opens an escape sequence. |
| 95 | pub esc: bool, |
| 96 | /// `(length, code)` by tuple index, most-significant bit first. |
| 97 | pub codes: &'static [(u8, u32)], |
| 98 | } |
| 99 | |
| 100 | /// The eleven spectral codebooks, indexed by `sect_cb - 1`. |
| 101 | pub static CODEBOOKS: [Codebook; 11] = [ |
| 102 | ''') |
| 103 | for cb in range(1, 12): |
| 104 | dim, lav, unsigned, esc = SPEC[cb] |
| 105 | out.append(f" Codebook {{ dim: {dim}, lav: {lav}, unsigned: {str(unsigned).lower()}, " |
| 106 | f"esc: {str(esc).lower()}, codes: &HCB{cb} }},\n") |
| 107 | out.append("];\n\n") |
| 108 | out.append('''/// The scalefactor codebook, as a `Codebook`-shaped table of `(length, code)`. |
| 109 | pub static SCALEFACTOR_CODES: &[(u8, u32)] = &HCB_SF; |
| 110 | |
| 111 | /// Sampling frequencies by `samplingFrequencyIndex` (ISO 14496-3 tbl 1.18). |
| 112 | pub static SAMPLE_RATES: [u32; 13] = [ |
| 113 | 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, |
| 114 | ]; |
| 115 | ''') |
| 116 | src = "".join(out) |
| 117 | open(sys.argv[1], "w").write(src) |
| 118 | print(f"wrote {sys.argv[1]}, {len(src)} bytes") |
| 119 | |
| 120 | |
| 121 | main() |