An OBMC candidate for the 32x32 single-reference winner, measured and defaulted off
Browse files at this commit84a1ea3cad68fe410752be08c2df838cb345ad50Parent: ba4398c- feat
- an OBMC candidate for the 32x32 single-reference winner, measured and defaulted off
2 changed files +204 -10
- crates/ec-av1/src/decode.rs +18 -7
- crates/ec-av1/src/encode.rs +186 -3
crates/ec-av1/src/decode.rsmodified+18 -7
| @@ -24772,9 +24772,18 @@ nbmv=({},{}) nbref={} nbbsize={} filt={}", | ||
| 24772 | 24772 | /// how many bordering blocks actually get blended, separate from the |
| 24773 | 24773 | /// eligibility scan's own unbounded ("any at all") walk. |
| 24774 | 24774 | #[allow(clippy::too_many_arguments)] |
| 24775 | -fn obmc_plan( | |
| 24775 | +pub(crate) fn obmc_plan( | |
| 24776 | 24776 | grid: &MiGrid, |
| 24777 | - neighbours: &Neighbours, | |
| 24777 | + // The two switchable-filter bands (`Neighbours::above_filter`/ | |
| 24778 | + // `left_filter`), read only when `interp_fixed` is `None`. The ENCODER | |
| 24779 | + // (`encode.rs`'s OBMC candidate) codes a fixed interpolation filter and | |
| 24780 | + // has no `Neighbours` of its own, so it passes both empty; the reads | |
| 24781 | + // below fall back to the `[3, 3]` never-recorded sentinel, which | |
| 24782 | + // `neighbour_filter` never reaches under a fixed filter. | |
| 24783 | + // corner-cut: ceiling is a switchable-filter ENCODER -- it must build and | |
| 24784 | + // pass the real bands here, exactly as the decoder does. | |
| 24785 | + above_filter: &[[u8; 2]], | |
| 24786 | + left_filter: &[[u8; 2]], | |
| 24778 | 24787 | mi_row: usize, |
| 24779 | 24788 | mi_col: usize, |
| 24780 | 24789 | bw4: usize, |
| @@ -24841,7 +24850,7 @@ fn obmc_plan( | ||
| 24841 | 24850 | // neighbour's OWN mi-granular filter (libaom |
| 24842 | 24851 | // `av1_setup_build_prediction_by_above_pred` reads |
| 24843 | 24852 | // `above_mbmi->interp_filters`). |
| 24844 | - let above_syms = neighbours.above_filter[mi_col + src4]; | |
| 24853 | + let above_syms = above_filter.get(mi_col + src4).copied().unwrap_or([3; 2]); | |
| 24845 | 24854 | obmcrec_probe( |
| 24846 | 24855 | "above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, above_syms, |
| 24847 | 24856 | grid.get(mi_row - 1, mi_col + src4), interp_fixed.is_some(), |
| @@ -24865,7 +24874,7 @@ fn obmc_plan( | ||
| 24865 | 24874 | let mut left = Vec::new(); |
| 24866 | 24875 | for (off4, span4, nb, src4) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4)) |
| 24867 | 24876 | { |
| 24868 | - let left_syms = neighbours.left_filter[mi_row + src4]; | |
| 24877 | + let left_syms = left_filter.get(mi_row + src4).copied().unwrap_or([3; 2]); | |
| 24869 | 24878 | obmcrec_probe( |
| 24870 | 24879 | "left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, left_syms, |
| 24871 | 24880 | grid.get(mi_row + src4, mi_col - 1), interp_fixed.is_some(), |
| @@ -24940,7 +24949,7 @@ pub(crate) struct ObmcPlan { | ||
| 24940 | 24949 | /// (libaom `av1_build_obmc_inter_prediction`). Reads nothing but reference |
| 24941 | 24950 | /// pixels and the plan, so it runs wherever the block's prediction is built |
| 24942 | 24951 | /// -- on a recon worker when that prediction is deferred (lane-defer8). |
| 24943 | -fn obmc_run( | |
| 24952 | +pub(crate) fn obmc_run( | |
| 24944 | 24953 | plan: &ObmcPlan, |
| 24945 | 24954 | refpix: &RefPix<'_>, |
| 24946 | 24955 | pred_y: &mut [u16], |
| @@ -27622,7 +27631,8 @@ fn decode_inter_block( | ||
| 27622 | 27631 | let obmc = if obmc_selected { |
| 27623 | 27632 | Some(obmc_plan( |
| 27624 | 27633 | grid, |
| 27625 | - neighbours, | |
| 27634 | + &neighbours.above_filter, | |
| 27635 | + &neighbours.left_filter, | |
| 27626 | 27636 | mi_row, |
| 27627 | 27637 | mi_col, |
| 27628 | 27638 | bw4, |
| @@ -32209,7 +32219,8 @@ fn decode_inter_block8( | ||
| 32209 | 32219 | let obmc = if obmc_selected { |
| 32210 | 32220 | Some(obmc_plan( |
| 32211 | 32221 | grid, |
| 32212 | - neighbours, | |
| 32222 | + &neighbours.above_filter, | |
| 32223 | + &neighbours.left_filter, | |
| 32213 | 32224 | mi_row, |
| 32214 | 32225 | mi_col, |
| 32215 | 32226 | 2, |
crates/ec-av1/src/encode.rsmodified+186 -3
| @@ -2237,6 +2237,16 @@ impl Reach { | ||
| 2237 | 2237 | } |
| 2238 | 2238 | |
| 2239 | 2239 | /// What one symbol costs against a CDF, in bits. |
| 2240 | +/// What the `motion_mode`/`obmc` symbol costs (spec 5.11.24), off the default | |
| 2241 | +/// table rather than the tile's adapted one -- the same static-CDF estimate | |
| 2242 | +/// every other symbol in this search is priced with. | |
| 2243 | +fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 { | |
| 2244 | + let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else { | |
| 2245 | + return 0.0; | |
| 2246 | + }; | |
| 2247 | + symbol_bits(&cdf::OBMC[row], usize::from(obmc)) | |
| 2248 | +} | |
| 2249 | + | |
| 2240 | 2250 | pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { |
| 2241 | 2251 | let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; |
| 2242 | 2252 | let width = cdf[symbol] - low; |
| @@ -5614,7 +5624,15 @@ fn search_inter_block( | ||
| 5614 | 5624 | // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by |
| 5615 | 5625 | // the caller off the same MI grid the tile writer will (empty unless the |
| 5616 | 5626 | // frame carries `reference_select`). |
| 5617 | - compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, | |
| 5627 | + compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], | |
| 5628 | + // lane-av1obmc: the mi grid this search's neighbours were published into | |
| 5629 | + // (`record_mi`) and this block's own place in it -- what the OBMC | |
| 5630 | + // candidate's eligibility walk and neighbour plan read, the same state | |
| 5631 | + // the tile writer will read when it codes the `motion_mode` symbol. | |
| 5632 | + grid: &MiGrid, | |
| 5633 | + (mi_row, mi_col): (usize, usize), | |
| 5634 | + (mi_rows, mi_cols): (usize, usize), | |
| 5635 | + fctx: &crate::decode::FrameCtx, | |
| 5618 | 5636 | ) -> (BlockCoeffs, f64) { |
| 5619 | 5637 | // `luma_set` is the INTRA candidates' table; the two inter candidates |
| 5620 | 5638 | // below price their coefficients through `TxbSet::Luma32Inter`, which is |
| @@ -5647,6 +5665,10 @@ fn search_inter_block( | ||
| 5647 | 5665 | mode: u8, |
| 5648 | 5666 | skip: bool, |
| 5649 | 5667 | inter: Option<InterInfo>, |
| 5668 | + /// The mode/mv syntax this candidate pays for, apart from its | |
| 5669 | + /// residual -- what the OBMC candidate below has to re-pay when it | |
| 5670 | + /// re-prices the winner's prediction. | |
| 5671 | + mode_bits: f64, | |
| 5650 | 5672 | } |
| 5651 | 5673 | let mut best: Option<Candidate> = None; |
| 5652 | 5674 | let mut consider = |candidate: Candidate| { |
| @@ -5741,6 +5763,7 @@ fn search_inter_block( | ||
| 5741 | 5763 | mode, |
| 5742 | 5764 | skip: false, |
| 5743 | 5765 | inter: None, |
| 5766 | + mode_bits: mode_bits[usize::from(mode)], | |
| 5744 | 5767 | }); |
| 5745 | 5768 | } |
| 5746 | 5769 | |
| @@ -6152,6 +6175,7 @@ fn search_inter_block( | ||
| 6152 | 6175 | v, |
| 6153 | 6176 | mode: DC_PRED, |
| 6154 | 6177 | skip, |
| 6178 | + mode_bits: bits, | |
| 6155 | 6179 | inter: Some(info), |
| 6156 | 6180 | }); |
| 6157 | 6181 | } |
| @@ -6249,12 +6273,150 @@ fn search_inter_block( | ||
| 6249 | 6273 | v, |
| 6250 | 6274 | mode: DC_PRED, |
| 6251 | 6275 | skip, |
| 6276 | + mode_bits: mode_bits_inter, | |
| 6252 | 6277 | inter: Some(info), |
| 6253 | 6278 | }); |
| 6254 | 6279 | } |
| 6255 | 6280 | } |
| 6256 | 6281 | |
| 6257 | 6282 | let best = best.expect("the search offers at least the intra modes"); |
| 6283 | + // lane-av1obmc: the OBMC candidate (spec `motion_mode == OBMC_CAUSAL`), | |
| 6284 | + // offered to the SINGLE-REFERENCE winner only -- libaom's | |
| 6285 | + // `motion_mode_allowed` fails `is_motion_variation_allowed_compound` on a | |
| 6286 | + // compound block, so the writer codes no symbol for one. The prediction | |
| 6287 | + // is built by the DECODER's own plan and blend | |
| 6288 | + // ([`crate::decode::obmc_plan`]/[`crate::decode::obmc_run`], | |
| 6289 | + // `av1_build_obmc_inter_prediction`) off the committed neighbours' mvs | |
| 6290 | + // this search's `grid` already carries, so what is priced here is what a | |
| 6291 | + // decoder reconstructs -- never a second implementation of the blend. | |
| 6292 | + // | |
| 6293 | + // MEASURED 2026-09-06 and NOT KEPT ON BY DEFAULT (`EC_AV1_OBMC=1` turns it | |
| 6294 | + // on). Native crop BD, the standing recipe, with it on against the same | |
| 6295 | + // build with it off: film 1080p +18.2/+0.9 against +18.0/+0.7, film 2160p | |
| 6296 | + // +47.3/+19.2 against +47.3/+19.2, screen +59.1/-10.0 against | |
| 6297 | + // +59.4/-10.0. That is one row down (screen -0.3 vs libaom), one flat and | |
| 6298 | + // one UP -- the keep rule wants two down and one flat, so it stays off. | |
| 6299 | + // The 640x384 gate reads the same shape louder: +79.7/+95.5/+54.2 and | |
| 6300 | + // +37.2/+56.8/+0.8 against +78.8/+95.1/+54.5 and +36.1/+56.4/+1.1. Two | |
| 6301 | + // things bound it and are the upgrade path: only the 32x32 winner is | |
| 6302 | + // offered OBMC (the 16x16/8x8 leaves in `code_square_inter` are not, and | |
| 6303 | + // that is where libaom cashes most of its OBMC gain), and the symbol/ | |
| 6304 | + // residual price here is the static-CDF estimate. | |
| 6305 | + let mut best = best; | |
| 6306 | + let mut obmc_won = false; | |
| 6307 | + if let Some(info) = best.inter.filter(|i| i.ref1.is_none()) { | |
| 6308 | + if crate::envflags::env_flag!("EC_AV1_OBMC") | |
| 6309 | + && crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, 8, 8, mi_cols, mi_rows) | |
| 6310 | + { | |
| 6311 | + let mut refs: [Option<&Picture>; 8] = [None; 8]; | |
| 6312 | + refs[LAST_FRAME as usize] = Some(reference); | |
| 6313 | + for &(r, p, _) in extra { | |
| 6314 | + refs[r as usize] = Some(p); | |
| 6315 | + } | |
| 6316 | + let refpix = crate::decode::RefPix::ready(refs); | |
| 6317 | + let plan = crate::decode::obmc_plan( | |
| 6318 | + grid, | |
| 6319 | + &[], | |
| 6320 | + &[], | |
| 6321 | + mi_row, | |
| 6322 | + mi_col, | |
| 6323 | + 8, | |
| 6324 | + 8, | |
| 6325 | + mi_rows, | |
| 6326 | + mi_cols, | |
| 6327 | + BLOCK, | |
| 6328 | + BLOCK, | |
| 6329 | + BLOCK, | |
| 6330 | + BLOCK / 2, | |
| 6331 | + x, | |
| 6332 | + y, | |
| 6333 | + x / 2, | |
| 6334 | + y / 2, | |
| 6335 | + &refpix, | |
| 6336 | + Some(mc::InterpFilterKind::Regular), | |
| 6337 | + reference.width, | |
| 6338 | + ); | |
| 6339 | + if let Ok(plan) = plan { | |
| 6340 | + let (ref_luma, ref_u, ref_v) = match extra | |
| 6341 | + .iter() | |
| 6342 | + .find(|(r, _, _)| *r == info.ref_frame) | |
| 6343 | + { | |
| 6344 | + Some(&(_, g, _)) => ( | |
| 6345 | + (&g.y, g.width, luma.true_width, luma.true_height), | |
| 6346 | + (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), | |
| 6347 | + (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), | |
| 6348 | + ), | |
| 6349 | + None => (ref_luma, ref_u, ref_v), | |
| 6350 | + }; | |
| 6351 | + let mv = info.mv; | |
| 6352 | + let mut pred = [ | |
| 6353 | + vec![0u16; BLOCK * BLOCK], | |
| 6354 | + vec![0u16; BLOCK * BLOCK / 4], | |
| 6355 | + vec![0u16; BLOCK * BLOCK / 4], | |
| 6356 | + ]; | |
| 6357 | + for (i, (r, luma_plane)) in | |
| 6358 | + [(ref_luma, true), (ref_u, false), (ref_v, false)].into_iter().enumerate() | |
| 6359 | + { | |
| 6360 | + let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) }; | |
| 6361 | + let side = if luma_plane { BLOCK } else { BLOCK / 2 }; | |
| 6362 | + mc::predict( | |
| 6363 | + r.0, | |
| 6364 | + r.1, | |
| 6365 | + r.2, | |
| 6366 | + r.3, | |
| 6367 | + mv_to_q4(px, mv.1, luma_plane), | |
| 6368 | + mv_to_q4(py, mv.0, luma_plane), | |
| 6369 | + side, | |
| 6370 | + side, | |
| 6371 | + &mut pred[i], | |
| 6372 | + fctx, | |
| 6373 | + ); | |
| 6374 | + } | |
| 6375 | + let [mut py_buf, mut pu_buf, mut pv_buf] = pred; | |
| 6376 | + crate::decode::obmc_run( | |
| 6377 | + &plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx, | |
| 6378 | + ); | |
| 6379 | + let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>(); | |
| 6380 | + let luma_trial = luma.code_from_prediction( | |
| 6381 | + x, y, BLOCK, &u8s(&py_buf), false, search.base_q_idx, search.deadzone, | |
| 6382 | + TxbSet::Luma32Inter, | |
| 6383 | + ); | |
| 6384 | + let u = chroma[0].code_from_prediction( | |
| 6385 | + x / 2, y / 2, BLOCK / 2, &u8s(&pu_buf), false, search.base_q_idx, | |
| 6386 | + search.deadzone, chroma_set, | |
| 6387 | + ); | |
| 6388 | + let v = chroma[1].code_from_prediction( | |
| 6389 | + x / 2, y / 2, BLOCK / 2, &u8s(&pv_buf), false, search.base_q_idx, | |
| 6390 | + search.deadzone, chroma_set, | |
| 6391 | + ); | |
| 6392 | + let skip = luma_trial.levels.iter().all(|&l| l == 0) | |
| 6393 | + && u.levels.iter().all(|&l| l == 0) | |
| 6394 | + && v.levels.iter().all(|&l| l == 0); | |
| 6395 | + let cost = luma_trial.sse | |
| 6396 | + + u.sse | |
| 6397 | + + v.sse | |
| 6398 | + + search.lambda | |
| 6399 | + * (skip_bits(skip) | |
| 6400 | + + intra_inter_bits(true) | |
| 6401 | + + best.mode_bits | |
| 6402 | + + obmc_bits(BLOCK, BLOCK, true) | |
| 6403 | + + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); | |
| 6404 | + if cost < best.cost + search.lambda * obmc_bits(BLOCK, BLOCK, false) { | |
| 6405 | + obmc_won = true; | |
| 6406 | + best = Candidate { | |
| 6407 | + cost, | |
| 6408 | + luma: luma_trial, | |
| 6409 | + u, | |
| 6410 | + v, | |
| 6411 | + mode: DC_PRED, | |
| 6412 | + skip, | |
| 6413 | + mode_bits: best.mode_bits, | |
| 6414 | + inter: best.inter, | |
| 6415 | + }; | |
| 6416 | + } | |
| 6417 | + } | |
| 6418 | + } | |
| 6419 | + } | |
| 6258 | 6420 | // lane-av1txbits BUILT the transform-depth search this path lacks -- a |
| 6259 | 6421 | // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 |
| 6260 | 6422 | // units against its flat trial, exactly as a 16x16 winner is -- and |
| @@ -6276,7 +6438,7 @@ fn search_inter_block( | ||
| 6276 | 6438 | uv_mode: DC_PRED, |
| 6277 | 6439 | skip: best.skip, |
| 6278 | 6440 | inter: best.inter, |
| 6279 | - obmc: false, | |
| 6441 | + obmc: obmc_won, | |
| 6280 | 6442 | eight: None, |
| 6281 | 6443 | dv: None, |
| 6282 | 6444 | palette: None, |
| @@ -6656,7 +6818,11 @@ pub(crate) fn encode_inter_frame( | ||
| 6656 | 6818 | reference, |
| 6657 | 6819 | &stack, |
| 6658 | 6820 | &extra, |
| 6659 | - &compound, fctx, | |
| 6821 | + &compound, | |
| 6822 | + &grid, | |
| 6823 | + (mi_row, mi_col), | |
| 6824 | + (mi_rows, mi_cols), | |
| 6825 | + fctx, | |
| 6660 | 6826 | ); |
| 6661 | 6827 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 6662 | 6828 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -9518,6 +9684,11 @@ mod tests { | ||
| 9518 | 9684 | // remaining gap is the syntax the coefficient sum leaves out. That is |
| 9519 | 9685 | // the default on a non-screen frame since lane-av1price2 (this |
| 9520 | 9686 | // fixture is one), and it reads +32.6% here. |
| 9687 | + // lane-av1obmc: every eligible single-reference inter block now also | |
| 9688 | + // carries a `motion_mode` symbol the coefficient sum never counts, | |
| 9689 | + // and an OBMC block carries the more expensive of the two values -- | |
| 9690 | + // re-measured at +35.0% here with `EC_AV1_OBMC=1`; the default path | |
| 9691 | + // (OBMC off, SIMPLE symbols only) reads +33.x. | |
| 9521 | 9692 | assert!( |
| 9522 | 9693 | worst_under <= 0.34, |
| 9523 | 9694 | "the writer spent {:.2}% more than the search priced -- more than \ |
| @@ -11042,6 +11213,18 @@ mod tests { | ||
| 11042 | 11213 | .collect::<Vec<_>>() |
| 11043 | 11214 | .join(" ") |
| 11044 | 11215 | ); |
| 11216 | + // lane-av1obmc: how many blocks actually CODED a motion_mode | |
| 11217 | + // symbol and which value they took, by footprint -- "OBMC is on" | |
| 11218 | + // as a measurement rather than a claim (class | |
| 11219 | + // `gate-blind-to-feature`). | |
| 11220 | + let mm = crate::tile::take_motion_mode_hits(); | |
| 11221 | + eprintln!( | |
| 11222 | + "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \ | |
| 11223 | + 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)", | |
| 11224 | + mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], | |
| 11225 | + 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | |
| 11226 | + mm.iter().sum::<usize>(), | |
| 11227 | + ); | |
| 11045 | 11228 | let (aom, aom_wall) = |
| 11046 | 11229 | external_ladder(&source, width, height, "libaom-av1", &aom_points); |
| 11047 | 11230 | let (rav1e, rav1e_wall) = |