From 125b0a90d6400a94d37e9782195a5715ec788293 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 07:23:52 +0300 Subject: [PATCH] fix(av1): a skipped var-tx inter block never reset its own luma coefficient contexts `read_block_tx_size` hands back var-tx leaves for a skipped inter block whenever the block is a 128 root (every one of them under TX_MODE_LARGEST). `decode_inter_block` then takes its `if skip` arm, which reconstructs straight from the predictor and never enters the per-transform-unit luma loop -- and the block-level `record_split_luma_rect_mi` deliberately writes no plane 0, on the premise that the loop already did. So the block left the PREVIOUS block's luma entropy levels standing where libaom's `av1_reset_entropy_context` zeroes all three planes over the block's footprint (class early-return-skips-tail). Measured on the 10-bit 512x512 --sb-size=128 witness, decode-order frame 1: the 128x128 is_inter=1 skip=0 block at mi(32,64) read txb_skip ctx 2 where aomdec reads 1, with equal entry range on both decoders, because the skipped 128x128 block at mi(32,32) never cleared left[32..48] (ours level 1, aomdec 0). Publishing the all-zero `luma_grid` on that arm is exactly the reset; the filtered EC_TRACE_COEFF ladder (all_zero/eob/after_bases) now matches 1397/1397 steps over all 10 frames and every plane is byte-exact vs ffmpeg. Sweep: the other three `record_split_luma*` callers are the intra bodies (decode_rect_split, decode_block, decode_block_128rect); all three publish per transform unit inside their loop even when `skip`, so the inter tail was the only instance. Both inter var-tx arms share that tail. The gate that pinned this is `intra128_in_inter_witness.obu`, built last round as an intra-128-in-inter witness. r16 measured three hits of that arm in it; with the stream decoding exactly the arm fires ZERO times, so those were desync artifacts (class counter-from-refused-stream) -- the EC_INTRA128_IN_INTER opt-in therefore STAYS, still ungated, and the test is renamed and repointed at the reset it actually proves (new counter `skip_vartx_luma_reset_hits`, 66 on this stream). Rungs added: EC_ECDUMP (the above/left luma level arrays and resulting ctx at every txb_skip read, matched by the same env var on the instrumented aomdec) and EC_ECPUB (every block/TU coefficient-context publication). --- crates/ec-av1/src/decode.rs | 65 ++++++++++++++++++++--- crates/ec-av1/src/stream.rs | 103 +++++++++++++++++++----------------- 2 files changed, 112 insertions(+), 56 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ad16d014..9c49282f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -5692,6 +5692,16 @@ impl Neighbours { left |= self.left[mi_r + cell][0].level; } let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)]; + if std::env::var_os("EC_ECDUMP").is_some() { + let ab: Vec = + (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect(); + let lf: Vec = + (0..h_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect(); + eprintln!( + "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},]", + ab.join(","), lf.join(",") + ); + } if std::env::var_os("EC_AV1_TRACE").is_some() { eprintln!( "TRACE luma_skip_ctx mi=({mi_r},{mi_c}) side_mi={side_mi} top={top} left={left} ctx={ctx} band={:?}", @@ -6036,6 +6046,9 @@ impl Neighbours { let (mi_r, mi_c) = at_mi; let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); let (w_mi, h_mi) = (w / MI, h / MI); + if std::env::var_os("EC_ECPUB").is_some() { + eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level); + } for cell in 0..h_mi { if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) { *slot = h; @@ -6141,6 +6154,9 @@ impl Neighbours { grid: &[i32], ) { let state = neighbour_state(grid); + if std::env::var_os("EC_ECPUB").is_some() { + eprintln!("EC_ECPUB tu mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={}", state.level); + } for cell in 0..h_px / MI { if mi_r + cell < self.mi_rows { self.left[mi_r + cell][0] = state; @@ -7717,13 +7733,17 @@ fn decode_intra_rect_in_inter( // palette (>64) do not exist at this size and are refused by name there. // lane-inter128intra r1: OPT-IN ONLY (`EC_INTRA128_IN_INTER=1`) until a // gate exists. The body below is believed right (it is the key frame's own - // proven body plus the three inter-frame differences), but the three 1080p - // cuts that reach this shape reach it AFTER a pre-existing desync (see the - // lane report: their frame 2 diverges from aomdec inside the superblock at - // mi(0,320), long before mi(128,416)), and no aomenc recipe found so far - // emits a 128-root HORZ/VERT half coded intra in an inter frame -- so - // nothing can currently prove this arm. A refusal is lifted only with a - // gate (COMMON), so the default path still refuses by name below. + // proven body plus the three inter-frame differences), but nothing proves + // it: no aomenc recipe found so far emits a 128-root HORZ/VERT half coded + // intra in an inter frame. A refusal is lifted only with a gate (COMMON), + // so the default path still refuses by name below. + // + // lane-t900 r17: the opt-in STAYS. `intra128_in_inter_witness.obu` was + // built as this arm's witness and r16 measured three hits in it -- but + // every one of them was an artifact of the skipped-128-block entropy + // desync fixed this round (class `counter-from-refused-stream`). With the + // stream decoding byte-exact the arm fires ZERO times, so the witness does + // not carry the shape at all and the lift has no gate. if bw.max(bh) == 128 && std::env::var_os("EC_INTRA128_IN_INTER").is_some() { if allow_screen_content_tools { return Err(unsupported( @@ -12065,6 +12085,18 @@ thread_local! { /// (an intra-coded PARTITION_HORZ/VERT half of a 128 superblock inside an /// inter frame) -- (128x64, 64x128). pub(crate) static INTRA128_IN_INTER_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; + /// lane-t900 r17: skipped INTER blocks whose `read_block_tx_size` still + /// handed back var-tx leaves, so the per-transform-unit luma publication + /// loop never ran and the block has to clear its own luma coefficient + /// contexts (libaom `av1_reset_entropy_context`). Every 128 root under + /// `TX_MODE_LARGEST` is one; the counter is what keeps that reset's gate + /// non-vacuous. + pub(crate) static SKIP_VARTX_LUMA_RESET_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta. +pub fn skip_vartx_luma_reset_hits() -> usize { + SKIP_VARTX_LUMA_RESET_HITS.with(std::cell::Cell::get) } /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta. @@ -23469,6 +23501,25 @@ fn decode_inter_block( (left, above) }); if vartx_leaves.is_some() { + // lane-t900 r17 root cause (class early-return-skips-tail): the + // "plane 0 is already correct per transform unit" premise below holds + // only when the per-TU luma loop RAN. A SKIP inter block takes the + // `if skip` arm above, which reconstructs straight from the predictor + // and never enters that loop -- yet `read_block_tx_size` still hands + // back four TX_64X64 leaves for a 128 root under `TX_MODE_LARGEST` + // (and for any block under it), so `vartx_leaves` is `Some` and the + // luma coefficient contexts were left at the PREVIOUS block's values. + // libaom calls `av1_reset_entropy_context` for every skipped block, + // zeroing all three planes over its own footprint; `luma_grid` is the + // all-zero grid on this arm, so publishing it is exactly that reset. + // Witnessed on a 10-bit 512x512 --sb-size=128 stream: the 128x128 + // block at mi(32,64) of decode-order frame 1 read `txb_skip_ctx` 2 + // where aomdec reads 1, because the skipped 128x128 block at + // mi(32,32) never cleared `left[32..48]`. + if skip { + SKIP_VARTX_LUMA_RESET_HITS.with(|c| c.set(c.get() + 1)); + neighbours.record_mi_luma_rect(at, write_w, write_h, &luma_grid); + } // Plane 0 is already correct per transform unit // ([`Neighbours::record_mi_luma`] above); this writes everything else // [`Neighbours::record_rect`] would. diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 17b119d0..32b93a18 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -30637,10 +30637,15 @@ mod tests { ); } - /// lane-t900 r16: the intra-coded 128x64 / 64x128 INTER-frame witness -- - /// IGNORED, and it is the `EC_INTRA128_IN_INTER` opt-in's blocker, not its - /// lift. Run it with - /// `EC_INTRA128_IN_INTER=1 cargo test -p ec-av1 intra_coded_128x64 -- --ignored`. + /// lane-t900 r16/r17: a 10-bit 512x512 `--sb-size=128` stream in which + /// every 128 root is a var-tx block, and the gate for the SKIPPED-128-block + /// luma entropy reset (r17). It was BUILT as an intra-128-in-inter witness + /// and r16 measured three hits of that arm in it -- every one of them an + /// artifact of the desync fixed here (class `counter-from-refused-stream`). + /// Decoding exactly, the arm fires ZERO times, so this stream does not + /// carry that shape and the `EC_INTRA128_IN_INTER` opt-in stays. Renamed + /// out of `an_intra_coded_128x64_block_inside_an_inter_frame_...` for that + /// reason -- the fixture keeps the original intent in its own name. /// /// `crates/ec-av1/fixtures/intra128_in_inter_witness.obu`, 13066 bytes, /// sha256 `dd41fc39367a3d38bf8a8a8106dc30d91b716b2a9123fdfa4716930f4f98caf9`, @@ -30667,42 +30672,35 @@ mod tests { /// cyclically repeating phases (GOLDEN holds an earlier phase, so the cut /// is predicted rather than coded intra). /// - /// It does NOT yet lift the opt-in, because the three hits land in a frame - /// this decoder does not decode exactly, and a counter read out of a - /// desynced frame proves nothing (class `counter-from-refused-stream`). - /// The blocking defect is measured, separate, and NOT this arm -- the same - /// source re-encoded with `--enable-intrabc=0 --enable-palette=0 - /// --enable-obmc=0 --enable-warped-motion=0 --enable-global-motion=0 - /// --enable-interintra-comp=0 --enable-masked-comp=0 - /// --enable-filter-intra=0 --enable-ab-partitions=0 - /// --enable-1to4-partitions=0` fires the arm 0 times and desyncs on - /// exactly the same frames, and the same source at `--sb-size=64` decodes - /// all 10 frames byte-exact. First-bad, measured against ffmpeg and - /// against this repository's instrumented aomdec: + /// r16 could not lift the opt-in: the three hits landed in a frame this + /// decoder did not decode exactly, and a counter read out of a desynced + /// frame proves nothing (class `counter-from-refused-stream`). The blocker + /// was a SEPARATE defect on the inter path, not this arm -- the same source + /// re-encoded with every optional tool disabled fired the arm 0 times and + /// desynced on exactly the same frames, and the same source at + /// `--sb-size=64` decoded all 10 frames byte-exact. /// - /// * pixels: decode-order frame 1, luma (row 122, col 354), 160285 - /// differing samples (Y 146713 / U 5936 / V 7636), max |delta| 738. - /// Frames 0, 4 and 7 -- the key frame and the two all-intra scene cuts -- - /// are byte-exact on every plane; every other frame is not. - /// * entropy (`EC_TRACE_COEFF` on both decoders, prefiltered to the - /// `all_zero`/`eob`/`after_bases` tags): frame 1, the 128x128 - /// `is_inter=1 skip=0` block at mi(32,64) (pixel 128,256), its FIRST - /// luma TU (`bc=0 br=0`, a TX_64X64 unit, `coding.side=32`). aomdec - /// reads `txb_skip` ctx 1 (`top == 0 && left == 0`) and gets - /// `all_zero=1`; we read ctx 2 and get `all_zero=0`. Entry range is - /// equal on both, so this is the txb_skip CONTEXT -- i.e. the luma - /// entropy levels the PRECEDING 128x128 inter block published into - /// `above`/`left` -- and not the symbol's own alphabet. - /// * `EC_TRACE_MODE` confirms the same point from the other side: the - /// first differing `EC_MODE` range is at the ENTRY of mi(32,96), i.e. - /// inside mi(32,64); every 128-root block before it matches exactly. + /// r17 root cause, and what this gate now pins (class + /// `early-return-skips-tail`): a SKIPPED inter block + /// whose `read_block_tx_size` still hands back var-tx leaves -- which every + /// 128 root does under `TX_MODE_LARGEST` -- took `decode_inter_block`'s + /// `if skip` arm, so the per-transform-unit luma publication loop never + /// ran, and the block-level `record_split_luma_rect_mi` deliberately writes + /// no plane 0. The skipped block therefore left the PREVIOUS block's luma + /// coefficient levels standing where libaom's `av1_reset_entropy_context` + /// zeroes all three planes over the block. First-bad before the fix + /// (`EC_TRACE_COEFF` on both decoders, prefiltered to the + /// `all_zero`/`eob`/`after_bases` tags, filtered index 426): frame 1, the + /// 128x128 `is_inter=1 skip=0` block at mi(32,64), its first luma TU + /// (`bc=0 br=0`, TX_64X64) -- aomdec `txb_skip` ctx 1 (`top == 0 && + /// left == 0`), ours ctx 2, entry range equal on both, because the skipped + /// 128x128 block at mi(32,32) never cleared `left[32..48]` (ours 1, + /// aomdec 0). With the publication restored the whole 10-frame filtered + /// ladder matches 1397/1397 steps. /// - /// The arm's own three hits are at mi(8,24) 128x64 and mi(16,16) / - /// mi(16,20) 64x128 (`EC_INTRA128` trace, decode.rs). - #[ignore = "lane-t900 r16: needs EC_INTRA128_IN_INTER=1, and the stream carries a separate 128x128-inter txb_skip-context desync at frame 1 mi(32,64)"] #[test] - fn an_intra_coded_128x64_block_inside_an_inter_frame_decodes_pixel_exact() { - const NAME: &str = "an_intra_coded_128x64_block_inside_an_inter_frame_decodes_pixel_exact"; + fn a_128_superblock_stream_whose_skipped_blocks_are_var_tx_decodes_pixel_exact() { + const NAME: &str = "a_128_superblock_stream_whose_skipped_blocks_are_var_tx_decodes_pixel_exact"; if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; @@ -30712,20 +30710,28 @@ mod tests { let stream = std::fs::read(&path) .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); let (width, height) = (512usize, 512usize); - let before = intra128_in_inter_counters(); + let before = crate::decode::skip_vartx_luma_reset_hits(); + let intra128_before = intra128_in_inter_counters(); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => panic!("{NAME}: decode_stream refused: {e}"), }; - let now = intra128_in_inter_counters(); - let fired = [now[0] - before[0], now[1] - before[1]]; - for (what, n) in [("128x64", fired[0]), ("64x128", fired[1])] { - assert!( - n > 0, - "{NAME}: an intra {what} block on the inter path never fired -- \ - the gate would be vacuous for it" - ); - } + let fired = crate::decode::skip_vartx_luma_reset_hits() - before; + assert!( + fired > 0, + "{NAME}: no skipped var-tx block cleared its own luma coefficient \ + contexts -- the gate would be vacuous for the defect it pins" + ); + let intra128_now = intra128_in_inter_counters(); + assert_eq!( + [ + intra128_now[0] - intra128_before[0], + intra128_now[1] - intra128_before[1] + ], + [0, 0], + "{NAME}: the intra-128-in-inter arm fired on a stream that decodes \ + exactly -- the opt-in now HAS a witness and should be lifted" + ); assert_eq!(frames.len(), 10, "{NAME}: 10 shown frames"); assert_eq!((frames[0].width, frames[0].height), (width, height)); let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len()); @@ -30743,8 +30749,7 @@ mod tests { } eprintln!( "{NAME}: 10 shown frames pixel-exact on every plane; \ - intra128_in_inter 128x64={} 64x128={}", - fired[0], fired[1] + skip_vartx_luma_reset={fired}" ); }