Commit d071207e41 for aom

commit d071207e41cd5e3e2e394e90f0d6899b47bc179b
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 5 12:38:16 2026 -0800

    txb_rdopt: split trellis try-lower & scan paths

    Outline cold try-lower and coeff>1 eob paths in av1_optimize_txb_multi()
    into `update_coeff_simple_gt1_try_lower`, `update_coeff_eob_gt1`, and
    `update_coeff_eob_eq1` (`AOM_NOINLINE`), inline `lps_cost` lookups for
    `abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE`, skip `accu_dist`
    updates when `need_accu_dist` is false, and replace the inner scan loop
    with a `scan_p` pointer walk in `update_coeff_simple_facade()`.

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, x86_64 AVX2,
    bit-exact):
      -3.19% 720p good (1T), -1.28% (1T) / -2.11% (8T) 1080p allintra,
      -0.19% 720p allintra (8T).
    Binary size (x86_64 -O3): libaom.a .text +1.9 KiB (+0.03%),
      .rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 1T -1028 KiB (-2.42%),
      8T +492 KiB (+1.17%).

    TAG=agy
    CONV=b929a630-a6cd-45a0-979c-1767c30178ee

    Change-Id: I0e446df335c883afd1b129c9ce5e286c395d36aa

diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index be698df8c1..3dd71f3c9c 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -9,10 +9,13 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <assert.h>
+
 #include "av1/encoder/txb_rdopt.h"
 #include "av1/encoder/txb_rdopt_utils.h"
 #include "av1/encoder/rdopt_utils.h"

+#include "aom_dsp/aom_dsp_common.h"
 #include "aom_ports/mem.h"
 #include "av1/common/idct.h"

@@ -23,7 +26,7 @@ static AOM_FORCE_INLINE void update_coeff_last(
     int sign, TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift,
     const int16_t *dequant, const LV_MAP_COEFF_COST *txb_costs,
     const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
-    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
+    const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int need_accu_dist) {
   assert(si > 0 && ci > 0);
   assert(abs_qc >= 2);
   const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
@@ -39,9 +42,15 @@ static AOM_FORCE_INLINE void update_coeff_last(
     const int br_ctx = get_br_ctx_eob(ci, bhl, tx_class);
     const int *const lps_cost = txb_costs->lps_cost[br_ctx];
     const int base_rate = base_eob[2] + av1_cost_literal(1);
-    rate = base_rate + get_br_cost(abs_qc, lps_cost);
-    rate_low = (abs_qc == 3) ? (base_eob[1] + av1_cost_literal(1))
-                             : (base_rate + get_br_cost(abs_qc_low, lps_cost));
+    if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
+      rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
+      rate_low = (abs_qc == 3)
+                     ? (base_eob[1] + av1_cost_literal(1))
+                     : (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
+    } else {
+      rate = base_rate + get_br_cost(abs_qc, lps_cost);
+      rate_low = base_rate + get_br_cost(abs_qc_low, lps_cost);
+    }
   }

   int dqv = dequant[1];
@@ -52,29 +61,36 @@ static AOM_FORCE_INLINE void update_coeff_last(
   get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);

   int64_t dist_diff_0, dist_diff_low_0;
+  int64_t scaled_qm = 0;
   if (qmatrix == NULL) {
     const int64_t tqc2 = (int64_t)tqc * 2;
     dist_diff_0 = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
     dist_diff_low_0 =
         ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
   } else {
-    const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
-    const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
-    dist_diff_0 = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
-    dist_diff_low_0 = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
+    scaled_qm = (int64_t)qmatrix[ci] << shift;
+    dist_diff_0 = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm);
+    dist_diff_low_0 = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm);
   }

   const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
   const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);

+  int64_t chosen_dist;
   if (rd_low < rd) {
     qcoeff[ci] = qc_low;
     dqcoeff[ci] = dqc_low;
     *accu_rate += rate_low;
-    *accu_dist += dist_diff_low_0;
+    chosen_dist = dist_diff_low_0;
   } else {
     *accu_rate += rate;
-    *accu_dist += dist_diff_0;
+    chosen_dist = dist_diff_0;
+  }
+  if (need_accu_dist) {
+    if (qmatrix != NULL) {
+      chosen_dist -= get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
+    }
+    *accu_dist += chosen_dist;
   }
 }

@@ -208,48 +224,78 @@ typedef struct {
   int dc_sign_ctx;
   int shift;
   int dequant_ac;
-  int sharpness;
+  int16_t sharpness;
+  TX_CLASS tx_class;
   int min_eob_cutoff;
 } TxbOptCtx;
 /*!\endcond */

