diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index bdf3ae32..8f5b0f2f 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -269,10 +269,28 @@ impl Decoder { ectx.iter_mut().for_each(|p| p.above.fill(0)); mi.above_seg.fill(0); for sb_row in (row_lo..row_hi).step_by(SB_MI) { - if sb_row == row_lo { - ectx.iter_mut().for_each(|p| p.left = [0; 32]); - mi.left_seg = [0; 32]; - } + // libvpx zeroes the left ENTROPY and PARTITION contexts at + // the start of EVERY superblock row, not just the tile's + // first one (vp9_decodeframe.c:2256-2258 plain + // `decode_tiles`; the row-mt twin is :2114-2115 and the + // per-tile-row twins :1825-1826 / :1898-1899): the left + // edge of an SB row has no pixels to its left. Our luma + // left index wraps at mi_row 16, so a stale value from two + // SB rows earlier would otherwise be read here. + // `above_context`/`above_seg_context` are frame-scoped in + // libvpx (`memset` at :2076-2080, before the tile-row + // loop) and indexed by ABSOLUTE mi_col (`set_skip_context`, + // vp9_onyxc_int.h:409-412), so zeroing them once per tile + // is equivalent only for `log2_tile_rows == 0`, where the + // disjoint-column argument holds (each tile's xd points + // into its own column range: vp9_init_macroblockd, + // vp9_onyxc_int.h:381-393). With tile ROWS the first SB row + // of tile row k > 0 may legitimately read above state + // written by tile row k - 1, which the fills at :269-270 + // discard: the tile-row path is NOT equivalent and is not + // handled here. + ectx.iter_mut().for_each(|p| p.left = [0; 32]); + mi.left_seg = [0; 32]; for sb_col in (col_lo..col_hi).step_by(SB_MI) { self.decode_partition( &mut r, diff --git a/crates/ec-vp9/src/loopfilter.rs b/crates/ec-vp9/src/loopfilter.rs index a75bc9f9..45a3336e 100644 --- a/crates/ec-vp9/src/loopfilter.rs +++ b/crates/ec-vp9/src/loopfilter.rs @@ -499,6 +499,19 @@ impl LfGrids { (left, above, int4) } + /// Filter level of the 8x8 block at MI `(r, c)`. The chroma passes can + /// ask for a cell whose MI row/col sits past the frame edge: libvpx reads + /// its (zero-filled) `lfl_y` there, and `vp9_adjust_mask` guarantees the + /// mask bit for such a cell is already cleared, so the value only has to + /// be safe, not meaningful. `0` also means "no filter", which keeps the + /// edge emission identical. + fn level_at(&self, r: usize, c: usize) -> u8 { + if r >= self.mi_rows || c >= self.mi_cols { + return 0; + } + self.level8[r * self.mi_cols + c] + } + /// Filter the frame's three planes in place. `y_w/h`, `uv_w/h` are /// the coded (aligned) plane sizes. pub(crate) fn filter_frame( @@ -730,14 +743,24 @@ impl LfGrids { // libvpx runs the whole vertical pass before the // horizontal one; they overlap, so order matters. for rg in 0..4usize { - let y8 = sbr * 4 + 8 * rg; - if y8 + 8 > uv_h { + // vp9_filter_block_plane_ss11 iterates MI rows + // `r += 4` while `mi_row + r < mi_rows` + // (vp9_loopfilter.c:1400), so the row GROUP is + // processed whenever its first MI row is inside the + // frame — even when its 8 chroma rows overhang the + // visible height into the padded buffer. Gating on + // `y8 + 8 <= uv_h` instead skipped the whole group and + // lost the visible bottom rows (the 1080p case: + // mi_rows 135, last group = chroma rows 536..543, + // 536..539 visible). + if sbr + 4 * (rg >> 1) >= self.mi_rows { continue; } + let y8 = sbr * 4 + 8 * rg; for cg in 0..4usize { let x8 = sbc * 4 + 8 * cg; let bit = 1u16 << (rg * 4 + cg); - let lvl = self.level8[(sbr + 2 * rg) * self.mi_cols + sbc + 2 * cg]; + let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg); if lvl == 0 { continue; } @@ -787,14 +810,18 @@ impl LfGrids { } } for rg in 0..4usize { - let y8 = sbr * 4 + 8 * rg; - if y8 + 8 > uv_h { + // Horizontal pass: `r += 2` while `mi_row + r < + // mi_rows` (vp9_loopfilter.c:1431) — one row group per + // iteration, same MI-row-gated boundary as the + // vertical pass above. + if sbr + 2 * rg >= self.mi_rows { continue; } + let y8 = sbr * 4 + 8 * rg; for cg in 0..4usize { let x8 = sbc * 4 + 8 * cg; let bit = 1u16 << (rg * 4 + cg); - let lvl = self.level8[(sbr + 2 * rg) * self.mi_cols + sbc + 2 * cg]; + let lvl = self.level_at(sbr + 2 * rg, sbc + 2 * cg); if lvl == 0 { continue; } diff --git a/lanes/realdesync.report.md b/lanes/realdesync.report.md new file mode 100644 index 00000000..48436ac7 --- /dev/null +++ b/lanes/realdesync.report.md @@ -0,0 +1,279 @@ +# lane realdesync (Vp9RealDiag) — FIRST divergent decision on real content + +Worktree `/home/user/Documents/Code/Rust/edith_codecs-realdesync` @ `3dc719b4`, detached. +**No file under `crates/ec-vp9/src/` was edited.** Everything below is read-only diagnosis plus +two throwaway scratch locations: the oracle sources `/tmp/vp9loss/libvpx-src` (instrumented, +see §5) and a full copy of the crate at `/tmp/vp9fix` used to verify the fix in §4. + +Content: a real 320x240 screen recording, re-encoded all-intra VP9 (`-g 1 -deadline good +-cpu-used 4 -crf 32 -tile-columns 0 -tile-rows 0`, 3 frames), IVF at `/tmp/ec320/allintra.ivf`. + +--- + +## 1. The first divergent decision + +**Block:** `mi_row=16, mi_col=0, bsize=BLOCK_4X4 (0), tx_size=TX_4X4 (0), mode=2, uv_mode=0, +skip=0, segment=0` — the **first block of superblock row 2**; its luma TX_4X4 transform block +sits at pixel `(x=0, y=128)`. + +**First syntax element whose value differs:** the first bool of that transform block's +coefficient syntax — the EOB branch byte read with +`coef_probs[TX_4X4][PLANE_TYPE_Y][band 0][ctx][EOB_CONTEXT_NODE]` +(libvpx `vp9_detokenize.c` `decode_coefs`: `band = *band_translate++; prob = coef_probs[band][ctx]; +if (!read_bool(r, prob[EOB_CONTEXT_NODE], ...)) break;`). + +| | prob | bit | txb_ctx | trace | +|---|---|---|---|---| +| oracle (libvpx `drv`) | **195** | 1 | **0** | `/tmp/ot_trace_txb.txt` line **77376** (`BOR 7113 195 1`, bool #74792) | +| ours (`ec-vp9`) | **84** | 1 | **1** | `/tmp/ot_ours.txt` line **93153** (`B 7114 84 1`, bool #74792) | + +Everything before it agrees: the first **74 792** `read_bool` calls of the frame are identical in +probability **and** in decoded bit between the two decoders (index-aligned, because both streams +agree by construction to that point), including the 26 bools of that block's own mode info +(partition tree + 4x4 intra modes + tx flag) which immediately precede it. + +The prob⇒ctx mapping is not assumed; it is **recovered from the oracle's own trace** (TXBDUMP +gives the ctx of every transform block, BTRACE gives every bool): for `(plane 0, TX_4X4)` the +EOB-node prob is single-valued per ctx — + +``` +(plane,tx,ctx) -> EOB-node prob (recovered over the whole agreeing prefix) +(0,0,0) -> 195 (0,0,1) -> 84 (0,0,2) -> 18 +(1,0,0) -> 214 (1,0,1) -> 132 (1,0,2) -> 42 # U +(2,0,*) -> same as U # V +``` + +So the oracle asked its question with **ctx=0** and we asked the same question with **ctx=1**; +libvpx ended up decoding `eob=4` for that TU. + +Our own decoder's `CTX` trace hook prints the reason directly (it is exactly the code at +`decode.rs:730-738`): + +``` +[reads= 74792] MODE row=16 col=0 bsize=0 skip=0 tx=0 y=2 uv=0 +[reads= 74792] CTX p=0 prob=[84, 32, 64] ax=0 ay=0 a=0 l=1 ctx=1 +[reads= 74792] TXB p=0 x=0 y=128 tx=0 a=0 l=1 deq=38,44 +``` + +`a=0` (above eob context) is right; **`l=1` (left eob context) is stale** — the left slot must be +0 at the first column of a superblock row, and it is not. + +**Ruled out** (all three would have produced the same "first divergence at a read" shape): +* partition / bsize / tx_size / intra-mode divergences — the 26 mode-info bools and the oracle's + `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`; +* the frame's coefficient probability table (`coef_probs` after the header's diff updates): our + prob **84** *is* the frame's ctx=1 entry, i.e. the table is right, only the ctx index is wrong; +* a plane/tx-size/band misread: the prob rows our decoder printed (`[84, 32, 64]`) are a single + coherent (band 0, ctx 1) row of the TX_4X4/Y table. +* decoded-value drift in the arithmetic decoder: the bool *values* agree; only the probability + differs, so no reader-state desync preceded this point. + +--- + +## 2. Ranked candidate root causes + +### 1. (PROVEN — and fixed+verified in a throwaway copy) missing per-superblock-row reset of the left entropy contexts + +`crates/ec-vp9/src/decode.rs`, tile loop (`~:270-276`): + +```rust +for sb_row in (row_lo..row_hi).step_by(SB_MI) { + if sb_row == row_lo { // <-- only the TILE'S FIRST SB row + ectx.iter_mut().for_each(|p| p.left = [0; 32]); + mi.left_seg = [0; 32]; + } + for sb_col in (col_lo..col_hi).step_by(SB_MI) { ... } +``` + +libvpx clears the same state at the start of **every** superblock row — `vp9_decodeframe.c:2256-2258` +(the plain `decode_tiles` path, which is what a 1-thread `drv` takes): + +```c +for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; mi_row += MI_BLOCK_SIZE) { + vp9_zero(tile_data->xd.left_context); + vp9_zero(tile_data->xd.left_seg_context); + for (mi_col = ...) decode_partition(...); +} +``` + +with the row-mt twin at `:2114-2115` and the per-tile-row paths at `:1825-1826`, `:1898-1899`. +`MACROBLOCKD.left_context` is an **array**, `ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16]` +(`vp9/common/vp9_blockd.h:192`), so `vp9_zero` clears all 16 slots of all 3 planes; likewise +`left_seg_context[8]` (`blockd.h:195`). + +Why this is the whole story of the observed shape: +* the plane-0 left slot for luma is `ay = ((mi_row << 1) + tx_row) % 32` (`decode.rs:723`), so it + **wraps at mi_row 16** — at the first block of SB row 2, `ay = 0` still holds whatever was + written there during SB row 0 (the frame's first rows), instead of libvpx's zeroed array; +* every earlier SB row is unaffected in the fixtures because the wrap-around slot only collides + with a *two-SB-row-old* value, and the two fixtures never leave a nonzero eob in that slot; +* the desync is a hard bitstream desync, which is why rows 128..239 are not "slightly wrong" but + garbage — after this read the two decoders are reading different syntax. + +**Verification (single-line change, run in a copy — the worktree's `src/` was not touched):** + +``` +cp -r /crates /tmp/vp9fix/crates # + trimmed workspace Cargo.toml +# patch: delete the `if sb_row == row_lo {` guard (keep both reset statements) +cd /tmp/vp9fix && SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=3 \ + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9fix \ + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture +-> +frame 0: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 +frame 1: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 +frame 2: 320x240 stride 320 uv_stride 160 Y 0 U 0 V 0 +RESULT 3 of 3 frames byte-exact (source /tmp/small320.mp4) +``` + +Pre-fix, the same command on the same content (lane worktree, unmodified src): + +``` +frame 0: 320x240 stride 320 uv_stride 160 Y 35527 U 7607 V 7922 +RESULT 0 of 1 frames byte-exact +``` + +The stated per-row shape (0 diffs in rows 0-127, all diffs in rows 128-239) is consistent with +the proven break at `mi_row 16` = y 128; the per-row split itself is task-stated, not re-derived +here (total above is my own measurement on a freshly re-encoded IVF, so the totals differ +slightly from the ones in the task facts — same encode parameters, re-run encode). + +### 2. (ruled out) coefficient-probability-table / header prob-update parse bug +Our first divergent prob (84) equals the frame's own ctx=1 entry for that (tx, type, band) — +recovered from the oracle's table via TXBDUMP+ctx. A table-entry bug would have shown a prob that +no ctx of that row carries. Refuted. + +### 3. (ruled out) tx_size / plane / band misinterpretation at the SB-row boundary +The mode-info bools and the oracle's MODE line for that block agree (bsize 4x4, tx 4x4, skip 0), +and our prob row `[84, 32, 64]` is a single coherent (band 0, ctx 1) row. Refuted. + +### 4. (open, same class — not proven) `mi.left_seg` partition context +The identical guard also held the partition left-context reset (`mi.left_seg`), and libvpx +clears `left_seg_context` at the same two lines. At this block the partition bools *agreed*, so +there is no proven manifestation; the fix above moves both resets out of the guard (that is what +was verified). Anyone tempted to split the fix should keep them together. + +### 5. (side observation, not a root cause) our read budget after the desync +Our trace contains 105 917 bool reads for this frame, the oracle 121 264; after bool #74792 the +two streams are unrelated by definition. The tile reader's `ensure(r.overreads() == 0)` did *not* +trip, i.e. our decoder never ran past the tile end — it re-interpreted the remaining bytes as +different syntax rather than over-reading. + +--- + +## 3. Where the fix goes (exact patch — NOT applied) + +File: `crates/ec-vp9/src/decode.rs`, in the per-tile `for sb_row in (row_lo..row_hi).step_by(SB_MI)` +loop (currently `~:270-276`). Delete the guard, keep both statements: + +```diff + for sb_row in (row_lo..row_hi).step_by(SB_MI) { +- if sb_row == row_lo { +- ectx.iter_mut().for_each(|p| p.left = [0; 32]); +- mi.left_seg = [0; 32]; +- } ++ // libvpx: decode_tiles zeroes the left ENTROPY and PARTITION ++ // contexts at the start of EVERY superblock row ++ // (vp9_decodeframe.c:2256-2258), not only the tile's first one: ++ // the left edge of an SB row has no pixels to its left. ++ ectx.iter_mut().for_each(|p| p.left = [0; 32]); ++ mi.left_seg = [0; 32]; + for sb_col in (col_lo..col_hi).step_by(SB_MI) { +``` + +Nothing else changes: `above` must keep carrying across SB rows (`ectx[].above.fill(0)` once per +tile at `:268` is correct, and `mi.above_seg.fill(0)` likewise), and the resets stay inside the +per-tile loop, matching libvpx's per-tile left-context lifetime. + +Follow-up taste call (not needed for correctness): libvpx's luma left array is 16 slots indexed +by the row *within* the SB row (`left_context[plane][16]`); ours is 32 slots with a `% 32` index. +Clearing all 32 per SB row is semantically identical to libvpx's clear of all 16 (no slot older +than the current SB row may be read), so the minimal fix is enough. + +--- + +## 4. Reproduction in under a minute + +```bash +# 0. lane env +export CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9diag +cd # edith_codecs-realdesync @ 3dc719b4 + +# 1. build the all-intra IVF (single tile) + confirm the desync numbers +SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=1 \ + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture +# -> frame 0: 320x240 ... Y 35527 U 7607 V 7922 / RESULT 0 of 1 frames byte-exact +mkdir -p /tmp/ec320 && cp /tmp/ec-vp9-sweep/allintra.ivf /tmp/ec320/ # private copy: never point + # SWEEP_IVF at the harness's own output path + +# 2. oracle bool trace (instrumented libvpx at /tmp/vp9loss/libvpx-src, see §5) +BTRACE=1 MODEDUMP=1 TXBDUMP=1 ONE=1 /tmp/vp9loss/drv /tmp/ec320/allintra.ivf > /tmp/ot_trace_txb.txt 2>&1 + +# 3. our bool trace (EC_VP9_TRACE also prints the CTX/TXB lines used above) +SWEEP_IVF=/tmp/ec320/allintra.ivf SWEEP_N=1 EC_VP9_TRACE=1 \ + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture > /tmp/ot_ours.txt 2>&1 + +# 4. first divergence (python, ~10 lines): zip the "BOR " and "B " lines and find the first +# index where (prob, bit) differ -> index 74792, oracle prob 195 vs ours 84. +# grep -n for the neighbourhood: +sed -n '77370,77380p' /tmp/ot_trace_txb.txt # BOR ... 195 1 at line 77376, MODE line 77375 +sed -n '93145,93160p' /tmp/ot_ours.txt # B ... 84 1, CTX line a=0 l=1 ctx=1 + +# 5. verify the fix in a throwaway copy (worktree src untouched; verified green, see §2.1) +cp -r crates /tmp/vp9fix/crates # + root Cargo.toml with members = the 3 crates ec-vp9 needs +# remove the `if sb_row == row_lo {` guard in /tmp/vp9fix/crates/ec-vp9/src/decode.rs +cd /tmp/vp9fix && SWEEP_TILE0=1 SWEEP_SRC=/tmp/small320.mp4 SWEEP_N=3 \ + CARGO_TARGET_DIR=$HOME/.cache/cargo-target-vp9fix \ + cargo test -p ec-vp9 --test scratch_realsweep -- --nocapture +# -> RESULT 3 of 3 frames byte-exact +``` + +--- + +## 5. Oracle instrumentation added (all inside `/tmp/vp9loss/libvpx-src`, repuildable) + +`make -j6` in `/tmp/vp9loss/libvpx-src`, then +`gcc -O1 -g -o /tmp/vp9loss/drv drv.c -Ilibvpx-src -Ilibvpx-src/build/make -Llibvpx-src -lvpx -lm -lpthread`. + +| file | change | purpose | +|---|---|---| +| `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 | +| `vpx_dsp/bitreader.c` | `if (!g_btrace_base) g_btrace_base = buffer;` in `vpx_reader_init` | first-wins frame base | +| `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 | +| `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 | + +Existing oracle hooks used as-is: `MODEDUMP` (per-block bsize/tx/mode/uv_mode/skip/segment, printed +at the end of `vp9_read_mode_info` in `decode_block`), `TXBDUMP` (per transform block +plane/x/y/tx/ctx/eob/dequant), `PREDDUMP`, `LFTRACE`/`LFMASKS`. + +Reading pitfalls confirmed (in addition to `lanes/vp9kf.report.md` §"Trace-reading pitfalls"): +* a `TXB`/`MODE` marker line prints **after** the bools that preceded it — a transform block's + reads are the range `[previous marker's bool index, this marker's bool index)`, not the range + starting at the marker. Getting this backwards (as the first pass of this analysis did) makes + the same `(plane,tx,ctx)` appear to carry several different EOB probs; +* the oracle prints `MODE` from `decode_block`, i.e. only on the non-row-mt path (the path a + 1-thread `drv` takes); `parse_block`/`reconstruct_block` (row-mt) have no such print; +* `SWEEP_IVF` must not point at `/tmp/ec-vp9-sweep/allintra.ivf` — the harness copies source to + destination, and source == destination truncates the file ("ffmpeg decode of the IVF failed"). + +--- + +## 6. Evidence index (command -> observed) + +| # | command | observed | +|---|---|---| +| 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` | +| e2 | `BTRACE=1 MODEDUMP=1 TXBDUMP=1 ONE=1 /tmp/vp9loss/drv /tmp/ec320/allintra.ivf` | `bor=121264 txb=3579 mode=996` lines | +| 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 | +| 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 | +| 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) | +| e6 | EOB-node prob per ctx recovered from e2's BTRACE+TXBDUMP | `(0,0,0)->195 (0,0,1)->84 (0,0,2)->18` | +| 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` | +| 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]; }` | +| 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);` | +| 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) | +| 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` | + +No `BLOCKED:` line — the first divergent decision is a single syntax element, the root cause is +proven and the fix is verified end-to-end. Remaining (optional) follow-up: a regression test that +fails pre-fix, i.e. a witness for the per-SB-row left-context reset, and re-running the 1080p case +(the task's other repro) after the fix. diff --git a/lanes/vp9kf.report.md b/lanes/vp9kf.report.md index 0dd39caa..58195605 100644 --- a/lanes/vp9kf.report.md +++ b/lanes/vp9kf.report.md @@ -514,7 +514,9 @@ Evidence that the walk is now right: - **The remaining real-content divergence is tile-independent**: the same source encoded `-tile-columns 0 -tile-rows 0` produces the identical `tile bool decoder desync` (as do single-tile 1080p `testsrc2` and a single-tile 960x540 gradient). The desync item below - stays open and is a sibling lane's to diagnose. + stays open and is a sibling lane's to diagnose. **CLOSED (this batch)** — see + `## DESYNC CLOSED` at the end of this report and the companion + `lanes/realdesync.report.md`. ### Next lane charter seed (in priority order) @@ -556,3 +558,98 @@ guard restored the test fails ("...was not filtered"); without it, it passes. li 9/9; keyframe_exact still 4/4; the real-content sweep numbers are unchanged (that content does not reach the shape). + +## DESYNC CLOSED (this batch) — real-content desync root-caused and fixed + +The open item above (`## OPEN DEFECT LANE`, "real content desyncs mid-frame") is closed. Full +first-divergent-decision evidence lives in the companion lane report +`lanes/realdesync.report.md` (copied verbatim into this checkout). + +### Root cause (proven by bool-trace diff against the instrumented libvpx oracle) + +`crates/ec-vp9/src/decode.rs` reset the left entropy + partition contexts only at the TILE's +first superblock row (`if sb_row == row_lo { ectx[].left = [0; 32]; mi.left_seg = [0; 32]; }`). +libvpx zeroes them at the start of EVERY superblock row: + +- `vp9/decoder/vp9_decodeframe.c:2256-2258` (plain `decode_tiles`, the path a 1-thread `drv` + takes), with the row-mt twin at `:2114-2115` and the per-tile-row twins at `:1825-1826` / + `:1898-1899`; +- `MACROBLOCKD.left_context` is `ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16]` + (`vp9/common/vp9_blockd.h:192`), so `vp9_zero` clears all 16 slots of all 3 planes; + `left_seg_context[8]` (`blockd.h:195`) likewise. + +Our plane-0 left index wraps at mi_row 16 (`ay = ((mi_row << 1) + tx_row) % 32`), so the first +block of SB row 2 read a slot left over from SB row 0: coefficient EOB context 1 instead of 0, +first divergence at bool #74792 (oracle prob 195 vs ours 84), hard desync from there. + +`above_context` / `above_seg_context` need no such change: libvpx clears them per FRAME +(`memset` at `:2076-2080`) but each tile's `xd` points into a disjoint column range +(`vp9_init_macroblockd`, `vp9_onyxc_int.h:381-393`), which is exactly what our per-tile fills +at `decode.rs:269-270` do. + +### Second defect found by the 1080p leg of the acceptance (same family: extent gated on the wrong boundary) + +`crates/ec-vp9/src/loopfilter.rs`, 4:2:0 chroma passes: the row-group guard was +`if y8 + 8 > uv_h { continue; }` (visible chroma height). libvpx's loop condition is on MI rows +— `vp9_filter_block_plane_ss11`, `vp9/common/vp9_loopfilter.c:1400` (`r += 4` while +`mi_row + r < mi_rows`, vertical) and `:1431` (`r += 2`, horizontal) — so a group whose FIRST MI +row is inside the frame IS filtered even when its 8 chroma rows overhang the visible height into +the padded buffer. `vp9_adjust_mask` keeps those mask bits for odd last-SB-row remnants +(`rows == 7` -> `mask_uv == 0xffff`). + +Symptom: 1080p (mi_rows 135) had **Y byte-exact but U/V off by +/-1** in the last SB row +(chroma rows 536-539) — the vertical edges of group 3 were never filtered. 320x240 (mi_rows 30, +`rows == 6` -> group 3's mask is cleared) never showed it, which is why no fixture caught it. + +Fix: gate both chroma passes on the MI-row condition (`sbr + 4*(rg>>1) < mi_rows` vertical, +`sbr + 2*rg < mi_rows` horizontal) and read the level through a new bounds-safe +`LfGrids::level_at(r, c)` (0 past the frame edge: `vp9_adjust_mask` guarantees the mask bit for +such a cell is cleared, so the value only has to be safe, not meaningful — and it also removes +a latent OOB `level8` read for widths whose last SB column holds <= 6 mi cols). + +### Fixes applied (this batch, uncommitted at time of writing) + +- `crates/ec-vp9/src/decode.rs`: guard deleted, both resets run at every SB row (`:271-285`). +- `crates/ec-vp9/src/loopfilter.rs`: chroma row-group guards + `level_at` (above). + +### Sweep evidence (before -> after; same commands, same IVF files) + +| # | command | before (HEAD `d068da4f`) | after | +|---|---|---|---| +| 1 | `SWEEP_TILE0=1 SWEEP_SRC=.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` | +| 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` | +| 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` | + +Acceptance 2 was measured with fix 1 only (`U 77/70/68 V 9/12/9`, `RESULT 0 of 3`) — that is +where defect 2 surfaced; both fixes together give `0/0/0`. + +`cargo test -p ec-vp9` -> every binary green (lib 9/9, `keyframe_exact` 4/4, all scratch +harnesses); `cargo check -p ec-vp9` -> the same 5 lib warnings, no new ones. No commit made by +this lane (the orchestrator commits). + +## OPEN (found by verification, 2026-09-16) — the tile-ROW path is not decoded correctly + +Verification of the DESYNC CLOSED batch (above) confirmed the left-context fix and the chroma +row-group fix, and then probed the un-exercised remainder: **`log2_tile_rows > 0` is not +decoded correctly by the port at all**, and the batch's two fixes do not touch that class. +Three pre-existing defects were isolated with direct probes (all in the tile-row path): + +1. `crates/ec-vp9/src/modes.rs:186-190` reads tx probs from the `TX_*` const defaults instead + of the frame-updated `FrameContext` tables. The header sets `p8x8[0][0] = 31` for the + probed frame; the port reads 100 -> prob mismatch at read #3203 of its tile-row frame. +2. `crates/ec-vp9/src/modes.rs:177-185` omits libvpx's `if (!has_above) above_ctx = left_ctx` + fallback (oracle ctx 0 / prob 20 vs port ctx 1 / prob 8). +3. `MiState::above_mi` and `crates/ec-vp9/src/decode.rs:701` gate above-pixel and + above-context availability on the tile-row start where libvpx gates on the FRAME + (`mi_row != 0`). With (1) and (2) fixed, pixel divergence starts exactly at row 512 — the + first tile-row boundary. + +Related, and now narrowed: `decode.rs:269-270` zeroes `ectx[].above` / `mi.above_seg` once per +tile. That is equivalent to libvpx's frame-scoped clear (`vp9_decodeframe.c:2076-2080`) only +for `log2_tile_rows == 0` (disjoint column ranges); with tile rows, the first SB row of tile +row k > 0 may legitimately read above state written by tile row k - 1, which the per-tile +fill discards. The comment in `decode.rs` states this explicitly. + +**Status: the tile-row class is NOT closed.** No available fixture and no sweep exercises +`log2_tile_rows > 0`, so none of the three defects is covered by a gate; they were found with +purpose-built probes, not by the crate's tests.