forge
Explore
Log in

Don't you dare go Hollow

lane/av1422ibctxsel: report -- the ss (1,0) non-split chroma gather is discriminated, routed, and gated

Browse files at this commit

655058592642c6de72bde9394e0ff5eb98a757a8Ahmet Kaan GümüşParent: 785e89a
lanes/av1422ibctxsel.report.mdadded+218 -0
@@ -0,0 +1,218 @@
1+# lane-av1422ibctxsel — the ss (1, 0) non-split chroma gather of
2+# `decode_intrabc_owned_rect` is DISCRIMINATED by a pinned witness, routed
3+# through the pair rule, and gated red-before/green-after
4+
5+Base `main` = `6d646f65` (branch `lane/av1422ibctxsel`, worktree
6+`~/.cache/wt/av1422ibctxsel`). Local only: no push, no merge. The fix commit
7+is `b5e034d4` (source + fixture + gate); this report is a second commit.
8+
9+## Verdict
10+
11+**Witnessed and fixed.** A real aomenc 4:2:2 stream enters the non-split
12+chroma gather of `decode_intrabc_owned_rect` **24 times** and, on **five** of
13+those entries (plane 2, strips mi (64,16)..(64,80) of frame 0), the two
14+gathers disagree: `dc_sign_ctx` **1** through the luma-span `around_mi_rect`
15+vs **0** through libaom's pair rule. Pre-fix that wrong context desyncs the
16+tile and **no frame decodes at all** (the decoder refuses mid-frame); post-fix
17+the pinned bytes are **byte-exact against ffmpeg on 4/4 frames**, with the
18+route counter reading 24. Both non-split chroma arms of the function are
19+routed under the `ss_x == 1 && ss_y == 0` guard; every other subsampling keeps
20+the plain gather verbatim. The witness is committed and pinned (size +
21+fnv1a64 + sha256) and the gate is mutation-proven (route reverted → red).
22+
23+## 1. The class, and the exact arithmetic a witness has to flip
24+
25+Both residual branches of `decode_intrabc_owned_rect` (`leaves.is_some()` —
26+the var-tx tree — and its `leaves == None` twin) read their **chroma**
27+coefficient context from `neighbours.around_mi_rect((mi_r, mi_c), bw, bh)` —
28+the block's **luma** span — and hand `around[1]`/`around[2]` to
29+`read_inter_plane_rect` planes 1 and 2 (`decode.rs:18037→18045/18062` in the
30+tree branch, `18076→18125/18142` in the twin, base line numbers). At ss
31+(1, 0) one chroma 4-px cell spans TWO luma mi columns, so the per-mi sum
32+counts each chroma column twice; libaom's `get_txb_ctx_general` reads one
33+vote per CHROMA cell, which is `around_mi_422_chroma`'s every-second
34+sampling (`decode.rs:10555`).
35+
36+`dc_sign_ctx(vote)` is `vote.signum()` → {0 zero, 1 negative, 2 positive}
37+(`decode.rs:8254`), so a witness needs the **sign** to differ:
38+
39+* full gather = `2A + L`; pair rule = `A + L`, where `A` = the pair-rule
40+ above sum and `L` = the left sum (identical in both).
41+* Chroma units at 4:2:2 are ≥2 luma mi wide and even-mi aligned, so the
42+ above part obeys **`full_above = 2 × pair_above` ALWAYS** — the above
43+ votes alone can never disagree in sign. The asymmetry lives in `L`: a flip
44+ needs `A` and `L` of **opposite sign with |L| ∈ [|A|, 2|A|)**.
45+* With one 32x32 chroma TU per strip (the non-split arm's unit), `L = 8 ×
46+ (previous strip's unit sign)` ∈ {+8, −8, 0} and `A = Σ (W_u/2) s_u` over
47+ the units covering the row above. The sharpest case is `A = ±8, L = ∓8`:
48+ `full = ±8` (a sign) vs `pair = 0` (ZERO) — a `dc_sign_ctx` 2-vs-0 or
49+ 1-vs-0 split.
50+
51+This is also why `lane-av1422ibcrect`'s A1 measured 0/24: on that stream all
52+above votes shared one sign (and `A = 0` for strips 1..5), so no arithmetic
53+could flip.
54+
55+## 2. Encoder facts the hunt established (libaom source in `~/.cache/aom-oracle/src`)
56+
57+* **`tx_mode` is downgraded to `TX_MODE_LARGEST` when the frame splits no
58+ transform**: `encodeframe.c:2690` — `if (features->tx_mode == TX_MODE_SELECT
59+ && cpi->td.mb.txfm_search_info.txb_split_count == 0) features->tx_mode =
60+ TX_MODE_LARGEST;`. A1's smooth sine content never splits → `txsel=false`
61+ (the ibcrect report's observation) even with `--enable-tx-size-search=1`.
62+ **A high-frequency patch makes `txb_split_count > 0` and the header bit
63+ stays SELECT** (E5+ below) — so the ticket's "vary tx-mode" lever is this.
64+* **The intraBC hash is LUMA-ONLY**: `hash_motion.c:398` `av1_get_block_hash_
65+ value(..., const uint8_t *y_src, ...)` hashes 2x2 Y sub-blocks of
66+ `cpi->source`; chroma is not in the key. Chroma offsets therefore cannot
67+ break a luma match — but they do enter the DV's RD cost (E4's −20 strip
68+ band made the encoder drop intraBC on 4 of 6 strips).
69+* **The geq source runs in the 4:2:0 domain** (`nullsrc … format=yuv420p`),
70+ so a band written in "luma rows" silently never fires for chroma: E2
71+ changed NOTHING (bit-identical output to A1, same sha256). The band must be
72+ expressed in 4:2:0 chroma rows (luma row `y` = chroma row `y/2`).
73+
74+## 3. The attempt table (8 aomenc encodes, all ≤180 s; budget was ≤12)
75+
76+Common flags (all rows): `--codec=av1 --i422 --profile=2 --passes=1
77+--end-usage=q --cpu-used=0 --threads=1 --row-mt=0 --lag-in-frames=0
78+--kf-max-dist=1 --tile-columns=0 --sb-size=64 --min-partition-size=32
79+--max-partition-size=64 --enable-rect-partitions=1 --enable-ab-partitions=1
80+--enable-1to4-partitions=0 --enable-palette=0 --tune-content=screen
81+--enable-intrabc=1 --deltaq-mode=0 --enable-tx-size-search=1 --obu`,
82+384x288, 4 frames, from a `geq` source; A1's base content is
83+`lum=128+90*sin((X+2N)/37)+30*sin(Y/29) cb=128+30*sin(X/23)
84+cr=128+30*sin((Y+N)/19)`.
85+
86+| id | delta over A1 | bytes | sha256 | arm | txsel | entries | ctx diffs |
87+|---|---|---|---|---|---|---|---|
88+| **A1** control | none (cq 32) | 1244 | `fd36bae4…b397db` | lar twin | false | 24 | **0** |
89+| E2 | `cr += 30*if(between(Y,192,255))` (luma rows — never fires) | 1244 | `fd36bae4…` (same) | lar | false | 24 | 0 |
90+| E2b | same band written in 4:2:0 chroma rows (96..127) | 1203 | `3bf9709a…3b1e54` | lar | false | 24 | 0 (above `A=+8`, strips `+8`) |
91+| E3 | band `−30` (chroma rows 96..127) | 1312 | `77d58313…077306` | lar | false | 11 | 0 (above mostly `None`) |
92+| E4 | E2b plus strip band `−20` (chroma rows 128..143) | 1339 | `fc68088a…8c3415b` | lar | false | 5 (intraBC survived on 2 of 6 strips) | 0 |
93+| E5 | luma `+40*sin(X*π/2)` patch, rows 0..31 x<64 | 2173 | `f4495f32…043c26f5e` | lar | **true** | 18 | 0 |
94+| E6 | E5 + `cr += 25*if(Y in 0..31) + 30*if(Y in 96..127)` + same luma patch inside strip 0's band | 3226 | `8d1bc1ec…e64459166` | lar | true | 17 | **4** (1 visible before the pre-fix refusal aborted the decode) |
95+| **E7** witness | E6 minus the strip luma patch | 2170 | `4f82d5da8b4c3d6553953fc373001a1c7a729c9b189214b65503af1e04eb6b68` | lar | true | 24 | **5** |
96+
97+E7's five discriminating entries (probe print, pre-fix):
98+
99+ EC_IBCNS422P lar mi=(64,16) … ss=(1,0) txsel=true plane=2 fv=-8 pv=0 fctx=1 pctx=0 diff=1
100+ EC_IBCNS422P lar mi=(64,32) … plane=2 fv=-8 pv=0 fctx=1 pctx=0 diff=1
101+ EC_IBCNS422P lar mi=(64,48) … plane=2 fv=-8 pv=0 fctx=1 pctx=0 diff=1
102+ EC_IBCNS422P lar mi=(64,64) … plane=2 fv=-8 pv=0 fctx=1 pctx=0 diff=1
103+ EC_IBCNS422P lar mi=(64,80) … plane=2 fv=-8 pv=0 fctx=1 pctx=0 diff=1
104+
105+i.e. `A = −8` (above uniform, the `+30` band) and `L = +8` (each strip's own
106+32x32 V unit is positive) → `full = 2A + L = −8` (negative, ctx 1) vs
107+`pair = A + L = 0` (zero, ctx 0). Five strips, frame 0 (frame 0 is the one
108+whose `cr` phase puts the strip residual positive).
109+
110+The entry/diff columns above were re-measured in one pass with the temporary
111+probe (the probe prints the raw gather vs the pair rule explicitly, so its
112+`diff` is independent of the route the decoder currently takes). E7's 24 is
113+corroborated by a second, committed instrument: the route counter
114+`IBC_OWNED_RECT_422_PAIR_HITS` reads exactly 24 in the gate.
115+
116+The probe (`EC_IBCNS422P`, env-gated, both arms, printing the luma-span
117+gather vs the pair rule) was **removed before the commit** — no probe and no
118+`EC_AV1_ALLOW_422_PROBE` remains in the tree.
119+
120+## 4. The fix
121+
122+`decode.rs`, `decode_intrabc_owned_rect` only:
123+
124+* tree branch: `let around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
125+ hit!(IBC_OWNED_RECT_422_PAIR_HITS); neighbours.around_mi_422_chroma(…) }
126+ else { neighbours.around_mi_rect(…) };` (plane 0 unused in this branch);
127+* twin branch: the same gate on a NEW `around_chroma` used by planes 1/2;
128+ `around` stays the plain luma-span read that plane 0's own context uses
129+ (`ss_x == 0` / `ss_y != 0` keep it for chroma too, verbatim);
130+* new counter `IBC_OWNED_RECT_422_PAIR_HITS` + accessor/reset
131+ (`ibc_owned_rect_422_pair_hits`), in the crate's `hit!` pattern.
132+
133+The census's stale comment at the tree branch ("UNWITNESSED", "the only
134+`around_mi_rect` …") was replaced by the dated routing comment; it also
135+records the honest residue: the tree branch itself still has **no** stream
136+that splits a transform on the strip (`TX_MODE_SELECT` + `txfm_partition` on
137+the 64x32 strip) — the witness enters the twin, and both branches share the
138+same routed code.
139+
140+## 5. Evidence
141+
142+* **Gate**: `a_422_intrabc_owned_rect_nonsplit_chroma_context_uses_the_pair_rule`
143+ (`stream.rs`) — pins size 2170 + fnv1a64 `0x92e2c4e45ca27e34` + sha256,
144+ asserts `assert_422_stream_pixel_exact(…, 384, 288, 4, 8, true)` (ffmpeg
145+ `yuv422p`, per-plane coefficient-unit census deltas
146+ `units=[149, 219, 225] coded=[126, 27, 98]`, all non-zero), asserts the
147+ route fired exactly **24** times, and asserts the 4:4:4 `r512.obu` arm
148+ fires it **0** times (guard-does-not-leak arm).
149+* **Red-before (mutation proof)**: with both routed conditions forced
150+ `if false` (temporary, reverted), the gate FAILS on the same pinned bytes:
151+ `decode_stream refused a real 4:2:2 stream: … a block size 4x8, 8x16 or
152+ 16x4 … has no chroma plane block at this frame's subsampling mode`.
153+* **Green-after**: 4/4 frames byte-exact vs ffmpeg; counter 24; gate + the
154+ 422 `sb128_strip` gate + `a_16x4_intrabc_pair_strip_decodes_pixel_exact`
155+ (4:2:0) + `a_444_intrabc_owned_rect_strip_sizes_its_chroma_plane_block_and_decodes`
156+ (4:4:4) all pass in one scoped run (`4 passed; 846 filtered out`,
157+ 1.8 s). Scoped tests only; no full suite.
158+* `cargo check -p ec-av1` clean (the one pre-existing `pin_reporting`
159+ dead-code warning, unrelated).
160+
161+## 6. Findings for other lanes (measured here, not fixed here)
162+
163+1. **The pre-fix refusal on E6/E7 was a DESYNC SYMPTOM, not a real
164+ sub-8x8 shape.** Pre-fix, our decoder refuses those bytes mid-frame while
165+ **both oracles decode them fully** (`aomdec --rawvideo` → exit 0,
166+ 884736 B = 4×221184; ffmpeg → 884736 B). Post-fix (same bytes, route
167+ fixed) the refusal is gone and the decode is byte-exact. So the wrong
168+ `dc_sign_ctx` → wrong dc-sign symbol → desync → the subsize guard fires on
169+ garbage. The refusal string itself is libaom's
170+ `decodeframe.c:1456` rule, but its trigger here was ours. `lane-av1422ibcrect`'s
171+ A2/A4 refusals were NOT re-examined by this lane; their streams were not
172+ measured against aomdec here.
173+2. **`scripts/fixture-library.tsv` needs a canonical-environment
174+ regeneration** (deferred, unblock: run
175+ `scripts/gen-fixture-library.sh` where `$ROOT/fixtures` is the real
176+ library). Measured: regenerating on a **pristine `git archive` of
177+ `6d646f65` with no edits** already differs from the committed manifest by
178+ 651 diff lines here (e.g. "212" vs "48" unresolved media literals,
179+ double-slash `fixtures//…` rows), and even with
180+ `EC_FIXTURES=<primary>/fixtures` the output is not idempotent with HEAD's
181+ file (dropped `encode.rs` rows, unreferenced-pin count 4→6). So this lane
182+ does NOT commit manifest churn; the new fixture's row (measured:
183+ `crates/ec-av1/fixtures/ibc422_nonsplit_ctx.obu crates/ec-av1/src/stream.rs:3587 … 4f82d5da…`)
184+ and the stream.rs line shifts will land with that regeneration.
185+3. Process note (recorded for truthfulness): during the mutation proof a
186+ bulk in-place replace of the routed-condition line matched a THIRD,
187+ pre-existing site in another function (`around_mi_422_chroma(pair_mi, pw, ph)`
188+ at base `19863-19872`). It was restored exactly and verified: the final
189+ `git diff` hunks touch only statics/accessors and
190+ `decode_intrabc_owned_rect` in `decode.rs`, and that function's text is
191+ byte-identical to HEAD outside the intended hunks.
192+
193+## 7. Repro
194+
195+```text
196+git worktree add -b lane/av1422ibctxsel ~/.cache/wt/av1422ibctxsel 6d646f65
197+cd ~/.cache/wt/av1422ibctxsel
198+
199+# witness (E7): aomenc = ~/.cache/aom-oracle/build/aomenc
200+ffmpeg -v error -f lavfi -i "nullsrc=size=384x288:rate=25,format=yuv420p,\
201+ geq=lum='128+90*sin((X+2*N)/37)+30*sin(Y/29)+40*sin(X*1.5707963)*(lt(X,64)*lt(Y,32))':\
202+ cb='128+30*sin(X/23)':\
203+ cr='128+30*sin((Y+N)/19)+25*if(between(Y,0,31),1,0)+30*if(between(Y,96,127),1,0)'" \
204+ -frames:v 4 -pix_fmt yuv422p -strict -1 -f yuv4mpegpipe -y E7.y4m
205+aomenc --codec=av1 --i422 --profile=2 --passes=1 --end-usage=q --cpu-used=0 \
206+ --threads=1 --row-mt=0 --lag-in-frames=0 --kf-max-dist=1 --tile-columns=0 \
207+ --sb-size=64 --min-partition-size=32 --max-partition-size=64 \
208+ --enable-rect-partitions=1 --enable-ab-partitions=1 --enable-1to4-partitions=0 \
209+ --enable-palette=0 --tune-content=screen --enable-intrabc=1 --deltaq-mode=0 \
210+ --enable-tx-size-search=1 --obu --cq-level=32 --limit=4 -o E7.obu E7.y4m
211+sha256sum E7.obu # 4f82d5da8b4c3d6553953fc373001a1c7a729c9b189214b65503af1e04eb6b68
212+
213+# gate (green)
214+CARGO_TARGET_DIR=~/.cache/tgt-av1422ibctxsel \
215+ cargo test -p ec-av1 --lib a_422_intrabc_owned_rect_nonsplit_chroma_context_uses_the_pair_rule
216+```
217+
218+`E7` = `crates/ec-av1/fixtures/ibc422_nonsplit_ctx.obu` (byte-identical).