forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB3bebc9dTahinli3 months ago988 commits

learn_huffman.py95 lines, 3.2 KiBHistoryRawDownload
1"""Measure every Layer III Huffman table by walking the code tree against the
2ffmpeg decoder: a prefix is a codeword exactly when every continuation decodes
3to the same pair (values clamped at 15, so linbits escapes compare equal)."""
4
5import json
6import sys
7import numpy as np
8from probe_engine import Engine, values
9
10# (table index used on the wire, entries per axis, linbits, coefficients read)
11BIG_TABLES = [
12 (1, 2, 0), (2, 3, 0), (3, 3, 0), (5, 4, 0), (6, 4, 0),
13 (7, 6, 0), (8, 6, 0), (9, 6, 0), (10, 8, 0), (11, 8, 0), (12, 8, 0),
14 (13, 16, 0), (15, 16, 0), (16, 16, 1), (24, 16, 4),
15]
16MAXDEPTH = 24
17
18
19def learn_table(engine, tsel, dim, quad=False):
20 """Returns {codeword bits (tuple) : tuple of values}."""
21 leaves = {}
22 level = [()]
23 depth = 0
24 while level:
25 depth += 1
26 if depth > MAXDEPTH:
27 raise RuntimeError(f"table {tsel}: tree deeper than {MAXDEPTH}")
28 probes = []
29 for prefix in level:
30 for fill in (0, 1):
31 bits = list(prefix) + [fill] * 48
32 if quad:
33 probes.append((bits, 0, 0, tsel))
34 else:
35 probes.append((bits, tsel, 1, 0))
36 got = engine.run(probes)
37 n = 4 if quad else 2
38 nxt = []
39 for i, prefix in enumerate(level):
40 v0 = np.minimum(values(got[2 * i], n), 15)
41 v1 = np.minimum(values(got[2 * i + 1], n), 15)
42 if tuple(v0) == tuple(v1):
43 leaves[prefix] = tuple(int(x) for x in v0)
44 else:
45 nxt.append(prefix + (0,))
46 nxt.append(prefix + (1,))
47 level = nxt
48 return leaves
49
50
51def check(leaves, dim, quad, name):
52 kraft = sum(2.0 ** -len(c) for c in leaves)
53 vals = sorted(leaves.values())
54 expect = dim ** (4 if quad else 2)
55 ok = True
56 if abs(kraft - 1.0) > 1e-12:
57 print(f" !! {name}: kraft = {kraft}")
58 ok = False
59 if len(leaves) != expect:
60 print(f" !! {name}: {len(leaves)} leaves, expected {expect}")
61 ok = False
62 if len(set(vals)) != len(vals):
63 print(f" !! {name}: duplicate values")
64 ok = False
65 return ok
66
67
68def main():
69 Minv = np.load("Minv.npy")
70 engine = Engine(Minv)
71 out = {}
72 allok = True
73 for tsel, dim, linbits in BIG_TABLES:
74 leaves = learn_table(engine, tsel, dim)
75 ok = check(leaves, dim, False, f"table {tsel}")
76 allok &= ok
77 maxlen = max(len(c) for c in leaves)
78 print(f"table {tsel:2d}: {len(leaves):3d} codes, max len {maxlen:2d}, "
79 f"linbits {linbits}, ok={ok}")
80 out[str(tsel)] = {"".join(map(str, c)): v for c, v in leaves.items()}
81 for tsel in (0, 1):
82 leaves = learn_table(engine, tsel, 2, quad=True)
83 ok = check(leaves, 2, True, f"count1 {tsel}")
84 allok &= ok
85 print(f"count1 {tsel}: {len(leaves)} codes, max len "
86 f"{max(len(c) for c in leaves)}, ok={ok}")
87 out[f"count1_{tsel}"] = {"".join(map(str, c)): v for c, v in leaves.items()}
88 with open("huffman.json", "w") as f:
89 json.dump(out, f, indent=0)
90 print("all tables consistent:", allok)
91 return 0 if allok else 1
92
93
94if __name__ == "__main__":
95 sys.exit(main())