Commit bf2011f589 for aom

commit bf2011f5892fdff2f632ad7278c8b5c7790f1a4f
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 26 04:37:53 2026 -0800

    av1,mt: batch CDEF search MT worker jobs

    Fetch up to kCdefBatchSize (4) non-skipped blocks under a single mutex
    acquisition in cdef_get_next_jobs(), bounding batch size dynamically by
    remaining effective superblocks (accounting for BLOCK_128X128 4x 64x64
    grid ratio) and num_workers so small frames and frame tails shrink to 1
    block per lock. Hoist adaptive_cdef_mode out of the inner loop and
    remove volatile from post-setjmp locals (safe under C11 7.13.2.1p3
    since setjmp() returns immediately without reading them). Output stays
    bit-exact.

    Verified bit-exact under Release, ASan+UBSan+IntSan, and TSan builds
    (*AVxEncoderThreadTest*, EncodeSmallWidthHeight, 8T enc,
    AOM_SIMD_CAPS_MASK), plus clang --analyze, GCC HBD=0, and GCC -m32.

    Speedup (AMD EPYC 7B13, 8T, 10-rep AB/BA, bit-exact):
      - avifenc -j 8: -2.92% median (-1.56% paired, stdev 3.07%)
      - aomenc 720p sparse q58 --threads=8: -1.02% paired
      - aomenc 512x512 / 256x256 good --threads=8: -0.87% / -0.31% paired
      - 1T unaffected (av1_cdef_mse_calc_frame_mt requires > 1 worker).
    Binary size (x86_64 -O3): libaom.a .text +0.0 KiB, stripped +0.0 KiB.
    Memory: 0 heap bytes (48 B worker stack arrays).

    TAG=agy
    CONV=4b696004-0036-42b2-b3fa-d1c7d63a973c
    Change-Id: I25bf55220a28a352ee9bf4db4d544ac48439bf0d

diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index cc497fffa6..48b2e393d7 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -3442,42 +3442,59 @@ static inline void cdef_reset_job_info(AV1CdefSync *cdef_sync) {
   cdef_sync->cdef_mt_exit = false;
 }

