forge
Explore
Log in

Don't you dare go Hollow

The motion search charges its rate term at half the block lambda and iterates each subpel stage

Browse files at this commit

215871e25cf3ba989128241f7b23749e6a41fe9fAhmet Kaan GümüşParent: f00abe4
  • perf
    • the motion search charges its rate term at half the block lambda and iterates each subpel stage
Show full message
perf(av1): the motion search charges its rate term at half the block lambda and iterates each subpel stage

The search compares SAD while the lambda it is handed is an SSE-domain RD
weight, so a coded vector was over-priced inside the search itself; and one
round per subpel stage cannot reach a winner two half-pels from the integer
centre. MV_SEARCH_LAMBDA 0.5 and MV_SUBPEL_ITERS 4, both swept.

Native BD gate, ours vs libaom / vs rav1e:
12-frame  film A +21.0/-4.8 -> +20.6/-5.1, film B +24.9/-1.9 -> +24.8/-2.1,
          screen +14.5/-33.2 -> +14.6/-33.2
long-GOP  film A +25.2/-7.2 -> +24.7/-7.5, film B +86.7/+7.3 -> +86.8/+7.4

Encoder wall +5-7%. Pins re-taken (inter vectors move).
crates/ec-av1/src/encode.rsmodified+7 -1
@@ -16488,8 +16488,14 @@ mod tests {
1648816488 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
1648916489 // single and compound are priced against the same syntax -- 8311 ->
1649016490 // 8325 bytes at q=150 and 33087 -> 33014 at q=60.
16491+ // Re-taken on lane-newmv: the motion search charges its own rate term
16492+ // at half the block RD lambda ([`crate::motion::MV_SEARCH_LAMBDA`])
16493+ // and each subpel stage re-runs from its own winner
16494+ // ([`crate::motion::MV_SUBPEL_ITERS`]), so every inter block's vector
16495+ // -- and with it its residual -- moved: 8325 -> 8288 bytes at q=150
16496+ // and 33014 -> 33090 at q=60.
1649116497 let pins: [(u8, usize, u64); 2] =
16492- [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];
16498+ [(150, 8288, 0x241d_8115_0a6d_0f34), (60, 33090, 0x7e02_777c_01b4_f035)];
1649316499 let coded: Vec<(u8, usize, u64)> = pins
1649416500 .iter()
1649516501 .map(|&(q, _, _)| {
crates/ec-av1/src/motion.rsmodified+48 -9
@@ -210,8 +210,15 @@ fn mv_bits((diff_row, diff_col): (i32, i32)) -> f64 {
210210 /// full RD lambda over-prices motion by whatever the SSE/SAD ratio of the
211211 /// content is -- the census fact this lane started from is that we code
212212 /// 0.83-2.5 mv bits/block where libaom codes 3.2-7.5. `EC_AV1_MV_SEARCH_LAMBDA`
213-/// (any build, like the encoder's other search margins); 1.0 is the control.
214-const MV_SEARCH_LAMBDA: f64 = 1.0;
213+/// (any build, like the encoder's other search margins); 1.0 was the control.
214+///
215+/// SWEPT on the native 12-frame BD gate, film B (vs libaom / vs rav1e), with
216+/// [`MV_SUBPEL_ITERS`] still at 1: 0.25 +25.2/-1.7, 0.5 +24.9/-2.0,
217+/// 0.75 +24.8/-1.9, 1.0 (control) +24.9/-1.9, 2.0 +26.9/-0.2. Over-charging
218+/// motion is the expensive direction -- the 2.0 arm loses 2.0/1.7 points --
219+/// and the floor of the bowl sits at 0.5-0.75. 0.5 ships together with
220+/// [`MV_SUBPEL_ITERS`] 4; the pair's rows are in lanes/newmv.report.md.
221+const MV_SEARCH_LAMBDA: f64 = 0.5;
215222
216223 /// [`MV_SEARCH_LAMBDA`], read once (this is the per-candidate cost path).
217224 fn search_lambda_mult() -> f64 {
@@ -222,6 +229,28 @@ fn search_lambda_mult() -> f64 {
222229 })
223230 }
224231
232+/// How many times each subpel refinement round may re-run from its own
233+/// winner ([`search_traced_from_step`]'s stages 2 and 3).
234+/// `EC_AV1_MV_SUBPEL_ITERS`; 1 was the single round the search shipped with.
235+///
236+/// MEASURED on the native BD gate at [`MV_SEARCH_LAMBDA`] 0.5: film A
237+/// 12-frame +21.0/-4.8 -> +20.6/-5.1, film B 12-frame +24.9/-1.9 ->
238+/// +24.8/-2.1, screen +14.5/-33.2 -> +14.6/-33.2, film A long-GOP
239+/// +25.2/-7.2 -> +24.7/-7.5, film B long-GOP +86.7/+7.3 -> +86.8/+7.4. Each
240+/// half alone is worth about half of it on film A (+20.8/-4.9 either way);
241+/// on film B this iteration alone LOSES (+25.2/-1.7) and only pays with the
242+/// lighter search rate term beside it. It costs 5-7% encoder wall.
243+const MV_SUBPEL_ITERS: u32 = 4;
244+
245+/// [`MV_SUBPEL_ITERS`], read once.
246+fn subpel_iters() -> u32 {
247+ static N: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
248+ *N.get_or_init(|| match std::env::var("EC_AV1_MV_SUBPEL_ITERS").ok() {
249+ Some(v) => v.parse().unwrap_or(MV_SUBPEL_ITERS),
250+ None => MV_SUBPEL_ITERS,
251+ })
252+}
253+
225254 /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits`
226255 /// cost it settled at.
227256 #[derive(Clone, Copy, Debug, PartialEq)]
@@ -541,13 +570,23 @@ fn search_traced_from_step(
541570 // costed at the precision it commits to, not the integer-pel SAD alone).
542571 let integer_evals = evals.get();
543572 for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] {
544- let round_centre = centre;
545- for (dr, dc) in neighbor_offsets(step) {
546- let candidate = (round_centre.0 + dr, round_centre.1 + dc);
547- let cost = cost_of(candidate);
548- if cost < best_cost {
549- best_cost = cost;
550- centre = candidate;
573+ // lane-newmv, class `search bounded by its heuristic`: ONE round per
574+ // step means a subpel winner two half-pels away from the integer
575+ // centre is unreachable. `EC_AV1_MV_SUBPEL_ITERS` runs the round
576+ // again from its own winner, up to that many times; 1 is the
577+ // control's single round.
578+ for _ in 0..subpel_iters() {
579+ let round_centre = centre;
580+ for (dr, dc) in neighbor_offsets(step) {
581+ let candidate = (round_centre.0 + dr, round_centre.1 + dc);
582+ let cost = cost_of(candidate);
583+ if cost < best_cost {
584+ best_cost = cost;
585+ centre = candidate;
586+ }
587+ }
588+ if centre == round_centre {
589+ break;
551590 }
552591 }
553592 trace.push(best_cost);