forge
Explore
Log in

Don't you dare go Hollow

Never code a transform unit that starts outside the frame

Browse files at this commit

683eca419f2cfc816938c940aae5f7cf6ca8b8c9Ahmet Kaan GümüşParent: 1058fbb
  • fix
    • never code a transform unit that starts outside the frame
Show full message
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.rsmodified+7 -14
@@ -1663,21 +1663,14 @@ impl Plane<'_> {
1663 /// A neighbour outside this plane's own TILE does not exist, exactly as1663 /// A neighbour outside this plane's own TILE does not exist, exactly as
1664 /// the writer's per-tile `Neighbours` bands start blank.1664 /// the writer's per-tile `Neighbours` bands start blank.
1665 ///1665 ///
1666- /// OPEN (lane-av1skipctx): the decisions this price changes make1666+ /// CLOSED (lane-av1straddle): the decisions this price changes used to
1667- /// `decode_stream_round_trips_an_odd_size_gop` and1667+ /// make `decode_stream_round_trips_an_odd_size_gop` and
1668 /// `an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction`1668 /// `an_odd_size_gop_round_trips_bit_exact_against_the_encoder_reconstruction`
1669- /// fail at 216x96, frame 0, V plane only -- the last, right-edge-1669+ /// fail at 216x96. Never this map: the writer coded the phantom
1670- /// STRADDLING 32x32 at luma (192, 64), whose writer trace says both1670+ /// transform units of a right-edge-straddling 32x32 (tile.rs
1671- /// chroma planes code nothing (`EC_RNG`: plane=1 skip_ctx=1, plane=21671+ /// `write_luma_tus`, now clipped to the frame's true `mi_cols`/`mi_rows`
1672- /// skip_ctx=0, both `eob0=1`) while the decoder reconstructs a different1672+ /// like decode.rs and libaom), which libdav1d and libaom both refused
1673- /// V; the differing samples are exactly that block plus its deblock halo1673+ /// outright while a decision change was what first walked into it.
1674- /// (chroma rows 29-47, cols 92-107). It is NOT this map: with `coef_ctx`
1675- /// ablated no `EC_AV1_PRUNE_K` in 1..=6 and no `EC_AV1_ESTIMATE=1`
1676- /// reproduces it, and a pricer writes no symbol -- it is a pre-existing
1677- /// writer/decoder desync in the straddling last block (class:
1678- /// last-block desync reads as reconstruction) that only a decision
1679- /// change reaches. Needs a decoder-side `txb_skip` context trace aligned
1680- /// to the writer's `EC_RNG` one to close.
1681 fn coef_ctx(&self, x: usize, y: usize, side: usize, set: TxbSet) -> (usize, usize) {1674 fn coef_ctx(&self, x: usize, y: usize, side: usize, set: TxbSet) -> (usize, usize) {
1682 if self.ctx.cells.is_empty() {1675 if self.ctx.cells.is_empty() {
1683 return (0, 0);1676 return (0, 0);
crates/ec-av1/src/stream.rsmodified+27 -12
@@ -3318,18 +3318,33 @@ pub(crate) mod tests {
3318 eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg");3318 eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg");
3319 return;3319 return;
3320 }3320 }
3321- let (width, height) = (128usize, 64usize);3321+ // lane-av1straddle: 216x96 joins 128x64 because only a size whose
3322- let pictures: Vec<_> = (0..3)3322+ // 32x32 blocks straddle the right-hand frame edge can see the writer
3323- .map(|i| panned_test_card(width, height, i * 3))3323+ // coding a transform unit that starts outside the frame -- libdav1d
3324- .collect();3324+ // AND libaom both refused the whole key frame while it did, and this
3325- let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap();3325+ // crate's own (lenient) decoder only differed in the last block's V.
3326- let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3);3326+ for &(width, height) in &[(128usize, 64usize), (216usize, 96usize)] {
3327- let decoded = decode_stream(&encoded.stream).unwrap();3327+ let pictures: Vec<_> = (0..3)
3328- assert_eq!(decoded.len(), 3);3328+ .map(|i| panned_test_card(width, height, i * 3))
3329- for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() {3329+ .collect();
3330- assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg");3330+ let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap();
3331- assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg");3331+ let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3);
3332- assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg");3332+ let decoded = decode_stream(&encoded.stream).unwrap();
3333+ assert_eq!(decoded.len(), 3);
3334+ for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() {
3335+ assert_eq!(got.y, want.y, "{width}x{height} frame {i} luma vs ffmpeg");
3336+ assert_eq!(got.u, want.u, "{width}x{height} frame {i} U vs ffmpeg");
3337+ assert_eq!(got.v, want.v, "{width}x{height} frame {i} V vs ffmpeg");
3338+ }
3339+ // ... and the encoder's own reconstruction is what ffmpeg sees,
3340+ // so the check fails on a writer-side desync too, not only on a
3341+ // decoder-side one.
3342+ for (i, (frame, want)) in encoded.frames.iter().zip(&ffmpeg_frames).enumerate() {
3343+ let rec = &frame.reconstruction;
3344+ assert_eq!(rec.y, want.y, "{width}x{height} frame {i} luma: reconstruction vs ffmpeg");
3345+ assert_eq!(rec.u, want.u, "{width}x{height} frame {i} U: reconstruction vs ffmpeg");
3346+ assert_eq!(rec.v, want.v, "{width}x{height} frame {i} V: reconstruction vs ffmpeg");
3347+ }
3333 }3348 }
3334 }3349 }
3335 3350
crates/ec-av1/src/tile.rsmodified+13 -0
@@ -3600,6 +3600,19 @@ fn write_luma_tus(
3600 for tu_row in 0..n {3600 for tu_row in 0..n {
3601 for tu_col in 0..n {3601 for tu_col in 0..n {
3602 let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI));3602 let tu_mi = (mi_r + tu_row * (tx / MI), mi_c + tu_col * (tx / MI));
3603+ // lane-av1straddle: libaom clips the transform loop to
3604+ // `max_blocks_wide/high` (`mb_to_right_edge`/`mb_to_bottom_edge`,
3605+ // off the frame's true `mi_cols`/`mi_rows`), and decode.rs does
3606+ // the same (`tu_px >= y.true_width` there): a unit whose TOP-LEFT
3607+ // sample is outside the frame is NEVER coded. Writing one -- as
3608+ // this loop did for the phantom right-hand column of a 32x32 at
3609+ // x=192 in a 216-wide frame -- desyncs the tile at the very next
3610+ // symbol, which is what made an odd-size GOP's last block
3611+ // reconstruct differently and both ffmpeg decoders (libdav1d and
3612+ // libaom) refuse the whole key frame.
3613+ if tu_mi.0 >= neighbours.mi_rows || tu_mi.1 >= neighbours.mi_cols {
3614+ continue;
3615+ }
3603 let unit = if n == 1 {3616 let unit = if n == 1 {
3604 grid.to_vec()3617 grid.to_vec()
3605 } else {3618 } else {