From 5925aeeb09508b9ffed3c62442f8dca6ccd70935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 20:50:11 +0300 Subject: [PATCH] wip(rect16): plain PARTITION_HORZ/VERT below 16x16 (decode_leaf_rect, real-mi addressed); fix vacuous vert_b gate (missing --passes=1) --- crates/ec-av1/src/decode.rs | 260 ++++++++++++++++++++++++- crates/ec-av1/src/refusal_inventory.rs | 3 +- crates/ec-av1/src/stream.rs | 47 +++-- 3 files changed, 292 insertions(+), 18 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 752b14d7..a0f7c60f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -412,6 +412,22 @@ pub(crate) fn vert_b_intra_hits() -> usize { VERT_B_INTRA_HITS.with(|c| c.get()) } +// How many `partition_w16` reads resolved to a plain `PARTITION_HORZ`/ +// `PARTITION_VERT` (lane-rect16 r2): re-measuring after the VERT_B fix above +// showed mandelbrot's real first-hit blocker is this plain arm at mi=(0,7), +// not VERT_B (`refusal-names-a-correlate`; the VERT_B write-up's "frame 0's +// very first non-NONE/SPLIT symbol" claim was about decode ORDER within one +// superblock's z-order recursion, not raster mi position). +thread_local! { + static HORZ_VERT_INTRA_HITS: std::cell::Cell = + const { std::cell::Cell::new(0) }; +} + +/// Current value of [`HORZ_VERT_INTRA_HITS`]. +pub(crate) fn horz_vert_intra_hits() -> usize { + HORZ_VERT_INTRA_HITS.with(|c| c.get()) +} + // How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` // (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every // call on the current thread -- the same before/after counter pattern as @@ -3403,6 +3419,156 @@ fn decode_block_rect( Ok(()) } +/// Decodes one `bw`x`bh` rect strip of a plain 16x16-level `PARTITION_HORZ`/ +/// `PARTITION_VERT` (lane-rect16 r2), sibling to [`decode_block_rect`] but +/// addressed by REAL mi coordinates rather than derived from an [`SUB`]-grid +/// `outer_at`, the same relationship [`decode_leaf8`] has to `decode_block`: +/// the second strip of a HORZ/VERT split sits offset by half the 16x16 +/// parent, which `decode_block_rect`'s `at`-derived `px = c * SUB` can't +/// name. `prev_leaf` mirrors `decode_leaf8`'s own convention -- when the +/// previous strip is directly above (same column, `mi_h` rows up) or +/// directly left (same row, `mi_w` cols over), its mode overrides the coarse +/// per-16x16-cell `above_mode`/`left_mode` slot for THIS strip's own mode +/// context read, and bookkeeping goes through the `_mi`/`_rect` neighbour +/// writers (`record_mi_rect`, `fill_skip_grid_rect`, `fill_lf_grid_rect`) at +/// this strip's real position, never the coarse `record_rect` (which the +/// caller uses once, after both strips, exactly like the `VERT_B` arm next +/// to it). Only the skip case is ported -- see [`decode_block_rect`]'s own +/// doc for why a coded rect strip below 16x16 has no coefficient tables here +/// yet. +#[allow(clippy::too_many_arguments)] +fn decode_leaf_rect( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + outer_at: (usize, usize), + leaf_mi: (usize, usize), + bw: usize, + bh: usize, + prev_leaf: Option<((usize, usize), usize)>, + y: &mut PlaneBuf, + u: &mut PlaneBuf, + v: &mut PlaneBuf, + enable_filter_intra: bool, + allow_screen_content_tools: bool, + base_q_idx: u8, + tx_select: bool, +) -> Result { + let _ = base_q_idx; + let (r, c) = outer_at; + let mut above_mode = neighbours.above_mode[c]; + let mut left_mode = neighbours.left_mode[r]; + let (mi_w, mi_h) = (bw / MI, bh / MI); + if let Some(((pr, pc), pmode)) = prev_leaf { + if pc == leaf_mi.1 && leaf_mi.0 == pr + mi_h { + above_mode = pmode; + } else if pr == leaf_mi.0 && leaf_mi.1 == pc + mi_w { + left_mode = pmode; + } + } + let smooth_neighbor = + is_smooth_mode(above_mode) || is_smooth_mode(left_mode); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } + let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) = + read_intra_mode_rect( + dec, + cdfs, + above_mode, + left_mode, + true, + bw, + bh, + enable_filter_intra, + neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), + allow_screen_content_tools, + leaf_mi.0, + leaf_mi.1, + )?; + if filter_intra.is_some() { + return Err(unsupported( + "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", + )); + } + let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; + let depth = if tx_select { + let ctx = tx_size_context_rect(neighbours, leaf_mi, bw, bh); + dec.symbol(&mut cdfs.tx_size_cat2[ctx]) + } else { + 0 + }; + if depth != 0 { + return Err(unsupported( + "a HORZ/VERT intra strip with a split transform (per-unit rect \ + prediction is not ported)", + )); + } + if !skip { + return Err(unsupported( + "a coded (non-skip) HORZ/VERT rect strip below 16x16 (this decoder ports only \ + the skip case at this size)", + )); + } + let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); + let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); + let (cpx, cpy) = (px / 2, py / 2); + let (chroma_w, chroma_h) = (bw / 2, bh / 2); + y.reconstruct_rect( + px, + py, + bw, + bh, + mode, + angle_delta_y, + reach, + &vec![0i32; bw * bh], + None, + filter_intra, + smooth_neighbor, + ); + let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + u.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &vec![0i32; chroma_w * chroma_h], + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, + false, + ); + v.reconstruct_rect( + cpx, + cpy, + chroma_w, + chroma_h, + uv_predict_mode, + angle_delta_uv, + reach, + &vec![0i32; chroma_w * chroma_h], + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, + false, + ); + neighbours.record_mi_rect( + leaf_mi, + bw, + bh, + &[ + vec![0i32; bw * bh], + vec![0i32; chroma_w * chroma_h], + vec![0i32; chroma_w * chroma_h], + ], + ); + neighbours.fill_skip_grid_rect(leaf_mi, mi_w, mi_h, skip); + neighbours.fill_lf_grid_rect(leaf_mi, mi_w, mi_h, bw as u8, bh as u8, 0); + Ok(mode) +} + /// Decodes one true `bw`x`bh` superblock-level `PARTITION_HORZ`/ /// `PARTITION_VERT` strip (lane-sbpart r2, `bw, bh` one of `(64, 32)` / /// `(32, 64)` -- [`decode_block_rect`]'s own sibling one level up). Unlike @@ -6737,9 +6903,101 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( neighbours.left_mode[sr] = mode_br; continue; } + if part16 == PARTITION_HORZ { + HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); + let (mi_row0, mi_col0) = ( + sr as u32 * SUB_MI, + sc as u32 * SUB_MI, + ); + let top = (mi_row0 as usize, mi_col0 as usize); + let bot = (mi_row0 as usize + 2, mi_col0 as usize); + let mode_top = decode_leaf_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at16, + top, + 16, + 8, + None, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + )?; + let mode_bot = decode_leaf_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at16, + bot, + 16, + 8, + Some((top, mode_top)), + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + )?; + neighbours.above_mode[sc] = mode_bot; + neighbours.left_mode[sr] = mode_bot; + continue; + } + if part16 == PARTITION_VERT { + HORZ_VERT_INTRA_HITS.with(|c| c.set(c.get() + 1)); + let (mi_row0, mi_col0) = ( + sr as u32 * SUB_MI, + sc as u32 * SUB_MI, + ); + let left = (mi_row0 as usize, mi_col0 as usize); + let right = (mi_row0 as usize, mi_col0 as usize + 2); + let mode_left = decode_leaf_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at16, + left, + 8, + 16, + None, + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + )?; + let mode_right = decode_leaf_rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at16, + right, + 8, + 16, + Some((left, mode_left)), + &mut y, + &mut u, + &mut v, + enable_filter_intra, + allow_screen_content_tools, + base_q_idx, + tx_select, + )?; + neighbours.above_mode[sc] = mode_right; + neighbours.left_mode[sr] = mode_right; + continue; + } if part16 != PARTITION_SPLIT { return Err(unsupported( - "a partition below 16x16 other than a clean split or a VERT_B (this decoder codes only the square arms, VERT_B, and a clean split below 16x16)", + "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)", )); } // A real (non-straddle) SPLIT of a diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 56ad7a04..fb771602 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -36,7 +36,8 @@ const CAPABILITY_CLAIMS: &[&str] = &[ const REFUSALS: &[&str] = &[ "GLOBALMV (round 3)", "a coded (non-skip) HORZ_B/VERT_B rect strip below 16x16 (this decoder ports only the skip case at this size)", - "a partition below 16x16 other than a clean split or a VERT_B (this decoder codes only the square arms, VERT_B, and a clean split below 16x16)", + "a coded (non-skip) HORZ/VERT rect strip below 16x16 (this decoder ports only the skip case at this size)", + "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)", "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", "a 32x32 partition type this decoder does not code (value={part32})", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 46e5118f..2e100638 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -5208,20 +5208,25 @@ mod tests { ); } - /// lane-rect16 r1: a plain default-settings aomenc run over lavfi + /// lane-rect16 r1/r2: a plain default-settings aomenc run over lavfi /// `mandelbrot` (`--cpu-used=4 --end-usage=q --cq-level=32`, no /// `--enable-*` flags) used to refuse frame 0 outright with "a partition - /// below 16x16 other than a clean split" -- reproduced live with - /// `decode_probe`, root-caused to a `PARTITION_VERT_B` `partition_w16` - /// symbol at mi=(3,2), not the plain HORZ/VERT this lane's charter - /// assumed (`refusal-names-a-correlate`). The `PARTITION_VERT_B` arm is - /// now wired (left 8x16 real `decode_block_rect` strip + two stacked - /// 8x8 `decode_leaf8` leaves on the right); `vert_b_intra_hits` proves - /// that arm fires. This exact stream's own VERT_B block is *coded* - /// (real coefficients), which is NOT ported (see the refusal on - /// `decode_block_rect`'s non-skip else-branch) -- so this gate proves - /// the new arm fires and no longer trips the OLD (now-false) refusal, - /// not a pixel-exact decode of this stream. + /// below 16x16 other than a clean split". r1 root-caused the FIRST hit to + /// a `PARTITION_VERT_B` `partition_w16` symbol at mi=(3,2) and wired it + /// (left 8x16 real `decode_block_rect` strip + two stacked 8x8 + /// `decode_leaf8` leaves on the right). r2 re-measured after that fix + /// (`decision-before-quantiser`-shaped: don't trust a stale premise) and + /// found this stream's REAL first-hit blocker in z-order is earlier, at + /// mi=(0,7): a plain `PARTITION_HORZ`. That arm is now wired too + /// (`decode_leaf_rect`, sibling to `decode_leaf8` but real-mi-addressed + /// for a non-square strip) -- `horz_vert_intra_hits` proves it fires. + /// This exact stream's own HORZ block at mi=(0,7) is *coded* (real + /// coefficients), which is NOT ported (same ceiling as VERT_B's own + /// non-skip refusal -- no rectangular-transform coefficient tables at + /// this size), so decode still stops there and `vert_b_intra_hits` + /// never gets a chance to increment on this particular stream. This gate + /// proves the HORZ arm fires and the OLD (now-false) two-clause refusal + /// never reappears, not a pixel-exact decode of this stream. #[test] fn a_real_aomenc_stream_with_mandelbrot_fires_the_vert_b_partition_arm() { const NAME: &str = "a_real_aomenc_stream_with_mandelbrot_fires_the_vert_b_partition_arm"; @@ -5269,6 +5274,7 @@ mod tests { "--cpu-used=4", "--end-usage=q", "--cq-level=32", + "--passes=1", "--obu", "-o", "-", @@ -5292,6 +5298,14 @@ mod tests { String::from_utf8_lossy(&out.stderr) ); let stream = out.stdout; + // lane-rect16 r2: the recipe above was missing `--passes=1` -- + // without it aomenc silently ran 2-pass and wrote nothing to stdout + // on pass 1 (still exit 0), so this gate always decoded an EMPTY + // stream and never actually exercised the arm it claims to gate. + assert!( + !stream.is_empty(), + "{NAME}: aomenc wrote an empty stream (missing --passes=1?)" + ); let result = decode_stream(&stream); // The now-lifted false claim ("codes only the square arms below // 16x16") must never appear again. @@ -5303,12 +5317,13 @@ mod tests { ); } assert!( - crate::decode::vert_b_intra_hits() > 0, - "{NAME}: the PARTITION_VERT_B arm never fired on this stream (regression -- it fired \ - deterministically at mi=(3,2) when this gate was written)" + crate::decode::horz_vert_intra_hits() > 0, + "{NAME}: the plain PARTITION_HORZ/VERT arm never fired on this stream (regression -- \ + it fired deterministically at mi=(0,7) when this gate was written)" ); eprintln!( - "{NAME}: vert_b_intra_hits={}, decode result: {:?}", + "{NAME}: horz_vert_intra_hits={}, vert_b_intra_hits={}, decode result: {:?}", + crate::decode::horz_vert_intra_hits(), crate::decode::vert_b_intra_hits(), result.as_ref().map(|f| f.len()).map_err(|e| e.to_string()) );