-// Checks if a job is available. If job is available,
-// populates next job information and returns 1, else returns 0.
-static inline int cdef_get_next_job(AV1CdefSync *cdef_sync,
-                                    CdefSearchCtx *cdef_search_ctx,
-                                    volatile int *cur_fbr,
-                                    volatile int *cur_fbc,
-                                    volatile int *sb_count) {
+// Fetches up to max_jobs non-skipped blocks under a single mutex
+// acquisition, dynamically bounding the batch size by the remaining effective
+// superblocks and num_workers (shrinking to 1 on small frames and near the
+// frame tail). Returns the number of jobs written to cur_fbr, cur_fbc, and
+// sb_count (0 at end of frame or after an error exit).
+static inline int cdef_get_next_jobs(AV1CdefSync *cdef_sync,
+                                     CdefSearchCtx *cdef_search_ctx,
+                                     int *cur_fbr, int *cur_fbc, int *sb_count,
+                                     int max_jobs, int num_workers,
+                                     BLOCK_SIZE sb_size) {
+  assert(max_jobs > 0);
+  assert(num_workers > 0);
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
 #if CONFIG_MULTITHREAD
   pthread_mutex_lock(cdef_sync->mutex_);
 #endif  // CONFIG_MULTITHREAD
-  int do_next_block = 0;
+  int n = 0;
   const int nvfb = cdef_search_ctx->nvfb;
   const int nhfb = cdef_search_ctx->nhfb;

-  // If a block is skip, do not process the block and
-  // check the skip condition for the next block.
-  while (!cdef_sync->cdef_mt_exit && !cdef_sync->end_of_frame &&
-         cdef_sb_skip(cdef_search_ctx->mi_params, cdef_sync->fbr,
-                      cdef_sync->fbc)) {
-    update_next_job_info(cdef_sync, nvfb, nhfb);
-  }
+  if (!cdef_sync->cdef_mt_exit) {
+    const int sb_shift = (sb_size == BLOCK_128X128) ? 2 : 0;
+    const int sb_round = (1 << sb_shift) - 1;
+    const int two_workers = 2 * num_workers;
+    while (n < max_jobs && !cdef_sync->end_of_frame) {
+      if (n > 0) {
+        const int remaining = (nvfb - cdef_sync->fbr) * nhfb - cdef_sync->fbc;
+        const int eff_remaining = (remaining + sb_round) >> sb_shift;
+        if (eff_remaining < (n + 1) * two_workers) break;
+      }

-  // Populates information needed for current job and update the row,
-  // column indices of the next block to be processed.
-  if (!cdef_sync->cdef_mt_exit && cdef_sync->end_of_frame == 0) {
-    do_next_block = 1;
-    *cur_fbr = cdef_sync->fbr;
-    *cur_fbc = cdef_sync->fbc;
-    *sb_count = cdef_search_ctx->sb_count;
-    cdef_search_ctx->sb_count++;
-    update_next_job_info(cdef_sync, nvfb, nhfb);
+      // If a block is skip, do not process the block and
+      // check the skip condition for the next block.
+      if (cdef_sb_skip(cdef_search_ctx->mi_params, cdef_sync->fbr,
+                       cdef_sync->fbc)) {
+        update_next_job_info(cdef_sync, nvfb, nhfb);
+        continue;
+      }
+
+      // Populates information needed for current job and update the row,
+      // column indices of the next block to be processed.
+      cur_fbr[n] = cdef_sync->fbr;
+      cur_fbc[n] = cdef_sync->fbc;
+      sb_count[n] = cdef_search_ctx->sb_count;
+      cdef_search_ctx->sb_count++;
+      n++;
+      update_next_job_info(cdef_sync, nvfb, nhfb);
+    }
   }
 #if CONFIG_MULTITHREAD
   pthread_mutex_unlock(cdef_sync->mutex_);
 #endif  // CONFIG_MULTITHREAD
-  return do_next_block;
+  return n;
 }

 // Hook function for each thread in CDEF search multi-threading.
@@ -3505,12 +3522,24 @@ static int cdef_filter_block_worker_hook(void *arg1, void *arg2) {
   }
   error_info->setjmp = 1;

-  volatile int cur_fbr, cur_fbc, sb_count;
-  while (cdef_get_next_job(cdef_sync, cdef_search_ctx, &cur_fbr, &cur_fbc,
-                           &sb_count)) {
-    av1_cdef_mse_calc_block(cdef_search_ctx, error_info, cur_fbr, cur_fbc,
-                            sb_count,
-                            thread_data->cpi->sf.lpf_sf.adaptive_cdef_mode);
+  // These locals are not read after longjmp() (the setjmp() branch above
+  // returns immediately), so they need not be volatile.
+  enum { kCdefBatchSize = 4 };
+  int cur_fbr[kCdefBatchSize];
+  int cur_fbc[kCdefBatchSize];
+  int sb_count[kCdefBatchSize];
+  const int adaptive_cdef_mode = thread_data->cpi->sf.lpf_sf.adaptive_cdef_mode;
+  const int num_workers =
+      thread_data->cpi->mt_info.num_mod_workers[MOD_CDEF_SEARCH];
+  const BLOCK_SIZE sb_size = thread_data->cpi->common.seq_params->sb_size;
+  int n;
+  while ((n = cdef_get_next_jobs(cdef_sync, cdef_search_ctx, cur_fbr, cur_fbc,
+                                 sb_count, kCdefBatchSize, num_workers,
+                                 sb_size)) > 0) {
+    for (int i = 0; i < n; ++i) {
+      av1_cdef_mse_calc_block(cdef_search_ctx, error_info, cur_fbr[i],
+                              cur_fbc[i], sb_count[i], adaptive_cdef_mode);
+    }
   }
   error_info->setjmp = 0;
   return 1;