wip(av1): motion-search census, stack-seeded starts, extra-ref NEWMV behind knobs
Browse files at this commit082d0dcbc2cbe9f300c4d925194e67c3ca03714cParent: 0f156682 changed files +332 -48
- crates/ec-av1/src/encode.rs +165 -28
- crates/ec-av1/src/motion.rs +167 -20
crates/ec-av1/src/encode.rsmodified+165 -28
| @@ -165,6 +165,24 @@ fn split_inter_8() -> bool { | ||
| 165 | 165 | /// NEARESTMV candidate it always had. |
| 166 | 166 | const LEAF_NEW_MV: bool = true; |
| 167 | 167 | |
| 168 | +/// Whether GOLDEN/ALTREF get a `NEWMV` search of their own. Set from the | |
| 169 | +/// lane report's measurement. | |
| 170 | +const EXTRA_REF_NEW_MV: bool = false; | |
| 171 | + | |
| 172 | +/// How much cheaper an extra reference's `NEARESTMV` has to be than LAST's | |
| 173 | +/// own best vector before that reference's `NEWMV` search is skipped: a | |
| 174 | +/// factor on the search cost scale, `1.0` meaning "as good or better". | |
| 175 | +/// Swept on the BD gate (see the lane report) -- `0.0` never skips. | |
| 176 | +const EXTRA_NEW_SKIP_MARGIN: f64 = 0.0; | |
| 177 | + | |
| 178 | +/// [`EXTRA_NEW_SKIP_MARGIN`], swept by `EC_AV1_MV_SKIP_MARGIN`. | |
| 179 | +fn extra_new_skip_margin() -> f64 { | |
| 180 | + match std::env::var("EC_AV1_MV_SKIP_MARGIN").ok() { | |
| 181 | + Some(v) if cfg!(test) => v.parse().unwrap_or(EXTRA_NEW_SKIP_MARGIN), | |
| 182 | + _ => EXTRA_NEW_SKIP_MARGIN, | |
| 183 | + } | |
| 184 | +} | |
| 185 | + | |
| 168 | 186 | /// [`LEAF_NEW_MV`], or what `EC_AV1_LEAF_NEWMV` names in a test build. |
| 169 | 187 | fn leaf_new_mv() -> bool { |
| 170 | 188 | match std::env::var("EC_AV1_LEAF_NEWMV").ok() { |
| @@ -2317,6 +2335,7 @@ fn code_square_inter( | ||
| 2317 | 2335 | let mut best = (inter_cost, None); |
| 2318 | 2336 | if leaf_new_mv() { |
| 2319 | 2337 | let source_block = luma.source_block(x, y, side); |
| 2338 | + let (seeds, seed_n) = mv_seeds(stack); | |
| 2320 | 2339 | let found = motion::search( |
| 2321 | 2340 | ref_luma.0, |
| 2322 | 2341 | ref_luma.1, |
| @@ -2328,6 +2347,7 @@ fn code_square_inter( | ||
| 2328 | 2347 | side, |
| 2329 | 2348 | side, |
| 2330 | 2349 | stack.pred_mv, |
| 2350 | + &seeds[..seed_n], | |
| 2331 | 2351 | search.lambda, fctx, |
| 2332 | 2352 | ); |
| 2333 | 2353 | let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); |
| @@ -3318,6 +3338,68 @@ fn mv_component_bits(diff: i32) -> Option<f64> { | ||
| 3318 | 3338 | Some(bits) |
| 3319 | 3339 | } |
| 3320 | 3340 | |
| 3341 | +/// Whether an extra reference (GOLDEN/ALTREF) is offered a `NEWMV` of its | |
| 3342 | +/// own -- a second motion search per block against that reference's picture, | |
| 3343 | +/// priced off that reference's own stack. `EC_AV1_MV_EXTRA_NEW=0` turns it | |
| 3344 | +/// off in a test build (what the lane measured both arms with). | |
| 3345 | +fn extra_ref_new_mv() -> bool { | |
| 3346 | + match std::env::var("EC_AV1_MV_EXTRA_NEW").ok() { | |
| 3347 | + Some(v) if cfg!(test) => v != "0", | |
| 3348 | + _ => EXTRA_REF_NEW_MV, | |
| 3349 | + } | |
| 3350 | +} | |
| 3351 | + | |
| 3352 | +/// [`tile::write_drl_idx`]'s cost against one stack: one `drl_mode` symbol | |
| 3353 | +/// per entry past `start`, `1` to advance and `0` to stop, at most two. | |
| 3354 | +fn drl_bits_for(stack: &MvStack, start: usize, target: usize) -> f64 { | |
| 3355 | + let mut bits = 0.0; | |
| 3356 | + let mut idx = start; | |
| 3357 | + while idx < start + 2 && stack.entries.len() > idx + 1 { | |
| 3358 | + let advance = idx < target; | |
| 3359 | + bits += symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[idx]], usize::from(advance)); | |
| 3360 | + if !advance { | |
| 3361 | + break; | |
| 3362 | + } | |
| 3363 | + idx += 1; | |
| 3364 | + } | |
| 3365 | + bits | |
| 3366 | +} | |
| 3367 | + | |
| 3368 | +/// The cheapest `(bits, drl index)` the `NEWMV` syntax can name `mv` with off | |
| 3369 | +/// `stack`: each DRL entry is a different `PredMv` (spec 7.10.2.10 | |
| 3370 | +/// `assign_mv`), so the same vector costs a different residual from each. | |
| 3371 | +fn best_new_mv_syntax(stack: &MvStack, mv: (i32, i32)) -> Option<(f64, u8)> { | |
| 3372 | + let mut best: Option<(f64, u8)> = None; | |
| 3373 | + for idx in 0..3usize { | |
| 3374 | + if idx > 0 && stack.entries.len() <= idx { | |
| 3375 | + break; | |
| 3376 | + } | |
| 3377 | + let base = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); | |
| 3378 | + if let Some(b) = mv_residual_bits(mv, base) { | |
| 3379 | + let total = b + drl_bits_for(stack, 0, idx); | |
| 3380 | + if best.is_none_or(|(t, _)| total < t) { | |
| 3381 | + best = Some((total, idx as u8)); | |
| 3382 | + } | |
| 3383 | + } | |
| 3384 | + } | |
| 3385 | + best | |
| 3386 | +} | |
| 3387 | + | |
| 3388 | +/// The starting points [`motion::search`] gets besides `stack.pred_mv`: the | |
| 3389 | +/// zero vector and the stack's own candidates. A neighbour block's vector is | |
| 3390 | +/// where this block's motion most often already is, which is what pays for | |
| 3391 | +/// the extra starting points (measured on the BD gate: -0.9/-0.8/-0.0 points | |
| 3392 | +/// vs libaom for 0.2 s off a 6 s ladder). | |
| 3393 | +fn mv_seeds(stack: &MvStack) -> ([(i32, i32); 4], usize) { | |
| 3394 | + let mut seeds = [(0, 0); 4]; | |
| 3395 | + let mut n = 1; | |
| 3396 | + for e in stack.entries.iter().take(3) { | |
| 3397 | + seeds[n] = e.mv; | |
| 3398 | + n += 1; | |
| 3399 | + } | |
| 3400 | + (seeds, n) | |
| 3401 | +} | |
| 3402 | + | |
| 3321 | 3403 | /// What [`crate::tile::write_mv`] (private to that module) would spend |
| 3322 | 3404 | /// coding `mv` as a residual against `pred`: the joint symbol naming which |
| 3323 | 3405 | /// components differ, then each differing component. `None` under the same |
| @@ -3611,19 +3693,7 @@ fn search_inter_block( | ||
| 3611 | 3693 | // past `start`, `1` to advance and `0` to stop, at most two. A `target` |
| 3612 | 3694 | // the loop cannot reach is never offered below -- every candidate's |
| 3613 | 3695 | // index is one this same loop walks to. |
| 3614 | - let drl_bits = |start: usize, target: usize| { | |
| 3615 | - let mut bits = 0.0; | |
| 3616 | - let mut idx = start; | |
| 3617 | - while idx < start + 2 && stack.entries.len() > idx + 1 { | |
| 3618 | - let advance = idx < target; | |
| 3619 | - bits += symbol_bits(&cdf::DRL_MODE[stack.drl_ctx[idx]], usize::from(advance)); | |
| 3620 | - if !advance { | |
| 3621 | - break; | |
| 3622 | - } | |
| 3623 | - idx += 1; | |
| 3624 | - } | |
| 3625 | - bits | |
| 3626 | - }; | |
| 3696 | + let drl_bits = |start: usize, target: usize| drl_bits_for(stack, start, target); | |
| 3627 | 3697 | let last_ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME); |
| 3628 | 3698 | let mut cands: Vec<((i32, i32), f64, InterInfo)> = vec![ |
| 3629 | 3699 | ( |
| @@ -3668,6 +3738,7 @@ fn search_inter_block( | ||
| 3668 | 3738 | } |
| 3669 | 3739 | |
| 3670 | 3740 | let source_block = luma.source_block(x, y, BLOCK); |
| 3741 | + let (seeds, seed_n) = mv_seeds(stack); | |
| 3671 | 3742 | #[cfg(test)] |
| 3672 | 3743 | let t = std::time::Instant::now(); |
| 3673 | 3744 | let found = motion::search( |
| @@ -3681,6 +3752,7 @@ fn search_inter_block( | ||
| 3681 | 3752 | BLOCK, |
| 3682 | 3753 | BLOCK, |
| 3683 | 3754 | stack.pred_mv, |
| 3755 | + &seeds[..seed_n], | |
| 3684 | 3756 | search.lambda, fctx, |
| 3685 | 3757 | ); |
| 3686 | 3758 | #[cfg(test)] |
| @@ -3689,20 +3761,7 @@ fn search_inter_block( | ||
| 3689 | 3761 | // Each DRL entry is a different `PredMv` (spec 7.10.2.10 `assign_mv`), so |
| 3690 | 3762 | // the same vector costs a different residual from each: take the index |
| 3691 | 3763 | // whose residual plus DRL bits is cheapest, not entry 0 by default. |
| 3692 | - let mut best_new: Option<(f64, u8)> = None; | |
| 3693 | - for idx in 0..3usize { | |
| 3694 | - if idx > 0 && stack.entries.len() <= idx { | |
| 3695 | - break; | |
| 3696 | - } | |
| 3697 | - let base = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); | |
| 3698 | - if let Some(b) = mv_residual_bits(mv, base) { | |
| 3699 | - let total = b + drl_bits(0, idx); | |
| 3700 | - if best_new.is_none_or(|(t, _)| total < t) { | |
| 3701 | - best_new = Some((total, idx as u8)); | |
| 3702 | - } | |
| 3703 | - } | |
| 3704 | - } | |
| 3705 | - if let Some((bits, ref_mv_idx)) = best_new { | |
| 3764 | + if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(stack, mv) { | |
| 3706 | 3765 | cands.push(( |
| 3707 | 3766 | mv, |
| 3708 | 3767 | last_ref_bits + symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 0) + bits, |
| @@ -3715,7 +3774,7 @@ fn search_inter_block( | ||
| 3715 | 3774 | )); |
| 3716 | 3775 | } |
| 3717 | 3776 | |
| 3718 | - for &(ref_frame, _, gstack) in extra { | |
| 3777 | + for &(ref_frame, g, gstack) in extra { | |
| 3719 | 3778 | let g_ref_bits = single_ref_bits(ref_frame); |
| 3720 | 3779 | let g_not_new = symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 1); |
| 3721 | 3780 | cands.push(( |
| @@ -3741,6 +3800,67 @@ fn search_inter_block( | ||
| 3741 | 3800 | ref_mv_idx: 0, |
| 3742 | 3801 | }, |
| 3743 | 3802 | )); |
| 3803 | + // A motion search of this reference's own: without it, GOLDEN/ALTREF | |
| 3804 | + // can only be coded at a vector some neighbour already found, which | |
| 3805 | + // is why they carried a third of the inter blocks at their two | |
| 3806 | + // search-free modes alone. | |
| 3807 | + // The search is skipped when this reference's own NEARESTMV already | |
| 3808 | + // prices out `margin` times better than LAST's best vector does -- | |
| 3809 | + // one candidate evaluation (`motion::cost_at`) standing in for a | |
| 3810 | + // whole search of ~50. | |
| 3811 | + let margin = extra_new_skip_margin(); | |
| 3812 | + let skip_search = extra_ref_new_mv() | |
| 3813 | + && margin > 0.0 | |
| 3814 | + && motion::cost_at( | |
| 3815 | + &g.y, | |
| 3816 | + g.width, | |
| 3817 | + luma.true_width, | |
| 3818 | + luma.true_height, | |
| 3819 | + &source_block, | |
| 3820 | + x, | |
| 3821 | + y, | |
| 3822 | + BLOCK, | |
| 3823 | + BLOCK, | |
| 3824 | + gstack.nearest_mv, | |
| 3825 | + gstack.pred_mv, | |
| 3826 | + search.lambda, | |
| 3827 | + fctx, | |
| 3828 | + ) * margin | |
| 3829 | + <= found.cost; | |
| 3830 | + if extra_ref_new_mv() && !skip_search { | |
| 3831 | + let (gseeds, gn) = mv_seeds(gstack); | |
| 3832 | + #[cfg(test)] | |
| 3833 | + let t = std::time::Instant::now(); | |
| 3834 | + let gfound = motion::search( | |
| 3835 | + &g.y, | |
| 3836 | + g.width, | |
| 3837 | + luma.true_width, | |
| 3838 | + luma.true_height, | |
| 3839 | + &source_block, | |
| 3840 | + x, | |
| 3841 | + y, | |
| 3842 | + BLOCK, | |
| 3843 | + BLOCK, | |
| 3844 | + gstack.pred_mv, | |
| 3845 | + &gseeds[..gn], | |
| 3846 | + search.lambda, fctx, | |
| 3847 | + ); | |
| 3848 | + #[cfg(test)] | |
| 3849 | + stage_add(0, t.elapsed()); | |
| 3850 | + let gmv = round_to_valid_mv(gfound.mv, gstack.pred_mv); | |
| 3851 | + if let Some((bits, ref_mv_idx)) = best_new_mv_syntax(gstack, gmv) { | |
| 3852 | + cands.push(( | |
| 3853 | + gmv, | |
| 3854 | + g_ref_bits + symbol_bits(&cdf::NEW_MV[gstack.new_mv_ctx], 0) + bits, | |
| 3855 | + InterInfo { | |
| 3856 | + ref_frame, | |
| 3857 | + mode: InterMode::NewMv, | |
| 3858 | + mv: gmv, | |
| 3859 | + ref_mv_idx, | |
| 3860 | + }, | |
| 3861 | + )); | |
| 3862 | + } | |
| 3863 | + } | |
| 3744 | 3864 | } |
| 3745 | 3865 | |
| 3746 | 3866 | // One trial set per distinct (reference, vector) pair: sort, cheapest |
| @@ -7507,6 +7627,7 @@ mod tests { | ||
| 7507 | 7627 | let _ = crate::tile::take_inter_mode_hits(); |
| 7508 | 7628 | let _ = crate::tile::take_drl_hits(); |
| 7509 | 7629 | let _ = crate::tile::take_ref_hits(); |
| 7630 | + let _ = crate::motion::take_census(); | |
| 7510 | 7631 | let (ours, ours_wall) = our_ladder(&source, width, height, fctx); |
| 7511 | 7632 | // How often the inter split actually fired over this clip's four |
| 7512 | 7633 | // encodes, against the whole 32x32 that was the only outcome |
| @@ -7528,6 +7649,22 @@ mod tests { | ||
| 7528 | 7649 | 100.0 * global as f64 / modes_total as f64, |
| 7529 | 7650 | 100.0 * new as f64 / modes_total as f64, |
| 7530 | 7651 | ); |
| 7652 | + // Where the wall-owning stage actually goes: how many candidate | |
| 7653 | + // evaluations (one `mc::predict` + SAD each) a search spends, how | |
| 7654 | + // many of them are subpel, and how often the winner is the seed | |
| 7655 | + // it started from -- the census the seeded search is judged by. | |
| 7656 | + let [calls, evals, subpel, rounds, near_pred, unmoved] = crate::motion::take_census(); | |
| 7657 | + let per = |n: u64| n as f64 / calls.max(1) as f64; | |
| 7658 | + eprintln!( | |
| 7659 | + "{name}: motion search {calls} calls, {evals} candidate evals ({:.1}/call, \ | |
| 7660 | + {:.1}% of them subpel), {rounds} integer rounds ({:.2}/call); winner within \ | |
| 7661 | + +-1 pel of pred_mv {:.1}%, winner = starting seed {:.1}%", | |
| 7662 | + per(evals), | |
| 7663 | + 100.0 * subpel as f64 / evals.max(1) as f64, | |
| 7664 | + per(rounds), | |
| 7665 | + 100.0 * near_pred as f64 / calls.max(1) as f64, | |
| 7666 | + 100.0 * unmoved as f64 / calls.max(1) as f64, | |
| 7667 | + ); | |
| 7531 | 7668 | eprintln!( |
| 7532 | 7669 | "{name}: references LAST {} GOLDEN {} (LAST2 {} LAST3 {} BWD {} ALT2 {} ALT {})", |
| 7533 | 7670 | refs[0], refs[3], refs[1], refs[2], refs[4], refs[5], refs[6] |
crates/ec-av1/src/motion.rsmodified+167 -20
| @@ -55,6 +55,56 @@ const SEARCH_INITIAL_STEP_PEL: i32 = 8; | ||
| 55 | 55 | /// cost function. |
| 56 | 56 | const SEARCH_MIN_STEP_PEL: i32 = 1; |
| 57 | 57 | |
| 58 | +/// [`SEARCH_INITIAL_STEP_PEL`], swept by `EC_AV1_MV_STEP` in a test build. | |
| 59 | +/// Swept 1/2/4/8 on the BD gate with the stack seeds below in place: the | |
| 60 | +/// narrow seeded shape a predictor-seeded search is supposed to allow costs | |
| 61 | +/// far more rate than it saves wall (step 1 +145.2/+219.4/+81.1 vs libaom, | |
| 62 | +/// step 2 +134.2/+196.1/+81.1, step 4 +126.0/+161.2/+81.1 against 8's | |
| 63 | +/// +125.6/+152.1/+80.9) for 0.2 s of a 6 s ladder -- the wide log stage is | |
| 64 | +/// what finds the 2160p clip's motion, and the search is not the wall. | |
| 65 | +fn seeded_step() -> i32 { | |
| 66 | + static STEP: std::sync::OnceLock<i32> = std::sync::OnceLock::new(); | |
| 67 | + *STEP.get_or_init(|| match std::env::var("EC_AV1_MV_STEP").ok() { | |
| 68 | + Some(v) if cfg!(test) => v.parse().unwrap_or(SEARCH_INITIAL_STEP_PEL), | |
| 69 | + _ => SEARCH_INITIAL_STEP_PEL, | |
| 70 | + }) | |
| 71 | +} | |
| 72 | + | |
| 73 | +/// Whether [`search`] starts from the best of the MV-stack's own candidates | |
| 74 | +/// (libaom's shape) as well as `pred_mv`. `EC_AV1_MV_SEEDED=0` restores the | |
| 75 | +/// `pred_mv`-only start in a test build (what the lane measured both arms | |
| 76 | +/// with). | |
| 77 | +fn seeded() -> bool { | |
| 78 | + // Read once: this is on the per-block search path, and `std::env::var` | |
| 79 | + // allocates and locks. | |
| 80 | + static SEEDED: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | |
| 81 | + *SEEDED.get_or_init(|| match std::env::var("EC_AV1_MV_SEEDED").ok() { | |
| 82 | + Some(v) if cfg!(test) => v != "0", | |
| 83 | + _ => true, | |
| 84 | + }) | |
| 85 | +} | |
| 86 | + | |
| 87 | +/// How the search spends itself since the last [`take_census`]: 0 calls, | |
| 88 | +/// 1 total candidate evaluations (one `predict` + SAD each), 2 of which in | |
| 89 | +/// the two subpel stages, 3 integer-stage rounds, 4 calls whose winner is | |
| 90 | +/// within one whole sample of `pred_mv`, 5 calls whose winner is the | |
| 91 | +/// starting centre itself. A gate prints these rather than assume where the | |
| 92 | +/// wall-owning stage goes (gate-blind-to-feature). | |
| 93 | +static CENSUS: [std::sync::atomic::AtomicU64; 6] = [ | |
| 94 | + std::sync::atomic::AtomicU64::new(0), | |
| 95 | + std::sync::atomic::AtomicU64::new(0), | |
| 96 | + std::sync::atomic::AtomicU64::new(0), | |
| 97 | + std::sync::atomic::AtomicU64::new(0), | |
| 98 | + std::sync::atomic::AtomicU64::new(0), | |
| 99 | + std::sync::atomic::AtomicU64::new(0), | |
| 100 | +]; | |
| 101 | + | |
| 102 | +/// Takes (and clears) the census. | |
| 103 | +#[cfg(test)] | |
| 104 | +pub(crate) fn take_census() -> [u64; 6] { | |
| 105 | + [0, 1, 2, 3, 4, 5].map(|i| CENSUS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | |
| 106 | +} | |
| 107 | + | |
| 58 | 108 | /// The eight offsets, at `step` in each axis, a diamond/log search round |
| 59 | 109 | /// checks around its current centre (the ninth point, no offset, is the |
| 60 | 110 | /// centre itself and is already priced). |
| @@ -158,6 +208,72 @@ pub struct MotionSearch { | ||
| 158 | 208 | pub cost: f64, |
| 159 | 209 | } |
| 160 | 210 | |
| 211 | +/// One candidate's `SAD + lambda * mv_bits(mv - pred_mv)`, predicted into the | |
| 212 | +/// caller's `dst16` scratch (`block_w * block_h`) so a whole search allocates | |
| 213 | +/// once. | |
| 214 | +#[allow(clippy::too_many_arguments)] | |
| 215 | +fn cost_of_mv( | |
| 216 | + reference: &[u16], | |
| 217 | + stride: usize, | |
| 218 | + ref_width: usize, | |
| 219 | + ref_height: usize, | |
| 220 | + source: &[u8], | |
| 221 | + block_x: usize, | |
| 222 | + block_y: usize, | |
| 223 | + block_w: usize, | |
| 224 | + block_h: usize, | |
| 225 | + mv: (i32, i32), | |
| 226 | + pred_mv: (i32, i32), | |
| 227 | + lambda: f64, | |
| 228 | + dst16: &mut [u16], | |
| 229 | + fctx: &crate::decode::FrameCtx, | |
| 230 | +) -> f64 { | |
| 231 | + let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; | |
| 232 | + let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; | |
| 233 | + predict( | |
| 234 | + reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, dst16, fctx, | |
| 235 | + ); | |
| 236 | + // lane-av1speed: accumulated as an integer (a block is at most | |
| 237 | + // 32x32 samples of at most 255 each, nowhere near `u32`, and an | |
| 238 | + // integer sum this small is exact in `f64` whatever order it is | |
| 239 | + // summed in) instead of one `f64` conversion and dependent add per | |
| 240 | + // sample. Same value, several times the throughput. | |
| 241 | + let sad: u32 = source | |
| 242 | + .iter() | |
| 243 | + .zip(dst16.iter()) | |
| 244 | + .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs()) | |
| 245 | + .sum(); | |
| 246 | + let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1); | |
| 247 | + f64::from(sad) + lambda * mv_bits(diff) | |
| 248 | +} | |
| 249 | + | |
| 250 | +/// [`cost_of_mv`] for one vector, on its own scratch -- what a caller uses to | |
| 251 | +/// price a single candidate (an extra reference's `NEARESTMV`, say) on the | |
| 252 | +/// same scale [`search`] reports its own cost on, for one evaluation instead | |
| 253 | +/// of a whole search. | |
| 254 | +#[allow(clippy::too_many_arguments)] | |
| 255 | +pub(crate) fn cost_at( | |
| 256 | + reference: &[u16], | |
| 257 | + stride: usize, | |
| 258 | + ref_width: usize, | |
| 259 | + ref_height: usize, | |
| 260 | + source: &[u8], | |
| 261 | + block_x: usize, | |
| 262 | + block_y: usize, | |
| 263 | + block_w: usize, | |
| 264 | + block_h: usize, | |
| 265 | + mv: (i32, i32), | |
| 266 | + pred_mv: (i32, i32), | |
| 267 | + lambda: f64, | |
| 268 | + fctx: &crate::decode::FrameCtx, | |
| 269 | +) -> f64 { | |
| 270 | + let mut dst16 = vec![0u16; block_w * block_h]; | |
| 271 | + cost_of_mv( | |
| 272 | + reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, mv, | |
| 273 | + pred_mv, lambda, &mut dst16, fctx, | |
| 274 | + ) | |
| 275 | +} | |
| 276 | + | |
| 161 | 277 | /// Searches one `block_w * block_h` block, at `(block_x, block_y)` in the |
| 162 | 278 | /// current picture's plane, against `reference` for the motion vector that |
| 163 | 279 | /// minimises `SAD + lambda * mv_bits(mv - pred_mv)`. |
| @@ -200,11 +316,14 @@ pub(crate) fn search( | ||
| 200 | 316 | block_w: usize, |
| 201 | 317 | block_h: usize, |
| 202 | 318 | pred_mv: (i32, i32), |
| 319 | + seeds: &[(i32, i32)], | |
| 203 | 320 | lambda: f64, fctx: &crate::decode::FrameCtx, |
| 204 | 321 | ) -> MotionSearch { |
| 205 | 322 | let (result, _trace) = search_traced( |
| 206 | 323 | reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, |
| 207 | - pred_mv, lambda, fctx, | |
| 324 | + pred_mv, | |
| 325 | + if seeded() { seeds } else { &[] }, | |
| 326 | + lambda, fctx, | |
| 208 | 327 | ); |
| 209 | 328 | result |
| 210 | 329 | } |
| @@ -224,6 +343,7 @@ fn search_traced( | ||
| 224 | 343 | block_w: usize, |
| 225 | 344 | block_h: usize, |
| 226 | 345 | pred_mv: (i32, i32), |
| 346 | + seeds: &[(i32, i32)], | |
| 227 | 347 | lambda: f64, fctx: &crate::decode::FrameCtx, |
| 228 | 348 | ) -> (MotionSearch, Vec<f64>) { |
| 229 | 349 | search_traced_from_step( |
| @@ -237,8 +357,13 @@ fn search_traced( | ||
| 237 | 357 | block_w, |
| 238 | 358 | block_h, |
| 239 | 359 | pred_mv, |
| 360 | + seeds, | |
| 240 | 361 | lambda, |
| 241 | - SEARCH_INITIAL_STEP_PEL, fctx, | |
| 362 | + if seeded() { | |
| 363 | + seeded_step() | |
| 364 | + } else { | |
| 365 | + SEARCH_INITIAL_STEP_PEL | |
| 366 | + }, fctx, | |
| 242 | 367 | ) |
| 243 | 368 | } |
| 244 | 369 | |
| @@ -257,6 +382,7 @@ fn search_traced_from_step( | ||
| 257 | 382 | block_w: usize, |
| 258 | 383 | block_h: usize, |
| 259 | 384 | pred_mv: (i32, i32), |
| 385 | + seeds: &[(i32, i32)], | |
| 260 | 386 | lambda: f64, |
| 261 | 387 | initial_step_pel: i32, fctx: &crate::decode::FrameCtx, |
| 262 | 388 | ) -> (MotionSearch, Vec<f64>) { |
| @@ -269,30 +395,34 @@ fn search_traced_from_step( | ||
| 269 | 395 | // allocate-and-convert per searched block, which profiled as the single |
| 270 | 396 | // largest symbol in the encoder (30% of `sequence_bench_sanity`). |
| 271 | 397 | let mut dst16 = vec![0u16; block_w * block_h]; |
| 398 | + let evals = std::cell::Cell::new(0u64); | |
| 272 | 399 | let mut cost_of = |mv: (i32, i32)| -> f64 { |
| 273 | - let x_q4 = (block_x as i32) * 16 + mv.1 * Q4_PER_Q3; | |
| 274 | - let y_q4 = (block_y as i32) * 16 + mv.0 * Q4_PER_Q3; | |
| 275 | - predict( | |
| 276 | - reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, | |
| 277 | - &mut dst16, fctx, | |
| 278 | - ); | |
| 279 | - // lane-av1speed: accumulated as an integer (a block is at most | |
| 280 | - // 32x32 samples of at most 255 each, nowhere near `u32`, and an | |
| 281 | - // integer sum this small is exact in `f64` whatever order it is | |
| 282 | - // summed in) instead of one `f64` conversion and dependent add per | |
| 283 | - // sample. Same value, several times the throughput. | |
| 284 | - let sad: u32 = source | |
| 285 | - .iter() | |
| 286 | - .zip(dst16.iter()) | |
| 287 | - .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs()) | |
| 288 | - .sum(); | |
| 289 | - let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1); | |
| 290 | - f64::from(sad) + lambda * mv_bits(diff) | |
| 400 | + evals.set(evals.get() + 1); | |
| 401 | + cost_of_mv( | |
| 402 | + reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, | |
| 403 | + mv, pred_mv, lambda, &mut dst16, fctx, | |
| 404 | + ) | |
| 291 | 405 | }; |
| 292 | 406 | |
| 293 | 407 | let mut trace = Vec::new(); |
| 294 | 408 | let mut centre = (round_to_pel(pred_mv.0), round_to_pel(pred_mv.1)); |
| 295 | 409 | let mut best_cost = cost_of(centre); |
| 410 | + // The stack's own candidates, whole-pel rounded, as extra starting | |
| 411 | + // points: a neighbour block's vector is where this block's motion most | |
| 412 | + // often already is, and starting there is what lets the log stage below | |
| 413 | + // start from is usually the vector this block wants. | |
| 414 | + for &seed in seeds { | |
| 415 | + let candidate = (round_to_pel(seed.0), round_to_pel(seed.1)); | |
| 416 | + if candidate == centre { | |
| 417 | + continue; | |
| 418 | + } | |
| 419 | + let cost = cost_of(candidate); | |
| 420 | + if cost < best_cost { | |
| 421 | + best_cost = cost; | |
| 422 | + centre = candidate; | |
| 423 | + } | |
| 424 | + } | |
| 425 | + let start_centre = centre; | |
| 296 | 426 | trace.push(best_cost); |
| 297 | 427 | |
| 298 | 428 | // Stage 1: log/diamond search over whole-pel steps, halving the step |
| @@ -317,6 +447,7 @@ fn search_traced_from_step( | ||
| 317 | 447 | } |
| 318 | 448 | let moved = best_in_round < best_cost; |
| 319 | 449 | best_cost = best_in_round; |
| 450 | + CENSUS[3].fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 320 | 451 | trace.push(best_cost); |
| 321 | 452 | if !moved { |
| 322 | 453 | break; |
| @@ -328,6 +459,7 @@ fn search_traced_from_step( | ||
| 328 | 459 | // Stages 2 and 3: one ±1-step refinement each at half-pel then |
| 329 | 460 | // quarter-pel, through the same cost function (so the refinement is |
| 330 | 461 | // costed at the precision it commits to, not the integer-pel SAD alone). |
| 462 | + let integer_evals = evals.get(); | |
| 331 | 463 | for step in [HALF_PEL_Q3, QUARTER_PEL_Q3] { |
| 332 | 464 | let round_centre = centre; |
| 333 | 465 | for (dr, dc) in neighbor_offsets(step) { |
| @@ -341,6 +473,17 @@ fn search_traced_from_step( | ||
| 341 | 473 | trace.push(best_cost); |
| 342 | 474 | } |
| 343 | 475 | |
| 476 | + use std::sync::atomic::Ordering::Relaxed; | |
| 477 | + CENSUS[0].fetch_add(1, Relaxed); | |
| 478 | + CENSUS[1].fetch_add(evals.get(), Relaxed); | |
| 479 | + CENSUS[2].fetch_add(evals.get() - integer_evals, Relaxed); | |
| 480 | + if (centre.0 - pred_mv.0).abs() <= PEL_Q3 && (centre.1 - pred_mv.1).abs() <= PEL_Q3 { | |
| 481 | + CENSUS[4].fetch_add(1, Relaxed); | |
| 482 | + } | |
| 483 | + if centre == start_centre { | |
| 484 | + CENSUS[5].fetch_add(1, Relaxed); | |
| 485 | + } | |
| 486 | + | |
| 344 | 487 | ( |
| 345 | 488 | MotionSearch { |
| 346 | 489 | mv: centre, |
| @@ -449,6 +592,7 @@ mod tests { | ||
| 449 | 592 | block_w, |
| 450 | 593 | block_h, |
| 451 | 594 | (0, 0), |
| 595 | + &[], | |
| 452 | 596 | 0.1, fctx, |
| 453 | 597 | ); |
| 454 | 598 | assert_eq!( |
| @@ -498,6 +642,7 @@ mod tests { | ||
| 498 | 642 | block_w, |
| 499 | 643 | block_h, |
| 500 | 644 | (0, 0), |
| 645 | + &[], | |
| 501 | 646 | 0.1, fctx, |
| 502 | 647 | ); |
| 503 | 648 | assert_eq!( |
| @@ -536,6 +681,7 @@ mod tests { | ||
| 536 | 681 | block_w, |
| 537 | 682 | block_h, |
| 538 | 683 | (0, 0), |
| 684 | + &[], | |
| 539 | 685 | 0.1, fctx, |
| 540 | 686 | ); |
| 541 | 687 | assert!(trace.len() > 1, "the search must run more than one round"); |
| @@ -605,6 +751,7 @@ mod tests { | ||
| 605 | 751 | block_w, |
| 606 | 752 | block_h, |
| 607 | 753 | (0, 0), |
| 754 | + &[], | |
| 608 | 755 | 0.1, |
| 609 | 756 | candidate_step, fctx, |
| 610 | 757 | ); |