diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 87e10e72..a1918ace 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -4360,6 +4360,155 @@ pub(crate) fn dump_split_census() { } } +/// lane-newmv: a multiplier on the mv RATE term of the BLOCK-level inter +/// decision only -- the `NEWMV` candidate's own mv residual + DRL bits, not +/// its mode/reference syntax and not the search's own cost function +/// ([`crate::motion`]'s `EC_AV1_MV_SEARCH_LAMBDA` is that one). `1.0` is the +/// control; below 1 a coded vector is charged less and `NEWMV` wins more +/// blocks. `EC_AV1_MV_LAMBDA`, class `rd-rate-term-calibration`. +pub(crate) fn mv_lambda_mult() -> f64 { + static M: std::sync::LazyLock = std::sync::LazyLock::new(|| { + crate::envflags::var("EC_AV1_MV_LAMBDA") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(MV_LAMBDA) + }); + *M +} + +/// The shipped value of [`mv_lambda_mult`]. See lanes/newmv.report.md. +const MV_LAMBDA: f64 = 1.0; + +/// What a `NEWMV` candidate is charged ON TOP of its own priced mv syntax +/// bits, `(EC_AV1_MV_LAMBDA - 1) * mv_bits` (added to the candidate's bits, +/// which the RD cost multiplies by lambda). Zero at the default weight. +fn mv_rate_surcharge(mv_bits: f64) -> f64 { + (mv_lambda_mult() - 1.0) * mv_bits +} + +/// lane-newmv census (`EC_AV1_NEWMV_CENSUS=1`): the block-level `NEWMV` +/// decision, bucketed by block side (8/16/32/64). Eight counters per bucket: +/// 0 blocks that ran a `LAST` motion search, 1 of those whose vector the +/// writer could name (offered to RD), 2 blocks `NEWMV` won, 3 the summed RD +/// cost it won by, 4 the summed cost it lost by, 5 the summed +/// `|mv - nearest_mv|` (1/8-pel units, both axes), 6 blocks whose searched +/// vector IS the stack's `NEARESTMV`, 7 the summed mv syntax bits (x64, as +/// [`SPLIT_CENSUS`]'s rate terms are). Four globals follow at 32: extra- +/// reference searches offered/skipped by [`EXTRA_NEW_SKIP_MARGIN`], leaf +/// second-reference searches offered/skipped by [`LEAF_SECOND_NEW_MARGIN`]. +static NEWMV_CENSUS: [std::sync::atomic::AtomicU64; 36] = + [const { std::sync::atomic::AtomicU64::new(0) }; 36]; + +/// Whether [`NEWMV_CENSUS`] is armed, read once. +pub(crate) fn newmv_census_on() -> bool { + static ON: std::sync::LazyLock = + std::sync::LazyLock::new(|| crate::envflags::env_flag!("EC_AV1_NEWMV_CENSUS")); + *ON +} + +/// Bumps one of [`NEWMV_CENSUS`]'s four global counters (32..36). +fn newmv_census_global(i: usize) { + if newmv_census_on() { + NEWMV_CENSUS[32 + i].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } +} + +/// Records one block's `NEWMV` decision into [`NEWMV_CENSUS`]. `other_cost` +/// is the best cost the block had WITHOUT the `NEWMV` candidate (its +/// `NEAREST`/`NEAR`/`GLOBAL`/intra winner), `new_cost` the `NEWMV` +/// candidate's own; both `f64::INFINITY` is a block that offered neither. +fn newmv_census_block( + side: usize, + offered: bool, + new_cost: f64, + other_cost: f64, + mv: (i32, i32), + nearest: (i32, i32), + mv_bits: f64, +) { + if !newmv_census_on() { + return; + } + use std::sync::atomic::Ordering::Relaxed; + let base = (side.trailing_zeros() as usize).saturating_sub(3).min(3) * 8; + let add = |i: usize, v: u64| { + NEWMV_CENSUS[base + i].fetch_add(v, Relaxed); + }; + add(0, 1); + if !offered { + return; + } + add(1, 1); + if new_cost < other_cost { + add(2, 1); + add(3, (other_cost - new_cost).max(0.0) as u64); + } else { + add(4, (new_cost - other_cost).max(0.0) as u64); + } + add(5, ((mv.0 - nearest.0).unsigned_abs() + (mv.1 - nearest.1).unsigned_abs()) as u64); + if mv == nearest { + add(6, 1); + } + add(7, (mv_bits * 64.0).max(0.0) as u64); +} + +/// Prints [`NEWMV_CENSUS`] to stderr when `EC_AV1_NEWMV_CENSUS=1`, on the +/// same guard [`dump_split_census`] rides (cumulative, so the last table a +/// run prints is the whole encode's). +pub(crate) fn dump_newmv_census() { + use std::sync::atomic::Ordering::Relaxed; + eprintln!( + "NEWMVCENSUS side searched offered won won% win_margin lose_margin \ + mv_dist_q3 mv==nearest% mv_bits" + ); + for side_idx in 0..4 { + let base = side_idx * 8; + let v: [u64; 8] = std::array::from_fn(|i| NEWMV_CENSUS[base + i].load(Relaxed)); + if v[0] == 0 { + continue; + } + let off = v[1].max(1) as f64; + eprintln!( + "NEWMVCENSUS {:>3} {:>8} {:>8} {:>7} {:>5.1} {:>10.0} {:>11.0} {:>10.2} {:>12.1} {:>7.2}", + 8 << side_idx, + v[0], + v[1], + v[2], + 100.0 * v[2] as f64 / off, + v[3] as f64 / v[2].max(1) as f64, + v[4] as f64 / (v[1] - v[2]).max(1) as f64, + v[5] as f64 / off, + 100.0 * v[6] as f64 / off, + v[7] as f64 / off / 64.0, + ); + } + let g: [u64; 4] = std::array::from_fn(|i| NEWMV_CENSUS[32 + i].load(Relaxed)); + eprintln!( + "NEWMVCENSUS pre-RD margin skips: extra-reference {}/{} ({:.1}%), \ + leaf second reference {}/{} ({:.1}%)", + g[1], + g[0], + 100.0 * g[1] as f64 / g[0].max(1) as f64, + g[3], + g[2], + 100.0 * g[3] as f64 / g[2].max(1) as f64, + ); + let [calls, evals, _subpel, rounds, near_pred, unmoved, widest, widest_moves] = + crate::motion::take_census(); + let per = |n: u64| n as f64 / calls.max(1) as f64; + eprintln!( + "NEWMVCENSUS search: {calls} calls, {:.1} evals/call, {:.2} integer rounds/call; \ + winner within +-1 pel of pred_mv {:.1}%, winner = starting seed {:.1}%, \ + moved at the WIDEST step {:.1}% of calls ({:.2} such moves/call)", + per(evals), + per(rounds), + 100.0 * near_pred as f64 / calls.max(1) as f64, + 100.0 * unmoved as f64 / calls.max(1) as f64, + 100.0 * widest as f64 / calls.max(1) as f64, + per(widest_moves), + ); +} + /// [`TX_TYPE_HITS`] for the INTER luma units (lane-txset2), counted apart so /// the intra witness's fire count stays its own. pub(crate) static INTER_TX_TYPE_HITS: [std::sync::atomic::AtomicUsize; 16] = @@ -6435,7 +6584,12 @@ fn code_square_inter( ); let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); searched_new_cost = Some(found.cost); - if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) { + let mv_residual = mv_residual_bits(new_mv, stack.pred_mv); + if mv_residual.is_none() { + // lane-newmv: a vector the writer cannot name never reaches RD. + newmv_census_block(side, false, f64::INFINITY, best.0, new_mv, stack.nearest_mv, 0.0); + } + if let Some(mv_bits) = mv_residual { searched_new = Some(new_mv); // Same reuse as `search_inter_block`: a NEWMV equal to the // stack's NEARESTMV is the same three trials, already run above. @@ -6477,11 +6631,13 @@ fn code_square_inter( + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + drl_bits + mv_bits + + mv_rate_surcharge(mv_bits) + if skip_new { 0.0 } else { luma_new.bits + u_new.bits + v_new.bits }); + newmv_census_block(side, true, cost_new, best.0, new_mv, stack.nearest_mv, mv_bits); if cost_new < best.0 { single_syntax = intra_inter_bits(true) + single_ref_bits @@ -6571,6 +6727,7 @@ fn code_square_inter( let margin = leaf_second_new_margin(); let mut second_new: Option<(i32, i32)> = None; if leaf_second_new_mv() { + newmv_census_global(2); let skip = margin > 0.0 && searched_new_cost.is_some_and(|last| { motion::cost_at( @@ -6579,6 +6736,9 @@ fn code_square_inter( ) * margin <= last }); + if skip { + newmv_census_global(3); + } if !skip { let mut seeds = [(0, 0); 4]; let mut seed_n = 1; @@ -9130,7 +9290,7 @@ fn search_skip_64( if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(stack, mv) { last_new_mv = Some(mv); cands.push(( - symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits + mv_rate_surcharge(bits), info_of(InterMode::NewMv, mv, ref_mv_idx), )); } @@ -9672,13 +9832,20 @@ fn search_inter_block( #[cfg(test)] stage_since(0, t); let mv = round_to_valid_mv(found.mv, stack.pred_mv); + // lane-newmv: this block's own `NEWMV` vector and what its mv syntax + // costs, for the census below (`None` when the writer cannot name it). + let mut newmv_syntax: Option<((i32, i32), f64)> = None; // Each DRL entry is a different `PredMv` (spec 7.10.2.10 `assign_mv`), so // the same vector costs a different residual from each: take the index // whose residual plus DRL bits is cheapest, not entry 0 by default. if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(stack, mv) { + newmv_syntax = Some((mv, bits)); cands.push(( mv, - last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, + last_ref_bits + + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + + bits + + mv_rate_surcharge(bits), InterInfo { ref1: None, mv1: (0, 0), @@ -9739,6 +9906,9 @@ fn search_inter_block( let searched = extra_ref_new_mv() && (ref_frame != crate::mvstack::LAST2_FRAME || last2_new_mv()); let margin = extra_new_skip_margin(); + if searched { + newmv_census_global(0); + } let skip_search = searched && margin > 0.0 && motion::cost_at( @@ -9757,6 +9927,9 @@ fn search_inter_block( fctx, ) * margin <= found.cost; + if skip_search { + newmv_census_global(1); + } if searched && !skip_search { let (gseeds, gn) = mv_seeds(gstack); #[cfg(test)] @@ -9980,6 +10153,10 @@ fn search_inter_block( }); cands.dedup_by_key(|c| (c.2.ref_frame, c.0)); census_add(7, cands.len()); + // lane-newmv: the NEWMV candidate's own RD cost against the best cost + // this block reaches WITHOUT it, taken inside the one loop that prices + // them all (a second pricing pass would be a second oracle). + let (mut new_cost, mut other_cost) = (f64::INFINITY, f64::INFINITY); { for (mv, mode_bits_inter, info) in cands { let (ref_luma, ref_u, ref_v) = match extra @@ -10066,8 +10243,23 @@ fn search_inter_block( mode_bits: mode_bits_inter, inter: Some(info), }); + if newmv_census_on() { + if info.mode == InterMode::NewMv + && info.ref1.is_none() + && info.ref_frame == crate::mvstack::LAST_FRAME + { + new_cost = cost; + } else { + other_cost = other_cost.min(cost); + } + } } } + if let Some((new_mv, bits)) = newmv_syntax { + newmv_census_block(BLOCK, true, new_cost, other_cost, new_mv, stack.nearest_mv, bits); + } else { + newmv_census_block(BLOCK, false, f64::INFINITY, other_cost, mv, stack.nearest_mv, 0.0); + } let best = best.expect("the search offers at least the intra modes"); // lane-av1obmc: the OBMC candidate (spec `motion_mode == OBMC_CAUSAL`), @@ -16296,8 +16488,14 @@ mod tests { // pays the `comp_mode = 0` symbol the writer codes ahead of it, so // single and compound are priced against the same syntax -- 8311 -> // 8325 bytes at q=150 and 33087 -> 33014 at q=60. + // Re-taken on lane-newmv: the motion search charges its own rate term + // at half the block RD lambda ([`crate::motion::MV_SEARCH_LAMBDA`]) + // and each subpel stage re-runs from its own winner + // ([`crate::motion::MV_SUBPEL_ITERS`]), so every inter block's vector + // -- and with it its residual -- moved: 8325 -> 8288 bytes at q=150 + // and 33014 -> 33090 at q=60. let pins: [(u8, usize, u64); 2] = - [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)]; + [(150, 8288, 0x241d_8115_0a6d_0f34), (60, 33090, 0x7e02_777c_01b4_f035)]; let coded: Vec<(u8, usize, u64)> = pins .iter() .map(|&(q, _, _)| { @@ -16944,7 +17142,8 @@ mod tests { // evaluations (one `mc::predict` + SAD each) a search spends, how // many of them are subpel, and how often the winner is the seed // it started from -- the census the seeded search is judged by. - let [calls, evals, subpel, rounds, near_pred, unmoved] = crate::motion::take_census(); + let [calls, evals, subpel, rounds, near_pred, unmoved, widest, widest_moves] = + crate::motion::take_census(); let per = |n: u64| n as f64 / calls.max(1) as f64; eprintln!( "{name}: motion search {calls} calls, {evals} candidate evals ({:.1}/call, \ @@ -16956,6 +17155,15 @@ mod tests { 100.0 * near_pred as f64 / calls.max(1) as f64, 100.0 * unmoved as f64 / calls.max(1) as f64, ); + // lane-newmv, class `instrument at bound`: how often the winner + // was walked to at the log stage's own WIDEST step -- a high + // share means the search stops where its step lets it, not where + // the content is. + eprintln!( + "{name}: motion search moved at the widest step in {:.1}% of calls ({:.2} such moves/call)", + 100.0 * widest as f64 / calls.max(1) as f64, + per(widest_moves), + ); eprintln!( "{name}: references LAST {} GOLDEN {} (LAST2 {} LAST3 {} BWD {} ALT2 {} ALT {})", refs[0], refs[3], refs[1], refs[2], refs[4], refs[5], refs[6] diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 2b5bef96..7f935774 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1386,13 +1386,19 @@ impl Av1Encoder { struct SplitCensusDump; impl Drop for SplitCensusDump { fn drop(&mut self) { - crate::encode::dump_split_census(); + if crate::encode::split_census_on() { + crate::encode::dump_split_census(); + } + if crate::encode::newmv_census_on() { + crate::encode::dump_newmv_census(); + } } } // `then_some` would CONSTRUCT the guard eagerly and drop it when the // flag is off, which runs `Drop` -- and printed the census header on // every unarmed encode. `then` builds it only when armed. - let _census = crate::encode::split_census_on().then(|| SplitCensusDump); + let _census = (crate::encode::split_census_on() || crate::encode::newmv_census_on()) + .then(|| SplitCensusDump); if (picture.width, picture.height) != (self.config.width, self.config.height) { return Err(Error::unsupported( "AV1 encode", diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index c55c994c..cc55bc4a 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -88,9 +88,15 @@ fn seeded() -> bool { /// 1 total candidate evaluations (one `predict` + SAD each), 2 of which in /// the two subpel stages, 3 integer-stage rounds, 4 calls whose winner is /// within one whole sample of `pred_mv`, 5 calls whose winner is the -/// starting centre itself. A gate prints these rather than assume where the -/// wall-owning stage goes (gate-blind-to-feature). -static CENSUS: [std::sync::atomic::AtomicU64; 6] = [ +/// starting centre itself, 6 calls that moved at least once at the log +/// stage's WIDEST step (the class `instrument at bound`: a search whose +/// winner was reached by walking at its own initial step is measuring its +/// step, not the content), 7 the total such widest-step moves. A gate prints +/// these rather than assume where the wall-owning stage goes +/// (gate-blind-to-feature). +static CENSUS: [std::sync::atomic::AtomicU64; 8] = [ + std::sync::atomic::AtomicU64::new(0), + std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0), @@ -100,9 +106,8 @@ static CENSUS: [std::sync::atomic::AtomicU64; 6] = [ ]; /// Takes (and clears) the census. -#[cfg(test)] -pub(crate) fn take_census() -> [u64; 6] { - [0, 1, 2, 3, 4, 5].map(|i| CENSUS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) +pub(crate) fn take_census() -> [u64; 8] { + [0, 1, 2, 3, 4, 5, 6, 7].map(|i| CENSUS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) } /// The eight offsets, at `step` in each axis, a diamond/log search round @@ -198,6 +203,54 @@ fn mv_bits((diff_row, diff_col): (i32, i32)) -> f64 { bits } +/// lane-newmv: what the SEARCH's own rate term is weighted by, on top of the +/// block RD lambda it is handed. The RD lambda is an SSE-domain weight +/// (`encode::Search::lambda` is `LAMBDA_SCALE * step * step`, compared against +/// squared error) while this search compares SAD, so charging mv bits at the +/// full RD lambda over-prices motion by whatever the SSE/SAD ratio of the +/// content is -- the census fact this lane started from is that we code +/// 0.83-2.5 mv bits/block where libaom codes 3.2-7.5. `EC_AV1_MV_SEARCH_LAMBDA` +/// (any build, like the encoder's other search margins); 1.0 was the control. +/// +/// SWEPT on the native 12-frame BD gate, film B (vs libaom / vs rav1e), with +/// [`MV_SUBPEL_ITERS`] still at 1: 0.25 +25.2/-1.7, 0.5 +24.9/-2.0, +/// 0.75 +24.8/-1.9, 1.0 (control) +24.9/-1.9, 2.0 +26.9/-0.2. Over-charging +/// motion is the expensive direction -- the 2.0 arm loses 2.0/1.7 points -- +/// and the floor of the bowl sits at 0.5-0.75. 0.5 ships together with +/// [`MV_SUBPEL_ITERS`] 4; the pair's rows are in lanes/newmv.report.md. +const MV_SEARCH_LAMBDA: f64 = 0.5; + +/// [`MV_SEARCH_LAMBDA`], read once (this is the per-candidate cost path). +fn search_lambda_mult() -> f64 { + static M: std::sync::OnceLock = std::sync::OnceLock::new(); + *M.get_or_init(|| match std::env::var("EC_AV1_MV_SEARCH_LAMBDA").ok() { + Some(v) => v.parse().unwrap_or(MV_SEARCH_LAMBDA), + None => MV_SEARCH_LAMBDA, + }) +} + +/// How many times each subpel refinement round may re-run from its own +/// winner ([`search_traced_from_step`]'s stages 2 and 3). +/// `EC_AV1_MV_SUBPEL_ITERS`; 1 was the single round the search shipped with. +/// +/// MEASURED on the native BD gate at [`MV_SEARCH_LAMBDA`] 0.5: film A +/// 12-frame +21.0/-4.8 -> +20.6/-5.1, film B 12-frame +24.9/-1.9 -> +/// +24.8/-2.1, screen +14.5/-33.2 -> +14.6/-33.2, film A long-GOP +/// +25.2/-7.2 -> +24.7/-7.5, film B long-GOP +86.7/+7.3 -> +86.8/+7.4. Each +/// half alone is worth about half of it on film A (+20.8/-4.9 either way); +/// on film B this iteration alone LOSES (+25.2/-1.7) and only pays with the +/// lighter search rate term beside it. It costs 5-7% encoder wall. +const MV_SUBPEL_ITERS: u32 = 4; + +/// [`MV_SUBPEL_ITERS`], read once. +fn subpel_iters() -> u32 { + static N: std::sync::OnceLock = std::sync::OnceLock::new(); + *N.get_or_init(|| match std::env::var("EC_AV1_MV_SUBPEL_ITERS").ok() { + Some(v) => v.parse().unwrap_or(MV_SUBPEL_ITERS), + None => MV_SUBPEL_ITERS, + }) +} + /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits` /// cost it settled at. #[derive(Clone, Copy, Debug, PartialEq)] @@ -244,7 +297,7 @@ fn cost_of_mv( .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs()) .sum(); let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1); - f64::from(sad) + lambda * mv_bits(diff) + f64::from(sad) + lambda * search_lambda_mult() * mv_bits(diff) } /// [`cost_of_mv`] for one vector, on its own scratch -- what a caller uses to @@ -480,6 +533,7 @@ fn search_traced_from_step( // Stage 1: log/diamond search over whole-pel steps, halving the step // each time a round finds no better neighbour, down to one sample. let mut step_pel = initial_step_pel; + let mut widest_moves = 0u64; while step_pel >= SEARCH_MIN_STEP_PEL { let step = step_pel * PEL_Q3; loop { @@ -498,6 +552,9 @@ fn search_traced_from_step( } } let moved = best_in_round < best_cost; + if moved && step_pel == initial_step_pel { + widest_moves += 1; + } best_cost = best_in_round; CENSUS[3].fetch_add(1, std::sync::atomic::Ordering::Relaxed); trace.push(best_cost); @@ -513,13 +570,23 @@ fn search_traced_from_step( // costed at the precision it commits to, not the integer-pel SAD alone). let integer_evals = evals.get(); for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] { - let round_centre = centre; - for (dr, dc) in neighbor_offsets(step) { - let candidate = (round_centre.0 + dr, round_centre.1 + dc); - let cost = cost_of(candidate); - if cost < best_cost { - best_cost = cost; - centre = candidate; + // lane-newmv, class `search bounded by its heuristic`: ONE round per + // step means a subpel winner two half-pels away from the integer + // centre is unreachable. `EC_AV1_MV_SUBPEL_ITERS` runs the round + // again from its own winner, up to that many times; 1 is the + // control's single round. + for _ in 0..subpel_iters() { + let round_centre = centre; + for (dr, dc) in neighbor_offsets(step) { + let candidate = (round_centre.0 + dr, round_centre.1 + dc); + let cost = cost_of(candidate); + if cost < best_cost { + best_cost = cost; + centre = candidate; + } + } + if centre == round_centre { + break; } } trace.push(best_cost); @@ -535,6 +602,10 @@ fn search_traced_from_step( if centre == start_centre { CENSUS[5].fetch_add(1, Relaxed); } + if widest_moves > 0 { + CENSUS[6].fetch_add(1, Relaxed); + CENSUS[7].fetch_add(widest_moves, Relaxed); + } ( MotionSearch { diff --git a/lanes/newmv.report.md b/lanes/newmv.report.md new file mode 100644 index 00000000..fa74b2f8 --- /dev/null +++ b/lanes/newmv.report.md @@ -0,0 +1,165 @@ +# lane-newmv — why we code NEWMV on a fifth of the blocks libaom does + +Worktree `edith_codecs-newmv`, branch `lane-newmv` off main 6b95562d. Every +BD row is the prebuilt release lib-test binary (`bd_rate_screen_native`, 12 +pictures, `gop 12`, four quantizers, native `gate_crop`; `bd_rate_film_long_gop` +for the 48-picture rows), each detached under its own `systemd-run` unit, read +off the log's own clip line. + +## 0. The controls reproduce + +| row | control (vs libaom / vs rav1e) | charter | +|---|---|---| +| film A, 12 frames | +21.0 / -4.8 | +21.0 / -4.8 | +| film B, 12 frames | +24.9 / -1.9 | +24.9 / -1.9 | +| screen, 12 frames | +14.5 / -33.2 | +14.5 / -33.2 | +| film A, long GOP | +25.2 / -7.2 | +25.2 / -7.2 | +| film B, long GOP | +86.7 / +7.3 | +86.7 / +7.3 | + +All five land to the digit. + +## 1. The instrument (`EC_AV1_NEWMV_CENSUS=1`) + +Per block side: how often a `LAST` `NEWMV` is searched, how often the writer +can name the vector (offered to RD), how often it wins, the mean RD margin on +each side of the decision, the searched vector's distance from `NEARESTMV`, +how often it IS `NEARESTMV`, and its mv syntax bits. Then the two pre-RD +margins' skip shares, and the search's own bound witness (class `instrument at +bound`: how often the winner was walked to at the log stage's WIDEST step). +It rides the guard `EC_AV1_SPLIT_CENSUS` already had in +`Av1Encoder::encode_frames`, so it prints on every exit and costs nothing when +unset. + +Film B, gate crop, 12 frames, q=90 — CONTROL: + +| side | searched | offered | won | won% | win margin | lose margin | \|mv-nearest\| | mv==nearest | mv bits | +|---|---|---|---|---|---|---|---|---|---| +| 8 | 65296 | 65296 | 13319 | 20.4% | 47 | 26 | 16.48 | 61.3% | 5.90 | +| 16 | 25500 | 25500 | 9564 | 37.5% | 106 | 35 | 17.29 | 41.6% | 6.68 | +| 32 | 18344 | 18344 | 3627 | 19.8% | 137 | 32 | 9.69 | 54.1% | 5.82 | + +pre-RD margin skips: extra-reference 86.2%, leaf second reference 4.2%. +search: 293394 calls, 69.2 evals/call, 6.35 integer rounds/call; winner within +±1 pel of `pred_mv` 54.9%, winner = starting seed 17.4%, moved at the widest +step 18.9% of calls. + +READ IT AS: + +1. **No margin drops LAST's `NEWMV`.** `searched == offered` in every row: the + candidate reaches RD on every inter block. The two skip margins + (`EXTRA_NEW_SKIP_MARGIN`, `LEAF_SECOND_NEW_MARGIN`) gate only the EXTRA + references' searches. The charter's first hypothesis is refuted by the + instrument. +2. **The vectors we do code are the right size.** 5.8-6.7 mv bits per CODED + `NEWMV` against libaom's 3.2-7.5 per BLOCK: the whole census gap + (0.83-2.5 mv bits/block) is the SHARE, not the precision. +3. **It loses by very little.** The mean losing margin (26-43 RD units) is a + quarter to a third of the mean winning one — a near-tie decision, i.e. a + population a small change in the search or the rate weight moves. +4. **The search is not at its own bound.** Only 18.9% of calls ever move at + the widest log step, so the initial step / distance scale is not what + stops it. But over half its winners sit within ±1 pel of `pred_mv` and + 41-61% of them ARE `NEARESTMV`. + +## 2. The sweep (film B, 12 frames) + +| arm | vs libaom | vs rav1e | +|---|---|---| +| control | +24.9 | -1.9 | +| `EC_AV1_MV_LAMBDA=0.5` (block RD mv rate x0.5) | +26.3 | -0.6 | +| `EC_AV1_MV_LAMBDA=1.5` | +25.3 | -1.7 | +| `EC_AV1_MV_SEARCH_LAMBDA=0.25` | +25.2 | -1.7 | +| `EC_AV1_MV_SEARCH_LAMBDA=0.5` | +24.9 | -2.0 | +| `EC_AV1_MV_SEARCH_LAMBDA=0.75` | +24.8 | -1.9 | +| `EC_AV1_MV_SEARCH_LAMBDA=2.0` | +26.9 | -0.2 | +| `EC_AV1_MV_SUBPEL_ITERS=4` | +25.2 | -1.7 | +| **`SUBPEL_ITERS=4` + `SEARCH_LAMBDA=0.5`** | **+24.8** | **-2.1** | + +The chartered lever — the mv RATE term of the BLOCK decision — is +**single-peaked at the control on both sides** (0.5 loses 1.4/1.3, 1.5 loses +0.4/0.2). That is the third instance of class `rd-rate-term-calibration` on +this encoder (lane-txd, lane-split, here): our block-level pricing of a coded +vector is already RD-optimal on this content, so the finding the charter named +as the fallback holds — it is the SEARCH, not the pricing. + +The two search-side knobs are new and both are one-liners: + +* `EC_AV1_MV_SEARCH_LAMBDA` (`motion::MV_SEARCH_LAMBDA`): the search compares + **SAD** while the lambda it is handed is the block RD's **SSE-domain** + weight (`LAMBDA_SCALE * step * step`), so charging mv bits at the full RD + lambda over-prices motion inside the search itself and pins the winner to + `pred_mv`. Halving it is the floor of a clean bowl; doubling it costs 2.0 + points. +* `EC_AV1_MV_SUBPEL_ITERS` (`motion::MV_SUBPEL_ITERS`): each subpel stage ran + ONE ±1-step round, so a winner two half-pels off the integer centre was + unreachable (class `search bounded by its heuristic`). It re-runs from its + own winner now, up to 4 times, stopping when a round does not move. + +## 3. What ships + +`MV_SEARCH_LAMBDA = 0.5`, `MV_SUBPEL_ITERS = 4`. `MV_LAMBDA` stays 1.0 (the +knob stays for the next sweep). + +| row | control | shipped | move | +|---|---|---|---| +| film A, 12 frames | +21.0 / -4.8 | **+20.6 / -5.1** | -0.4 / -0.3 | +| film B, 12 frames | +24.9 / -1.9 | **+24.8 / -2.1** | -0.1 / -0.2 | +| screen, 12 frames | +14.5 / -33.2 | +14.6 / -33.2 | +0.1 / 0.0 | +| film A, long GOP | +25.2 / -7.2 | **+24.7 / -7.5** | -0.5 / -0.3 | +| film B, long GOP | +86.7 / +7.3 | +86.8 / +7.4 | +0.1 / +0.1 | + +Keep rule met: both film rows improve on both columns at 12 frames; at long +GOP film A is 0.5/0.3 down with film B flat inside ±0.3; screen is 0.1 worse, +inside the 0.3 the rule allows. Wall (ours, same arm's own line): film A 146.5 +-> 150.4 s, film B 123.2 -> 131.4 s, film A long GOP 564.9 -> 607.0 s, film B +long GOP 417.2 -> 437.7 s — +2.7% to +7.4%, under the 15% ceiling. + +Decomposed on film A (12 frames): each half alone reads +20.8/-4.9, the pair ++20.6/-5.1 — they compose. On film B the subpel iteration ALONE loses +(+25.2/-1.7) and only pays with the lighter search rate term beside it, which +is why the pair ships as a pair. + +## 4. The mechanism moved, not just the bytes + +Same census, same clip and quantizer, after the change: + +| side | won% (was) | mv bits (was) | mv==nearest (was) | +|---|---|---|---| +| 8 | **30.7%** (20.4) | 7.82 (5.90) | 36.4% (61.3) | +| 16 | **46.5%** (37.5) | 8.21 (6.68) | 21.6% (41.6) | +| 32 | **23.3%** (19.8) | 6.76 (5.82) | 38.5% (54.1) | + +The search now spends 81.7 evals/call (was 69.2) and its winner sits within +±1 pel of `pred_mv` in 42.6% of calls (was 54.9). The NEWMV share moves toward +libaom's 54-76% at 3.2-7.5 mv bits/block, i.e. the BD move is the mechanism +the census pointed at — not something else that happened to move bytes. + +## 5. Pins + +`the_encoders_own_streams_are_byte_identical_to_their_pins` re-taken with a +two-line comment: 8325 -> 8288 bytes at q=150 and 33014 -> 33090 at q=60. +Green at the default and at `EC_AV1_SPEED=6`. No other byte pin went red +(split suite below). + +## 6. Gates + +* split suite, release lib tests: s1 (`--skip stream::`) 339 passed / 0 failed + RC=0; s2 (`stream:: --skip 10bit`) 200 / 0 RC=0; s3 (`10bit`) 42 / 0 RC=0. +* `--ignored encoder::tests::every_speed_preset_decodes_sample_exact_through_both_decoders` + 1 passed RC=0. +* `cargo check --workspace --all-targets`: 0 errors, 0 ec-av1 warnings. + +## 7. What this lane did NOT do + +* `deferred: allow_high_precision_mv` — the writer codes every frame with it + off, so the finest vector the search can commit to is a quarter pel + (`round_to_valid_mv` rounds the residual to an even 1/8-pel count). Whether + 1/8-pel vectors pay is unmeasured; it needs the header bit, the `mv_hp` + symbols and a decode witness first, then this same gate. +* `deferred: the mv rate weight of the COMPOUND halves and of the 64x64 skip + root` — `EC_AV1_MV_LAMBDA` reaches the single-reference `NEWMV` candidates + only. Since the single-reference sweep came back single-peaked at 1.0, + re-pricing the compound halves has no measured reason to be different. +* `deferred: a per-speed-preset value for MV_SUBPEL_ITERS` — it costs 5-7% + wall at every preset, and the preset ladder's own wall/BD Pareto + (lanes `software speed Pareto`) is where that belongs, not here.