forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-tpl' (9c6fbbf1): temporal mv probe hoisted per block (parity derivation collapses to one shifted index, pinned by a spec-enumerating test), compound stack reads the cell once; four per-block gate counters thread-local instead of process-global atomics; byte-exact, -0.2% instructions on the 4K segment

Browse files at this commit

dc919eaa2bb9d8de6d7c0190bb9eeee297b93586Ahmet Kaan GümüşParent: 26ac3e0e08e4e4
crates/ec-av1/src/motion_field.rsmodified+105 -26
@@ -114,13 +114,6 @@ impl TplField {
114114 self.cells.iter().filter(|c| c.is_some()).count()
115115 }
116116
117- fn get(&self, row8: usize, col8: usize) -> Option<((i32, i32), i32)> {
118- if row8 < self.rows && col8 < self.cols {
119- self.cells[row8 * self.cols + col8]
120- } else {
121- None
122- }
123- }
124117 }
125118
126119 /// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range
@@ -508,22 +501,7 @@ pub fn add_tpl_ref_mv(
508501 cur_offset_0: i32,
509502 allow_high_precision_mv: bool,
510503 ) -> Option<TplCandidate> {
511- let pos_row = if mi_row & 1 != 0 {
512- blk_row
513- } else {
514- blk_row + 1
515- };
516- let pos_col = if mi_col & 1 != 0 {
517- blk_col
518- } else {
519- blk_col + 1
520- };
521- let row8 = mi_row as isize + pos_row;
522- let col8 = mi_col as isize + pos_col;
523- if row8 < 0 || col8 < 0 {
524- return None;
525- }
526- let probe = tpl.get(row8 as usize / 2, col8 as usize / 2);
504+ let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col);
527505 if crate::envflags::env_flag!("EC_TRACE_TPL") {
528506 match probe {
529507 None => eprintln!("EC_TPL mi_row={mi_row} mi_col={mi_col} blk=({blk_row},{blk_col}) INVALID"),
@@ -534,15 +512,116 @@ pub fn add_tpl_ref_mv(
534512 }
535513 }
536514 let (fwd_mv, ref_frame_offset) = probe?;
537- let projected = get_mv_projection(fwd_mv, cur_offset_0, ref_frame_offset);
538- let mv = lower_mv_precision(projected, allow_high_precision_mv);
539- Some(TplCandidate { mv })
515+ Some(TplCandidate {
516+ mv: project_tpl_mv(fwd_mv, cur_offset_0, ref_frame_offset, allow_high_precision_mv),
517+ })
518+}
519+
520+/// The projection half of [`add_tpl_ref_mv`], split out so a caller holding a
521+/// [`TplProbe`] cell can project it to more than one reference offset without
522+/// reading the field twice (lane-tpl: the compound stack does exactly that).
523+#[inline]
524+pub fn project_tpl_mv(
525+ fwd_mv: (i32, i32),
526+ cur_offset: i32,
527+ ref_frame_offset: i32,
528+ allow_high_precision_mv: bool,
529+) -> (i32, i32) {
530+ let projected = get_mv_projection(fwd_mv, cur_offset, ref_frame_offset);
531+ lower_mv_precision(projected, allow_high_precision_mv)
532+}
533+
534+/// One query block's view of [`TplField`] (lane-tpl). `add_tpl_ref_mv`'s
535+/// parity derivation and row base are the same for every `(blk_row,
536+/// blk_col)` the mv stack probes, so they are hoisted here and the per-sample
537+/// cost drops to an add, a shift and the bounds check: `row8 = mi_row +
538+/// pos_row` with `pos_row = blk_row + (mi_row even)` means the cell index is
539+/// `(mi_row + 1 - (mi_row & 1) + blk_row) >> 1`, and an arithmetic shift
540+/// keeps the spec's `row8 < 0` rejection exact (`row8 < 0` iff the shifted
541+/// index is negative).
542+pub struct TplProbe<'a> {
543+ cells: &'a [Option<((i32, i32), i32)>],
544+ cols: isize,
545+ rows: isize,
546+ base_row: isize,
547+ base_col: isize,
548+}
549+
550+impl TplField {
551+ /// [`TplProbe`] for the query block at `(mi_row, mi_col)`.
552+ #[inline]
553+ pub fn probe(&self, mi_row: usize, mi_col: usize) -> TplProbe<'_> {
554+ TplProbe {
555+ cells: &self.cells,
556+ cols: self.cols as isize,
557+ rows: self.rows as isize,
558+ base_row: mi_row as isize + 1 - (mi_row & 1) as isize,
559+ base_col: mi_col as isize + 1 - (mi_col & 1) as isize,
560+ }
561+ }
562+}
563+
564+impl TplProbe<'_> {
565+ /// The stored `(mv, ref_frame_offset)` at the 8x8-unit offset
566+ /// `(blk_row, blk_col)` from the query block, or `None` where the spec's
567+ /// probe falls outside the field.
568+ #[inline]
569+ pub fn cell(&self, blk_row: isize, blk_col: isize) -> Option<((i32, i32), i32)> {
570+ let row = (self.base_row + blk_row) >> 1;
571+ let col = (self.base_col + blk_col) >> 1;
572+ if row < 0 || col < 0 || row >= self.rows || col >= self.cols {
573+ return None;
574+ }
575+ self.cells[(row * self.cols + col) as usize]
576+ }
540577 }
541578
542579 #[cfg(test)]
543580 mod tests {
544581 use super::*;
545582
583+ /// lane-tpl: `TplProbe::cell` replaced `add_tpl_ref_mv`'s per-sample
584+ /// parity derivation with one hoisted base; this pins it against that
585+ /// derivation, spelled out, over every parity and every offset the mv
586+ /// stack (and its extension samples) can probe with.
587+ #[test]
588+ fn probe_cell_matches_the_spec_parity_derivation() {
589+ let (rows, cols) = (5usize, 6usize);
590+ let cells: Vec<Option<((i32, i32), i32)>> = (0..rows * cols)
591+ .map(|i| Some(((i as i32, -(i as i32)), 1)))
592+ .collect();
593+ let tpl = TplField { cols, rows, cells };
594+ for mi_row in 0..2 * rows {
595+ for mi_col in 0..2 * cols {
596+ let probe = tpl.probe(mi_row, mi_col);
597+ for blk_row in -4isize..=16 {
598+ for blk_col in -4isize..=16 {
599+ // the spec's own derivation (7.10.2.8)
600+ let pos_row = if mi_row & 1 != 0 { blk_row } else { blk_row + 1 };
601+ let pos_col = if mi_col & 1 != 0 { blk_col } else { blk_col + 1 };
602+ let row8 = mi_row as isize + pos_row;
603+ let col8 = mi_col as isize + pos_col;
604+ let want = if row8 < 0 || col8 < 0 {
605+ None
606+ } else {
607+ let (r, c) = (row8 as usize / 2, col8 as usize / 2);
608+ if r < rows && c < cols {
609+ tpl.cells[r * cols + c]
610+ } else {
611+ None
612+ }
613+ };
614+ assert_eq!(
615+ probe.cell(blk_row, blk_col),
616+ want,
617+ "mi=({mi_row},{mi_col}) blk=({blk_row},{blk_col})"
618+ );
619+ }
620+ }
621+ }
622+ }
623+ }
624+
546625 #[test]
547626 fn relative_dist_wraps_at_order_hint_bits() {
548627 // 7-bit order hints (aomenc's usual default): 1 - 126 should read as
crates/ec-av1/src/mvstack.rsmodified+115 -88
@@ -78,52 +78,61 @@
7878 //! stack to two entries (spec 7.10.2.12) — this module's stack can be
7979 //! shorter than two candidates where libaom's never is.
8080
81-/// How many query blocks [`find_mv_stack_with_sign_bias`] has folded at
82-/// least one temporal MV candidate into (spec 7.9/7.10.2.8's
83-/// `add_tpl_ref_mv`) — the real-aomenc temporal-MV gate's own firing
84-/// counter (`crate::decode::tmv_hits`), proving the projection path in
85-/// [`crate::motion_field`] is actually reached, not just compiled.
86-static TMV_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
81+thread_local! {
82+ /// How many query blocks [`find_mv_stack_with_sign_bias`] has folded at
83+ /// least one temporal MV candidate into (spec 7.9/7.10.2.8's
84+ /// `add_tpl_ref_mv`) — the real-aomenc temporal-MV gate's own firing
85+ /// counter (`crate::decode::tmv_hits`), proving the projection path in
86+ /// [`crate::motion_field`] is actually reached, not just compiled.
87+ /// lane-tpl: thread-local (like `restoration`'s stripe counters), not an
88+ /// atomic -- this one is bumped once per inter block, and a shared cache line
89+ /// on 16 frame threads' critical path costs more than the counter is worth.
90+ /// Every gate that reads it decodes on its own thread (`EC_AV1_THREADS`
91+ /// defaults to 1, which is the single-threaded decoder unchanged).
92+ static TMV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
93+}
8794
8895 /// The number of query blocks that received at least one temporal MV
8996 /// candidate so far, across every [`find_mv_stack_with_sign_bias`] call in
9097 /// this process.
9198 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
9299 pub(crate) fn tmv_hits() -> usize {
93- TMV_HITS.load(std::sync::atomic::Ordering::Relaxed)
100+ TMV_HITS.with(|c| c.get())
94101 }
95102
96-/// lane-mvtwin r1 firing counters, one per divergence this round closed.
97-/// `COMP_GM_FILL_HITS`: compound extension candidates whose `comp_list` slot
98-/// came from `gm_mv_candidates[side]` rather than a scanned neighbour, and
99-/// where that global mv was NOT `(0, 0)` (the only case the old zero fill
100-/// could not fake). `STACK_FULL_DROPS`: distinct candidates libaom drops at
101-/// `MAX_REF_MV_STACK_SIZE` on insertion. `TILE_REACH_CLIPS`: row/col reaches
102-/// shortened by a tile origin past mi 0, which the old frame-edge clamp
103-/// missed entirely.
104-static COMP_GM_FILL_HITS: std::sync::atomic::AtomicUsize =
105- std::sync::atomic::AtomicUsize::new(0);
106-static STACK_FULL_DROPS: std::sync::atomic::AtomicUsize =
107- std::sync::atomic::AtomicUsize::new(0);
108-static TILE_REACH_CLIPS: std::sync::atomic::AtomicUsize =
109- std::sync::atomic::AtomicUsize::new(0);
103+thread_local! {
104+ /// lane-mvtwin r1 firing counters, one per divergence this round closed.
105+ /// `COMP_GM_FILL_HITS`: compound extension candidates whose `comp_list` slot
106+ /// came from `gm_mv_candidates[side]` rather than a scanned neighbour, and
107+ /// where that global mv was NOT `(0, 0)` (the only case the old zero fill
108+ /// could not fake). `STACK_FULL_DROPS`: distinct candidates libaom drops at
109+ /// `MAX_REF_MV_STACK_SIZE` on insertion. `TILE_REACH_CLIPS`: row/col reaches
110+ /// shortened by a tile origin past mi 0, which the old frame-edge clamp
111+ /// missed entirely.
112+ /// lane-tpl: thread-local for the same reason as [`TMV_HITS`]
113+ /// (`STACK_FULL_DROPS` is bumped per candidate insertion, the hottest of the
114+ /// four).
115+ static COMP_GM_FILL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
116+ static STACK_FULL_DROPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
117+ static TILE_REACH_CLIPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
118+}
110119
111120 /// [`COMP_GM_FILL_HITS`].
112121 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
113122 pub(crate) fn comp_gm_fill_hits() -> usize {
114- COMP_GM_FILL_HITS.load(std::sync::atomic::Ordering::Relaxed)
123+ COMP_GM_FILL_HITS.with(|c| c.get())
115124 }
116125
117126 /// [`STACK_FULL_DROPS`].
118127 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
119128 pub(crate) fn stack_full_drops() -> usize {
120- STACK_FULL_DROPS.load(std::sync::atomic::Ordering::Relaxed)
129+ STACK_FULL_DROPS.with(|c| c.get())
121130 }
122131
123132 /// [`TILE_REACH_CLIPS`].
124133 #[allow(dead_code)] // read only from the `#[cfg(test)]` gates
125134 pub(crate) fn tile_reach_clips() -> usize {
126- TILE_REACH_CLIPS.load(std::sync::atomic::Ordering::Relaxed)
135+ TILE_REACH_CLIPS.with(|c| c.get())
127136 }
128137
129138 /// Everything [`find_mv_stack_with_sign_bias`] needs to fold temporal MV
@@ -372,7 +381,7 @@ fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32)
372381 if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) {
373382 entry.weight += weight;
374383 } else if candidates.len() >= MAX_STACK_SIZE {
375- STACK_FULL_DROPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
384+ STACK_FULL_DROPS.with(|c| c.set(c.get() + 1));
376385 } else {
377386 // libaom `add_ref_mv_candidate` (mvref_common.c:100) and
378387 // `add_tpl_ref_mv` (:388) only push when
@@ -932,7 +941,7 @@ pub fn find_mv_stack_with_sign_bias(
932941 // `None` for the cells outside the tile.
933942 let (tile_row0, tile_col0) = grid.tile_origin();
934943 if tile_row0 > 0 || tile_col0 > 0 {
935- TILE_REACH_CLIPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
944+ TILE_REACH_CLIPS.with(|c| c.set(c.get() + 1));
936945 }
937946 let max_row_offset: isize = if mi_row > tile_row0 {
938947 let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj);
@@ -1036,27 +1045,48 @@ pub fn find_mv_stack_with_sign_bias(
10361045 let step_w = if bw4 >= 16 { 4 } else { 2 };
10371046 let mut first_sample_missing = true;
10381047 let mut first_sample_far = false;
1039- let mut blk_row = 0usize;
1040- while blk_row < blk_row_end {
1041- let mut blk_col = 0usize;
1042- while blk_col < blk_col_end {
1043- if let Some(cand) = crate::motion_field::add_tpl_ref_mv(
1048+ // lane-tpl: the parity/bounds derivation and the field's row base are
1049+ // per BLOCK, not per sample -- hoisted into `TplProbe` here so each of
1050+ // the (up to 19) probes below costs an add, a shift and a bounds
1051+ // check. The `EC_TRACE_TPL` dump lives in `add_tpl_ref_mv` itself, so
1052+ // the traced path keeps calling it and the hot path never re-reads the
1053+ // flag.
1054+ let trace_tpl = crate::envflags::env_flag!("EC_TRACE_TPL");
1055+ let probe = field.probe(mi_row, mi_col);
1056+ let sample = |blk_row: isize, blk_col: isize| -> Option<(i32, i32)> {
1057+ if trace_tpl {
1058+ return crate::motion_field::add_tpl_ref_mv(
10441059 field,
10451060 mi_row,
10461061 mi_col,
1047- blk_row as isize,
1048- blk_col as isize,
1062+ blk_row,
1063+ blk_col,
10491064 cur_offset_0,
10501065 allow_high_precision_mv,
1051- ) {
1066+ )
1067+ .map(|c| c.mv);
1068+ }
1069+ let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?;
1070+ Some(crate::motion_field::project_tpl_mv(
1071+ fwd_mv,
1072+ cur_offset_0,
1073+ rfo,
1074+ allow_high_precision_mv,
1075+ ))
1076+ };
1077+ let mut blk_row = 0usize;
1078+ while blk_row < blk_row_end {
1079+ let mut blk_col = 0usize;
1080+ while blk_col < blk_col_end {
1081+ if let Some(mv) = sample(blk_row as isize, blk_col as isize) {
10521082 any_hit = true;
10531083 if blk_row == 0 && blk_col == 0 {
10541084 first_sample_missing = false;
10551085 let this_gm_mv = gm_mv(gm, ref_frame);
1056- first_sample_far = (cand.mv.0 - this_gm_mv.0).abs() >= 16
1057- || (cand.mv.1 - this_gm_mv.1).abs() >= 16;
1086+ first_sample_far = (mv.0 - this_gm_mv.0).abs() >= 16
1087+ || (mv.1 - this_gm_mv.1).abs() >= 16;
10581088 }
1059- add_candidate(&mut candidates, cand.mv, 2);
1089+ add_candidate(&mut candidates, mv, 2);
10601090 }
10611091 blk_col += step_w;
10621092 }
@@ -1079,22 +1109,14 @@ pub fn find_mv_stack_with_sign_bias(
10791109 if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask {
10801110 continue;
10811111 }
1082- if let Some(cand) = crate::motion_field::add_tpl_ref_mv(
1083- field,
1084- mi_row,
1085- mi_col,
1086- row_off,
1087- col_off,
1088- cur_offset_0,
1089- allow_high_precision_mv,
1090- ) {
1112+ if let Some(mv) = sample(row_off, col_off) {
10911113 any_hit = true;
1092- add_candidate(&mut candidates, cand.mv, 2);
1114+ add_candidate(&mut candidates, mv, 2);
10931115 }
10941116 }
10951117 }
10961118 if any_hit {
1097- TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1119+ TMV_HITS.with(|c| c.set(c.get() + 1));
10981120 }
10991121 zero_mv_ctx = usize::from(first_sample_missing || first_sample_far);
11001122 }
@@ -1394,7 +1416,7 @@ fn add_compound_candidate(
13941416 if let Some(entry) = candidates.iter_mut().find(|e| e.mv0 == mv0 && e.mv1 == mv1) {
13951417 entry.weight += weight;
13961418 } else if candidates.len() >= MAX_STACK_SIZE {
1397- STACK_FULL_DROPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1419+ STACK_FULL_DROPS.with(|c| c.set(c.get() + 1));
13981420 } else {
13991421 // Same insertion cap as [`add_candidate`]: libaom's compound branch
14001422 // of `add_ref_mv_candidate` (mvref_common.c:128) and `add_tpl_ref_mv`
@@ -1682,7 +1704,7 @@ fn combine_compound_candidates(
16821704 std::array::from_fn(|i| {
16831705 merged.get(i).copied().unwrap_or_else(|| {
16841706 if gm_mv_candidates[side] != (0, 0) {
1685- COMP_GM_FILL_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1707+ COMP_GM_FILL_HITS.with(|c| c.set(c.get() + 1));
16861708 }
16871709 gm_mv_candidates[side]
16881710 })
@@ -1742,7 +1764,7 @@ pub fn find_mv_stack_compound(
17421764 // `None` for the cells outside the tile.
17431765 let (tile_row0, tile_col0) = grid.tile_origin();
17441766 if tile_row0 > 0 || tile_col0 > 0 {
1745- TILE_REACH_CLIPS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1767+ TILE_REACH_CLIPS.with(|c| c.set(c.get() + 1));
17461768 }
17471769 let max_row_offset: isize = if mi_row > tile_row0 {
17481770 let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj);
@@ -1836,32 +1858,50 @@ pub fn find_mv_stack_compound(
18361858 let step_w = if bw4 >= 16 { 4 } else { 2 };
18371859 let mut first_sample_missing = true;
18381860 let mut first_sample_far = false;
1839- let mut blk_row = 0usize;
1840- while blk_row < blk_row_end {
1841- let mut blk_col = 0usize;
1842- while blk_col < blk_col_end {
1843- let cand0 = crate::motion_field::add_tpl_ref_mv(
1844- field,
1845- mi_row,
1846- mi_col,
1847- blk_row as isize,
1848- blk_col as isize,
1861+ // lane-tpl: the parity/bounds derivation and the field's row base are
1862+ // per BLOCK, not per sample -- hoisted into `TplProbe` here so each of
1863+ // the (up to 19) probes below costs an add, a shift and a bounds
1864+ // check. The `EC_TRACE_TPL` dump lives in `add_tpl_ref_mv` itself, so
1865+ // the traced path keeps calling it and the hot path never re-reads the
1866+ // flag.
1867+ let trace_tpl = crate::envflags::env_flag!("EC_TRACE_TPL");
1868+ let probe = field.probe(mi_row, mi_col);
1869+ let sample = |blk_row: isize, blk_col: isize| -> Option<((i32, i32), (i32, i32))> {
1870+ if trace_tpl {
1871+ for off in [cur_offset_0, cur_offset_1] {
1872+ let _ = crate::motion_field::add_tpl_ref_mv(
1873+ field,
1874+ mi_row,
1875+ mi_col,
1876+ blk_row,
1877+ blk_col,
1878+ off,
1879+ allow_high_precision_mv,
1880+ );
1881+ }
1882+ }
1883+ let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?;
1884+ Some((
1885+ crate::motion_field::project_tpl_mv(
1886+ fwd_mv,
18491887 cur_offset_0,
1888+ rfo,
18501889 allow_high_precision_mv,
1851- );
1852- let cand1 = crate::motion_field::add_tpl_ref_mv(
1853- field,
1854- mi_row,
1855- mi_col,
1856- blk_row as isize,
1857- blk_col as isize,
1890+ ),
1891+ crate::motion_field::project_tpl_mv(
1892+ fwd_mv,
18581893 cur_offset_1,
1894+ rfo,
18591895 allow_high_precision_mv,
1860- );
1861- if cand0.is_some() || cand1.is_some() {
1896+ ),
1897+ ))
1898+ };
1899+ let mut blk_row = 0usize;
1900+ while blk_row < blk_row_end {
1901+ let mut blk_col = 0usize;
1902+ while blk_col < blk_col_end {
1903+ if let Some((mv0, mv1)) = sample(blk_row as isize, blk_col as isize) {
18621904 any_hit = true;
1863- let mv0 = cand0.map_or((0, 0), |c| c.mv);
1864- let mv1 = cand1.map_or((0, 0), |c| c.mv);
18651905 if blk_row == 0 && blk_col == 0 {
18661906 first_sample_missing = false;
18671907 let this_gm0 = gm_mv(gm, ref_frame.0);
@@ -1899,27 +1939,14 @@ pub fn find_mv_stack_compound(
18991939 if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask {
19001940 continue;
19011941 }
1902- let cand0 = crate::motion_field::add_tpl_ref_mv(
1903- field, mi_row, mi_col, row_off, col_off, cur_offset_0,
1904- allow_high_precision_mv,
1905- );
1906- let cand1 = crate::motion_field::add_tpl_ref_mv(
1907- field, mi_row, mi_col, row_off, col_off, cur_offset_1,
1908- allow_high_precision_mv,
1909- );
1910- if cand0.is_some() || cand1.is_some() {
1942+ if let Some((mv0, mv1)) = sample(row_off, col_off) {
19111943 any_hit = true;
1912- add_compound_candidate(
1913- &mut candidates,
1914- cand0.map_or((0, 0), |c| c.mv),
1915- cand1.map_or((0, 0), |c| c.mv),
1916- 2,
1917- );
1944+ add_compound_candidate(&mut candidates, mv0, mv1, 2);
19181945 }
19191946 }
19201947 }
19211948 if any_hit {
1922- TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1949+ TMV_HITS.with(|c| c.set(c.get() + 1));
19231950 }
19241951 zero_mv_ctx = usize::from(first_sample_missing || first_sample_far);
19251952 }