diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 445c6520..a1918ace 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -16488,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, _, _)| { diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index 8aa96107..cc55bc4a 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -210,8 +210,15 @@ fn mv_bits((diff_row, diff_col): (i32, i32)) -> f64 { /// 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 is the control. -const MV_SEARCH_LAMBDA: f64 = 1.0; +/// (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 { @@ -222,6 +229,28 @@ fn search_lambda_mult() -> f64 { }) } +/// 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)] @@ -541,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);