From b4af12528fd9fad6e77f46139e1e29b14588ec84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 23:13:46 +0300 Subject: [PATCH] fix(av1): rect has_tr/has_bl row stride is 32 mi (MAX_MIB_SIZE_LOG2=5), not 16 -- r4's '4 - bw_log2' halved it and every table row but the first read the wrong byte; pinned by a bijection test over a whole superblock walk --- crates/ec-av1/src/encode.rs | 59 +++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 5852a836..1450cb93 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1159,7 +1159,19 @@ impl Reach { return false; } let table = has_tr_rect_table(bw, bh); - Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col) + Self::bit(table, Self::rect_table_index(bw_log2, blk_row, blk_col)) + } + + /// libaom `has_top_right`'s own index arithmetic: + /// `(blk_row << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) + blk_col`, and + /// `MAX_MIB_SIZE_LOG2` is 5 (`enums.h`: libaom's tables are laid out for a + /// 128x128 superblock = 32 mi), NOT this crate's 64x64 = 16. lane-rectx r5: + /// r4 wrote `4 - bw_log2`, halving the row stride -- every table row but + /// the first then read the wrong byte (16x8 wrong at 80 mi positions in a + /// 64-wide SB, 8x16 at 16, 32x16 at 64, a regression against main). + /// [`tests::rect_reach_tables_are_indexed_with_a_32_mi_row_stride`] pins it. + fn rect_table_index(bw_log2: usize, blk_row: usize, blk_col: usize) -> usize { + (blk_row << (5 - bw_log2)) + blk_col } /// libaom `has_bottom_left`, same granularity as [`Self::top_right_rect`]. @@ -1181,7 +1193,7 @@ impl Reach { return false; } let table = has_bl_rect_table(bw, bh); - Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col) + Self::bit(table, Self::rect_table_index(bw_log2, blk_row, blk_col)) } /// Neither, which is all a mode that reads no further than its own edges @@ -3123,6 +3135,49 @@ pub fn encode_sequence( #[cfg(test)] mod tests { + + /// lane-rectx r5: libaom lays `has_tr_*`/`has_bl_*` out row-major for a + /// 128x128 superblock -- `MAX_MIB_SIZE_LOG2 = 5` mi columns per row, so a + /// row holds `32 >> bw_in_mi_log2` bits and the table holds + /// `32 >> bh_in_mi_log2` such rows. Walking every block position of that + /// superblock must therefore hit every bit of the table EXACTLY ONCE. + /// r4's `4 - bw_log2` index halved the row stride: the walk then covered + /// only a quarter of the 16x8 table and read the wrong byte for every + /// block row but the first (silently wrong above-right/below-left + /// availability -- wrong pixels, no error). + #[test] + fn rect_reach_tables_are_indexed_with_a_32_mi_row_stride() { + for (bw, bh) in [(16usize, 8usize), (8, 16), (32, 16), (16, 32), (64, 32), (32, 64)] { + let (bw_log2, bh_log2) = + ((bw / 4).trailing_zeros() as usize, (bh / 4).trailing_zeros() as usize); + let (rows, cols) = (32 >> bh_log2, 32 >> bw_log2); + for (name, table) in + [("has_tr", has_tr_rect_table(bw, bh)), ("has_bl", has_bl_rect_table(bw, bh))] + { + assert_eq!( + table.len() * 8, + rows * cols, + "{name}_{bw}x{bh}: libaom's table is {rows} rows of {cols} bits" + ); + let mut seen = vec![0u32; table.len() * 8]; + for blk_row in 0..rows { + for blk_col in 0..cols { + let idx = Reach::rect_table_index(bw_log2, blk_row, blk_col); + assert!( + idx < seen.len(), + "{name}_{bw}x{bh}: index {idx} past the table at ({blk_row},{blk_col})" + ); + seen[idx] += 1; + } + } + assert!( + seen.iter().all(|&n| n == 1), + "{name}_{bw}x{bh}: the block walk is not a bijection onto the table's bits \ + (row stride is not {cols})" + ); + } + } + } use super::*; use crate::intra::{D45_PRED, D135_PRED, H_PRED, KEY_FRAME_MODES, NON_DIRECTIONAL, V_PRED}; use std::io::Write;