forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-t900' (1416e926): a skipped var-tx inter block never reset its own luma coefficient contexts; 128-superblock skipped-var-tx witness gated

Browse files at this commit

08475274ad7ff2f6b0ad322bd3ba7fc4b7de1ca0Ahmet Kaan GümüşParent: f5d348d125b0a9
crates/ec-av1/fixtures/intra128_in_inter_witness.obuadded+0 -0

Binary file changed

crates/ec-av1/src/decode.rsmodified+65 -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(
@@ -7731,6 +7751,13 @@ fn decode_intra_rect_in_inter(
77317751 is consumed for square blocks only)",
77327752 ));
77337753 }
7754+ if coeff_trace_on() {
7755+ // lane-t900 r16: which decode-order frame this arm fires in --
7756+ // interleaved with the `EC_COEFF_FRAME` markers, this is what
7757+ // proves the hit is counted out of a frame that compares exact
7758+ // (class `counter-from-refused-stream`).
7759+ eprintln!("EC_INTRA128 mi_row={r} mi_col={c} bw={bw} bh={bh}");
7760+ }
77347761 INTRA_IN_INTER_MODE.with(|c| c.set(Some((3, skip))));
77357762 let res = decode_block_128rect(
77367763 dec,
@@ -12058,6 +12085,18 @@ thread_local! {
1205812085 /// (an intra-coded PARTITION_HORZ/VERT half of a 128 superblock inside an
1205912086 /// inter frame) -- (128x64, 64x128).
1206012087 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)
1206112100 }
1206212101
1206312102 /// [`INTRA128_IN_INTER_HITS`], for a gate's own before/after delta.
@@ -23462,6 +23501,25 @@ fn decode_inter_block(
2346223501 (left, above)
2346323502 });
2346423503 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+ }
2346523523 // Plane 0 is already correct per transform unit
2346623524 // ([`Neighbours::record_mi_luma`] above); this writes everything else
2346723525 // [`Neighbours::record_rect`] would.
crates/ec-av1/src/stream.rsmodified+116 -0
@@ -30637,6 +30637,122 @@ mod tests {
3063730637 );
3063830638 }
3063930639
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.
30649+ ///
30650+ /// `crates/ec-av1/fixtures/intra128_in_inter_witness.obu`, 13066 bytes,
30651+ /// sha256 `dd41fc39367a3d38bf8a8a8106dc30d91b716b2a9123fdfa4716930f4f98caf9`,
30652+ /// produced by this repository's aomenc oracle:
30653+ ///
30654+ /// ```text
30655+ /// aomenc --codec=av1 -w 512 -h 512 --input-bit-depth=10 --bit-depth=10 \
30656+ /// --sb-size=128 --min-partition-size=64 --max-partition-size=128 \
30657+ /// --enable-rect-partitions=1 --kf-max-dist=9999 --lag-in-frames=0 \
30658+ /// --cpu-used=1 --end-usage=q --cq-level=24 --threads=1 --obu -o s.obu src.y4m
30659+ /// ```
30660+ ///
30661+ /// The source is 10 frames of 10-bit 512x512 whose left two superblock
30662+ /// columns carry 128-wide bands 64 rows apart (so the 128 root's cheapest
30663+ /// split is `PARTITION_HORZ`) and whose right two carry 64-wide vertical
30664+ /// bands (`PARTITION_VERT`), with two hard scene cuts at frames 4 and 7 and
30665+ /// `--kf-max-dist=9999` so those cuts must be coded as INTER frames whose
30666+ /// content no reference holds. It is the first stream found in this
30667+ /// repository that makes the encoder code a 128-root HORZ/VERT half INTRA
30668+ /// inside an inter frame at all: `intra128_in_inter` fires 1x 128x64 +
30669+ /// 2x 64x128. Three recipes that do NOT fire it, so the shape matters: a
30670+ /// small new patch on a static background (the frame costs 80 bytes, the
30671+ /// encoder skips), unstructured texture (the 128 root always SPLITs), and
30672+ /// cyclically repeating phases (GOLDEN holds an earlier phase, so the cut
30673+ /// is predicted rather than coded intra).
30674+ ///
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.
30682+ ///
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.
30700+ ///
30701+ #[test]
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";
30704+ if !have_ffmpeg() {
30705+ eprintln!("SKIP {NAME}: no ffmpeg");
30706+ return;
30707+ }
30708+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
30709+ .join("fixtures/intra128_in_inter_witness.obu");
30710+ let stream = std::fs::read(&path)
30711+ .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display()));
30712+ let (width, height) = (512usize, 512usize);
30713+ let before = crate::decode::skip_vartx_luma_reset_hits();
30714+ let intra128_before = intra128_in_inter_counters();
30715+ let frames = match decode_stream(&stream) {
30716+ Ok(frames) => frames,
30717+ Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
30718+ };
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+ );
30735+ assert_eq!(frames.len(), 10, "{NAME}: 10 shown frames");
30736+ assert_eq!((frames[0].width, frames[0].height), (width, height));
30737+ let ffmpeg_frames = ffmpeg_decode_sequence_10bit(&stream, width, height, frames.len());
30738+ assert_eq!(
30739+ ffmpeg_frames.len(),
30740+ frames.len(),
30741+ "{NAME}: ffmpeg returned {} frames, we decoded {}",
30742+ ffmpeg_frames.len(),
30743+ frames.len()
30744+ );
30745+ for (i, (ours, theirs)) in frames.iter().zip(&ffmpeg_frames).enumerate() {
30746+ assert_eq!(ours.y, theirs.y, "{NAME}: frame {i} luma vs ffmpeg");
30747+ assert_eq!(ours.u, theirs.u, "{NAME}: frame {i} U vs ffmpeg");
30748+ assert_eq!(ours.v, theirs.v, "{NAME}: frame {i} V vs ffmpeg");
30749+ }
30750+ eprintln!(
30751+ "{NAME}: 10 shown frames pixel-exact on every plane; \
30752+ skip_vartx_luma_reset={fired}"
30753+ );
30754+ }
30755+
3064030756 /// lane-t900 r10: the small-side GLOBALMV / rect warp-reach witness.
3064130757 /// `crates/ec-av1/fixtures/gm_small_side_witness.obu`, 1158066 bytes,
3064230758 /// sha256 `f1cdbcbb8d6d469c7957d70252ab42b24ab75f48a717600ffa25991458775dca`: