Commit 77dfdef95c for aom

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

    av1: Unsigned clamped cost and skip tables

    Use unsigned shift and index arithmetic in av1_cost_symbol_clamped,
    skip duplicate coeff cost fills and unused intra-only mode cost tables
    in rd.c (lazily backfilling the deferred default-CDF inter mode costs
    on the next inter frame if frame-level mode cost update is skipped),
    and guard seg_tmp_pred_cost and temporal_update on
    primary_ref_frame != PRIMARY_REF_NONE.

    Speedup: -0.68% (1T) / -0.44% (8T) AVIF allintra speed 6 encode time
    reduction on x86_64 (bit-exact).

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

diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 97cf2e9460..517c886a51 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -759,6 +759,11 @@ typedef struct {
   //! spatial_pred_cost
   int spatial_pred_cost[SPATIAL_PREDICTION_PROBS][MAX_SEGMENTS];
   /**@}*/
+
+  //! Whether intra-only mode rate fill deferred default inter cost tables.
+  uint8_t intra_only_skipped_inter_costs;
+  //! Whether intra-only mode rate fill also deferred inter_tx_type_costs.
+  uint8_t intra_only_skipped_inter_tx_type;
 } ModeCosts;

 /*! \brief Holds mv costs for encoding and motion search.
diff --git a/av1/encoder/cost.h b/av1/encoder/cost.h
index 5b108050b2..e2f068217b 100644
--- a/av1/encoder/cost.h
+++ b/av1/encoder/cost.h
@@ -12,6 +12,8 @@
 #ifndef AOM_AV1_ENCODER_COST_H_
 #define AOM_AV1_ENCODER_COST_H_

+#include <assert.h>
+
 #include "aom_dsp/entcode.h"
 #include "aom_dsp/prob.h"
 #include "aom/aom_integer.h"
@@ -36,12 +38,12 @@ extern uint16_t av1_cost_lut_low[COST_LUT_LOW];  // filled by av1_init_cost_luts

 static inline int av1_cost_symbol_clamped(int p15) {
   assert(0 < p15 && p15 < CDF_PROB_TOP);
-  const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
   // Since CDF_PROB_TOP == 32768 and av1_prob_cost has 129 entries (including a
   // duplicate 128th entry for prob_idx == 128), get_prob(p15 << shift, 32768) -
   // 128 simplifies to ((p15 << shift) - 16320) >> 7.
-  const int prob_idx = ((p15 << shift) - 16320) >> 7;
-  return av1_prob_cost[prob_idx] + av1_cost_literal(shift);
+  const unsigned int shift = (unsigned int)(CDF_PROB_BITS - 1 - get_msb(p15));
+  const unsigned int prob_idx = (((unsigned int)p15 << shift) - 16320U) >> 7;
+  return av1_prob_cost[prob_idx] + (int)av1_cost_literal(shift);
 }

 // Calculate the cost of a symbol with probability p15 / 2^15
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index a70b9cb6e7..2f8f6e65dc 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -2847,9 +2847,9 @@ static inline void encode_frame_internal(AV1_COMP *cpi) {
   }

   if (cm->seg.enabled) {
-    cm->seg.temporal_update = 1;
-    if (rdc->seg_tmp_pred_cost[0] < rdc->seg_tmp_pred_cost[1])
-      cm->seg.temporal_update = 0;
+    cm->seg.temporal_update =
+        cm->features.primary_ref_frame != PRIMARY_REF_NONE &&
+        rdc->seg_tmp_pred_cost[0] >= rdc->seg_tmp_pred_cost[1];
   }

   if (cpi->sf.inter_sf.prune_obmc_prob_thresh > 0 &&
diff --git a/av1/encoder/encodeframe_utils.c b/av1/encoder/encodeframe_utils.c
index 4c3d537444..7a3fbcddf2 100644
--- a/av1/encoder/encodeframe_utils.c
+++ b/av1/encoder/encodeframe_utils.c
@@ -387,7 +387,8 @@ void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
     if (mi_addr->uv_mode == UV_CFL_PRED && !is_cfl_allowed(xd))
       mi_addr->uv_mode = UV_DC_PRED;

-    if (!dry_run && !mi_addr->skip_txfm) {
+    if (!dry_run && !mi_addr->skip_txfm &&
+        cm->features.primary_ref_frame != PRIMARY_REF_NONE) {
       int cdf_num;
       const uint8_t spatial_pred = av1_get_spatial_seg_pred(
           cm, xd, &cdf_num, cpi->cyclic_refresh->skip_over4x4);
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 23f5e77764..c21fc53a84 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -13,6 +13,7 @@
 #include <limits.h>
 #include <math.h>
 #include <stdio.h>
+#include <string.h>

 #include "aom_dsp/aom_dsp_common.h"
 #include "aom_mem/aom_mem.h"
@@ -80,9 +81,40 @@ static const int av1_ext_tx_set_idx_to_type[2][AOMMAX(EXT_TX_SETS_INTRA,
   },
 };

+static void fill_inter_tx_type_costs(ModeCosts *mode_costs,
+                                     const FRAME_CONTEXT *fc) {
+  for (int i = TX_4X4; i < EXT_TX_SIZES; ++i) {
+    for (int s = 1; s < EXT_TX_SETS_INTER; ++s) {
+      if (use_inter_ext_tx_for_txsize[s][i]) {
+        av1_cost_tokens_from_cdf(
+            mode_costs->inter_tx_type_costs[s][i], fc->inter_ext_tx_cdf[s][i],
+            av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
+      }
+    }
+  }
+}
+
+// Populates the three mode cost tables deferred on intra-only frames.
+static void fill_deferred_inter_mode_rates(ModeCosts *mode_costs,
+                                           const FRAME_CONTEXT *fc,
+                                           int fill_inter_tx_type) {
+  mode_costs->intra_only_skipped_inter_costs = 0;
+  mode_costs->intra_only_skipped_inter_tx_type = 0;
+  for (int i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
+    av1_cost_tokens_from_cdf(mode_costs->switchable_interp_costs[i],
+                             fc->switchable_interp_cdf[i], NULL);
+  }
+  if (fill_inter_tx_type) fill_inter_tx_type_costs(mode_costs, fc);
+  for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) {
+    av1_cost_tokens_from_cdf(mode_costs->tmp_pred_cost[i], fc->seg.pred_cdf[i],
+                             NULL);
+  }
+}
+
 void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
                          FRAME_CONTEXT *fc) {
   int i, j;
+  const int is_intra_only = frame_is_intra_only(cm);

   for (i = 0; i < PARTITION_CONTEXTS; ++i)
     av1_cost_tokens_from_cdf(mode_costs->partition_cost[i],
@@ -123,10 +155,6 @@ void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
                                fc->filter_intra_cdfs[i], NULL);
   }

-  for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
-    av1_cost_tokens_from_cdf(mode_costs->switchable_interp_costs[i],
-                             fc->switchable_interp_cdf[i], NULL);
-
   for (i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
     av1_cost_tokens_from_cdf(mode_costs->palette_y_size_cost[i],
                              fc->palette_y_size_cdf[i], NULL);
@@ -183,16 +211,13 @@ void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
                              fc->txfm_partition_cdf[i], NULL);
   }

+  const int allow_intrabc = av1_allow_intrabc(cm);
+  if (allow_intrabc) {
+    fill_inter_tx_type_costs(mode_costs, fc);
+    av1_cost_tokens_from_cdf(mode_costs->intrabc_cost, fc->intrabc_cdf, NULL);
+  }
   for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
-    int s;
-    for (s = 1; s < EXT_TX_SETS_INTER; ++s) {
-      if (use_inter_ext_tx_for_txsize[s][i]) {
-        av1_cost_tokens_from_cdf(
-            mode_costs->inter_tx_type_costs[s][i], fc->inter_ext_tx_cdf[s][i],
-            av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
-      }
-    }
-    for (s = 1; s < EXT_TX_SETS_INTRA; ++s) {
+    for (int s = 1; s < EXT_TX_SETS_INTRA; ++s) {
       if (use_intra_ext_tx_for_txsize[s][i]) {
         for (j = 0; j < INTRA_MODES; ++j) {
           av1_cost_tokens_from_cdf(
@@ -207,19 +232,17 @@ void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
     av1_cost_tokens_from_cdf(mode_costs->angle_delta_cost[i],
                              fc->angle_delta_cdf[i], NULL);
   }
-  av1_cost_tokens_from_cdf(mode_costs->intrabc_cost, fc->intrabc_cdf, NULL);
-
   for (i = 0; i < SPATIAL_PREDICTION_PROBS; ++i) {
     av1_cost_tokens_from_cdf(mode_costs->spatial_pred_cost[i],
                              fc->seg.spatial_pred_seg_cdf[i], NULL);
   }

-  for (i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) {
-    av1_cost_tokens_from_cdf(mode_costs->tmp_pred_cost[i], fc->seg.pred_cdf[i],
-                             NULL);
-  }
+  if (is_intra_only) {
+    mode_costs->intra_only_skipped_inter_costs = 1;
+    mode_costs->intra_only_skipped_inter_tx_type = (uint8_t)!allow_intrabc;
+  } else {
+    fill_deferred_inter_mode_rates(mode_costs, fc, /*fill_inter_tx_type=*/1);

