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(
2582 stack.pred_mv,2582 stack.pred_mv,
2583 &seeds[..seed_n],2583 &seeds[..seed_n],
2584 search.lambda, fctx,2584 search.lambda, fctx,
2585+ 1,
2585 );2586 );
2586 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);2587 let new_mv = round_to_valid_mv(found.mv, stack.pred_mv);
2587 if let Some(mv_bits) = mv_residual_bits(new_mv, stack.pred_mv) {2588 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) {
3803 /// coding `mv` as a residual against `pred`: the joint symbol naming which3804 /// coding `mv` as a residual against `pred`: the joint symbol naming which
3804 /// components differ, then each differing component. `None` under the same3805 /// components differ, then each differing component. `None` under the same
3805 /// condition [`mv_component_bits`] returns `None`.3806 /// 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+
3806 fn mv_residual_bits(mv: (i32, i32), pred: (i32, i32)) -> Option<f64> {3823 fn mv_residual_bits(mv: (i32, i32), pred: (i32, i32)) -> Option<f64> {
3807 let diff = (mv.0 - pred.0, mv.1 - pred.1);3824 let diff = (mv.0 - pred.0, mv.1 - pred.1);
3808 let joint = match (diff.0 != 0, diff.1 != 0) {3825 let joint = match (diff.0 != 0, diff.1 != 0) {
@@ -4215,6 +4232,7 @@ fn search_inter_block(
4215 stack.pred_mv,4232 stack.pred_mv,
4216 &seeds[..seed_n],4233 &seeds[..seed_n],
4217 search.lambda, fctx,4234 search.lambda, fctx,
4235+ 1,
4218 );4236 );
4219 #[cfg(test)]4237 #[cfg(test)]
4220 stage_add(0, t.elapsed());4238 stage_add(0, t.elapsed());
@@ -4314,6 +4332,7 @@ fn search_inter_block(
4314 gstack.pred_mv,4332 gstack.pred_mv,
4315 &gseeds[..gn],4333 &gseeds[..gn],
4316 search.lambda, fctx,4334 search.lambda, fctx,
4335+ ref_distance(ref_frame),
4317 );4336 );
4318 #[cfg(test)]4337 #[cfg(test)]
4319 stage_add(0, t.elapsed());4338 stage_add(0, t.elapsed());
@@ -4762,6 +4781,12 @@ pub(crate) fn encode_inter_frame(
4762 // `frame::write_frame_header`'s `skipModeAllowed` reads this.4781 // `frame::write_frame_header`'s `skipModeAllowed` reads this.
4763 header.order_hints = order_hints;4782 header.order_hints = order_hints;
4764 header.reference_select = reference_select();4783 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);
4765 if let Some(p) = pyramid {4790 if let Some(p) = pyramid {
4766 header.show_frame = p.show_frame;4791 header.show_frame = p.show_frame;
4767 header.showable_frame = !p.show_frame;4792 header.showable_frame = !p.show_frame;
@@ -8371,7 +8396,7 @@ mod tests {
8371 })8396 })
8372 };8397 };
8373 let pins: [(u8, usize, u64); 2] =8398 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)];
8375 for (q, bytes, hash) in pins {8400 for (q, bytes, hash) in pins {
8376 let encoded = encode_sequence(&source, q, 0.5).unwrap();8401 let encoded = encode_sequence(&source, q, 0.5).unwrap();
8377 assert_eq!(8402 assert_eq!(
crates/ec-av1/src/motion.rsmodified+39 -2
@@ -318,19 +318,54 @@ pub(crate) fn search(
318 pred_mv: (i32, i32),318 pred_mv: (i32, i32),
319 seeds: &[(i32, i32)],319 seeds: &[(i32, i32)],
320 lambda: f64, fctx: &crate::decode::FrameCtx,320 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,
321 ) -> MotionSearch {326 ) -> MotionSearch {
322- let (result, _trace) = search_traced(327+ let (result, _trace) = search_traced_from_step(
323 reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h,328 reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h,
324 pred_mv,329 pred_mv,
325 if seeded() { seeds } else { &[] },330 if seeded() { seeds } else { &[] },
326- lambda, fctx,331+ lambda,
332+ dist_initial_step(dist), fctx,
327 );333 );
328 result334 result
329 }335 }
330 336
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+
331 /// [`search`], plus the running best cost after every round any of its three365 /// [`search`], plus the running best cost after every round any of its three
332 /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below366 /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below
333 /// checks.367 /// checks.
368+#[cfg(test)]
334 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor369 #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor
335 fn search_traced(370 fn search_traced(
336 reference: &[u16],371 reference: &[u16],
@@ -594,6 +629,7 @@ mod tests {
594 (0, 0),629 (0, 0),
595 &[],630 &[],
596 0.1, fctx,631 0.1, fctx,
632+ 1,
597 );633 );
598 assert_eq!(634 assert_eq!(
599 result.mv,635 result.mv,
@@ -644,6 +680,7 @@ mod tests {
644 (0, 0),680 (0, 0),
645 &[],681 &[],
646 0.1, fctx,682 0.1, fctx,
683+ 1,
647 );684 );
648 assert_eq!(685 assert_eq!(
649 result.mv,686 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]) {
207 ORDER_HINTS.with(|c| c.set((bits, order_hint, hints)));207 ORDER_HINTS.with(|c| c.set((bits, order_hint, hints)));
208 }208 }
209 209
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+
210 /// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec216 /// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec
211 /// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref`217 /// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref`
212 /// trees), symbol for symbol at the same contexts -- which are, as that218 /// trees), symbol for symbol at the same contexts -- which are, as that