#!/usr/bin/env python3 """Extract the fixed probability/quant tables from the local RFC 6386 text into a Rust module (crates/ec-vp8/src/tables.rs). The tables are transcribed VERBATIM from the RFC text; the script asserts each table's element count so a parsing slip cannot produce a silently wrong table. Run from the worktree root: python3 scripts/extract_vp8_tables.py [rfc.txt] [out.rs] """ import re import sys RFC = sys.argv[1] if len(sys.argv) > 1 else "/home/user/.cache/vp8/rfc6386.txt" OUT = sys.argv[2] if len(sys.argv) > 2 else "crates/ec-vp8/src/tables.rs" text = open(RFC, encoding="utf-8").read() # Strip RFC page furniture (running headers/footers land mid-table). text = "\n".join( l for l in text.splitlines() if not re.search(r"RFC 6386\s+VP8 Data Format.*2011$", l) and not re.search(r"Bankoski, et al\..*\[Page \d+\]$", l) ) text = text.replace("\f", "") INT_RE = re.compile(r"-?\d+") def collect(name: str, expect: int) -> list[int]: """Find `name`'s C array DEFINITION -- '=' reachable from the name through nothing but brackets, identifiers, spaces and hyphens (the section-13.4 pseudocode mentions `coeff_update_probs [i] [j] [k] [t]))` and must not match) -- and collect every integer between '=' and the terminating '};'.""" for m in re.finditer(rf"\b{re.escape(name)}\b[\[\]\w\s-]*?=", text): body = text[m.end() : m.end() + 60000] end = body.find("};") assert end != -1, f"{name}: no terminator" vals = [int(v) for v in INT_RE.findall(body[:end])] if len(vals) != expect: raise SystemExit( f"{name}: expected {expect} ints, got {len(vals)}: {vals[:10]}..." ) return vals raise SystemExit(f"{name}: definition not found") # (rust name, RFC name, expected ints incl. Pcat sentinels, dims, type) TABLES = [ ("COEFF_UPDATE_PROBS", "coeff_update_probs", 4 * 8 * 3 * 11, (4, 8, 3, 11), "u8"), ("DEFAULT_COEFF_PROBS", "default_coeff_probs", 4 * 8 * 3 * 11, (4, 8, 3, 11), "u8"), ("KF_BMODE_PROB", "kf_bmode_prob", 10 * 10 * 9, (10, 10, 9), "u8"), ("COEFF_BANDS", "coeff_bands", 16, (16,), "u8"), ("DC_QLOOKUP", "dc_qlookup", 128, (128,), "i16"), ("AC_QLOOKUP", "ac_qlookup", 128, (128,), "i16"), ("PCAT1", "Pcat1", 2, (2,), "u8"), ("PCAT2", "Pcat2", 3, (3,), "u8"), ("PCAT3", "Pcat3", 4, (4,), "u8"), ("PCAT4", "Pcat4", 5, (5,), "u8"), ("PCAT5", "Pcat5", 6, (6,), "u8"), ("PCAT6", "Pcat6", 12, (12,), "u8"), ("DEFAULT_MV_PROBS", "k_default_mv_probs", 2 * 19, (2, 19), "u8"), ("MV_UPDATE_PROBS", "k_mv_entropy_update_probs", 2 * 19, (2, 19), "u8"), ] def rust_ty(ty: str, dims: tuple[int, ...]) -> str: t = ty for d in reversed(dims): t = f"[{t}; {d}]" return t def nest(vals: list[int], dims: tuple[int, ...]) -> str: if len(dims) == 1: return "[" + ", ".join(str(v) for v in vals) + "]" step = len(vals) // dims[0] parts = [ nest(vals[i * step : (i + 1) * step], dims[1:]) for i in range(dims[0]) ] if len(dims) == 2: return "[\n" + ",\n".join(" " + p for p in parts) + "\n ]" return "[\n" + ",\n".join(" " + p for p in parts) + "\n ]" out = [ "//! Fixed tables transcribed from RFC 6386 (VP8 Data Format and", "//! Decoding Guide). GENERATED by `scripts/extract_vp8_tables.py` from", "//! the RFC text; do not edit by hand. The extractor asserts every", "//! table's element count, so a slip cannot drop or add values.", "#![allow(clippy::all)]", "", ] for rust_name, rfc_name, expect, dims, ty in TABLES: vals = collect(rfc_name, expect) note = " (trailing 0 is the DCTextra sentinel)" if rfc_name.startswith("Pcat") else "" out.append(f"/// RFC 6386 `{rfc_name}`{note}.") out.append(f"pub const {rust_name}: {rust_ty(ty, dims)} = {nest(vals, dims)};\n") open(OUT, "w", encoding="utf-8").write("\n".join(out)) print(f"wrote {OUT}")