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> {
4730 /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is4730 /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is
4731 /// exact — every block this crate ever codes is at least 8x8, so no 8x84731 /// exact — every block this crate ever codes is at least 8x8, so no 8x8
4732 /// cell straddles two different blocks' values.4732 /// cell straddles two different blocks' values.
4733+#[allow(unsafe_code)] // lane-mf: `par::Shared` band split, same corner-cut as the filter stages
4733 pub(crate) fn build_motion_field(4734 pub(crate) fn build_motion_field(
4734 grid: &MiGrid,4735 grid: &MiGrid,
4735 mi_cols: usize,4736 mi_cols: usize,
@@ -4737,6 +4738,7 @@ pub(crate) fn build_motion_field(
4737 order_hint: u32,4738 order_hint: u32,
4738 order_hint_bits: u32,4739 order_hint_bits: u32,
4739 ref_order_hints: [u32; 7],4740 ref_order_hints: [u32; 7],
4741+ fctx: &FrameCtx,
4740 ) -> crate::motion_field::MotionField {4742 ) -> crate::motion_field::MotionField {
4741 let mut field =4743 let mut field =
4742 crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints, false);4744 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(
4746 // referencing one stores only its OTHER side's mv. With4748 // referencing one stores only its OTHER side's mv. With
4747 // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and4749 // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and
4748 // nothing is skipped.4750 // nothing is skipped.
4749- let other_side = |rf: i8| -> bool {4751+ // lane-mf: hoisted out of the per-4x4-unit loop -- the answer depends
4750- if order_hint_bits == 0 {4752+ // only on the reference frame, so it is 7 table lookups per frame instead
4751- return false;4753+ // of two per inter unit.
4752- }4754+ let other_side: [bool; 8] = std::array::from_fn(|rf| {
4753- let oh = ref_order_hints[(rf - LAST_FRAME) as usize];4755+ let i = rf.wrapping_sub(LAST_FRAME as usize);
4754- crate::motion_field::get_relative_dist(order_hint_bits, oh, order_hint) > 04756+ order_hint_bits != 0
4755- || oh == order_hint4757+ && i < 7
4756- };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+ });
4757 // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is4761 // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is
4758 // capped at 12 bits per component; a longer one is not stored at all.4762 // capped at 12 bits per component; a longer one is not stored at all.
4759 const REFMVS_LIMIT: i32 = (1 << 12) - 1;4763 const REFMVS_LIMIT: i32 = (1 << 12) - 1;
4760- for row in 0..mi_rows {4764+ let (cols, rows, cells) = field.cells_mut();
4761- for col in 0..mi_cols {4765+ // Fills cell rows `r0..r1`; `out` is exactly those rows' cells, so bands
4762- let Some(info) = grid.get_frame(row, col) else {4766+ // never touch each other (an 8x8 cell lives in one cell row, and mi row
4763- continue;4767+ // `row` writes cell row `row / 2`).
4764- };4768+ let fill = |r0: usize, r1: usize, out: &mut [Option<crate::motion_field::SavedMv>]| {
4765- // `av1_copy_frame_mvs` runs for EVERY block of an inter frame and4769+ for row in (r0 * 2)..(r1 * 2).min(mi_rows) {
4766- // CLEARS the cells it covers first (`mv->ref_frame = NONE_FRAME`),4770+ let out = &mut out[(row / 2 - r0) * cols..][..cols];
4767- // so an intra block -- or an inter block whose every slot is on the4771+ for col in 0..mi_cols {
4768- // other side / over the limit -- wipes what an earlier block wrote4772+ let Some(info) = grid.get_frame(row, col) else {
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 {
4788 continue;4773 continue;
4789 };4774 };
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;
4791 continue;4783 continue;
4792 }4784 }
4793- if mv.0.abs() > REFMVS_LIMIT || mv.1.abs() > REFMVS_LIMIT {4785+ // `av1_copy_frame_mvs` walks BOTH reference slots and the LAST
4794- continue;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 });
4795 }4807 }
4796- saved = Some(crate::motion_field::SavedMv { mv, ref_frame: rf });4808+ out[col / 2] = saved;
4797 }4809 }
4798- field.set(row, col, saved);
4799 }4810 }
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+ });
4800 }4825 }
4801 if crate::envflags::env_flag!("EC_TRACE_TPL") {4826 if crate::envflags::env_flag!("EC_TRACE_TPL") {
4802 eprintln!("EC_TPL_SAVED oh={order_hint}");4827 eprintln!("EC_TPL_SAVED oh={order_hint}");
@@ -34025,6 +34050,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
34025 order_hint,34050 order_hint,
34026 order_hint_bits,34051 order_hint_bits,
34027 ref_order_hints,34052 ref_order_hints,
34053+ fctx,
34028 );34054 );
34029 34055
34030 let (fw, fh) = (lr_w, frame_height as usize);34056 let (fw, fh) = (lr_w, frame_height as usize);
crates/ec-av1/src/motion_field.rsmodified+8 -0
@@ -68,6 +68,7 @@ impl MotionField {
68 /// (`mv->ref_frame = NONE_FRAME; mv->mv.as_int = 0;`), which every block68 /// (`mv->ref_frame = NONE_FRAME; mv->mv.as_int = 0;`), which every block
69 /// of an inter frame performs -- an intra (or nothing-to-store) block69 /// of an inter frame performs -- an intra (or nothing-to-store) block
70 /// sharing an 8x8 cell with an earlier inter one wipes that cell.70 /// 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
71 pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: Option<SavedMv>) {72 pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: Option<SavedMv>) {
72 let (r, c) = (mi_row / 2, mi_col / 2);73 let (r, c) = (mi_row / 2, mi_col / 2);
73 if r < self.rows && c < self.cols {74 if r < self.rows && c < self.cols {
@@ -87,6 +88,13 @@ impl MotionField {
87 .collect()88 .collect()
88 }89 }
89 90
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+
90 fn get(&self, row8: usize, col8: usize) -> Option<SavedMv> {98 fn get(&self, row8: usize, col8: usize) -> Option<SavedMv> {
91 if row8 < self.rows && col8 < self.cols {99 if row8 < self.rows && col8 < self.cols {
92 self.cells[row8 * self.cols + col8]100 self.cells[row8 * self.cols + col8]