Commit a3a35a0803 for aom

commit a3a35a0803e9c15c7723144eb3345374210303cc
Author: Frank Galligan <fgalligan@google.com>
Date:   Fri Nov 13 10:43:54 2026 -0800

    av1: Optimize eob_facade and tx_size helpers

    Add fast update_coeff_eob_facade specialization in txb_rdopt, replace
    adjusted-tx-size lookups in txb_common.h with compact lookup tables,
    remove the now-unused get_lower_levels_ctx_fast helper, and gate
    internal encoder header hidden visibility behind
    AOM_INTERNAL_HIDDEN_VISIBILITY on ELF targets.

    Speedup: -0.92% (1T) / -0.55% (8T) encode time reduction (bit-exact).

    TAG=agy
    CONV=b929a630-a6cd-45a0-979c-1767c30178ee
    Change-Id: I6c1c1cabfe15222ebb3eb9327642b8d22e80049b

diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index 7c1c68531b..ff82f04dee 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -12,6 +12,9 @@
 #ifndef AOM_AV1_COMMON_TXB_COMMON_H_
 #define AOM_AV1_COMMON_TXB_COMMON_H_

+#include <assert.h>
+#include <stdint.h>
+
 #include "av1/common/av1_common_int.h"

 #ifdef __cplusplus
