diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 575c891d..8036e855 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -24179,6 +24179,25 @@ fn overlappable_left( out } +/// libaom `motion_mode_allowed`'s neighbour term: does ANY bordering inter +/// block sit on this block's above row or left column ("check_num_overlappable +/// _neighbors", `obmc.h`). The single source of truth for the DECODER's own +/// `motion_mode_eligible` and for the tile WRITER's (`tile::write_motion_mode`) +/// -- the two disagreeing by one block writes a `motion_mode` symbol the +/// decoder never reads (class `symbol-consumption-gap`). +pub(crate) fn has_overlappable_neighbour( + grid: &MiGrid, + mi_row: usize, + mi_col: usize, + bw4: usize, + bh4: usize, + mi_cols: usize, + mi_rows: usize, +) -> bool { + !overlappable_above(grid, mi_row, mi_col, bw4, mi_cols, 1).is_empty() + || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows, 1).is_empty() +} + /// The warp-sample gather's own `has_top_right` probe (libaom `av1_findSamples` /// calls the same `mvref_common.c` function the MV stack does). lane-sb128 r2: /// this used to hardcode `SB_MI` (a 64x64 superblock) -- at `sb_size == 128` @@ -24753,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, @@ -24822,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(), @@ -24846,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(), @@ -24921,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], @@ -27249,9 +27277,9 @@ fn decode_inter_block( // min(bw, bh) >= 8, false for a 16x4 / 4x16 strip. && write_w.min(write_h) >= 8 && interintra_mode.is_none() - && (!overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).is_empty() - || !overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1) - .is_empty()) + && has_overlappable_neighbour( + grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize, + ) // libaom `motion_mode_allowed`: a GLOBALMV block whose model // is non-IDENTITY-non-TRANSLATION under free (non-integer) // mv reads no motion_mode/obmc symbol at all -- implicit @@ -27603,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, @@ -31988,8 +32017,9 @@ fn decode_inter_block8( && !skip_mode && interintra_mode.is_none() && !(is_global_mv_block && !force_integer_mv) - && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() - || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); + && has_overlappable_neighbour( + grid, mi_row, mi_col, 2, 2, mi_cols as usize, mi_rows as usize, + ); let mut warp_params: Option = None; let mut warped_selected = false; let mut obmc_selected = false; @@ -32189,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, @@ -33043,13 +33074,19 @@ pub(crate) fn decode_inter_frame_tile( // starts from what the previous frame stored, not from the defaults. Same // class as `tx_select` above, third instance. initial_cdfs: Option, + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` + // (spec 5.9.2). Hard-coding `false` here desynced every stream this + // crate's own encoder writes once inter blocks started carrying a + // `motion_mode` symbol (class: test asserts against a stale header). + switchable_motion_mode: bool, fctx: &crate::decode::FrameCtx, ) -> Result { decode_inter_frame_tile_lr( data, mi_cols, mi_rows, base_q_idx, frame_width, frame_height, refpix, cdef, loop_filter, allow_high_precision_mv, force_integer_mv, interp_fixed, enable_dual_filter, reference_select, tx_select, - &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, fctx, + &LoopRestorationParams::default(), initial_cdfs, NO_SIGN_BIAS, false, + switchable_motion_mode, fctx, ) } @@ -33090,6 +33127,11 @@ pub(crate) fn decode_inter_frame_tile_lr( // `sign_bias` rather than beside the other bools on purpose (the // `adjacent-same-typed-args` class). allow_screen_content_tools: bool, + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` + // (spec 5.9.2). Hard-coding `false` here desynced every stream this + // crate's own encoder writes once inter blocks started carrying a + // `motion_mode` symbol (class: test asserts against a stale header). + switchable_motion_mode: bool, fctx: &crate::decode::FrameCtx, ) -> Result { let single_tile = TileInfo { @@ -33118,6 +33160,7 @@ pub(crate) fn decode_inter_frame_tile_lr( initial_cdfs, sign_bias, allow_screen_content_tools, + switchable_motion_mode, fctx, ) } @@ -33148,6 +33191,11 @@ pub(crate) fn decode_inter_frame_tiles_lr( // This frame header's own `allow_screen_content_tools` (see the // single-tile wrapper): the palette syntax its intra blocks carry. allow_screen_content_tools: bool, + // lane-av1obmc: this frame header's own `is_motion_mode_switchable` + // (spec 5.9.2). Hard-coding `false` here desynced every stream this + // crate's own encoder writes once inter blocks started carrying a + // `motion_mode` symbol (class: test asserts against a stale header). + switchable_motion_mode: bool, fctx: &crate::decode::FrameCtx, ) -> Result { decode_inter_frame_tile_with_cdfs( @@ -33182,7 +33230,7 @@ pub(crate) fn decode_inter_frame_tiles_lr( [0; 2], true, tx_select, - false, + switchable_motion_mode, false, allow_screen_content_tools, DeltaParams::default(), @@ -37314,6 +37362,7 @@ mod tests { false, false, None, + false, fctx, ) .unwrap_err(); @@ -37732,6 +37781,9 @@ mod tests { // This frame's own `allow_screen_content_tools`, off the // `Encoded` the encoder handed back (stale-header class). frame.screen, + // Same stale-header class: the bit that makes this frame's + // inter blocks carry a `motion_mode` symbol. + frame.switchable_motion_mode, fctx, ) .unwrap(); @@ -38063,6 +38115,9 @@ mod tests { // This frame's own `allow_screen_content_tools`, off the // `Encoded` the encoder handed back (stale-header class). frame.screen, + // Same stale-header class: the bit that makes this frame's + // inter blocks carry a `motion_mode` symbol. + frame.switchable_motion_mode, fctx, ) .unwrap(); diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index ced2d7d6..bc359a45 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -693,6 +693,7 @@ pub(crate) fn crop_encoded(encoded: &Encoded, width: usize, height: usize) -> En mi_rows: encoded.mi_rows, base_q_idx: encoded.base_q_idx, tx_select: encoded.tx_select, + switchable_motion_mode: encoded.switchable_motion_mode, screen: encoded.screen, start_cdfs: encoded.start_cdfs.clone(), next_cdfs: encoded.next_cdfs.clone(), @@ -747,6 +748,12 @@ pub struct Encoded { /// a `tx_depth` frame as a `TxMode::Largest` one and desyncs at the first /// block. pub(crate) tx_select: bool, + /// This frame header's `is_motion_mode_switchable` (spec 5.9.2) -- the + /// bit that makes every eligible single-reference inter block carry a + /// `motion_mode` symbol. Same contract as `tx_select` above: a test + /// decoding `tile` directly must pass it along or it desyncs at the + /// first eligible block. + pub(crate) switchable_motion_mode: bool, /// The CDF state this frame's tile writer started from -- the defaults /// for a key frame and for the first inter frame, and the previous /// frame's stored tables after that. A test that decodes `tile` directly @@ -1152,7 +1159,16 @@ pub fn inter_frame_headers_slots( }, allow_high_precision_mv: false, interpolation_filter: ec_av1_syntax::InterpolationFilter::Eighttap, - is_motion_mode_switchable: false, + // lane-av1obmc: with this bit set every single-reference inter block + // libaom's `motion_mode_allowed` accepts carries a `motion_mode` + // symbol (`crate::tile::write_motion_mode`); `allow_warped_motion` + // stays false, so the alphabet is the 2-symbol `obmc_cdf`. It is tied + // to the OBMC knob rather than always on: with every block choosing + // SIMPLE_TRANSLATION the symbol is pure cost (+0.07% / +0.02% on the + // gate clip), so the DEFAULT path writes no motion_mode syntax at all + // and stays byte-identical to the streams before this lane. A future + // warp knob joins this disjunction. + is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC"), use_ref_frame_mvs: false, // Forces `get_tx_set` (spec 5.11.48) to the two-symbol // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only @@ -2226,6 +2242,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; @@ -3855,6 +3881,7 @@ fn code_square_inter( palette_uv: None, tx_depth, inter: Some(info), + obmc: false, }, cost + dcost, ); @@ -3886,6 +3913,7 @@ fn code_square_inter( palette: None, palette_uv: None, tx_depth, + obmc: false, inter: Some(InterInfo { ref1: None, mv1: (0, 0), @@ -5047,6 +5075,8 @@ pub(crate) fn encode_key_frame_inner( mi_rows: header.mi_rows, base_q_idx, tx_select, + // A key frame codes no inter block, so no motion_mode symbol. + switchable_motion_mode: false, screen, next_cdfs: start_cdfs.clone(), start_cdfs, @@ -5599,7 +5629,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 @@ -5632,6 +5670,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| { @@ -5726,6 +5768,7 @@ fn search_inter_block( mode, skip: false, inter: None, + mode_bits: mode_bits[usize::from(mode)], }); } @@ -6137,6 +6180,7 @@ fn search_inter_block( v, mode: DC_PRED, skip, + mode_bits: bits, inter: Some(info), }); } @@ -6234,12 +6278,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 @@ -6261,6 +6443,7 @@ fn search_inter_block( uv_mode: DC_PRED, skip: best.skip, inter: best.inter, + obmc: obmc_won, eight: None, dv: None, palette: None, @@ -6640,7 +6823,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); @@ -6975,6 +7162,7 @@ pub(crate) fn encode_inter_frame( .unwrap_or_else(|| crate::cdf_state::Cdfs::new(crate::tile::q_ctx_of(base_q_idx))); let start_cdfs = CdfSnapshot(cdfs); let reference_select_bit = header.reference_select; + let switchable_motion_mode = header.is_motion_mode_switchable; let order_hint_bits = seq.order_hint_bits; let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; @@ -6996,6 +7184,7 @@ pub(crate) fn encode_inter_frame( crate::tile::arm_sign_bias(sign_bias); // Thread-locals: every tile job arms its own worker. crate::tile::arm_reference_select(reference_select_bit); + crate::tile::arm_motion_mode(switchable_motion_mode); crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); crate::tile::arm_screen(screen); let mut cdfs = start_cdfs.0.clone(); @@ -7122,6 +7311,7 @@ pub(crate) fn encode_inter_frame( Some(start_cdfs.0.clone()), sign_bias, h.allow_screen_content_tools, + switchable_motion_mode, fctx, ) }, @@ -7154,6 +7344,7 @@ pub(crate) fn encode_inter_frame( mi_rows: header.mi_rows, base_q_idx, tx_select, + switchable_motion_mode, screen, start_cdfs, next_cdfs, @@ -7924,6 +8115,7 @@ mod tests { mv: (0, 0), ref_mv_idx: 0, }), + obmc: false, }; let skipped_block = BlockCoeffs { skip: true, @@ -7943,6 +8135,10 @@ mod tests { Quadrant::Whole(skipped_block.clone()), Quadrant::Whole(skipped_block), ]; + // lane-av1obmc: the header this tile is decoded under carries + // `is_motion_mode_switchable`, so the writer must be armed with it + // too or the decoder reads a `motion_mode` symbol nobody wrote. + crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable); let tile = crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) .unwrap(); @@ -9493,6 +9689,14 @@ 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`. Like + // `EC_AV1_PRICE_FRAME_CDFS=1` before it, that knob changes encoder + // decisions, so this bound and the byte pins above FAIL BY DESIGN + // under it; the default path (no motion_mode syntax at all) is the + // one they are measured on. assert!( worst_under <= 0.34, "the writer spent {:.2}% more than the search priced -- more than \ @@ -11017,6 +11221,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) = diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 4716428e..079e0946 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -381,6 +381,99 @@ pub(crate) fn arm_reference_select(on: bool) { REFERENCE_SELECT.with(|c| c.set(on)); } +thread_local! { + /// This frame header's own `is_motion_mode_switchable` bit (spec 5.9.2), + /// armed exactly like [`REFERENCE_SELECT`]. `true` makes every + /// single-reference inter block that libaom's `motion_mode_allowed` + /// accepts carry a `motion_mode`/`obmc` symbol right after its MV syntax, + /// exactly where decode.rs' `read_motion_mode` reads one. + static MOTION_MODE: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Arms the frame's `is_motion_mode_switchable` bit for the next tile written +/// on this thread; cleared by [`CdefIdxGuard`] when that writer returns. +pub(crate) fn arm_motion_mode(on: bool) { + MOTION_MODE.with(|c| c.set(on)); +} + +/// How many single-reference inter blocks this writer coded a `motion_mode` +/// symbol for, split by the value it coded and by the block's own footprint +/// -- the histogram a gate prints so that "OBMC is on" is a measurement, not +/// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for +/// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8. +static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 6] = + [const { std::sync::atomic::AtomicUsize::new(0) }; 6]; + +/// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the +/// counts to its own encode. +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates +pub(crate) fn take_motion_mode_hits() -> [usize; 6] { + std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) +} + +/// `read_motion_mode`'s write side (spec 5.11.24), called on a +/// single-reference inter block right after its MV syntax and before the +/// (never switchable here) interpolation filter -- libaom's own sequential +/// order in `read_inter_block_mode_info`. +/// +/// Eligibility is decode.rs' own `motion_mode_eligible`, term for term, with +/// the terms this writer settles statically dropped: no block it codes is +/// `skip_mode`, interintra or a non-IDENTITY-global `GLOBALMV` one, so what +/// is left is the footprint floor and the overlappable-neighbour walk +/// ([`crate::decode::has_overlappable_neighbour`], shared with the decoder so +/// the two cannot drift). The frame header keeps `allow_warped_motion == 0`, +/// so the alphabet is always the 2-symbol `obmc_cdf`, never the 3-symbol +/// `motion_mode_cdf`. +fn write_motion_mode( + enc: &mut SymbolEncoder, + cdfs: &mut Cdfs, + grid: &MiGrid, + (mi_row, mi_col): (usize, usize), + (bw4, bh4): (usize, usize), + (write_w, write_h): (usize, usize), + (mi_cols, mi_rows): (usize, usize), + obmc: bool, +) -> Result<()> { + let eligible = MOTION_MODE.with(std::cell::Cell::get) + && write_w.min(write_h) >= 8 + && crate::decode::has_overlappable_neighbour( + grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, + ); + if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { + eprintln!( + "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={}", + eligible as u8 + ); + } + if !eligible { + // The encoder trialled an OBMC prediction for a block this writer + // codes as SIMPLE_TRANSLATION: its reconstruction is not the + // decoder's (class encoder-grid-drift), which is exactly what the + // `EC_COMP_MISMATCH` rung exists to catch. + if obmc && crate::envflags::env_flag!("EC_COMP_MISMATCH") { + eprintln!( + "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})" + ); + } + return Ok(()); + } + let row = crate::decode::motion_mode_cdf_row(write_w, write_h).ok_or_else(|| { + Error::unsupported( + "AV1 tile", + format!("a {write_w}x{write_h} block has no motion_mode CDF row"), + ) + })?; + enc.symbol(usize::from(obmc), &mut cdfs.obmc[row]); + let bucket = match write_w.min(write_h) { + 32.. => 0, + 16 => 1, + _ => 2, + }; + MOTION_MODE_HITS[usize::from(obmc) + 2 * bucket] + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); + Ok(()) +} + /// The compound half of a block's end-of-block neighbour record: nothing for /// a single-reference or intra block, and `record_compound_mi`'s overwrite of /// the ALTREF default for a compound one (this writer only codes @@ -759,6 +852,7 @@ impl Drop for CdefIdxGuard { SIGN_BIAS.with(|c| c.set(crate::mvstack::NO_SIGN_BIAS)); SCREEN.with(|c| c.set(false)); REFERENCE_SELECT.with(|c| c.set(false)); + MOTION_MODE.with(|c| c.set(false)); INTRABC.with(|c| c.set(false)); INTRABC_GRID.with(|c| *c.borrow_mut() = None); ORDER_HINTS.with(|c| c.set((7, 0, [0; 7]))); @@ -1240,6 +1334,12 @@ pub struct BlockCoeffs { /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key /// frame writers never read this field. pub inter: Option, + /// Whether this (single-reference inter) block's prediction was built + /// with OBMC -- spec `motion_mode == OBMC_CAUSAL`, written by + /// [`write_motion_mode`]. `false` (the [`Default`]) is + /// `SIMPLE_TRANSLATION`, the only motion mode this writer coded before + /// lane-av1obmc. + pub obmc: bool, /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), /// each with its own 8x8 luma transform and 4x4 chroma transforms, in /// 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( ); } } + write_motion_mode( + &mut enc, + &mut cdfs, + &grid, + (mi_row, mi_col), + (8, 8), + (BLOCK, BLOCK), + (mi_cols as usize, mi_rows as usize), + block.obmc, + )?; mode_for_tx = 0; } } else { @@ -5663,6 +5773,16 @@ fn write_inter_frame_leaf( ); } } + write_motion_mode( + enc, + cdfs, + grid, + (mi_row, mi_col), + (SUB_MI as usize, SUB_MI as usize), + (SUB, SUB), + (mi_cols as usize, mi_rows as usize), + block.obmc, + )?; mode_for_tx = 0; } } else { @@ -5893,6 +6013,16 @@ fn write_inter_frame_leaf8( ); } } + write_motion_mode( + enc, + cdfs, + grid, + (mi_row, mi_col), + (2, 2), + (8, 8), + (mi_cols as usize, mi_rows as usize), + block.obmc, + )?; mode_for_tx = 0; } } else {