From 84a1ea3cad68fe410752be08c2df838cb345ad50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 16:45:20 +0300 Subject: [PATCH] feat(av1): an OBMC candidate for the 32x32 single-reference winner, measured and defaulted off The encoder now offers the 32x32 single-reference winner an OBMC_CAUSAL candidate built by the DECODER's own plan and blend (decode::obmc_plan / obmc_run, libaom av1_build_obmc_inter_prediction) off the committed neighbours' mvs the search grid already carries, prices the motion_mode symbol against the default obmc CDF, and the tile writer codes whichever value won. obmc_plan lost its Neighbours parameter for the two filter bands themselves so an encoder with a fixed interpolation filter can call it; no second implementation of the blend exists. MEASURED and NOT KEPT ON: native crop BD with it on is +18.2/+0.9, +47.3/+19.2, +59.1/-10.0 against +18.0/+0.7, +47.3/+19.2, +59.4/-10.0 off -- one row down, one flat, one up, short of the two-down-one-flat rule. It is behind EC_AV1_OBMC=1 with the numbers at the decision site; the default path is byte-identical to the SIMPLE-only commit before it. Only the 32x32 winner is offered OBMC and the price is the static-CDF estimate: the leaves and a real-CDF price are the upgrade path. Byte-exact either way: EC_COMP_MISMATCH=1 prints nothing with OBMC on, and every decode round trip (our own decoder and ffmpeg) is sample-exact. --- crates/ec-av1/src/decode.rs | 25 +++-- crates/ec-av1/src/encode.rs | 189 +++++++++++++++++++++++++++++++++++- 2 files changed, 204 insertions(+), 10 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 73dffedb..8036e855 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -24772,9 +24772,18 @@ nbmv=({},{}) nbref={} nbbsize={} filt={}", /// how many bordering blocks actually get blended, separate from the /// eligibility scan's own unbounded ("any at all") walk. #[allow(clippy::too_many_arguments)] -fn obmc_plan( +pub(crate) fn obmc_plan( grid: &MiGrid, - neighbours: &Neighbours, + // The two switchable-filter bands (`Neighbours::above_filter`/ + // `left_filter`), read only when `interp_fixed` is `None`. The ENCODER + // (`encode.rs`'s OBMC candidate) codes a fixed interpolation filter and + // has no `Neighbours` of its own, so it passes both empty; the reads + // below fall back to the `[3, 3]` never-recorded sentinel, which + // `neighbour_filter` never reaches under a fixed filter. + // corner-cut: ceiling is a switchable-filter ENCODER -- it must build and + // pass the real bands here, exactly as the decoder does. + above_filter: &[[u8; 2]], + left_filter: &[[u8; 2]], mi_row: usize, mi_col: usize, bw4: usize, @@ -24841,7 +24850,7 @@ fn obmc_plan( // neighbour's OWN mi-granular filter (libaom // `av1_setup_build_prediction_by_above_pred` reads // `above_mbmi->interp_filters`). - let above_syms = neighbours.above_filter[mi_col + src4]; + let above_syms = above_filter.get(mi_col + src4).copied().unwrap_or([3; 2]); obmcrec_probe( "above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, above_syms, grid.get(mi_row - 1, mi_col + src4), interp_fixed.is_some(), @@ -24865,7 +24874,7 @@ fn obmc_plan( let mut left = Vec::new(); 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]; + let left_syms = left_filter.get(mi_row + src4).copied().unwrap_or([3; 2]); obmcrec_probe( "left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, left_syms, grid.get(mi_row + src4, mi_col - 1), interp_fixed.is_some(), @@ -24940,7 +24949,7 @@ pub(crate) struct ObmcPlan { /// (libaom `av1_build_obmc_inter_prediction`). Reads nothing but reference /// pixels and the plan, so it runs wherever the block's prediction is built /// -- on a recon worker when that prediction is deferred (lane-defer8). -fn obmc_run( +pub(crate) fn obmc_run( plan: &ObmcPlan, refpix: &RefPix<'_>, pred_y: &mut [u16], @@ -27622,7 +27631,8 @@ fn decode_inter_block( let obmc = if obmc_selected { Some(obmc_plan( grid, - neighbours, + &neighbours.above_filter, + &neighbours.left_filter, mi_row, mi_col, bw4, @@ -32209,7 +32219,8 @@ fn decode_inter_block8( let obmc = if obmc_selected { Some(obmc_plan( grid, - neighbours, + &neighbours.above_filter, + &neighbours.left_filter, mi_row, mi_col, 2, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index a176fb47..edcbe2c1 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2237,6 +2237,16 @@ impl Reach { } /// What one symbol costs against a CDF, in bits. +/// What the `motion_mode`/`obmc` symbol costs (spec 5.11.24), off the default +/// table rather than the tile's adapted one -- the same static-CDF estimate +/// every other symbol in this search is priced with. +fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 { + let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else { + return 0.0; + }; + symbol_bits(&cdf::OBMC[row], usize::from(obmc)) +} + pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; let width = cdf[symbol] - low; @@ -5614,7 +5624,15 @@ fn search_inter_block( // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by // the caller off the same MI grid the tile writer will (empty unless the // frame carries `reference_select`). - compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], + // lane-av1obmc: the mi grid this search's neighbours were published into + // (`record_mi`) and this block's own place in it -- what the OBMC + // candidate's eligibility walk and neighbour plan read, the same state + // the tile writer will read when it codes the `motion_mode` symbol. + grid: &MiGrid, + (mi_row, mi_col): (usize, usize), + (mi_rows, mi_cols): (usize, usize), + fctx: &crate::decode::FrameCtx, ) -> (BlockCoeffs, f64) { // `luma_set` is the INTRA candidates' table; the two inter candidates // below price their coefficients through `TxbSet::Luma32Inter`, which is @@ -5647,6 +5665,10 @@ fn search_inter_block( mode: u8, skip: bool, inter: Option, + /// The mode/mv syntax this candidate pays for, apart from its + /// residual -- what the OBMC candidate below has to re-pay when it + /// re-prices the winner's prediction. + mode_bits: f64, } let mut best: Option = None; let mut consider = |candidate: Candidate| { @@ -5741,6 +5763,7 @@ fn search_inter_block( mode, skip: false, inter: None, + mode_bits: mode_bits[usize::from(mode)], }); } @@ -6152,6 +6175,7 @@ fn search_inter_block( v, mode: DC_PRED, skip, + mode_bits: bits, inter: Some(info), }); } @@ -6249,12 +6273,150 @@ fn search_inter_block( v, mode: DC_PRED, skip, + mode_bits: mode_bits_inter, inter: Some(info), }); } } let best = best.expect("the search offers at least the intra modes"); + // lane-av1obmc: the OBMC candidate (spec `motion_mode == OBMC_CAUSAL`), + // offered to the SINGLE-REFERENCE winner only -- libaom's + // `motion_mode_allowed` fails `is_motion_variation_allowed_compound` on a + // compound block, so the writer codes no symbol for one. The prediction + // is built by the DECODER's own plan and blend + // ([`crate::decode::obmc_plan`]/[`crate::decode::obmc_run`], + // `av1_build_obmc_inter_prediction`) off the committed neighbours' mvs + // this search's `grid` already carries, so what is priced here is what a + // decoder reconstructs -- never a second implementation of the blend. + // + // MEASURED 2026-09-06 and NOT KEPT ON BY DEFAULT (`EC_AV1_OBMC=1` turns it + // on). Native crop BD, the standing recipe, with it on against the same + // build with it off: film 1080p +18.2/+0.9 against +18.0/+0.7, film 2160p + // +47.3/+19.2 against +47.3/+19.2, screen +59.1/-10.0 against + // +59.4/-10.0. That is one row down (screen -0.3 vs libaom), one flat and + // one UP -- the keep rule wants two down and one flat, so it stays off. + // The 640x384 gate reads the same shape louder: +79.7/+95.5/+54.2 and + // +37.2/+56.8/+0.8 against +78.8/+95.1/+54.5 and +36.1/+56.4/+1.1. Two + // things bound it and are the upgrade path: only the 32x32 winner is + // offered OBMC (the 16x16/8x8 leaves in `code_square_inter` are not, and + // that is where libaom cashes most of its OBMC gain), and the symbol/ + // residual price here is the static-CDF estimate. + let mut best = best; + let mut obmc_won = false; + if let Some(info) = best.inter.filter(|i| i.ref1.is_none()) { + if crate::envflags::env_flag!("EC_AV1_OBMC") + && crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, 8, 8, mi_cols, mi_rows) + { + let mut refs: [Option<&Picture>; 8] = [None; 8]; + refs[LAST_FRAME as usize] = Some(reference); + for &(r, p, _) in extra { + refs[r as usize] = Some(p); + } + let refpix = crate::decode::RefPix::ready(refs); + let plan = crate::decode::obmc_plan( + grid, + &[], + &[], + mi_row, + mi_col, + 8, + 8, + mi_rows, + mi_cols, + BLOCK, + BLOCK, + BLOCK, + BLOCK / 2, + x, + y, + x / 2, + y / 2, + &refpix, + Some(mc::InterpFilterKind::Regular), + reference.width, + ); + if let Ok(plan) = plan { + let (ref_luma, ref_u, ref_v) = match extra + .iter() + .find(|(r, _, _)| *r == info.ref_frame) + { + Some(&(_, g, _)) => ( + (&g.y, g.width, luma.true_width, luma.true_height), + (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), + (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), + ), + None => (ref_luma, ref_u, ref_v), + }; + let mv = info.mv; + let mut pred = [ + vec![0u16; BLOCK * BLOCK], + vec![0u16; BLOCK * BLOCK / 4], + vec![0u16; BLOCK * BLOCK / 4], + ]; + for (i, (r, luma_plane)) in + [(ref_luma, true), (ref_u, false), (ref_v, false)].into_iter().enumerate() + { + let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) }; + let side = if luma_plane { BLOCK } else { BLOCK / 2 }; + mc::predict( + r.0, + r.1, + r.2, + r.3, + mv_to_q4(px, mv.1, luma_plane), + mv_to_q4(py, mv.0, luma_plane), + side, + side, + &mut pred[i], + fctx, + ); + } + let [mut py_buf, mut pu_buf, mut pv_buf] = pred; + crate::decode::obmc_run( + &plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx, + ); + let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::>(); + let luma_trial = luma.code_from_prediction( + x, y, BLOCK, &u8s(&py_buf), false, search.base_q_idx, search.deadzone, + TxbSet::Luma32Inter, + ); + let u = chroma[0].code_from_prediction( + x / 2, y / 2, BLOCK / 2, &u8s(&pu_buf), false, search.base_q_idx, + search.deadzone, chroma_set, + ); + let v = chroma[1].code_from_prediction( + x / 2, y / 2, BLOCK / 2, &u8s(&pv_buf), false, search.base_q_idx, + search.deadzone, chroma_set, + ); + let skip = luma_trial.levels.iter().all(|&l| l == 0) + && u.levels.iter().all(|&l| l == 0) + && v.levels.iter().all(|&l| l == 0); + let cost = luma_trial.sse + + u.sse + + v.sse + + search.lambda + * (skip_bits(skip) + + intra_inter_bits(true) + + best.mode_bits + + obmc_bits(BLOCK, BLOCK, true) + + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); + if cost < best.cost + search.lambda * obmc_bits(BLOCK, BLOCK, false) { + obmc_won = true; + best = Candidate { + cost, + luma: luma_trial, + u, + v, + mode: DC_PRED, + skip, + mode_bits: best.mode_bits, + inter: best.inter, + }; + } + } + } + } // lane-av1txbits BUILT the transform-depth search this path lacks -- a // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 // units against its flat trial, exactly as a 16x16 winner is -- and @@ -6276,7 +6438,7 @@ fn search_inter_block( uv_mode: DC_PRED, skip: best.skip, inter: best.inter, - obmc: false, + obmc: obmc_won, eight: None, dv: None, palette: None, @@ -6656,7 +6818,11 @@ pub(crate) fn encode_inter_frame( reference, &stack, &extra, - &compound, fctx, + &compound, + &grid, + (mi_row, mi_col), + (mi_rows, mi_cols), + fctx, ); cost_whole += search.lambda * partition_bits(BLOCK, false); let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); @@ -9518,6 +9684,11 @@ mod tests { // remaining gap is the syntax the coefficient sum leaves out. That is // the default on a non-screen frame since lane-av1price2 (this // fixture is one), and it reads +32.6% here. + // lane-av1obmc: every eligible single-reference inter block now also + // carries a `motion_mode` symbol the coefficient sum never counts, + // and an OBMC block carries the more expensive of the two values -- + // re-measured at +35.0% here with `EC_AV1_OBMC=1`; the default path + // (OBMC off, SIMPLE symbols only) reads +33.x. assert!( worst_under <= 0.34, "the writer spent {:.2}% more than the search priced -- more than \ @@ -11042,6 +11213,18 @@ mod tests { .collect::>() .join(" ") ); + // lane-av1obmc: how many blocks actually CODED a motion_mode + // symbol and which value they took, by footprint -- "OBMC is on" + // as a measurement rather than a claim (class + // `gate-blind-to-feature`). + let mm = crate::tile::take_motion_mode_hits(); + eprintln!( + "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \ + 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)", + mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], + 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::().max(1) as f64, + mm.iter().sum::(), + ); let (aom, aom_wall) = external_ladder(&source, width, height, "libaom-av1", &aom_points); let (rav1e, rav1e_wall) =