From 589db238f942dff20f3c5c0552bd1b7b4ea7e5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 29 Sep 2026 03:56:45 +0300 Subject: [PATCH] ec-av1: clip a lossless block's TX grid at the frame edge A lossless block's transform-unit grid was built as `side / MI` square, unbounded by the frame. libaom bounds it: `max_block_high` / `max_block_wide` (av1_common_int.h:1565, :1579) fold `mb_to_bottom_edge` / `mb_to_right_edge` into `block_size_high/wide` before `>> MI_SIZE_LOG2`, and `decode_token_recon_block` then caps every mu chunk with `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y)` -- so the units of an overhanging block that fall outside the frame are never read. The var-tx branch of `read_block_tx_size` twenty lines below already applied exactly this clip (`max_w_mi`/`max_h_mi`); the lossless branch, and the same branch of `read_block_tx_size_rect`, did not. Mechanism, measured on the pinned 128x96 4:4:4 lossless stream (96 is not a multiple of the 128 superblock, so each of its five inter frames carries one unsplit 128 root overhanging the bottom by 32 px). The frame is 24 mi tall, so `max_blocks_high >> 2` is 24 transform rows where the block's own `side / MI` is 32. The unclipped walk therefore read 8 rows x 32 columns = 256 transform units per inter frame that libaom never codes: 2560 all_zero reads per frame against the oracle's 2304. The `EC_SYMR` ladder put the first fork at read 71322, exactly on that row-24 boundary -- `EC_COEFF_STEP tag=all_zero` shows the oracle moving to `plane=1 bc=0 br=16` (chroma, txb_skip ctx 10) where we were still reading `plane=0` luma ctx 1. Both sides read the same `decodetxb.c:158` txb_skip CDF, so this was never the coefficient reader Agent04-2's census excluded: the walk was still in the wrong PLANE. After the fix the ladder is 235940 reads on both sides with zero fork, and all six frames match the oracle `aomdec --rawvideo` in decode order exactly (the unclipped tree was wrong from frame 1: 5596 samples at the first differing byte 16544, growing to 33561 by frame 5). The clip is a no-op on any block that fits, which is what keeps every other lossless and 4:2:0 pin byte-identical; the gate's control arm pins that direction (both counters must read 0 on a stream whose blocks all fit). Gate: `a_lossless_block_clips_its_transform_grid_at_the_frame_edge`. Non-vacuous -- `lossless_edge_clipped()` fires 5 times (once per 128 root) and reads 0 on a frame that fits, `lossless_edge_clip_units()` reads exactly 1280 = 5 x 256, and every sample of every frame is asserted against the oracle. Mutation proof, run: reverting the clip to the unclipped grid turns it RED at the counter assertion (0/5 roots), and the same reverted build is 5596..33561 samples off the oracle. --- crates/ec-av1/src/decode.rs | 81 ++++++++++++++++++++++-- crates/ec-av1/src/stream.rs | 122 ++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 6 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 68e40e81..6e438f9b 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -27565,9 +27565,37 @@ fn read_block_tx_size( // `read_block_tx_size` (`decodeframe.c`) sends a lossless block down the // `read_tx_size` arm, which returns TX_4X4 before it reads anything. if lossless(fctx) { - let mut leaves = Vec::new(); - for row in 0..side / MI { - for col in 0..side / MI { + // lane-av1lm444loss: libaom CLIPS the transform-unit grid at the frame + // edge -- `max_block_high`/`max_block_wide` (`av1_common_int.h:1565`, + // `:1579`) fold `mb_to_bottom_edge`/`mb_to_right_edge` into + // `block_size_high/wide` before `>> MI_SIZE_LOG2`, so a block that + // hangs off the bottom/right codes only the units INSIDE the frame. + // `decode_token_recon_block`'s mu walk then bounds every chunk with + // `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + row, + // max_blocks_high), ss_y)`, so the out-of-frame units are never read. + // + // The unclipped `side / MI` grid is correct arithmetic for a block that + // FITS, which is every block of a frame whose height and width are + // multiples of the superblock -- so this reads identically on all of + // those, and only differs on the partial edge block. Measured on the + // 128x96 4:4:4 lossless pin (one 128 root per inter frame, frame 32 + // mi = 24 tx rows tall): the unclipped grid read 32 rows where the + // oracle read 24 -- 256 extra luma units per inter frame (1024 vs + // the oracle's 768), the `EC_SYMR` ladder's first fork landing exactly + // at that row-24 boundary (`plane=0` luma where the oracle had moved + // to `plane=1`), and the pixel diff growing 5596 -> 33561 wrong + // samples over the six frames. + let side_mi = side / MI; + let max_w_mi = side_mi.min(mi_cols.saturating_sub(at_mi.1)); + let max_h_mi = side_mi.min(mi_rows.saturating_sub(at_mi.0)); + if max_w_mi != side_mi || max_h_mi != side_mi { + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); + LOSSLESS_EDGE_CLIP_UNITS + .with(|c| c.set(c.get() + (side_mi * side_mi - max_w_mi * max_h_mi))); + } + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); + for row in 0..max_h_mi { + for col in 0..max_w_mi { leaves.push((row, col, 4, 4)); } } @@ -28047,9 +28075,24 @@ fn read_block_tx_size_rect( fctx: &crate::decode::FrameCtx, ) -> Result>> { if lossless(fctx) { - let mut leaves = Vec::new(); - for row in 0..bh / MI { - for col in 0..bw / MI { + // lane-av1lm444loss: the same frame-edge clip as the square builder + // above -- `max_block_high`/`max_block_wide` bound a rect block's + // transform grid by the part of it inside the frame, so a 1:4 strip + // hanging off the bottom codes only its in-frame rows. Identical + // arithmetic for a block that fits. + let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(at_mi.1)); + let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(at_mi.0)); + let full_w_mi = bw / MI; + let full_h_mi = bh / MI; + if max_w_mi != full_w_mi || max_h_mi != full_h_mi { + LOSSLESS_EDGE_CLIPPED.with(|c| c.set(c.get() + 1)); + LOSSLESS_EDGE_CLIP_UNITS.with(|c| { + c.set(c.get() + (full_w_mi * full_h_mi - max_w_mi * max_h_mi)) + }); + } + let mut leaves = Vec::with_capacity(max_w_mi * max_h_mi); + for row in 0..max_h_mi { + for col in 0..max_w_mi { leaves.push((row, col, 4, 4)); } } @@ -32310,6 +32353,19 @@ thread_local! { /// 128x96 4:4:4 lossless stream, 1536 per inter frame (768 per plane), /// against the oracle's 1536. pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1lm444loss: lossless blocks whose transform-unit grid was + /// CLIPPED at the frame edge -- `max_block_high`/`max_block_wide` cut the + /// grid short of the block's own `side / MI`, so libaom codes fewer TX_4X4 + /// units than the block is wide/tall. Zero on any frame whose height and + /// width are multiples of the superblock, so a gate cannot look armed by + /// merely decoding. + pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// lane-av1lm444loss: the transform units that clip REMOVED -- the + /// `(side / MI - clipped) * (side / MI)` overflow the pre-fix walk read + /// past the frame bottom/right edge. On the 128x96 4:4:4 lossless pin this + /// is 256 per inter frame (8 unclipped luma rows x 32 columns), and 0 on a + /// frame that fits. + pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// [`MU_CHUNK_WALKS`], for a gate's own before/after delta. @@ -32329,6 +32385,19 @@ pub(crate) fn mu_chunk_walk_units() -> usize { pub(crate) fn mu_chunk_order_hits() -> usize { MU_CHUNK_ORDER_HITS.with(std::cell::Cell::get) } +/// [`LOSSLESS_EDGE_CLIPPED`] -- lossless blocks whose TX grid the frame-edge +/// clip shortened, for a gate's own before/after delta. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn lossless_edge_clipped() -> usize { + LOSSLESS_EDGE_CLIPPED.with(std::cell::Cell::get) +} + +/// [`LOSSLESS_EDGE_CLIP_UNITS`] -- transform units the clip removed, i.e. the +/// ones the unclipped pre-fix walk read past the frame edge. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn lossless_edge_clip_units() -> usize { + LOSSLESS_EDGE_CLIP_UNITS.with(std::cell::Cell::get) +} /// [`CHROMA_SPLIT_TX_HITS`], for a gate's own before/after delta. #[allow(dead_code)] // read only from the `#[cfg(test)]` gates diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8a53d0e9..04c7b28a 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6858,6 +6858,128 @@ pub(crate) mod tests { } } + /// lane-av1lm444loss: a LOSSLESS block's transform-unit grid is clipped at + /// the FRAME edge, exactly as libaom's `max_block_high`/`max_block_wide` + /// clip it (`av1_common_int.h:1565`, `:1579` fold `mb_to_bottom_edge` / + /// `mb_to_right_edge` into `block_size_high/wide` before `>> + /// MI_SIZE_LOG2`), so a block hanging off the bottom/right codes only the + /// units inside the frame. `decode_token_recon_block` then bounds every + /// mu chunk with `unit_height = ROUND_POWER_OF_TWO(AOMMIN(mu_blocks_high + /// + row, max_blocks_high), ss_y)`, so the overflow units are never read + /// there either. + /// + /// Fixture is the sibling [`a_lossless_444_128_root_lossless_stream_reads_chunks_chunk_major`] + /// pin -- 128x96 yuv444p 4:4:4 lossless, 6 frames, 63429 bytes -- because + /// 96 is NOT a multiple of the 128 superblock: its five inter frames each + /// carry one unsplit 128 root that overhangs the bottom by 32 px. That is + /// the whole reason this stream is the witness. The frame is 24 mi tall, + /// so `max_blocks_high >> 2` is 24 transform rows where the block's own + /// `side / MI` is 32. + /// + /// The three assertions are all non-vacuous and all EXACT: + /// + /// 1. `lossless_edge_clipped()` fired -- 5, one per inter frame's 128 + /// root. It is 0 on any frame whose height and width are multiples of + /// the superblock, so a stream that merely decodes cannot satisfy it. + /// 2. `lossless_edge_clip_units()` is 1280 = 5 x 256: the 8 transform rows + /// x 32 columns the unclipped walk read past the frame bottom, per + /// inter frame. The pre-fix walk read 2560 units per inter frame + /// against the oracle's 2304, and this is that 256. + /// 3. every sample of every frame matches the oracle `aomdec + /// --rawvideo` in decode order -- all six frames, hidden alt-ref + /// included, which the pre-fix tree failed from frame 1 on + /// (5596 wrong samples there, growing to 33561 by frame 5). + /// + /// The control arm pins the other direction of the invariant: the + /// `ll444_minp64_128root_control` stream has no overhanging root, so its + /// clip counters must both read 0 -- the clip cannot fire on a block that + /// fits, which is what keeps every other lossless pin byte-identical. + #[test] + fn a_lossless_block_clips_its_transform_grid_at_the_frame_edge() { + const NAME: &str = "a_lossless_block_clips_its_transform_grid_at_the_frame_edge"; + const W: usize = 128; + const H: usize = 96; + const FRAMES: usize = 6; + /// 5 inter frames, one unsplit 128 root each; frame 0 splits. + const ROOTS: usize = 5; + /// `max_blocks_high >> 2` = 24 rows (96 px / 4), against the block's + /// own `side / MI` = 32 -- so 8 rows x 32 columns = 256 units per root + /// were read past the frame bottom before the clip. + const CLIP_UNITS_PER_ROOT: usize = 256; + + let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/ll444_128root_lossless.obu"); + let stream = read_pin(&obu, 63429, 0xf07d_47fc_fd51_2658, NAME); + let _guard = lock_gate_counters(); + let (c0, u0) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + let frames = decode_stream(&stream) + .unwrap_or_else(|e| panic!("{NAME}: the pinned stream no longer decodes cleanly: {e}")); + let (c1, u1) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + assert_eq!( + c1 - c0, + ROOTS, + "{NAME}: the frame-edge clip fired on {}/{} of the 128 roots -- it must be 0 \ + on a frame that fits, so this is what makes the gate non-vacuous", + c1 - c0, + ROOTS + ); + assert_eq!( + u1 - u0, + ROOTS * CLIP_UNITS_PER_ROOT, + "{NAME}: the clip removed {}/{} transform units; the unclipped walk read {} \ + per inter frame past the 96 px frame bottom (8 rows x 32 columns)", + u1 - u0, + ROOTS * CLIP_UNITS_PER_ROOT, + CLIP_UNITS_PER_ROOT + ); + assert_eq!(frames.len(), FRAMES, "{NAME}: frame count"); + for f in &frames { + assert_eq!((f.width, f.height), (W, H), "{NAME}: dimensions"); + } + if aomdec_path().is_file() { + assert_rawvideo_matches(&obu, &stream, NAME, FRAMES); + } else { + eprintln!( + "SKIP {NAME} aomdec arm: no oracle aomdec at {}", + aomdec_path().display() + ); + } + + // Control: a stream whose roots all fit must not clip at all. + let ctrl = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("fixtures/ll444_minp64_128root_control.obu"); + let ctrl_stream = read_pin( + &ctrl, + 61494, + 0xf6f5_a3cb_efc7_eb10, + &format!("{NAME} control"), + ); + let (c2, u2) = ( + crate::decode::lossless_edge_clipped(), + crate::decode::lossless_edge_clip_units(), + ); + let _ = decode_stream(&ctrl_stream) + .unwrap_or_else(|e| panic!("{NAME}: the control arm no longer decodes cleanly: {e}")); + assert_eq!( + crate::decode::lossless_edge_clipped() - c2, + 0, + "{NAME} control: the clip fired on a stream whose blocks all fit -- the clip \ + must be a no-op there, which is what keeps every other lossless pin \ + byte-identical" + ); + assert_eq!( + crate::decode::lossless_edge_clip_units() - u2, + 0, + "{NAME} control: the clip removed units from a stream whose blocks all fit" + ); + } + /// lane-av1llpredgate2: the debug-exact pin for the ss-aware /// `obmc_skip_chroma_above` decision (lane-av1-llpred2). Fixture is the /// llpred/llpred2 reports' stream pinned into `fixtures/`: testsrc2