forge
Explore
Log in

Don't you dare go Hollow

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

Browse files at this commit

b4af12528fd9fad6e77f46139e1e29b14588ec84Ahmet Kaan GümüşParent: 8a60151
  • fix
    • 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
Show full message
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.rsmodified+57 -2
@@ -1159,7 +1159,19 @@ impl Reach {
1159 return false;1159 return false;
1160 }1160 }
1161 let table = has_tr_rect_table(bw, bh);1161 let table = has_tr_rect_table(bw, bh);
1162- Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col)1162+ Self::bit(table, Self::rect_table_index(bw_log2, blk_row, blk_col))
1163+ }
1164+
1165+ /// libaom `has_top_right`'s own index arithmetic:
1166+ /// `(blk_row << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) + blk_col`, and
1167+ /// `MAX_MIB_SIZE_LOG2` is 5 (`enums.h`: libaom's tables are laid out for a
1168+ /// 128x128 superblock = 32 mi), NOT this crate's 64x64 = 16. lane-rectx r5:
1169+ /// r4 wrote `4 - bw_log2`, halving the row stride -- every table row but
1170+ /// the first then read the wrong byte (16x8 wrong at 80 mi positions in a
1171+ /// 64-wide SB, 8x16 at 16, 32x16 at 64, a regression against main).
1172+ /// [`tests::rect_reach_tables_are_indexed_with_a_32_mi_row_stride`] pins it.
1173+ fn rect_table_index(bw_log2: usize, blk_row: usize, blk_col: usize) -> usize {
1174+ (blk_row << (5 - bw_log2)) + blk_col
1163 }1175 }
1164 1176
1165 /// libaom `has_bottom_left`, same granularity as [`Self::top_right_rect`].1177 /// libaom `has_bottom_left`, same granularity as [`Self::top_right_rect`].
@@ -1181,7 +1193,7 @@ impl Reach {
1181 return false;1193 return false;
1182 }1194 }
1183 let table = has_bl_rect_table(bw, bh);1195 let table = has_bl_rect_table(bw, bh);
1184- Self::bit(table, (blk_row << (4 - bw_log2)) + blk_col)1196+ Self::bit(table, Self::rect_table_index(bw_log2, blk_row, blk_col))
1185 }1197 }
1186 1198
1187 /// Neither, which is all a mode that reads no further than its own edges1199 /// Neither, which is all a mode that reads no further than its own edges
@@ -3123,6 +3135,49 @@ pub fn encode_sequence(
3123 3135
3124 #[cfg(test)]3136 #[cfg(test)]
3125 mod tests {3137 mod tests {
3138+
3139+ /// lane-rectx r5: libaom lays `has_tr_*`/`has_bl_*` out row-major for a
3140+ /// 128x128 superblock -- `MAX_MIB_SIZE_LOG2 = 5` mi columns per row, so a
3141+ /// row holds `32 >> bw_in_mi_log2` bits and the table holds
3142+ /// `32 >> bh_in_mi_log2` such rows. Walking every block position of that
3143+ /// superblock must therefore hit every bit of the table EXACTLY ONCE.
3144+ /// r4's `4 - bw_log2` index halved the row stride: the walk then covered
3145+ /// only a quarter of the 16x8 table and read the wrong byte for every
3146+ /// block row but the first (silently wrong above-right/below-left
3147+ /// availability -- wrong pixels, no error).
3148+ #[test]
3149+ fn rect_reach_tables_are_indexed_with_a_32_mi_row_stride() {
3150+ for (bw, bh) in [(16usize, 8usize), (8, 16), (32, 16), (16, 32), (64, 32), (32, 64)] {
3151+ let (bw_log2, bh_log2) =
3152+ ((bw / 4).trailing_zeros() as usize, (bh / 4).trailing_zeros() as usize);
3153+ let (rows, cols) = (32 >> bh_log2, 32 >> bw_log2);
3154+ for (name, table) in
3155+ [("has_tr", has_tr_rect_table(bw, bh)), ("has_bl", has_bl_rect_table(bw, bh))]
3156+ {
3157+ assert_eq!(
3158+ table.len() * 8,
3159+ rows * cols,
3160+ "{name}_{bw}x{bh}: libaom's table is {rows} rows of {cols} bits"
3161+ );
3162+ let mut seen = vec![0u32; table.len() * 8];
3163+ for blk_row in 0..rows {
3164+ for blk_col in 0..cols {
3165+ let idx = Reach::rect_table_index(bw_log2, blk_row, blk_col);
3166+ assert!(
3167+ idx < seen.len(),
3168+ "{name}_{bw}x{bh}: index {idx} past the table at ({blk_row},{blk_col})"
3169+ );
3170+ seen[idx] += 1;
3171+ }
3172+ }
3173+ assert!(
3174+ seen.iter().all(|&n| n == 1),
3175+ "{name}_{bw}x{bh}: the block walk is not a bijection onto the table's bits \
3176+ (row stride is not {cols})"
3177+ );
3178+ }
3179+ }
3180+ }
3126 use super::*;3181 use super::*;
3127 use crate::intra::{D45_PRED, D135_PRED, H_PRED, KEY_FRAME_MODES, NON_DIRECTIONAL, V_PRED};3182 use crate::intra::{D45_PRED, D135_PRED, H_PRED, KEY_FRAME_MODES, NON_DIRECTIONAL, V_PRED};
3128 use std::io::Write;3183 use std::io::Write;