From 603f865f3931efd4659b90268efe1271c7cd2116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 14:41:41 +0300 Subject: [PATCH] feat(av1): intra 128x64/64x128 at the 128 superblock root -- rect intra block with per-TU prediction, mu-chunk chroma, and a frame-edge write clip The key-frame path refused the 128 root's HORZ/VERT arm by name (r7). It now decodes one BLOCK_128X64 (or 64x128) per half through decode_block_128rect: two-or-more SQUARE luma transform units (max_txsize_rect_lookup caps at TX_64X64), one 32x32 chroma unit per 64x64 mu chunk per plane on the offset-10 txb_skip rows, per-TU prediction in raster order, one cdef_idx per half fanned over the CDEF units it covers. PlaneBuf::reconstruct/reconstruct_rect now clip at the padded frame edge: our buffer is 32-aligned while libaom's is superblock aligned, and a 64x64 unit of a bottom-edge 128x64 block indexed one row past a 1080p buffer -- two panics on real 10-bit 1920x792 extracts. Gate: a_real_aomenc_sb128_intra_rect_block_decodes_pixel_exact, 5 real aomenc streams (8+10 bit, HORZ and VERT, cq 8..55), every decode-order frame pixel-exact vs aomdec, 96 intra 128-rect blocks. --- crates/ec-av1/src/decode.rs | 407 ++++++++++++++++++++++++- crates/ec-av1/src/refusal_inventory.rs | 2 +- crates/ec-av1/src/stream.rs | 177 +++++++++++ lanes/sb128c-r8.report.md | 114 +++++++ 4 files changed, 691 insertions(+), 9 deletions(-) create mode 100644 lanes/sb128c-r8.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 35d8f9e3..2a22fee8 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -9245,7 +9245,16 @@ impl PlaneBuf { ); } for row in 0..bh { + // Same bottom/right frame-edge clip as [`Self::reconstruct`]'s + // (lane-sb128c r8) -- a 64x128 block at the right edge of a frame + // whose padded width is not superblock aligned hangs past it. + if y + row >= self.height { + break; + } for col in 0..bw { + if x + col >= self.width { + break; + } let idx = row * bw + col; let mut base = i32::from(prediction[idx]); if let Some((alpha_q3, ac_q3)) = cfl { @@ -9338,7 +9347,21 @@ impl PlaneBuf { ); } for row in 0..side { + // lane-sb128c r8: libaom's frame buffer is superblock + // aligned; ours is padded to 32 (`block_grid`), so a 64x64 + // transform unit of a 128x64 block at the BOTTOM frame edge + // can hang past the last padded row (1920x792: a unit at + // y=768 wants 832 rows of an 800-row buffer, which panicked + // the film's key frames). Those samples are outside the + // frame: never output, and no block below ever references + // them. + if y + row >= self.height { + break; + } for col in 0..side { + if x + col >= self.width { + break; + } let idx = row * side + col; let mut base = i32::from(prediction[idx]); if let Some((alpha_q3, ac_q3)) = cfl { @@ -10099,6 +10122,307 @@ fn decode_block( Ok(()) } +thread_local! { + /// lane-sb128c r8: INTRA (key-frame) 128x64 blocks decoded at the 128 + /// root's HORZ arm, and 64x128 ones at its VERT arm. + pub(crate) static INTRA_SB128_HORZ_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + pub(crate) static INTRA_SB128_VERT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// Those blocks by resolved intra `tx_depth` (0 = TX_64X64 units, + /// 1 = TX_32X32, 2 = TX_16X16) -- proof the depth symbol is really read. + pub(crate) static INTRA_SB128_DEPTH_HITS: std::cell::Cell<[usize; 3]> = const { std::cell::Cell::new([0; 3]) }; +} + +/// [`INTRA_SB128_HORZ_HITS`] / [`INTRA_SB128_VERT_HITS`] / +/// [`INTRA_SB128_DEPTH_HITS`], for a gate's own before/after delta. +pub(crate) fn intra_sb128_hits() -> (usize, usize, [usize; 3]) { + ( + INTRA_SB128_HORZ_HITS.with(std::cell::Cell::get), + INTRA_SB128_VERT_HITS.with(std::cell::Cell::get), + INTRA_SB128_DEPTH_HITS.with(std::cell::Cell::get), + ) +} + +/// The 128 root's HORZ/VERT arm on an INTRA frame: ONE 128x64 (HORZ) or +/// 64x128 (VERT) intra block (lane-sb128c r8; the inter twin is +/// `decode_inter_block`'s own rect path). +/// +/// libaom `decode_block` -> `predict_and_reconstruct_intra_block` per +/// transform unit, with the 128-specific geometry: +/// * `max_txsize_rect_lookup[BLOCK_128X64]` is TX_64X64, so every luma unit +/// is SQUARE and there are at least two of them -- depth 0: 64x64 x2, +/// depth 1: 32x32 x8, depth 2: 16x16 x32 (`bsize_to_tx_size_cat` counts +/// the `sub_tx_size_map` chain TX_64X64->32->16->8->4, i.e. cat 3, the +/// same CDF a 64x64 square block reads). +/// * `av1_get_max_uv_txsize` = `av1_get_adjusted_tx_size( +/// max_txsize_rect_lookup[BLOCK_64X32])` caps at TX_32X32, so the block's +/// 64x32 uv plane block is TWO 32x32 chroma units per plane -- one per +/// 64x64 luma "mu" chunk, and `decode_token_recon_block` codes Y, U, V of +/// a chunk before moving to the next (the interleave lane-sb128b r3 +/// measured at the 128x128 NONE root). +/// * `get_txb_ctx`: the uv plane block is bigger than the uv transform, so +/// chroma `txb_skip_ctx` takes the offset-10 rows (7 + 3). +/// No CfL, no filter intra (both cap at 32x32) and no palette +/// (`av1_allow_palette` caps at 64x64) exist at this size -- the mode +/// reader's own size gates already say so, and the guard below is defensive. +#[allow(clippy::too_many_arguments)] +fn decode_block_128rect( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + at: (usize, usize), + bw: usize, + bh: usize, + y: &mut PlaneBuf, + u: &mut PlaneBuf, + v: &mut PlaneBuf, + base_q_idx: u8, + enable_filter_intra: bool, + allow_screen_content_tools: bool, + tx_select: bool, + reduced_tx_set: bool, +) -> Result<()> { + let (r, c) = at; + let (px, py) = (c * SUB, r * SUB); + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); + let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c); + let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at); + let palette_uv_cache = neighbours.palette_uv_cache(at); + let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = + read_intra_mode_rect( + dec, + cdfs, + nb_above_mode, + nb_left_mode, + // `is_cfl_allowed` caps CFL at 32x32: `uv_mode` comes off the + // 13-symbol no-CFL CDF here, never the 14-symbol one. + false, + true, + bw, + bh, + enable_filter_intra, + neighbours.skip_txfm_ctx(mi_r, mi_c), + allow_screen_content_tools, + Some((palette_ctx, &palette_cache)), + &palette_uv_cache, + mi_r, + mi_c, + )?; + if alpha.is_some() || filter_intra.is_some() || palette_y.is_some() || palette_uv.is_some() { + return Err(unsupported( + "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of \ + their size gates caps at 64x64 or below, so none of these symbols exists there)", + )); + } + let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; + let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } + let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c); + // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or + // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc). + let depth = if tx_select { + let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh); + dec.symbol(&mut cdfs.tx_size_cat3[ctx]) + } else { + 0 + }; + if depth != 0 { + TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); + } + INTRA_SB128_DEPTH_HITS.with(|c| { + let mut h = c.get(); + h[depth.min(2)] += 1; + c.set(h); + }); + let logical_tx = 64 >> depth; + // A 64-point transform codes nothing past its low 32 coefficients. + let coeff_tx_side = logical_tx.min(32); + let luma_set = txbset_for(logical_tx, reduced_tx_set); + let luma_scan = default_scan(coeff_tx_side); + let chroma_tx = 32; + let chroma_scan = default_scan(chroma_tx); + let (cpx, cpy) = (px / 2, py / 2); + let (chroma_w, chroma_h) = (bw / 2, bh / 2); + let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height); + let (nch_x, nch_y) = (bw / 64, bh / 64); + // One 32x32 chroma unit per mu chunk, per plane. + debug_assert_eq!((chroma_w / chroma_tx, chroma_h / chroma_tx), (nch_x, nch_y)); + let tpc = 64 / logical_tx; + let zero_tu = vec![0i32; logical_tx * logical_tx]; + let zero_cu = vec![0i32; chroma_tx * chroma_tx]; + let mut last_grids = [Vec::new(), Vec::new()]; + let mut units: Vec<((usize, usize), Vec, Vec)> = Vec::new(); + for ch_row in 0..nch_y { + for ch_col in 0..nch_x { + for tu_row in ch_row * tpc..(ch_row + 1) * tpc { + for tu_col in ch_col * tpc..(ch_col + 1) * tpc { + let tu_mi = ( + mi_r + tu_row * (logical_tx / MI), + mi_c + tu_col * (logical_tx / MI), + ); + let (tu_px, tu_py) = (px + tu_col * logical_tx, py + tu_row * logical_tx); + // libaom clips the transform loop to + // `max_blocks_wide/high`: a unit whose top-left sample is + // outside the frame is never coded. + if tu_px >= y.true_width || tu_py >= y.true_height { + continue; + } + // Edge availability of a unit INSIDE a block, with the + // 128 geometry -- `has_top_right` is false for a block + // whose largest side is > 64, which [`Reach::of_rect`] + // already answered for the block and [`tu_reach`] carries + // down to the units on its own top row. + let tu_r = tu_reach( + bw, + bh, + tu_col * logical_tx, + tu_row * logical_tx, + logical_tx, + reach, + px, + py, + y.width, + y.height, + ); + if skip { + y.reconstruct( + tu_px, tu_py, logical_tx, mode, angle_delta_y, tu_r, &zero_tu, None, + None, smooth_neighbor, + ); + neighbours.record_mi_luma(tu_mi, logical_tx, &zero_tu); + continue; + } + // The unit is smaller than its block, so `txb_skip_ctx` + // is the neighbour-magnitude table, never the lone-TU 0. + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); + let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; + let tu_grid = read_plane( + dec, + cdfs, + luma_set, + &luma_scan, + 0, + tu_around, + mode, + mode, + angle_delta_y, + tu_r, + y, + tu_px, + tu_py, + logical_tx, + coeff_tx_side, + base_q_idx, + None, + None, + Some(tu_skip_ctx), + smooth_neighbor, + )?; + neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); + } + } + // The chroma context arrays are indexed in LUMA mi, so this + // unit's anchor is its own 64x64 mu chunk. + let luma_span = chroma_tx * 2; + let cu_mi = ( + mi_r + ch_row * (luma_span / MI), + mi_c + ch_col * (luma_span / MI), + ); + let cu_r = tu_reach( + bw, + bh, + ch_col * luma_span, + ch_row * luma_span, + luma_span, + reach, + px, + py, + y.width, + y.height, + ); + let (cu_x, cu_y) = (cpx + ch_col * chroma_tx, cpy + ch_row * chroma_tx); + if skip { + u.reconstruct( + cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, + None, smooth_neighbor_uv, + ); + v.reconstruct( + cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, + None, smooth_neighbor_uv, + ); + continue; + } + let cu_around = neighbours.around_mi(cu_mi, luma_span); + let u_grid = read_plane( + dec, cdfs, TxbSet::Chroma32, &chroma_scan, 1, cu_around[1], mode, uv_predict_mode, + angle_delta_uv, cu_r, u, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, None, None, + Some(3), smooth_neighbor_uv, + )?; + let v_grid = read_plane( + dec, cdfs, TxbSet::Chroma32, &chroma_scan, 2, cu_around[2], mode, uv_predict_mode, + angle_delta_uv, cu_r, v, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, None, None, + Some(3), smooth_neighbor_uv, + )?; + CHROMA_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); + // Immediately, so the NEXT unit of this same block reads this + // one's coefficient context; replayed after `record_split_luma_rect` + // below, which would otherwise clobber them whole-block. + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 1, &u_grid); + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, 2, &v_grid); + units.push((cu_mi, u_grid.clone(), v_grid.clone())); + last_grids = [u_grid, v_grid]; + } + } + if skip { + neighbours.record_rect( + at, + bw, + bh, + mode, + uv_predict_mode, + &[ + vec![0i32; bw * bh], + vec![0i32; chroma_w * chroma_h], + vec![0i32; chroma_w * chroma_h], + ], + ); + } else { + neighbours.record_split_luma_rect( + at, + bw, + bh, + mode, + uv_predict_mode, + [&last_grids[0], &last_grids[1]], + ); + for (cu_mi, u_grid, v_grid) in &units { + neighbours.record_mi_chroma(*cu_mi, 64, 64, 1, u_grid); + neighbours.record_mi_chroma(*cu_mi, 64, 64, 2, v_grid); + } + } + // No palette exists at this size; the unconditional stamp clears any + // stale neighbour palette state, as every other block path does. + neighbours.record_palette_y_rect(at, bw, bh, 0, [0u16; 8]); + neighbours.record_palette_uv_rect(at, bw, bh, 0, [0u16; 8]); + neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); + neighbours.fill_lf_grid_rect( + (mi_r, mi_c), + bw / MI, + bh / MI, + logical_tx as u8, + logical_tx as u8, + 0, + ); + if bw > bh { + INTRA_SB128_HORZ_HITS.with(|c| c.set(c.get() + 1)); + } else { + INTRA_SB128_VERT_HITS.with(|c| c.set(c.get() + 1)); + } + Ok(()) +} + + /// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring /// [`crate::tile::write_leaf8`]: its intra-mode context comes from the /// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid), @@ -13698,14 +14022,81 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( mi_rows, )?; if matches!(part128, PARTITION_HORZ | PARTITION_VERT) { - // lane-sb128c r7: the INTER path decodes these (two 128x64 - // / two 64x128 blocks, the film's own bottom-edge shape); - // the key path's `decode_block` is square-only, so an - // intra 128x64 would silently decode as four 64x64 - // quadrants. Refuse by name instead of corrupting. - return Err(unsupported( - "a 128x128 superblock HORZ/VERT partition on an intra frame (the inter path decodes it; the intra one is square-block only)", - )); + // lane-sb128c r8: libaom `decode_partition` + // (decodeframe.c:1554) codes PARTITION_HORZ at the 128 + // root as `subsize = BLOCK_128X64` at (mi_row, mi_col) + // plus a second one at (mi_row + hbs, mi_col) only + // `if (mi_row + hbs < mi_rows)`; PARTITION_VERT mirrors it + // on the column. `hbs` is `mi_size_high[BLOCK_128X128]/2` + // = 16 mi. Both halves together cover the whole root, so + // the remaining three 64x64 quadrant visits code nothing + // (same flag the NONE arm sets). + sb128_none = true; + let horz = part128 == PARTITION_HORZ; + let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); + let (bw, bh) = if horz { (128usize, 64usize) } else { (64, 128) }; + for i in 0..2 { + let at = if horz { + (at128.0 + i * 4, at128.1) + } else { + (at128.0, at128.1 + i * 4) + }; + let (blk_mi_r, blk_mi_c) = (at.0 * (SUB / MI), at.1 * (SUB / MI)); + if i > 0 + && (blk_mi_r >= mi_rows as usize || blk_mi_c >= mi_cols as usize) + { + break; + } + // Each half reads its OWN `cdef_idx` literal + // (libaom `read_cdef` keys the `-1` test on the + // block's own `MB_MODE_INFO`, and the two halves have + // different ones). + CDEF_TRANSMITTED.with(|c| c.set(false)); + decode_block_128rect( + &mut dec, + &mut cdfs, + &mut neighbours, + at, + bw, + bh, + &mut y, + &mut u, + &mut v, + base_q_idx, + enable_filter_intra, + allow_screen_content_tools, + tx_select, + reduced_tx_set, + )?; + } + // One `cdef_idx` per BLOCK, copied to every 64x64 CDEF + // unit it covers (the inter path's own fan-out). + let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize); + CDEF_IDX_GRID.with(|g| { + let mut g = g.borrow_mut(); + let cols = CDEF_SB_COLS.with(|c| c.get()); + if cols == 0 { + return; + } + for (src, dst) in if horz { + [((0, 0), (0, 1)), ((1, 0), (1, 1))] + } else { + [((0, 0), (1, 0)), ((0, 1), (1, 1))] + } { + let (si, di) = ( + (q_r + src.0) * cols + q_c + src.1, + (q_r + dst.0) * cols + q_c + dst.1, + ); + if q_c + src.1 < cols + && q_c + dst.1 < cols + && si < g.len() + && di < g.len() + { + g[di] = g[si]; + } + } + }); + continue; } if part128 == PARTITION_NONE { // One 128x128 intra block: four TX_64X64 luma units and a diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 807142d5..3b861561 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -45,7 +45,7 @@ const REFUSALS: &[&str] = &[ "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", "a 128x128 superblock AB partition (HORZ_A/HORZ_B/VERT_A/VERT_B at the 128 root)", "an inter var-tx tree with a leaf transform larger than 64x64", - "a 128x128 superblock HORZ/VERT partition on an intra frame (the inter path decodes it; the intra one is square-block only)", + "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of their size gates caps at 64x64 or below, so none of these symbols exists there)", "intrabc under a 128x128 superblock (libaom's av1_is_dv_valid derives the block-vector delay from sb_size, which this decoder hardcodes to 64)", "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)", "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 189876f2..901357a3 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -92,6 +92,12 @@ pub fn sb128_rect_counters() -> (usize, usize, usize, usize) { ) } +/// lane-sb128c r8: the 128 root's INTRA (key-frame) rect arm -- (128x64 +/// blocks, 64x128 blocks, blocks per resolved intra `tx_depth` 0/1/2). +pub fn intra_sb128_counters() -> (usize, usize, [usize; 3]) { + crate::decode::intra_sb128_hits() +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -19131,6 +19137,177 @@ mod tests { ); } + /// lane-sb128c r8: the 128 root's HORZ/VERT arm on a KEY frame -- one + /// 128x64 (or 64x128) INTRA block, the shape r7 still refused by name. + /// A smooth `gradients` source makes the key frame itself take the arm + /// (`mandelbrot`'s key frames always split), at sizes whose last + /// superblock row/column is a partial 64 so the gathered frame-edge bit + /// decides the root. `--enable-tx-size-search=1` on half the arms is what + /// makes the intra `tx_depth` symbol real (depth 1 = 32x32 units, + /// depth 2 = 16x16). Every decode-order frame (hidden alt-refs included) + /// is compared against the instrumented aomdec, and the per-depth + /// counters are printed for every compared attempt. + #[test] + fn a_real_aomenc_sb128_intra_rect_block_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_sb128_intra_rect_block_decodes_pixel_exact"; + let _gate_lock = lock_gate_counters(); + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + // (tag, cq, width, height, 10-bit, vertical twin, tx-size-search) + let arms: Vec<(u32, &str, usize, usize, bool, bool, bool)> = vec![ + (42, "--cq-level=55", 384, 320, false, false, false), + (43, "--cq-level=32", 384, 320, true, false, true), + (44, "--cq-level=8", 256, 192, false, false, true), + (46, "--cq-level=55", 192, 256, false, true, false), + (45, "--cq-level=45", 192, 256, true, true, true), + ]; + let (mut matched, mut named_refusals, mut out_of_scope) = (0u32, 0u32, 0u32); + let (mut horz_arms, mut vert_arms) = (0u32, 0u32); + let mut depth_seen = [0usize; 3]; + for (tag, cq, width, height, ten_bit, vert, tx_search) in arms { + let pix = if ten_bit { "yuv420p10le" } else { "yuv420p" }; + let y4m = Command::new("ffmpeg") + .args([ + "-v", "error", "-f", "lavfi", "-i", + // DETERMINISTIC smooth source: lavfi `gradients` ignores + // its seed and gives a different picture every run (a + // whole round was spent on a "flake" that was really this), + // so the smoothness comes from long-period sinusoids + // through `geq` instead -- same effect on the encoder's + // partition choice, byte-identical across runs. + &format!( + "nullsrc=size={width}x{height}:rate=25,format=yuv420p, geq=lum='128+90*sin((X+2*N)/37)+30*sin(Y/29)': cb='128+30*sin(X/23)':cr='128+30*sin((Y+N)/19)'" + ), + "-pix_fmt", pix, "-strict", "-1", "-t", "0.2", + "-f", "yuv4mpegpipe", "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "{NAME}: ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let mut args: Vec<&str> = vec![ + "--codec=av1", "--passes=1", "--end-usage=q", cq, + "--cpu-used=0", "--threads=1", "--row-mt=0", + "--sb-size=128", "--min-partition-size=64", + "--enable-rect-partitions=1", "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", "--enable-palette=0", + "--enable-intrabc=0", "--deltaq-mode=0", "--limit=5", + // EVERY frame a key frame: this gate exists for the INTRA + // 128x64/64x128 block, and a mixed stream lands on other + // lanes' inter gaps (sub-8 leaves, rect inter residual) + // before it ever gets there. The inter twin of this same + // 128-root arm has its own gate + // (`..._sb128_gathered_edge_horz_partition_decodes_pixel_exact`). + "--kf-max-dist=1", + ]; + // AOMENC FLAG PRECEDENCE: the LAST occurrence wins, so this + // per-arm override goes after the base recipe. + args.push(if tx_search { + "--enable-tx-size-search=1" + } else { + "--enable-tx-size-search=0" + }); + if ten_bit { + args.extend_from_slice(&["--input-bit-depth=10", "--bit-depth=10"]); + } + args.extend_from_slice(&["--obu", "-o", "-", "-"]); + let mut child = Command::new(aomenc_path()) + .args(&args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "{NAME}: aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = intra_sb128_counters(); + if let Err(e) = decode_stream(&stream) { + let msg = e.to_string(); + assert!( + msg.contains("unsupported"), + "{NAME} failed outright, not a named refusal \ + (arm {tag} {width}x{height}): {msg}" + ); + named_refusals += 1; + eprintln!("{NAME}: arm {tag} {width}x{height} refusal: {msg}"); + continue; + } + let (frames, hidden) = decode_all_frames_vs_oracle( + &stream, + &format!("sb128c-intra-{tag}-{width}x{height}"), + ); + assert!(frames > 0, "{NAME}: no frames decoded at {width}x{height}"); + let after = intra_sb128_counters(); + let (horz, vert_hits) = (after.0 - before.0, after.1 - before.1); + let depth: Vec = + (0..3).map(|i| after.2[i] - before.2[i]).collect(); + eprintln!( + "{NAME}: arm {tag} cq {cq} {width}x{height} {}bit {} txsearch={}: \ + {frames} frames pixel-exact ({hidden} hidden), \ + intra_128x64 +{horz} intra_64x128 +{vert_hits} \ + depth[64/32/16] {depth:?}", + if ten_bit { 10 } else { 8 }, + if vert { "VERT" } else { "HORZ" }, + u8::from(tx_search), + ); + if horz == 0 && vert_hits == 0 { + out_of_scope += 1; + } + if horz > 0 { + horz_arms += 1; + } + if vert_hits > 0 { + vert_arms += 1; + } + for i in 0..3 { + depth_seen[i] += depth[i]; + } + matched += 1; + } + assert!( + matched > 0, + "{NAME}: every attempt refused ({named_refusals}); the gate never \ + decoded a stream" + ); + assert!( + horz_arms > 0, + "{NAME}: {matched} stream(s) decoded pixel-exact ({out_of_scope} carried \ + no 128-root rect intra block at all) but NOT ONE key-frame 128x64 \ + block fired -- the shape this gate exists for" + ); + assert!( + vert_arms > 0, + "{NAME}: the VERT twin never produced a key-frame 64x128 block" + ); + assert!( + depth_seen[0] > 0, + "{NAME}: no depth-0 (TX_64X64) intra 128-rect block: {depth_seen:?}" + ); + } + #[test] #[ignore = "lane-sb128 r2 diagnostic: is the frame-6 mismatch sb128-specific?"] fn sb128_r2_control_sb64() { diff --git a/lanes/sb128c-r8.report.md b/lanes/sb128c-r8.report.md new file mode 100644 index 00000000..214cb6f8 --- /dev/null +++ b/lanes/sb128c-r8.report.md @@ -0,0 +1,114 @@ +# lane-sb128c r8 -- the INTRA (key-frame) 128x64 / 64x128 block + +Branch `lane-sb128c`, worktree `edith_codecs-sb128c`, +`CARGO_TARGET_DIR=$HOME/.cache/cargo-target-sb128c`. + +## What changed + +- `crates/ec-av1/src/decode.rs:10101` `decode_block_128rect` (NEW): a rect + intra block at the 128 root. libaom `decode_block` -> + `predict_and_reconstruct_intra_block` per transform unit, with the 128 + geometry: `max_txsize_rect_lookup[BLOCK_128X64] == TX_64X64` so every luma + unit is SQUARE and there are >= two (depth 0: 64x64 x2, 1: 32x32 x8, + 2: 16x16 x32 -- `bsize_to_tx_size_cat` counts the `sub_tx_size_map` chain + TX_64X64->32->16->8->4, i.e. cat 3, the CDF a 64x64 square block reads); + `av1_get_max_uv_txsize` = `av1_get_adjusted_tx_size( + max_txsize_rect_lookup[BLOCK_64X32])` caps at TX_32X32, so the 64x32 uv + plane block is TWO 32x32 chroma units per plane, one per 64x64 "mu" chunk, + Y then U then V inside each chunk (`decode_token_recon_block`); + `get_txb_ctx` puts chroma on the offset-10 rows (uv plane block > uv tx). + Mode info goes through `read_intra_mode_rect` with `cfl = false` + (`is_cfl_allowed` caps at 32x32); filter intra and palette do not exist at + this size (`av1_filter_intra_allowed_bsize` 32x32, + `palette_bsize_ctx_wh` returns `None` above 64) and are refused by name if + one ever appears. Per-TU edge availability through `tu_reach` + (`has_top_right` false for a block whose long side is > 64, via + `Reach::of_rect`); lf/skip grids stamped with the resolved unit size. +- `crates/ec-av1/src/decode.rs:14027` key-frame path: the 128 root's + HORZ/VERT arm is no longer refused. It decodes `BLOCK_128X64` at + (mi_row, mi_col) plus a second at (mi_row + 16, mi_col) only + `if (mi_row + hbs < mi_rows)` (VERT mirrors it on the column, libaom + `decode_partition` decodeframe.c:1554), each half reading its OWN + `cdef_idx` literal, fanned out over the 64x64 CDEF units it covers. +- `crates/ec-av1/src/decode.rs:9346` / `:9247` `PlaneBuf::reconstruct` + + `reconstruct_rect`: bottom/right frame-edge CLIP. ROOT CAUSE of two + `index out of bounds` panics on the 1080p film's key frames (len 1536000, + index 1536000/1536384): our frame buffer is padded to 32 (`block_grid`) + while libaom's is superblock aligned, so a 64x64 unit of a 128x64 block at + y=768 of a 792-row frame wants 832 rows of an 800-row buffer. Those samples + are outside the frame -- never output, never referenced. +- `crates/ec-av1/src/stream.rs:95` `intra_sb128_counters()`, + `crates/ec-av1/src/decode.rs:10101` `INTRA_SB128_{HORZ,VERT,DEPTH}_HITS`. +- `crates/ec-av1/src/refusal_inventory.rs:48`: the intra-128x64 refusal is + REPLACED by the defensive CfL/filter-intra/palette one. + +## Gate (GREEN) + +`cargo test -p ec-av1 --lib intra_rect_block -- --nocapture` +(`a_real_aomenc_sb128_intra_rect_block_decodes_pixel_exact`): 5 real aomenc +streams, `--sb-size=128 --cpu-used=0 --min-partition-size=64 +--enable-rect-partitions=1 --kf-max-dist=1`, every decode-order frame compared +against the instrumented aomdec, hard asserts on the HORZ counter, the VERT +counter and depth[0]. + + arm 42 cq55 384x320 8bit HORZ txsearch=0: 5 frames exact, 128x64 +28 depth [28,0,0] + arm 43 cq32 384x320 10bit HORZ txsearch=1: 5 frames exact, 128x64 +30 depth [30,0,0] + arm 44 cq8 256x192 8bit HORZ txsearch=1: 5 frames exact, 128x64 +14 depth [14,0,0] + arm 46 cq55 192x256 8bit VERT txsearch=0: 5 frames exact, 64x128 +10 depth [10,0,0] + arm 45 cq45 192x256 10bit VERT txsearch=1: 5 frames exact, 64x128 +10 depth [10,0,0] + test result: ok. 1 passed; 0 failed + +Sources are `geq` long-period sinusoids, NOT lavfi `gradients`: gradients +ignores its seed and gave a different picture every run (one arm passed and +then failed with an unchanged decoder -- the flake was the fixture). + +EVIDENCE: $HOME/.cache/sb128c-gate-r8.log | cargo test -p ec-av1 --lib intra_rect_block --nocapture | 5/5 arms 5 frames each pixel-exact vs aomdec, 96 intra 128-rect blocks, 0 out of scope + +Sibling gate re-run GREEN (COMMON's SIBLING GATES rule): +`gathered_edge_horz` 4/4 arms pixel-exact, edge +2/+24/+2/+2. + +## Film (1080p 10-bit AV1, 4 extracts) + +| extract | r7 | r8 | edge_horz | inter_128x64 | +|---|---|---|---|---| +| 2 s head (1920x792) | OK 48/48 | **OK, 49/49 dumps bit-exact vs aomdec** | 720 | 720 | +| `~/.cache/reelc-head.obu` | OK 48/48 | OK, decodes to completion | 720 | 720 | +| `-ss 8100` 2 s | stopped: split intra strip TU 32x64 | PANICKED with the arm live, now stops at "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding" | 1 | 0 | +| `-ss 9000` 2 s | stopped: non-skip rect strip | now stops DEEPER, at "an inter SB-level AB partition" | 12 | 0 | + +EVIDENCE: $HOME/.cache/sb128c-r8/d2 vs $HOME/.cache/sb128c-r7/o2 | EC_AV1_FINAL_DUMP decode_probe head2s.obu, cmp per frame | dumps_compared=49 frames_differing=0 + +The two panics the r8 arm exposed on the film are the frame-edge clip above; +without it this round would have shipped a crash on his own film. + +## AB shapes at the 128 root -- MEASURED, still refused + +16-attempt sweep (512x512 and 384x320 geq sinusoids x cq 18/32/45/55 x 8+10 +bit, `--sb-size=128 --min-partition-size=64 --enable-ab-partitions=1`, +`$HOME/.cache/sb128c-r8/ab.sh`): **aomenc DOES pick them** -- 2 of 16 attempts +refuse with "a 128x128 superblock AB partition" (512x512 cq18 and cq45, +8-bit), and one more with the inter SB-level AB refusal. So the r7 residue +"whether aomenc ever picks them is unmeasured" is now answered YES; they +cannot be closed by measurement and need the implementation. + +## Suite + +`cargo test -p ec-av1 --lib` under a systemd unit (MemoryMax=10G), +log `$HOME/.cache/sb128c-suite-r8.log` -- see the round's closing note. + +## Residue + +- deferred: the four AB shapes at the 128 root (key and inter) -- MEASURED to + be reachable (above). Unblocked by this round's `decode_block_128rect` (the + 128x64/64x128 piece each AB shape needs) plus the libaom visit order in + `lanes/sb128c-r6.handoff.md`; the sweep script pins the recipe that + produces one. +- deferred: intra `tx_depth` > 0 at the 128 root (32x32 / 16x16 units) is + implemented but UNEXERCISED -- every arm, including cq 8 with + `--enable-tx-size-search=1`, resolved depth 0. Unblocked by finding an + encoder setting that splits a 128x64's transform; the gate prints the + per-depth counters so the round that finds one sees it immediately. +- accepted: an inter frame of the 192x256 smooth source mismatches at frame 1 + (`$HOME/.cache/sb128c-r8/vert46.obu`, byte 12033, 36073 bytes differ). It is + NOT this lane's shape: the key frames of that same stream are exact, and the + r7 inter gate is green on its own source. Left pinned for a hunter.