Commit daa4d0b46e for aom

commit daa4d0b46e5ee9d2f5e1615044b106c3b8766af3
Author: Frank Galligan <fgalligan@google.com>
Date:   Sat Nov 28 22:19:43 2026 -0800

    av1,tx: precompute per-frame tx_type_prune_mask

    Precompute per-TX_SIZE transform-type pruning bitmasks once per frame
    in av1_init_tx_type_prune_mask() instead of recomputing threshold
    loops for every transform block in get_tx_mask(), falling back to
    max_prob selection only when all allowed types are pruned.

    Verified bit-exact under Release and ASan+UBSan+IntSan across 1T/8T
    allintra (--tune=iq --enable-qm=1), good (cpu-used=2,3,4,6, 8b/10b),
    rt (cpu-used=6), *AVxEncoderThreadTest*, -m32, REALTIME_ONLY=1, and
    HBD=0/QM=0.

    Speedup (AMD EPYC 7B13, 10-rep AB/BA paired, bit-exact):
      - aomenc 720p 10b good cpu-used=3 1T: -0.75% (-1.04% med, sd 0.75%)
      - aomenc 1080p allintra iq 1T: -0.89% (-0.65% median, stdev 1.81%)
      - aomenc 720p good cpu-used=6: -0.49% 1T (sd 1.72%) / -0.46% 8T
    Binary size (x86_64 -O3): libaom.a .text +4.80 KiB (+0.058%).
    Memory: +38 B in AV1_COMP (uint16_t[TX_SIZES_ALL]); no RSS impact.

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

diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 564761c34c..b15b4230a4 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -73,6 +73,7 @@
 #include "av1/encoder/segmentation.h"
 #include "av1/encoder/tokenize.h"
 #include "av1/encoder/tpl_model.h"
+#include "av1/encoder/tx_search.h"
 #include "av1/encoder/var_based_part.h"

 #if CONFIG_TUNE_VMAF
