forge
Explore
Log in

Don't you dare go Hollow

The motion search starts from a step scaled by the reference's order-hint distance

Browse files at this commit

d53c8524afd75c78fa0c1b968850b75ae8295e1aAhmet Kaan GümüşParent: 757ff82
  • feat
    • the motion search starts from a step scaled by the reference's order-hint distance
Show full message
feat(av1): the motion search starts from a step scaled by the reference's order-hint distance

A reference twice as far back holds content that has moved about twice as
far, so motion::search now takes that reference's own order-hint distance and
opens its log stage at step * (1 + 0.5 * (dist - 1)), capped at 4x the flat
step. LAST stays at the flat step; GOLDEN (the key frame, 2..12 frames back
in this GOP) and ALTREF (2) widen.

The hints had to be armed before the search: tile::arm_order_hints ran beside
the tile WRITE, so ref_distance read (0, 0, [0; 7]) and returned 1 for every
reference -- the first sweep came out byte-identical at every scale (the knob
never reached the tool). Armed in encode_inter_frame's header setup, the
traced distances are ALTREF 2 and GOLDEN 2..12.

Sweep vs libaom (1080p/2160p, screen byte-identical at every point): 0
+100.7/+115.8, 0.25 +98.5/+112.2, 0.375 +98.2/+115.6, 0.5 +94.4/+104.3, 0.625
+95.9/+106.5, 0.75 +92.8/+112.7, 1.0 +98.8/+113.0. Ships at 0.5 (-6.3/-11.5
/flat, vs rav1e +47.5/+63.6/+13.8); wall 5.7s/5.1s/5.8s, unchanged.

