diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 28a64805..5635704f 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -2582,6 +2582,7 @@ fn code_square_inter( stack.pred_mv, &seeds[..seed_n], search.lambda, fctx, + 1, ); let new_mv = round_to_valid_mv(found.mv, stack.pred_mv); 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) { /// coding `mv` as a residual against `pred`: the joint symbol naming which /// components differ, then each differing component. `None` under the same /// condition [`mv_component_bits`] returns `None`. +/// How many frames back `ref_frame` sits from the frame being coded, off the +/// order hints [`crate::tile::arm_order_hints`] armed for it. `LAST_FRAME` is +/// 1 in this encoder's GOP; `GOLDEN`/`ALTREF` can be much further, which is +/// what [`crate::motion::search`] scales its starting step by. +fn ref_distance(ref_frame: i8) -> u32 { + let (bits, order_hint, hints) = crate::tile::order_hints(); + let i = (ref_frame - crate::mvstack::LAST_FRAME).clamp(0, 6) as usize; + let d = crate::motion_field::get_relative_dist(bits, order_hint, hints[i]) + .unsigned_abs() + .max(1); + if crate::envflags::env_flag!("EC_TRACE_DIST") { + eprintln!("EC_DIST ref={ref_frame} bits={bits} oh={order_hint} hint={} d={d}", hints[i]); + } + d +} + fn mv_residual_bits(mv: (i32, i32), pred: (i32, i32)) -> Option { let diff = (mv.0 - pred.0, mv.1 - pred.1); let joint = match (diff.0 != 0, diff.1 != 0) { @@ -4215,6 +4232,7 @@ fn search_inter_block( stack.pred_mv, &seeds[..seed_n], search.lambda, fctx, + 1, ); #[cfg(test)] stage_add(0, t.elapsed()); @@ -4314,6 +4332,7 @@ fn search_inter_block( gstack.pred_mv, &gseeds[..gn], search.lambda, fctx, + ref_distance(ref_frame), ); #[cfg(test)] stage_add(0, t.elapsed()); @@ -4762,6 +4781,12 @@ pub(crate) fn encode_inter_frame( // `frame::write_frame_header`'s `skipModeAllowed` reads this. header.order_hints = order_hints; header.reference_select = reference_select(); + // Armed HERE, not only beside the tile write below: `ref_distance`'s own + // per-reference order-hint distance is read by the block SEARCH, which + // runs long before the writer arms these (the distance-scaled search step + // read `(0, 0, [0; 7])` and came out 1 for every reference -- the knob + // never reached the tool). + crate::tile::arm_order_hints(seq.order_hint_bits, order_hint, order_hints); if let Some(p) = pyramid { header.show_frame = p.show_frame; header.showable_frame = !p.show_frame; @@ -8371,7 +8396,7 @@ mod tests { }) }; let pins: [(u8, usize, u64); 2] = - [(150, 7166, 0xc608_4e2a_071e_1d72), (60, 26950, 0x019f_a19c_251b_f61d)]; + [(150, 7103, 0xb9b7_763b_6f18_a887), (60, 26778, 0x4071_f16e_1e5d_87c3)]; for (q, bytes, hash) in pins { let encoded = encode_sequence(&source, q, 0.5).unwrap(); assert_eq!( diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index d6314902..d352856a 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -318,19 +318,54 @@ pub(crate) fn search( pred_mv: (i32, i32), seeds: &[(i32, i32)], lambda: f64, fctx: &crate::decode::FrameCtx, + // How many frames back (order-hint distance) this reference sits: a + // reference twice as far away holds content that has moved about twice + // as far, so the log stage starts from a proportionally wider step. See + // [`dist_initial_step`]. + dist: u32, ) -> MotionSearch { - let (result, _trace) = search_traced( + let (result, _trace) = search_traced_from_step( reference, stride, ref_width, ref_height, source, block_x, block_y, block_w, block_h, pred_mv, if seeded() { seeds } else { &[] }, - lambda, fctx, + lambda, + dist_initial_step(dist), fctx, ); result } +/// The integer-pel log stage's starting step for a reference at order-hint +/// distance `dist`: `step * (1 + k * (dist - 1))`, `k` = `EC_AV1_MV_DIST_SCALE` +/// in a test build. `k = 0` is the flat step every reference used before. +fn dist_initial_step(dist: u32) -> i32 { + let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL }; + static K: std::sync::OnceLock = std::sync::OnceLock::new(); + let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() { + Some(v) if cfg!(test) => v.parse().unwrap_or(MV_DIST_SCALE), + _ => MV_DIST_SCALE, + }); + if dist <= 1 || k <= 0.0 { + return base; + } + let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0); + // Past 4x the base the log stage spends its rounds on vectors no + // reference in this encoder's GOP reaches. + scaled.round().clamp(f64::from(base), f64::from(base * 4)) as i32 +} + +/// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate +/// (vs libaom, 1080p/2160p/screen): 0 = +100.7/+115.8/+73.3, 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 -- screen is byte-identical at every +/// point (its motion is small enough that a wider start converges to the same +/// vector). 0.5 wins both film clips together; the ranking either side of it +/// is near-tie churn, not a gradient. +const MV_DIST_SCALE: f64 = 0.5; + /// [`search`], plus the running best cost after every round any of its three /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below /// checks. +#[cfg(test)] #[allow(clippy::too_many_arguments)] // one reference plane, one block, one predictor fn search_traced( reference: &[u16], @@ -594,6 +629,7 @@ mod tests { (0, 0), &[], 0.1, fctx, + 1, ); assert_eq!( result.mv, @@ -644,6 +680,7 @@ mod tests { (0, 0), &[], 0.1, fctx, + 1, ); assert_eq!( result.mv, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 55ff59a1..9d83df5c 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -207,6 +207,12 @@ pub(crate) fn arm_order_hints(bits: u32, order_hint: u32, hints: [u32; 7]) { ORDER_HINTS.with(|c| c.set((bits, order_hint, hints))); } +/// What [`arm_order_hints`] last armed: `(order_hint_bits, order_hint, +/// ref_order_hints)`, for the encoder's own per-reference distance. +pub(crate) fn order_hints() -> (u32, u32, [u32; 7]) { + ORDER_HINTS.with(std::cell::Cell::get) +} + /// Writer-side counterpart of decode.rs `read_compound_ref_frames` (spec /// 5.11.25's `comp_reference_type`/`uni_comp_ref`/`comp_ref`/`comp_bwdref` /// trees), symbol for symbol at the same contexts -- which are, as that