Hoist the temporal-MV probe out of the per-sample loop, thread-local the mvstack gate counters
Browse files at this commite08e4e454bdf7b9c0454d2f9aaa8f08674823b91Parent: 6d124e3- perf
- hoist the temporal-MV probe out of the per-sample loop, thread-local the mvstack gate counters
2 changed files +204 -98
- crates/ec-av1/src/motion_field.rs +105 -26
- crates/ec-av1/src/mvstack.rs +99 -72
crates/ec-av1/src/motion_field.rsmodified+105 -26
| @@ -114,13 +114,6 @@ impl TplField { | |||
| 114 | self.cells.iter().filter(|c| c.is_some()).count() | 114 | self.cells.iter().filter(|c| c.is_some()).count() |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 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 | - } | ||
| 124 | } | 117 | } |
| 125 | 118 | ||
| 126 | /// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range | 119 | /// spec `get_relative_dist` (5.9.3): `a - b` wrapped into the signed range |
| @@ -508,22 +501,7 @@ pub fn add_tpl_ref_mv( | |||
| 508 | cur_offset_0: i32, | 501 | cur_offset_0: i32, |
| 509 | allow_high_precision_mv: bool, | 502 | allow_high_precision_mv: bool, |
| 510 | ) -> Option<TplCandidate> { | 503 | ) -> Option<TplCandidate> { |
| 511 | - let pos_row = if mi_row & 1 != 0 { | 504 | + let probe = tpl.probe(mi_row, mi_col).cell(blk_row, blk_col); |
| 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); | ||
| 527 | if crate::envflags::env_flag!("EC_TRACE_TPL") { | 505 | if crate::envflags::env_flag!("EC_TRACE_TPL") { |
| 528 | match probe { | 506 | match probe { |
| 529 | None => eprintln!("EC_TPL mi_row={mi_row} mi_col={mi_col} blk=({blk_row},{blk_col}) INVALID"), | 507 | 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( | |||
| 534 | } | 512 | } |
| 535 | } | 513 | } |
| 536 | let (fwd_mv, ref_frame_offset) = probe?; | 514 | let (fwd_mv, ref_frame_offset) = probe?; |
| 537 | - let projected = get_mv_projection(fwd_mv, cur_offset_0, ref_frame_offset); | 515 | + Some(TplCandidate { |
| 538 | - let mv = lower_mv_precision(projected, allow_high_precision_mv); | 516 | + mv: project_tpl_mv(fwd_mv, cur_offset_0, ref_frame_offset, allow_high_precision_mv), |
| 539 | - Some(TplCandidate { 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 | + } | ||
| 540 | } | 577 | } |
| 541 | 578 | ||
| 542 | #[cfg(test)] | 579 | #[cfg(test)] |
| 543 | mod tests { | 580 | mod tests { |
| 544 | use super::*; | 581 | use super::*; |
| 545 | 582 | ||
| 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 | + | ||
| 546 | #[test] | 625 | #[test] |
| 547 | fn relative_dist_wraps_at_order_hint_bits() { | 626 | fn relative_dist_wraps_at_order_hint_bits() { |
| 548 | // 7-bit order hints (aomenc's usual default): 1 - 126 should read as | 627 | // 7-bit order hints (aomenc's usual default): 1 - 126 should read as |
crates/ec-av1/src/mvstack.rsmodified+99 -72
| @@ -78,21 +78,29 @@ | |||
| 78 | //! stack to two entries (spec 7.10.2.12) — this module's stack can be | 78 | //! stack to two entries (spec 7.10.2.12) — this module's stack can be |
| 79 | //! shorter than two candidates where libaom's never is. | 79 | //! shorter than two candidates where libaom's never is. |
| 80 | 80 | ||
| 81 | +thread_local! { | ||
| 81 | /// How many query blocks [`find_mv_stack_with_sign_bias`] has folded at | 82 | /// 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 | /// 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 | /// `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 | /// counter (`crate::decode::tmv_hits`), proving the projection path in |
| 85 | /// [`crate::motion_field`] is actually reached, not just compiled. | 86 | /// [`crate::motion_field`] is actually reached, not just compiled. |
| 86 | -static TMV_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | 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 | +} | ||
| 87 | 94 | ||
| 88 | /// The number of query blocks that received at least one temporal MV | 95 | /// The number of query blocks that received at least one temporal MV |
| 89 | /// candidate so far, across every [`find_mv_stack_with_sign_bias`] call in | 96 | /// candidate so far, across every [`find_mv_stack_with_sign_bias`] call in |
| 90 | /// this process. | 97 | /// this process. |
| 91 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 98 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 92 | pub(crate) fn tmv_hits() -> usize { | 99 | pub(crate) fn tmv_hits() -> usize { |
| 93 | - TMV_HITS.load(std::sync::atomic::Ordering::Relaxed) | 100 | + TMV_HITS.with(|c| c.get()) |
| 94 | } | 101 | } |
| 95 | 102 | ||
| 103 | +thread_local! { | ||
| 96 | /// lane-mvtwin r1 firing counters, one per divergence this round closed. | 104 | /// lane-mvtwin r1 firing counters, one per divergence this round closed. |
| 97 | /// `COMP_GM_FILL_HITS`: compound extension candidates whose `comp_list` slot | 105 | /// `COMP_GM_FILL_HITS`: compound extension candidates whose `comp_list` slot |
| 98 | /// came from `gm_mv_candidates[side]` rather than a scanned neighbour, and | 106 | /// came from `gm_mv_candidates[side]` rather than a scanned neighbour, and |
| @@ -101,29 +109,30 @@ pub(crate) fn tmv_hits() -> usize { | |||
| 101 | /// `MAX_REF_MV_STACK_SIZE` on insertion. `TILE_REACH_CLIPS`: row/col reaches | 109 | /// `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 | 110 | /// shortened by a tile origin past mi 0, which the old frame-edge clamp |
| 103 | /// missed entirely. | 111 | /// missed entirely. |
| 104 | -static COMP_GM_FILL_HITS: std::sync::atomic::AtomicUsize = | 112 | + /// lane-tpl: thread-local for the same reason as [`TMV_HITS`] |
| 105 | - std::sync::atomic::AtomicUsize::new(0); | 113 | + /// (`STACK_FULL_DROPS` is bumped per candidate insertion, the hottest of the |
| 106 | -static STACK_FULL_DROPS: std::sync::atomic::AtomicUsize = | 114 | + /// four). |
| 107 | - std::sync::atomic::AtomicUsize::new(0); | 115 | + static COMP_GM_FILL_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 108 | -static TILE_REACH_CLIPS: std::sync::atomic::AtomicUsize = | 116 | + static STACK_FULL_DROPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 109 | - std::sync::atomic::AtomicUsize::new(0); | 117 | + static TILE_REACH_CLIPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 118 | +} | ||
| 110 | 119 | ||
| 111 | /// [`COMP_GM_FILL_HITS`]. | 120 | /// [`COMP_GM_FILL_HITS`]. |
| 112 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 121 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 113 | pub(crate) fn comp_gm_fill_hits() -> usize { | 122 | 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()) |
| 115 | } | 124 | } |
| 116 | 125 | ||
| 117 | /// [`STACK_FULL_DROPS`]. | 126 | /// [`STACK_FULL_DROPS`]. |
| 118 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 127 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 119 | pub(crate) fn stack_full_drops() -> usize { | 128 | 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()) |
| 121 | } | 130 | } |
| 122 | 131 | ||
| 123 | /// [`TILE_REACH_CLIPS`]. | 132 | /// [`TILE_REACH_CLIPS`]. |
| 124 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 133 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 125 | pub(crate) fn tile_reach_clips() -> usize { | 134 | 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()) |
| 127 | } | 136 | } |
| 128 | 137 | ||
| 129 | /// Everything [`find_mv_stack_with_sign_bias`] needs to fold temporal MV | 138 | /// 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) | |||
| 372 | if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) { | 381 | if let Some(entry) = candidates.iter_mut().find(|e| e.mv == mv) { |
| 373 | entry.weight += weight; | 382 | entry.weight += weight; |
| 374 | } else if candidates.len() >= MAX_STACK_SIZE { | 383 | } 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)); |
| 376 | } else { | 385 | } else { |
| 377 | // libaom `add_ref_mv_candidate` (mvref_common.c:100) and | 386 | // libaom `add_ref_mv_candidate` (mvref_common.c:100) and |
| 378 | // `add_tpl_ref_mv` (:388) only push when | 387 | // `add_tpl_ref_mv` (:388) only push when |
| @@ -932,7 +941,7 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 932 | // `None` for the cells outside the tile. | 941 | // `None` for the cells outside the tile. |
| 933 | let (tile_row0, tile_col0) = grid.tile_origin(); | 942 | let (tile_row0, tile_col0) = grid.tile_origin(); |
| 934 | if tile_row0 > 0 || tile_col0 > 0 { | 943 | 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)); |
| 936 | } | 945 | } |
| 937 | let max_row_offset: isize = if mi_row > tile_row0 { | 946 | let max_row_offset: isize = if mi_row > tile_row0 { |
| 938 | let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); | 947 | let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); |
| @@ -1036,27 +1045,48 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 1036 | let step_w = if bw4 >= 16 { 4 } else { 2 }; | 1045 | let step_w = if bw4 >= 16 { 4 } else { 2 }; |
| 1037 | let mut first_sample_missing = true; | 1046 | let mut first_sample_missing = true; |
| 1038 | let mut first_sample_far = false; | 1047 | let mut first_sample_far = false; |
| 1039 | - let mut blk_row = 0usize; | 1048 | + // lane-tpl: the parity/bounds derivation and the field's row base are |
| 1040 | - while blk_row < blk_row_end { | 1049 | + // per BLOCK, not per sample -- hoisted into `TplProbe` here so each of |
| 1041 | - let mut blk_col = 0usize; | 1050 | + // the (up to 19) probes below costs an add, a shift and a bounds |
| 1042 | - while blk_col < blk_col_end { | 1051 | + // check. The `EC_TRACE_TPL` dump lives in `add_tpl_ref_mv` itself, so |
| 1043 | - if let Some(cand) = crate::motion_field::add_tpl_ref_mv( | 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( | ||
| 1044 | field, | 1059 | field, |
| 1045 | mi_row, | 1060 | mi_row, |
| 1046 | mi_col, | 1061 | mi_col, |
| 1047 | - blk_row as isize, | 1062 | + blk_row, |
| 1048 | - blk_col as isize, | 1063 | + blk_col, |
| 1049 | cur_offset_0, | 1064 | cur_offset_0, |
| 1050 | allow_high_precision_mv, | 1065 | 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) { | ||
| 1052 | any_hit = true; | 1082 | any_hit = true; |
| 1053 | if blk_row == 0 && blk_col == 0 { | 1083 | if blk_row == 0 && blk_col == 0 { |
| 1054 | first_sample_missing = false; | 1084 | first_sample_missing = false; |
| 1055 | let this_gm_mv = gm_mv(gm, ref_frame); | 1085 | let this_gm_mv = gm_mv(gm, ref_frame); |
| 1056 | - first_sample_far = (cand.mv.0 - this_gm_mv.0).abs() >= 16 | 1086 | + first_sample_far = (mv.0 - this_gm_mv.0).abs() >= 16 |
| 1057 | - || (cand.mv.1 - this_gm_mv.1).abs() >= 16; | 1087 | + || (mv.1 - this_gm_mv.1).abs() >= 16; |
| 1058 | } | 1088 | } |
| 1059 | - add_candidate(&mut candidates, cand.mv, 2); | 1089 | + add_candidate(&mut candidates, mv, 2); |
| 1060 | } | 1090 | } |
| 1061 | blk_col += step_w; | 1091 | blk_col += step_w; |
| 1062 | } | 1092 | } |
| @@ -1079,22 +1109,14 @@ pub fn find_mv_stack_with_sign_bias( | |||
| 1079 | if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { | 1109 | if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { |
| 1080 | continue; | 1110 | continue; |
| 1081 | } | 1111 | } |
| 1082 | - if let Some(cand) = crate::motion_field::add_tpl_ref_mv( | 1112 | + if let Some(mv) = sample(row_off, col_off) { |
| 1083 | - field, | ||
| 1084 | - mi_row, | ||
| 1085 | - mi_col, | ||
| 1086 | - row_off, | ||
| 1087 | - col_off, | ||
| 1088 | - cur_offset_0, | ||
| 1089 | - allow_high_precision_mv, | ||
| 1090 | - ) { | ||
| 1091 | any_hit = true; | 1113 | any_hit = true; |
| 1092 | - add_candidate(&mut candidates, cand.mv, 2); | 1114 | + add_candidate(&mut candidates, mv, 2); |
| 1093 | } | 1115 | } |
| 1094 | } | 1116 | } |
| 1095 | } | 1117 | } |
| 1096 | if any_hit { | 1118 | if any_hit { |
| 1097 | - TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 1119 | + TMV_HITS.with(|c| c.set(c.get() + 1)); |
| 1098 | } | 1120 | } |
| 1099 | zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); | 1121 | zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); |
| 1100 | } | 1122 | } |
| @@ -1394,7 +1416,7 @@ fn add_compound_candidate( | |||
| 1394 | if let Some(entry) = candidates.iter_mut().find(|e| e.mv0 == mv0 && e.mv1 == mv1) { | 1416 | if let Some(entry) = candidates.iter_mut().find(|e| e.mv0 == mv0 && e.mv1 == mv1) { |
| 1395 | entry.weight += weight; | 1417 | entry.weight += weight; |
| 1396 | } else if candidates.len() >= MAX_STACK_SIZE { | 1418 | } 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)); |
| 1398 | } else { | 1420 | } else { |
| 1399 | // Same insertion cap as [`add_candidate`]: libaom's compound branch | 1421 | // Same insertion cap as [`add_candidate`]: libaom's compound branch |
| 1400 | // of `add_ref_mv_candidate` (mvref_common.c:128) and `add_tpl_ref_mv` | 1422 | // of `add_ref_mv_candidate` (mvref_common.c:128) and `add_tpl_ref_mv` |
| @@ -1682,7 +1704,7 @@ fn combine_compound_candidates( | |||
| 1682 | std::array::from_fn(|i| { | 1704 | std::array::from_fn(|i| { |
| 1683 | merged.get(i).copied().unwrap_or_else(|| { | 1705 | merged.get(i).copied().unwrap_or_else(|| { |
| 1684 | if gm_mv_candidates[side] != (0, 0) { | 1706 | 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)); |
| 1686 | } | 1708 | } |
| 1687 | gm_mv_candidates[side] | 1709 | gm_mv_candidates[side] |
| 1688 | }) | 1710 | }) |
| @@ -1742,7 +1764,7 @@ pub fn find_mv_stack_compound( | |||
| 1742 | // `None` for the cells outside the tile. | 1764 | // `None` for the cells outside the tile. |
| 1743 | let (tile_row0, tile_col0) = grid.tile_origin(); | 1765 | let (tile_row0, tile_col0) = grid.tile_origin(); |
| 1744 | if tile_row0 > 0 || tile_col0 > 0 { | 1766 | 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)); |
| 1746 | } | 1768 | } |
| 1747 | let max_row_offset: isize = if mi_row > tile_row0 { | 1769 | let max_row_offset: isize = if mi_row > tile_row0 { |
| 1748 | let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); | 1770 | let reach = if bh4 < 2 { -4 } else { -6 } + isize::from(row_adj); |
| @@ -1836,32 +1858,50 @@ pub fn find_mv_stack_compound( | |||
| 1836 | let step_w = if bw4 >= 16 { 4 } else { 2 }; | 1858 | let step_w = if bw4 >= 16 { 4 } else { 2 }; |
| 1837 | let mut first_sample_missing = true; | 1859 | let mut first_sample_missing = true; |
| 1838 | let mut first_sample_far = false; | 1860 | let mut first_sample_far = false; |
| 1839 | - let mut blk_row = 0usize; | 1861 | + // lane-tpl: the parity/bounds derivation and the field's row base are |
| 1840 | - while blk_row < blk_row_end { | 1862 | + // per BLOCK, not per sample -- hoisted into `TplProbe` here so each of |
| 1841 | - let mut blk_col = 0usize; | 1863 | + // the (up to 19) probes below costs an add, a shift and a bounds |
| 1842 | - while blk_col < blk_col_end { | 1864 | + // check. The `EC_TRACE_TPL` dump lives in `add_tpl_ref_mv` itself, so |
| 1843 | - let cand0 = crate::motion_field::add_tpl_ref_mv( | 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( | ||
| 1844 | field, | 1873 | field, |
| 1845 | mi_row, | 1874 | mi_row, |
| 1846 | mi_col, | 1875 | mi_col, |
| 1847 | - blk_row as isize, | 1876 | + blk_row, |
| 1848 | - blk_col as isize, | 1877 | + blk_col, |
| 1849 | - cur_offset_0, | 1878 | + off, |
| 1850 | allow_high_precision_mv, | 1879 | allow_high_precision_mv, |
| 1851 | ); | 1880 | ); |
| 1852 | - let cand1 = crate::motion_field::add_tpl_ref_mv( | 1881 | + } |
| 1853 | - field, | 1882 | + } |
| 1854 | - mi_row, | 1883 | + let (fwd_mv, rfo) = probe.cell(blk_row, blk_col)?; |
| 1855 | - mi_col, | 1884 | + Some(( |
| 1856 | - blk_row as isize, | 1885 | + crate::motion_field::project_tpl_mv( |
| 1857 | - blk_col as isize, | 1886 | + fwd_mv, |
| 1887 | + cur_offset_0, | ||
| 1888 | + rfo, | ||
| 1889 | + allow_high_precision_mv, | ||
| 1890 | + ), | ||
| 1891 | + crate::motion_field::project_tpl_mv( | ||
| 1892 | + fwd_mv, | ||
| 1858 | cur_offset_1, | 1893 | cur_offset_1, |
| 1894 | + rfo, | ||
| 1859 | allow_high_precision_mv, | 1895 | allow_high_precision_mv, |
| 1860 | - ); | 1896 | + ), |
| 1861 | - if cand0.is_some() || cand1.is_some() { | 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) { | ||
| 1862 | any_hit = true; | 1904 | 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); | ||
| 1865 | if blk_row == 0 && blk_col == 0 { | 1905 | if blk_row == 0 && blk_col == 0 { |
| 1866 | first_sample_missing = false; | 1906 | first_sample_missing = false; |
| 1867 | let this_gm0 = gm_mv(gm, ref_frame.0); | 1907 | let this_gm0 = gm_mv(gm, ref_frame.0); |
| @@ -1899,27 +1939,14 @@ pub fn find_mv_stack_compound( | |||
| 1899 | if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { | 1939 | if row < 0 || row >= sb_mask || col < 0 || col >= sb_mask { |
| 1900 | continue; | 1940 | continue; |
| 1901 | } | 1941 | } |
| 1902 | - let cand0 = crate::motion_field::add_tpl_ref_mv( | 1942 | + if let Some((mv0, mv1)) = sample(row_off, col_off) { |
| 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() { | ||
| 1911 | any_hit = true; | 1943 | any_hit = true; |
| 1912 | - add_compound_candidate( | 1944 | + add_compound_candidate(&mut candidates, mv0, mv1, 2); |
| 1913 | - &mut candidates, | ||
| 1914 | - cand0.map_or((0, 0), |c| c.mv), | ||
| 1915 | - cand1.map_or((0, 0), |c| c.mv), | ||
| 1916 | - 2, | ||
| 1917 | - ); | ||
| 1918 | } | 1945 | } |
| 1919 | } | 1946 | } |
| 1920 | } | 1947 | } |
| 1921 | if any_hit { | 1948 | if any_hit { |
| 1922 | - TMV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 1949 | + TMV_HITS.with(|c| c.set(c.get() + 1)); |
| 1923 | } | 1950 | } |
| 1924 | zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); | 1951 | zero_mv_ctx = usize::from(first_sample_missing || first_sample_far); |
| 1925 | } | 1952 | } |