From f546cd0cd99786bc63507a4f768113bbd9da361c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 12:27:24 +0300 Subject: [PATCH] fix(av1): the loop-restoration coefficient reference must reset at every tile Spec 5.11.2 `decode_tile` resets `RefLrWiener` / `RefSgrXqd` to the midpoint filter at the top of EVERY tile. `decode.rs` declared that running state once per frame (both the key-frame and the inter tile loops), so every tile after the first decoded its Wiener/Sgrproj coefficients off the previous tile's reference. The symbol COUNT is unchanged, so entropy stayed in sync and reconstruction stayed byte-exact -- only the filtered pixels moved. On the multi-tile inter gate at seed 44, frame 0 (the key frame) had the whole right tile column wrong: luma x[128..255] y[0..91], 1330 samples, max delta 2, chroma exact. Stage ladder that named it, on the pinned 2x2-tile stream: `EC_AV1_PREFILT_DUMP` exact, `EC_AV1_POSTDEBLOCK_DUMP` exact, `EC_AV1_POSTCDEF_DUMP` exact -- leaving loop restoration as the only remaining stage. That last rung did not exist on the oracle; it was added to its `decodeframe.c` (8-bit and forced-highbitdepth safe) this round. Class: per-tile state not reset. Swept: both tile loops in `decode.rs` are the only ones, and `lr_reference` was the only spec `decode_tile` reset missing there (above context, `DeltaLF`, `CurrentQIndex` were already per-tile). `a_real_aomenc_multi_tile_inter_stream_decodes_pixel_exact` is un-ignored: 6/6 seeds, 16/16 frames pixel-exact. --- crates/ec-av1/src/decode.rs | 30 ++++++++++++++++++++++-------- crates/ec-av1/src/stream.rs | 30 +++++++++++++----------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a60c7b73..689d1897 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -16399,10 +16399,6 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( mi_rows as usize, ); let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); - let mut lr_reference = [( - crate::restoration::WienerInfo::default(), - crate::restoration::SgrprojInfo::default(), - ); 3]; // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its // own byte range and its own fresh copy of the frame's initial CDFs @@ -16430,6 +16426,17 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( let (sb_r0, sb_r1) = ((mi_row0 / SB_MI).min(sb_rows), mi_row1.div_ceil(SB_MI).min(sb_rows)); let (sb_c0, sb_c1) = ((mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols)); + // lane-t900 r19: spec 5.11.2 `decode_tile` resets `RefLrWiener` / + // `RefSgrXqd` to the midpoint filter at the top of EVERY tile, not + // once per frame. Carried across a tile boundary, the loop-restoration + // coefficients of the next tile decode off the previous tile's + // reference -- same symbol count, so entropy and reconstruction stay + // exact and only the filtered pixels of every tile after the first + // are wrong. + let mut lr_reference = [( + crate::restoration::WienerInfo::default(), + crate::restoration::SgrprojInfo::default(), + ); 3]; let mut cdfs = base_cdfs.clone(); // spec `decode_tile`: `CurrentQIndex` resets to the frame's own // `base_q_idx` at the top of every tile, not once per frame. @@ -27435,10 +27442,6 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( ); let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); let mut lr_grid = crate::restoration::RestorationGrid::new(lr, frame_width, frame_height); - let mut lr_reference = [( - crate::restoration::WienerInfo::default(), - crate::restoration::SgrprojInfo::default(), - ); 3]; // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets @@ -27460,6 +27463,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( (mi_col0 / SB_MI).min(sb_cols), mi_col1.div_ceil(SB_MI).min(sb_cols), ); + // lane-t900 r19: spec 5.11.2 `decode_tile` resets `RefLrWiener` / + // `RefSgrXqd` to the midpoint filter at the top of EVERY tile, not + // once per frame. Carried across a tile boundary, the loop-restoration + // coefficients of the next tile decode off the previous tile's + // reference -- same symbol count, so entropy and reconstruction stay + // exact and only the filtered pixels of every tile after the first + // are wrong. + let mut lr_reference = [( + crate::restoration::WienerInfo::default(), + crate::restoration::SgrprojInfo::default(), + ); 3]; let mut cdfs = base_cdfs.clone(); CURRENT_Q_IDX.with(|c| c.set(i32::from(base_q_idx))); QUANT_DELTAS.with(|c| c.set(deltas)); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 7bae2be9..86f92d02 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -28608,24 +28608,20 @@ mod tests { ); } - /// OPEN DEFECT (lane-tiles r11), which is why this arm is `#[ignore]`d - /// rather than deleted or quietly reseeded: at seed 42 this recipe - /// mismatches ffmpeg from frame 4 on, in one 32x32 block at - /// x[224..255] y[32..63] (measured with `--loopfilter-control=0`, which - /// removes the deblock spreading; with deblocking on the region grows to - /// x[221..255] y[30..69] and crosses the tile row boundary at y=64), and - /// the error then propagates through inter prediction to 12 of 16 frames. - /// Ablations that did NOT remove it: `--enable-restoration=0`, - /// `--enable-cdef=0`, `--loopfilter-control=0`. It is seed-specific - /// (seeds 47 and 48 decode 16/16 exact with two tile rows) and its - /// tile attribution is UNPROVEN: the single-tile control at seed 42 - /// re-encodes to different partitions and refuses on - /// "an inter SB-level partition type other than SPLIT", so no - /// same-content one-tile comparison exists. Reproducer: - /// `EC_AV1_GATE_DUMP= cargo test -p ec-av1 --lib - /// a_real_aomenc_multi_tile_inter_stream -- --ignored`. + /// lane-t900 r19, the defect this arm exists for, now FIXED: spec 5.11.2 + /// `decode_tile` resets `RefLrWiener`/`RefSgrXqd` (the running + /// loop-restoration coefficient reference) to the midpoint filter at the + /// top of EVERY tile; `decode.rs` declared that state once per frame, so + /// every tile after the first decoded its Wiener/Sgrproj coefficients off + /// the previous tile's reference. The symbol COUNT is unchanged, so + /// entropy and reconstruction stayed byte-exact and only the filtered + /// pixels moved -- at seed 44 frame 0 (the key frame) the whole right tile + /// column, luma x[128..255] y[0..91], 1330 samples off by at most 2, with + /// chroma exact. Ladder that named it: `EC_AV1_PREFILT_DUMP` exact, + /// `EC_AV1_POSTDEBLOCK_DUMP` exact, `EC_AV1_POSTCDEF_DUMP` exact (that + /// last rung added to the oracle's `decodeframe.c` in this round), so the + /// only remaining stage was loop restoration. #[test] - #[ignore = "open defect: seed-42 32x32 block at x[224..255] y[32..63] mismatches; tile attribution unproven"] fn a_real_aomenc_multi_tile_inter_stream_decodes_pixel_exact() { run_multi_tile_gate( "a_real_aomenc_multi_tile_inter_stream_decodes_pixel_exact",