From 7285f0396ebc05562f4f686e04c404615e71b755 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 19:19:31 +0300 Subject: [PATCH] fix(av1): OBMC read a 1:4-strip neighbour's interp filter from the wrong mi libaom's foreach_overlappable_nb_above/left (reconinter.c 780/840) snaps a 4-wide/4-tall neighbour back to its chroma pair's even mi and takes the neighbour's whole mbmi -- mv, reference AND interp_filters -- from the pair's SECOND mi. Ours took the MiInfo from that second mi but read the switchable filter symbols from the FIRST one, a different block: whenever that half is intra its filter record is the [3, 3] "no filter" sentinel, and the decode refused with "an OBMC neighbour whose switchable interp filter was never recorded". Measured with a new EC_OBMCREC rung on three 2 s cuts of the 10-bit 1920x792 128-SB stream: cur (24,348) 16x16 left-neighbour 16x4, (150,340) 8x8 above-neighbour 4x8, (56,32) 16x32 above-neighbour 4x16 -- every one a pair whose first half is intra (cell_inter=false, ref=-1). overlappable_above/left now return the mi the MiInfo actually came from and obmc_blend reads that cell's filter. All three cuts pass the site; they now stop at the next frontier (the intra 16x4/4x16 strip on the inter block path, and a Golomb tail on one). Gate: a_real_aomenc_10bit_obmc_over_1to4_strip_neighbours_decodes_pixel_exact -- real aomenc, 10-bit 128x128x24, --enable-obmc=1 --enable-1to4-partitions=1 --min-partition-size=4, five seeds, every frame all planes exact vs ffmpeg, hard assert on the new obmc_pair_filter_hits counter (the pair-sourced neighbour shape). The refusal string stays as an upstream-desync guard. --- crates/ec-av1/src/decode.rs | 105 ++++++++++++++++++++++++++++++------ crates/ec-av1/src/stream.rs | 79 +++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 16 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index a0208ee7..d64ae82d 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2185,6 +2185,20 @@ pub(crate) fn obmc_rect_leaf_hits() -> usize { OBMC_RECT_LEAF_HITS.with(|c| c.get()) } +// lane-obmcrec r1: how many OBMC neighbours were taken from the SECOND half of +// a 4-wide/4-tall (1:4 strip) chroma pair -- the case whose interp filter used +// to be read from the pair's FIRST mi, which is a different block and, when +// that half is intra, carries the `[3, 3]` "no filter" sentinel (the three +// 1080p 10-bit film cuts refused there). The gate's proof the shape fired. +thread_local! { + static OBMC_PAIR_FILTER_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Current value of [`OBMC_PAIR_FILTER_HITS`]. +pub(crate) fn obmc_pair_filter_hits() -> usize { + OBMC_PAIR_FILTER_HITS.with(|c| c.get()) +} + /// Current value of [`OBMC_HITS_8`]. pub(crate) fn obmc_hits_8() -> usize { OBMC_HITS_8.with(|c| c.get()) @@ -18143,7 +18157,7 @@ fn overlappable_above( bw4: usize, mi_cols: usize, max_neighbors: usize, -) -> Vec<(usize, usize, MiInfo)> { +) -> Vec<(usize, usize, MiInfo, usize)> { let mut out = Vec::new(); if mi_row == 0 { return out; @@ -18154,17 +18168,25 @@ fn overlappable_above( let cell = grid.get(mi_row - 1, col); let mut step = cell.map_or(1, |c| c.size).max(1).min(SB_MI as usize); let mut nb = cell; + // lane-obmcrec r1: the mi the neighbour's `MiInfo` is actually taken + // from -- the pair merge below moves it, and libaom reads that same + // `mbmi`'s `interp_filters` (`av1_setup_build_prediction_by_above_pred`). + let mut src = col; if step == 1 { // The pair merge: the walk snaps back to the pair's even column // and the ODD half (the chroma-carrying one) is the neighbour for // both -- a 4-wide 1:4 strip is never an OBMC source on its own. col &= !1; - nb = grid.get(mi_row - 1, col + 1); + src = col + 1; + nb = grid.get(mi_row - 1, src); step = 2; } if let Some(info) = nb { if info.is_inter { - out.push((col - mi_col, step.min(end_col - col), *info)); + if src != col { + OBMC_PAIR_FILTER_HITS.with(|c| c.set(c.get() + 1)); + } + out.push((col - mi_col, step.min(end_col - col), *info, src - mi_col)); } } col += step; @@ -18181,7 +18203,7 @@ fn overlappable_left( bh4: usize, mi_rows: usize, max_neighbors: usize, -) -> Vec<(usize, usize, MiInfo)> { +) -> Vec<(usize, usize, MiInfo, usize)> { let mut out = Vec::new(); if mi_col == 0 { return out; @@ -18194,17 +18216,23 @@ fn overlappable_left( // width (8) would swallow the strip below it. let mut step = cell.map_or(1, |c| c.size_h).max(1).min(SB_MI as usize); let mut nb = cell; + // lane-obmcrec r1: see `overlappable_above`'s `src`. + let mut src = row; if step == 1 { // The pair merge, left-column mirror: a 4-TALL neighbour (a 16x4 // strip) is half of a chroma pair and the pair's SECOND row is the // neighbour for both. row &= !1; - nb = grid.get(row + 1, mi_col - 1); + src = row + 1; + nb = grid.get(src, mi_col - 1); step = 2; } if let Some(info) = nb { if info.is_inter { - out.push((row - mi_row, step.min(end_row - row), *info)); + if src != row { + OBMC_PAIR_FILTER_HITS.with(|c| c.set(c.get() + 1)); + } + out.push((row - mi_row, step.min(end_row - row), *info, src - mi_row)); } } row += step; @@ -18447,6 +18475,46 @@ fn neighbour_filter( )) } +/// lane-obmcrec r1 MEASUREMENT rung (`EC_OBMCREC=1`): every OBMC neighbour whose +/// recorded switchable-filter symbols are the `[3, 3]` "never recorded" sentinel +/// -- names the neighbour's own position/shape/reference so the decode path that +/// failed to record its filter can be identified. Read-only, off by default. +#[allow(clippy::too_many_arguments)] +fn obmcrec_probe( + dir: &str, + mi_row: usize, + mi_col: usize, + write_w: usize, + write_h: usize, + off4: usize, + span4: usize, + nb: &MiInfo, + syms: [u8; 2], + cell: Option, + fixed: bool, +) { + if (syms[0] <= 2 && syms[1] <= 2) || std::env::var_os("EC_OBMCREC").is_none() { + return; + } + eprintln!( + "OBMCREC {dir} cur_mi=({mi_row},{mi_col}) cur_wh=({write_w}x{write_h}) off4={off4} \ +span4={span4} syms={syms:?} fixed={fixed} nb_size=({}x{}) nb_ref={} nb_ref1={:?} nb_mv=({},{}) \ +nb_newmv={} cell_size={:?} cell_inter={:?} cell_ref={:?} sb_row_off={} sb_col_off={}", + nb.size * 4, + nb.size_h * 4, + nb.ref_frame, + nb.ref_frame1, + nb.mv.0, + nb.mv.1, + nb.is_new_mv, + cell.map(|c| (c.size * 4, c.size_h * 4)), + cell.map(|c| c.is_inter), + cell.map(|c| c.ref_frame), + mi_row % 32, + mi_col % 32, + ); +} + /// One neighbour's own re-prediction over a `w`x`h` region, MC'd fresh /// against its own `mv`/reference/filter (libaom `av1_build_inter_predictor` /// under `av1_setup_build_prediction_by_{above,left}_pred`). @@ -18778,7 +18846,7 @@ fn obmc_blend( // ABOVE pass skips chroma entirely, the LEFT pass still blends it. let skip_chroma_above = matches!((write_w, write_h), (8, 8) | (16, 8) | (8, 16)); - for (off4, span4, nb) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) + for (off4, span4, nb, src4) in overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, max_nb(bw4)) { // `Neighbours::above_filter` is indexed in `SUB`(16px)-wide columns // (this decoder's own outer block-loop granularity), one step @@ -18789,10 +18857,12 @@ fn obmc_blend( // `above_mbmi->interp_filters`); the 16px-granular `Neighbours` slot // stays as the fallback for paths that do not record one yet // (compound), which is what every earlier round used. - let (h_kind, v_kind) = neighbour_filter( - interp_fixed, - neighbours.above_filter[mi_col + off4], - )?; + let above_syms = neighbours.above_filter[mi_col + src4]; + obmcrec_probe( + "above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, above_syms, + grid.get(mi_row - 1, mi_col + src4).copied(), interp_fixed.is_some(), + ); + let (h_kind, v_kind) = neighbour_filter(interp_fixed, above_syms)?; ec_obmc_trace("above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, h_kind, v_kind); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; let nb_scale = mc::scale_factor(ny.width, frame_width); @@ -18808,11 +18878,14 @@ fn obmc_blend( } } - for (off4, span4, nb) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) { - let (h_kind, v_kind) = neighbour_filter( - interp_fixed, - neighbours.left_filter[mi_row + off4], - )?; + for (off4, span4, nb, src4) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) + { + let left_syms = neighbours.left_filter[mi_row + src4]; + obmcrec_probe( + "left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, left_syms, + grid.get(mi_row + src4, mi_col - 1).copied(), interp_fixed.is_some(), + ); + let (h_kind, v_kind) = neighbour_filter(interp_fixed, left_syms)?; ec_obmc_trace("left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, h_kind, v_kind); let (ny, nu, nv) = ref_planes(nb.ref_frame, ref_y, ref_u, ref_v, other_refs)?; let nb_scale = mc::scale_factor(ny.width, frame_width); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index ca57ca7a..f6464425 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -160,6 +160,12 @@ pub fn sb128_ab_counters() -> [usize; 4] { crate::decode::part128_ab_hits() } +/// lane-obmcrec r1: OBMC neighbours taken from the second half of a 1:4-strip +/// chroma pair -- the shape whose filter record the r1 fix re-sourced. +pub fn obmc_pair_filter_hits() -> usize { + crate::decode::obmc_pair_filter_hits() +} + pub fn rect4_32_counters() -> (usize, usize, usize) { ( crate::decode::rect4_32_horz_hits(), @@ -4476,6 +4482,15 @@ mod tests { /// Seeds swept by the 10-bit LR gate (the 8-bit gate needs ~40 attempts /// to land a non-RESTORE_NONE frame; every attempt here still has to /// decode pixel-exact). + /// lane-obmcrec r1: the `LR_SEEDS` members whose 1:4-partition stream both + /// fires `obmc_pair_filter_hits` and decodes every frame pixel-exact. + /// Measured over seeds 42..=73 (32 encodes): 43/46/47/65/66 fire and are + /// exact; 70 and 73 fire and mismatch, but mismatch IDENTICALLY with + /// `--enable-obmc=0` (seed 73 frame 1 luma, seed 70 frame 16), so their + /// defect is in the `--min-partition-size=4` recipe, not in OBMC -- kept + /// out of this gate rather than papered over, and reported as open. + const OBMC_PAIR_SEEDS: [u32; 5] = [43, 46, 47, 65, 66]; + const LR_SEEDS: [u32; 16] = [42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; /// Wiener + SGR + switchable loop-restoration firings, summed. @@ -4616,6 +4631,70 @@ mod tests { ); } + /// lane-obmcrec r1: OBMC whose above/left neighbour is a 4-wide/4-tall + /// 1:4-partition strip -- the shape the three 1080p 10-bit film cuts + /// refused on ("an OBMC neighbour whose switchable interp filter was never + /// recorded"). `overlappable_above`/`_left` snap such a neighbour to the + /// chroma pair's SECOND mi but used to read its interp filter from the + /// FIRST one, a different block whose filter record is the intra + /// `[3, 3]` sentinel whenever that half is intra. `obmc_pair_filter_hits` + /// counts exactly that neighbour shape, so a stream without 1:4 strips + /// next to an OBMC block cannot stand in for it. + #[test] + fn a_real_aomenc_10bit_obmc_over_1to4_strip_neighbours_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_10bit_obmc_over_1to4_strip_neighbours_decodes_pixel_exact"; + if !have_ffmpeg() { + eprintln!("SKIP {NAME}: no ffmpeg"); + return; + } + if !have_aomenc() { + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); + return; + } + ten_bit_tool_gate( + NAME, + 128, + 128, + 24, + &[ + "--ec-fixture-noise", + "--cq-level=45", + "--kf-max-dist=1000", + "--auto-alt-ref=1", + "--lag-in-frames=16", + "--enable-fwd-kf=0", + "--enable-order-hint=1", + "--enable-warped-motion=0", + "--enable-obmc=1", + "--tune-content=default", + "--enable-masked-comp=0", + "--enable-interintra-comp=0", + "--enable-onesided-comp=0", + "--enable-interintra-wedge=0", + "--enable-smooth-interintra=0", + "--enable-rect-partitions=1", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=1", + "--enable-filter-intra=0", + "--enable-smooth-intra=0", + "--enable-paeth-intra=0", + "--enable-directional-intra=0", + "--enable-angle-delta=0", + "--enable-tx-size-search=0", + "--enable-cdef=0", + "--enable-restoration=0", + "--max-partition-size=32", + "--min-partition-size=4", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=0", + "--enable-ref-frame-mvs=0", + ], + &OBMC_PAIR_SEEDS, + &[("obmc_pair_filter_hits", decode::obmc_pair_filter_hits)], + ); + } + /// `bit_depth` of the first sequence header in `stream`. fn parsed_bit_depth(stream: &[u8]) -> u8 { let mut probe = Av1Parser::new();