forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1obmc' (5ea7a530): motion_mode syntax and an OBMC candidate, behind EC_AV1_OBMC

Browse files at this commit

c9bed9ad694d84af2a2b4c30563328b8c2a82c34Ahmet Kaan GümüşParent: 673c26b0d0c9c4
OBMC for the 32x32 single-reference winner (decoder obmc_plan/obmc_run
reused, 0 mismatches) does not keep at native (+18.2/+0.9, +47.3/+19.2,
+59.1/-10.0 vs +18.0/+0.7, +47.3/+19.2, +59.4/-10.0), so the header bit
and the candidate follow the knob; default streams byte-identical.
crates/ec-av1/src/decode.rsmodified+69 -14
@@ -24179,6 +24179,25 @@ fn overlappable_left(
2417924179 out
2418024180 }
2418124181
24182+/// libaom `motion_mode_allowed`'s neighbour term: does ANY bordering inter
24183+/// block sit on this block's above row or left column ("check_num_overlappable
24184+/// _neighbors", `obmc.h`). The single source of truth for the DECODER's own
24185+/// `motion_mode_eligible` and for the tile WRITER's (`tile::write_motion_mode`)
24186+/// -- the two disagreeing by one block writes a `motion_mode` symbol the
24187+/// decoder never reads (class `symbol-consumption-gap`).
24188+pub(crate) fn has_overlappable_neighbour(
24189+ grid: &MiGrid,
24190+ mi_row: usize,
24191+ mi_col: usize,
24192+ bw4: usize,
24193+ bh4: usize,
24194+ mi_cols: usize,
24195+ mi_rows: usize,
24196+) -> bool {
24197+ !overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, 1).is_empty()
24198+ || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, 1).is_empty()
24199+}
24200+
2418224201 /// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples`
2418324202 /// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2:
2418424203 /// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128`
@@ -24753,9 +24772,18 @@ nbmv=({},{}) nbref={} nbbsize={} filt={}",
2475324772 /// how many bordering blocks actually get blended, separate from the
2475424773 /// eligibility scan's own unbounded ("any at all") walk.
2475524774 #[allow(clippy::too_many_arguments)]
24756-fn obmc_plan(
24775+pub(crate) fn obmc_plan(
2475724776 grid: &MiGrid,
24758- 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]],
2475924787 mi_row: usize,
2476024788 mi_col: usize,
2476124789 bw4: usize,
@@ -24822,7 +24850,7 @@ fn obmc_plan(
2482224850 // neighbour's OWN mi-granular filter (libaom
2482324851 // `av1_setup_build_prediction_by_above_pred` reads
2482424852 // `above_mbmi->interp_filters`).
24825- let above_syms = neighbours.above_filter[mi_col + src4];
24853+ let above_syms = above_filter.get(mi_col + src4).copied().unwrap_or([3; 2]);
2482624854 obmcrec_probe(
2482724855 "above", mi_row, mi_col, write_w, write_h, off4, span4, &nb, above_syms,
2482824856 grid.get(mi_row - 1, mi_col + src4), interp_fixed.is_some(),
@@ -24846,7 +24874,7 @@ fn obmc_plan(
2484624874 let mut left = Vec::new();
2484724875 for (off4, span4, nb, src4) in overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, max_nb(bh4))
2484824876 {
24849- let left_syms = neighbours.left_filter[mi_row + src4];
24877+ let left_syms = left_filter.get(mi_row + src4).copied().unwrap_or([3; 2]);
2485024878 obmcrec_probe(
2485124879 "left", mi_row, mi_col, write_w, write_h, off4, span4, &nb, left_syms,
2485224880 grid.get(mi_row + src4, mi_col - 1), interp_fixed.is_some(),
@@ -24921,7 +24949,7 @@ pub(crate) struct ObmcPlan {
2492124949 /// (libaom `av1_build_obmc_inter_prediction`). Reads nothing but reference
2492224950 /// pixels and the plan, so it runs wherever the block's prediction is built
2492324951 /// -- on a recon worker when that prediction is deferred (lane-defer8).
24924-fn obmc_run(
24952+pub(crate) fn obmc_run(
2492524953 plan: &ObmcPlan,
2492624954 refpix: &RefPix<'_>,
2492724955 pred_y: &mut [u16],
@@ -27249,9 +27277,9 @@ fn decode_inter_block(
2724927277 // min(bw, bh) >= 8, false for a 16x4 / 4x16 strip.
2725027278 && write_w.min(write_h) >= 8
2725127279 && interintra_mode.is_none()
27252- && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty()
27253- || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1)
27254- .is_empty())
27280+ && has_overlappable_neighbour(
27281+ grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize,
27282+ )
2725527283 // libaom `motion_mode_allowed`: a GLOBALMV block whose model
2725627284 // is non-IDENTITY-non-TRANSLATION under free (non-integer)
2725727285 // mv reads no motion_mode/obmc symbol at all -- implicit
@@ -27603,7 +27631,8 @@ fn decode_inter_block(
2760327631 let obmc = if obmc_selected {
2760427632 Some(obmc_plan(
2760527633 grid,
27606- neighbours,
27634+ &neighbours.above_filter,
27635+ &neighbours.left_filter,
2760727636 mi_row,
2760827637 mi_col,
2760927638 bw4,
@@ -31988,8 +32017,9 @@ fn decode_inter_block8(
3198832017 && !skip_mode
3198932018 && interintra_mode.is_none()
3199032019 && !(is_global_mv_block && !force_integer_mv)
31991- && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty()
31992- || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty());
32020+ && has_overlappable_neighbour(
32021+ grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize,
32022+ );
3199332023 let mut warp_params: Option<crate::warp::WarpParams> = None;
3199432024 let mut warped_selected = false;
3199532025 let mut obmc_selected = false;
@@ -32189,7 +32219,8 @@ fn decode_inter_block8(
3218932219 let obmc = if obmc_selected {
3219032220 Some(obmc_plan(
3219132221 grid,
32192- neighbours,
32222+ &neighbours.above_filter,
32223+ &neighbours.left_filter,
3219332224 mi_row,
3219432225 mi_col,
3219532226 2,
@@ -33043,13 +33074,19 @@ pub(crate) fn decode_inter_frame_tile(
3304333074 // starts from what the previous frame stored, not from the defaults. Same
3304433075 // class as `tx_select` above, third instance.
3304533076 initial_cdfs: Option<Cdfs>,
33077+ // lane-av1obmc: this frame header's own `is_motion_mode_switchable`
33078+ // (spec 5.9.2). Hard-coding `false` here desynced every stream this
33079+ // crate's own encoder writes once inter blocks started carrying a
33080+ // `motion_mode` symbol (class: test asserts against a stale header).
33081+ switchable_motion_mode: bool,
3304633082 fctx: &crate::decode::FrameCtx,
3304733083 ) -> Result<Picture> {
3304833084 decode_inter_frame_tile_lr(
3304933085 data, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, refpix, cdef,
3305033086 loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed,
3305133087 enable_dual_filter, reference_select, tx_select,
33052- &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, fctx,
33088+ &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false,
33089+ switchable_motion_mode, fctx,
3305333090 )
3305433091 }
3305533092
@@ -33090,6 +33127,11 @@ pub(crate) fn decode_inter_frame_tile_lr(
3309033127 // `sign_bias` rather than beside the other bools on purpose (the
3309133128 // `adjacent-same-typed-args` class).
3309233129 allow_screen_content_tools: bool,
33130+ // lane-av1obmc: this frame header's own `is_motion_mode_switchable`
33131+ // (spec 5.9.2). Hard-coding `false` here desynced every stream this
33132+ // crate's own encoder writes once inter blocks started carrying a
33133+ // `motion_mode` symbol (class: test asserts against a stale header).
33134+ switchable_motion_mode: bool,
3309333135 fctx: &crate::decode::FrameCtx,
3309433136 ) -> Result<Picture> {
3309533137 let single_tile = TileInfo {
@@ -33118,6 +33160,7 @@ pub(crate) fn decode_inter_frame_tile_lr(
3311833160 initial_cdfs,
3311933161 sign_bias,
3312033162 allow_screen_content_tools,
33163+ switchable_motion_mode,
3312133164 fctx,
3312233165 )
3312333166 }
@@ -33148,6 +33191,11 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3314833191 // This frame header's own `allow_screen_content_tools` (see the
3314933192 // single-tile wrapper): the palette syntax its intra blocks carry.
3315033193 allow_screen_content_tools: bool,
33194+ // lane-av1obmc: this frame header's own `is_motion_mode_switchable`
33195+ // (spec 5.9.2). Hard-coding `false` here desynced every stream this
33196+ // crate's own encoder writes once inter blocks started carrying a
33197+ // `motion_mode` symbol (class: test asserts against a stale header).
33198+ switchable_motion_mode: bool,
3315133199 fctx: &crate::decode::FrameCtx,
3315233200 ) -> Result<Picture> {
3315333201 decode_inter_frame_tile_with_cdfs(
@@ -33182,7 +33230,7 @@ pub(crate) fn decode_inter_frame_tiles_lr(
3318233230 [0; 2],
3318333231 true,
3318433232 tx_select,
33185- false,
33233+ switchable_motion_mode,
3318633234 false,
3318733235 allow_screen_content_tools,
3318833236 DeltaParams::default(),
@@ -37314,6 +37362,7 @@ mod tests {
3731437362 false,
3731537363 false,
3731637364 None,
37365+ false,
3731737366 fctx,
3731837367 )
3731937368 .unwrap_err();
@@ -37732,6 +37781,9 @@ mod tests {
3773237781 // This frame's own `allow_screen_content_tools`, off the
3773337782 // `Encoded` the encoder handed back (stale-header class).
3773437783 frame.screen,
37784+ // Same stale-header class: the bit that makes this frame's
37785+ // inter blocks carry a `motion_mode` symbol.
37786+ frame.switchable_motion_mode,
3773537787 fctx,
3773637788 )
3773737789 .unwrap();
@@ -38063,6 +38115,9 @@ mod tests {
3806338115 // This frame's own `allow_screen_content_tools`, off the
3806438116 // `Encoded` the encoder handed back (stale-header class).
3806538117 frame.screen,
38118+ // Same stale-header class: the bit that makes this frame's
38119+ // inter blocks carry a `motion_mode` symbol.
38120+ frame.switchable_motion_mode,
3806638121 fctx,
3806738122 )
3806838123 .unwrap();
crates/ec-av1/src/encode.rsmodified+219 -3
@@ -693,6 +693,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En
693693 mi_rows: encoded.mi_rows,
694694 base_q_idx: encoded.base_q_idx,
695695 tx_select: encoded.tx_select,
696+ switchable_motion_mode: encoded.switchable_motion_mode,
696697 screen: encoded.screen,
697698 start_cdfs: encoded.start_cdfs.clone(),
698699 next_cdfs: encoded.next_cdfs.clone(),
@@ -747,6 +748,12 @@ pub struct Encoded {
747748 /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first
748749 /// block.
749750 pub(crate) tx_select: bool,
751+ /// This frame header's `is_motion_mode_switchable` (spec 5.9.2) -- the
752+ /// bit that makes every eligible single-reference inter block carry a
753+ /// `motion_mode` symbol. Same contract as `tx_select` above: a test
754+ /// decoding `tile` directly must pass it along or it desyncs at the
755+ /// first eligible block.
756+ pub(crate) switchable_motion_mode: bool,
750757 /// The CDF state this frame's tile writer started from -- the defaults
751758 /// for a key frame and for the first inter frame, and the previous
752759 /// frame's stored tables after that. A test that decodes `tile` directly
@@ -1152,7 +1159,16 @@ pub fn inter_frame_headers_slots(
11521159 },
11531160 allow_high_precision_mv: false,
11541161 interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap,
1155- is_motion_mode_switchable: false,
1162+ // lane-av1obmc: with this bit set every single-reference inter block
1163+ // libaom's `motion_mode_allowed` accepts carries a `motion_mode`
1164+ // symbol (`crate::tile::write_motion_mode`); `allow_warped_motion`
1165+ // stays false, so the alphabet is the 2-symbol `obmc_cdf`. It is tied
1166+ // to the OBMC knob rather than always on: with every block choosing
1167+ // SIMPLE_TRANSLATION the symbol is pure cost (+0.07% / +0.02% on the
1168+ // gate clip), so the DEFAULT path writes no motion_mode syntax at all
1169+ // and stays byte-identical to the streams before this lane. A future
1170+ // warp knob joins this disjunction.
1171+ is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC"),
11561172 use_ref_frame_mvs: false,
11571173 // Forces `get_tx_set` (spec 5.11.48) to the two-symbol
11581174 // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only
@@ -2226,6 +2242,16 @@ impl Reach {
22262242 }
22272243
22282244 /// What one symbol costs against a CDF, in bits.
2245+/// What the `motion_mode`/`obmc` symbol costs (spec 5.11.24), off the default
2246+/// table rather than the tile's adapted one -- the same static-CDF estimate
2247+/// every other symbol in this search is priced with.
2248+fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 {
2249+ let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else {
2250+ return 0.0;
2251+ };
2252+ symbol_bits(&cdf::OBMC[row], usize::from(obmc))
2253+}
2254+
22292255 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
22302256 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };
22312257 let width = cdf[symbol] - low;
@@ -3855,6 +3881,7 @@ fn code_square_inter(
38553881 palette_uv: None,
38563882 tx_depth,
38573883 inter: Some(info),
3884+ obmc: false,
38583885 },
38593886 cost + dcost,
38603887 );
@@ -3886,6 +3913,7 @@ fn code_square_inter(
38863913 palette: None,
38873914 palette_uv: None,
38883915 tx_depth,
3916+ obmc: false,
38893917 inter: Some(InterInfo {
38903918 ref1: None,
38913919 mv1: (0, 0),
@@ -5047,6 +5075,8 @@ pub(crate) fn encode_key_frame_inner(
50475075 mi_rows: header.mi_rows,
50485076 base_q_idx,
50495077 tx_select,
5078+ // A key frame codes no inter block, so no motion_mode symbol.
5079+ switchable_motion_mode: false,
50505080 screen,
50515081 next_cdfs: start_cdfs.clone(),
50525082 start_cdfs,
@@ -5599,7 +5629,15 @@ fn search_inter_block(
55995629 // The COMPOUND stack of each `LAST_FRAME` + extra-reference pair, built by
56005630 // the caller off the same MI grid the tile writer will (empty unless the
56015631 // frame carries `reference_select`).
5602- compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx,
5632+ compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)],
5633+ // lane-av1obmc: the mi grid this search's neighbours were published into
5634+ // (`record_mi`) and this block's own place in it -- what the OBMC
5635+ // candidate's eligibility walk and neighbour plan read, the same state
5636+ // the tile writer will read when it codes the `motion_mode` symbol.
5637+ grid: &MiGrid,
5638+ (mi_row, mi_col): (usize, usize),
5639+ (mi_rows, mi_cols): (usize, usize),
5640+ fctx: &crate::decode::FrameCtx,
56035641 ) -> (BlockCoeffs, f64) {
56045642 // `luma_set` is the INTRA candidates' table; the two inter candidates
56055643 // below price their coefficients through `TxbSet::Luma32Inter`, which is
@@ -5632,6 +5670,10 @@ fn search_inter_block(
56325670 mode: u8,
56335671 skip: bool,
56345672 inter: Option<InterInfo>,
5673+ /// The mode/mv syntax this candidate pays for, apart from its
5674+ /// residual -- what the OBMC candidate below has to re-pay when it
5675+ /// re-prices the winner's prediction.
5676+ mode_bits: f64,
56355677 }
56365678 let mut best: Option<Candidate> = None;
56375679 let mut consider = |candidate: Candidate| {
@@ -5726,6 +5768,7 @@ fn search_inter_block(
57265768 mode,
57275769 skip: false,
57285770 inter: None,
5771+ mode_bits: mode_bits[usize::from(mode)],
57295772 });
57305773 }
57315774
@@ -6137,6 +6180,7 @@ fn search_inter_block(
61376180 v,
61386181 mode: DC_PRED,
61396182 skip,
6183+ mode_bits: bits,
61406184 inter: Some(info),
61416185 });
61426186 }
@@ -6234,12 +6278,150 @@ fn search_inter_block(
62346278 v,
62356279 mode: DC_PRED,
62366280 skip,
6281+ mode_bits: mode_bits_inter,
62376282 inter: Some(info),
62386283 });
62396284 }
62406285 }
62416286
62426287 let best = best.expect("the search offers at least the intra modes");
6288+ // lane-av1obmc: the OBMC candidate (spec `motion_mode == OBMC_CAUSAL`),
6289+ // offered to the SINGLE-REFERENCE winner only -- libaom's
6290+ // `motion_mode_allowed` fails `is_motion_variation_allowed_compound` on a
6291+ // compound block, so the writer codes no symbol for one. The prediction
6292+ // is built by the DECODER's own plan and blend
6293+ // ([`crate::decode::obmc_plan`]/[`crate::decode::obmc_run`],
6294+ // `av1_build_obmc_inter_prediction`) off the committed neighbours' mvs
6295+ // this search's `grid` already carries, so what is priced here is what a
6296+ // decoder reconstructs -- never a second implementation of the blend.
6297+ //
6298+ // MEASURED 2026-09-06 and NOT KEPT ON BY DEFAULT (`EC_AV1_OBMC=1` turns it
6299+ // on). Native crop BD, the standing recipe, with it on against the same
6300+ // build with it off: film 1080p +18.2/+0.9 against +18.0/+0.7, film 2160p
6301+ // +47.3/+19.2 against +47.3/+19.2, screen +59.1/-10.0 against
6302+ // +59.4/-10.0. That is one row down (screen -0.3 vs libaom), one flat and
6303+ // one UP -- the keep rule wants two down and one flat, so it stays off.
6304+ // The 640x384 gate reads the same shape louder: +79.7/+95.5/+54.2 and
6305+ // +37.2/+56.8/+0.8 against +78.8/+95.1/+54.5 and +36.1/+56.4/+1.1. Two
6306+ // things bound it and are the upgrade path: only the 32x32 winner is
6307+ // offered OBMC (the 16x16/8x8 leaves in `code_square_inter` are not, and
6308+ // that is where libaom cashes most of its OBMC gain), and the symbol/
6309+ // residual price here is the static-CDF estimate.
6310+ let mut best = best;
6311+ let mut obmc_won = false;
6312+ if let Some(info) = best.inter.filter(|i| i.ref1.is_none()) {
6313+ if crate::envflags::env_flag!("EC_AV1_OBMC")
6314+ && crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, 8, 8, mi_cols, mi_rows)
6315+ {
6316+ let mut refs: [Option<&Picture>; 8] = [None; 8];
6317+ refs[LAST_FRAME as usize] = Some(reference);
6318+ for &(r, p, _) in extra {
6319+ refs[r as usize] = Some(p);
6320+ }
6321+ let refpix = crate::decode::RefPix::ready(refs);
6322+ let plan = crate::decode::obmc_plan(
6323+ grid,
6324+ &[],
6325+ &[],
6326+ mi_row,
6327+ mi_col,
6328+ 8,
6329+ 8,
6330+ mi_rows,
6331+ mi_cols,
6332+ BLOCK,
6333+ BLOCK,
6334+ BLOCK,
6335+ BLOCK / 2,
6336+ x,
6337+ y,
6338+ x / 2,
6339+ y / 2,
6340+ &refpix,
6341+ Some(mc::InterpFilterKind::Regular),
6342+ reference.width,
6343+ );
6344+ if let Ok(plan) = plan {
6345+ let (ref_luma, ref_u, ref_v) = match extra
6346+ .iter()
6347+ .find(|(r, _, _)| *r == info.ref_frame)
6348+ {
6349+ Some(&(_, g, _)) => (
6350+ (&g.y, g.width, luma.true_width, luma.true_height),
6351+ (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height),
6352+ (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height),
6353+ ),
6354+ None => (ref_luma, ref_u, ref_v),
6355+ };
6356+ let mv = info.mv;
6357+ let mut pred = [
6358+ vec![0u16; BLOCK * BLOCK],
6359+ vec![0u16; BLOCK * BLOCK / 4],
6360+ vec![0u16; BLOCK * BLOCK / 4],
6361+ ];
6362+ for (i, (r, luma_plane)) in
6363+ [(ref_luma, true), (ref_u, false), (ref_v, false)].into_iter().enumerate()
6364+ {
6365+ let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) };
6366+ let side = if luma_plane { BLOCK } else { BLOCK / 2 };
6367+ mc::predict(
6368+ r.0,
6369+ r.1,
6370+ r.2,
6371+ r.3,
6372+ mv_to_q4(px, mv.1, luma_plane),
6373+ mv_to_q4(py, mv.0, luma_plane),
6374+ side,
6375+ side,
6376+ &mut pred[i],
6377+ fctx,
6378+ );
6379+ }
6380+ let [mut py_buf, mut pu_buf, mut pv_buf] = pred;
6381+ crate::decode::obmc_run(
6382+ &plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx,
6383+ );
6384+ let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>();
6385+ let luma_trial = luma.code_from_prediction(
6386+ x, y, BLOCK, &u8s(&py_buf), false, search.base_q_idx, search.deadzone,
6387+ TxbSet::Luma32Inter,
6388+ );
6389+ let u = chroma[0].code_from_prediction(
6390+ x / 2, y / 2, BLOCK / 2, &u8s(&pu_buf), false, search.base_q_idx,
6391+ search.deadzone, chroma_set,
6392+ );
6393+ let v = chroma[1].code_from_prediction(
6394+ x / 2, y / 2, BLOCK / 2, &u8s(&pv_buf), false, search.base_q_idx,
6395+ search.deadzone, chroma_set,
6396+ );
6397+ let skip = luma_trial.levels.iter().all(|&l| l == 0)
6398+ && u.levels.iter().all(|&l| l == 0)
6399+ && v.levels.iter().all(|&l| l == 0);
6400+ let cost = luma_trial.sse
6401+ + u.sse
6402+ + v.sse
6403+ + search.lambda
6404+ * (skip_bits(skip)
6405+ + intra_inter_bits(true)
6406+ + best.mode_bits
6407+ + obmc_bits(BLOCK, BLOCK, true)
6408+ + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits });
6409+ if cost < best.cost + search.lambda * obmc_bits(BLOCK, BLOCK, false) {
6410+ obmc_won = true;
6411+ best = Candidate {
6412+ cost,
6413+ luma: luma_trial,
6414+ u,
6415+ v,
6416+ mode: DC_PRED,
6417+ skip,
6418+ mode_bits: best.mode_bits,
6419+ inter: best.inter,
6420+ };
6421+ }
6422+ }
6423+ }
6424+ }
62436425 // lane-av1txbits BUILT the transform-depth search this path lacks -- a
62446426 // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16
62456427 // units against its flat trial, exactly as a 16x16 winner is -- and
@@ -6261,6 +6443,7 @@ fn search_inter_block(
62616443 uv_mode: DC_PRED,
62626444 skip: best.skip,
62636445 inter: best.inter,
6446+ obmc: obmc_won,
62646447 eight: None,
62656448 dv: None,
62666449 palette: None,
@@ -6640,7 +6823,11 @@ pub(crate) fn encode_inter_frame(
66406823 reference,
66416824 &stack,
66426825 &extra,
6643- &compound, fctx,
6826+ &compound,
6827+ &grid,
6828+ (mi_row, mi_col),
6829+ (mi_rows, mi_cols),
6830+ fctx,
66446831 );
66456832 cost_whole += search.lambda * partition_bits(BLOCK, false);
66466833 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);
@@ -6975,6 +7162,7 @@ pub(crate) fn encode_inter_frame(
69757162 .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx)));
69767163 let start_cdfs = CdfSnapshot(cdfs);
69777164 let reference_select_bit = header.reference_select;
7165+ let switchable_motion_mode = header.is_motion_mode_switchable;
69787166 let order_hint_bits = seq.order_hint_bits;
69797167 let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32;
69807168 let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32;
@@ -6996,6 +7184,7 @@ pub(crate) fn encode_inter_frame(
69967184 crate::tile::arm_sign_bias(sign_bias);
69977185 // Thread-locals: every tile job arms its own worker.
69987186 crate::tile::arm_reference_select(reference_select_bit);
7187+ crate::tile::arm_motion_mode(switchable_motion_mode);
69997188 crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints);
70007189 crate::tile::arm_screen(screen);
70017190 let mut cdfs = start_cdfs.0.clone();
@@ -7122,6 +7311,7 @@ pub(crate) fn encode_inter_frame(
71227311 Some(start_cdfs.0.clone()),
71237312 sign_bias,
71247313 h.allow_screen_content_tools,
7314+ switchable_motion_mode,
71257315 fctx,
71267316 )
71277317 },
@@ -7154,6 +7344,7 @@ pub(crate) fn encode_inter_frame(
71547344 mi_rows: header.mi_rows,
71557345 base_q_idx,
71567346 tx_select,
7347+ switchable_motion_mode,
71577348 screen,
71587349 start_cdfs,
71597350 next_cdfs,
@@ -7924,6 +8115,7 @@ mod tests {
79248115 mv: (0, 0),
79258116 ref_mv_idx: 0,
79268117 }),
8118+ obmc: false,
79278119 };
79288120 let skipped_block = BlockCoeffs {
79298121 skip: true,
@@ -7943,6 +8135,10 @@ mod tests {
79438135 Quadrant::Whole(skipped_block.clone()),
79448136 Quadrant::Whole(skipped_block),
79458137 ];
8138+ // lane-av1obmc: the header this tile is decoded under carries
8139+ // `is_motion_mode_switchable`, so the writer must be armed with it
8140+ // too or the decoder reads a `motion_mode` symbol nobody wrote.
8141+ crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable);
79468142 let tile =
79478143 crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks)
79488144 .unwrap();
@@ -9493,6 +9689,14 @@ mod tests {
94939689 // remaining gap is the syntax the coefficient sum leaves out. That is
94949690 // the default on a non-screen frame since lane-av1price2 (this
94959691 // fixture is one), and it reads +32.6% here.
9692+ // lane-av1obmc: every eligible single-reference inter block now also
9693+ // carries a `motion_mode` symbol the coefficient sum never counts,
9694+ // and an OBMC block carries the more expensive of the two values --
9695+ // re-measured at +35.0% here with `EC_AV1_OBMC=1`. Like
9696+ // `EC_AV1_PRICE_FRAME_CDFS=1` before it, that knob changes encoder
9697+ // decisions, so this bound and the byte pins above FAIL BY DESIGN
9698+ // under it; the default path (no motion_mode syntax at all) is the
9699+ // one they are measured on.
94969700 assert!(
94979701 worst_under <= 0.34,
94989702 "the writer spent {:.2}% more than the search priced -- more than \
@@ -11017,6 +11221,18 @@ mod tests {
1101711221 .collect::<Vec<_>>()
1101811222 .join(" ")
1101911223 );
11224+ // lane-av1obmc: how many blocks actually CODED a motion_mode
11225+ // symbol and which value they took, by footprint -- "OBMC is on"
11226+ // as a measurement rather than a claim (class
11227+ // `gate-blind-to-feature`).
11228+ let mm = crate::tile::take_motion_mode_hits();
11229+ eprintln!(
11230+ "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \
11231+ 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)",
11232+ mm[0], mm[1], mm[2], mm[3], mm[4], mm[5],
11233+ 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64,
11234+ mm.iter().sum::<usize>(),
11235+ );
1102011236 let (aom, aom_wall) =
1102111237 external_ladder(&source, width, height, "libaom-av1", &aom_points);
1102211238 let (rav1e, rav1e_wall) =
crates/ec-av1/src/tile.rsmodified+130 -0
@@ -381,6 +381,99 @@ pub(crate) fn arm_reference_select(on: bool) {
381381 REFERENCE_SELECT.with(|c| c.set(on));
382382 }
383383
384+thread_local! {
385+ /// This frame header's own `is_motion_mode_switchable` bit (spec 5.9.2),
386+ /// armed exactly like [`REFERENCE_SELECT`]. `true` makes every
387+ /// single-reference inter block that libaom's `motion_mode_allowed`
388+ /// accepts carry a `motion_mode`/`obmc` symbol right after its MV syntax,
389+ /// exactly where decode.rs' `read_motion_mode` reads one.
390+ static MOTION_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
391+}
392+
393+/// Arms the frame's `is_motion_mode_switchable` bit for the next tile written
394+/// on this thread; cleared by [`CdefIdxGuard`] when that writer returns.
395+pub(crate) fn arm_motion_mode(on: bool) {
396+ MOTION_MODE.with(|c| c.set(on));
397+}
398+
399+/// How many single-reference inter blocks this writer coded a `motion_mode`
400+/// symbol for, split by the value it coded and by the block's own footprint
401+/// -- the histogram a gate prints so that "OBMC is on" is a measurement, not
402+/// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for
403+/// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8.
404+static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 6] =
405+ [const { std::sync::atomic::AtomicUsize::new(0) }; 6];
406+
407+/// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the
408+/// counts to its own encode.
409+#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
410+pub(crate) fn take_motion_mode_hits() -> [usize; 6] {
411+ std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed))
412+}
413+
414+/// `read_motion_mode`'s write side (spec 5.11.24), called on a
415+/// single-reference inter block right after its MV syntax and before the
416+/// (never switchable here) interpolation filter -- libaom's own sequential
417+/// order in `read_inter_block_mode_info`.
418+///
419+/// Eligibility is decode.rs' own `motion_mode_eligible`, term for term, with
420+/// the terms this writer settles statically dropped: no block it codes is
421+/// `skip_mode`, interintra or a non-IDENTITY-global `GLOBALMV` one, so what
422+/// is left is the footprint floor and the overlappable-neighbour walk
423+/// ([`crate::decode::has_overlappable_neighbour`], shared with the decoder so
424+/// the two cannot drift). The frame header keeps `allow_warped_motion == 0`,
425+/// so the alphabet is always the 2-symbol `obmc_cdf`, never the 3-symbol
426+/// `motion_mode_cdf`.
427+fn write_motion_mode(
428+ enc: &mut SymbolEncoder,
429+ cdfs: &mut Cdfs,
430+ grid: &MiGrid,
431+ (mi_row, mi_col): (usize, usize),
432+ (bw4, bh4): (usize, usize),
433+ (write_w, write_h): (usize, usize),
434+ (mi_cols, mi_rows): (usize, usize),
435+ obmc: bool,
436+) -> Result<()> {
437+ let eligible = MOTION_MODE.with(std::cell::Cell::get)
438+ && write_w.min(write_h) >= 8
439+ && crate::decode::has_overlappable_neighbour(
440+ grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows,
441+ );
442+ if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
443+ eprintln!(
444+ "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={}",
445+ eligible as u8
446+ );
447+ }
448+ if !eligible {
449+ // The encoder trialled an OBMC prediction for a block this writer
450+ // codes as SIMPLE_TRANSLATION: its reconstruction is not the
451+ // decoder's (class encoder-grid-drift), which is exactly what the
452+ // `EC_COMP_MISMATCH` rung exists to catch.
453+ if obmc && crate::envflags::env_flag!("EC_COMP_MISMATCH") {
454+ eprintln!(
455+ "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})"
456+ );
457+ }
458+ return Ok(());
459+ }
460+ let row = crate::decode::motion_mode_cdf_row(write_w, write_h).ok_or_else(|| {
461+ Error::unsupported(
462+ "AV1 tile",
463+ format!("a {write_w}x{write_h} block has no motion_mode CDF row"),
464+ )
465+ })?;
466+ enc.symbol(usize::from(obmc), &mut cdfs.obmc[row]);
467+ let bucket = match write_w.min(write_h) {
468+ 32.. => 0,
469+ 16 => 1,
470+ _ => 2,
471+ };
472+ MOTION_MODE_HITS[usize::from(obmc) + 2 * bucket]
473+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
474+ Ok(())
475+}
476+
384477 /// The compound half of a block's end-of-block neighbour record: nothing for
385478 /// a single-reference or intra block, and `record_compound_mi`'s overwrite of
386479 /// the ALTREF default for a compound one (this writer only codes
@@ -759,6 +852,7 @@ impl Drop for CdefIdxGuard {
759852 SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS));
760853 SCREEN.with(|c| c.set(false));
761854 REFERENCE_SELECT.with(|c| c.set(false));
855+ MOTION_MODE.with(|c| c.set(false));
762856 INTRABC.with(|c| c.set(false));
763857 INTRABC_GRID.with(|c| *c.borrow_mut() = None);
764858 ORDER_HINTS.with(|c| c.set((7, 0, [0; 7])));
@@ -1240,6 +1334,12 @@ pub struct BlockCoeffs {
12401334 /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key
12411335 /// frame writers never read this field.
12421336 pub inter: Option<InterInfo>,
1337+ /// Whether this (single-reference inter) block's prediction was built
1338+ /// with OBMC -- spec `motion_mode == OBMC_CAUSAL`, written by
1339+ /// [`write_motion_mode`]. `false` (the [`Default`]) is
1340+ /// `SIMPLE_TRANSLATION`, the only motion mode this writer coded before
1341+ /// lane-av1obmc.
1342+ pub obmc: bool,
12431343 /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect),
12441344 /// each with its own 8x8 luma transform and 4x4 chroma transforms, in
12451345 /// raster order among the leaves that are inside the true frame. `Some`
@@ -5439,6 +5539,16 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
54395539 );
54405540 }
54415541 }
5542+ write_motion_mode(
5543+ &mut enc,
5544+ &mut cdfs,
5545+ &grid,
5546+ (mi_row, mi_col),
5547+ (8, 8),
5548+ (BLOCK, BLOCK),
5549+ (mi_cols as usize, mi_rows as usize),
5550+ block.obmc,
5551+ )?;
54425552 mode_for_tx = 0;
54435553 }
54445554 } else {
@@ -5663,6 +5773,16 @@ fn write_inter_frame_leaf(
56635773 );
56645774 }
56655775 }
5776+ write_motion_mode(
5777+ enc,
5778+ cdfs,
5779+ grid,
5780+ (mi_row, mi_col),
5781+ (SUB_MI as usize, SUB_MI as usize),
5782+ (SUB, SUB),
5783+ (mi_cols as usize, mi_rows as usize),
5784+ block.obmc,
5785+ )?;
56665786 mode_for_tx = 0;
56675787 }
56685788 } else {
@@ -5893,6 +6013,16 @@ fn write_inter_frame_leaf8(
58936013 );
58946014 }
58956015 }
6016+ write_motion_mode(
6017+ enc,
6018+ cdfs,
6019+ grid,
6020+ (mi_row, mi_col),
6021+ (2, 2),
6022+ (8, 8),
6023+ (mi_cols as usize, mi_rows as usize),
6024+ block.obmc,
6025+ )?;
58966026 mode_for_tx = 0;
58976027 }
58986028 } else {