Byte pins move with the streams.
crates/ec-av1/src/encode.rsmodified+26 -1
@@ -2582,6 +2582,7 @@ fn code_square_inter(
25822582 stack.pred_mv,
25832583 &seeds[..seed_n],
25842584 search.lambda, fctx,
2585+ 1,
25852586 );
25862587 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);
25872588 if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) {
@@ -3803,6 +3804,22 @@ fn mv_seeds(stack: &MvStack) -> ([(i32, i32); 4], usize) {
38033804 /// coding `mv` as a residual against `pred`: the joint symbol naming which
38043805 /// components differ, then each differing component. `None` under the same
38053806 /// condition [`mv_component_bits`] returns `None`.
3807+/// How many frames back `ref_frame` sits from the frame being coded, off the
3808+/// order hints [`crate::tile::arm_order_hints`] armed for it. `LAST_FRAME` is
3809+/// 1 in this encoder's GOP; `GOLDEN`/`ALTREF` can be much further, which is
3810+/// what [`crate::motion::search`] scales its starting step by.
3811+fn ref_distance(ref_frame: i8) -> u32 {
3812+ let (bits, order_hint, hints) = crate::tile::order_hints();
3813+ let i = (ref_frame - crate::mvstack::LAST_FRAME).clamp(0, 6) as usize;
3814+ let d = crate::motion_field::get_relative_dist(bits, order_hint, hints[i])
3815+ .unsigned_abs()
3816+ .max(1);
3817+ if crate::envflags::env_flag!("EC_TRACE_DIST") {
3818+ eprintln!("EC_DIST ref={ref_frame} bits={bits} oh={order_hint} hint={} d={d}", hints[i]);
3819+ }
3820+ d
3821+}
3822+
38063823 fn mv_residual_bits(mv: (i32, i32), pred: (i32, i32)) -> Option<f64> {
38073824 let diff = (mv.0 - pred.0, mv.1 - pred.1);
38083825 let joint = match (diff.0 != 0, diff.1 != 0) {
@@ -4215,6 +4232,7 @@ fn search_inter_block(
42154232 stack.pred_mv,
42164233 &seeds[..seed_n],
42174234 search.lambda, fctx,
4235+ 1,
42184236 );
42194237 #[cfg(test)]
42204238 stage_add(0, t.elapsed());
@@ -4314,6 +4332,7 @@ fn search_inter_block(
43144332 gstack.pred_mv,
43154333 &gseeds[..gn],
43164334 search.lambda, fctx,
4335+ ref_distance(ref_frame),
43174336 );
43184337 #[cfg(test)]
43194338 stage_add(0, t.elapsed());
@@ -4762,6 +4781,12 @@ pub(crate) fn encode_inter_frame(
47624781 // `frame::write_frame_header`'s `skipModeAllowed` reads this.
47634782 header.order_hints = order_hints;
47644783 header.reference_select = reference_select();
4784+ // Armed HERE, not only beside the tile write below: `ref_distance`'s own
4785+ // per-reference order-hint distance is read by the block SEARCH, which
4786+ // runs long before the writer arms these (the distance-scaled search step
4787+ // read `(0, 0, [0; 7])` and came out 1 for every reference -- the knob
4788+ // never reached the tool).
4789+ crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints);
47654790 if let Some(p) = pyramid {
47664791 header.show_frame = p.show_frame;
47674792 header.showable_frame = !p.show_frame;
@@ -8371,7 +8396,7 @@ mod tests {
83718396 })
83728397 };
83738398 let pins: [(u8, usize, u64); 2] =
8374- [(150, 7166, 0xc608_4e2a_071e_1d72), (60, 26950, 0x019f_a19c_251b_f61d)];
8399+ [(150, 7103, 0xb9b7_763b_6f18_a887), (60, 26778, 0x4071_f16e_1e5d_87c3)];
83758400 for (q, bytes, hash) in pins {
83768401 let encoded = encode_sequence(&source, q, 0.5).unwrap();
83778402 assert_eq!(
crates/ec-av1/src/motion.rsmodified+39 -2
@@ -318,19 +318,54 @@ pub(crate) fn search(
318318 pred_mv: (i32, i32),
319319 seeds: &[(i32, i32)],
320320 lambda: f64, fctx: &crate::decode::FrameCtx,
321+ // How many frames back (order-hint distance) this reference sits: a
322+ // reference twice as far away holds content that has moved about twice
323+ // as far, so the log stage starts from a proportionally wider step. See
324+ // [`dist_initial_step`].
325+ dist: u32,
321326 ) -> MotionSearch {
322- let (result, _trace) = search_traced(
327+ let (result, _trace) = search_traced_from_step(
323328 reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h,
324329 pred_mv,
325330 if seeded() { seeds } else { &[] },
326- lambda, fctx,
331+ lambda,
332+ dist_initial_step(dist), fctx,
327333 );
328334 result
329335 }
330336
337+/// The integer-pel log stage's starting step for a reference at order-hint
338+/// distance `dist`: `step * (1 + k * (dist - 1))`, `k` = `EC_AV1_MV_DIST_SCALE`
339+/// in a test build. `k = 0` is the flat step every reference used before.
340+fn dist_initial_step(dist: u32) -> i32 {
341+ let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL };
342+ static K: std::sync::OnceLock<f64> = std::sync::OnceLock::new();
343+ let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() {
344+ Some(v) if cfg!(test) => v.parse().unwrap_or(MV_DIST_SCALE),
345+ _ => MV_DIST_SCALE,
346+ });
347+ if dist <= 1 || k <= 0.0 {
348+ return base;
349+ }
350+ let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0);
351+ // Past 4x the base the log stage spends its rounds on vectors no
352+ // reference in this encoder's GOP reaches.
353+ scaled.round().clamp(f64::from(base), f64::from(base * 4)) as i32
354+}
355+
356+/// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate
357+/// (vs libaom, 1080p/2160p/screen): 0 = +100.7/+115.8/+73.3, 0.25
358+/// +98.5/+112.2, 0.375 +98.2/+115.6, 0.5 +94.4/+104.3, 0.625 +95.9/+106.5,
359+/// 0.75 +92.8/+112.7, 1.0 +98.8/+113.0 -- screen is byte-identical at every
360+/// point (its motion is small enough that a wider start converges to the same
361+/// vector). 0.5 wins both film clips together; the ranking either side of it
362+/// is near-tie churn, not a gradient.
363+const MV_DIST_SCALE: f64 = 0.5;
364+
331365 /// [`search`], plus the running best cost after every round any of its three
332366 /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below
333367 /// checks.
368+#[cfg(test)]
334369 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
335370 fn search_traced(
336371 reference: &[u16],
@@ -594,6 +629,7 @@ mod tests {
594629 (0, 0),
595630 &[],
596631 0.1, fctx,
632+ 1,
597633 );
598634 assert_eq!(
599635 result.mv,
@@ -644,6 +680,7 @@ mod tests {
644680 (0, 0),
645681 &[],
646682 0.1, fctx,
683+ 1,
647684 );
648685 assert_eq!(
649686 result.mv,
crates/ec-av1/src/tile.rsmodified+6 -0
@@ -207,6 +207,12 @@ pub(crate) fn arm_order_hints(bits: u32, order_hint: u32, hints: [u32; 7]) {
207207 ORDER_HINTS.with(|c| c.set((bits, order_hint, hints)));
208208 }
209209
210+/// What [`arm_order_hints`] last armed: `(order_hint_bits, order_hint,
211+/// ref_order_hints)`, for the encoder's own per-reference distance.
212+pub(crate) fn order_hints() -> (u32, u32, [u32; 7]) {
213+ ORDER_HINTS.with(std::cell::Cell::get)
214+}
215+
210216 /// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec
211217 /// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref`
212218 /// trees), symbol for symbol at the same contexts -- which are, as that