forge
Explore
Log in

Don't you dare go Hollow

The saved motion field builds off the frame thread, one other-side table per frame

Browse files at this commit

259f3b77dc24a8ae5358ef732c93115d46a0046fAhmet Kaan GümüşParent: 5b55d04
  • perf
    • the saved motion field builds off the frame thread, one other-side table per frame
Show full message
perf(av1): the saved motion field builds off the frame thread, one other-side table per frame

`build_motion_field` (libaom `av1_copy_frame_mvs`) was a serial whole-frame
4x4-unit pass on the frame thread that re-evaluated the `other_side`
order-hint comparison for every reference slot of every inter unit.

Two changes, both output-identical:

* the per-reference `other_side` answer is a `[bool; 8]` computed once per
  frame instead of two `get_relative_dist` calls per inter unit;
* the pass is split into cell-row bands over the filter pool with the
  existing `par::bands`/`Shared`/`run_bands` pattern. An 8x8 motion-field
  cell lives entirely in one cell row, so a band owns `cells[r0 * cols..r1 *
  cols]` and no two bands ever touch the same cell -- the write order inside
  a band is unchanged, so the "last qualifying slot wins" and
  "clear-then-maybe-write" semantics are preserved exactly.

The field is consumed only by LATER frames, never by this frame's entropy
decode; it is published as part of `SlotValue` at `SlotPromise::fulfil`, so it
cannot be deferred past the fulfil without splitting that publication.

