From c53ad6b993a15d6a88ef52f9d9138bded94e7f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 16:51:48 +0300 Subject: [PATCH] feat(ec-aac): derive four SBR noise Huffman codebooks, black box Wires the SBR table-derivation rig's widths/books/values stages into its CLI and adds the value-assignment stage (bisecting a codeword's raw start value to the reference decoder's own range-check boundary), then emits the codebooks that closed with a complete, fully-valued prefix code: NOISE_T, NOISE_F, NOISEB_T, NOISEB_F. The eight envelope books derive lengths cleanly but each hit at least one codeword whose value probe crashes the reference decoder rather than range-checking gracefully ("channel element 0.1 is not allocated"); they are left out of sbr_tables.rs rather than shipped with a guessed value, per the rig's own rule against fabricating table entries. --- crates/ec-aac/src/lib.rs | 1 + crates/ec-aac/src/sbr_tables.rs | 248 +++++++++++++++++++++++++++++++ scripts/aac-tables/sbrtables.py | 252 +++++++++++++++++++++++++++++++- 3 files changed, 499 insertions(+), 2 deletions(-) create mode 100644 crates/ec-aac/src/sbr_tables.rs diff --git a/crates/ec-aac/src/lib.rs b/crates/ec-aac/src/lib.rs index 1f007bb4..b73606b0 100644 --- a/crates/ec-aac/src/lib.rs +++ b/crates/ec-aac/src/lib.rs @@ -28,6 +28,7 @@ mod config; mod decode; mod encode; mod huffman; +mod sbr_tables; pub mod tables; pub use config::{ diff --git a/crates/ec-aac/src/sbr_tables.rs b/crates/ec-aac/src/sbr_tables.rs new file mode 100644 index 00000000..cd7142e2 --- /dev/null +++ b/crates/ec-aac/src/sbr_tables.rs @@ -0,0 +1,248 @@ +//! SBR (HE-AAC v1) Huffman codebooks (ISO/IEC 14496-3 §4.6.18.3.6). +//! +//! Derived black box from a reference decoder's own bit accounting by +//! `scripts/aac-tables/sbrtables.py`: no source was consulted. The +//! decoder's "Expected to read N SBR bytes actually read M" complaint +//! reads codeword lengths off a controlled FIL payload, and its +//! `env_facs_q`/`noise_facs_q` range check reads the delta value each +//! codeword carries off the same payload. Every table here closed with +//! a Kraft sum of exactly 1 (asserted below). +#![allow(dead_code)] + +// Not emitted (values incomplete or book did not close): ['ENV15_F', 'ENV15_T', 'ENV30_T', 'ENV30_F', 'ENVB15_F', 'ENVB15_T', 'ENVB30_T', 'ENVB30_F'] + +/// SBR Huffman book `NOISE_T`: (length, code, delta) by codeword. +pub(crate) static NOISE_T: [(u8, u32, i32); 63] = [ + (1, 0, 0), + (2, 2, 1), + (3, 6, -1), + (4, 14, -2), + (5, 30, 2), + (6, 62, -3), + (8, 252, 3), + (8, 253, -4), + (10, 1016, 4), + (11, 2034, -5), + (13, 8140, 5), + (13, 8141, 11), + (13, 8142, -31), + (13, 8143, -30), + (13, 8144, -29), + (13, 8145, -28), + (13, 8146, -27), + (13, 8147, -26), + (13, 8148, -25), + (13, 8149, -24), + (13, 8150, -23), + (13, 8151, -22), + (13, 8152, -21), + (13, 8153, -20), + (13, 8154, -19), + (13, 8155, -18), + (13, 8156, -17), + (13, 8157, -16), + (13, 8158, -15), + (13, 8159, -14), + (13, 8160, -13), + (13, 8161, -12), + (13, 8162, -11), + (13, 8163, -10), + (13, 8164, -9), + (13, 8165, -8), + (13, 8166, -7), + (13, 8167, -6), + (13, 8168, 6), + (13, 8169, 7), + (13, 8170, 8), + (13, 8171, 9), + (13, 8172, 10), + (13, 8173, 12), + (13, 8174, 13), + (13, 8175, 14), + (13, 8176, 15), + (13, 8177, 16), + (13, 8178, 17), + (13, 8179, 18), + (13, 8180, 19), + (13, 8181, 20), + (13, 8182, 21), + (13, 8183, 22), + (13, 8184, 23), + (13, 8185, 24), + (13, 8186, 25), + (13, 8187, 26), + (13, 8188, 27), + (13, 8189, 28), + (13, 8190, 29), + (14, 16382, 30), + (14, 16383, 31), +]; + +/// SBR Huffman book `NOISE_F`: (length, code, delta) by codeword. +pub(crate) static NOISE_F: [(u8, u32, i32); 63] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 1), + (4, 14, -2), + (5, 30, 2), + (6, 62, -3), + (8, 252, 3), + (8, 253, -4), + (9, 508, 4), + (9, 509, -5), + (10, 1020, 5), + (10, 1021, -6), + (11, 2044, 6), + (11, 2045, -7), + (12, 4092, 7), + (12, 4093, -8), + (13, 8188, 8), + (14, 16378, 9), + (14, 16379, -9), + (15, 32760, -10), + (15, 32761, 10), + (15, 32762, 11), + (16, 65526, -11), + (16, 65527, -12), + (16, 65528, 12), + (16, 65529, 13), + (17, 131060, -13), + (17, 131061, -15), + (17, 131062, 14), + (17, 131063, 15), + (18, 262128, -14), + (18, 262129, 18), + (18, 262130, -18), + (18, 262131, -24), + (18, 262132, -19), + (18, 262133, 16), + (18, 262134, 17), + (19, 524270, -22), + (19, 524271, -21), + (19, 524272, -16), + (19, 524273, 20), + (19, 524274, 21), + (19, 524275, 22), + (19, 524276, 25), + (19, 524277, -23), + (19, 524278, -20), + (19, 524279, 24), + (20, 1048560, -31), + (20, 1048561, -30), + (20, 1048562, -29), + (20, 1048563, -28), + (20, 1048564, -27), + (20, 1048565, -26), + (20, 1048566, -25), + (20, 1048567, -17), + (20, 1048568, 19), + (20, 1048569, 23), + (20, 1048570, 26), + (20, 1048571, 27), + (20, 1048572, 28), + (20, 1048573, 29), + (20, 1048574, 30), + (20, 1048575, 31), +]; + +/// SBR Huffman book `NOISEB_T`: (length, code, delta) by codeword. +pub(crate) static NOISEB_T: [(u8, u32, i32); 25] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 1), + (5, 28, -2), + (6, 58, 2), + (8, 236, -12), + (8, 237, -11), + (8, 238, -10), + (8, 239, -9), + (8, 240, -8), + (8, 241, -7), + (8, 242, -6), + (8, 243, -5), + (8, 244, -4), + (8, 245, -3), + (8, 246, 3), + (8, 247, 4), + (8, 248, 5), + (8, 249, 6), + (8, 250, 7), + (8, 251, 8), + (8, 252, 9), + (8, 253, 10), + (8, 254, 11), + (8, 255, 12), +]; + +/// SBR Huffman book `NOISEB_F`: (length, code, delta) by codeword. +pub(crate) static NOISEB_F: [(u8, u32, i32); 25] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 1), + (4, 14, -2), + (5, 30, 2), + (6, 62, 3), + (7, 126, -3), + (8, 254, -4), + (9, 510, 4), + (11, 2044, -5), + (12, 4090, 5), + (13, 8182, 6), + (13, 8183, -12), + (13, 8184, -11), + (13, 8185, -10), + (13, 8186, -9), + (13, 8187, -8), + (14, 16376, -7), + (14, 16377, -6), + (14, 16378, 7), + (14, 16379, 8), + (14, 16380, 9), + (14, 16381, 10), + (14, 16382, 11), + (14, 16383, 12), +]; + +#[cfg(test)] +mod tests { + use super::*; + + fn kraft_is_one(codes: &[(u8, u32, i32)]) { + let sum: f64 = codes.iter().map(|&(l, _, _)| 2f64.powi(-(l as i32))).sum(); + assert!((sum - 1.0).abs() < 1e-9, "Kraft sum {sum}"); + } + + fn is_prefix_free(codes: &[(u8, u32, i32)]) { + for (i, &(li, ci, _)) in codes.iter().enumerate() { + for &(lj, cj, _) in codes.iter().skip(i + 1) { + let (short, long) = if li <= lj { (li, lj) } else { (lj, li) }; + let (sc, lc) = if li <= lj { (ci, cj) } else { (cj, ci) }; + assert_ne!(sc, lc >> (long - short), "{ci:?} prefixes {cj:?}"); + } + } + } + + #[test] + fn noise_t_is_a_complete_prefix_code() { + kraft_is_one(&NOISE_T); + is_prefix_free(&NOISE_T); + } + + #[test] + fn noise_f_is_a_complete_prefix_code() { + kraft_is_one(&NOISE_F); + is_prefix_free(&NOISE_F); + } + + #[test] + fn noiseb_t_is_a_complete_prefix_code() { + kraft_is_one(&NOISEB_T); + is_prefix_free(&NOISEB_T); + } + + #[test] + fn noiseb_f_is_a_complete_prefix_code() { + kraft_is_one(&NOISEB_F); + is_prefix_free(&NOISEB_F); + } +} diff --git a/scripts/aac-tables/sbrtables.py b/scripts/aac-tables/sbrtables.py index 8218fadf..18b935f5 100644 --- a/scripts/aac-tables/sbrtables.py +++ b/scripts/aac-tables/sbrtables.py @@ -663,7 +663,13 @@ BAD_VALUE = re.compile(r"is invalid|out of range|overflow|SBR reset failed") # survives both ends of its table (0 and 30 are the limits the tool enforces), # so both ends are tried and the probe it does not refuse is the one that # measured something. -RAW_TRIES = [(0, 16), (30, 16), (0, 0), (30, 30)] +# A balance value is checked doubled (a coupled channel's noise floor is the +# base channel's combined with the ratio the balance value codes), so 16 -- the +# middle of the raw field -- can already read as 32 and refuse on its own, +# before the codeword under test ever contributes: the extra, smaller-magnitude +# pairs are the fallback once that is observed (`exact_bits` requires a sane +# 1..32 answer, not just an unrefused one). +RAW_TRIES = [(0, 16), (30, 16), (0, 0), (30, 30), (8, 8), (0, 8), (8, 0)] def as_bits(length, code): @@ -756,7 +762,7 @@ def exact_bits(oracle, cfg, tables, kind, word, books): break top = 8 * n - 4 - base + (trailing if BAD_VALUE.search(err) else 0) lo, hi = max(lo, top - 7), min(hi, top) - if not bad and lo == hi: + if not bad and lo == hi and 1 <= lo <= 32: return lo return None @@ -853,6 +859,223 @@ def stage_books(oracle, sf_index=7, only=None, books=None): return out +# -------------------------------------------------------------- values stage + +# `env_facs_q`/`noise_facs_q` are the reference decoder's own accumulated +# scalefactor: it prints the value it computed the instant that value leaves +# the valid range, which is `raw + delta` for a plain codebook or, measured +# below, `2 * (raw + delta)` for a balance one (the coupled channel's value is +# reconstructed from double the coded ratio). Placing the codeword under test +# at `raw = 0` and at the field's own maximum brackets every delta the ten +# tables carry: a delta that overflows high is read straight off the +# maximum-raw probe, a delta that overflows low wraps through the decoder's +# own byte-sized accumulator and is read off the zero-raw probe once +# unwrapped -- the write-up above is what pins the wrap width at 256 and the +# scale at 1 or 2, not an assumption. +VALUE_RE_CACHE = {} + + +def value_re(field): + if field not in VALUE_RE_CACHE: + VALUE_RE_CACHE[field] = re.compile(rf"{field} (-?\d+) is invalid") + return VALUE_RE_CACHE[field] + + +def values_config(kind, sf_index=7): + """A config in which `kind` carries exactly one codeword, plus the raw + field/width/scale that codeword's delta is read against.""" + balance = kind.startswith("ENVB") or kind.startswith("NOISEB") + is_env = kind.startswith("ENV") + amp_res = 1 if "30" in kind else 0 + scale = 2 if balance else 1 + element = "cpe" if balance else "sce" + if kind.endswith("_F"): + if is_env: + over = dict(element=element, coupling=1, num_env=1, amp_res=amp_res, + df_env=[0] * 8, df_noise=[0, 0], freq_res=1) + found = find_config(sf_index, over, n_high=2, n_q=1) + key, width = ("env0b" if balance else "env0"), RAW_ENV[(1 if balance else 0, amp_res)] + else: + over = dict(element=element, coupling=1, num_env=1, amp_res=0, + df_env=[0] * 8, df_noise=[0, 0], freq_res=0) + found = find_config(sf_index, over, n_q=2) + key, width = ("noise0b" if balance else "noise0"), RAW_NOISE + if found is None: + return None + cfg, tables = found + else: + found = recipe_config(kind, sf_index) + if found is None: + return None + cfg, tables = found + if is_env: + key, width = ("env0b" if balance else "env0"), RAW_ENV[(1 if balance else 0, amp_res_of(cfg))] + else: + key, width = ("noise0b" if balance else "noise0"), RAW_NOISE + kinds, _ = plan(cfg, tables) + if kinds.count(kind) != 1: + return None + return cfg, tables, key, width, scale + + +def book_values(oracle, kind, cfg, tables, key, width, scale, books): + """`{(length, code): delta}` for every codeword `stage_books` found.""" + codes = sorted(books[kind]) + raw_max = (1 << width) - 1 + field = "env_facs_q" if kind.startswith("ENV") else "noise_facs_q" + pat = value_re(field) + cases, meta = [], [] + for length, code in codes: + # The exact codeword, no trailing pad: `stage_books` already closed the + # length for every codeword here, so (unlike the length-search bisection, + # which pads because the length is what it is looking for) padding would + # only shift every field after this slot and desync the frame. + word = as_bits(length, code) + words = fill_words(cfg, tables, kind, word, books, extra=0) + if words is None: + continue + for raw in (0, raw_max): + v = dict(cfg) + arr = list(v[key]) + arr[0] = raw + v[key] = arr + cases.append((v, tables, words)) + meta.append((length, code, raw)) + got = measure(oracle, cases) + seen = {} + crashed = set() + clean = set() + for (length, code, raw), (_n, err) in zip(meta, got): + m = pat.search(err) + if not m: + # Not a range-check complaint: either a clean decode (only the + # routine byte-count line) or the parser aborting the element for + # an unrelated reason -- only the former licenses the "both ends + # valid" inference below, so the two are told apart here. + if "is not allocated" in err or "Invalid data found" in err: + crashed.add((length, code)) + else: + clean.add((length, code)) + continue + val = int(m.group(1)) + if raw == 0: + v = val % 256 + if v > 128: + v -= 256 + if v % scale: + continue + delta = v // scale + else: + if val % scale: + continue + delta = val // scale - raw + seen.setdefault((length, code), set()).add(delta) + values, gaps = {}, [] + for length, code in codes: + vs = seen.get((length, code)) + if not vs: + if (length, code) in clean and (length, code) not in crashed: + # Neither end complained and neither crashed either: the + # codeword's delta and the raw value it rides with both + # stayed inside `[0, raw_max]`, which only `delta = 0` can do + # at *both* ends of the field at once. + values[(length, code)] = 0 + else: + gaps.append((length, code, "crashed" if (length, code) in crashed else "no signal")) + continue + if len(vs) > 1: + gaps.append((length, code, vs)) + continue + values[(length, code)] = next(iter(vs)) + return values, gaps + + +def stage_values(oracle, books, sf_index=7): + """Assigns the delta value every codeword of every closed book carries.""" + values = {} + for kind in KINDS: + if kind not in books: + print(f" {kind}: no lengths, skipped") + continue + made = values_config(kind, sf_index) + if made is None: + print(f" {kind}: no single-codeword configuration for values") + continue + cfg, tables, key, width, scale = made + vals, gaps = book_values(oracle, kind, cfg, tables, key, width, scale, books) + if gaps: + print(f" {kind}: {len(gaps)}/{len(books[kind])} codewords unresolved: {gaps[:5]}") + if len(vals) == len(books[kind]): + values[kind] = vals + lo, hi = min(vals.values()), max(vals.values()) + print(f" {kind}: {len(vals)} values, range {lo}..{hi}") + else: + print(f" {kind}: incomplete ({len(vals)}/{len(books[kind])}), not emitted") + return values + + +def write_rust(books, values, path=None): + """Emits the closed, fully-valued books as `crates/ec-aac/src/sbr_tables.rs`.""" + path = path or os.path.join(ROOT, "crates", "ec-aac", "src", "sbr_tables.rs") + ready = [k for k in KINDS if k in books and k in values and len(values[k]) == len(books[k])] + skipped = [k for k in KINDS if k not in ready] + lines = [ + "//! SBR (HE-AAC v1) Huffman codebooks (ISO/IEC 14496-3 §4.6.18.3.6).", + "//!", + "//! Derived black box from a reference decoder's own bit accounting by", + "//! `scripts/aac-tables/sbrtables.py`: no source was consulted. The", + "//! decoder's \"Expected to read N SBR bytes actually read M\" complaint", + "//! reads codeword lengths off a controlled FIL payload, and its", + "//! `env_facs_q`/`noise_facs_q` range check reads the delta value each", + "//! codeword carries off the same payload. Every table here closed with", + "//! a Kraft sum of exactly 1 (asserted below).", + "#![allow(dead_code)]", + "", + ] + if skipped: + lines.append(f"// Not emitted (values incomplete or book did not close): {skipped}") + lines.append("") + for kind in ready: + codes = sorted(books[kind]) + vals = values[kind] + lines.append(f"/// SBR Huffman book `{kind}`: (length, code, delta) by codeword.") + lines.append(f"pub(crate) static {kind}: [(u8, u32, i32); {len(codes)}] = [") + for length, code in codes: + lines.append(f" ({length}, {code}, {vals[(length, code)]}),") + lines.append("];") + lines.append("") + lines.append("#[cfg(test)]") + lines.append("mod tests {") + lines.append(" use super::*;") + lines.append("") + lines.append(" fn kraft_is_one(codes: &[(u8, u32, i32)]) {") + lines.append(" let sum: f64 = codes.iter().map(|&(l, _, _)| 2f64.powi(-(l as i32))).sum();") + lines.append(" assert!((sum - 1.0).abs() < 1e-9, \"Kraft sum {sum}\");") + lines.append(" }") + lines.append("") + lines.append(" fn is_prefix_free(codes: &[(u8, u32, i32)]) {") + lines.append(" for (i, &(li, ci, _)) in codes.iter().enumerate() {") + lines.append(" for &(lj, cj, _) in codes.iter().skip(i + 1) {") + lines.append(" let (short, long) = if li <= lj { (li, lj) } else { (lj, li) };") + lines.append(" let (sc, lc) = if li <= lj { (ci, cj) } else { (cj, ci) };") + lines.append(" assert_ne!(sc, lc >> (long - short), \"{ci:?} prefixes {cj:?}\");") + lines.append(" }") + lines.append(" }") + lines.append(" }") + lines.append("") + for kind in ready: + lines.append(" #[test]") + lines.append(f" fn {kind.lower()}_is_a_complete_prefix_code() {{") + lines.append(f" kraft_is_one(&{kind});") + lines.append(f" is_prefix_free(&{kind});") + lines.append(" }") + lines.append("") + lines.append("}") + with open(path, "w") as f: + f.write("\n".join(lines) + "\n") + print(f"wrote {path}: {len(ready)} books, {len(skipped)} skipped") + return ready, skipped + # ---------------------------------------------------------------- raw widths @@ -1120,6 +1343,21 @@ def stage_diag(oracle): print(f"diag: {total - disagree}/{total} headers agree on n_master") +def run_books_to_fixpoint(oracle, sf_index=7): + """Repeats `stage_books` until no further kind closes, honoring the + dependency order `stage_books` itself discovers via its `missing` check.""" + books = {} + remaining = set(KINDS) + while remaining: + made = stage_books(oracle, sf_index, only=remaining, books=books) + if not made: + break + remaining -= set(made) + if remaining: + print(f"books: did not close: {sorted(remaining)}") + return books + + if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument("--stage", default="sweep") @@ -1131,6 +1369,16 @@ if __name__ == "__main__": stage_sweep(oracle) if args.stage == "diag": stage_diag(oracle) + if args.stage == "k0": + stage_k0(oracle) + if args.stage in ("widths", "all"): + stage_widths(oracle) + books = None + if args.stage in ("books", "values", "all"): + books = run_books_to_fixpoint(oracle) + if args.stage in ("values", "all"): + values = stage_values(oracle, books) + write_rust(books, values) finally: oracle.save() print(f"oracle: {oracle.runs} decoder runs, {oracle.hits} cache hits")