forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBabd0a08Tahinli3 months ago1343 commits

extract-eob-class1.py69 lines, 2.3 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.
9
10Usage: extract-eob-class1.py <token_cdfs.h> [--check <cdf.rs>]
11"""
12
13import re
14import sys
15
16SIZES = [16, 32, 64, 128, 256, 512, 1024]
17QSUF = ["_Q0", "_Q1", "", "_Q3"]
18
19
20def parse(src: str, size: int):
21 m = re.search(rf"av1_default_eob_multi{size}_cdfs\b(.*?)\n\s*\n", src, re.S)
22 if not m:
23 sys.exit(f"table eob_multi{size} not found")
24 rows = [
25 [int(x) for x in r.split(",")]
26 for r in re.findall(r"AOM_CDF\d+\(([^)]*)\)", m.group(1))
27 ]
28 # flat order: q-major, then plane, then class
29 if len(rows) != 4 * 2 * 2:
30 sys.exit(f"eob_multi{size}: expected 16 rows, got {len(rows)}")
31 return rows
32
33
34def consts(src):
35 out = {}
36 for size in SIZES:
37 rows = parse(src, size)
38 for q in range(4):
39 for p, plane in enumerate(("LUMA", "CHROMA")):
40 row = rows[q * 4 + p * 2 + 1]
41 out[f"EOB_PT_{size}_{plane}_CLASS1{QSUF[q]}"] = row + [32768, 0]
42 return out
43
44
45def main():
46 src = open(sys.argv[1]).read()
47 want = consts(src)
48 if "--check" in sys.argv:
49 rs = open(sys.argv[sys.argv.index("--check") + 1]).read()
50 seen = bad = 0
51 for name, vals in want.items():
52 m = re.search(rf"const {name}: \[u16; \d+\] =\s*(\[[^]]*\]);", rs)
53 if not m:
54 continue
55 seen += 1
56 got = [int(x) for x in re.findall(r"\d+", m.group(1))]
57 if got != vals:
58 bad += 1
59 print(f"MISMATCH {name}\n ours {got}\n aom {vals}")
60 print(f"checked {seen}/{len(want)} consts, {bad} mismatched")
61 missing = [n for n in want if f"const {n}:" not in rs]
62 print(f"missing {len(missing)}: {' '.join(sorted(missing))}")
63 sys.exit(1 if bad else 0)
64 for name, vals in want.items():
65 body = ", ".join(str(v) for v in vals)
66 print(f"pub const {name}: [u16; {len(vals)}] = [{body}];")
67
68
69main()