-#if defined(__GNUC__) || defined(__clang__)
-__attribute__((noinline))
-#endif
-static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
-                                   TX_CLASS tx_class, const TxbOptCtx *ctx) {
+enum { kMaxNzNum = 2 };
+
+// Keep trellis reduction helpers out-of-line to limit register pressure in
+// the hot interior scan loops.
+static AOM_NOINLINE int update_coeff_simple_gt1_try_lower(
+    intptr_t ci, tran_low_t abs_qc, intptr_t coeff_ctx, tran_low_t abs_dqc,
+    const TxbOptCtx *restrict ctx, tran_low_t abs_tqc) {
   assert(ci > 0);
-  const LV_MAP_COEFF_COST *txb_costs = ctx->txb_costs;
-  const tran_low_t abs_qc = abs(qc);
-  const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
-  const tran_low_t abs_dqc = abs(ctx->dqcoeff[ci]);
-  int rate, rate_low;
+  assert(abs_qc >= 2);
+  const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
+  const int *const base_cost = txb_costs->base_cost[coeff_ctx];
+  // update_coeff_simple_gt1_try_lower is only invoked for AC coefficients
+  // (si > 0), so dequant_ac (dequant[1]) applies without a (ci > 0) branch.
   if (abs_qc == 2) {
-    const int *const base_cost = txb_costs->base_cost[coeff_ctx];
-    rate = base_cost[6];
-    if (abs_dqc < abs_tqc) return rate;
-    rate_low = base_cost[5];
-  } else {
-    const int br_ctx = get_br_ctx_simple_stride(ctx->levels, ci, ctx->bhl,
-                                                ctx->stride, tx_class);
-    const int *const lps_cost = txb_costs->lps_cost[br_ctx];
-    const int *const base_cost = txb_costs->base_cost[coeff_ctx];
-    const int base_rate = base_cost[7];
-    if (LIKELY(abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE)) {
-      rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
-      if (abs_dqc < abs_tqc) return rate;
-      rate_low = (abs_qc == 3)
-                     ? base_cost[6]
-                     : (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
+    int dqv = ctx->dequant_ac;
+    if (ctx->iqmatrix != NULL) {
+      dqv =
+          ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
+    }
+    const int shift = ctx->shift;
+    const tran_low_t abs_dqc_low = dqv >> shift;
+    const qm_val_t *restrict qmatrix = ctx->qmatrix;
+    int64_t dist_diff_0, dist_diff_low_0;
+    if (qmatrix == NULL) {
+      const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
+      dist_diff_0 =
+          ((int64_t)abs_dqc * (abs_dqc - abs_tqc2)) * (1LL << (2 * shift));
+      dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
+                        (1LL << (2 * shift));
     } else {
-      rate = base_rate + get_br_cost(abs_qc, lps_cost);
-      if (abs_dqc < abs_tqc) return rate;
-      rate_low = base_rate + get_br_cost(abs_qc - 1, lps_cost);
+      const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
+      dist_diff_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc, scaled_qm);
+      dist_diff_low_0 =
+          get_coeff_dist_scaled_qm(abs_tqc, abs_dqc_low, scaled_qm);
+    }
+    const int rate = base_cost[6];
+    const int rate_low = base_cost[5];
+    const int64_t rdmult = ctx->rdmult;
+    const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
+    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
+    if (rd_low < rd) {
+      tran_low_t *const qcoeff = ctx->qcoeff;
+      const int sign = (qcoeff[ci] < 0) ? 1 : 0;
+      qcoeff[ci] = (-sign ^ 1) + sign;
+      ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
+      ctx->levels[get_padded_idx((int)ci, ctx->bhl)] = 1;
+      return rate_low;
     }
+    return rate;
+  }
+
+  const int br_ctx = get_br_ctx_simple_stride(ctx->levels, (int)ci, ctx->bhl,
+                                              ctx->stride, ctx->tx_class);
+  const int *const lps_cost = txb_costs->lps_cost[br_ctx];
+  const int base_rate = base_cost[7];
+  int rate, rate_low;
+  if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
+    rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
+    rate_low = (abs_qc == 3)
+                   ? base_cost[6]
+                   : (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
+  } else {
+    rate = base_rate + get_br_cost(abs_qc, lps_cost);
+    rate_low = base_rate + get_br_cost(abs_qc - 1, lps_cost);
   }

-  // update_coeff_simple_gt1 is only invoked for AC coefficients (si > 0), so
-  // dequant_ac (dequant[1]) can be used directly without a (ci > 0) branch.
   int dqv = ctx->dequant_ac;
   if (ctx->iqmatrix != NULL) {
     dqv = ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
@@ -257,7 +303,7 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
   const int shift = ctx->shift;
   const tran_low_t abs_qc_low = abs_qc - 1;
   const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
-  const qm_val_t *qmatrix = ctx->qmatrix;
+  const qm_val_t *restrict qmatrix = ctx->qmatrix;
   int64_t dist_diff_0, dist_diff_low_0;
   if (qmatrix == NULL) {
     const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
@@ -274,25 +320,51 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
   const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
   const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
   if (rd_low < rd) {
-    const int sign = (qc < 0) ? 1 : 0;
-    ctx->qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
+    tran_low_t *const qcoeff = ctx->qcoeff;
+    const int sign = (qcoeff[ci] < 0) ? 1 : 0;
+    qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
     ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
-    ctx->levels[get_padded_idx(ci, ctx->bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
+    ctx->levels[get_padded_idx((int)ci, ctx->bhl)] =
+        AOMMIN(abs_qc_low, INT8_MAX);
     return rate_low;
   }
   return rate;
 }

-#if defined(__GNUC__) || defined(__clang__)
-__attribute__((noinline))
-#endif
-static int update_coeff_simple_nz(int ci, tran_low_t qc, int coeff_ctx,
-                                  TX_CLASS tx_class, const TxbOptCtx *ctx) {
+static AOM_NOINLINE int update_coeff_simple_gt1(intptr_t ci, tran_low_t qc,
+                                                intptr_t coeff_ctx,
+                                                TX_CLASS tx_class,
+                                                const TxbOptCtx *restrict ctx) {
+  assert(ci > 0);
+  const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
+  const tran_low_t abs_dqc = abs(ctx->dqcoeff[ci]);
+  const tran_low_t abs_qc = abs(qc);
+  if (abs_dqc >= abs_tqc) {
+    return update_coeff_simple_gt1_try_lower(ci, abs_qc, coeff_ctx, abs_dqc,
+                                             ctx, abs_tqc);
+  }
+  const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
+  const int *const base_cost = txb_costs->base_cost[coeff_ctx];
+  if (abs_qc == 2) return base_cost[6];
+  const int br_ctx = get_br_ctx_simple_stride(ctx->levels, (int)ci, ctx->bhl,
+                                              ctx->stride, tx_class);
+  const int *const lps_cost = txb_costs->lps_cost[br_ctx];
+  const int base_rate = base_cost[7];
+  if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
+    return base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
+  }
+  return base_rate + get_br_cost(abs_qc, lps_cost);
+}
+
+static AOM_NOINLINE int update_coeff_simple_nz(intptr_t ci, tran_low_t qc,
+                                               intptr_t coeff_ctx,
+                                               TX_CLASS tx_class,
+                                               const TxbOptCtx *restrict ctx) {
   assert(ci > 0);
   if ((unsigned int)(qc + 1) > 2) {
     return update_coeff_simple_gt1(ci, qc, coeff_ctx, tx_class, ctx);
   }
-  const LV_MAP_COEFF_COST *txb_costs = ctx->txb_costs;
+  const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
   const int *const base_cost = txb_costs->base_cost[coeff_ctx];
   const int rate = base_cost[5];
   const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
@@ -301,7 +373,7 @@ static int update_coeff_simple_nz(int ci, tran_low_t qc, int coeff_ctx,
     return rate;
   }

-  const qm_val_t *qmatrix = ctx->qmatrix;
+  const qm_val_t *restrict qmatrix = ctx->qmatrix;
   const int shift = ctx->shift;
   int64_t dist_diff_0;
   if (qmatrix == NULL) {
@@ -319,102 +391,94 @@ static int update_coeff_simple_nz(int ci, tran_low_t qc, int coeff_ctx,
   if (rd_low < rd) {
     ctx->qcoeff[ci] = 0;
     ctx->dqcoeff[ci] = 0;
-    ctx->levels[get_padded_idx(ci, ctx->bhl)] = 0;
+    ctx->levels[get_padded_idx((int)ci, ctx->bhl)] = 0;
     return rate_low;
   }
   return rate;
 }

-#if defined(__GNUC__) || defined(__clang__)
-__attribute__((noinline))
-#endif
-static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,
-                               TX_CLASS tx_class, TxbOptCtx *ctx) {
-  const LV_MAP_COEFF_COST *txb_costs = ctx->txb_costs;
-  const tran_low_t abs_qc = abs(qc);
-  const tran_low_t tqc = ctx->tcoeff[ci];
-  const tran_low_t dqc = ctx->dqcoeff[ci];
-  const int shift = ctx->shift;
-  const qm_val_t *qmatrix = ctx->qmatrix;
-
-  int64_t dist;
-  int64_t dist0 = 0;
-  int64_t scaled_qm = 0;
+static AOM_FORCE_INLINE int64_t
+calc_coeff_dist(tran_low_t tqc, tran_low_t dqc, int shift,
+                const qm_val_t *restrict qmatrix, int ci) {
   if (qmatrix == NULL) {
     const int64_t tqc2 = (int64_t)tqc * 2;
-    dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
-  } else {
-    scaled_qm = (int64_t)qmatrix[ci] << shift;
-    dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
-    dist = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
-  }
-
-  const int new_eob = si + 1;
-  const int min_eob_cutoff = ctx->min_eob_cutoff;
-  const int sharpness = ctx->sharpness;
-  const int qc_threshold = (si <= min_eob_cutoff) ? 2 : 1;
-  const int allow_lower_qc = sharpness ? (abs_qc > qc_threshold) : 1;
-  const int allow_new_eob = (sharpness == 0 || new_eob >= min_eob_cutoff);
-
-  if (!allow_lower_qc && !allow_new_eob) {
-    const int rate = (ci == 0)
-                         ? (txb_costs->base_cost[coeff_ctx][abs_qc] +
-                            txb_costs->dc_sign_cost[ctx->dc_sign_ctx][qc < 0])
-                         : txb_costs->base_cost[coeff_ctx][4 + abs_qc];
-    ctx->accu_dist += dist;
-    ctx->nz_ci[ctx->nz_num] = ci;
-    ++ctx->nz_num;
-    return ctx->accu_rate + rate;
+    return ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
   }
+  const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
+  const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
+  return get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
+}

+static AOM_NOINLINE int update_coeff_eob_gt1(int si, int ci, tran_low_t qc,
+                                             int coeff_ctx, TX_CLASS tx_class,
+                                             TxbOptCtx *restrict ctx) {
+  const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
+  const tran_low_t abs_qc = abs(qc);
+  assert(abs_qc >= 2);
+  const tran_low_t abs_qc_low = abs_qc - 1;
   const int bhl = ctx->bhl;
   const intptr_t stride = ctx->stride;
-  uint8_t *levels = ctx->levels;
+  uint8_t *restrict levels = ctx->levels;
   const int sign = (qc < 0) ? 1 : 0;
   const int sign_cost = (ci == 0)
                             ? txb_costs->dc_sign_cost[ctx->dc_sign_ctx][sign]
                             : av1_cost_literal(1);
   const int *const base_cost = txb_costs->base_cost[coeff_ctx];
-  const int *lps_cost = txb_costs->lps_cost[0];
-  int rate = base_cost[AOMMIN(abs_qc, 3)] + sign_cost;
-  if (abs_qc > NUM_BASE_LEVELS) {
+  int rate, rate_low;
+  if (abs_qc == 2) {
+    rate = base_cost[2] + sign_cost;
+    rate_low = base_cost[1] + sign_cost;
+  } else {
     const int br_ctx = get_br_ctx_stride(levels, ci, bhl, stride, tx_class);
-    lps_cost = txb_costs->lps_cost[br_ctx];
-    rate += get_br_cost(abs_qc, lps_cost);
-  }
-
-  const int64_t dist_orig = dist;
-  tran_low_t qc_low = 0, dqc_low = 0;
-  tran_low_t abs_qc_low = 0;
-  int64_t dist_low = 0;
-  if (abs_qc > 1) {
-    const int dqv = get_dqv(ctx->dequant, ci, ctx->iqmatrix);
-    get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
-    abs_qc_low = abs_qc - 1;
-    if (qmatrix == NULL) {
-      const int64_t tqc2 = (int64_t)tqc * 2;
-      dist_low = ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
+    const int *const lps_cost = txb_costs->lps_cost[br_ctx];
+    const int base_rate = base_cost[3] + sign_cost;
+    if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
+      rate = base_rate + lps_cost[abs_qc - (1 + NUM_BASE_LEVELS)];
+      rate_low = (abs_qc == 3)
+                     ? (base_cost[2] + sign_cost)
+                     : (base_rate + lps_cost[abs_qc - (2 + NUM_BASE_LEVELS)]);
     } else {
-      dist_low = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
+      rate = base_rate + get_br_cost(abs_qc, lps_cost);
+      rate_low = base_rate + get_br_cost(abs_qc_low, lps_cost);
     }
   }

+  const tran_low_t tqc = ctx->tcoeff[ci];
+  const tran_low_t dqc = ctx->dqcoeff[ci];
+  const int shift = ctx->shift;
+  const qm_val_t *restrict qmatrix = ctx->qmatrix;
+  int dqv = ctx->dequant_ac;
+  if (ci == 0) dqv = ctx->dequant[0];
+  if (ctx->iqmatrix != NULL) {
+    dqv = ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
+  }
+  tran_low_t qc_low, dqc_low;
+  get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
+
+  int64_t dist, dist_low;
+  if (qmatrix == NULL) {
+    const int64_t tqc2 = (int64_t)tqc * 2;
+    dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
+    dist_low = ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
+  } else {
+    const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
+    const int64_t dist0 = get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
+    dist = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) - dist0;
+    dist_low = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
+  }
+
+  const int64_t dist_orig = dist;
   const int64_t rdmult = ctx->rdmult;
   int accu_rate = ctx->accu_rate;
-  int64_t accu_dist = ctx->accu_dist;
+  const int64_t accu_dist = ctx->accu_dist;
   int64_t rd = RDCOST(rdmult, accu_rate + rate, accu_dist + dist);
   int lower_level = 0;

+  const int min_eob_cutoff = ctx->min_eob_cutoff;
+  const int sharpness = ctx->sharpness;
+  const int qc_threshold = (si <= min_eob_cutoff) ? 2 : 1;
+  const int allow_lower_qc = sharpness ? (abs_qc > qc_threshold) : 1;
   if (allow_lower_qc) {
-    int rate_low;
-    if (abs_qc == 1) {
-      rate_low = base_cost[0];
-    } else {
-      rate_low = base_cost[AOMMIN(abs_qc_low, 3)] + sign_cost;
-      if (abs_qc_low > NUM_BASE_LEVELS) {
-        rate_low += get_br_cost(abs_qc_low, lps_cost);
-      }
-    }
     const int64_t rd_low =
         RDCOST(rdmult, accu_rate + rate_low, accu_dist + dist_low);
     if (rd_low < rd) {
@@ -425,45 +489,49 @@ static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,
     }
   }

-  if (allow_new_eob) {
-    int lower_level_new_eob = 0;
+  const int new_eob = si + 1;
+  if (sharpness == 0 || new_eob >= min_eob_cutoff) {
     const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, ctx->width, si);
     const int new_eob_cost =
         get_eob_cost(new_eob, ctx->txb_eob_costs, txb_costs, tx_class);
     const int *const base_eob_cost =
         txb_costs->base_eob_cost[coeff_ctx_new_eob];
-    const int *lps_cost_eob = txb_costs->lps_cost[0];
-    int rate_coeff_eob =
-        new_eob_cost + base_eob_cost[AOMMIN(abs_qc, 3) - 1] + sign_cost;
-    if (abs_qc > NUM_BASE_LEVELS) {
+    int rate_coeff_eob, rate_coeff_eob_low;
+    if (abs_qc == 2) {
+      rate_coeff_eob = new_eob_cost + base_eob_cost[1] + sign_cost;
+      rate_coeff_eob_low = new_eob_cost + base_eob_cost[0] + sign_cost;
+    } else {
       const int br_ctx_eob = get_br_ctx_eob(ci, bhl, tx_class);
-      lps_cost_eob = txb_costs->lps_cost[br_ctx_eob];
-      rate_coeff_eob += get_br_cost(abs_qc, lps_cost_eob);
+      const int *const lps_cost_eob = txb_costs->lps_cost[br_ctx_eob];
+      const int base_eob_rate = new_eob_cost + base_eob_cost[2] + sign_cost;
+      if (abs_qc < 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
+        rate_coeff_eob =
+            base_eob_rate + lps_cost_eob[abs_qc - (1 + NUM_BASE_LEVELS)];
+        rate_coeff_eob_low =
+            (abs_qc == 3) ? (new_eob_cost + base_eob_cost[1] + sign_cost)
+                          : (base_eob_rate +
+                             lps_cost_eob[abs_qc - (2 + NUM_BASE_LEVELS)]);
+      } else {
+        rate_coeff_eob = base_eob_rate + get_br_cost(abs_qc, lps_cost_eob);
+        rate_coeff_eob_low =
+            base_eob_rate + get_br_cost(abs_qc_low, lps_cost_eob);
+      }
     }
+    int lower_level_new_eob = 0;
     int64_t dist_new_eob = dist_orig;
     int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
-
-    if (abs_qc_low > 0) {
-      int rate_coeff_eob_low =
-          new_eob_cost + base_eob_cost[AOMMIN(abs_qc_low, 3) - 1] + sign_cost;
-      if (abs_qc_low > NUM_BASE_LEVELS) {
-        rate_coeff_eob_low += get_br_cost(abs_qc_low, lps_cost_eob);
-      }
-      const int64_t dist_new_eob_low = dist_low;
-      const int64_t rd_new_eob_low =
-          RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
-      if (rd_new_eob_low < rd_new_eob) {
-        lower_level_new_eob = 1;
-        rd_new_eob = rd_new_eob_low;
-        rate_coeff_eob = rate_coeff_eob_low;
-        dist_new_eob = dist_new_eob_low;
-      }
+    const int64_t rd_new_eob_low = RDCOST(rdmult, rate_coeff_eob_low, dist_low);
+    if (rd_new_eob_low < rd_new_eob) {
+      lower_level_new_eob = 1;
+      rd_new_eob = rd_new_eob_low;
+      rate_coeff_eob = rate_coeff_eob_low;
+      dist_new_eob = dist_low;
     }

     if (rd_new_eob < rd) {
-      tran_low_t *qcoeff = ctx->qcoeff;
-      tran_low_t *dqcoeff = ctx->dqcoeff;
-      const int *nz_ci = ctx->nz_ci;
+      tran_low_t *restrict qcoeff = ctx->qcoeff;
+      tran_low_t *restrict dqcoeff = ctx->dqcoeff;
+      const int *restrict nz_ci = ctx->nz_ci;
       const int nz_num = ctx->nz_num;
       for (int ni = 0; ni < nz_num; ++ni) {
         int last_ci = nz_ci[ni];
@@ -485,17 +553,130 @@ static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,
     ctx->accu_dist = accu_dist + dist;
   }

-  tran_low_t *qcoeff = ctx->qcoeff;
+  ctx->nz_ci[ctx->nz_num] = ci;
+  ++ctx->nz_num;
   if (lower_level) {
-    qcoeff[ci] = qc_low;
+    ctx->qcoeff[ci] = qc_low;
     ctx->dqcoeff[ci] = dqc_low;
     levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
   }
-  if (qcoeff[ci]) {
+  return accu_rate;
+}
+
+static AOM_NOINLINE int update_coeff_eob_eq1(int si, int ci, tran_low_t qc,
+                                             int coeff_ctx, TX_CLASS tx_class,
+                                             TxbOptCtx *restrict ctx) {
+  assert(abs(qc) == 1);
+  const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
+  const int sign_cost = (ci == 0)
+                            ? txb_costs->dc_sign_cost[ctx->dc_sign_ctx][qc < 0]
+                            : av1_cost_literal(1);
+  const int *const base_cost = txb_costs->base_cost[coeff_ctx];
+  int rate = base_cost[1] + sign_cost;
+  const int bhl = ctx->bhl;
+  const int new_eob = si + 1;
+  const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, ctx->width, si);
+  const int new_eob_cost =
+      get_eob_cost(new_eob, ctx->txb_eob_costs, txb_costs, tx_class);
+  const int rate_coeff_eob =
+      new_eob_cost + txb_costs->base_eob_cost[coeff_ctx_new_eob][0] + sign_cost;
+
+  const int64_t rdmult = ctx->rdmult;
+  int accu_rate = ctx->accu_rate;
+  const int64_t accu_dist = ctx->accu_dist;
+  int new_eob_wins = 0;
+  if (ctx->sharpness != 0) {
+    // RDCOST() is linear in distortion, so dist cancels between rd_new_eob
+    // and rd; compare directly without computing dist first.
+    if (RDCOST(rdmult, rate_coeff_eob, 0) >=
+        RDCOST(rdmult, accu_rate + rate, accu_dist)) {
+      const int nz_num = ctx->nz_num;
+      // Once nz_num exceeds kMaxNzNum, update_coeff_eob_facade() exits to
+      // update_coeff_simple_facade() / update_coeff_dc(), which never read
+      // accu_dist.
+      if (nz_num < kMaxNzNum) {
+        ctx->accu_dist =
+            accu_dist + calc_coeff_dist(ctx->tcoeff[ci], ctx->dqcoeff[ci],
+                                        ctx->shift, ctx->qmatrix, ci);
+      }
+      ctx->nz_ci[nz_num] = ci;
+      ctx->nz_num = nz_num + 1;
+      return accu_rate + rate;
+    }
+    new_eob_wins = 1;
+  }
+
+  const int64_t dist_orig = calc_coeff_dist(ctx->tcoeff[ci], ctx->dqcoeff[ci],
+                                            ctx->shift, ctx->qmatrix, ci);
+  int64_t dist = dist_orig;
+  int lower_level = 0;
+  if (!new_eob_wins) {
+    assert(ctx->sharpness == 0);
+    int64_t rd = RDCOST(rdmult, accu_rate + rate, accu_dist + dist);
+    const int rate_low = base_cost[0];
+    const int64_t rd_low = RDCOST(rdmult, accu_rate + rate_low, accu_dist);
+    if (rd_low < rd) {
+      lower_level = 1;
+      rd = rd_low;
+      rate = rate_low;
+      dist = 0;
+    }
+    new_eob_wins = RDCOST(rdmult, rate_coeff_eob, dist_orig) < rd;
+  }
+
+  uint8_t *restrict levels = ctx->levels;
+  if (new_eob_wins) {
+    tran_low_t *restrict qcoeff = ctx->qcoeff;
+    tran_low_t *restrict dqcoeff = ctx->dqcoeff;
+    int *restrict nz_ci = ctx->nz_ci;
+    const int nz_num = ctx->nz_num;
+    for (int ni = 0; ni < nz_num; ++ni) {
+      int last_ci = nz_ci[ni];
+      levels[get_padded_idx(last_ci, bhl)] = 0;
+      qcoeff[last_ci] = 0;
+      dqcoeff[last_ci] = 0;
+    }
+    ctx->eob = new_eob;
+    ctx->nz_num = 1;
+    nz_ci[0] = ci;
+    ctx->accu_dist = dist_orig;
+    return rate_coeff_eob;
+  }
+
+  accu_rate += rate;
+  ctx->accu_dist = accu_dist + dist;
+  if (lower_level) {
+    ctx->qcoeff[ci] = 0;
+    ctx->dqcoeff[ci] = 0;
+    levels[get_padded_idx(ci, bhl)] = 0;
+    return accu_rate;
+  }
+  ctx->nz_ci[ctx->nz_num] = ci;
+  ++ctx->nz_num;
+  return accu_rate;
+}
+
+static AOM_NOINLINE int update_coeff_eob_nz(int si, int ci, tran_low_t qc,
+                                            int coeff_ctx, TX_CLASS tx_class,
+                                            TxbOptCtx *restrict ctx) {
+  const tran_low_t abs_qc = abs(qc);
+  assert(abs_qc >= 1);
+  // Skip trellis lowering once si + 1 < min_eob_cutoff with abs_qc <= 2 when
+  // sharpness > 0 (since allow_lower_qc == 0 and no future new_eob can fire).
+  if (ctx->sharpness != 0 && si + 1 < ctx->min_eob_cutoff && abs_qc <= 2) {
+    const LV_MAP_COEFF_COST *restrict txb_costs = ctx->txb_costs;
+    const int rate = (ci == 0)
+                         ? (txb_costs->base_cost[coeff_ctx][abs_qc] +
+                            txb_costs->dc_sign_cost[ctx->dc_sign_ctx][qc < 0])
+                         : txb_costs->base_cost[coeff_ctx][4 + abs_qc];
     ctx->nz_ci[ctx->nz_num] = ci;
     ++ctx->nz_num;
+    return ctx->accu_rate + rate;
   }
-  return accu_rate;
+  if (abs_qc > 1) {
+    return update_coeff_eob_gt1(si, ci, qc, coeff_ctx, tx_class, ctx);
+  }
+  return update_coeff_eob_eq1(si, ci, qc, coeff_ctx, tx_class, ctx);
 }

 static inline void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
@@ -554,9 +735,9 @@ static inline int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd,

 static AOM_FORCE_INLINE void update_coeff_eob_facade(
     int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, intptr_t stride,
-    const int16_t *__restrict scan,
-    const LV_MAP_COEFF_COST *__restrict txb_costs, const tran_low_t *qcoeff,
-    const uint8_t *levels, int max_nz_num, TxbOptCtx *ctx) {
+    const int16_t *restrict scan, const LV_MAP_COEFF_COST *restrict txb_costs,
+    const tran_low_t *qcoeff, const uint8_t *levels, int max_nz_num,
+    TxbOptCtx *ctx) {
   int s = *si;
   int local_rate = ctx->accu_rate;
   const int8_t *const nz_map_ctx =
@@ -584,54 +765,56 @@ static AOM_FORCE_INLINE void update_coeff_eob_facade(
 }

 static AOM_FORCE_INLINE void update_coeff_simple_facade(
-    int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, intptr_t stride,
-    const int16_t *__restrict scan,
-    const LV_MAP_COEFF_COST *__restrict txb_costs, const tran_low_t *qcoeff,
-    const uint8_t *levels, int sharpness, TxbOptCtx *ctx) {
-  int s = *si;
+    int si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, intptr_t stride,
+    const int16_t *restrict scan, const LV_MAP_COEFF_COST *restrict txb_costs,
+    const tran_low_t *qcoeff, const uint8_t *levels, int sharpness,
+    TxbOptCtx *ctx) {
+  assert(si >= 1);
+  const int16_t *scan_p = scan + si;
+  const int16_t *const scan_end = scan + 1;
   int local_rate = ctx->accu_rate;
   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;
+  const int (*const local_base_cost)[8] = txb_costs->base_cost;
   if (sharpness != 0) {
-    while (s >= 1) {
-      int ci;
+    for (;;) {
+      intptr_t ci;
       tran_low_t qc;
       int coeff_ctx;
-      do {
-        ci = scan[s];
+      for (;;) {
+        ci = *scan_p;
         assert(ci > 0);
-        coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
-                                                     nz_map_ctx, tx_class);
+        coeff_ctx = get_lower_levels_ctx_simple_fast(
+            levels, (int)ci, bhl, stride, nz_map_ctx, tx_class);
         qc = qcoeff[ci];
-        if (UNLIKELY((unsigned int)(qc + 1) > 2)) break;
-        local_rate += base_cost[coeff_ctx][4 + (qc != 0)];
-      } while (--s >= 1);
-      if (s < 1) break;
+        if ((unsigned int)(qc + 1) > 2) break;
+        local_rate += local_base_cost[coeff_ctx][4 + (qc & 1)];
+        if (--scan_p < scan_end) goto simple_done;
+      }
       local_rate += update_coeff_simple_gt1(ci, qc, coeff_ctx, tx_class, ctx);
-      --s;
+      if (--scan_p < scan_end) break;
     }
   } else {
-    while (s >= 1) {
-      int ci;
+    for (;;) {
+      intptr_t ci;
       tran_low_t qc;
       int coeff_ctx;
-      do {
-        ci = scan[s];
+      for (;;) {
+        ci = *scan_p;
         assert(ci > 0);
-        coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
-                                                     nz_map_ctx, tx_class);
+        coeff_ctx = get_lower_levels_ctx_simple_fast(
+            levels, (int)ci, bhl, stride, nz_map_ctx, tx_class);
         qc = qcoeff[ci];
         if (qc != 0) break;
-        local_rate += base_cost[coeff_ctx][0];
-      } while (--s >= 1);
-      if (s < 1) break;
+        local_rate += local_base_cost[coeff_ctx][0];
+        if (--scan_p < scan_end) goto simple_done;
+      }
       local_rate += update_coeff_simple_nz(ci, qc, coeff_ctx, tx_class, ctx);
-      --s;
+      if (--scan_p < scan_end) break;
     }
   }
+simple_done:
   ctx->accu_rate = local_rate;
-  *si = 0;
 }

 static inline int64_t get_txb_rdmult(const struct AV1_COMP *cpi,
@@ -671,9 +854,9 @@ static inline int64_t get_txb_rdmult(const struct AV1_COMP *cpi,
       rshift);
 }

-#if defined(__GNUC__) || defined(__clang__)
-__attribute__((noinline))
-#endif
+// Keep av1_optimize_txb_multi() out-of-line so the eob == 1 fast path in
+// av1_optimize_txb() stays small and spill-free.
+AOM_NOINLINE
 int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
                            int block, TX_SIZE tx_size, TX_TYPE tx_type,
                            const TXB_CTX *const txb_ctx, int *rate_cost,
@@ -727,26 +910,33 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
   const tran_low_t qc = qcoeff[ci];
   const tran_low_t abs_qc = abs(qc);
   const int sign = qc < 0;
-  const int max_nz_num = 2;
-  int nz_ci[3] = { ci, 0, 0 };
+  const int min_eob_cutoff = is_noise_pattern ? 8 : 5;
+  // accu_dist is only read by new_eob truncation (>= min_eob_cutoff when
+  // sharpness != 0) and update_skip() (sharpness == 0), so it can be skipped
+  // when sharpness != 0 && eob <= min_eob_cutoff (dist cancels in rd_low < rd).
+  const int need_accu_dist = sharpness == 0 || eob > min_eob_cutoff;
+  const int max_nz_num = kMaxNzNum;
+  int nz_ci[kMaxNzNum + 1] = { ci, 0, 0 };
   if (abs_qc >= 2) {
     update_coeff_last(&accu_rate, &accu_dist, si, ci, abs_qc, sign, tx_class,
                       bhl, width, rdmult, shift, dequant, txb_costs, tcoeff,
-                      qcoeff, dqcoeff, iqmatrix, qmatrix);
+                      qcoeff, dqcoeff, iqmatrix, qmatrix, need_accu_dist);
     --si;
   } else {
     assert(abs_qc == 1);
     const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
     accu_rate += txb_costs->base_eob_cost[coeff_ctx][0] + av1_cost_literal(1);
-    const tran_low_t tqc = tcoeff[ci];
-    const tran_low_t dqc = dqcoeff[ci];
-    if (qmatrix == NULL) {
-      accu_dist +=
-          ((int64_t)dqc * (dqc - (int64_t)tqc * 2)) * (1LL << (2 * shift));
-    } else {
-      const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
-      accu_dist += get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) -
-                   get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
+    if (need_accu_dist) {
+      const tran_low_t tqc = tcoeff[ci];
+      const tran_low_t dqc = dqcoeff[ci];
+      if (qmatrix == NULL) {
+        accu_dist +=
+            ((int64_t)dqc * (dqc - (int64_t)tqc * 2)) * (1LL << (2 * shift));
+      } else {
+        const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
+        accu_dist += get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) -
+                     get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
+      }
     }
     --si;
   }
@@ -779,8 +969,9 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
                     txb_ctx->dc_sign_ctx,
                     shift,
                     dequant[1],
-                    sharpness,
-                    is_noise_pattern ? 8 : 5 };
+                    (int16_t)sharpness,
+                    tx_class,
+                    min_eob_cutoff };
   TxbOptCtx *ctx_ptr = &ctx;

   if (si >= 1) {
@@ -789,9 +980,10 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
     update_coeff_eob_facade(&si, tx_size, tx_class_literal, bhl, stride, scan, \
                             txb_costs, qcoeff, levels, max_nz_num, ctx_ptr);   \
     if (si >= 1) {                                                             \
-      update_coeff_simple_facade(&si, tx_size, tx_class_literal, bhl, stride,  \
+      update_coeff_simple_facade(si, tx_size, tx_class_literal, bhl, stride,   \
                                  scan, txb_costs, qcoeff, levels, sharpness,   \
                                  ctx_ptr);                                     \
+      si = 0;                                                                  \
     }                                                                          \
     break
     switch (tx_class) {