From 759e6884e21ccd52826fd7337646f2ac4a6aba4d 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 13:54:25 +0300 Subject: [PATCH] fix(av1): rect inter blocks picked the 4-tap chroma kernel from the square prediction buffer, not the block decode_inter_block predicts a rectangular block over its enclosing square buffer (side x side, chroma_side x chroma_side; write_w/write_h govern only the pixel write). The narrow-block decision lived in mc::predict_with_filters and read that buffer's dimensions, while libaom av1_get_interp_filter_params_with_block_size takes it per axis from the prediction block's own w/h -- so the chroma of an 8x16 / 16x8 inter block (4x8 / 8x4) used the 8-tap kernel where libaom uses the 4-tap one. Luma is never <= 4 wide on this path, which is why such streams decoded luma-exact and differed by +-1 on scattered chroma samples (lane-interedge r1's 192x128 control, no frame-edge node, cdef/LR off). mc gains predict_with_filters_kern / predict_scaled_kern / predict_compound_intermediate_kern (old entry points delegate with kern = block dims) plus a RECT_NARROW_KERN_HITS counter; decode_inter_block's 12 prediction calls pass write_w/write_h and write_chroma_w/write_chroma_h. New gate a_real_aomenc_rect_inter_block_predicts_chroma_with_the_narrow_kernel_pixel_exact: real aomenc mandelbrot 192x128 and 128x192, 8- and 10-bit, cq 20/32/45, 6 frames, lag-in-frames=0, every decode-order frame compared on Y, U and V; 12/12 arms exact, narrow-kernel counter nonzero at both depths and in both orientations. Same recipe mismatched chroma in all arms before the fix. --- crates/ec-av1/src/decode.rs | 48 +++++++++---- crates/ec-av1/src/mc.rs | 122 +++++++++++++++++++++++++++++++-- lanes/rectchroma2-r1.report.md | 87 +++++++++++++++++++++++ 3 files changed, 240 insertions(+), 17 deletions(-) create mode 100644 lanes/rectchroma2-r1.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index af4ff7e0..3cfc6efa 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -17419,7 +17419,7 @@ fn decode_inter_block( } let mut inter0_y = vec![0i32; side * side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &py0.data, py0.width, py0.true_width, @@ -17429,6 +17429,8 @@ fn decode_inter_block( scale0, side, side, + write_w, + write_h, h_filter, v_filter, &mut inter0_y, @@ -17444,7 +17446,7 @@ fn decode_inter_block( ); } let mut inter1_y = vec![0i32; side * side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &py1.data, py1.width, py1.true_width, @@ -17454,6 +17456,8 @@ fn decode_inter_block( scale1, side, side, + write_w, + write_h, h_filter, v_filter, &mut inter1_y, @@ -17491,7 +17495,7 @@ fn decode_inter_block( } let mut inter0_u = vec![0i32; chroma_side * chroma_side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &pu0.data, pu0.width, pu0.true_width, @@ -17501,6 +17505,8 @@ fn decode_inter_block( scale0, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut inter0_u, @@ -17518,7 +17524,7 @@ fn decode_inter_block( } } let mut inter1_u = vec![0i32; chroma_side * chroma_side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &pu1.data, pu1.width, pu1.true_width, @@ -17528,6 +17534,8 @@ fn decode_inter_block( scale1, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut inter1_u, @@ -17561,7 +17569,7 @@ fn decode_inter_block( } let mut inter0_v = vec![0i32; chroma_side * chroma_side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &pv0.data, pv0.width, pv0.true_width, @@ -17571,6 +17579,8 @@ fn decode_inter_block( scale0, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut inter0_v, @@ -17588,7 +17598,7 @@ fn decode_inter_block( } } let mut inter1_v = vec![0i32; chroma_side * chroma_side]; - mc::predict_compound_intermediate( + mc::predict_compound_intermediate_kern( &pv1.data, pv1.width, pv1.true_width, @@ -17598,6 +17608,8 @@ fn decode_inter_block( scale1, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut inter1_v, @@ -18485,7 +18497,7 @@ fn decode_inter_block( let mut pred_u = vec![0u16; chroma_side * chroma_side]; let mut pred_v = vec![0u16; chroma_side * chroma_side]; if luma_scale == mc::REF_NO_SCALE { - mc::predict_with_filters( + mc::predict_with_filters_kern( &py_ref.data, py_ref.width, py_ref.true_width, @@ -18494,11 +18506,13 @@ fn decode_inter_block( mv_to_q4(py, mv.0, true), side, side, + write_w, + write_h, h_filter, v_filter, &mut pred_y, ); - mc::predict_with_filters( + mc::predict_with_filters_kern( &pu_ref.data, pu_ref.width, pu_ref.true_width, @@ -18507,11 +18521,13 @@ fn decode_inter_block( mv_to_q4(cpy, mv.0, false), chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut pred_u, ); - mc::predict_with_filters( + mc::predict_with_filters_kern( &pv_ref.data, pv_ref.width, pv_ref.true_width, @@ -18520,12 +18536,14 @@ fn decode_inter_block( mv_to_q4(cpy, mv.0, false), chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut pred_v, ); } else { - mc::predict_scaled( + mc::predict_scaled_kern( &py_ref.data, py_ref.width, py_ref.true_width, @@ -18535,11 +18553,13 @@ fn decode_inter_block( luma_scale, side, side, + write_w, + write_h, h_filter, v_filter, &mut pred_y, ); - mc::predict_scaled( + mc::predict_scaled_kern( &pu_ref.data, pu_ref.width, pu_ref.true_width, @@ -18549,11 +18569,13 @@ fn decode_inter_block( luma_scale, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut pred_u, ); - mc::predict_scaled( + mc::predict_scaled_kern( &pv_ref.data, pv_ref.width, pv_ref.true_width, @@ -18563,6 +18585,8 @@ fn decode_inter_block( luma_scale, chroma_side, chroma_side, + write_chroma_w, + write_chroma_h, h_filter, v_filter, &mut pred_v, diff --git a/crates/ec-av1/src/mc.rs b/crates/ec-av1/src/mc.rs index b57f1b35..5d57d0d8 100644 --- a/crates/ec-av1/src/mc.rs +++ b/crates/ec-av1/src/mc.rs @@ -333,6 +333,36 @@ pub fn predict_with_filters( h_kind: InterpFilterKind, v_kind: InterpFilterKind, dst: &mut [u16], +) { + predict_with_filters_kern( + reference, stride, true_width, true_height, x_q4, y_q4, block_w, + block_h, block_w, block_h, h_kind, v_kind, dst, + ); +} + +/// [`predict_with_filters`] with the 4-tap decision taken from the block's +/// TRUE width/height (`kern_w`/`kern_h`) rather than the destination buffer's. +/// libaom's `av1_get_interp_filter_params_with_block_size` (reconinter.h, +/// called per axis from `inter_predictor` with `w` then `h`) picks the narrow +/// kernel from the PREDICTION block's own dimensions; our inter path predicts +/// a rectangular block over its enclosing square buffer, so a rect block whose +/// chroma is 4 wide or 4 tall (luma 8x16 / 16x8 and taller kin) must still ask +/// for the 4-tap kernel on that axis -- lane-rectchroma2 r1. +#[allow(clippy::too_many_arguments)] +pub fn predict_with_filters_kern( + reference: &[u16], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y_q4: i32, + block_w: usize, + block_h: usize, + kern_w: usize, + kern_h: usize, + h_kind: InterpFilterKind, + v_kind: InterpFilterKind, + dst: &mut [u16], ) { assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); @@ -374,12 +404,13 @@ pub fn predict_with_filters( let (h_wide, h_narrow) = h_kind.tables(); let (v_wide, v_narrow) = v_kind.tables(); - let h_filter = if block_w <= 4 { + note_narrow_kern(block_w, block_h, kern_w, kern_h); + let h_filter = if kern_w <= 4 { &h_narrow[xfrac] } else { &h_wide[xfrac] }; - let v_filter = if block_h <= 4 { + let v_filter = if kern_h <= 4 { &v_narrow[yfrac] } else { &v_wide[yfrac] @@ -452,6 +483,32 @@ thread_local! { /// only intra blocks cannot pass it by construction (class /// `gate-blind-to-feature`). static INTER_PRED_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; + + /// lane-rectchroma2 r1: how often the 4-tap decision came out DIFFERENT + /// from what the destination buffer's own dimensions would have given -- + /// i.e. how often a rectangular block predicted over its enclosing square + /// buffer asked for libaom's narrow kernel on an axis. Zero means a gate + /// never exercised the rect-chroma shape at all. + static RECT_NARROW_KERN_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`RECT_NARROW_KERN_HITS`]. +pub fn rect_narrow_kern_hits() -> usize { + RECT_NARROW_KERN_HITS.with(|c| c.get()) +} + +/// Resets [`RECT_NARROW_KERN_HITS`] (per-attempt counting, class +/// `counter-from-refused-stream`). +pub fn reset_rect_narrow_kern_hits() { + RECT_NARROW_KERN_HITS.with(|c| c.set(0)); +} + +/// Bumps [`RECT_NARROW_KERN_HITS`] when the block's true dims pick a kernel +/// the destination buffer's dims would not have. +fn note_narrow_kern(block_w: usize, block_h: usize, kern_w: usize, kern_h: usize) { + if (kern_w <= 4) != (block_w <= 4) || (kern_h <= 4) != (block_h <= 4) { + RECT_NARROW_KERN_HITS.with(|c| c.set(c.get() + 1)); + } } /// Current value of [`INTER_PRED_HITS`]. @@ -498,6 +555,7 @@ fn horizontal_scaled_pass( y0: i32, x_scale_fp: i64, block_w: usize, + kern_w: usize, rows: usize, h_kind: InterpFilterKind, intermediate: &mut [i32], @@ -510,7 +568,7 @@ fn horizontal_scaled_pass( let x_qn = pos_x_q10 + c as i64 * x_step_qn; let int_pel = (x_qn >> 10) as i32; let filter_idx = ((x_qn & 1023) >> 6) as usize; - let h_filter = if block_w <= 4 { + let h_filter = if kern_w <= 4 { &h_narrow[filter_idx] } else { &h_wide[filter_idx] @@ -555,6 +613,31 @@ pub fn predict_scaled( h_kind: InterpFilterKind, v_kind: InterpFilterKind, dst: &mut [u16], +) { + predict_scaled_kern( + reference, stride, true_width, true_height, x_q4, y_q4, x_scale_fp, + block_w, block_h, block_w, block_h, h_kind, v_kind, dst, + ); +} + +/// [`predict_scaled`] with the 4-tap decision from the block's true dims +/// (see [`predict_with_filters_kern`]). +#[allow(clippy::too_many_arguments)] +pub fn predict_scaled_kern( + reference: &[u16], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y_q4: i32, + x_scale_fp: i64, + block_w: usize, + block_h: usize, + kern_w: usize, + kern_h: usize, + h_kind: InterpFilterKind, + v_kind: InterpFilterKind, + dst: &mut [u16], ) { assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); @@ -568,7 +651,8 @@ pub fn predict_scaled( // Q10; off/pos_x_q10 fold the block's own x_q4 (Q4) into that same Q10 // grid (spec 7.11.3.3's `dec_calc_subpel_params`). let (v_wide, v_narrow) = v_kind.tables(); - let v_filter = if block_h <= 4 { + note_narrow_kern(block_w, block_h, kern_w, kern_h); + let v_filter = if kern_h <= 4 { &v_narrow[yfrac] } else { &v_wide[yfrac] @@ -585,6 +669,7 @@ pub fn predict_scaled( y0, x_scale_fp, block_w, + kern_w, rows, h_kind, &mut intermediate, @@ -648,6 +733,31 @@ pub fn predict_compound_intermediate( h_kind: InterpFilterKind, v_kind: InterpFilterKind, dst: &mut [i32], +) { + predict_compound_intermediate_kern( + reference, stride, true_width, true_height, x_q4, y_q4, x_scale_fp, + block_w, block_h, block_w, block_h, h_kind, v_kind, dst, + ); +} + +/// [`predict_compound_intermediate`] with the 4-tap decision from the block's +/// true dims (see [`predict_with_filters_kern`]). +#[allow(clippy::too_many_arguments)] +pub fn predict_compound_intermediate_kern( + reference: &[u16], + stride: usize, + true_width: usize, + true_height: usize, + x_q4: i32, + y_q4: i32, + x_scale_fp: i64, + block_w: usize, + block_h: usize, + kern_w: usize, + kern_h: usize, + h_kind: InterpFilterKind, + v_kind: InterpFilterKind, + dst: &mut [i32], ) { assert_eq!(dst.len(), block_w * block_h, "the destination is the block"); assert!(!reference.is_empty(), "a reference plane has samples"); @@ -661,7 +771,8 @@ pub fn predict_compound_intermediate( let yfrac = y_q4.rem_euclid(16) as usize; let (v_wide, v_narrow) = v_kind.tables(); - let v_filter = if block_h <= 4 { + note_narrow_kern(block_w, block_h, kern_w, kern_h); + let v_filter = if kern_h <= 4 { &v_narrow[yfrac] } else { &v_wide[yfrac] @@ -678,6 +789,7 @@ pub fn predict_compound_intermediate( y0, x_scale_fp, block_w, + kern_w, rows, h_kind, &mut intermediate, diff --git a/lanes/rectchroma2-r1.report.md b/lanes/rectchroma2-r1.report.md new file mode 100644 index 00000000..abdc94f2 --- /dev/null +++ b/lanes/rectchroma2-r1.report.md @@ -0,0 +1,87 @@ +# lane-rectchroma2 r1 -- rect INTER blocks predicted chroma with the WIDE kernel + +## Premise re-measured first + +lane-interedge r1's residue reproduced on this tree (main 2c04819 + lane-cdef + lane-golomb +173b793): mandelbrot 192x128 (the CONTROL size -- zero frame-edge nodes), 6 frames, +`--enable-rect-partitions=1 --min-partition-size=8 --enable-tx-size-search=0 --lag-in-frames=0`, +intra tools off, cq 20/32/45, 8- and 10-bit: **luma exact, chroma differs on frames 2..5 in all +six arms**. Streams hashed twice, identical. + +EVIDENCE: `~/.cache/rectchroma2-tmp/c192-128-{8,10}-{20,32,45}.obu` (each sha256 twice, equal) +| `decode_probe` with `EC_AV1_FINAL_DUMP` vs `ffmpeg -pix_fmt yuv420p[10le]`, every decode-order +frame, per plane | e.g. cq32 8-bit: `[(2,'chroma (0,32)..(1,40) n=17'), (4,...), (5,...)]`, luma +exact in all 6 arms. + +## Stage bisect + +Regenerated the same recipe with `--loopfilter-control=0` (deblock off; CDEF/LR already off in +that recipe): **the chroma mismatch survives unchanged** -> not a filter-stage defect, so the +charter's suspect (d) (chroma deblock levels / uv tx grid for rect inter blocks) is exonerated. +The surviving diffs are +-1 on scattered samples inside 4-wide-by-8-tall and 8-wide-by-4-tall +CHROMA regions (e.g. frame 4: chroma x32..35 rows 26..39, and x48..55 rows 16..19) -- i.e. the +chroma halves of 8x16 and 16x8 LUMA blocks. + +EVIDENCE: `~/.cache/rectchroma2-tmp/n192-128-8-32.obu` (`gen_nolf.sh`) | decode + full-plane +compare | `MISMATCH [(2,'chroma ... n=15'), (4,...n=36), (5,...n=45)]`, luma exact. + +## Root cause (charter suspect (a), confirmed) + +`crates/ec-av1/src/decode.rs` `decode_inter_block` predicts a RECTANGULAR block over its +enclosing SQUARE buffer (`side`x`side`, chroma `chroma_side`x`chroma_side`; `write_w`/`write_h` +govern only the pixel write -- the lane-partitions r1 corner-cut). The 4-tap decision lived +inside `mc::predict_with_filters` and read that BUFFER's dimensions. libaom +`av1_get_interp_filter_params_with_block_size` (reconinter.h, called per axis from +`inter_predictor` with `w` then `h`) reads the PREDICTION BLOCK's dimensions: `w <= 4` -> +`av1_interp_4tap[0]` (SHARP and REGULAR both) / `[1]` (SMOOTH). Luma is never <= 4 wide on this +path, so only chroma of a rect block (4x8 / 8x4 / 4x16 ...) was affected -- exactly the +"luma exact, chroma wrong" signature. + +- `crates/ec-av1/src/mc.rs`: `predict_with_filters_kern` / `predict_scaled_kern` / + `predict_compound_intermediate_kern` -- the existing three entry points keep their signatures + and delegate with `kern = block dims`; `horizontal_scaled_pass` takes `kern_w`. +- `crates/ec-av1/src/mc.rs`: `RECT_NARROW_KERN_HITS` + `rect_narrow_kern_hits()` / + `reset_rect_narrow_kern_hits()` / `note_narrow_kern()` -- counts predictions whose kernel + choice DIFFERS from what the square buffer would have given (one bump per prediction). +- `crates/ec-av1/src/decode.rs`: the 12 prediction calls inside `decode_inter_block` now pass + `write_w/write_h` (luma) and `write_chroma_w/write_chroma_h` (chroma) as the kernel dims. + +Class sweep: every other `mc::predict*` call site passes a buffer whose dims ARE the block's +(`obmc_neighbour_pred` takes the neighbour's true w/h; the 8x8 leaf path is square, chroma 4x4; +the CfL bilinear path is unrelated), so no sibling site needed the change. + +## Gate (GREEN) + +``` +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-rectchroma2 EC_NOMEMGUARD=1 EC_AV1_REQUIRE_AOMENC=1 \ +PATH="$HOME/.cache/aom-oracle/build:$PATH" cargo test -p ec-av1 --lib narrow_kernel -- --nocapture +``` +`a_real_aomenc_rect_inter_block_predicts_chroma_with_the_narrow_kernel_pixel_exact` +(`crates/ec-av1/src/stream.rs`): real aomenc, mandelbrot 192x128 AND its transposed twin +128x192 (so 4x8 and 8x4 chroma both fire), 8- and 10-bit, cq 20/32/45, 6 frames, +`--lag-in-frames=0` so decode order == display order and EVERY frame is compared on Y, U AND V +against ffmpeg. CDEF and loop restoration left ON. Streams encoded twice and asserted equal. +`compared == 12` is a hard assert (a refusal fails the gate, never SKIPs), and the narrow-kernel +counter -- reset per attempt, read only on compared attempts -- must be nonzero at BOTH depths +and in BOTH orientations. + +EVIDENCE: gate stdout | 12 aomenc -> ec-av1 -> ffmpeg attempts, 6 frames each, all three planes +| `12/12 arms 6 frames full-plane exact; narrow-kernel hits 24/4/16 (192x128 8-bit cq20/32/45), +18/26/36 (10-bit), 42/20/18 and 36/36/34 for 128x192` -- 0 refusals, 0 out-of-scope mismatches. + +Before the fix the SAME recipe mismatched chroma in every arm (the premise sweep above), so the +gate is not vacuous. + +## Refusals +None lifted (no inventory string covers this defect -- it was a wrong-pixels defect behind an +already-accepted path). `refusal_inventory.rs`/`gate_coverage.rs` untouched by intent, both green +in the suite below. + +## Suite +`$HOME/.cache/rectchroma2-suite-r1.log`: SUITE_RESULT + +## Residue +- accepted: `predict_scaled_kern` (scaled reference / superres) got the same fix by construction + but is not exercised by this gate -- superres rect-chroma has no aomenc recipe on this lane. +- deferred(lane-interedge merge): that lane's frame-edge gate can now become a FULL-PLANE arm; + its luma-only compare was a workaround for exactly this defect.