forge
Explore
Log in

Don't you dare go Hollow

A skipped var-tx inter block never reset its own luma coefficient contexts

Browse files at this commit

125b0a90d6400a94d37e9782195a5715ec788293Ahmet Kaan GümüşParent: afb49c4
  • fix
    • a skipped var-tx inter block never reset its own luma coefficient contexts
Show full message
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.rsmodified+58 -7
@@ -5692,6 +5692,16 @@ impl Neighbours {
56925692 left |= self.left[mi_r + cell][0].level;
56935693 }
56945694 let ctx = SKIP_CONTEXTS[(top as usize).min(4)][(left as usize).min(4)];
5695+ if std::env::var_os("EC_ECDUMP").is_some() {
5696+ let ab: Vec<String> =
5697+ (0..w_mi).map(|k| self.above[mi_c + k][0].level.to_string()).collect();
5698+ let lf: Vec<String> =
5699+ (0..h_mi).map(|k| self.left[mi_r + k][0].level.to_string()).collect();
5700+ eprintln!(
5701+ "EC_ECDUMP plane=0 mi=({mi_r},{mi_c}) wh=({w_mi},{h_mi}) ctx={ctx} above=[{},] left=[{},]",
5702+ ab.join(","), lf.join(",")
5703+ );
5704+ }
56955705 if std::env::var_os("EC_AV1_TRACE").is_some() {
56965706 eprintln!(
56975707 "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 {
60366046 let (mi_r, mi_c) = at_mi;
60376047 let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane]));
60386048 let (w_mi, h_mi) = (w / MI, h / MI);
6049+ if std::env::var_os("EC_ECPUB").is_some() {
6050+ eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);
6051+ }
60396052 for cell in 0..h_mi {
60406053 if let Some(slot) = self.left_side_mi.get_mut(mi_r + cell) {
60416054 *slot = h;
@@ -6141,6 +6154,9 @@ impl Neighbours {
61416154 grid: &[i32],
61426155 ) {
61436156 let state = neighbour_state(grid);
6157+ if std::env::var_os("EC_ECPUB").is_some() {
6158+ eprintln!("EC_ECPUB tu mi=({mi_r},{mi_c}) wh=({w_px},{h_px}) lvl={}", state.level);
6159+ }
61446160 for cell in 0..h_px / MI {
61456161 if mi_r + cell < self.mi_rows {
61466162 self.left[mi_r + cell][0] = state;
@@ -7717,13 +7733,17 @@ fn decode_intra_rect_in_inter(
77177733 // palette (>64) do not exist at this size and are refused by name there.
77187734 // lane-inter128intra r1: OPT-IN ONLY (`EC_INTRA128_IN_INTER=1`) until a
77197735 // gate exists. The body below is believed right (it is the key frame's own
7720- // proven body plus the three inter-frame differences), but the three 1080p
7721- // cuts that reach this shape reach it AFTER a pre-existing desync (see the
7722- // lane report: their frame 2 diverges from aomdec inside the superblock at
7723- // mi(0,320), long before mi(128,416)), and no aomenc recipe found so far
7724- // emits a 128-root HORZ/VERT half coded intra in an inter frame -- so
7725- // nothing can currently prove this arm. A refusal is lifted only with a
7726- // gate (COMMON), so the default path still refuses by name below.
7736+ // proven body plus the three inter-frame differences), but nothing proves
7737+ // it: no aomenc recipe found so far emits a 128-root HORZ/VERT half coded
7738+ // intra in an inter frame. A refusal is lifted only with a gate (COMMON),
7739+ // so the default path still refuses by name below.
7740+ //
7741+ // lane-t900 r17: the opt-in STAYS. `intra128_in_inter_witness.obu` was
7742+ // built as this arm's witness and r16 measured three hits in it -- but
7743+ // every one of them was an artifact of the skipped-128-block entropy
7744+ // desync fixed this round (class `counter-from-refused-stream`). With the
7745+ // stream decoding byte-exact the arm fires ZERO times, so the witness does
7746+ // not carry the shape at all and the lift has no gate.
77277747 if bw.max(bh) == 128 && std::env::var_os("EC_INTRA128_IN_INTER").is_some() {
77287748 if allow_screen_content_tools {
77297749 return Err(unsupported(
@@ -12065,6 +12085,18 @@ thread_local! {
1206512085 /// (an intra-coded PARTITION_HORZ/VERT half of a 128 superblock inside an
1206612086 /// inter frame) -- (128x64, 64x128).
1206712087 pub(crate) static INTRA128_IN_INTER_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) };
12088+ /// lane-t900 r17: skipped INTER blocks whose `read_block_tx_size` still
12089+ /// handed back var-tx leaves, so the per-transform-unit luma publication
12090+ /// loop never ran and the block has to clear its own luma coefficient
12091+ /// contexts (libaom `av1_reset_entropy_context`). Every 128 root under
12092+ /// `TX_MODE_LARGEST` is one; the counter is what keeps that reset's gate
12093+ /// non-vacuous.
12094+ pub(crate) static SKIP_VARTX_LUMA_RESET_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
12095+}
12096+
12097+/// [`SKIP_VARTX_LUMA_RESET_HITS`], for a gate's own before/after delta.
12098+pub fn skip_vartx_luma_reset_hits() -> usize {
12099+ SKIP_VARTX_LUMA_RESET_HITS.with(std::cell::Cell::get)
1206812100 }
1206912101
1207012102 /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta.
@@ -23469,6 +23501,25 @@ fn decode_inter_block(
2346923501 (left, above)
2347023502 });
2347123503 if vartx_leaves.is_some() {
23504+ // lane-t900 r17 root cause (class early-return-skips-tail): the
23505+ // "plane 0 is already correct per transform unit" premise below holds
23506+ // only when the per-TU luma loop RAN. A SKIP inter block takes the
23507+ // `if skip` arm above, which reconstructs straight from the predictor
23508+ // and never enters that loop -- yet `read_block_tx_size` still hands
23509+ // back four TX_64X64 leaves for a 128 root under `TX_MODE_LARGEST`
23510+ // (and for any block under it), so `vartx_leaves` is `Some` and the
23511+ // luma coefficient contexts were left at the PREVIOUS block's values.
23512+ // libaom calls `av1_reset_entropy_context` for every skipped block,
23513+ // zeroing all three planes over its own footprint; `luma_grid` is the
23514+ // all-zero grid on this arm, so publishing it is exactly that reset.
23515+ // Witnessed on a 10-bit 512x512 --sb-size=128 stream: the 128x128
23516+ // block at mi(32,64) of decode-order frame 1 read `txb_skip_ctx` 2
23517+ // where aomdec reads 1, because the skipped 128x128 block at
23518+ // mi(32,32) never cleared `left[32..48]`.
23519+ if skip {
23520+ SKIP_VARTX_LUMA_RESET_HITS.with(|c| c.set(c.get() + 1));
23521+ neighbours.record_mi_luma_rect(at, write_w, write_h, &luma_grid);
23522+ }
2347223523 // Plane 0 is already correct per transform unit
2347323524 // ([`Neighbours::record_mi_luma`] above); this writes everything else
2347423525 // [`Neighbours::record_rect`] would.
crates/ec-av1/src/stream.rsmodified+54 -49
@@ -30637,10 +30637,15 @@ mod tests {
3063730637 );
3063830638 }
3063930639
30640- /// lane-t900 r16: the intra-coded 128x64 / 64x128 INTER-frame witness --
30641- /// IGNORED, and it is the `EC_INTRA128_IN_INTER` opt-in's blocker, not its
30642- /// lift. Run it with
30643- /// `EC_INTRA128_IN_INTER=1 cargo test -p ec-av1 intra_coded_128x64 -- --ignored`.
30640+ /// lane-t900 r16/r17: a 10-bit 512x512 `--sb-size=128` stream in which
30641+ /// every 128 root is a var-tx block, and the gate for the SKIPPED-128-block
30642+ /// luma entropy reset (r17). It was BUILT as an intra-128-in-inter witness
30643+ /// and r16 measured three hits of that arm in it -- every one of them an
30644+ /// artifact of the desync fixed here (class `counter-from-refused-stream`).
30645+ /// Decoding exactly, the arm fires ZERO times, so this stream does not
30646+ /// carry that shape and the `EC_INTRA128_IN_INTER` opt-in stays. Renamed
30647+ /// out of `an_intra_coded_128x64_block_inside_an_inter_frame_...` for that
30648+ /// reason -- the fixture keeps the original intent in its own name.
3064430649 ///
3064530650 /// `crates/ec-av1/fixtures/intra128_in_inter_witness.obu`, 13066 bytes,
3064630651 /// sha256 `dd41fc39367a3d38bf8a8a8106dc30d91b716b2a9123fdfa4716930f4f98caf9`,
@@ -30667,42 +30672,35 @@ mod tests {
3066730672 /// cyclically repeating phases (GOLDEN holds an earlier phase, so the cut
3066830673 /// is predicted rather than coded intra).
3066930674 ///
30670- /// It does NOT yet lift the opt-in, because the three hits land in a frame
30671- /// this decoder does not decode exactly, and a counter read out of a
30672- /// desynced frame proves nothing (class `counter-from-refused-stream`).
30673- /// The blocking defect is measured, separate, and NOT this arm -- the same
30674- /// source re-encoded with `--enable-intrabc=0 --enable-palette=0
30675- /// --enable-obmc=0 --enable-warped-motion=0 --enable-global-motion=0
30676- /// --enable-interintra-comp=0 --enable-masked-comp=0
30677- /// --enable-filter-intra=0 --enable-ab-partitions=0
30678- /// --enable-1to4-partitions=0` fires the arm 0 times and desyncs on
30679- /// exactly the same frames, and the same source at `--sb-size=64` decodes
30680- /// all 10 frames byte-exact. First-bad, measured against ffmpeg and
30681- /// against this repository's instrumented aomdec:
30675+ /// r16 could not lift the opt-in: the three hits landed in a frame this
30676+ /// decoder did not decode exactly, and a counter read out of a desynced
30677+ /// frame proves nothing (class `counter-from-refused-stream`). The blocker
30678+ /// was a SEPARATE defect on the inter path, not this arm -- the same source
30679+ /// re-encoded with every optional tool disabled fired the arm 0 times and
30680+ /// desynced on exactly the same frames, and the same source at
30681+ /// `--sb-size=64` decoded all 10 frames byte-exact.
3068230682 ///
30683- /// * pixels: decode-order frame 1, luma (row 122, col 354), 160285
30684- /// differing samples (Y 146713 / U 5936 / V 7636), max |delta| 738.
30685- /// Frames 0, 4 and 7 -- the key frame and the two all-intra scene cuts --
30686- /// are byte-exact on every plane; every other frame is not.
30687- /// * entropy (`EC_TRACE_COEFF` on both decoders, prefiltered to the
30688- /// `all_zero`/`eob`/`after_bases` tags): frame 1, the 128x128
30689- /// `is_inter=1 skip=0` block at mi(32,64) (pixel 128,256), its FIRST
30690- /// luma TU (`bc=0 br=0`, a TX_64X64 unit, `coding.side=32`). aomdec
30691- /// reads `txb_skip` ctx 1 (`top == 0 && left == 0`) and gets
30692- /// `all_zero=1`; we read ctx 2 and get `all_zero=0`. Entry range is
30693- /// equal on both, so this is the txb_skip CONTEXT -- i.e. the luma
30694- /// entropy levels the PRECEDING 128x128 inter block published into
30695- /// `above`/`left` -- and not the symbol's own alphabet.
30696- /// * `EC_TRACE_MODE` confirms the same point from the other side: the
30697- /// first differing `EC_MODE` range is at the ENTRY of mi(32,96), i.e.
30698- /// inside mi(32,64); every 128-root block before it matches exactly.
30683+ /// r17 root cause, and what this gate now pins (class
30684+ /// `early-return-skips-tail`): a SKIPPED inter block
30685+ /// whose `read_block_tx_size` still hands back var-tx leaves -- which every
30686+ /// 128 root does under `TX_MODE_LARGEST` -- took `decode_inter_block`'s
30687+ /// `if skip` arm, so the per-transform-unit luma publication loop never
30688+ /// ran, and the block-level `record_split_luma_rect_mi` deliberately writes
30689+ /// no plane 0. The skipped block therefore left the PREVIOUS block's luma
30690+ /// coefficient levels standing where libaom's `av1_reset_entropy_context`
30691+ /// zeroes all three planes over the block. First-bad before the fix
30692+ /// (`EC_TRACE_COEFF` on both decoders, prefiltered to the
30693+ /// `all_zero`/`eob`/`after_bases` tags, filtered index 426): frame 1, the
30694+ /// 128x128 `is_inter=1 skip=0` block at mi(32,64), its first luma TU
30695+ /// (`bc=0 br=0`, TX_64X64) -- aomdec `txb_skip` ctx 1 (`top == 0 &&
30696+ /// left == 0`), ours ctx 2, entry range equal on both, because the skipped
30697+ /// 128x128 block at mi(32,32) never cleared `left[32..48]` (ours 1,
30698+ /// aomdec 0). With the publication restored the whole 10-frame filtered
30699+ /// ladder matches 1397/1397 steps.
3069930700 ///
30700- /// The arm's own three hits are at mi(8,24) 128x64 and mi(16,16) /
30701- /// mi(16,20) 64x128 (`EC_INTRA128` trace, decode.rs).
30702- #[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)"]
3070330701 #[test]
30704- fn an_intra_coded_128x64_block_inside_an_inter_frame_decodes_pixel_exact() {
30705- const NAME: &str = "an_intra_coded_128x64_block_inside_an_inter_frame_decodes_pixel_exact";
30702+ fn a_128_superblock_stream_whose_skipped_blocks_are_var_tx_decodes_pixel_exact() {
30703+ const NAME: &str = "a_128_superblock_stream_whose_skipped_blocks_are_var_tx_decodes_pixel_exact";
3070630704 if !have_ffmpeg() {
3070730705 eprintln!("SKIP {NAME}: no ffmpeg");
3070830706 return;
@@ -30712,20 +30710,28 @@ mod tests {
3071230710 let stream = std::fs::read(&path)
3071330711 .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display()));
3071430712 let (width, height) = (512usize, 512usize);
30715- let before = intra128_in_inter_counters();
30713+ let before = crate::decode::skip_vartx_luma_reset_hits();
30714+ let intra128_before = intra128_in_inter_counters();
3071630715 let frames = match decode_stream(&stream) {
3071730716 Ok(frames) => frames,
3071830717 Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
3071930718 };
30720- let now = intra128_in_inter_counters();
30721- let fired = [now[0] - before[0], now[1] - before[1]];
30722- for (what, n) in [("128x64", fired[0]), ("64x128", fired[1])] {
30723- assert!(
30724- n > 0,
30725- "{NAME}: an intra {what} block on the inter path never fired -- \
30726- the gate would be vacuous for it"
30727- );
30728- }
30719+ let fired = crate::decode::skip_vartx_luma_reset_hits() - before;
30720+ assert!(
30721+ fired > 0,
30722+ "{NAME}: no skipped var-tx block cleared its own luma coefficient \
30723+ contexts -- the gate would be vacuous for the defect it pins"
30724+ );
30725+ let intra128_now = intra128_in_inter_counters();
30726+ assert_eq!(
30727+ [
30728+ intra128_now[0] - intra128_before[0],
30729+ intra128_now[1] - intra128_before[1]
30730+ ],
30731+ [0, 0],
30732+ "{NAME}: the intra-128-in-inter arm fired on a stream that decodes \
30733+ exactly -- the opt-in now HAS a witness and should be lifted"
30734+ );
3072930735 assert_eq!(frames.len(), 10, "{NAME}: 10 shown frames");
3073030736 assert_eq!((frames[0].width, frames[0].height), (width, height));
3073130737 let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len());
@@ -30743,8 +30749,7 @@ mod tests {
3074330749 }
3074430750 eprintln!(
3074530751 "{NAME}: 10 shown frames pixel-exact on every plane; \
30746- intra128_in_inter 128x64={} 64x128={}",
30747- fired[0], fired[1]
30752+ skip_vartx_luma_reset={fired}"
3074830753 );
3074930754 }
3075030755