From d02638da5ff12dc601c19791293626fa92868074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 07:48:08 +0300 Subject: [PATCH] fix(av1): thread the last leaf's mode past the whole straddling quadrant write_leaf8's above_mode[c]/left_mode[r] forced write (r12) ran once per leaf inside the quadrant's leaf loop, so the first leaf's write clobbered the true external neighbour mode a second leaf of the *same* straddling 16x16 still needed to read on its non-adjacency axis. Move the write out of write_leaf8 to run once, after the whole leaf loop, from the last (bottom/right-most) leaf. a_single_axis_straddle_round_trips_through_ffmpeg now decodes clean; un-ignored. Lifted the RD-cost guard that kept leaf8 splits from competing on cost (now decoder-proven); full gate 136/0/9. --- crates/ec-av1/src/encode.rs | 56 +++++++++++++++++++++++++++++-------- crates/ec-av1/src/tile.rs | 27 ++++++++++++------ 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 53536db9..763dc6cb 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1820,13 +1820,13 @@ pub(crate) fn encode_key_frame_inner( // A split whose subs are all whole 16x16 is a real quality // alternative to `whole`, exactly as before; a split - // carrying an 8x8-leaf sub is only ever forced (`!whole_ - // legal`), never chosen on cost -- the leaf path is new this - // round and not yet proven against a real decoder (see the - // r7 handoff), so it must not regress a size that already - // had a legal, working whole-32x32 alternative. - let has_leaf8 = split.iter().any(|b| b.eight.is_some()); - if !whole_legal || (split_blocks && !has_leaf8 && cost_split < cost_whole) { + // carrying an 8x8-leaf sub was, through r14, only ever + // forced (`!whole_legal`), never chosen on cost, because the + // leaf path was not yet proven against a real decoder. r15 + // closed that gap (`a_single_axis_straddle_round_trips_ + // through_ffmpeg` decodes clean), so the guard is lifted: + // leaf8 splits now compete on cost like any other split. + if !whole_legal || (split_blocks && cost_split < cost_whole) { modes.extend_from_slice(&split_modes); blocks.push(Quadrant::Split(split)); } else { @@ -3322,6 +3322,31 @@ mod tests { ); } + /// The r15 fix at production size: three 640x360 key frames (inter is + /// still refused at this size, per the test above, so this stands in for + /// the charter's "3-frame sequence" using three independent key frames of + /// shifted content), each proven decoder-clean through ffmpeg rather than + /// only encoder-clean. + #[test] + fn a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames() { + if !have_ffmpeg() { + eprintln!( + "SKIP a_640x360_half_straddle_round_trips_through_ffmpeg_across_three_frames: \ + no ffmpeg" + ); + return; + } + let (width, height) = (640usize, 360usize); + for shift in [0i64, 7, 19] { + let picture = panned_test_card(width, height, shift); + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + let decoded = ffmpeg_decode(&encoded.stream, width, height); + assert_eq!(decoded.y, encoded.reconstruction.y, "shift {shift}: luma"); + assert_eq!(decoded.u, encoded.reconstruction.u, "shift {shift}: U"); + assert_eq!(decoded.v, encoded.reconstruction.v, "shift {shift}: V"); + } + } + /// One frame of a real clip, scaled to a whole number of 32x32 blocks. fn clip_frame(clip: &str, skip: &str, width: usize, height: usize) -> Picture { let out = Command::new("ffmpeg") @@ -3929,12 +3954,19 @@ mod tests { /// transform) where the writer intends `TX_8X8` (`txs_ctx=2`), meaning /// something *before* this leaf's own `EC_TXBSKIP`/`EC_TXTYPE` still /// desyncs the stream by exactly the width of one earlier symbol -- not - /// yet isolated to a single named symbol. Ignored rather than deleted, - /// so the class stays named and the gate stays green. + /// yet isolated to a single named symbol. r15 found the last bug: the + /// header fix from r14 explained the rng skew fully, and the remaining + /// desync (still past `mi_row=4`) was `write_leaf8`'s (4) fix itself -- + /// its `above_mode[c]`/`left_mode[r]` forced write ran *inside* the leaf + /// loop, once per leaf, so the first leaf's write clobbered the true + /// external neighbour mode the second leaf of the *same* straddling + /// quadrant still needed for its non-adjacency axis (a column-straddle + /// case: two leaves share one `outer_at` column index, so leaf 1's + /// write to `left_mode[r]` overwrote the real western neighbour before + /// leaf 2 read it). Fixed by moving the write out of `write_leaf8` and + /// doing it once, after the whole leaf loop, from the last (bottom/ + /// right-most) leaf's mode. The stream now round-trips through ffmpeg. #[test] - #[ignore = "lane-av1-rect r14: leaf(0,8)'s rng now matches through several further \ - leaves after the reduced_tx_set header fix; residual desync moved past \ - mi_row=4, not yet isolated to a named symbol"] fn a_single_axis_straddle_round_trips_through_ffmpeg() { if !have_ffmpeg() { eprintln!("SKIP a_single_axis_straddle_round_trips_through_ffmpeg: no ffmpeg"); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index df87d921..4ac159ab 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -1248,6 +1248,25 @@ pub fn sb_coeff_key_frame_tile( ) }); } + // `record()`'s `above_mode`/`left_mode` + // write is a no-op at an 8x8 leaf's own + // side (`side / SUB == 0`), so the next + // 16x16 quadrant beyond this straddling + // one would otherwise see whatever stale + // mode sat here before it: force the + // write once the whole quadrant's leaves + // are done, from the last (bottom/right- + // most) leaf -- lane-av1-rect r15: doing + // this *inside* `write_leaf8`, once per + // leaf, let the first leaf's write + // clobber the true external neighbour a + // second leaf of the *same* quadrant + // still needed to read on its + // non-adjacency axis. + if let Some((_, mode)) = prev_leaf { + neighbours.above_mode[at.1] = mode; + neighbours.left_mode[at.0] = mode; + } } } } @@ -1381,14 +1400,6 @@ fn write_leaf8( mode, ); neighbours.record_mi(leaf_mi, 8, grids); - // `record()`'s `above_mode`/`left_mode` write is a no-op at this side - // (`side / SUB == 0` for an 8x8 leaf), so the next 16x16 quadrant that - // reads this outer cell -- one straddling quadrant stacked above/left of - // another, never touched by `prev_leaf`'s same-call override -- would - // otherwise see whatever stale mode sat there before this leaf: force - // the write here instead (r12 lane-av1-rect, leaf(2,8)'s mode desync). - neighbours.above_mode[c] = mode; - neighbours.left_mode[r] = mode; mode }