Commit ff723424b5 for aom
commit ff723424b5e6a3758e2e0ee3f6974ea30106f158
Author: Joe Azar-Williams <jazarwil@google.com>
Date: Mon Nov 23 16:44:21 2026 +0000
av1: Speed up exhaustive_mesh_search
Rework the search loops of exhaustive_mesh_search():
- Compute the reference pointer once per row and step along it, and
read the source buffer, strides, and sdx4df function pointer once per
call. Previously, the step == 1 loop re-derived all four candidate
addresses from MVs and reloaded those fields for every 4-way SAD call;
that per-call overhead drove most of the cost on IntraBC screen
content (where step == 1 already batched >92% of candidates).
- Use the 4-way sdx4df callback for every step value (step >= 1) instead
of only step == 1, and carry up to 3 trailing row candidates across
row boundaries in tail_addrs[4] / tail_mvs[4] so at most 3 scalar
get_mvpred_sad() calls remain per search (previously step > 1 used
scalar SAD everywhere, and step == 1 left 0-2 trailing scalar calls
per row). All sdx4df / sdsx4df backends (C, SSE2, AVX2, AVX-512, NEON,
neon_dotprod, and high-bitdepth) accept four independent reference
pointers.
At the start of each row, any carried-over tail is completed and flushed
first so all candidates are evaluated in the same raster order as before
(preserving update_mvs_and_sad() tie-breaking and second_best_mv). To
keep encoder output bit-exact in this change, last_col preserves the
step == 1 tail bound that skips end_col when (end_col - start_col + 1)
is not a multiple of 4 (previously `for (i = 0; i < end_col - c; ++i)`).
An early return skips empty windows (including clamped 1-column windows
at step == 1 where skipping end_col leaves no columns to search).
Including end_col changes bitstreams and is left for a follow-up.
Performance (bit-exact across all tested configurations):
- 5-clip two-pass VOD suite (240p-1080p, --good, cpu-used 1-2):
- Intel Skylake (AVX2, FDO+ThinLTO, n=10): -1.42% CPU time (p=0.023),
-1.77% cycles, -2.61% instructions (p < 0.001); per-rung (n=6):
-0.46% to -2.48% cycles, -0.65% to -3.76% instructions (p=0.002).
- AMD EPYC Genoa (AVX-512, FDO+ThinLTO, n=8): -1.53% CPU time,
-1.53% cycles, -2.74% instructions (p < 0.001).
- AMD EPYC Milan (AVX2, non-FDO ThinLTO, n=8): -1.72% CPU time,
-1.76% cycles, -2.20% instructions (p < 0.001).
- Direct A/B vs scalar row-tail fallback without carry (n=8,
FDO+ThinLTO): omitting the carry regresses +0.36% cycles / +0.66%
instructions on Skylake (p < 0.001) and +0.21% CPU time / +0.20%
cycles / +0.70% instructions on Genoa (p <= 0.001).
- Standalone aomenc on AMD EPYC Milan (non-FDO, 1T pinned, interleaved
perf stat, median, --end-usage=q --cq-level=32; two-pass --good unless
noted):
- screendata.y4m (--tune-content=screen, cpu-used=1, 10f, n=3):
clang 19: -8.78% time, -8.84% cycles, -10.51% instructions
GCC 15: -11.00% time, -11.09% cycles, -8.59% instructions
- screendata.y4m (--allintra, --tune-content=screen, cpu-used=1, 2f,
n=3):
clang 19: -10.75% time, -10.95% cycles, -11.27% instructions
GCC 15: -13.64% time, -13.65% cycles, -8.25% instructions
- screendata.y4m (--tune-content=screen, cpu-used=3, 10f, n=5):
clang 19: -2.99% time, -3.04% cycles, -4.10% instructions
GCC 15: -2.34% time, -2.62% cycles, -3.74% instructions
- desktop_credits.y4m (--tune-content=screen, cpu-used=1, 15f, n=3):
clang 19: -4.41% time, -4.54% cycles, -5.71% instructions
GCC 15: -6.50% time, -6.59% cycles, -4.34% instructions
- desktop_credits.y4m (--tune-content=screen, cpu-used=0, 8f, n=3):
clang 19: -4.09% time, -4.22% cycles, -5.04% instructions
GCC 15: -5.65% time, -5.69% cycles, -3.74% instructions
- niklas_1280_720_30.y4m (cpu-used=1, 6f, n=3):
clang 19: -0.83% time, -0.80% cycles, -1.01% instructions
- niklas_1280_720_30.y4m (cpu-used=2, 8f, n=3):
clang 19: -0.89% time, -0.94% cycles, -1.41% instructions
- paris_352_288_30.y4m (cpu-used=1, 12f, n=3):
clang 19: -0.27% time, -0.45% cycles, -0.31% instructions
- paris_352_288_30.y4m (cpu-used=2, 30f, n=3):
clang 19: -0.10% time, -0.22% cycles, -0.31% instructions
GCC 15: +0.24% time, +0.20% cycles, -0.36% instructions
Validation:
- Bit-exact standalone aomenc configs with --test-decode=fatal: 27
clang 19 configs confirmed to reach exhaustive_mesh_search() (18 also
checked with GCC 15), covering 8/10/12-bit, 4:2:0/4:4:4/monochrome,
cpu-used 0-6, screen and natural content, lossless, VBR, odd/tiny
dimensions (67x53, 17x13), multi-threaded tiled with row-mt, and
--allintra screen cpu-used 0-2.
- Bit-exact on x86 (C-only, SSE2, SSE4.1, AVX2, AVX-512) and aarch64
(NEON/dotprod, 26/26 pass 1 + bitstream digests matching base) on the
5-clip VOD mix and 8 golden variants.
- Equivalence harness verifying exact return value, visit order,
cost-stage sequence, best_mv, second_best_mv, and <= 3 scalar tail
calls: 5.7M searches (GCC 15 -O3) and 1.7M searches (clang 19
ASan/UBSan/pointer-overflow).
- Passed ASan, MSan, encoder fuzz test, targeted test_libaom unit
tests (2,457 tests across 13 suites including IntrabcTest,
IntraBCTilesTest, EncodeAPI, EncodeSmallWidthHeight, and C/SSE2/AVX2
SADx4Test/SADSkipx4Test), and -Werror -Wall -Wextra on clang 19 and
GCC 15 across highbd, lowbd (CONFIG_AV1_HIGHBITDEPTH=0),
CONFIG_REALTIME_ONLY=1, and generic C-only builds.
#agy-alpha-evolve
Change-Id: Ie1410355668dd6549e122e818d7886c1f384186d
diff --git a/av1/encoder/mcomp.c b/av1/encoder/mcomp.c
index ec56704ff9..7ad9951620 100644
--- a/av1/encoder/mcomp.c
+++ b/av1/encoder/mcomp.c
@@ -1555,9 +1555,7 @@ static int exhaustive_mesh_search(FULLPEL_MV start_mv,
const struct buf_2d *const ref = ms_params->ms_buffers.ref;
const int ref_stride = ref->stride;
unsigned int best_sad = INT_MAX;
- int r, c, i;
int start_col, end_col, start_row, end_row;
- const int col_step = (step > 1) ? step : 4;
assert(step >= 1);
@@ -1571,46 +1569,84 @@ static int exhaustive_mesh_search(FULLPEL_MV start_mv,
end_row = AOMMIN(range, ms_params->mv_limits.row_max - start_mv.row);
end_col = AOMMIN(range, ms_params->mv_limits.col_max - start_mv.col);
- for (r = start_row; r <= end_row; r += step) {
- for (c = start_col; c <= end_col; c += col_step) {
- // Step > 1 means we are not checking every location in this pass.
- if (step > 1) {
- const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c };
- unsigned int sad = get_mvpred_sad(
- ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
- update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
- /*raw_best_sad=*/NULL, best_mv, second_best_mv);
- } else {
- // 4 sads in a single call if we are checking every location
- if (c + 3 <= end_col) {
- unsigned int sads[4];
- const uint8_t *addrs[4];
- for (i = 0; i < 4; ++i) {
- const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
- addrs[i] = get_buf_from_fullmv(ref, &mv);
- }
-
- ms_params->sdx4df(src->buf, src->stride, addrs, ref_stride, sads);
+ // TODO(jazarwil@google.com): Existing off-by-one inherited from libvpx (the
+ // original step == 1 tail loop used `i < end_col - c`): with step == 1 the
+ // last column is skipped unless the column count is a multiple of 4. Kept
+ // bit-exact here; searching end_col changes the encoder output.
+ const int last_col =
+ (step == 1 && ((end_col - start_col + 1) & 3)) ? end_col - 1 : end_col;
+ if (start_row > end_row || start_col > last_col) return best_sad;
+
+ // Last column at which 4 candidates still fit in the row.
+ const int last_batch_col = last_col - 3 * step;
+ const uint8_t *const src_buf = src->buf;
+ const int src_stride = src->stride;
+ const aom_sad_multi_d_fn_t sdx4df = ms_params->sdx4df;
+ const int step4 = step * 4;
+ const uint8_t *tail_addrs[4];
+ FULLPEL_MV tail_mvs[4];
+ int tail_count = 0;
+
+ for (int r = start_row; r <= end_row; r += step) {
+ const int16_t mv_row = (int16_t)(start_mv.row + r);
+ const FULLPEL_MV row_mv = { mv_row, start_mv.col };
+ const uint8_t *const row_ptr = get_buf_from_fullmv(ref, &row_mv);
+ int c = start_col;
+
+ // Complete any batch carried from the previous row first so candidates are
+ // evaluated in raster order; update_mvs_and_sad() keeps the first MV on
+ // ties and records second_best_mv in visit order.
+ if (tail_count > 0) {
+ while (tail_count < 4 && c <= last_col) {
+ tail_addrs[tail_count] = row_ptr + c;
+ tail_mvs[tail_count].row = mv_row;
+ tail_mvs[tail_count].col = (int16_t)(start_mv.col + c);
+ ++tail_count;
+ c += step;
+ }
+ if (tail_count == 4) {
+ unsigned int sads[4];
+ sdx4df(src_buf, src_stride, tail_addrs, ref_stride, sads);
+ for (int i = 0; i < 4; ++i) {
+ update_mvs_and_sad(sads[i], &tail_mvs[i], mv_cost_params, &best_sad,
+ /*raw_best_sad=*/NULL, best_mv, second_best_mv);
+ }
+ tail_count = 0;
+ }
+ }
- for (i = 0; i < 4; ++i) {
- if (sads[i] < best_sad) {
- const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
- update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad,
- /*raw_best_sad=*/NULL, best_mv,
- second_best_mv);
- }
- }
- } else {
- for (i = 0; i < end_col - c; ++i) {
- const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
- unsigned int sad = get_mvpred_sad(
- ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
- update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
- /*raw_best_sad=*/NULL, best_mv, second_best_mv);
- }
+ for (; c <= last_batch_col; c += step4) {
+ unsigned int sads[4];
+ const uint8_t *const col_ptr = row_ptr + c;
+ const uint8_t *const addrs[4] = { col_ptr, col_ptr + step,
+ col_ptr + 2 * step,
+ col_ptr + 3 * step };
+ sdx4df(src_buf, src_stride, addrs, ref_stride, sads);
+ for (int i = 0; i < 4; ++i) {
+ if (sads[i] < best_sad) {
+ const FULLPEL_MV mv = { mv_row,
+ (int16_t)(start_mv.col + c + i * step) };
+ update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad,
+ /*raw_best_sad=*/NULL, best_mv, second_best_mv);
}
}
}
+
+ assert(c > last_col || tail_count == 0);
+ for (; c <= last_col; c += step) {
+ assert(tail_count < 4);
+ tail_addrs[tail_count] = row_ptr + c;
+ tail_mvs[tail_count].row = mv_row;
+ tail_mvs[tail_count].col = (int16_t)(start_mv.col + c);
+ ++tail_count;
+ }
+ }
+
+ for (int i = 0; i < tail_count; ++i) {
+ const unsigned int sad =
+ get_mvpred_sad(ms_params, src, tail_addrs[i], ref_stride);
+ update_mvs_and_sad(sad, &tail_mvs[i], mv_cost_params, &best_sad,
+ /*raw_best_sad=*/NULL, best_mv, second_best_mv);
}
return best_sad;