@@ -2714,6 +2715,7 @@ static inline void encode_frame_internal(AV1_COMP *cpi) {
   av1_initialize_rd_consts(cpi);
   av1_set_sad_per_bit(cpi, &x->sadperbit, quant_params->base_qindex);
   populate_thresh_to_force_zeromv_skip(cpi);
+  av1_init_tx_type_prune_mask(cpi);

   enc_row_mt->sync_read_ptr = av1_row_mt_sync_read_dummy;
   enc_row_mt->sync_write_ptr = av1_row_mt_sync_write_dummy;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 073374bf7d..23d6c9f83c 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3391,6 +3391,12 @@ typedef struct AV1_COMP {
    */
   int keep_comp_ref_frame_mask;

+  /*!
+   * Bit i set: tx_type i is pruned (tx_type_probs < thresh). Only valid when
+   * sf.tx_sf.tx_type_search.prune_tx_type_using_stats != 0.
+   */
+  uint16_t tx_type_prune_mask[TX_SIZES_ALL];
+
   /*!
    * Loop Restoration context.
    */
diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index f35c24b58c..707a3f0ebf 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -9,6 +9,7 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <assert.h>
 #include <inttypes.h>

 #include "config/av1_rtcd.h"
@@ -1758,6 +1759,30 @@ static inline const int *get_tx_type_probs(const AV1_COMP *cpi,
   return cpi->ppi->frame_probs.tx_type_probs[update_type][tx_size];
 }

+void av1_init_tx_type_prune_mask(AV1_COMP *cpi) {
+  const int prune_stats =
+      cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats;
+  if (!prune_stats) return;
+  const AV1_PRIMARY *const ppi = cpi->ppi;
+  const FRAME_UPDATE_TYPE update_type =
+      get_frame_update_type(&ppi->gf_group, cpi->gf_frame_index);
+  static const int thresh_arr[2][FRAME_UPDATE_TYPES] = {
+    { 10, 15, 15, 10, 15, 15, 15 }, { 10, 17, 17, 10, 17, 17, 17 }
+  };
+  assert((unsigned)update_type < FRAME_UPDATE_TYPES);
+  assert(prune_stats == 1 || prune_stats == 2);
+  const int thresh = thresh_arr[prune_stats - 1][update_type];
+  for (int tx_size = 0; tx_size < TX_SIZES_ALL; ++tx_size) {
+    const int *const tx_type_probs =
+        get_tx_type_probs(cpi, update_type, (TX_SIZE)tx_size);
+    uint16_t prune = 0;
+    for (int i = 0; i < TX_TYPES; ++i) {
+      if (tx_type_probs[i] < thresh) prune |= (uint16_t)(1u << i);
+    }
+    cpi->tx_type_prune_mask[tx_size] = prune;
+  }
+}
+
 static inline uint16_t get_tx_mask(
     const AV1_COMP *cpi, MACROBLOCK *x, int plane, int block, int blk_row,
     int blk_col, BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
@@ -1859,27 +1884,28 @@ static inline uint16_t get_tx_mask(
     int i;

     if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats) {
-      const FRAME_UPDATE_TYPE update_type =
-          get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
-      const int *const tx_type_probs =
-          get_tx_type_probs(cpi, update_type, tx_size);
-      static const int thresh_arr[2][7] = { { 10, 15, 15, 10, 15, 15, 15 },
-                                            { 10, 17, 17, 10, 17, 17, 17 } };
-      const int thresh =
-          thresh_arr[cpi->sf.tx_sf.tx_type_search.prune_tx_type_using_stats - 1]
-                    [update_type];
-      uint16_t prune = 0;
-      int max_prob = -1;
-      int max_idx = 0;
-      for (i = 0; i < TX_TYPES; i++) {
-        if (tx_type_probs[i] > max_prob && (allowed_tx_mask & (1 << i))) {
-          max_prob = tx_type_probs[i];
-          max_idx = i;
+      assert((unsigned)tx_size < TX_SIZES_ALL);
+      const uint16_t prune = cpi->tx_type_prune_mask[tx_size];
+      const uint16_t unpruned = allowed_tx_mask & (uint16_t)(~prune);
+      if (unpruned != 0) {
+        allowed_tx_mask = unpruned;
+      } else {
+        // If all allowed types are pruned, keep only the most probable allowed
+        // type (matches the previous per-block logic).
+        const FRAME_UPDATE_TYPE update_type =
+            get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
+        const int *const tx_type_probs =
+            get_tx_type_probs(cpi, update_type, tx_size);
+        int max_prob = -1;
+        int max_idx = 0;
+        for (i = 0; i < TX_TYPES; i++) {
+          if (tx_type_probs[i] > max_prob && (allowed_tx_mask & (1 << i))) {
+            max_prob = tx_type_probs[i];
+            max_idx = i;
+          }
         }
-        if (tx_type_probs[i] < thresh) prune |= (1 << i);
+        allowed_tx_mask = (uint16_t)(1 << max_idx);
       }
-      if ((prune >> max_idx) & 0x01) prune &= ~(1 << max_idx);
-      allowed_tx_mask &= (~prune);
     }

     if (cpi->sf.tx_sf.tx_type_search.prune_tx_type_est_rd ||
diff --git a/av1/encoder/tx_search.h b/av1/encoder/tx_search.h
index c3185686cc..837d192cb4 100644
--- a/av1/encoder/tx_search.h
+++ b/av1/encoder/tx_search.h
@@ -197,6 +197,9 @@ int av1_txfm_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
                     RD_STATS *rd_stats, RD_STATS *rd_stats_y,
                     RD_STATS *rd_stats_uv, int mode_rate, int64_t ref_best_rd);

+/*!\brief Precomputes cpi->tx_type_prune_mask[] for the current frame. */
+void av1_init_tx_type_prune_mask(AV1_COMP *cpi);
+
 #if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
     (defined(__GNUC__) || defined(__clang__))
 #pragma GCC visibility pop