vp9kf: real content decodes byte-exact -- per-SB-row left contexts, MI-row chroma extents
Browse files at this commitbd4fadc55a519509dc3392d0254d5f3324f70d64Parent: 7bd2aba4 changed files +432 -11
- crates/ec-vp9/src/decode.rs +22 -4
- crates/ec-vp9/src/loopfilter.rs +33 -6
- lanes/realdesync.report.md +279 -0
- lanes/vp9kf.report.md +98 -1
crates/ec-vp9/src/decode.rsmodified+22 -4
| @@ -269,10 +269,28 @@ impl Decoder { | |||
| 269 | ectx.iter_mut().for_each(|p| p.above.fill(0)); | 269 | ectx.iter_mut().for_each(|p| p.above.fill(0)); |
| 270 | mi.above_seg.fill(0); | 270 | mi.above_seg.fill(0); |
| 271 | for sb_row in (row_lo..row_hi).step_by(SB_MI) { | 271 | for sb_row in (row_lo..row_hi).step_by(SB_MI) { |
| 272 | - if sb_row == row_lo { | 272 | + // libvpx zeroes the left ENTROPY and PARTITION contexts at |
| 273 | - ectx.iter_mut().for_each(|p| p.left = [0; 32]); | 273 | + // the start of EVERY superblock row, not just the tile's |
| 274 | - mi.left_seg = [0; 32]; | 274 | + // first one (vp9_decodeframe.c:2256-2258 plain |
| 275 | - } | 275 | + // `decode_tiles`; the row-mt twin is :2114-2115 and the |
| 276 | + // per-tile-row twins :1825-1826 / :1898-1899): the left | ||
| 277 | + // edge of an SB row has no pixels to its left. Our luma | ||
| 278 | + // left index wraps at mi_row 16, so a stale value from two | ||
| 279 | + // SB rows earlier would otherwise be read here. | ||
| 280 | + // `above_context`/`above_seg_context` are frame-scoped in | ||
| 281 | + // libvpx (`memset` at :2076-2080, before the tile-row | ||
| 282 | + // loop) and indexed by ABSOLUTE mi_col (`set_skip_context`, | ||
| 283 | + // vp9_onyxc_int.h:409-412), so zeroing them once per tile | ||
| 284 | + // is equivalent only for `log2_tile_rows == 0`, where the | ||
| 285 | + // disjoint-column argument holds (each tile's xd points | ||
| 286 | + // into its own column range: vp9_init_macroblockd, | ||
| 287 | + // vp9_onyxc_int.h:381-393). With tile ROWS the first SB row | ||
| 288 | + // of tile row k > 0 may legitimately read above state | ||
| 289 | + // written by tile row k - 1, which the fills at :269-270 | ||
| 290 | + // discard: the tile-row path is NOT equivalent and is not | ||
| 291 | + // handled here. | ||
| 292 | + ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 293 | + mi.left_seg = [0; 32]; | ||
| 276 | for sb_col in (col_lo..col_hi).step_by(SB_MI) { | 294 | for sb_col in (col_lo..col_hi).step_by(SB_MI) { |
| 277 | self.decode_partition( | 295 | self.decode_partition( |
| 278 | &mut r, | 296 | &mut r, |
crates/ec-vp9/src/loopfilter.rsmodified+33 -6
| @@ -499,6 +499,19 @@ impl LfGrids { | |||
| 499 | (left, above, int4) | 499 | (left, above, int4) |
| 500 | } | 500 | } |
| 501 | 501 | ||
| 502 | + /// Filter level of the 8x8 block at MI `(r, c)`. The chroma passes can | ||
| 503 | + /// ask for a cell whose MI row/col sits past the frame edge: libvpx reads | ||
| 504 | + /// its (zero-filled) `lfl_y` there, and `vp9_adjust_mask` guarantees the | ||
| 505 | + /// mask bit for such a cell is already cleared, so the value only has to | ||
| 506 | + /// be safe, not meaningful. `0` also means "no filter", which keeps the | ||
| 507 | + /// edge emission identical. | ||
| 508 | + fn level_at(&self, r: usize, c: usize) -> u8 { | ||
| 509 | + if r >= self.mi_rows || c >= self.mi_cols { | ||
| 510 | + return 0; | ||
| 511 | + } | ||
| 512 | + self.level8[r * self.mi_cols + c] | ||
| 513 | + } | ||
| 514 | + | ||
| 502 | /// Filter the frame's three planes in place. `y_w/h`, `uv_w/h` are | 515 | /// Filter the frame's three planes in place. `y_w/h`, `uv_w/h` are |
| 503 | /// the coded (aligned) plane sizes. | 516 | /// the coded (aligned) plane sizes. |
| 504 | pub(crate) fn filter_frame( | 517 | pub(crate) fn filter_frame( |
| @@ -730,14 +743,24 @@ impl LfGrids { | |||
| 730 | // libvpx runs the whole vertical pass before the | 743 | // libvpx runs the whole vertical pass before the |
| 731 | // horizontal one; they overlap, so order matters. | 744 | // horizontal one; they overlap, so order matters. |
| 732 | for rg in 0..4usize { | 745 | for rg in 0..4usize { |
| 733 | - let y8 = sbr * 4 + 8 * rg; | 746 | + // vp9_filter_block_plane_ss11 iterates MI rows |
| 734 | - if y8 + 8 > uv_h { | 747 | + // `r += 4` while `mi_row + r < mi_rows` |
| 748 | + // (vp9_loopfilter.c:1400), so the row GROUP is | ||
| 749 | + // processed whenever its first MI row is inside the | ||
| 750 | + // frame — even when its 8 chroma rows overhang the | ||
| 751 | + // visible height into the padded buffer. Gating on | ||
| 752 | + // `y8 + 8 <= uv_h` instead skipped the whole group and | ||
| 753 | + // lost the visible bottom rows (the 1080p case: | ||
| 754 | + // mi_rows 135, last group = chroma rows 536..543, | ||
| 755 | + // 536..539 visible). | ||
| 756 | + if sbr + 4 * (rg >> 1) >= self.mi_rows { | ||
| 735 | continue; | 757 | continue; |
| 736 | } | 758 | } |
| 759 | + let y8 = sbr * 4 + 8 * rg; | ||
| 737 | for cg in 0..4usize { | 760 | for cg in 0..4usize { |
| 738 | let x8 = sbc * 4 + 8 * cg; | 761 | let x8 = sbc * 4 + 8 * cg; |
| 739 | let bit = 1u16 << (rg * 4 + cg); | 762 | let bit = 1u16 << (rg * 4 + cg); |
| 740 | - let lvl = self.level8[(sbr + 2 * rg) * self.mi_cols + sbc + 2 * cg]; | 763 | + let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg); |
| 741 | if lvl == 0 { | 764 | if lvl == 0 { |
| 742 | continue; | 765 | continue; |
| 743 | } | 766 | } |
| @@ -787,14 +810,18 @@ impl LfGrids { | |||
| 787 | } | 810 | } |
| 788 | } | 811 | } |
| 789 | for rg in 0..4usize { | 812 | for rg in 0..4usize { |
| 790 | - let y8 = sbr * 4 + 8 * rg; | 813 | + // Horizontal pass: `r += 2` while `mi_row + r < |
| 791 | - if y8 + 8 > uv_h { | 814 | + // mi_rows` (vp9_loopfilter.c:1431) — one row group per |
| 815 | + // iteration, same MI-row-gated boundary as the | ||
| 816 | + // vertical pass above. | ||
| 817 | + if sbr + 2 * rg >= self.mi_rows { | ||
| 792 | continue; | 818 | continue; |
| 793 | } | 819 | } |
| 820 | + let y8 = sbr * 4 + 8 * rg; | ||
| 794 | for cg in 0..4usize { | 821 | for cg in 0..4usize { |
| 795 | let x8 = sbc * 4 + 8 * cg; | 822 | let x8 = sbc * 4 + 8 * cg; |
| 796 | let bit = 1u16 << (rg * 4 + cg); | 823 | let bit = 1u16 << (rg * 4 + cg); |
| 797 | - let lvl = self.level8[(sbr + 2 * rg) * self.mi_cols + sbc + 2 * cg]; | 824 | + let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg); |
| 798 | if lvl == 0 { | 825 | if lvl == 0 { |
| 799 | continue; | 826 | continue; |
| 800 | } | 827 | } |
lanes/realdesync.report.mdadded+279 -0
| @@ -0,0 +1,279 @@ | |||
| 1 | +# lane realdesync (Vp9RealDiag) — FIRST divergent decision on real content | ||
| 2 | + | ||
| 3 | +Worktree `/home/user/Documents/Code/Rust/edith_codecs-realdesync` @ `3dc719b4`, detached. | ||
| 4 | +**No file under `crates/ec-vp9/src/` was edited.** Everything below is read-only diagnosis plus | ||
| 5 | +two throwaway scratch locations: the oracle sources `/tmp/vp9loss/libvpx-src` (instrumented, | ||
| 6 | +see §5) and a full copy of the crate at `/tmp/vp9fix` used to verify the fix in §4. | ||
| 7 | + | ||
| 8 | +Content: a real 320x240 screen recording, re-encoded all-intra VP9 (`-g 1 -deadline good | ||
| 9 | +-cpu-used 4 -crf 32 -tile-columns 0 -tile-rows 0`, 3 frames), IVF at `/tmp/ec320/allintra.ivf`. | ||
| 10 | + | ||
| 11 | +--- | ||
| 12 | + | ||
| 13 | +## 1. The first divergent decision | ||
| 14 | + | ||
| 15 | +**Block:** `mi_row=16, mi_col=0, bsize=BLOCK_4X4 (0), tx_size=TX_4X4 (0), mode=2, uv_mode=0, | ||
| 16 | +skip=0, segment=0` — the **first block of superblock row 2**; its luma TX_4X4 transform block | ||
| 17 | +sits at pixel `(x=0, y=128)`. | ||
| 18 | + | ||
| 19 | +**First syntax element whose value differs:** the first bool of that transform block's | ||
| 20 | +coefficient syntax — the EOB branch byte read with | ||
| 21 | +`coef_probs[TX_4X4][PLANE_TYPE_Y][band 0][ctx][EOB_CONTEXT_NODE]` | ||
| 22 | +(libvpx `vp9_detokenize.c` `decode_coefs`: `band = *band_translate++; prob = coef_probs[band][ctx]; | ||
| 23 | +if (!read_bool(r, prob[EOB_CONTEXT_NODE], ...)) break;`). | ||
| 24 | + | ||
| 25 | +| | prob | bit | txb_ctx | trace | | ||
| 26 | +|---|---|---|---|---| | ||
| 27 | +| oracle (libvpx `drv`) | **195** | 1 | **0** | `/tmp/ot_trace_txb.txt` line **77376** (`BOR 7113 <count> 195 1`, bool #74792) | | ||
| 28 | +| ours (`ec-vp9`) | **84** | 1 | **1** | `/tmp/ot_ours.txt` line **93153** (`B 7114 <count> 84 1`, bool #74792) | | ||
| 29 | + | ||
| 30 | +Everything before it agrees: the first **74 792** `read_bool` calls of the frame are identical in | ||
| 31 | +probability **and** in decoded bit between the two decoders (index-aligned, because both streams | ||
| 32 | +agree by construction to that point), including the 26 bools of that block's own mode info | ||
| 33 | +(partition tree + 4x4 intra modes + tx flag) which immediately precede it. | ||
| 34 | + | ||
| 35 | +The prob⇒ctx mapping is not assumed; it is **recovered from the oracle's own trace** (TXBDUMP | ||
| 36 | +gives the ctx of every transform block, BTRACE gives every bool): for `(plane 0, TX_4X4)` the | ||
| 37 | +EOB-node prob is single-valued per ctx — | ||
| 38 | + | ||
| 39 | +``` | ||
| 40 | +(plane,tx,ctx) -> EOB-node prob (recovered over the whole agreeing prefix) | ||
| 41 | +(0,0,0) -> 195 (0,0,1) -> 84 (0,0,2) -> 18 | ||
| 42 | +(1,0,0) -> 214 (1,0,1) -> 132 (1,0,2) -> 42 # U | ||
| 43 | +(2,0,*) -> same as U # V | ||
| 44 | +``` | ||
| 45 | + | ||
| 46 | +So the oracle asked its question with **ctx=0** and we asked the same question with **ctx=1**; | ||
| 47 | +libvpx ended up decoding `eob=4` for that TU. | ||
| 48 | + | ||
| 49 | +Our own decoder's `CTX` trace hook prints the reason directly (it is exactly the code at | ||
| 50 | +`decode.rs:730-738`): | ||
| 51 | + | ||
| 52 | +``` | ||
| 53 | +[reads= 74792] MODE row=16 col=0 bsize=0 skip=0 tx=0 y=2 uv=0 | ||
| 54 | +[reads= 74792] CTX p=0 prob=[84, 32, 64] ax=0 ay=0 a=0 l=1 ctx=1 | ||
| 55 | +[reads= 74792] TXB p=0 x=0 y=128 tx=0 a=0 l=1 deq=38,44 | ||
| 56 | +``` | ||
| 57 | + | ||
| 58 | +`a=0` (above eob context) is right; **`l=1` (left eob context) is stale** — the left slot must be | ||
| 59 | +0 at the first column of a superblock row, and it is not. | ||
| 60 | + | ||
| 61 | +**Ruled out** (all three would have produced the same "first divergence at a read" shape): | ||
| 62 | +* partition / bsize / tx_size / intra-mode divergences — the 26 mode-info bools and the oracle's | ||
| 63 | + `MODE 0 16 0 0 0 2 0 0 0` line agree with our `MODE row=16 col=0 bsize=0 skip=0 tx=0 y=2 uv=0`; | ||
| 64 | +* the frame's coefficient probability table (`coef_probs` after the header's diff updates): our | ||
| 65 | + prob **84** *is* the frame's ctx=1 entry, i.e. the table is right, only the ctx index is wrong; | ||
| 66 | +* a plane/tx-size/band misread: the prob rows our decoder printed (`[84, 32, 64]`) are a single | ||
| 67 | + coherent (band 0, ctx 1) row of the TX_4X4/Y table. | ||
| 68 | +* decoded-value drift in the arithmetic decoder: the bool *values* agree; only the probability | ||
| 69 | + differs, so no reader-state desync preceded this point. | ||
| 70 | + | ||
| 71 | +--- | ||
| 72 | + | ||
| 73 | +## 2. Ranked candidate root causes | ||
| 74 | + | ||
| 75 | +### 1. (PROVEN — and fixed+verified in a throwaway copy) missing per-superblock-row reset of the left entropy contexts | ||
| 76 | + | ||
| 77 | +`crates/ec-vp9/src/decode.rs`, tile loop (`~:270-276`): | ||
| 78 | + | ||
| 79 | +```rust | ||
| 80 | +for sb_row in (row_lo..row_hi).step_by(SB_MI) { | ||
| 81 | + if sb_row == row_lo { // <-- only the TILE'S FIRST SB row | ||
| 82 | + ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 83 | + mi.left_seg = [0; 32]; | ||
| 84 | + } | ||
| 85 | + for sb_col in (col_lo..col_hi).step_by(SB_MI) { ... } | ||
| 86 | +``` | ||
| 87 | + | ||
| 88 | +libvpx clears the same state at the start of **every** superblock row — `vp9_decodeframe.c:2256-2258` | ||
| 89 | +(the plain `decode_tiles` path, which is what a 1-thread `drv` takes): | ||
| 90 | + | ||
| 91 | +```c | ||
| 92 | +for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; mi_row += MI_BLOCK_SIZE) { | ||
| 93 | + vp9_zero(tile_data->xd.left_context); | ||
| 94 | + vp9_zero(tile_data->xd.left_seg_context); | ||
| 95 | + for (mi_col = ...) decode_partition(...); | ||
| 96 | +} | ||
| 97 | +``` | ||
| 98 | + | ||
| 99 | +with the row-mt twin at `:2114-2115` and the per-tile-row paths at `:1825-1826`, `:1898-1899`. | ||
| 100 | +`MACROBLOCKD.left_context` is an **array**, `ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16]` | ||
| 101 | +(`vp9/common/vp9_blockd.h:192`), so `vp9_zero` clears all 16 slots of all 3 planes; likewise | ||
| 102 | +`left_seg_context[8]` (`blockd.h:195`). | ||
| 103 | + | ||
| 104 | +Why this is the whole story of the observed shape: | ||
| 105 | +* the plane-0 left slot for luma is `ay = ((mi_row << 1) + tx_row) % 32` (`decode.rs:723`), so it | ||
| 106 | + **wraps at mi_row 16** — at the first block of SB row 2, `ay = 0` still holds whatever was | ||
| 107 | + written there during SB row 0 (the frame's first rows), instead of libvpx's zeroed array; | ||
| 108 | +* every earlier SB row is unaffected in the fixtures because the wrap-around slot only collides | ||
| 109 | + with a *two-SB-row-old* value, and the two fixtures never leave a nonzero eob in that slot; | ||
| 110 | +* the desync is a hard bitstream desync, which is why rows 128..239 are not "slightly wrong" but | ||
| 111 | + garbage — after this read the two decoders are reading different syntax. | ||
| 112 | + | ||
| 113 | +**Verification (single-line change, run in a copy — the worktree's `src/` was not touched):** | ||
| 114 | + | ||
| 115 | +``` | ||
| 116 | +cp -r <worktree>/crates /tmp/vp9fix/crates # + trimmed workspace Cargo.toml | ||
| 117 | +# patch: delete the `if sb_row == row_lo {` guard (keep both reset statements) | ||
| 118 | +cd /tmp/vp9fix && SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=3 \ | ||
| 119 | + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9fix \ | ||
| 120 | + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture | ||
| 121 | +-> | ||
| 122 | +frame 0: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 | ||
| 123 | +frame 1: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 | ||
| 124 | +frame 2: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 | ||
| 125 | +RESULT 3 of 3 frames byte-exact (source /tmp/small320.mp4) | ||
| 126 | +``` | ||
| 127 | + | ||
| 128 | +Pre-fix, the same command on the same content (lane worktree, unmodified src): | ||
| 129 | + | ||
| 130 | +``` | ||
| 131 | +frame 0: 320x240 stride 320 uv_stride 160 Y 35527 U 7607 V 7922 | ||
| 132 | +RESULT 0 of 1 frames byte-exact | ||
| 133 | +``` | ||
| 134 | + | ||
| 135 | +The stated per-row shape (0 diffs in rows 0-127, all diffs in rows 128-239) is consistent with | ||
| 136 | +the proven break at `mi_row 16` = y 128; the per-row split itself is task-stated, not re-derived | ||
| 137 | +here (total above is my own measurement on a freshly re-encoded IVF, so the totals differ | ||
| 138 | +slightly from the ones in the task facts — same encode parameters, re-run encode). | ||
| 139 | + | ||
| 140 | +### 2. (ruled out) coefficient-probability-table / header prob-update parse bug | ||
| 141 | +Our first divergent prob (84) equals the frame's own ctx=1 entry for that (tx, type, band) — | ||
| 142 | +recovered from the oracle's table via TXBDUMP+ctx. A table-entry bug would have shown a prob that | ||
| 143 | +no ctx of that row carries. Refuted. | ||
| 144 | + | ||
| 145 | +### 3. (ruled out) tx_size / plane / band misinterpretation at the SB-row boundary | ||
| 146 | +The mode-info bools and the oracle's MODE line for that block agree (bsize 4x4, tx 4x4, skip 0), | ||
| 147 | +and our prob row `[84, 32, 64]` is a single coherent (band 0, ctx 1) row. Refuted. | ||
| 148 | + | ||
| 149 | +### 4. (open, same class — not proven) `mi.left_seg` partition context | ||
| 150 | +The identical guard also held the partition left-context reset (`mi.left_seg`), and libvpx | ||
| 151 | +clears `left_seg_context` at the same two lines. At this block the partition bools *agreed*, so | ||
| 152 | +there is no proven manifestation; the fix above moves both resets out of the guard (that is what | ||
| 153 | +was verified). Anyone tempted to split the fix should keep them together. | ||
| 154 | + | ||
| 155 | +### 5. (side observation, not a root cause) our read budget after the desync | ||
| 156 | +Our trace contains 105 917 bool reads for this frame, the oracle 121 264; after bool #74792 the | ||
| 157 | +two streams are unrelated by definition. The tile reader's `ensure(r.overreads() == 0)` did *not* | ||
| 158 | +trip, i.e. our decoder never ran past the tile end — it re-interpreted the remaining bytes as | ||
| 159 | +different syntax rather than over-reading. | ||
| 160 | + | ||
| 161 | +--- | ||
| 162 | + | ||
| 163 | +## 3. Where the fix goes (exact patch — NOT applied) | ||
| 164 | + | ||
| 165 | +File: `crates/ec-vp9/src/decode.rs`, in the per-tile `for sb_row in (row_lo..row_hi).step_by(SB_MI)` | ||
| 166 | +loop (currently `~:270-276`). Delete the guard, keep both statements: | ||
| 167 | + | ||
| 168 | +```diff | ||
| 169 | + for sb_row in (row_lo..row_hi).step_by(SB_MI) { | ||
| 170 | +- if sb_row == row_lo { | ||
| 171 | +- ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 172 | +- mi.left_seg = [0; 32]; | ||
| 173 | +- } | ||
| 174 | ++ // libvpx: decode_tiles zeroes the left ENTROPY and PARTITION | ||
| 175 | ++ // contexts at the start of EVERY superblock row | ||
| 176 | ++ // (vp9_decodeframe.c:2256-2258), not only the tile's first one: | ||
| 177 | ++ // the left edge of an SB row has no pixels to its left. | ||
| 178 | ++ ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 179 | ++ mi.left_seg = [0; 32]; | ||
| 180 | + for sb_col in (col_lo..col_hi).step_by(SB_MI) { | ||
| 181 | +``` | ||
| 182 | + | ||
| 183 | +Nothing else changes: `above` must keep carrying across SB rows (`ectx[].above.fill(0)` once per | ||
| 184 | +tile at `:268` is correct, and `mi.above_seg.fill(0)` likewise), and the resets stay inside the | ||
| 185 | +per-tile loop, matching libvpx's per-tile left-context lifetime. | ||
| 186 | + | ||
| 187 | +Follow-up taste call (not needed for correctness): libvpx's luma left array is 16 slots indexed | ||
| 188 | +by the row *within* the SB row (`left_context[plane][16]`); ours is 32 slots with a `% 32` index. | ||
| 189 | +Clearing all 32 per SB row is semantically identical to libvpx's clear of all 16 (no slot older | ||
| 190 | +than the current SB row may be read), so the minimal fix is enough. | ||
| 191 | + | ||
| 192 | +--- | ||
| 193 | + | ||
| 194 | +## 4. Reproduction in under a minute | ||
| 195 | + | ||
| 196 | +```bash | ||
| 197 | +# 0. lane env | ||
| 198 | +export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9diag | ||
| 199 | +cd <worktree> # edith_codecs-realdesync @ 3dc719b4 | ||
| 200 | + | ||
| 201 | +# 1. build the all-intra IVF (single tile) + confirm the desync numbers | ||
| 202 | +SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=1 \ | ||
| 203 | + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture | ||
| 204 | +# -> frame 0: 320x240 ... Y 35527 U 7607 V 7922 / RESULT 0 of 1 frames byte-exact | ||
| 205 | +mkdir -p /tmp/ec320 && cp /tmp/ec-vp9-sweep/allintra.ivf /tmp/ec320/ # private copy: never point | ||
| 206 | + # SWEEP_IVF at the harness's own output path | ||
| 207 | + | ||
| 208 | +# 2. oracle bool trace (instrumented libvpx at /tmp/vp9loss/libvpx-src, see §5) | ||
| 209 | +BTRACE=1 MODEDUMP=1 TXBDUMP=1 ONE=1 /tmp/vp9loss/drv /tmp/ec320/allintra.ivf > /tmp/ot_trace_txb.txt 2>&1 | ||
| 210 | + | ||
| 211 | +# 3. our bool trace (EC_VP9_TRACE also prints the CTX/TXB lines used above) | ||
| 212 | +SWEEP_IVF=/tmp/ec320/allintra.ivf SWEEP_N=1 EC_VP9_TRACE=1 \ | ||
| 213 | + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture > /tmp/ot_ours.txt 2>&1 | ||
| 214 | + | ||
| 215 | +# 4. first divergence (python, ~10 lines): zip the "BOR " and "B " lines and find the first | ||
| 216 | +# index where (prob, bit) differ -> index 74792, oracle prob 195 vs ours 84. | ||
| 217 | +# grep -n for the neighbourhood: | ||
| 218 | +sed -n '77370,77380p' /tmp/ot_trace_txb.txt # BOR ... 195 1 at line 77376, MODE line 77375 | ||
| 219 | +sed -n '93145,93160p' /tmp/ot_ours.txt # B ... 84 1, CTX line a=0 l=1 ctx=1 | ||
| 220 | + | ||
| 221 | +# 5. verify the fix in a throwaway copy (worktree src untouched; verified green, see §2.1) | ||
| 222 | +cp -r crates /tmp/vp9fix/crates # + root Cargo.toml with members = the 3 crates ec-vp9 needs | ||
| 223 | +# remove the `if sb_row == row_lo {` guard in /tmp/vp9fix/crates/ec-vp9/src/decode.rs | ||
| 224 | +cd /tmp/vp9fix && SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=3 \ | ||
| 225 | + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9fix \ | ||
| 226 | + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture | ||
| 227 | +# -> RESULT 3 of 3 frames byte-exact | ||
| 228 | +``` | ||
| 229 | + | ||
| 230 | +--- | ||
| 231 | + | ||
| 232 | +## 5. Oracle instrumentation added (all inside `/tmp/vp9loss/libvpx-src`, repuildable) | ||
| 233 | + | ||
| 234 | +`make -j6` in `/tmp/vp9loss/libvpx-src`, then | ||
| 235 | +`gcc -O1 -g -o /tmp/vp9loss/drv drv.c -Ilibvpx-src -Ilibvpx-src/build/make -Llibvpx-src -lvpx -lm -lpthread`. | ||
| 236 | + | ||
| 237 | +| file | change | purpose | | ||
| 238 | +|---|---|---| | ||
| 239 | +| `vpx_dsp/bitreader.h` | `extern const uint8_t *g_btrace_base;` after `vpx_integer.h`; `if (getenv("BTRACE")) fprintf(stderr, "BOR %d %d %d %d\n", (int)(r->buffer - g_btrace_base), r->count, prob, bit);` before `return bit;` in `vpx_read` | header-phase bool trace | | ||
| 240 | +| `vpx_dsp/bitreader.c` | `if (!g_btrace_base) g_btrace_base = buffer;` in `vpx_reader_init` | first-wins frame base | | ||
| 241 | +| `vp9/decoder/vp9_detokenize.c` | the same `BOR` print at both `return 1;` / zero-branch exits of the static `read_bool` (the cached `value/count/range` variant the tokenizer uses) | coefficient-phase bool trace. NOTE: `r->buffer` only advances at refills here, so `pos` is stale by up to 8 bytes in this phase — align on (prob,bit) only | | ||
| 242 | +| `vp9/decoder/vp9_decodeframe.c` | `const uint8_t *g_btrace_base;` + `g_btrace_base = data;` in `setup_token_decoder`; `g_btrace_base = NULL;` at the top of `vp9_decode_frame`; `tpos=%d` appended to the existing `MODEDUMP` line | base + per-block reader offset | | ||
| 243 | + | ||
| 244 | +Existing oracle hooks used as-is: `MODEDUMP` (per-block bsize/tx/mode/uv_mode/skip/segment, printed | ||
| 245 | +at the end of `vp9_read_mode_info` in `decode_block`), `TXBDUMP` (per transform block | ||
| 246 | +plane/x/y/tx/ctx/eob/dequant), `PREDDUMP`, `LFTRACE`/`LFMASKS`. | ||
| 247 | + | ||
| 248 | +Reading pitfalls confirmed (in addition to `lanes/vp9kf.report.md` §"Trace-reading pitfalls"): | ||
| 249 | +* a `TXB`/`MODE` marker line prints **after** the bools that preceded it — a transform block's | ||
| 250 | + reads are the range `[previous marker's bool index, this marker's bool index)`, not the range | ||
| 251 | + starting at the marker. Getting this backwards (as the first pass of this analysis did) makes | ||
| 252 | + the same `(plane,tx,ctx)` appear to carry several different EOB probs; | ||
| 253 | +* the oracle prints `MODE` from `decode_block`, i.e. only on the non-row-mt path (the path a | ||
| 254 | + 1-thread `drv` takes); `parse_block`/`reconstruct_block` (row-mt) have no such print; | ||
| 255 | +* `SWEEP_IVF` must not point at `/tmp/ec-vp9-sweep/allintra.ivf` — the harness copies source to | ||
| 256 | + destination, and source == destination truncates the file ("ffmpeg decode of the IVF failed"). | ||
| 257 | + | ||
| 258 | +--- | ||
| 259 | + | ||
| 260 | +## 6. Evidence index (command -> observed) | ||
| 261 | + | ||
| 262 | +| # | command | observed | | ||
| 263 | +|---|---|---| | ||
| 264 | +| e1 | `SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=1` `cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture` | `frame 0: 320x240 stride 320 uv_stride 160 Y 35527 U 7607 V 7922` / `RESULT 0 of 1 frames byte-exact` | | ||
| 265 | +| e2 | `BTRACE=1 MODEDUMP=1 TXBDUMP=1 ONE=1 /tmp/vp9loss/drv /tmp/ec320/allintra.ivf` | `bor=121264 txb=3579 mode=996` lines | | ||
| 266 | +| e3 | `SWEEP_IVF=/tmp/ec320/allintra.ivf SWEEP_N=1 EC_VP9_TRACE=1 cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture` | `blines=105917`, same Y/U/V numbers as e1 | | ||
| 267 | +| e4 | index-aligned diff of the two bool streams | first mismatch at index 74792: `O pos=7113 prob=195 bit=1` / `M pos=7114 prob=84 bit=1`; 74792 reads agree before it | | ||
| 268 | +| e5 | last markers before the divergence in `/tmp/ot_trace_txb.txt` | `MODE 0 16 0 0 0 2 0 0 0 tpos=7113` @ bool index 74792 (preceded by `MODE 0 15 37 3 1 0 0 1 0`, `MODE 0 14 38 6 2 9 0 0 0` + its 3 `TXB` lines) | | ||
| 269 | +| e6 | EOB-node prob per ctx recovered from e2's BTRACE+TXBDUMP | `(0,0,0)->195 (0,0,1)->84 (0,0,2)->18` | | ||
| 270 | +| e7 | our `CTX` trace at bool index 74792 | `CTX p=0 prob=[84, 32, 64] ax=0 ay=0 a=0 l=1 ctx=1` + `TXB p=0 x=0 y=128 tx=0 a=0 l=1` | | ||
| 271 | +| e8 | `sed -n '270,276p' crates/ec-vp9/src/decode.rs` | `if sb_row == row_lo { ectx...left = [0; 32]; mi.left_seg = [0; 32]; }` | | ||
| 272 | +| e9 | `sed -n '2256,2258p' /tmp/vp9loss/libvpx-src/vp9/decoder/vp9_decodeframe.c` | `for (mi_row = ...; mi_row += MI_BLOCK_SIZE) { vp9_zero(tile_data->xd.left_context); vp9_zero(tile_data->xd.left_seg_context);` | | ||
| 273 | +| e10 | `grep -n left_context /tmp/vp9loss/libvpx-src/vp9/common/vp9_blockd.h` | `192: ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];` (array ⇒ `vp9_zero` clears it) | | ||
| 274 | +| e11 | fix applied to `/tmp/vp9fix` copy, `SWEEP_N=3` sweep | `frame 0/1/2: Y 0 U 0 V 0` / `RESULT 3 of 3 frames byte-exact` | | ||
| 275 | + | ||
| 276 | +No `BLOCKED:` line — the first divergent decision is a single syntax element, the root cause is | ||
| 277 | +proven and the fix is verified end-to-end. Remaining (optional) follow-up: a regression test that | ||
| 278 | +fails pre-fix, i.e. a witness for the per-SB-row left-context reset, and re-running the 1080p case | ||
| 279 | +(the task's other repro) after the fix. | ||
lanes/vp9kf.report.mdmodified+98 -1
| @@ -514,7 +514,9 @@ Evidence that the walk is now right: | |||
| 514 | - **The remaining real-content divergence is tile-independent**: the same source encoded | 514 | - **The remaining real-content divergence is tile-independent**: the same source encoded |
| 515 | `-tile-columns 0 -tile-rows 0` produces the identical `tile bool decoder desync` (as do | 515 | `-tile-columns 0 -tile-rows 0` produces the identical `tile bool decoder desync` (as do |
| 516 | single-tile 1080p `testsrc2` and a single-tile 960x540 gradient). The desync item below | 516 | single-tile 1080p `testsrc2` and a single-tile 960x540 gradient). The desync item below |
| 517 | - stays open and is a sibling lane's to diagnose. | 517 | + stays open and is a sibling lane's to diagnose. **CLOSED (this batch)** — see |
| 518 | + `## DESYNC CLOSED` at the end of this report and the companion | ||
| 519 | + `lanes/realdesync.report.md`. | ||
| 518 | 520 | ||
| 519 | ### Next lane charter seed (in priority order) | 521 | ### Next lane charter seed (in priority order) |
| 520 | 522 | ||
| @@ -556,3 +558,98 @@ guard restored the test fails ("...was not filtered"); without it, it passes. li | |||
| 556 | 9/9; keyframe_exact still 4/4; the real-content sweep numbers are unchanged (that content | 558 | 9/9; keyframe_exact still 4/4; the real-content sweep numbers are unchanged (that content |
| 557 | does not reach the shape). | 559 | does not reach the shape). |
| 558 | 560 | ||
| 561 | + | ||
| 562 | +## DESYNC CLOSED (this batch) — real-content desync root-caused and fixed | ||
| 563 | + | ||
| 564 | +The open item above (`## OPEN DEFECT LANE`, "real content desyncs mid-frame") is closed. Full | ||
| 565 | +first-divergent-decision evidence lives in the companion lane report | ||
| 566 | +`lanes/realdesync.report.md` (copied verbatim into this checkout). | ||
| 567 | + | ||
| 568 | +### Root cause (proven by bool-trace diff against the instrumented libvpx oracle) | ||
| 569 | + | ||
| 570 | +`crates/ec-vp9/src/decode.rs` reset the left entropy + partition contexts only at the TILE's | ||
| 571 | +first superblock row (`if sb_row == row_lo { ectx[].left = [0; 32]; mi.left_seg = [0; 32]; }`). | ||
| 572 | +libvpx zeroes them at the start of EVERY superblock row: | ||
| 573 | + | ||
| 574 | +- `vp9/decoder/vp9_decodeframe.c:2256-2258` (plain `decode_tiles`, the path a 1-thread `drv` | ||
| 575 | + takes), with the row-mt twin at `:2114-2115` and the per-tile-row twins at `:1825-1826` / | ||
| 576 | + `:1898-1899`; | ||
| 577 | +- `MACROBLOCKD.left_context` is `ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16]` | ||
| 578 | + (`vp9/common/vp9_blockd.h:192`), so `vp9_zero` clears all 16 slots of all 3 planes; | ||
| 579 | + `left_seg_context[8]` (`blockd.h:195`) likewise. | ||
| 580 | + | ||
| 581 | +Our plane-0 left index wraps at mi_row 16 (`ay = ((mi_row << 1) + tx_row) % 32`), so the first | ||
| 582 | +block of SB row 2 read a slot left over from SB row 0: coefficient EOB context 1 instead of 0, | ||
| 583 | +first divergence at bool #74792 (oracle prob 195 vs ours 84), hard desync from there. | ||
| 584 | + | ||
| 585 | +`above_context` / `above_seg_context` need no such change: libvpx clears them per FRAME | ||
| 586 | +(`memset` at `:2076-2080`) but each tile's `xd` points into a disjoint column range | ||
| 587 | +(`vp9_init_macroblockd`, `vp9_onyxc_int.h:381-393`), which is exactly what our per-tile fills | ||
| 588 | +at `decode.rs:269-270` do. | ||
| 589 | + | ||
| 590 | +### Second defect found by the 1080p leg of the acceptance (same family: extent gated on the wrong boundary) | ||
| 591 | + | ||
| 592 | +`crates/ec-vp9/src/loopfilter.rs`, 4:2:0 chroma passes: the row-group guard was | ||
| 593 | +`if y8 + 8 > uv_h { continue; }` (visible chroma height). libvpx's loop condition is on MI rows | ||
| 594 | +— `vp9_filter_block_plane_ss11`, `vp9/common/vp9_loopfilter.c:1400` (`r += 4` while | ||
| 595 | +`mi_row + r < mi_rows`, vertical) and `:1431` (`r += 2`, horizontal) — so a group whose FIRST MI | ||
| 596 | +row is inside the frame IS filtered even when its 8 chroma rows overhang the visible height into | ||
| 597 | +the padded buffer. `vp9_adjust_mask` keeps those mask bits for odd last-SB-row remnants | ||
| 598 | +(`rows == 7` -> `mask_uv == 0xffff`). | ||
| 599 | + | ||
| 600 | +Symptom: 1080p (mi_rows 135) had **Y byte-exact but U/V off by +/-1** in the last SB row | ||
| 601 | +(chroma rows 536-539) — the vertical edges of group 3 were never filtered. 320x240 (mi_rows 30, | ||
| 602 | +`rows == 6` -> group 3's mask is cleared) never showed it, which is why no fixture caught it. | ||
| 603 | + | ||
| 604 | +Fix: gate both chroma passes on the MI-row condition (`sbr + 4*(rg>>1) < mi_rows` vertical, | ||
| 605 | +`sbr + 2*rg < mi_rows` horizontal) and read the level through a new bounds-safe | ||
| 606 | +`LfGrids::level_at(r, c)` (0 past the frame edge: `vp9_adjust_mask` guarantees the mask bit for | ||
| 607 | +such a cell is cleared, so the value only has to be safe, not meaningful — and it also removes | ||
| 608 | +a latent OOB `level8` read for widths whose last SB column holds <= 6 mi cols). | ||
| 609 | + | ||
| 610 | +### Fixes applied (this batch, uncommitted at time of writing) | ||
| 611 | + | ||
| 612 | +- `crates/ec-vp9/src/decode.rs`: guard deleted, both resets run at every SB row (`:271-285`). | ||
| 613 | +- `crates/ec-vp9/src/loopfilter.rs`: chroma row-group guards + `level_at` (above). | ||
| 614 | + | ||
| 615 | +### Sweep evidence (before -> after; same commands, same IVF files) | ||
| 616 | + | ||
| 617 | +| # | command | before (HEAD `d068da4f`) | after | | ||
| 618 | +|---|---|---|---| | ||
| 619 | +| 1 | `SWEEP_TILE0=1 SWEEP_SRC=<real 320x240 clip>.mp4 cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture` | `Y 35527/34764/35551 U 7607.. V 7656..` -> `RESULT 0 of 3 frames byte-exact` | `Y 0 U 0 V 0` x3 -> `RESULT 3 of 3 frames byte-exact` | | ||
| 620 | +| 2 | `SWEEP_IVF=/tmp/t1080-single.ivf` (real 1920x1080 clip, `-g 1 -tile-columns 0 -tile-rows 0`) | `Y 1809097/1810938/1808592 U 412982.. V 403183..` -> `RESULT 0 of 3` | `Y 0 U 0 V 0` x3 -> `RESULT 3 of 3 frames byte-exact` | | ||
| 621 | +| 3 | `SWEEP_IVF=/tmp/t1080.ivf` (same clip, 4 tile columns, `cols_log2=2`) | `DECODE REFUSED/ERR vp9: tile bool decoder desync` x3 -> `RESULT 0 of 3` | `Y 0 U 0 V 0` x3 -> `RESULT 3 of 3 frames byte-exact` | | ||
| 622 | + | ||
| 623 | +Acceptance 2 was measured with fix 1 only (`U 77/70/68 V 9/12/9`, `RESULT 0 of 3`) — that is | ||
| 624 | +where defect 2 surfaced; both fixes together give `0/0/0`. | ||
| 625 | + | ||
| 626 | +`cargo test -p ec-vp9` -> every binary green (lib 9/9, `keyframe_exact` 4/4, all scratch | ||
| 627 | +harnesses); `cargo check -p ec-vp9` -> the same 5 lib warnings, no new ones. No commit made by | ||
| 628 | +this lane (the orchestrator commits). | ||
| 629 | + | ||
| 630 | +## OPEN (found by verification, 2026-09-16) — the tile-ROW path is not decoded correctly | ||
| 631 | + | ||
| 632 | +Verification of the DESYNC CLOSED batch (above) confirmed the left-context fix and the chroma | ||
| 633 | +row-group fix, and then probed the un-exercised remainder: **`log2_tile_rows > 0` is not | ||
| 634 | +decoded correctly by the port at all**, and the batch's two fixes do not touch that class. | ||
| 635 | +Three pre-existing defects were isolated with direct probes (all in the tile-row path): | ||
| 636 | + | ||
| 637 | +1. `crates/ec-vp9/src/modes.rs:186-190` reads tx probs from the `TX_*` const defaults instead | ||
| 638 | + of the frame-updated `FrameContext` tables. The header sets `p8x8[0][0] = 31` for the | ||
| 639 | + probed frame; the port reads 100 -> prob mismatch at read #3203 of its tile-row frame. | ||
| 640 | +2. `crates/ec-vp9/src/modes.rs:177-185` omits libvpx's `if (!has_above) above_ctx = left_ctx` | ||
| 641 | + fallback (oracle ctx 0 / prob 20 vs port ctx 1 / prob 8). | ||
| 642 | +3. `MiState::above_mi` and `crates/ec-vp9/src/decode.rs:701` gate above-pixel and | ||
| 643 | + above-context availability on the tile-row start where libvpx gates on the FRAME | ||
| 644 | + (`mi_row != 0`). With (1) and (2) fixed, pixel divergence starts exactly at row 512 — the | ||
| 645 | + first tile-row boundary. | ||
| 646 | + | ||
| 647 | +Related, and now narrowed: `decode.rs:269-270` zeroes `ectx[].above` / `mi.above_seg` once per | ||
| 648 | +tile. That is equivalent to libvpx's frame-scoped clear (`vp9_decodeframe.c:2076-2080`) only | ||
| 649 | +for `log2_tile_rows == 0` (disjoint column ranges); with tile rows, the first SB row of tile | ||
| 650 | +row k > 0 may legitimately read above state written by tile row k - 1, which the per-tile | ||
| 651 | +fill discards. The comment in `decode.rs` states this explicitly. | ||
| 652 | + | ||
| 653 | +**Status: the tile-row class is NOT closed.** No available fixture and no sweep exercises | ||
| 654 | +`log2_tile_rows > 0`, so none of the three defects is covered by a gate; they were found with | ||
| 655 | +purpose-built probes, not by the crate's tests. | ||