From 9a73c3092d9bf0e6fe9e80d539c4a0916ed6f6d7 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 20:46:10 +0300 Subject: [PATCH] fix(av1): interintra built a square intra predictor for rectangular blocks `interintra_blend` took one `side` and used it for the edge fetch, the intra predictor's `(bw, bh)`, `ii_size_scales` and the blend bounds. On a rectangular block (`write_w != write_h`) `side` is the LONGER dimension, so a 32x16 block got a 32x32 SMOOTH/DC predictor built off 32 rows of left edge (16 of which are not yet decoded) instead of libaom's 32x16 one. The mask scale happened to come out right (`128 / max(bw,bh)` == `ii_size_scales`), the predictor did not: the blend error is zero on the block's first row and grows downward, exactly the `sm_weights_h` divergence between a height-32 and a height-16 SMOOTH_PRED. Blend now takes `(bw, bh, stride)`: `edges_rect`/`predict` at the true footprint, mask scale off `max(bw, bh)`, loop over `bh x bw`, `pred` indexed at the buffer's own stride. New `interintra_rect_hits()` counts the shape (every existing interintra gate passed with the bug, so the rect arm was vacuous) and `decode_probe` reports it. Also `EC_AV1_NO_GRAIN=1` in `decode_stream`, which splits a mismatch into reconstruction vs film-grain synthesis in one run (it is what ruled grain out here). Measured on a 2-frame cut of the 10-bit 3840x1608 stream (key frame + first hidden alt-ref, 59 rect interintra blocks): the alt-ref's pre-filter dump vs the instrumented aomdec goes from a first mismatch at luma (17,1088) to that superblock exact; first remaining mismatch moves to (448,1152), a separate NEARMV mv-stack defect (ours (-40,-340), aomdec (-32,-350)). --- crates/ec-av1/examples/decode_probe.rs | 1 + crates/ec-av1/src/decode.rs | 71 +++++++++++++++++++------- crates/ec-av1/src/stream.rs | 11 +++- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index e1312c64..d38d4691 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -43,6 +43,7 @@ fn main() { let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits()); + println!("interintra_rect: {}", ec_av1::decode::interintra_rect_hits()); let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits(); println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}"); let si = ec_av1::stream::sub8_intra_rect_hits(); diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 74e8f70a..a82736cb 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2075,6 +2075,20 @@ pub(crate) fn interintra_hits() -> usize { INTERINTRA_HITS.with(|c| c.get()) } +// lane-midcut r1: of those, how many were RECTANGULAR (`write_w != write_h`) +// -- the shape `interintra_blend` used to build a square `side x side` intra +// predictor for, with `ii_size_scales`/`edges` taken at the square. Every +// pre-existing interintra gate passed with that bug, so the rect arm needs +// its own counter to stop being vacuous. +thread_local! { + static INTERINTRA_RECT_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`INTERINTRA_RECT_HITS`]. +pub fn interintra_rect_hits() -> usize { + INTERINTRA_RECT_HITS.with(|c| c.get()) +} + // How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE, // wedge or diffwtd) and had `compound_type`/`wedge_idx`/`wedge_sign`/ // `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a @@ -18941,7 +18955,9 @@ fn interintra_blend( plane: &PlaneBuf, x: usize, y: usize, - side: usize, + bw: usize, + bh: usize, + stride: usize, ii_mode: u8, wedge: Option<(&'static [u8], usize)>, pred: &mut [u16], @@ -18953,16 +18969,17 @@ fn interintra_blend( 2 => crate::intra::H_PRED, _ => crate::intra::SMOOTH_PRED, }; - let (above, left, corner) = plane.edges( + let (above, left, corner) = plane.edges_rect( x, y, - side, + bw, + bh, crate::encode::Reach { above_right: false, below_left: false, }, ); - let mut intra = vec![0u16; side * side]; + let mut intra = vec![0u16; bw * bh]; // Edge filtering never applies here: V/H at delta 0 are plain edge // copies (angle 90/180 skip the directional walk), DC/SMOOTH carry no // angle at all. @@ -18972,22 +18989,25 @@ fn interintra_blend( above.as_deref(), left.as_deref(), corner, - side, - side, + bw, + bh, false, false, &mut intra, ); - let scale = 128 / side; - for i in 0..side { - for j in 0..side { + // `ii_size_scales[bsize]` is keyed on the block's LONGER side + // (`MAX_SB_SIZE >> max(bw,bh)` order), not on the square this decoder + // builds the predictor buffer at. + let scale = 128 / bw.max(bh); + for i in 0..bh { + for j in 0..bw { let m = u32::from(match wedge { // lane-wii r2: wedge-interintra mask (aom_blend_a64_mask // over the wedge codebook, intra as src0, fixed sign 0). // Luma plane: mask rows are luma-resolution, ms == side. // 4:2:0 chroma: 2x2 box average (reconinter.c sub8x8 -- // subw == subh == 1). - Some((mask, ms)) if ms == side => mask[i * side + j], + Some((mask, ms)) if ms == stride => mask[i * stride + j], Some((mask, ms)) => { let t = 2 * i * ms + 2 * j; ((u32::from(mask[t]) @@ -19004,9 +19024,9 @@ fn interintra_blend( _ => II_WEIGHTS_1D[i.min(j) * scale], }, }); - let idx = i * side + j; - pred[idx] = - ((m * u32::from(intra[idx]) + (64 - m) * u32::from(pred[idx]) + 32) >> 6) as u16; + let idx = i * stride + j; + pred[idx] = ((m * u32::from(intra[i * bw + j]) + (64 - m) * u32::from(pred[idx]) + 32) + >> 6) as u16; } } } @@ -21873,9 +21893,18 @@ fn decode_inter_block( // result, all planes (reconinter.c av1_build_interintra_predictor // plane loop). if let Some(ii) = interintra_mode { - interintra_blend(y, px, py, side, ii, wedge_mask, &mut pred_y); - interintra_blend(u, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_u); - interintra_blend(v, cpx, cpy, chroma_side, ii, wedge_mask, &mut pred_v); + if write_w != write_h { + INTERINTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); + } + interintra_blend(y, px, py, write_w, write_h, side, ii, wedge_mask, &mut pred_y); + interintra_blend( + u, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask, + &mut pred_u, + ); + interintra_blend( + v, cpx, cpy, write_chroma_w, write_chroma_h, chroma_side, ii, wedge_mask, + &mut pred_v, + ); } // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8` @@ -25935,9 +25964,13 @@ fn decode_inter_block8( // Non-wedge interintra blend, mutually exclusive with OBMC (the // motion_mode symbol is not read for an interintra block). if let Some(ii) = interintra_mode { - interintra_blend(y, px, py, SIDE, ii, wedge_mask, &mut pred_y); - interintra_blend(u, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_u); - interintra_blend(v, cpx, cpy, CHROMA_SIDE, ii, wedge_mask, &mut pred_v); + interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y); + interintra_blend( + u, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_u, + ); + interintra_blend( + v, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_v, + ); } split8 = read_block_tx_size( diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8411b175..6ee548c3 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -29,6 +29,13 @@ use crate::decode::{ }; use crate::encode::Picture; +/// lane-midcut r1: `EC_AV1_NO_GRAIN=1` skips film grain synthesis at output, so a +/// mid-film mismatch splits into "reconstruction" (compare against +/// `ffmpeg -c:v libdav1d -filmgrain 0`) and "grain synthesis" in one run. +fn no_grain() -> bool { + std::env::var_os("EC_AV1_NO_GRAIN").is_some() +} + /// lane-hidden r1: where `EC_AV1_FINAL_DUMP`'s per-frame dump goes for the /// current thread, overriding the environment variable. Every ffmpeg/aomdec /// pixel gate in this repo compares SHOWN frames only (class @@ -297,7 +304,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { order_hint_slots[i] = order_hint_slots[slot]; } } - let output = if header.film_grain.apply_grain { + let output = if header.film_grain.apply_grain && !no_grain() { // lane-hbd10 r1: grain synthesis is bit-depth generic // (`film_grain.rs`'s `grain_range`/`scale_lut`, spec 7.18.3). let bit_depth = parser @@ -967,7 +974,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { decode::note_fwd_kf(); } if header.show_frame { - let output = if header.film_grain.apply_grain { + let output = if header.film_grain.apply_grain && !no_grain() { let mc_identity = parser .sequence_header() .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);