From 44a12afb848d9227a5d0c78b8c2566748e59271a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 13:37:50 +0300 Subject: [PATCH] fix(av1): the writer's intrabc DV predictor and wavefront follow the superblock size tile::intrabc_dv_pred hardcoded mib_size = 16 while decode.rs read_intrabc_dv takes sb_mi_cur, so under EC_AV1_SB128 the two derived different fallback DVs from identical symbols (writer -48 px, reader -112 px at mi=(36,4)) and the key frame reconstructed differently -- no divergent symbol at all, the whole trace matches except the MVDBG note. Ibc::dv_valid likewise counted libaom's 256-pixel intrabc delay in 64s under a 128 superblock, offering sources the 128 visit order had not reconstructed yet. Witness: a_repeated_pattern_key_frame_codes_intrabc_blocks... ok under EC_AV1_SB128=1. --- crates/ec-av1/src/encode.rs | 28 ++++++++++++++++++---------- crates/ec-av1/src/tile.rs | 13 +++++++------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 3bb6ca80..75ac5502 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -5161,7 +5161,9 @@ fn intrabc_step() -> usize { const IBC_HASH: usize = 16; /// libaom `INTRABC_DELAY_PIXELS / 64` -- how many 64x64 superblocks behind -/// the current one the source's bottom-right corner must already be. +/// the current one the source's bottom-right corner must already be. Under +/// 128 superblocks the same 256 pixels are two superblocks, which +/// [`Ibc::dv_valid`] derives from the size itself. const INTRABC_DELAY_SB64: i32 = 4; /// FNV-1a over one `IBC_HASH`-square of `plane` at `(x, y)`. @@ -5297,22 +5299,28 @@ impl Ibc { if sx < tx0 || sy < ty0 || sx + w > tx1 || sy + h > ty1 { return false; } - let active_sb_row = (y as i32 - ty0) >> 6; - let active_sb_col = (x as i32 - tx0) >> 6; - let src_sb_row = (sy + h - 1 - ty0) >> 6; - let src_sb_col = (sx + w - 1 - tx0) >> 6; + // lane-b128m: libaom counts this wavefront in SUPERBLOCKS, and the + // superblock is 128 wide under `EC_AV1_SB128` (`INTRABC_DELAY_SB = + // INTRABC_DELAY_PIXELS >> sb_size_log2`, i.e. 2 there and 4 here). + // Counting 64s under a 128 superblock let the search take a source + // from a 64 row the 128 visit order had not reconstructed yet. + let sb_log2 = if crate::encode::sb128_on() { 7 } else { 6 }; + let delay: i32 = if sb_log2 == 7 { INTRABC_DELAY_SB64 / 2 } else { INTRABC_DELAY_SB64 }; + let active_sb_row = (y as i32 - ty0) >> sb_log2; + let active_sb_col = (x as i32 - tx0) >> sb_log2; + let src_sb_row = (sy + h - 1 - ty0) >> sb_log2; + let src_sb_col = (sx + w - 1 - tx0) >> sb_log2; // This lane's own restriction (see the doc comment). if src_sb_row >= active_sb_row { return false; } - let total = ((tx1 - tx0 - 1) >> 6) + 1; - if src_sb_row * total + src_sb_col >= active_sb_row * total + active_sb_col - INTRABC_DELAY_SB64 - { + let total = ((tx1 - tx0 - 1) >> sb_log2) + 1; + if src_sb_row * total + src_sb_col >= active_sb_row * total + active_sb_col - delay { return false; } // libaom's wavefront: only the top-left cone of the frame is legal. - let wf = (1 + INTRABC_DELAY_SB64) * (active_sb_row - src_sb_row); - src_sb_col < active_sb_col - INTRABC_DELAY_SB64 + wf + let wf = (1 + delay) * (active_sb_row - src_sb_row); + src_sb_col < active_sb_col - delay + wf } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 8071ea10..2a1dff5e 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -3724,12 +3724,13 @@ fn intrabc_dv_pred(mi_r: usize, mi_c: usize, side: usize) -> (i32, i32) { }); let mut pred = stack_pred.unwrap_or((0, 0)); if pred == (0, 0) { - // 64x64 superblocks: the only size this encoder's sequence header - // signals (`use_128x128_superblock: false`), so `mib_size` is 16 mi. - // The tile row start is 0, exactly as decode.rs `read_intrabc_dv` - // takes it -- both are written for the single-tile-row streams this - // encoder produces. - let sb_mi = 16i32; + // `av1_find_ref_dv` takes `mib_size`, the SEQUENCE's superblock size + // in mi units (lane-b128m: hardcoding 16 here made the writer's DV + // predictor one 64 superblock up while decode.rs `read_intrabc_dv` + // took 128 -- identical symbols, different reconstructed DV). The + // tile row start is 0, exactly as the reader takes it -- both are + // written for the single-tile-row streams this encoder produces. + let sb_mi = if sb128_armed() { 32i32 } else { 16i32 }; let sb_px = sb_mi * MI as i32; pred = if (mi_r as i32) < sb_mi { (0, -(sb_px + INTRABC_DELAY_PIXELS) * 8)