forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBe898fd7Tahinli2 months ago3777 commits

flipctl-422.py141 lines, 5.6 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Flip control: prove the ffmpeg comparator BITES, before any verdict.
3
4[`cmp422-ffmpeg`][c] re-derives the decode->display assignment from LUMA
5identity on every call, so a flipped oracle sample can never be re-mapped --
6the identity it keys on is exactly what the flip breaks. This control
7therefore maps ONCE from the untouched streams and then flips one SAMPLE per
8arm with the mapping PINNED, which is the only way to ask "does one wrong
9sample move the count by exactly one?".
10
11Two independent directions, both required to pass:
12
13* **oracle arm** -- flip one ffmpeg sample. The plane that owns it must move by
14 EXACTLY +1, every other plane by exactly 0. On a byte-exact cell this is the
15 only thing that can move, so a `0/0/0` that survives a flip is a comparator
16 that is not reading the oracle.
17* **ours arm** -- flip one of OUR samples with the oracle untouched. Same
18 delta. This is the direction the whole corpus verdict rests on, and on a
19 `0/0/0` cell it is what separates "we agree" from "we never looked".
20
21Every decode->display SOLUTION is exercised, so an ambiguous mapping cannot
22hide a failure behind the one that was picked.
23
24Usage:
25 python3 scripts/flipctl-422.py cells.json
26
27`cells.json` entries: `{"name", "path", "prefix"}`, `prefix` being our
28`EC_AV1_FINAL_DUMP` prefix. Exit status 0 only if every arm passes.
29"""
30import importlib.util
31import json
32import os
33import sys
34
35_HERE = os.path.dirname(os.path.abspath(__file__))
36_spec = importlib.util.spec_from_file_location(
37 "cmp422_ffmpeg", os.path.join(_HERE, "cmp422-ffmpeg.py"))
38C = importlib.util.module_from_spec(_spec)
39_spec.loader.exec_module(C)
40
41
42def plane_counts(ours, disp, mapping, hidden, sizes, bps):
43 """The comparator's own accounting (`cmp422-ffmpeg.compare`'s `measure`),
44 with the decode->display assignment PINNED so a flipped sample cannot
45 re-map."""
46 tot = [0, 0, 0]
47 for oi, a in enumerate(ours):
48 if oi in hidden:
49 continue
50 b = disp[mapping[oi]]
51 off = 0
52 for pi, sz in enumerate(sizes):
53 for i in range(sz):
54 s = off + i * bps
55 if a[s:s + bps] != b[s:s + bps]:
56 tot[pi] += 1
57 off += sz * bps
58 return tot
59
60
61def flip_sample(buf, sample_index, bps):
62 """Flip bit 0 of one SAMPLE's low byte: exactly +1 wrong sample at both
63 depths (2**bitdepth < 2**16, so nothing carries out of the container)."""
64 out = bytearray(buf)
65 out[sample_index * bps] ^= 1
66 return bytes(out)
67
68
69def arms_for(sizes):
70 """(plane, sample_index) per plane: the first, the middle and the last."""
71 out = []
72 for pi, sz in enumerate(sizes):
73 out += [(pi, 0), (pi, sz // 2), (pi, sz - 1)]
74 return out
75
76
77def one_cell(cell, path, prefix):
78 w, h, ssx, ssy, depth, pix = C.probe_geometry(path)
79 bps = 1 if depth <= 8 else 2
80 sizes, fsz = C.plane_spans(w, h, ssx, ssy, bps)
81 disp = C.split(C.ffmpeg_frames(path, pix), sizes, fsz, cell)
82 ours = [open(p, "rb").read() for p in C.frame_files(prefix)]
83 if not ours:
84 raise SystemExit(f"{cell}: no EC_AV1_FINAL_DUMP frames -- vacuous")
85 for i, o in enumerate(ours):
86 if len(o) != fsz:
87 raise SystemExit(f"{cell}: our frame {i} is {len(o)} B, "
88 f"geometry {w}x{h} ss={ssx}{ssy} depth={depth} implies {fsz} B")
89 sols = C.map_decode_to_display(ours, disp, sizes, bps, cell)
90 shown = set()
91 for mapping, hidden in sols:
92 shown |= {d for oi, d in enumerate(mapping) if oi not in hidden}
93 shown = sorted(shown)
94 # Two shown display frames per cell -- the first and one in the middle --
95 # so the arm is not confined to frame 0.
96 ks = sorted({shown[0], shown[len(shown) // 2]})
97 results, arms = [], 0
98 base0 = plane_counts(ours, disp, sols[0][0], sols[0][1], sizes, bps)
99 for k in ks:
100 for sol_i, (mapping, hidden) in enumerate(sols):
101 dmap = {d: oi for oi, d in enumerate(mapping) if oi not in hidden}
102 if k not in dmap:
103 continue
104 oi = dmap[k]
105 base = plane_counts(ours, disp, mapping, hidden, sizes, bps)
106 for pi, si in arms_for(sizes):
107 off = sum(sizes[:pi]) + si # sample index inside the frame
108 want = [0, 0, 0]
109 want[pi] = 1
110 for side in ("oracle", "ours"):
111 if side == "oracle":
112 f = list(disp)
113 f[k] = flip_sample(disp[k], off, bps)
114 got = plane_counts(ours, f, mapping, hidden, sizes, bps)
115 else:
116 f = list(ours)
117 f[oi] = flip_sample(ours[oi], off, bps)
118 got = plane_counts(f, disp, mapping, hidden, sizes, bps)
119 delta = [got[i] - base[i] for i in range(3)]
120 ok = delta == want
121 results.append(ok)
122 arms += 1
123 if not ok:
124 print(f" FAIL {side:6s} disp f{k} plane {'YUV'[pi]} "
125 f"sample {si} sol{sol_i}: {base} -> {got} "
126 f"delta {delta} want {want}")
127 print(f"{cell}: baseline {base0} {len(ours)} decode / {len(disp)} shown, "
128 f"hidden {sols[0][1]}, {len(sols)} mapping solution(s), {arms} arms")
129 return results
130
131
132def main(spec_path):
133 rows = []
134 for c in json.load(open(spec_path)):
135 rows += one_cell(c["name"], c["path"], c["prefix"])
136 print(f"\n{sum(rows)} PASS, {len(rows) - sum(rows)} FAIL of {len(rows)} flip arms")
137 return 0 if all(rows) else 1
138
139
140if __name__ == "__main__":
141 sys.exit(main(sys.argv[1]))