@@ -56,18 +59,27 @@ static const TX_CLASS tx_type_to_class[TX_TYPES] = {
 };

 static inline int get_txb_bhl(TX_SIZE tx_size) {
-  tx_size = av1_get_adjusted_tx_size(tx_size);
-  return tx_size_high_log2[tx_size];
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  static const uint8_t txb_bhl_tab[TX_SIZES_ALL] = { 2, 3, 4, 5, 5, 3, 2,
+                                                     4, 3, 5, 4, 5, 5, 4,
+                                                     2, 5, 3, 5, 4 };
+  return txb_bhl_tab[tx_size];
 }

 static inline int get_txb_wide(TX_SIZE tx_size) {
-  tx_size = av1_get_adjusted_tx_size(tx_size);
-  return tx_size_wide[tx_size];
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  static const uint8_t txb_wide_tab[TX_SIZES_ALL] = { 4,  8,  16, 32, 32, 4,  8,
+                                                      8,  16, 16, 32, 32, 32, 4,
+                                                      16, 8,  32, 16, 32 };
+  return txb_wide_tab[tx_size];
 }

 static inline int get_txb_high(TX_SIZE tx_size) {
-  tx_size = av1_get_adjusted_tx_size(tx_size);
-  return tx_size_high[tx_size];
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  static const uint8_t txb_high_tab[TX_SIZES_ALL] = {
+    4, 8, 16, 32, 32, 8, 4, 16, 8, 32, 16, 32, 32, 16, 4, 32, 8, 32, 16
+  };
+  return txb_high_tab[tx_size];
 }

 static inline uint8_t *set_levels(uint8_t *const levels_buf, const int height) {
@@ -317,39 +329,6 @@ static AOM_FORCE_INLINE int get_lower_levels_ctx(const uint8_t *levels,
   return get_nz_map_ctx_from_stats(stats, coeff_idx, bhl, tx_size, tx_class);
 }

-static AOM_FORCE_INLINE int get_lower_levels_ctx_fast(
-    const uint8_t *levels, int coeff_idx, int bhl, intptr_t stride,
-    const int8_t *nz_map_ctx_offset, TX_CLASS tx_class) {
-  assert(bhl >= MIN_TX_SIZE_LOG2 && bhl <= MAX_TX_SIZE_LOG2 - 1);
-  assert(stride == ((intptr_t)1 << bhl) + TX_PAD_HOR);
-  const uint8_t *const nb = levels + get_padded_idx(coeff_idx, bhl);
-  const uint8_t *const lvs = nb + stride;
-  if (tx_class == TX_CLASS_2D) {
-    if (coeff_idx == 0) return 0;
-    const int offset = nz_map_ctx_offset[coeff_idx];
-    if ((nb[1] | nb[2] | lvs[0] | lvs[1] | lvs[stride]) == 0) return offset;
-    const int mag = clip_max3[lvs[0]] + clip_max3[nb[1]] + clip_max3[lvs[1]] +
-                    clip_max3[lvs[stride]] + clip_max3[nb[2]];
-    return offset + AOMMIN((mag + 1) >> 1, 4);
-  } else if (tx_class == TX_CLASS_VERT) {
-    const int offset =
-        nz_map_ctx_offset_1d[coeff_idx & ((int)stride - (TX_PAD_HOR + 1))];
-    if ((lvs[0] | nb[1] | nb[2] | nb[3] | nb[4]) == 0) return offset;
-    const int mag = clip_max3[lvs[0]] + clip_max3[nb[1]] + clip_max3[nb[2]] +
-                    clip_max3[nb[3]] + clip_max3[nb[4]];
-    return offset + AOMMIN((mag + 1) >> 1, 4);
-  } else {
-    assert(tx_class == TX_CLASS_HORIZ);
-    const uint8_t *const lvs2 = lvs + stride;
-    const int offset = nz_map_ctx_offset_1d[coeff_idx >> bhl];
-    if ((lvs[0] | nb[1] | lvs2[0] | lvs2[stride] | lvs2[stride * 2]) == 0)
-      return offset;
-    const int mag = clip_max3[lvs[0]] + clip_max3[nb[1]] + clip_max3[lvs2[0]] +
-                    clip_max3[lvs2[stride]] + clip_max3[lvs2[stride * 2]];
-    return offset + AOMMIN((mag + 1) >> 1, 4);
-  }
-}
-
 // Specialized variant for AC coefficients (coeff_idx > 0), skipping the
 // redundant coeff_idx == 0 checks and dispatching on tx_class once.
 static AOM_FORCE_INLINE int get_lower_levels_ctx_simple_fast(
diff --git a/av1/encoder/av1_fwd_txfm1d.h b/av1/encoder/av1_fwd_txfm1d.h
index 4d4aa32169..7697980596 100644
--- a/av1/encoder/av1_fwd_txfm1d.h
+++ b/av1/encoder/av1_fwd_txfm1d.h
@@ -18,6 +18,11 @@
 extern "C" {
 #endif

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility push(hidden)
+#endif
+
 void av1_fdct4(const int32_t *input, int32_t *output, int8_t cos_bit,
                const int8_t *stage_range);
 void av1_fdct8(const int32_t *input, int32_t *output, int8_t cos_bit,
@@ -42,6 +47,12 @@ void av1_fidentity16_c(const int32_t *input, int32_t *output, int8_t cos_bit,
                        const int8_t *stage_range);
 void av1_fidentity32_c(const int32_t *input, int32_t *output, int8_t cos_bit,
                        const int8_t *stage_range);
+
+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility pop
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/av1/encoder/encodemb.h b/av1/encoder/encodemb.h
index a6331f0f16..0c9f1f5ba2 100644
--- a/av1/encoder/encodemb.h
+++ b/av1/encoder/encodemb.h
@@ -66,6 +66,11 @@ struct encode_b_args {
   TRELLIS_OPT_TYPE enable_optimize_b;
 };

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility push(hidden)
+#endif
+
 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
                    RUN_TYPE dry_run);

@@ -342,6 +347,11 @@ static const uint16_t dc_coeff_scale[TX_SIZES_ALL] = {
   2896, 0,    0,    2048, 2048, 4096, 4096, 0,    0
 };

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility pop
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/hybrid_fwd_txfm.h b/av1/encoder/hybrid_fwd_txfm.h
index 32fcc434d9..2d40d68efd 100644
--- a/av1/encoder/hybrid_fwd_txfm.h
+++ b/av1/encoder/hybrid_fwd_txfm.h
@@ -21,6 +21,11 @@
 extern "C" {
 #endif

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility push(hidden)
+#endif
+
 void av1_highbd_fwd_txfm(const int16_t *src_diff, tran_low_t *coeff,
                          int diff_stride, TxfmParam *txfm_param);

@@ -41,6 +46,12 @@ static inline void av1_fwd_txfm(const int16_t *src_diff, tran_low_t *coeff,
  */
 void av1_quick_txfm(int use_hadamard, TX_SIZE tx_size, BitDepthInfo bd_info,
                     const int16_t *src_diff, int src_stride, tran_low_t *coeff);
+
+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility pop
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/rdopt.h b/av1/encoder/rdopt.h
index 655bbda99e..9682ab70a9 100644
--- a/av1/encoder/rdopt.h
+++ b/av1/encoder/rdopt.h
@@ -35,6 +35,11 @@ struct TileInfo;
 struct macroblock;
 struct RD_STATS;

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility push(hidden)
+#endif
+
 /*!\brief AV1 intra mode selection for intra frames.
  *
  * \ingroup intra_mode_search
@@ -353,6 +358,11 @@ static inline void av1_copy_mbmi_ext_to_mbmi_ext_frame(
          sizeof(mbmi_ext->global_mvs));
 }

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility pop
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/tx_search.h b/av1/encoder/tx_search.h
index a16d549da9..c3185686cc 100644
--- a/av1/encoder/tx_search.h
+++ b/av1/encoder/tx_search.h
@@ -33,6 +33,11 @@ enum {
   FTXS_USE_TRANSFORM_DOMAIN = 1 << 2
 } UENUM1BYTE(FAST_TX_SEARCH_MODE);

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility push(hidden)
+#endif
+
 static inline int tx_size_cost(const MACROBLOCK *const x, BLOCK_SIZE bsize,
                                TX_SIZE tx_size) {
   assert(bsize == x->e_mbd.mi[0]->bsize);
@@ -192,6 +197,11 @@ 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);

+#if defined(AOM_INTERNAL_HIDDEN_VISIBILITY) && defined(__ELF__) && \
+    (defined(__GNUC__) || defined(__clang__))
+#pragma GCC visibility pop
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index 8d6a71cfd2..6bf3a48e51 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -74,7 +74,10 @@ static AOM_FORCE_INLINE void update_coeff_dc(
     tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels,
     const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
   const tran_low_t qc = qcoeff[0];
-  const int coeff_ctx = get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
+  const int coeff_ctx =
+      (tx_class == TX_CLASS_2D)
+          ? 0
+          : get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
   if (qc == 0) {
     *accu_rate += txb_costs->base_cost[coeff_ctx][0];
   } else {
@@ -524,23 +527,37 @@ static AOM_FORCE_INLINE void update_coeff_eob_facade(
   const int8_t *const nz_map_ctx =
       (tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
   const int (*base_cost)[8] = txb_costs->base_cost;
-  while (s >= 0 && ctx.nz_num <= max_nz_num) {
+  while (s >= 1 && ctx.nz_num <= max_nz_num) {
     int ci;
     tran_low_t qc;
     int coeff_ctx;
     do {
       ci = scan[s];
-      coeff_ctx = get_lower_levels_ctx_fast(levels, ci, bhl, stride, nz_map_ctx,
-                                            tx_class);
+      coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
+                                                   nz_map_ctx, tx_class);
       qc = qcoeff[ci];
       if (qc != 0) break;
       local_rate += base_cost[coeff_ctx][0];
-    } while (--s >= 0);
-    if (s < 0) break;
+    } while (--s >= 1);
+    if (s < 1) break;
     ctx.accu_rate = local_rate;
     local_rate = update_coeff_eob_nz(s, ci, qc, coeff_ctx, tx_class, &ctx);
     --s;
   }
+  if (s == 0 && ctx.nz_num <= max_nz_num) {
+    const int coeff_ctx =
+        (tx_class == TX_CLASS_2D)
+            ? 0
+            : get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
+    const tran_low_t qc = qcoeff[0];
+    if (qc == 0) {
+      local_rate += txb_costs->base_cost[coeff_ctx][0];
+    } else {
+      ctx.accu_rate = local_rate;
+      local_rate = update_coeff_eob_nz(0, 0, qc, coeff_ctx, tx_class, &ctx);
+    }
+    s = -1;
+  }
   *accu_rate = local_rate;
   *accu_dist = ctx.accu_dist;
   *eob = ctx.eob;
@@ -679,46 +696,38 @@ static int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x,
     --si;
   }

-#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                              \
-  case tx_class_literal:                                                     \
-    update_coeff_eob_facade(                                                 \
-        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,          \
-        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift,   \
-        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,    \
-        levels, sharpness, iqmatrix, qmatrix, max_nz_num, is_noise_pattern); \
+#define UPDATE_COEFF_PASSES_CASE(tx_class_literal)                             \
+  case tx_class_literal:                                                       \
+    update_coeff_eob_facade(                                                   \
+        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,            \
+        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift,     \
+        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,      \
+        levels, sharpness, iqmatrix, qmatrix, max_nz_num, is_noise_pattern);   \
+    if (si >= 1) {                                                             \
+      update_coeff_simple_facade(                                              \
+          &accu_rate, &si, eob, tx_size, tx_class_literal, bhl, rdmult, shift, \
+          dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff, levels,           \
+          sharpness, iqmatrix, qmatrix);                                       \
+    }                                                                          \
     break
   if (si >= 0) {
     switch (tx_class) {
-      UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
-      UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
-      UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
-#undef UPDATE_COEFF_EOB_CASE
+      UPDATE_COEFF_PASSES_CASE(TX_CLASS_2D);
+      UPDATE_COEFF_PASSES_CASE(TX_CLASS_HORIZ);
+      UPDATE_COEFF_PASSES_CASE(TX_CLASS_VERT);
+#undef UPDATE_COEFF_PASSES_CASE
       default: assert(false);
     }
   }

-  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
-    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
-                non_skip_cost, qcoeff, dqcoeff);
-  } else if (si >= 1) {
-#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                            \
-  case tx_class_literal:                                                      \
-    update_coeff_simple_facade(&accu_rate, &si, eob, tx_size,                 \
-                               tx_class_literal, bhl, rdmult, shift, dequant, \
-                               scan, txb_costs, tcoeff, qcoeff, dqcoeff,      \
-                               levels, sharpness, iqmatrix, qmatrix);         \
-    break
-    switch (tx_class) {
-      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
-      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
-      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
-#undef UPDATE_COEFF_SIMPLE_CASE
-      default: assert(false);
+  if (si == -1) {
+    if (nz_num <= max_nz_num && sharpness == 0) {
+      update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
+                  non_skip_cost, qcoeff, dqcoeff);
     }
-  }
-
-  // DC position
-  if (si == 0) {
+  } else {
+    assert(si == 0);
+    // DC position (si == 0)
     // no need to update accu_dist because it's not used after this point
     update_coeff_dc(&accu_rate, tx_size, tx_class, bhl, rdmult, shift,
                     txb_ctx->dc_sign_ctx, dequant, txb_costs, tcoeff, qcoeff,