From ab4ff0c5ff3647c931b95a7c77a0bcecf3081762 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:34:32 +0300 Subject: [PATCH] fix(av1): the superblock size travels with a FrameCtx copy; two reach gates pin 64 filter_ctx_copy dropped sb128_flag/reach_sb_px, so a tile SEARCH worker searched at 64 while the calling thread searched at 128: the coded bytes depended on EC_AV1_TILE_THREADS under EC_AV1_SB128 (tile_bytes_do_not_depend_on_the_thread_count, 1x2 tiles, 9251 vs 9239). every_tile_layout: the spec's uniform spacing derives the tile count from the superblock grid, so a 128-superblock frame with 5 SB columns codes 3 tiles for tile_cols_log2=2; the test now expects the encoder's own grid. The two reach gates transcribe libaom's 64-superblock tables, so they pin a 64 context instead of following the process knob. --- crates/ec-av1/src/decode.rs | 7 +++++++ crates/ec-av1/src/encode.rs | 12 ++++++++++-- crates/ec-av1/src/encoder.rs | 17 +++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 4f42c748..8f194378 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -275,6 +275,13 @@ pub(crate) fn filter_ctx_copy(fctx: &FrameCtx) -> FrameCtx { out.seg_mi_dims.set(fctx.seg_mi_dims.get()); out.seg.replace(fctx.seg.borrow().clone()); out.seg_ids.replace(fctx.seg_ids.borrow().clone()); + // lane-b128m: the superblock size travels with the copy. A tile SEARCH + // worker (`encode::search_tiles`) gets its context from here, so dropping + // it made every worker search at 64 while the calling thread searched at + // 128 -- the bytes a frame came out as then depended on the tile thread + // count under `EC_AV1_SB128`. + out.sb128_flag.set(fctx.sb128_flag.get()); + out.reach_sb_px.set(fctx.reach_sb_px.get()); out } diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index ee521b14..3bb6ca80 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -13510,7 +13510,11 @@ mod tests { /// (the horizontal shapes, which those partitions never produce). #[test] fn every_rect_shape_reaches_what_libaom_says_over_the_whole_superblock() { - let fctx = &crate::decode::FrameCtx::for_encoder(); + // lane-b128m: this test's oracle is libaom's 64-SUPERBLOCK table, so + // it pins the 64 reach explicitly instead of following the process's + // own superblock knob (`for_encoder`), which made it red under + // `EC_AV1_SB128=1` for no defect of the code under test. + let fctx = &crate::decode::FrameCtx::new(); // (bw, bh, has_tr len, has_tr byte sum, has_bl len, has_bl byte sum). const SHAPES: [(usize, usize, usize, u32, usize, u32); 14] = [ (4, 8, 64, 9280, 64, 550), @@ -14437,7 +14441,11 @@ mod tests { /// wrong for one block size while looking plausible for the other. #[test] fn reach_matches_libaom_has_top_right_and_has_bottom_left() { - let fctx = &crate::decode::FrameCtx::for_encoder(); + // lane-b128m: this test's oracle is libaom's 64-SUPERBLOCK table, so + // it pins the 64 reach explicitly instead of following the process's + // own superblock knob (`for_encoder`), which made it red under + // `EC_AV1_SB128=1` for no defect of the code under test. + let fctx = &crate::decode::FrameCtx::new(); // Transcribed from has_top_right/has_bottom_left's row_off==0, // col_off==0 (whole-transform) path, with MAX_MIB_SIZE_LOG2 = 5 (a // 128px reference grid) pinned as libaom pins it, independent of the diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 7f935774..8292f0a5 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -3675,6 +3675,19 @@ mod tests { tile_rows_log2: rows_log2, }; let layout = format!("{}x{} tiles", 1 << cols_log2, 1 << rows_log2); + // lane-b128m: the SPEC's uniform spacing derives the tile count + // from the superblock grid (`tileWidthSb = ceil(sbCols >> log2)`), + // so a frame with fewer superblock columns than the requested + // count codes fewer tiles -- which is what a 128-superblock + // sequence does here. The grid the encoder itself computes is the + // expectation; at 64 superblocks it is `1 << log2` exactly. + let grid = crate::tile::TileLayout::new( + (width as u32).div_ceil(4), + (height as u32).div_ceil(4), + cols_log2, + rows_log2, + ); + let (want_cols, want_rows) = (grid.cols(), grid.rows()); let mut enc = Av1Encoder::new(config).unwrap(); let mut stream = Vec::new(); for packet in encode_all(&mut enc, &sources) { @@ -3693,12 +3706,12 @@ mod tests { if let ec_av1_syntax::ObuKind::Frame(parsed, tiles) = &obu.kind { assert_eq!( (parsed.tile_info.cols, parsed.tile_info.rows), - (1 << cols_log2, 1 << rows_log2), + (want_cols, want_rows), "{layout}: the header's own tile grid" ); assert_eq!( tiles.len(), - 1 << (cols_log2 + rows_log2), + (want_cols * want_rows) as usize, "{layout}: tile payloads located in the tile group" ); frames += 1;