From 177f6ca19a05c84dd590bd3514c8fe38d024bd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 10:34:52 +0300 Subject: [PATCH] lane-av1422llpred: per-axis reach for lossless 4:2:2 chroma (r1: the found fix; r2: the class sweep) r1 -- THE FIX (lane-av1422llpred). read_intra_chroma_lossless computed the per-TU Reach with tu_reach(side, side, col_off, row_off, 4 << ss_x(fctx), ...), and tu_reach takes ONE transform size and passes it as BOTH tx_w and tx_h, so the transform HEIGHT came from ss_x instead of ss_y. A TX_4x4 chroma unit is 4 << ss_x luma px WIDE but 4 << ss_y luma px TALL; at 4:2:0 and 4:4:4 the two shifts are identical, which is exactly why both formats have been byte-exact and only 4:2:2 could reach it. Now tu_reach_rect with tx_h = 4 << ss_y(fctx) (decode.rs:37568). The sibling walk in decode_block already did this correctly with separate spans. libaom: av1_dr_prediction_z3_c (reconintra.c:609) with the reach decided by has_bottom_left (reconintra.c:411) -- row_off + tx_size_high_unit[TX_4X4] < AOMMAX(mi_size_high[bsize] >> ss_y, 1). Ours before: row_off + tx_h < bh -> 0 + 8 < 8 -> false, so below_left was denied. After: 0 + 4 < 8 -> true. WHY INTERIOR-ONLY: build_directional_and_filter_intra_predictors (reconintra.c:1150) fills left_col[0..3] and only extends to [4..7] when n_bottomleft_px > 0; with the reach denied our Edges::build replicated left_col[3] across 4..7. z3 reads left_col[base]/[base+1] with base = (23*(c+1)+r)>>6, so row0 and col0 only read [0..3] (byte-identical) while the interior reads the replicated tail. Feeding libaom's own formula the 8-sample left column reproduces the oracle's col0 exactly -- the arithmetic was right, the edge was short. MEASURED by Main on a bypassed release build of this branch vs the oracle (aomdec --rawvideo, per plane, 320x240 4:2:2 intra): ll422_allintra Y 0 / U 3126 -> 2984 / V 3139 -> 3067. Prediction-sum disagreements 490 -> 453 of 9600 paired units. W_intrabc and X_intrabc_tiled UNCHANGED (917974/474506/487694 and 864325/421718/414871) -- both are inter/intrabc streams and the changed function is reached only from intra lossless 4:2:2. Oracle-flip control: one oracle V byte rotated moves V by exactly +1. r2 -- THE CLASS SWEEP, done by Main, because r1 fixed one instance of a shape with more. tu_reach takes ONE size for two axes; grepping every call site for a subsampled single size found TWO more, both in decode_rect4_16_strip (18985 and 19072), both converted to tu_reach_rect with tx_h = 4 << ss_y(fctx). The remaining 15 tu_reach sites pass no subsampled size, and the sites that already used separate sizes (19022/19116/37613/37783) were already correct. MEASURED: a no-op on both 4:2:2 cells we have (W_intrabc byte-for-byte the same counts; ll422_allintra unchanged), so those two are recorded as correct-for-the-4:2:2-port and NOT as a measured win -- they are provably an identity at 4:2:0 and 4:4:4 where ss_x == ss_y, which is the only reason they are safe to land unmeasured. NOT GATEABLE, stated plainly: 4:2:2 is refused at the sequence header (stream.rs:1803), so no committed test build can reach read_intra_chroma_lossless at ss (1,0) and no committed test can gate this. The evidence is the measurement table plus the oracle-flip isolation control -- no source-scan substitute, and this change is NOT described as gated. Regression: 422/lossless/444/warp/intra -> 175 passed, 0 failed, 2 ignored. --- crates/ec-av1/src/decode.rs | 19 ++++- lanes/av1422llpred.report.md | 140 +++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 lanes/av1422llpred.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 92821179..8b84761c 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -18982,12 +18982,19 @@ fn decode_rect4_16_strip( // never reads (measured: key frame 0, 4:4:4 lossless // 16x4 strip at (104,26), U 29 + V 34 samples wrong, // entropy bit-identical over 351109 EC_SYMR reads). - let cu_reach = tu_reach( + // lane-av1422llpred r2: this site had the OTHER half + // of the same shape -- `tu_reach` takes ONE size for + // both axes, so a subsampled chroma unit's height + // came from `<< ss_x` instead of `<< ss_y` (a TX_4x4 + // chroma unit is `4 << ss_x` luma px wide but + // `4 << ss_y` tall). Identical at 4:2:0/4:4:4. + let cu_reach = tu_reach_rect( pw, ph, ox << ss_x(fctx), oy << ss_y(fctx), 4 << ss_x(fctx), + 4 << ss_y(fctx), pair_reach, ppx, ppy, @@ -19069,12 +19076,17 @@ fn decode_rect4_16_strip( // Same ss-aware luma footprint as the skip arm // above -- `ox * 2 / 8` is the 4:2:0-only form and // mis-answers `of_tu` at 4:4:4 (see there). - let cu_reach = tu_reach( + // lane-av1422llpred r2: and the SKIP arm's fix + // missed the other half of the same shape -- one + // size for both axes makes the height come from + // `<< ss_x`, so only 4:2:2 can reach it. + let cu_reach = tu_reach_rect( pw, ph, ox << ss_x(fctx), oy << ss_y(fctx), 4 << ss_x(fctx), + 4 << ss_y(fctx), pair_reach, ppx, ppy, @@ -37564,12 +37576,13 @@ fn read_intra_chroma_lossless( let cu_around = neighbours.around_mi(cu_mi, MI); // The unit's origin and extent in LUMA pixels: `<< ss` at // 4:2:0 (8x8 luma for a 4x4 chroma unit), identity at 4:4:4. - let cu_reach = tu_reach( + let cu_reach = tu_reach_rect( side, side, ox << ss_x(fctx), oy << ss_y(fctx), 4 << ss_x(fctx), + 4 << ss_y(fctx), reach, px, py, diff --git a/lanes/av1422llpred.report.md b/lanes/av1422llpred.report.md new file mode 100644 index 00000000..9b0e2adf --- /dev/null +++ b/lanes/av1422llpred.report.md @@ -0,0 +1,140 @@ +# lane-av1422llpred — 4:2:2 chroma intra prediction divergence + +Target: `probe/ll422_allintra.obu` (320x240, 4:2:2 lossless all-intra, 1 frame). +Base: `2005a35d` (both prior fixes already landed). Baseline reproduced exactly: +**Y 0 / U 3126 / V 3139**. + +## Root cause + +`read_intra_chroma_lossless` computed a per-transform-unit `Reach` with +`tu_reach(side, side, col_off, row_off, 4 << ss_x(fctx), …)`. `tu_reach` passes its +single transform-size argument as **both** `tx_w` and `tx_h` +(`crates/ec-av1/src/encode.rs:1578`, `Reach::of_tu(bw, bh, col_off, row_off, tx, tx, block)`), +so the unit's transform height was shifted by `ss_x` instead of `ss_y`. + +A TX_4x4 chroma unit is `4 << ss_x` luma px wide but `4 << ss_y` luma px tall. +At 4:2:0 (`ss_x == ss_y == 1`) and 4:4:4 (`ss_x == ss_y == 0`) the two shifts are +equal and the bug is invisible — which is exactly why both formats are byte-exact. +**Only 4:2:2 (`ss_x == 1`, `ss_y == 0`) is reachable for the defect.** + +### The named expressions + +| | expression | value at the first wrong unit | +|---|---|---| +| libaom | `av1_dr_prediction_z3_c`, `reconintra.c:609`; `dy = dr_intra_derivative[270-200]` = `dr_intra_derivative[70]` = **23** (`reconintra.h:122` / `:141`, table `reconintra.h:81`) | — | +| libaom reach | `has_bottom_left` early return, `reconintra.c:411`: `row_off + tx_size_high_unit[TX_4X4] < AOMMAX(mi_size_high[bsize] >> ss_y, 1)` → `0 + 1 < 2` → **1** | `n_bl=4` (oracle `EC_PRED` rung) | +| ours (was) | `Reach::of_tu` below-left chain, `encode.rs:4041`: `row_off + tx_h < bh` → `0 + (4<<1=8) < 8` → **false** → fell through to `block.below_left` = **false** | left edge sliced to 4 samples | +| ours (now) | `4 << ss_y(fctx)` → `0 + (4<<0=4) < 8` → **true** | `n_bl` equivalent | + +Call site: `crates/ec-av1/src/decode.rs:37668` (`read_intra_chroma_lossless`). +The sibling walk in `decode_block` (`decode.rs:22476`) already used `tu_reach_rect` +with separate `luma_span_x`/`luma_span_y`; `read_intra_chroma_lossless` did not. + +### Why interior-only + +`build_directional_and_filter_intra_predictors` (`reconintra.c:1150`) copies +`n_left_px` samples into `left_col[0..3]`, then **only if `n_bottomleft_px > 0`** +copies the next `n_bottomleft_px` into `left_col[4..7]`. With the reach denied, +our `Edges::build` (`intra.rs:187`) replicated `left_col[3]` over indices 4..7. +`av1_dr_prediction_z3_c` reads `left_col[base]`/`[base+1]` with `base = (23*(c+1)+r) >> 6`: + +- row 0 / col 0 of the block only ever read `left_col[0..3]` → **byte-identical** +- the interior (and `col0[3]`) read `left_col[4..7]` → the replicated tail + +The below-left edge supplies only the **base** term (the DC of the directional +walk); the angle interpolation term is unchanged. That is precisely an +interior-only difference with matching row0/col0 and matching coefficients. + +First wrong unit (paired by decode order, 9600 records each side, pairing verified +unambiguous — coordinate agreement exact except a uniform −2 reporting artifact in +libaom's `xd->mi_col` on odd columns): + +``` +mi(18,30) plane=V chroma (60,72) 4x4 mode=7 (D203_PRED) angle_delta=-1 p_angle=200 +ours pred=[158,158,157,156, 156,156,155,154, 154,154,153,153, 153,153,153,153] sum=2476 +oracle pred sum=2468 row0 SAME col0 ours=[158,156,154,153] oracle=[158,156,154,152] +oracle n_top=4 n_left=4 n_tr=-1 n_bl=4 bsize=3(BLOCK_8X8) part=0 +our fed left slice (EC_DEBUG_EDGES): Some([158,157,155,153]) <-- 4, not 8 +``` +Feeding libaom's own `z3` formula the 8-sample left column +`[158,157,155,153,151,149,146,143]` reproduces the oracle's col0 `[158,156,154,152]` +exactly; our predictor arithmetic was already correct — only the edge was short. +Residual was byte-identical (`res0=[-1,-3,-4,-5]` both sides). + +## Measurements + +`ll422_allintra` (per plane, vs `aomdec --rawvideo`): + +| cell | plane | before | after | +|---|---|---|---| +| ll422_allintra | Y | 0 | **0** | +| ll422_allintra | U | 3126 | **2984** (−142) | +| ll422_allintra | V | 3139 | **3067** (−72) | + +First wrong U sample moved from rel 1853 to rel 5222; the earlier divergence class +is gone. 490 → 453 prediction-sum disagreements out of 9600 paired units. + +**Oracle-flip control:** one byte of the oracle V plane flipped +(offset 126780, 157→158) → V count moved **3067 → 3068, exactly +1**. The +comparator is live. + +**Regression** (W_intrabc / X_intrabc_tiled, 16 frames each, totals over all frames): + +| cell | plane | before | after | +|---|---|---|---| +| W_intrabc | Y / U / V | 917974 / 474506 / 487694 | **917974 / 474506 / 487694** (no change) | +| X_intrabc_tiled | Y / U / V | 864325 / 421718 / 414871 | **864325 / 421718 / 414871** (no change) | + +Unchanged because both are inter/intrabc streams; the changed function is reached +only from intra lossless 4:2:2. + +`cargo test -p ec-av1 --lib -- 422 lossless 444 warp --skip bitrate_target_lands_within_5_percent_over_48_frames` +→ **90 passed, 0 failed** (with the 4:2:2 header guard in place). +(Run with the bypass patched in, 5 `…_refuse_by_name` tests fail — that is the +bypass disabling the refusal, not a regression.) + +## Gating — this change is NOT gateable + +**Stated plainly:** 4:2:2 is refused at the sequence header +(`crates/ec-av1/src/stream.rs:1803`, `if seq.subsampling_x != seq.subsampling_y`). +No committed test build can reach `read_intra_chroma_lossless` at ss (1,0), so +**there is no committed test that can gate this fix.** The evidence is the +measurement table above plus the oracle-flip isolation control, not a test. +Per instruction, no source-scan gate was substituted and this change is **not** +described as gated. The 4:2:2 refuse-vs-lift product decision is untouched. + +## Remaining residual — identified, not fixed + +Residual drops 3126→2984 / 3139→3067, so this is one member of a family. The next +first divergence after the fix: + +``` +idx 2818 mi(8,49) plane=U chroma (96,32) 4x4 mode=7 (D203_PRED) ad=0 p_angle=203 +ours sum=2656 oracle sum=2653 row0 SAME, col0 SAME -> interior-only again +oracle: n_top=4 n_left=4 n_tr=-1 n_bl=4 bsize=2 (BLOCK_8X4) +``` +`bsize=2` is **BLOCK_8X4** — a rectangular luma leaf. `read_intra_chroma_lossless` +still passes `side, side` as the reach's `(bw, bh)`, so for a rect leaf `bh` is the +leaf's WIDTH, not its height; libaom's `has_bottom_left` uses +`mi_size_high[bsize]` (`reconintra.c:410`). For mi(8,49) libaom returns 1 through +the **superblock leftmost-column** branch (`reconintra.c:421-434`): +`blk_col_in_sb == 0` → `row_off_in_sb + 1 < 16` → true, a branch +`Reach::of_tu` does not model at all. Our `left` slice is still 4 samples there. + +Two distinct follow-ups, both in the same function: +1. pass the rect leaf's true luma `(bw, bh)` instead of `side, side`; +2. port `has_bottom_left`'s `blk_col_in_sb == 0` superblock branch into + `Reach::of_tu` (currently only the `blk_row` bottom-row and table cases exist). + +Neither is fixable by the one-line per-axis change; both need the rect-leaf shape +and the superblock leftmost-column predicate. + +## Provenance + +- worktree `/home/user/.cache/wt/av1422llpred`, branch `lane-av1422llpred`, from `main` = `2005a35d` +- `CARGO_TARGET_DIR=/home/user/.cache/tgt/av1422llpred` +- oracle: instrumented `/home/user/.cache/aom-oracle/build/aomdec`, `EC_PRED` rung + (`reconintra.c:1868`), picture counter `aom_ec_pict_idx` (`decodeframe.c:89`) +- rungs used: ours `EC_PRED` / `EC_DEBUG_EDGES`; oracle `EC_PRED` / `EC_PREDOUT8` +- bypass patch (`stream.rs` `if false && …`) applied for probing only and + **restored**; the only committed-tree change is the 1-line `decode.rs` fix