The motion search charges its rate term at half the block lambda and iterates each subpel stage
Browse files at this commit215871e25cf3ba989128241f7b23749e6a41fe9fParent: f00abe4- perf
- the motion search charges its rate term at half the block lambda and iterates each subpel stage
2 changed files +55 -10
crates/ec-av1/src/encode.rsmodified+7 -1
| @@ -16488,8 +16488,14 @@ mod tests { | |||
| 16488 | // pays the `comp_mode = 0` symbol the writer codes ahead of it, so | 16488 | // pays the `comp_mode = 0` symbol the writer codes ahead of it, so |
| 16489 | // single and compound are priced against the same syntax -- 8311 -> | 16489 | // single and compound are priced against the same syntax -- 8311 -> |
| 16490 | // 8325 bytes at q=150 and 33087 -> 33014 at q=60. | 16490 | // 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. | ||
| 16491 | let pins: [(u8, usize, u64); 2] = | 16497 | 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)]; |
| 16493 | let coded: Vec<(u8, usize, u64)> = pins | 16499 | let coded: Vec<(u8, usize, u64)> = pins |
| 16494 | .iter() | 16500 | .iter() |
| 16495 | .map(|&(q, _, _)| { | 16501 | .map(|&(q, _, _)| { |
crates/ec-av1/src/motion.rsmodified+48 -9
| @@ -210,8 +210,15 @@ fn mv_bits((diff_row, diff_col): (i32, i32)) -> f64 { | |||
| 210 | /// full RD lambda over-prices motion by whatever the SSE/SAD ratio of the | 210 | /// full RD lambda over-prices motion by whatever the SSE/SAD ratio of the |
| 211 | /// content is -- the census fact this lane started from is that we code | 211 | /// content is -- the census fact this lane started from is that we code |
| 212 | /// 0.83-2.5 mv bits/block where libaom codes 3.2-7.5. `EC_AV1_MV_SEARCH_LAMBDA` | 212 | /// 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. | 213 | +/// (any build, like the encoder's other search margins); 1.0 was the control. |
| 214 | -const MV_SEARCH_LAMBDA: f64 = 1.0; | 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; | ||
| 215 | 222 | ||
| 216 | /// [`MV_SEARCH_LAMBDA`], read once (this is the per-candidate cost path). | 223 | /// [`MV_SEARCH_LAMBDA`], read once (this is the per-candidate cost path). |
| 217 | fn search_lambda_mult() -> f64 { | 224 | fn search_lambda_mult() -> f64 { |
| @@ -222,6 +229,28 @@ fn search_lambda_mult() -> f64 { | |||
| 222 | }) | 229 | }) |
| 223 | } | 230 | } |
| 224 | 231 | ||
| 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 | + | ||
| 225 | /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits` | 254 | /// One search's outcome: the best MV found and the `SAD + lambda * mv_bits` |
| 226 | /// cost it settled at. | 255 | /// cost it settled at. |
| 227 | #[derive(Clone, Copy, Debug, PartialEq)] | 256 | #[derive(Clone, Copy, Debug, PartialEq)] |
| @@ -541,13 +570,23 @@ fn search_traced_from_step( | |||
| 541 | // costed at the precision it commits to, not the integer-pel SAD alone). | 570 | // costed at the precision it commits to, not the integer-pel SAD alone). |
| 542 | let integer_evals = evals.get(); | 571 | let integer_evals = evals.get(); |
| 543 | for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] { | 572 | for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] { |
| 544 | - let round_centre = centre; | 573 | + // lane-newmv, class `search bounded by its heuristic`: ONE round per |
| 545 | - for (dr, dc) in neighbor_offsets(step) { | 574 | + // step means a subpel winner two half-pels away from the integer |
| 546 | - let candidate = (round_centre.0 + dr, round_centre.1 + dc); | 575 | + // centre is unreachable. `EC_AV1_MV_SUBPEL_ITERS` runs the round |
| 547 | - let cost = cost_of(candidate); | 576 | + // again from its own winner, up to that many times; 1 is the |
| 548 | - if cost < best_cost { | 577 | + // control's single round. |
| 549 | - best_cost = cost; | 578 | + for _ in 0..subpel_iters() { |
| 550 | - centre = candidate; | 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; | ||
| 551 | } | 590 | } |
| 552 | } | 591 | } |
| 553 | trace.push(best_cost); | 592 | trace.push(best_cost); |