-  if (!frame_is_intra_only(cm)) {
     for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
       av1_cost_tokens_from_cdf(mode_costs->comp_inter_cost[i],
                                fc->comp_inter_cdf[i], NULL);
@@ -677,41 +700,51 @@ void av1_fill_coeff_costs(CoeffCosts *coeff_costs, FRAME_CONTEXT *fc,
                                    NULL);
       }

-      for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
-        av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
-                                 fc->dc_sign_cdf[plane][ctx], NULL);
-
-      int br_rate_all[LEVEL_CONTEXTS][BR_CDF_SIZE];
-      if (is_realtime) {
-        av1_cost_tokens_from_cdf_4_batch(
-            br_rate_all[0], BR_CDF_SIZE,
-            fc->coeff_br_cdf[AOMMIN(tx_size, TX_32X32)][plane][0],
-            CDF_SIZE(BR_CDF_SIZE), LEVEL_CONTEXTS);
+      if (tx_size == TX_4X4) {
+        for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
+          av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
+                                   fc->dc_sign_cdf[plane][ctx], NULL);
       } else {
-        for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
-          av1_cost_tokens_from_cdf(
-              br_rate_all[ctx],
-              fc->coeff_br_cdf[AOMMIN(tx_size, TX_32X32)][plane][ctx], NULL);
+        memcpy(pcost->dc_sign_cost,
+               coeff_costs->coeff_costs[TX_4X4][plane].dc_sign_cost,
+               sizeof(pcost->dc_sign_cost));
       }
