From 71a371f745212ecdf896e37fa7c666d15572e42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 28 Aug 2026 03:43:40 +0300 Subject: [PATCH] fix(av1): gate switchable_interp neighbour ctx by ref_frame, keep GOLDEN_FRAME masked resolve_interp_filter fed a block's above/left filter context straight from Neighbours regardless of which reference frame each neighbour coded -- a different-ref neighbour's filter choice was corrupting the symbol read for this block's own filter, instead of reading as unset ([3,3], the same sentinel intra neighbours already produce). Gate both above_filter and left_filter on above_ref/left_ref matching this block's ref_frame. Round 8's GOLDEN_FRAME unmask attempt with this ctx fix still produces a real mismatch: a golden gate run 20x reproduces a frame-0 (intra keyframe) luma diff against ffmpeg whenever GOLDEN_FRAME actually fires later in the same stream (seeds 57, 59, both live-reproduced). The ctx fix itself is not the cause -- decode_inter_block8 never resolves switchable_interp context at all (it refuses non-LAST_FRAME before reaching any filter code), so this bug is golden-adjacent elsewhere, unproven past that. GOLDEN_FRAME stays a named refusal; added the same EC_AV1_GATE_DUMP pin-on-mismatch pattern the CDF forwarding gate already has to the golden gate test, and captured the reproducing stream for the next round. --- crates/ec-av1/src/decode.rs | 37 +++++++++++++++++++++++++++++++------ crates/ec-av1/src/stream.rs | 12 ++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 309d2aea..378c965b 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3793,10 +3793,17 @@ fn decode_inter_block( LAST_FRAME => (ref_y, ref_u, ref_v), _ => { return Err(unsupported( - "a reference frame other than LAST_FRAME (round 7: GOLDEN_FRAME's decode \ - path exists but is masked -- a narrow +-1 MC residue on a dual-axis \ - subpel GOLDEN block, isolated to pre-deblock this round, keeps real \ - GOLDEN streams from pixel-exact)", + "a reference frame other than LAST_FRAME (round 8: GOLDEN_FRAME's decode \ + path exists but is masked -- unmasking it with a switchable_interp \ + ref_frame-gated context fix still produces a real frame-0 (intra \ + keyframe) luma mismatch against ffmpeg whenever GOLDEN_FRAME actually \ + fires downstream in the same stream, reproduced twice live \ + (seeds 57, 59; reproducing stream dumped to \ + fixtures/golden6-mismatch.obu). Root cause is not \ + the switchable_interp context (that gate is correct and kept) but \ + something GOLDEN-adjacent that corrupts an unrelated earlier frame's \ + comparison -- possibly frame-count/ordering divergence between our \ + decoder and ffmpeg once aomenc's RD picks GOLDEN, unproven past that)", )); } }; @@ -3873,14 +3880,32 @@ fn decode_inter_block( }; (mv, false) }; + // spec `get_ref_filter_type`: a neighbour's own filter only feeds + // this block's switchable_interp context when that neighbour coded + // the SAME reference frame this block is about to read -- a + // different-ref neighbour reads as "no neighbour" ([3, 3], the + // sentinel `Neighbours::new` already seeds unset slots with), + // exactly like an intra neighbour. Without this gate, a GOLDEN_FRAME + // block next to a LAST_FRAME one inherits that neighbour's filter + // choice into its own context, corrupting the symbol it reads. + let above_filter_ctx = if neighbours.above_ref[c] == ref_frame { + neighbours.above_filter[c] + } else { + [3, 3] + }; + let left_filter_ctx = if neighbours.left_ref[r] == ref_frame { + neighbours.left_filter[r] + } else { + [3, 3] + }; let (h_filter, v_filter, resolved_filter) = resolve_interp_filter( dec, cdfs, interp_fixed, enable_dual_filter, is_globalmv, - neighbours.above_filter[c], - neighbours.left_filter[r], + above_filter_ctx, + left_filter_ctx, ); block_filter = resolved_filter; globalmv_for_lf = is_globalmv; diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 93d2f15c..622f2093 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2042,6 +2042,18 @@ mod tests { } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); + // Same pin-on-mismatch pattern as the CDF forwarding gate above: + // env-gated, no cost on a normal run. + let mismatched = frames + .iter() + .zip(&ffmpeg_frames) + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); + if mismatched { + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { + std::fs::write(&path, &stream).expect("writing pinned stream"); + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); + } + } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})");