forge
Explore
Log in

Don't you dare go Hollow

scripts: preserve the dixie-faithful decision-trace reference model used to localize the M2 blocker

Browse files at this commit

d24d4c425d89d9ed4c29d7359ca8c7a5255e7fc8Ahmet Kaan GümüşParent: a8debcf
scripts/vp8ref_model.pyadded+284 -0
@@ -0,0 +1,284 @@
1+#!/usr/bin/env python3
2+"""Dixie-faithful full key-frame VP8 decode model (modes + tokens, no
3+pixels) with per-decision tracing, interleaved per MB row exactly like
4+the reference decoder. Diff its `B` lines against the Rust decoder's
5+EC_VP8_TRACE output.
6+
7+Usage: python3 vp8ref.py frame.ivf
8+"""
9+import re
10+import sys
11+
12+TABLES_RS = "crates/ec-vp8/src/tables.rs"
13+
14+
15+class BD:
16+ def __init__(self, data):
17+ self.data = data
18+ self.pos = 2 if len(data) >= 2 else len(data)
19+ v = 0
20+ if len(data) >= 1:
21+ v = data[0] << 8
22+ if len(data) >= 2:
23+ v |= data[1]
24+ self.value = v
25+ self.range = 255
26+ self.bit_count = 0
27+
28+ def bool(self, prob):
29+ global DEC
30+ DEC += 1
31+ split = 1 + (((self.range - 1) * prob) >> 8)
32+ big = split << 8
33+ if self.value >= big:
34+ self.range -= split
35+ self.value -= big
36+ bit = 1
37+ else:
38+ self.range = split
39+ bit = 0
40+ while self.range < 128:
41+ self.value <<= 1
42+ self.range <<= 1
43+ self.bit_count += 1
44+ if self.bit_count == 8:
45+ self.bit_count = 0
46+ if self.pos < len(self.data):
47+ self.value |= self.data[self.pos]
48+ self.pos += 1
49+ print(f"B {self.pos} {self.bit_count} {prob} {bit}")
50+ return bit
51+
52+ def lit(self, n):
53+ v = 0
54+ for _ in range(n):
55+ v = (v << 1) + self.bool(128)
56+ return v
57+
58+ def maybe_int(self, n):
59+ if not self.bool(128):
60+ return 0
61+ v = self.lit(n)
62+ return -v if self.bool(128) else v
63+
64+ def tree(self, tree, probs):
65+ i = 0
66+ while True:
67+ b = self.bool(probs[i >> 1])
68+ i = tree[i + b]
69+ if i <= 0:
70+ return -i
71+
72+
73+def table(name):
74+ src = open(TABLES_RS).read()
75+ m = re.search(rf"pub const {name}: [^=]+= ", src)
76+ body = src[m.end(): src.index("];", m.end())]
77+ return [int(x) for x in re.findall(r"-?\d+", body)]
78+
79+
80+DEC = 0
81+
82+
83+def main():
84+ CUP = table("COEFF_UPDATE_PROBS")
85+ KF_BMODE = table("KF_BMODE_PROB")
86+ BANDS = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0]
87+ ZIGZAG = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15]
88+ CAT_EXTRA = [
89+ [173, 148, 140],
90+ [176, 155, 140, 135],
91+ [180, 157, 141, 134, 130],
92+ [254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129],
93+ ]
94+ KF_YMODE_TREE = [-4, 2, 4, 6, 0, -1, -2, -3]
95+ BMODE_TREE = [0, 2, -1, 4, -2, 6, 8, 12, -3, 10, -5, -6, -4, 14, -7, 16, -8, -9]
96+ UV_MODE_TREE = [0, 2, -1, 4, -2, -3]
97+ YMODE_MAP = {0: 0, 1: 2, 2: 3, 3: 1} # 16x16 mode -> bmode context
98+ LEFT = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8]
99+ ABOVE = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8]
100+
101+ raw = open(sys.argv[1], "rb").read()
102+ sz = int.from_bytes(raw[32:36], "little")
103+ frame = raw[44 : 44 + sz]
104+ tag = int.from_bytes(frame[0:3], "little")
105+ part0_sz = tag >> 5
106+ w = int.from_bytes(frame[6:8], "little") & 0x3FFF
107+ h = int.from_bytes(frame[8:10], "little") & 0x3FFF
108+ cols, rows = (w + 15) // 16, (h + 15) // 16
109+
110+ d = BD(frame[10 : 10 + part0_sz])
111+ d.lit(2) # colour space + clamping
112+ # segmentation
113+ if d.bool(128):
114+ d.bool(128)
115+ if d.bool(128):
116+ d.bool(128)
117+ for _ in range(4):
118+ d.maybe_int(7)
119+ for _ in range(4):
120+ d.maybe_int(6)
121+ # loop filter header
122+ d.lit(1)
123+ d.lit(6)
124+ d.lit(3)
125+ if d.bool(128):
126+ if d.bool(128):
127+ for _ in range(4):
128+ d.maybe_int(6)
129+ for _ in range(4):
130+ d.maybe_int(6)
131+ # token partitions
132+ log2 = d.lit(2)
133+ nparts = 1 << log2
134+ off = 10 + part0_sz
135+ sizes = []
136+ for i in range(nparts):
137+ if i < nparts - 1:
138+ sizes.append(int.from_bytes(frame[off : off + 3], "little"))
139+ off += 3
140+ else:
141+ sizes.append(len(frame) - off)
142+ parts = []
143+ po = off
144+ for i in range(nparts):
145+ parts.append(frame[po : po + sizes[i]])
146+ po += sizes[i]
147+ # quant
148+ d.lit(7)
149+ for _ in range(5):
150+ d.maybe_int(4)
151+ # reference header: refresh_entropy is read on key frames too
152+ d.bool(128)
153+ # coefficient probability updates
154+ probs = table("DEFAULT_COEFF_PROBS")
155+ idx = 0
156+ for _i in range(4):
157+ for _j in range(8):
158+ for _k in range(3):
159+ for _l in range(11):
160+ if d.bool(CUP[idx]):
161+ probs[idx] = d.lit(8)
162+ idx += 1
163+ # mb_no_skip_coeff
164+ skip_enabled = d.bool(128)
165+ prob_skip = d.lit(8) if skip_enabled else 128
166+
167+ # per-MB state; row 0 / col W are the DC_PRED border
168+ mbmode = [[0] * (cols + 1) for _ in range(rows + 1)]
169+ bmode = [[[0] * 16 for _ in range(cols + 1)] for _ in range(rows + 1)]
170+ skips = [[False] * (cols + 1) for _ in range(rows + 1)]
171+ above_ctx = [[0] * 9 for _ in range(cols)]
172+
173+ DEC = [0]
174+ def get_coeffs(ptype, n0, ctx, out):
175+ base = ptype * 264
176+ n = n0
177+ p = base + BANDS[n0] * 33 + ctx * 11
178+ DEC[0] += 1
179+ if DEC[0] == 2563:
180+ print(f"# at2563: ptype={ptype} n0={n0} n={n} band={BANDS[n]} ctx={ctx} nodeoff=0 entry={p} val={probs[p]}")
181+ if not d2.bool(probs[p]):
182+ return 0
183+ while True:
184+ n += 1
185+ DEC[0] += 1
186+ if DEC[0] == 2563:
187+ print(f"# at2563: ptype={ptype} n={n} band={BANDS[n]} nodeoff=1 entry={p+1} val={probs[p+1]}")
188+ if not d2.bool(probs[p + 1]):
189+ p = base + BANDS[n] * 33
190+ else:
191+ DEC[0] += 1
192+ if DEC[0] == 2563:
193+ print(f"# at2563: nodeoff=2 n={n} band={BANDS[n]} ctx={ctx} p={p}")
194+ if not d2.bool(probs[p + 2]):
195+ p = base + BANDS[n] * 33 + 11
196+ v = 1
197+ else:
198+ if not d2.bool(probs[p + 3]):
199+ if not d2.bool(probs[p + 4]):
200+ v = 2
201+ else:
202+ v = 3 + d2.bool(probs[p + 5])
203+ else:
204+ if not d2.bool(probs[p + 6]):
205+ if not d2.bool(probs[p + 7]):
206+ v = 5 + d2.bool(159)
207+ else:
208+ v = 7 + 2 * d2.bool(165)
209+ v += d2.bool(145)
210+ else:
211+ bit1 = d2.bool(probs[p + 8])
212+ bit0 = d2.bool(probs[p + 9 + bit1])
213+ cat = 2 * bit1 + bit0
214+ v = 0
215+ for prob in CAT_EXTRA[cat]:
216+ v += v + d2.bool(prob)
217+ v += 3 + (8 << cat)
218+ p = base + BANDS[n] * 33 + 22
219+ out[ZIGZAG[n - 1]] = -v if d2.bool(128) else v
220+ if n == 16 or not d2.bool(probs[p]):
221+ return n
222+ if n == 16:
223+ return 16
224+
225+ for r in range(1, rows + 1):
226+ d2 = BD(parts[(r - 1) % nparts])
227+ left_ctx = [0] * 9
228+ # phase 1: every mode record of the row (partition 0)
229+ for c in range(1, cols + 1):
230+ print(f"M {r-1} {c-1}")
231+ if skip_enabled:
232+ skips[r][c] = bool(d.bool(prob_skip))
233+ ym = d.tree(KF_YMODE_TREE, [145, 156, 163, 128])
234+ mbmode[r][c] = ym
235+ if ym == 4:
236+ for j in range(16):
237+ if j < 4:
238+ am = mbmode[r - 1][c]
239+ a = bmode[r - 1][c][j + 12] if am == 4 else YMODE_MAP[am]
240+ else:
241+ a = bmode[r][c][j - 4]
242+ if j % 4 == 0:
243+ lm = mbmode[r][c - 1]
244+ l = bmode[r][c - 1][j + 3] if lm == 4 else YMODE_MAP[lm]
245+ else:
246+ l = bmode[r][c][j - 1]
247+ bmode[r][c][j] = d.tree(BMODE_TREE, KF_BMODE[(a * 10 + l) * 9 : (a * 10 + l) * 9 + 9])
248+ d.tree(UV_MODE_TREE, [142, 114, 183])
249+ # phase 2: tokens of the row (partition r-1 % n)
250+ for c in range(1, cols + 1):
251+ ac = above_ctx[c - 1]
252+ hy2 = mbmode[r][c] != 4
253+ if skips[r][c]:
254+ for i in range(8):
255+ ac[i] = 0
256+ left_ctx[i] = 0
257+ if hy2:
258+ ac[8] = 0
259+ left_ctx[8] = 0
260+ continue
261+
262+ def one(blkidx, ptype, n0):
263+ o = [0] * 16
264+ ctxv = min(2, left_ctx[LEFT[blkidx]] + ac[ABOVE[blkidx]])
265+ print(f"T {r-1} {c-1} {blkidx} {ptype} {ctxv}")
266+ eob = get_coeffs(ptype, n0, ctxv, o)
267+ t = 1 if eob > 0 else 0
268+ left_ctx[LEFT[blkidx]] = t
269+ ac[ABOVE[blkidx]] = t
270+
271+ if hy2:
272+ one(24, 1, 0)
273+ for b in range(16):
274+ one(b, 0, 1)
275+ else:
276+ for b in range(16):
277+ one(b, 3, 0)
278+ for b in range(4):
279+ one(16 + b, 2, 0)
280+ for b in range(4):
281+ one(20 + b, 2, 0)
282+
283+
284+main()