-      for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
-        const int *br_rate = br_rate_all[ctx];
-        int prev_cost = 0;
-        int i, j;
-        for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
-          for (j = 0; j < BR_CDF_SIZE - 1; j++) {
-            pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
+
+      if (tx_size <= TX_32X32) {
+        int br_rate_all[LEVEL_CONTEXTS][BR_CDF_SIZE];
+        if (is_realtime) {
+          av1_cost_tokens_from_cdf_4_batch(
+              br_rate_all[0], BR_CDF_SIZE, fc->coeff_br_cdf[tx_size][plane][0],
+              CDF_SIZE(BR_CDF_SIZE), LEVEL_CONTEXTS);
+        } else {
+          for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
+            av1_cost_tokens_from_cdf(
+                br_rate_all[ctx], fc->coeff_br_cdf[tx_size][plane][ctx], NULL);
+        }
+        for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
+          const int *br_rate = br_rate_all[ctx];
+          int prev_cost = 0;
+          int i, j;
+          for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
+            for (j = 0; j < BR_CDF_SIZE - 1; j++) {
+              pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
+            }
+            prev_cost += br_rate[j];
           }
-          prev_cost += br_rate[j];
+          pcost->lps_cost[ctx][i] = prev_cost;
         }
-        pcost->lps_cost[ctx][i] = prev_cost;
-      }
-      for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
-        pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
-            pcost->lps_cost[ctx][0];
-        for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
-          pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
-              pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
+        for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
+          pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
+              pcost->lps_cost[ctx][0];
+          for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
+            pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
+                pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
+          }
         }
+      } else {
+        memcpy(pcost->lps_cost,
+               coeff_costs->coeff_costs[TX_32X32][plane].lps_cost,
+               sizeof(pcost->lps_cost));
       }
     }
   }
@@ -881,8 +914,17 @@ void av1_initialize_rd_consts(AV1_COMP *cpi) {
   // Frame level mode cost update
   if (should_force_mode_cost_update(cpi) ||
       is_frame_level_cost_upd_freq_set(cm, inter_sf->mode_cost_upd_level,
-                                       use_nonrd_pick_mode, frames_since_key))
+                                       use_nonrd_pick_mode, frames_since_key)) {
     av1_fill_mode_rates(cm, &x->mode_costs, cm->fc);
+  } else if (!frame_is_intra_only(cm) &&
+             x->mode_costs.intra_only_skipped_inter_costs) {
+    const FRAME_CONTEXT *const default_fc =
+        cm->default_frame_context->initialized ? cm->default_frame_context
+                                               : cm->fc;
+    fill_deferred_inter_mode_rates(
+        &x->mode_costs, default_fc,
+        x->mode_costs.intra_only_skipped_inter_tx_type);
+  }

   // Frame level dv cost update
   if (av1_need_dv_costs(cpi)) {