From f6d6eda40c3868ba51065b056a9ec9f01fbb6750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 3 Sep 2026 02:15:36 +0300 Subject: [PATCH] fix(av1): the chroma edge-filter type reads the mi grid's uv_mode, not a one-slot-per-column map get_intra_edge_filter_type takes the above/left uv_mode of the CHROMA neighbour mi (libaom set_mi_row_col: chroma_above_mbmi = base_mi[-stride]), and libaom writes one MB_MODE_INFO into every mi cell a block covers. Our neighbour band kept a single (last_row, uv_mode) slot per mi column, so a sub-8x8 pair's own first (non-chroma-ref) half stamped its DC_PRED over that column before the pair's chroma-ref half -- which snaps its read to the base (even) column -- looked at it: the mi-exact match failed and the coarse 16x16 band answered DC where a SMOOTH_PRED chroma block really sat one mi row above. The edge-filter type flipped 1 -> 0, changing the chroma edge filter/upsample decision: chroma-only +-1..3, luma bit-exact. Adds a full mi-granular uv_mode grid (superblock-padded, per frame, tile availability unchanged) read first, with the old maps kept as the fallback for cells no block has written, plus counter uv_mode_grid_override_hits. 10-bit 3840x1608 10 s head: 66 of 241 decode frames differed (first 161, worst 199 B) -> 0 of 241 byte-exact vs instrumented aomdec. 10-bit 1920x792 cut: 134 of 275 differed (worst 2410 B, the 1942 B chroma-only intra-in-inter frame included) -> 5, worst 3 B. Second 1920x792 cut unchanged (36 frames, 1-13 B, the open bottom-edge MC residue). --- crates/ec-av1/src/decode.rs | 68 ++++++++++++++++++++++- lanes/t900-r12.handoff.md | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 lanes/t900-r12.handoff.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 478f159d..fca72f51 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2597,6 +2597,20 @@ pub(crate) fn uv_mode_mi_override_hits() -> usize { UV_MODE_MI_OVERRIDE_HITS.with(|c| c.get()) } +// lane-t900 r12: how many chroma edge-filter-type reads took their above/left +// `uv_mode` from the mi GRID where the one-slot-per-column map would have +// answered differently -- the sub-8x8 pair case where the pair's own first +// half had already overwritten the column slot the chroma-ref half reads. +thread_local! { + static UV_MODE_GRID_OVERRIDE_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`UV_MODE_GRID_OVERRIDE_HITS`]. +pub fn uv_mode_grid_override_hits() -> usize { + UV_MODE_GRID_OVERRIDE_HITS.with(|c| c.get()) +} + // lane-t900 r8: how many intra edge-filter-type reads (luma + chroma) the // INTER neighbour stamp saved: the coarse SUB band still held a SMOOTH mode // from an earlier block in that pixel row/column while the mi-exact map -- @@ -4633,6 +4647,18 @@ struct Neighbours { /// the leaf above writes the same row slot the block to the left owns. uv_mode_col: Vec<(usize, usize)>, uv_mode_row: Vec<(usize, usize)>, + /// Every mi cell's own `uv_mode`, the mi grid libaom's + /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`, + /// blockd.h). lane-t900 r12: the one-slot-per-column + /// `uv_mode_col`/`uv_mode_row` maps above hold only the LAST block written + /// in that column, so a sub-8x8 pair's own first (non-chroma-ref) half + /// overwrote the row the pair's chroma-ref half then wanted to read -- + /// the mi-exact match failed and the coarse [`SUB`] band answered instead. + /// `u8::MAX` = no block has written this cell yet (fall back to the maps). + uv_mode_grid: Vec, + /// [`Self::uv_mode_grid`]'s stride, in mi units (superblock-padded, like + /// every other band here). + uv_grid_cols: usize, /// Whether the block above/left of this column/row was coded `skip` -- /// an inter frame's own `skip` context (spec `SkipContext`), which the /// key-frame writer never tracks (its skip context is always zero); a @@ -4811,6 +4837,8 @@ impl Neighbours { sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], uv_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)], uv_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], + uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)], + uv_grid_cols: cols * (SUB / MI), above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)], left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)], // lane-inter8 r2: mi(4px)-granular, not [`SUB`]-granular -- an @@ -5749,6 +5777,18 @@ impl Neighbours { *slot = (last_c, uv_mode); } } + // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block + // covers, so the chroma neighbour read below is a plain grid read. + let stride = self.uv_grid_cols; + for row in mi_r..=last_r { + for col in mi_c..=last_c { + if col < stride + && let Some(cell) = self.uv_mode_grid.get_mut(row * stride + col) + { + *cell = uv_mode as u8; + } + } + } } /// Whether either mi-exact chroma neighbour of the block at `(mi_r, mi_c)` @@ -5801,16 +5841,34 @@ impl Neighbours { { UV_TILE_EDGE_SUPPRESSED_HITS.with(|h| h.set(h.get() + 1)); } + // The mi grid is libaom's own read; the two maps below stay as the + // fallback for cells no block has written (see [`Self::uv_mode_grid`]). + let cell = |grid: &Vec, row: usize, col: usize| -> Option { + (col < self.uv_grid_cols) + .then(|| grid.get(row * self.uv_grid_cols + col).copied()) + .flatten() + .filter(|&m| m != u8::MAX) + .map(usize::from) + }; let above = match self.uv_mode_col.get(mi_c) { + _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m, Some(&(row, m)) if have_above && row == mi_r - 1 => m, _ if have_above => self.above_uv_mode[c], _ => DC_PRED, }; let left = match self.uv_mode_row.get(mi_r) { + _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m, Some(&(col, m)) if have_left && col == mi_c - 1 => m, _ if have_left => self.left_uv_mode[r], _ => DC_PRED, }; + if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| { + !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if row == mi_r - 1 && cm == m) + }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| { + !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if col == mi_c - 1 && cm == m) + }) { + UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1)); + } // lane-cfl r1 counter: how often the mi-exact chroma neighbour differs // from the coarse [`SUB`] slot -- i.e. how often a chroma edge would // have been filtered at the wrong strength by the coarse map alone. @@ -18109,7 +18167,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // in reconstruction (this dump) vs is introduced by the loop filter // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump). // Decode-order frame marker for the EC_OBMC / EC_TRACE_MODE ladders. - if std::env::var_os("EC_OBMC").is_some() || std::env::var_os("EC_TRACE_MODE").is_some() { + if std::env::var_os("EC_OBMC").is_some() + || std::env::var_os("EC_TRACE_MODE").is_some() + || std::env::var_os("EC_PRED").is_some() + { eprintln!( "EC_PICT idx={}", PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst) @@ -29677,7 +29738,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // in reconstruction (this dump) vs is introduced by the loop filter // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump). // Decode-order frame marker for the EC_OBMC / EC_TRACE_MODE ladders. - if std::env::var_os("EC_OBMC").is_some() || std::env::var_os("EC_TRACE_MODE").is_some() { + if std::env::var_os("EC_OBMC").is_some() + || std::env::var_os("EC_TRACE_MODE").is_some() + || std::env::var_os("EC_PRED").is_some() + { eprintln!( "EC_PICT idx={}", PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst) diff --git a/lanes/t900-r12.handoff.md b/lanes/t900-r12.handoff.md new file mode 100644 index 00000000..f8a02baa --- /dev/null +++ b/lanes/t900-r12.handoff.md @@ -0,0 +1,104 @@ +# lane-t900 r12 handoff -- the chroma edge-filter type read a one-slot-per-column uv_mode map; a sub-8x8 pair overwrote its own neighbour row + +Tip: `2b65cb8e+` on `lane-t900` (base: main `2b65cb8e`, fast-forwarded in before any edit). + +## Target A -- 4K 10 s head, decode frame 161: ROOT CAUSE FOUND AND FIXED + +Stage split first (r11 method rule), BEFORE reading any prediction code: +`EC_AV1_PREFILT_DUMP16` on both decoders over a 250-OBU / 168-decode-frame cut of the +10 s head (`~/.cache/t900-tmp/reelb250.obu`) -- the mismatch is already in RECONSTRUCTION +(pre-filter), frames 161 and 162 only, and it is **CHROMA ONLY, luma bit-exact**: + + f161 U 37 px, V 22 px, deltas -3..+3, in three anti-diagonal triangles at + chroma (856,160), (1080,252), (1088,696) + +Block: chroma (856,160) = luma mi(80,429), a 4x8 luma leaf (PARTITION_VERT of an 8x8), +i.e. the CHROMA-REFERENCE half of a sub-8x8 pair. `EC_PRED` on both decoders +(ours needs `EC_PICT` under `EC_PRED`, added this round, decode.rs:29684 -- the second +of the two EC_PICT sites is the inter-frame one): + + aomdec EC_PRED mi_row=80 mi_col=429 plane=1 txw=4 txh=4 mode=3 p_angle=45 + have_top=1 have_left=1 n_top=4 n_left=4 n_tr=4 n_bl=-1 bsize=3 part=2 ft=1 + EC_PREDOUT sum=8355 row0=514,521,523,523 + ours OUR_PRED x=856 y=160 side=4 mode=3 ad=0 ft=0 sum=8358 row0=[514,524,522,523] + +One field: `ft` (intra_edge_filter_type) 1 vs 0. Same mode, same angle, same edges. + +Root cause (`decode.rs` `Neighbours::smooth_uv_neighbour_unsnapped`): libaom reads the +chroma neighbours straight off the mi grid (`chroma_above_mbmi = base_mi[-stride]`, +`set_mi_row_col`), where every mi cell a block covers holds that block's `uv_mode`. +Ours kept only ONE slot per mi column (`uv_mode_col[col] = (last_row, uv_mode)`), so +inside a sub-8x8 pair the pair's own FIRST half (mi(80,428), non-chroma-ref, stamps +DC_PRED over mi rows 80..81) overwrote column 428 before the pair's chroma-ref half +(mi(80,429), which snaps its neighbour read to base column 428) read it. The mi-exact +match `row == mi_r - 1` then failed and the coarse SUB-grid band answered DC instead of +the SMOOTH_PRED chroma block really sitting at mi(79,428) -- `ft` 1 -> 0, which flips +`av1_use_intra_edge_upsample`/filter strength on the chroma edge. + +Fix: `uv_mode_grid`, a full mi-granular `uv_mode` grid (superblock-padded stride +`uv_grid_cols`, `u8::MAX` = unwritten) written by `record_uv_mode_mi` over every cell a +block covers and read first by `smooth_uv_neighbour_unsnapped`; the two old maps stay as +the fallback for unwritten cells, so no existing path changes answer unless the grid +knows better. Availability stays tile-relative (`mi_r > tile_row0_mi` / +`mi_c > tile_col0_mi`, untouched); the grid is per-frame (`Neighbours::new`). +New counter `ec_av1::decode::uv_mode_grid_override_hits()`. + +Measured (cmp.sh = `EC_AV1_FINAL_DUMP` both decoders, byte compare per decode frame): + +| stream | before r12 | after r12 | +|---|---|---| +| 4K 10 s head, 168-frame cut `reelb250.obu` | 2 differ (f161 60 B, f162 72 B) | **0 of 168** | +| 4K 10 s head, full `~/.cache/hg-0-10s.obu` (241 f) | 66 differ, first 161, worst 199 B | **0 of 241** | +| 1080p @5400 cut (275 f) | 134 differ, first 61, f113 1942 B, worst 2410 B | **5 differ, first 61, worst 3 B (f63)** | +| 1080p @900 cut (267 f) | 36 differ, first 41, worst 13 B | 36 / 41 / 13 (unchanged) | + +## Target B -- 1080p @5400 f113 (1942 B, chroma-only intra in an inter frame): FIXED BY A + +Same root cause, same fix: f113 is byte-exact now, and the cut went 134 -> 5 differing +frames, worst 2410 B -> 3 B. The r11 charter premise (CfL / chroma DC ctx / the r8 +DC_PRED stamp being wrong on the chroma side) was NOT the defect -- the stamp is right +(an inter neighbour must read as not-smooth for chroma); what was wrong is WHICH cell +the read landed on. Residue on that cut: 5 frames, 1-3 B each (f61, f63, f136, f174, +f236) -- same 1-3 B shape as target C, unmeasured this round. + +## Target C -- 1-3 B bottom-edge residue on the @900 cut: UNTOUCHED, premise narrowed + +36 frames, first 41, worst 13 B; unchanged by A. Not started this round beyond reading +the reference-clamp path on both sides: +* ours `mc::sample` clamps every tap to `true_width/true_height`, which + `decode.rs:16238` sets to the MI-ALIGNED size (`mi_cols*4`, `mi_rows*4`), halved for + chroma; +* libaom clamps via `extend_mc_border` -> `build_mc_border(..., pre_buf->width, + pre_buf->height)` and `setup_pred_plane` fills those from the CROP dims + (`uv_crop_width/height`). +For 1920x792: MiRows 198 -> aligned 792 == crop 792, chroma 396 == 396, so +aligned-vs-crop CANNOT be the explanation on this stream (r11 said the same; now read +off both sources). Next step is the tap-row rung as chartered: `EC_MC_CALL` already +exists on our side (`mc.rs:380`, prints x_q4/y_q4/xfrac/yfrac/tw/th/stride); add the +matching rung in `dec_calc_subpel_params` / `update_extend_mc_border_params` +(decodeframe.c:484-560) printing `block.x0/x1/y0/y1`, `frame_width/height` and whether +the border-extension path was TAKEN -- note that path is skipped entirely when the MV is +whole-pel and both frame dims are multiples of 8, so libaom then reads the ref buffer's +own extended border rows rather than a clamped copy. Frame 41, chroma (353,395), skip +32x32 mi(192,176) mv0=(-4,14). + +## Method notes for the successor + +* Our `EC_PICT idx=N` marker is printed at the END of frame N's decode, so a frame's + trace lines are the ones BEFORE its marker, not after (an r11/r12 off-by-one that + silently reads the next frame). +* `EC_PICT` now also prints under `EC_PRED` (both sites). +* aomdec has no frame marker; align its `EC_PRED` lines by grepping a small mi window + in both decoders and matching the sequences 1:1 -- an UNMATCHED extra line on our side + is itself the finding. +* Dumps: `~/.cache/t900-tmp/pf_r12_161/` (prefilter, 168 f), `d_r12_reelb10/`, + `d_r12_cut_900/`, `d_r12_cut_5400/`. + +## Owed (not done at the turn cap) + +* Witness gate: no gate asserts `uv_mode_grid_override_hits() > 0` yet. Check the + existing 128sb/gm_small_side/frame_edge_mv_clamping fixtures for a hit first + (`decode_probe` does not print the new counter yet), else pin the smallest prefix of + the 4K head that fires it (the defect frame is 161, so a ~250-OBU cut). +* Full `cargo test -p ec-av1 --lib` suite run (charter said no suite this round). +* 1080p head 0-10 s re-probe (was 78 differing, first 86).