seg4k, 4dc14f52 -> here: instructions:u 412.358 -> 410.565 G at 1 thread
(-0.44%, the hoist alone -- bands collapse to 1 at FILTER_THREADS=1) and
421.45 -> 419.13 G at (16,4,2) (-0.55%). All 12 gate hashes unchanged at 1, 4
and 16 threads.
crates/ec-av1/src/decode.rsmodified+67 -41
@@ -4730,6 +4730,7 @@ pub(crate) struct TplFrameArgs<'a> {
47304730 /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is
47314731 /// exact — every block this crate ever codes is at least 8x8, so no 8x8
47324732 /// cell straddles two different blocks' values.
4733+#[allow(unsafe_code)] // lane-mf: `par::Shared` band split, same corner-cut as the filter stages
47334734 pub(crate) fn build_motion_field(
47344735 grid: &MiGrid,
47354736 mi_cols: usize,
@@ -4737,6 +4738,7 @@ pub(crate) fn build_motion_field(
47374738 order_hint: u32,
47384739 order_hint_bits: u32,
47394740 ref_order_hints: [u32; 7],
4741+ fctx: &FrameCtx,
47404742 ) -> crate::motion_field::MotionField {
47414743 let mut field =
47424744 crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints, false);
@@ -4746,57 +4748,80 @@ pub(crate) fn build_motion_field(
47464748 // referencing one stores only its OTHER side's mv. With
47474749 // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and
47484750 // nothing is skipped.
4749- let other_side = |rf: i8| -> bool {
4750- if order_hint_bits == 0 {
4751- return false;
4752- }
4753- let oh = ref_order_hints[(rf - LAST_FRAME) as usize];
4754- crate::motion_field::get_relative_dist(order_hint_bits, oh, order_hint) > 0
4755- || oh == order_hint
4756- };
4751+ // lane-mf: hoisted out of the per-4x4-unit loop -- the answer depends
4752+ // only on the reference frame, so it is 7 table lookups per frame instead
4753+ // of two per inter unit.
4754+ let other_side: [bool; 8] = std::array::from_fn(|rf| {
4755+ let i = rf.wrapping_sub(LAST_FRAME as usize);
4756+ order_hint_bits != 0
4757+ && i < 7
4758+ && (crate::motion_field::get_relative_dist(order_hint_bits, ref_order_hints[i], order_hint) > 0
4759+ || ref_order_hints[i] == order_hint)
4760+ });
47574761 // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is
47584762 // capped at 12 bits per component; a longer one is not stored at all.
47594763 const REFMVS_LIMIT: i32 = (1 << 12) - 1;
4760- for row in 0..mi_rows {
4761- for col in 0..mi_cols {
4762- let Some(info) = grid.get_frame(row, col) else {
4763- continue;
4764- };
4765- // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and
4766- // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`),
4767- // so an intra block -- or an inter block whose every slot is on the
4768- // other side / over the limit -- wipes what an earlier block wrote
4769- // into the same 8x8 cell (only reachable from sub-8x8 leaves, which
4770- // share a cell).
4771- if !info.is_inter {
4772- field.set(row, col, None);
4773- continue;
4774- }
4775- // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST
4776- // qualifying one wins -- a compound block stores its second
4777- // reference's mv, never its first (unless the second is on the
4778- // other side or over the limit). Storing slot 0 unconditionally
4779- // put the wrong (mv, ref_frame_offset) pair in every compound
4780- // block's cell, which a later frame's temporal candidate then
4781- // projected with the wrong reference distance.
4782- let mut saved: Option<crate::motion_field::SavedMv> = None;
4783- for (rf, mv) in [
4784- (info.ref_frame, Some(info.mv)),
4785- (info.ref_frame1.unwrap_or(0), info.mv1),
4786- ] {
4787- let (Some(mv), true) = (mv, rf > 0) else {
4764+ let (cols, rows, cells) = field.cells_mut();
4765+ // Fills cell rows `r0..r1`; `out` is exactly those rows' cells, so bands
4766+ // never touch each other (an 8x8 cell lives in one cell row, and mi row
4767+ // `row` writes cell row `row / 2`).
4768+ let fill = |r0: usize, r1: usize, out: &mut [Option<crate::motion_field::SavedMv>]| {
4769+ for row in (r0 * 2)..(r1 * 2).min(mi_rows) {
4770+ let out = &mut out[(row / 2 - r0) * cols..][..cols];
4771+ for col in 0..mi_cols {
4772+ let Some(info) = grid.get_frame(row, col) else {
47884773 continue;
47894774 };
4790- if other_side(rf) {
4775+ // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and
4776+ // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`),
4777+ // so an intra block -- or an inter block whose every slot is on the
4778+ // other side / over the limit -- wipes what an earlier block wrote
4779+ // into the same 8x8 cell (only reachable from sub-8x8 leaves, which
4780+ // share a cell).
4781+ if !info.is_inter {
4782+ out[col / 2] = None;
47914783 continue;
47924784 }
4793- if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT {
4794- continue;
4785+ // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST
4786+ // qualifying one wins -- a compound block stores its second
4787+ // reference's mv, never its first (unless the second is on the
4788+ // other side or over the limit). Storing slot 0 unconditionally
4789+ // put the wrong (mv, ref_frame_offset) pair in every compound
4790+ // block's cell, which a later frame's temporal candidate then
4791+ // projected with the wrong reference distance.
4792+ let mut saved: Option<crate::motion_field::SavedMv> = None;
4793+ for (rf, mv) in [
4794+ (info.ref_frame, Some(info.mv)),
4795+ (info.ref_frame1.unwrap_or(0), info.mv1),
4796+ ] {
4797+ let (Some(mv), true) = (mv, rf > 0) else {
4798+ continue;
4799+ };
4800+ if other_side[rf as usize] {
4801+ continue;
4802+ }
4803+ if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT {
4804+ continue;
4805+ }
4806+ saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf });
47954807 }
4796- saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf });
4808+ out[col / 2] = saved;
47974809 }
4798- field.set(row, col, saved);
47994810 }
4811+ };
4812+ // lane-mf: the whole-frame pass split over the filter pool in cell-row
4813+ // bands (same `Shared` corner-cut as the filter stages -- disjointness is
4814+ // the band arithmetic above, proved by the 1-vs-16-thread byte-exact gate).
4815+ let bands = crate::par::bands(rows, crate::par::filter_threads());
4816+ if bands.len() <= 1 {
4817+ fill(0, rows, cells);
4818+ } else {
4819+ let shared = crate::par::Shared::new(cells);
4820+ crate::par::run_bands(&bands, fctx, |r0, r1, _| {
4821+ // SAFETY: cell rows `r0..r1` are this band's alone.
4822+ let cells = unsafe { shared.get() };
4823+ fill(r0, r1, &mut cells[r0 * cols..r1 * cols]);
4824+ });
48004825 }
48014826 if crate::envflags::env_flag!("EC_TRACE_TPL") {
48024827 eprintln!("EC_TPL_SAVED oh={order_hint}");
@@ -34025,6 +34050,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3402534050 order_hint,
3402634051 order_hint_bits,
3402734052 ref_order_hints,
34053+ fctx,
3402834054 );
3402934055
3403034056 let (fw, fh) = (lr_w, frame_height as usize);
crates/ec-av1/src/motion_field.rsmodified+8 -0
@@ -68,6 +68,7 @@ impl MotionField {
6868 /// (`mv->ref_frame = NONE_FRAME; mv->mv.as_int = 0;`), which every block
6969 /// of an inter frame performs -- an intra (or nothing-to-store) block
7070 /// sharing an 8x8 cell with an earlier inter one wipes that cell.
71+ #[allow(dead_code)] // lane-mf: the frame build now fills cell rows directly; kept for the module's tests
7172 pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: Option<SavedMv>) {
7273 let (r, c) = (mi_row / 2, mi_col / 2);
7374 if r < self.rows && c < self.cols {
@@ -87,6 +88,13 @@ impl MotionField {
8788 .collect()
8889 }
8990
91+ /// lane-mf: the cell grid itself (`cols`, `rows`, cells in row-major
92+ /// order), so [`crate::decode::build_motion_field`] can fill disjoint
93+ /// row bands of it in parallel. Cell row `r` owns `cells[r * cols..][..cols]`.
94+ pub(crate) fn cells_mut(&mut self) -> (usize, usize, &mut [Option<SavedMv>]) {
95+ (self.cols, self.rows, &mut self.cells)
96+ }
97+
9098 fn get(&self, row8: usize, col8: usize) -> Option<SavedMv> {
9199 if row8 < self.rows && col8 < self.cols {
92100 self.cells[row8 * self.cols + col8]