Merge branch 'lane-mf' (72fe2c59): motion field build hoists the per-ref side test to a per-frame table and runs in row bands over the filter pool; byte-exact, -0.55% instructions at the composed setting
Browse files at this commit2 changed files +75 -41
crates/ec-av1/src/decode.rsmodified+67 -41
| @@ -4730,6 +4730,7 @@ pub(crate) struct TplFrameArgs<'a> { | ||
| 4730 | 4730 | /// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is |
| 4731 | 4731 | /// exact — every block this crate ever codes is at least 8x8, so no 8x8 |
| 4732 | 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 | 4734 | pub(crate) fn build_motion_field( |
| 4734 | 4735 | grid: &MiGrid, |
| 4735 | 4736 | mi_cols: usize, |
| @@ -4737,6 +4738,7 @@ pub(crate) fn build_motion_field( | ||
| 4737 | 4738 | order_hint: u32, |
| 4738 | 4739 | order_hint_bits: u32, |
| 4739 | 4740 | ref_order_hints: [u32; 7], |
| 4741 | + fctx: &FrameCtx, | |
| 4740 | 4742 | ) -> crate::motion_field::MotionField { |
| 4741 | 4743 | let mut field = |
| 4742 | 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 | 4748 | // referencing one stores only its OTHER side's mv. With |
| 4747 | 4749 | // `enable_order_hint` off (`order_hint_bits == 0`) every side is 0 and |
| 4748 | 4750 | // 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 | + }); | |
| 4757 | 4761 | // libaom `REFMVS_LIMIT` (`mvref_common.h`): a stored motion field mv is |
| 4758 | 4762 | // capped at 12 bits per component; a longer one is not stored at all. |
| 4759 | 4763 | 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 { | |
| 4788 | 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 | 4783 | continue; |
| 4792 | 4784 | } |
| 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 }); | |
| 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 | 4826 | if crate::envflags::env_flag!("EC_TRACE_TPL") { |
| 4802 | 4827 | eprintln!("EC_TPL_SAVED oh={order_hint}"); |
| @@ -34025,6 +34050,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 34025 | 34050 | order_hint, |
| 34026 | 34051 | order_hint_bits, |
| 34027 | 34052 | ref_order_hints, |
| 34053 | + fctx, | |
| 34028 | 34054 | ); |
| 34029 | 34055 | |
| 34030 | 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 | 68 | /// (`mv->ref_frame = NONE_FRAME; mv->mv.as_int = 0;`), which every block |
| 69 | 69 | /// of an inter frame performs -- an intra (or nothing-to-store) block |
| 70 | 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 | 72 | pub fn set(&mut self, mi_row: usize, mi_col: usize, saved: Option<SavedMv>) { |
| 72 | 73 | let (r, c) = (mi_row / 2, mi_col / 2); |
| 73 | 74 | if r < self.rows && c < self.cols { |
| @@ -87,6 +88,13 @@ impl MotionField { | ||
| 87 | 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 | 98 | fn get(&self, row8: usize, col8: usize) -> Option<SavedMv> { |
| 91 | 99 | if row8 < self.rows && col8 < self.cols { |
| 92 | 100 | self.cells[row8 * self.cols + col8] |