From 683eca419f2cfc816938c940aae5f7cf6ca8b8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 17:22:12 +0300 Subject: [PATCH] fix(av1): never code a transform unit that starts outside the frame The writer's per-unit luma loop walked every unit of a block's transform grid, including the ones a right-edge- (or bottom-edge-) straddling block has entirely outside the frame. libaom clips that loop to max_blocks_wide/high and decode.rs does the same, so the extra all_zero symbols desynced the tile at the very next symbol: at 216x96 an odd-size GOP's last block reconstructed a different V, and both libdav1d and libaom refused the whole key frame. Clip the loop to the frame's true mi_cols/mi_rows. The block-aligned byte pins do not move (their clips have no straddling block); the ffmpeg GOP gate now runs 216x96 as well, three-way exact against reconstruction. --- crates/ec-av1/src/encode.rs | 21 +++++++------------- crates/ec-av1/src/stream.rs | 39 +++++++++++++++++++++++++------------ crates/ec-av1/src/tile.rs | 13 +++++++++++++ 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 323a857f..e29bbf72 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1663,21 +1663,14 @@ impl Plane<'_> { /// A neighbour outside this plane's own TILE does not exist, exactly as /// the writer's per-tile `Neighbours` bands start blank. /// - /// OPEN (lane-av1skipctx): the decisions this price changes make - /// `decode_stream_round_trips_an_odd_size_gop` and + /// CLOSED (lane-av1straddle): the decisions this price changes used to + /// make `decode_stream_round_trips_an_odd_size_gop` and /// `an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction` - /// fail at 216x96, frame 0, V plane only -- the last, right-edge- - /// STRADDLING 32x32 at luma (192, 64), whose writer trace says both - /// chroma planes code nothing (`EC_RNG`: plane=1 skip_ctx=1, plane=2 - /// skip_ctx=0, both `eob0=1`) while the decoder reconstructs a different - /// V; the differing samples are exactly that block plus its deblock halo - /// (chroma rows 29-47, cols 92-107). It is NOT this map: with `coef_ctx` - /// ablated no `EC_AV1_PRUNE_K` in 1..=6 and no `EC_AV1_ESTIMATE=1` - /// reproduces it, and a pricer writes no symbol -- it is a pre-existing - /// writer/decoder desync in the straddling last block (class: - /// last-block desync reads as reconstruction) that only a decision - /// change reaches. Needs a decoder-side `txb_skip` context trace aligned - /// to the writer's `EC_RNG` one to close. + /// fail at 216x96. Never this map: the writer coded the phantom + /// transform units of a right-edge-straddling 32x32 (tile.rs + /// `write_luma_tus`, now clipped to the frame's true `mi_cols`/`mi_rows` + /// like decode.rs and libaom), which libdav1d and libaom both refused + /// outright while a decision change was what first walked into it. fn coef_ctx(&self, x: usize, y: usize, side: usize, set: TxbSet) -> (usize, usize) { if self.ctx.cells.is_empty() { return (0, 0); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 4cc6869d..2ef0a4bb 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -3318,18 +3318,33 @@ pub(crate) mod tests { eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg"); return; } - let (width, height) = (128usize, 64usize); - let pictures: Vec<_> = (0..3) - .map(|i| panned_test_card(width, height, i * 3)) - .collect(); - let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); - let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); - let decoded = decode_stream(&encoded.stream).unwrap(); - assert_eq!(decoded.len(), 3); - for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { - assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg"); - assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg"); - assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg"); + // lane-av1straddle: 216x96 joins 128x64 because only a size whose + // 32x32 blocks straddle the right-hand frame edge can see the writer + // coding a transform unit that starts outside the frame -- libdav1d + // AND libaom both refused the whole key frame while it did, and this + // crate's own (lenient) decoder only differed in the last block's V. + for &(width, height) in &[(128usize, 64usize), (216usize, 96usize)] { + let pictures: Vec<_> = (0..3) + .map(|i| panned_test_card(width, height, i * 3)) + .collect(); + let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap(); + let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); + let decoded = decode_stream(&encoded.stream).unwrap(); + assert_eq!(decoded.len(), 3); + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { + assert_eq!(got.y, want.y, "{width}x{height} frame {i} luma vs ffmpeg"); + assert_eq!(got.u, want.u, "{width}x{height} frame {i} U vs ffmpeg"); + assert_eq!(got.v, want.v, "{width}x{height} frame {i} V vs ffmpeg"); + } + // ... and the encoder's own reconstruction is what ffmpeg sees, + // so the check fails on a writer-side desync too, not only on a + // decoder-side one. + for (i, (frame, want)) in encoded.frames.iter().zip(&ffmpeg_frames).enumerate() { + let rec = &frame.reconstruction; + assert_eq!(rec.y, want.y, "{width}x{height} frame {i} luma: reconstruction vs ffmpeg"); + assert_eq!(rec.u, want.u, "{width}x{height} frame {i} U: reconstruction vs ffmpeg"); + assert_eq!(rec.v, want.v, "{width}x{height} frame {i} V: reconstruction vs ffmpeg"); + } } } diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 765edab5..621df2cc 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -3600,6 +3600,19 @@ fn write_luma_tus( for tu_row in 0..n { for tu_col in 0..n { let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI)); + // lane-av1straddle: libaom clips the transform loop to + // `max_blocks_wide/high` (`mb_to_right_edge`/`mb_to_bottom_edge`, + // off the frame's true `mi_cols`/`mi_rows`), and decode.rs does + // the same (`tu_px >= y.true_width` there): a unit whose TOP-LEFT + // sample is outside the frame is NEVER coded. Writing one -- as + // this loop did for the phantom right-hand column of a 32x32 at + // x=192 in a 216-wide frame -- desyncs the tile at the very next + // symbol, which is what made an odd-size GOP's last block + // reconstruct differently and both ffmpeg decoders (libdav1d and + // libaom) refuse the whole key frame. + if tu_mi.0 >= neighbours.mi_rows || tu_mi.1 >= neighbours.mi_cols { + continue; + } let unit = if n == 1 { grid.to_vec() } else {