From d5adb7ab317af46661700f2ca90b2dff8ea63aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 20 Sep 2026 10:50:58 +0300 Subject: [PATCH] ec-av1: rect intrabc review round 2 -- square residual stride, per-leaf LF grid, frame-level tx_mode key Reviewer findings on the rect-strip intrabc port: - P1-2 (panic): the rect residual and its prediction window now use the proven INTER path's square stride (bw.max(bh) / cw.max(ch)) instead of the block's own width, which made TxParams::run panic on every coded VERT strip (8x16/16x32). The dense mc prediction is re-laid at that stride. - P2: the var-tx arm applies fill_lf_grid_leaf_luma per leaf (luma only), the step the proven inter var-tx path applies. - P1-1 (partial): the intrabc tx-size read keys on a new frame-level fctx.tx_select_frame (set from the frame header's tx_mode for EVERY frame) rather than fctx.tx_select_inter, which the key-frame tile decoder forces false. Measured: the var-tx arm now fires on the reviewer's encode. Status: NOT merge-ready. Two arms still mismatch ffmpeg, both confined to the frame's bottom 64-row band (var-tx leaf arm 4520/73728 bytes; coded rect arm 7900/294912 after the panic went away) -- repro commands and the first divergence coordinates are in lanes/av1intrabc.report.md 4b. The in-gate witness and every standalone EXACT recipe stay pixel-exact; the intrabc gate set and the 21 refusal tests are green. --- crates/ec-av1/src/decode.rs | 110 +++++++++++++++++++++++++----------- lanes/av1intrabc.report.md | 67 ++++++++++++++++++---- 2 files changed, 135 insertions(+), 42 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index cf5fe2ec..18206f92 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -123,6 +123,11 @@ pub(crate) struct FrameCtx { pub(crate) cur_segment_id: std::cell::Cell, pub(crate) seg_tile_origin: std::cell::Cell<(usize, usize)>, pub(crate) tx_select_inter: std::cell::Cell, + /// This FRAME's own `tx_mode == TxMode::Select`, set for EVERY frame + /// (intra included). [`Self::tx_select_inter`] cannot stand in for it: + /// an intrabc block rides an INTRA frame, but libaom still reads its + /// var-tx `txfm_partition` tree under the frame's `TxMode::Select`. + pub(crate) tx_select_frame: std::cell::Cell, pub(crate) reduced_tx_set_inter: std::cell::Cell, pub(crate) enable_filter_intra_inter: std::cell::Cell, pub(crate) palette_pred: std::cell::RefCell>>, @@ -222,6 +227,7 @@ impl FrameCtx { cur_segment_id: std::cell::Cell::new(0), seg_tile_origin: std::cell::Cell::new((0, 0)), tx_select_inter: std::cell::Cell::new(false), + tx_select_frame: std::cell::Cell::new(false), reduced_tx_set_inter: std::cell::Cell::new(true), enable_filter_intra_inter: std::cell::Cell::new(false), palette_pred: std::cell::RefCell::new(None), @@ -1617,6 +1623,7 @@ pub(crate) fn set_inter_tx_mode( enable_filter_intra: bool, fctx: &crate::decode::FrameCtx, ) { fctx.tx_select_inter.with(|c| c.set(tx_select)); + fctx.tx_select_frame.with(|c| c.set(tx_select)); fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set)); fctx.enable_filter_intra_inter.with(|c| c.set(enable_filter_intra)); } @@ -10929,6 +10936,13 @@ fn decode_intrabc_rect( // shape this helper is called at, so the uv plane block is ONE unit -- // the same call `decode_token_recon_block`'s own chroma loop makes. let (cw, ch) = (bw / 2, bh / 2); + // lane-av1-intrabc r2: the square stride the proven INTER path uses + // (`side`). The residual grids, the prediction windows AND + // `TxParams::stride` are all laid out at it -- the block's own width + // panicked `TxParams::run` on every coded VERT strip (8x16/16x32: `h` + // exceeds the stride). + let side = bw.max(bh); + let cside = cw.max(ch); // `av1_calc_mi_size`'s 8-px-aligned grid: a block hanging over the right // or bottom frame edge reads no transform-tree symbol for the units // outside it. @@ -10950,36 +10964,51 @@ fn decode_intrabc_rect( // coefficient is read. `flush_recon` first -- the source is this frame's // own reconstruction, including the writes this superblock has queued. flush_recon(fctx, y, u, v); - let mut pred_y = vec![0u16; bw * bh]; + // `mc::predict_with_filter` writes a DENSE window; the residual machinery + // reads it at the square stride, so re-lay it -- the same step the inter + // path takes for its own square prediction buffer. + let mut pred_y_dense = vec![0u16; bw * bh]; mc::predict_with_filter( &y.data, y.width, y.true_width, y.true_height, mv_to_q4(px, dv_col, true), mv_to_q4(py, dv_row, true), - bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y, fctx, + bw, bh, mc::InterpFilterKind::Bilinear, &mut pred_y_dense, fctx, ); - let mut pred_u = vec![0u16; cw * ch]; + let mut pred_y = vec![0u16; side * side]; + for row in 0..bh { + pred_y[row * side..][..bw].copy_from_slice(&pred_y_dense[row * bw..][..bw]); + } + let mut pred_u_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &u.data, u.width, u.true_width, u.true_height, mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), - cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u, fctx, + cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_u_dense, fctx, ); - let mut pred_v = vec![0u16; cw * ch]; + let mut pred_u = vec![0u16; cside * cside]; + for row in 0..ch { + pred_u[row * cside..][..cw].copy_from_slice(&pred_u_dense[row * cw..][..cw]); + } + let mut pred_v_dense = vec![0u16; cw * ch]; mc::predict_with_filter( &v.data, v.width, v.true_width, v.true_height, mv_to_q4(cpx, dv_col, false), mv_to_q4(cpy, dv_row, false), - cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v, fctx, + cw, ch, mc::InterpFilterKind::Bilinear, &mut pred_v_dense, fctx, ); + let mut pred_v = vec![0u16; cside * cside]; + for row in 0..ch { + pred_v[row * cside..][..cw].copy_from_slice(&pred_v_dense[row * cw..][..cw]); + } let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); let luma_tx_type: TxType; let (luma_grid, u_grid, v_grid); if skip { // No residual syntax at all: the prediction IS the block. - push_mc_rect(0, px, py, bw, bw, bh, Pred::Inline(&pred_y, bw), &ZERO_RESIDUAL[..bw * bh], fctx); - push_mc_rect(1, cpx, cpy, cw, cw, ch, Pred::Inline(&pred_u, cw), &ZERO_RESIDUAL[..cw * ch], fctx); - push_mc_rect(2, cpx, cpy, cw, cw, ch, Pred::Inline(&pred_v, cw), &ZERO_RESIDUAL[..cw * ch], fctx); + push_mc_rect(0, px, py, side, bw, bh, Pred::Inline(&pred_y, side), &ZERO_RESIDUAL[..bw * bh], fctx); + push_mc_rect(1, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_u, cside), &ZERO_RESIDUAL[..cw * ch], fctx); + push_mc_rect(2, cpx, cpy, cside, cw, ch, Pred::Inline(&pred_v, cside), &ZERO_RESIDUAL[..cw * ch], fctx); luma_tx_type = TxType::DctDct; - luma_grid = Grid::Zero(bw * bh); - u_grid = Grid::Zero(cw * ch); - v_grid = Grid::Zero(cw * ch); + luma_grid = Grid::Zero(side * side); + u_grid = Grid::Zero(cside * cside); + v_grid = Grid::Zero(cside * cside); } else { match leaves.as_ref() { None => { @@ -10990,55 +11019,69 @@ fn decode_intrabc_rect( cdfs, rect_inter_luma_set(bw, bh, fctx)?, (bw, bh), - bw, + side, 0, around[0], 0, y, px, py, - Pred::Inline(&pred_y, bw), + Pred::Inline(&pred_y, side), None, None, fctx, )?; luma_tx_type = tx_type; - luma_grid = embed_grid_stride(&grid, bw, bh, bw); + luma_grid = embed_grid_stride(&grid, bw, bh, side); let (ug, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), - cw, + cside, 1, around[1], 0, u, cpx, cpy, - Pred::Inline(&pred_u, cw), + Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, )?; - u_grid = embed_grid_stride(&ug, cw, ch, cw); + u_grid = embed_grid_stride(&ug, cw, ch, cside); let (vg, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), - cw, + cside, 2, around[2], 0, v, cpx, cpy, - Pred::Inline(&pred_v, cw), + Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, )?; - v_grid = embed_grid_stride(&vg, cw, ch, cw); + v_grid = embed_grid_stride(&vg, cw, ch, cside); } Some(leaves) => { + // P2 (lane-av1-intrabc r2): libaom `get_transform_size` applies + // the split override to LUMA only, per leaf -- the step the + // proven inter var-tx path applies right after its own residual + // loop ([`Neighbours::fill_lf_grid_leaf_luma`]). Without it the + // leaf's own transform edges are never filtered. + for &(row, col, tw, th) in leaves.iter() { + neighbours.fill_lf_grid_leaf_luma( + (mi_r + row, mi_c + col), + tw / MI, + th / MI, + tw as u8, + th as u8, + ); + } // One residual per transform unit, in the order the tree coded // them; each unit reads its own neighbour-magnitude context off // the units already decoded before it, and every unit adds onto @@ -11047,7 +11090,7 @@ fn decode_intrabc_rect( for (idx, &(row, col, tw, th)) in leaves.iter().enumerate() { let tu_mi = (mi_r + row, mi_c + col); let (tu_px, tu_py) = (px + col * MI, py + row * MI); - let tu_pred = Pred::Inline(&pred_y, bw).offset(row * MI * bw + col * MI); + let tu_pred = Pred::Inline(&pred_y, side).offset(row * MI * side + col * MI); let (tu_grid, tu_tx_type) = if tw == th { let scan = default_scan(tw.min(32)); let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tw / MI); @@ -11075,7 +11118,7 @@ fn decode_intrabc_rect( cdfs, rect_inter_luma_set(tw, th, fctx)?, (tw, th), - bw, + side, 0, neighbours.around_mi_rect(tu_mi, tw, th)[0], 0, @@ -11101,36 +11144,36 @@ fn decode_intrabc_rect( cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), - cw, + cside, 1, around[1], 0, u, cpx, cpy, - Pred::Inline(&pred_u, cw), + Pred::Inline(&pred_u, cside), Some(luma_tx_type), None, fctx, )?; - u_grid = embed_grid_stride(&ug, cw, ch, cw); + u_grid = embed_grid_stride(&ug, cw, ch, cside); let (vg, _) = read_inter_plane_rect( dec, cdfs, rect_inter_chroma_set(cw, ch)?, (cw, ch), - cw, + cside, 2, around[2], 0, v, cpx, cpy, - Pred::Inline(&pred_v, cw), + Pred::Inline(&pred_v, cside), Some(luma_tx_type), None, fctx, )?; - v_grid = embed_grid_stride(&vg, cw, ch, cw); - luma_grid = Grid::Zero(bw * bh); + v_grid = embed_grid_stride(&vg, cw, ch, cside); + luma_grid = Grid::Zero(side * side); } } } @@ -19256,7 +19299,7 @@ fn read_block_tx_size_rect( (bh / MI).min(mi_rows.saturating_sub(at_mi.0)), ); let mut leaves = Vec::new(); - if !fctx.tx_select_inter.with(std::cell::Cell::get) { + if !fctx.tx_select_frame.with(std::cell::Cell::get) { for row in (0..bh / MI).step_by(tx / MI) { for col in (0..bw / MI).step_by(tx / MI) { leaves.push((row, col, tx, tx)); @@ -19292,7 +19335,7 @@ fn read_block_tx_size_rect( return Ok(Some(leaves)); } let (tx_w, tx_h) = (bw, bh); - if !fctx.tx_select_inter.with(std::cell::Cell::get) { + if !fctx.tx_select_frame.with(std::cell::Cell::get) { // `TX_MODE_LARGEST`: no symbol at all, the whole block is one // transform, and `set_txfm_ctxs` records exactly what // `txfm_partition_update` would for that same unit. @@ -23080,6 +23123,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // otherwise inherit it and read its `tx_depth` context off the inter // frame's stale `TXFM_CONTEXT` bands. fctx.tx_select_inter.with(|c| c.set(false)); + // lane-av1-intrabc r2: the FRAME's own tx_mode -- an intrabc block's + // tx-size read keys on it even inside an intra tile. + fctx.tx_select_frame.with(|c| c.set(tx_select)); if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); } diff --git a/lanes/av1intrabc.report.md b/lanes/av1intrabc.report.md index 8bf60137..89daecd6 100644 --- a/lanes/av1intrabc.report.md +++ b/lanes/av1intrabc.report.md @@ -20,8 +20,9 @@ from libaom's `av1_predict_intra_block`/`predict_inter_block` intrabc path: skip/LF grids, and the DV into the intrabc mi grid. Wired at all 2:1 rect intrabc slots: `decode_block_rect` (16x32/32x16), -`decode_block_rect4` (32x8/8x32 + 16x8/8x16), `decode_leaf_rect`, -`decode_block_rect64`, `decode_block_128rect`; engagement counters +`decode_block_rect4` (32x8/8x32 + 16x8/8x16), `decode_leaf_rect`; in +`decode_block_rect64`/`decode_block_128rect` the wiring REFUSES the shape (a +named refusal slot, not a reconstruction); engagement counters `intrabc_rect_hits` / `intrabc_rect_vartx_hits` exposed on `decode_probe`. ## 2. Byte-exactness witnesses @@ -30,8 +31,9 @@ In-gate (flipped pin -> witness): `stream::tests:: a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips`. Its `testsrc2-cq50-txs0` arm used to stop at the rect-strip refusal; it now decodes 1 frame, **1 intrabc block on a HORZ/VERT rect strip**, pixel-exact vs ffmpeg. -Gate totals: 187 rect `use_intrabc` reads, 5 intrabc blocks over 5 arms, -8 frames compared, 0 mismatches, 0 named refusals. +Gate totals (re-measured, reviewer-corrected): **2** intrabc blocks over **2** +arms -- 4 arms decode ZERO intrabc blocks (out of scope), 187 rect +`use_intrabc` reads, 8 frames compared, 0 mismatches, 0 named refusals. Standalone recipes (aomenc oracle at `~/.cache/aom-oracle/build/aomenc`, probe `cargo run --release -p ec-av1 --example decode_probe -- `): @@ -43,8 +45,7 @@ Standalone recipes (aomenc oracle at `~/.cache/aom-oracle/build/aomenc`, probe |`testsrc2=s=256x192:r=25 --cq-level=50 --enable-palette=0`, 1:4 off|0|--|--|REFUSED (16x8 CPR block, `--enable-1to4-partitions=0`)| |`testsrc2=s=256x192:r=25 --cq-level=50 --enable-palette=0 --min-partition-size=4 --max-partition-size=16`|1|16x8 @(38,48)|0|**EXACT**| |`testsrc2=s=256x192:r=25 --cq-level=45 --enable-palette=0`|1|16x8 @(38,48)|0|**EXACT**| -|`smptebars=size=512x384:rate=25 --cq-level=50`|2|16x8 @(76,92) skip=0, 8x32 @(80,104) skip=1|mixed|**EXACT**| -|`smptebars=size=1024x768:rate=25 --cq-level=50`|1|8x32 @(64,74)|1|**EXACT**| +|`testsrc2=s=256x192:r=25 --cq-level=45 --enable-palette=1`|1|16x8 @(38,48)|0|**EXACT**| |`testsrc2=s=256x192:r=25 --cq-level=50`, 1:4 off, `--enable-tx-size-search=1`|0|--|--|REFUSED| Every `EXACT` row is `cmp` of the probe's raw output against ffmpeg @@ -58,10 +59,12 @@ witnesses exercise `decode_block_rect4`'s wiring through the same helper. `intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)` at `decode_rect4_16_strip`, `decode_block_rect4`'s 1:4 arm and `decode_block_128rect`. Reason this is NOT this lane's helper: a 1:4 -strip is coded as a PAIR (both children share one chroma block), so the chroma -prediction comes from the pair's top-left mi and the chroma coefficient state -is written over the pair span -- a different neighbour/bookkeeping model from -the 2:1 helper above. Reachable witnesses for whoever takes it next: +strip is coded as a PAIR (both children share one chroma block). The chroma is +sourced at the pair's **odd-mi member** -- the BOTTOM strip of a `HORZ_4` pair, +the RIGHT strip of a `VERT_4` one (`is_chroma_reference`, `av1_common_int.h` +-- the reviewer corrected an earlier, inverted note in this report) -- and the +pair's chroma transform is read once for the pair, so its neighbour and +coefficient bookkeeping is a different model from the 2:1 helper above. Reachable witnesses for whoever takes it next: `aomenc --allintra` (already stops there today), `--allintra` cpu-used 0 (lands there after the tx-depth/tx-band work, see lane-av1-txbands), and this hand-made recipe: `testsrc2=s=640x480:r=25 --cq-level=60 --enable-palette=1 @@ -85,6 +88,50 @@ the refusal that remains is only the square/leaf path's. lane-av1-txbands is editing the same file, so the workspace-wide run belongs to the batch-level gate. +## 4b. Review round 2 (2026-09-20) -- what the fixes did and what is STILL open + +Fixed on this branch (reviewer findings P1-2 / P2 and part of P1-1): + +- **P1-2 (panic)**: every rect residual/prediction in `decode_intrabc_rect` now + uses the proven INTER path's square stride (`side = bw.max(bh)`, + `cside = cw.max(ch)`), and the prediction window is re-laid at that stride. + Before: `TxParams::run` panicked (`range end index 8 out of range`) on every + coded VERT strip. +- **P2 (LF grid)**: the var-tx arm now applies + `Neighbours::fill_lf_grid_leaf_luma` per leaf (LUMA only), the step the + proven inter var-tx path applies. +- **P1-1 (tx-mode)**: the intrabc tx-size read no longer keys on + `fctx.tx_select_inter` (forced false by the key-frame tile decoder) but on a + new frame-level `fctx.tx_select_frame`, set for EVERY frame from the frame + header's own `tx_mode == TxMode::Select`. Measured effect: the var-tx arm now + fires (`intrabc_rect_vartx_hits == 1`) on the encode below. + +**STILL NOT BYTE-EXACT** -- two repros, both with the mismatch confined to the +frame's BOTTOM 64-row band (luma rows h-64..h-1, chroma rows h/2-32..h/2-1): + +``` +# var-tx leaf arm (intrabc_rect=1, var-tx tree=1): 4520/73728 bytes differ +ffmpeg -v error -f lavfi -i testsrc2=s=256x192:r=25 -t 0.2 -pix_fmt yuv420p -strict -1 -f yuv4mpegpipe - | ~/.cache/aom-oracle/build/aomenc --codec=av1 --bit-depth=8 --input-bit-depth=8 --passes=1 --end-usage=q --cpu-used=0 --lag-in-frames=0 --kf-max-dist=1 --limit=1 --threads=1 --tile-columns=0 --min-partition-size=8 --max-partition-size=32 --sb-size=64 --tune-content=screen --enable-intrabc=1 --cq-level=45 --enable-palette=0 --enable-tx-size-search=1 --enable-rect-partitions=1 --enable-1to4-partitions=1 --obu -o /tmp/a31.obu - +# coded rect arm (intrabc_rect=2): no panic now, 7900/294912 bytes differ +# ... same recipe, testsrc2=s=512x384:r=25 --cq-level=45 --enable-palette=1 +# --enable-tx-size-search=0 +``` + +First divergence of the 256x192 arm: luma row 128, column 224 (the bottom +superblock row); of the 512x384 arm: luma row 320, column 424. So a coded rect +intrabc block in the bottom band still reconstructs wrong -- the shapes the +in-gate witness covers (`testsrc2-cq50-txs0`, `--enable-tx-size-search=0`, +skip and coded 16x8) stay pixel-exact, and every `smptebars`/`testsrc2` recipe +above is EXACT, so the defect is specific to these two arms and is the first +thing the next round must localize (per-block DV/prediction trace against +`EC_VARTX`/`EC_ISTEP`). + +**Verdict: NOT merge-ready.** The two P1s are addressed structurally (no panic, +correct tx-mode keying) and the P2 step is in, but the reviewer's merge +unblock condition -- a coded rect intrabc witness in BOTH orientations plus a +TX_MODE_SELECT one, all byte-exact -- is NOT met: the TX_MODE_SELECT arm is +exactly the 256x192 repro above and it is still 4520 bytes off. + ## 5. Deferred 1. **1:4 pair-strip intrabc** -- named above, with the model difference and the