forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB83d96c2Tahinli2 months ago2648 commits

extract-eob-class1.py72 lines, 2.5 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Extract libaom's eob_multi CDF rows and emit / verify our Rust consts.
3
4`av1_default_eob_multi{16,32,64,128,256,512,1024}_cdfs` are
5`[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(n)]`; the third index is
6`eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1`. This prints the
7class-1 (index 1) rows as `EOB_PT_<n>_<PLANE>_CLASS1[_Qk]` consts and, with
8`--check <cdf.rs>`, diffs every const that file already defines -- BOTH classes,
9so a 2D const accidentally transcribed from the class-1 row is caught too
10(rectsplit r4 suspected exactly that of `EOB_PT_512_CHROMA`).
11
12Usage: extract-eob-class1.py <token_cdfs.h> [--check <cdf.rs>]
13"""
14
15import re
16import sys
17
18SIZES = [16, 32, 64, 128, 256, 512, 1024]
19QSUF = ["_Q0", "_Q1", "", "_Q3"]
20
21
22def parse(src: str, size: int):
23 m = re.search(rf"av1_default_eob_multi{size}_cdfs\b(.*?)\n\s*\n", src, re.S)
24 if not m:
25 sys.exit(f"table eob_multi{size} not found")
26 rows = [
27 [int(x) for x in r.split(",")]
28 for r in re.findall(r"AOM_CDF\d+\(([^)]*)\)", m.group(1))
29 ]
30 # flat order: q-major, then plane, then class
31 if len(rows) != 4 * 2 * 2:
32 sys.exit(f"eob_multi{size}: expected 16 rows, got {len(rows)}")
33 return rows
34
35
36def consts(src):
37 out = {}
38 for size in SIZES:
39 rows = parse(src, size)
40 for q in range(4):
41 for p, plane in enumerate(("LUMA", "CHROMA")):
42 for cls, suf in ((0, ""), (1, "_CLASS1")):
43 row = rows[q * 4 + p * 2 + cls]
44 out[f"EOB_PT_{size}_{plane}{suf}{QSUF[q]}"] = row + [32768, 0]
45 return out
46
47
48def main():
49 src = open(sys.argv[1]).read()
50 want = consts(src)
51 if "--check" in sys.argv:
52 rs = open(sys.argv[sys.argv.index("--check") + 1]).read()
53 seen = bad = 0
54 for name, vals in want.items():
55 m = re.search(rf"const {name}: \[u16; \d+\] =\s*(\[[^]]*\]);", rs)
56 if not m:
57 continue
58 seen += 1
59 got = [int(x) for x in re.findall(r"\d+", m.group(1))]
60 if got != vals:
61 bad += 1
62 print(f"MISMATCH {name}\n ours {got}\n aom {vals}")
63 print(f"checked {seen}/{len(want)} consts, {bad} mismatched")
64 missing = [n for n in want if f"const {n}:" not in rs]
65 print(f"missing {len(missing)}: {' '.join(sorted(missing))}")
66 sys.exit(1 if bad else 0)
67 for name, vals in want.items():
68 body = ", ".join(str(v) for v in vals)
69 print(f"pub const {name}: [u16; {len(vals)}] = [{body}];")
70
71
72main()