Commit 13d8bbc3a3 for aom

commit 13d8bbc3a38aa1118acd6aeb4107dbc9fe1d602f
Author: Frank Galligan <fgalligan@google.com>
Date:   Fri Nov 6 20:25:07 2026 -0800

    txb_rdopt: narrow 2D init_levels & si<=3 fast path

    Add get_init_levels_width_2d() in av1_optimize_txb_multi() and
    warehouse_efficients_txb_multi() (when staying on the scalar loop) to
    clamp 2D av1_txb_init_levels() width for low-EOB blocks, and add
    optimize_txb_2d_fast() for 2D blocks with si <= 3 (eob <= 5,
    sharpness != 0).

    Have update_coeff_last() return the chosen abs level for
    optimize_txb_2d_fast(), and add warehouse_efficients_2d_fast() for 2D
    eob 3..4.

    Speedup (AMD EPYC 7B13, --cpu-used=6, bit-exact vs 433c8742ce):
    - -0.58% (1T) / -1.93% (8T) 720p --good (30f),
      -0.94% (1T) 10-bit --allintra --tune=iq --sharpness=3.
    Binary size (x86_64 -O3): libaom.a .text +3.4 KiB (+0.05%),
      .rodata +0.0 KiB (+0.00%); stripped aomenc +4.0 KiB (+0.04%);
      zero heap or static allocations.

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

diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index 5ab48987b5..bdf8f6b550 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -23,7 +23,7 @@

 // Specialized trellis update for the last non-zero coefficient (si == eob - 1)
 // when its magnitude is >= 2.
-static AOM_FORCE_INLINE void update_coeff_last(
+static AOM_FORCE_INLINE tran_low_t update_coeff_last(
     int *accu_rate, int64_t *accu_dist, int si, int ci, tran_low_t abs_qc,
     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,
@@ -79,11 +79,13 @@ static AOM_FORCE_INLINE void update_coeff_last(
   const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);

   int64_t chosen_dist;
+  tran_low_t final_abs = abs_qc;
   if (rd_low < rd) {
     qcoeff[ci] = qc_low;
     dqcoeff[ci] = dqc_low;
     *accu_rate += rate_low;
     chosen_dist = dist_diff_low_0;
+    final_abs = abs_qc_low;
   } else {
     *accu_rate += rate;
     chosen_dist = dist_diff_0;
@@ -94,6 +96,7 @@ static AOM_FORCE_INLINE void update_coeff_last(
     }
     *accu_dist += chosen_dist;
   }
+  return final_abs;
 }

 // Cold [0, 1 << RDDIV_BITS] tie-breaker helper kept out of line to keep
@@ -856,6 +859,105 @@ static inline int64_t get_txb_rdmult(const struct AV1_COMP *cpi,
       rshift);
 }

+// 2D scans are diagonal: the first 10 (36) positions stay in columns 0..3
+// (0..7 when height >= 8). Neighbor reads reach col + 2, which falls inside
+// the TX_PAD_BOTTOM (4) zero-padded trailing columns past the initialized
+// width.
+static AOM_FORCE_INLINE int get_init_levels_width_2d(int width, int bhl,
+                                                     int eob) {
+  static_assert(TX_PAD_BOTTOM == 4, "");
+  assert(width >= 4 && (width & 3) == 0);
+  assert(bhl >= MIN_TX_SIZE_LOG2 && bhl <= MAX_TX_SIZE_LOG2 - 1);
+  assert(eob > 1);
+  if (eob <= 10) return 4;
+  if (eob <= 36 && bhl >= 3) return AOMMIN(width, 8);
+  return width;
+}
+
+// Fast path for 2D blocks with si <= 3 (eob <= 5, sharpness != 0) when
+// qcoeff[scan[0..si]] are in [-2, 2] and no remaining coeff can be lowered
+// (qc_dc == 0 whenever nz_num exceeds max_nz_num, plus |qc1| <= 1 if si == 3).
+// Relies on 2D scan geometry: nz_off[scan[1]] == nz_off[scan[2]], and scan[3]
+// is either 2 * scan[2] or 2 * scan[1].
+static AOM_FORCE_INLINE int optimize_txb_2d_fast(
+    const MACROBLOCK *x, const MACROBLOCKD *xd, const AV1_COMMON *cm,
+    const struct macroblock_plane *p, int plane, int block, TX_SIZE tx_size,
+    TX_TYPE tx_type, const TXB_CTX *txb_ctx, const LV_MAP_COEFF_COST *txb_costs,
+    const int16_t *scan, const tran_low_t *qcoeff, int si, int ci,
+    tran_low_t last_abs_qc, int accu_rate, int *rate_cost) {
+  assert(si >= 0 && si <= 3);
+  assert(tx_type_to_class[tx_type] == TX_CLASS_2D);
+  assert(scan[0] == 0 && last_abs_qc >= 1);
+  static_assert(kMaxNzNum == 2, "");
+  const tran_low_t qc_dc = qcoeff[0];
+  if ((unsigned int)(qc_dc + 2) > 4) return 0;
+  unsigned int ac_cul = (unsigned int)last_abs_qc;
+  if (si != 0) {
+    const int ci1 = scan[1];
+    const tran_low_t qc1 = qcoeff[ci1];
+    if ((unsigned int)(qc1 + 2) > 4) return 0;
+    const unsigned int abs_qc1 = (unsigned int)abs(qc1);
+    const int8_t *const nz_off = av1_nz_map_ctx_offset[tx_size];
+    const int off1 = (uint8_t)nz_off[ci1];
+    if (si == 1) {
+      accu_rate += txb_costs->base_cost[off1][4 + abs_qc1];
+      ac_cul += abs_qc1;
+    } else {
+      const int ci2 = scan[2];
+      const tran_low_t qc2 = qcoeff[ci2];
+      if ((unsigned int)(qc2 + 2) > 4) return 0;
+      const unsigned int abs_qc2 = (unsigned int)abs(qc2);
+      assert(off1 == (uint8_t)nz_off[ci2]);
+      if (si == 2) {
+        if (qc_dc != 0 && qc1 != 0 && qc2 != 0) return 0;
+        const int ctx3 = 1 + (last_abs_qc >= 3);
+        const int is_sq3 = (ci == 2 * ci2);
+        assert(ci == 2 * ci2 || ci == 2 * ci1);
+        accu_rate +=
+            txb_costs->base_cost[off1 + (is_sq3 ? ctx3 : 0)][4 + abs_qc2] +
+            txb_costs->base_cost[off1 + (is_sq3 ? 0 : ctx3)][4 + abs_qc1];
+        ac_cul += abs_qc2 + abs_qc1;
+      } else {
+        const int ci3 = scan[3];
+        const tran_low_t qc3 = qcoeff[ci3];
+        if ((unsigned int)(qc3 + 2) > 4 ||
+            !((qc3 == 0 || qc2 == 0)
+                  ? (qc_dc == 0 || (qc3 == 0 && qc2 == 0) || qc1 == 0)
+                  : (qc_dc == 0 && (unsigned int)(qc1 + 1) <= 2))) {
+          return 0;
+        }
+        const int last_mag = (int)AOMMIN(last_abs_qc, 3);
+        const unsigned int abs_qc3 = (unsigned int)abs(qc3);
+        const int is_sq3 = (ci3 == 2 * ci2);
+        assert(ci3 == 2 * ci2 || ci3 == 2 * ci1);
+        assert(ci == ci1 + ci2);
+        const int mag2 = last_mag + (is_sq3 ? (int)abs_qc3 : 0);
+        const int mag1 = last_mag + (is_sq3 ? 0 : (int)abs_qc3);
+        accu_rate +=
+            txb_costs->base_cost[(uint8_t)nz_off[ci3]][4 + abs_qc3] +
+            txb_costs->base_cost[off1 + ((mag2 + 1) >> 1)][4 + abs_qc2] +
+            txb_costs->base_cost[off1 + ((mag1 + 1) >> 1)][4 + abs_qc1];
+        ac_cul += abs_qc3 + abs_qc2 + abs_qc1;
+      }
+    }
+  }
+  const unsigned int abs_dc = (unsigned int)abs(qc_dc);
+  if (qc_dc == 0) {
+    accu_rate += txb_costs->base_cost[0][0];
+  } else {
+    accu_rate += txb_costs->base_cost[0][abs_dc] +
+                 txb_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][qc_dc < 0];
+  }
+  int cul = (int)AOMMIN(abs_dc + ac_cul, (unsigned int)COEFF_CONTEXT_MASK);
+  set_dc_sign(&cul, qc_dc);
+  p->txb_entropy_ctx[block] = (uint8_t)cul;
+  const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
+                                            cm->features.reduced_tx_set_used);
+  *rate_cost = accu_rate + txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0] +
+               tx_type_cost;
+  return 1;
+}
+
 // 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
@@ -910,7 +1012,7 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
   int si = eob - 1;
   const int ci = scan[si];
   const tran_low_t qc = qcoeff[ci];
-  const tran_low_t abs_qc = abs(qc);
+  tran_low_t last_abs_qc = abs(qc);
   const int sign = qc < 0;
   const int min_eob_cutoff = is_noise_pattern ? 8 : 5;
   // accu_dist is only read by new_eob truncation (>= min_eob_cutoff when
@@ -919,13 +1021,13 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
   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, need_accu_dist);
-    --si;
+  if (last_abs_qc >= 2) {
+    last_abs_qc = update_coeff_last(&accu_rate, &accu_dist, si, ci, last_abs_qc,
+                                    sign, tx_class, bhl, width, rdmult, shift,
+                                    dequant, txb_costs, tcoeff, qcoeff, dqcoeff,
+                                    iqmatrix, qmatrix, need_accu_dist);
   } else {
-    assert(abs_qc == 1);
+    assert(last_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);
     if (need_accu_dist) {
@@ -940,13 +1042,24 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
                      get_coeff_dist_scaled_qm(tqc, 0, scaled_qm);
       }
     }
-    --si;
+  }
+  --si;
+
+  if (sharpness != 0 && si <= 3 && tx_class == TX_CLASS_2D) {
+    if (optimize_txb_2d_fast(x, xd, cm, p, plane, block, tx_size, tx_type,
+                             txb_ctx, txb_costs, scan, qcoeff, si, ci,
+                             last_abs_qc, accu_rate, rate_cost)) {
+      return eob;
+    }
   }

   uint8_t levels_buf[TX_PAD_2D];
   uint8_t *const levels = set_levels(levels_buf, height);
   if (si >= 1 || tx_class != TX_CLASS_2D || (unsigned int)(qcoeff[0] + 2) > 4) {
-    av1_txb_init_levels(qcoeff, width, height, levels);
+    const int init_w = (tx_class == TX_CLASS_2D)
+                           ? get_init_levels_width_2d(width, bhl, eob)
+                           : width;
+    av1_txb_init_levels(qcoeff, init_w, height, levels);
   }

   const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
@@ -1772,6 +1885,78 @@ static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
   return cost;
 }

+static AOM_FORCE_INLINE int warehouse_efficients_2d_fast(
+    const tran_low_t *const qcoeff, const int16_t *const scan,
+    const TX_SIZE tx_size, const int bhl, const int eob,
+    const TXB_CTX *const txb_ctx, const LV_MAP_COEFF_COST *const coeff_costs,
+    int cost, int *out_cost) {
+  assert(eob == 3 || eob == 4);
+  assert(scan[0] == 0);
+  const int pos1 = scan[1];
+  const tran_low_t v1 = qcoeff[pos1];
+  const tran_low_t v0 = qcoeff[0];
+  if ((unsigned int)(v1 + 2) > 4 || (unsigned int)(v0 + 2) > 4) {
+    return 0;
+  }
+  const int off1 = (uint8_t)av1_nz_map_ctx_offset[tx_size][pos1];
+  const unsigned int abs_v1 = (unsigned int)abs(v1);
+  if (eob == 3) {
+    const int pos2 = scan[2];
+    const tran_low_t v2 = qcoeff[pos2];
+    assert(get_lower_levels_ctx_eob(bhl, get_txb_wide(tx_size), 2) == 1);
+    if (v2 != 0) {
+      const unsigned int level2 = (unsigned int)abs(v2);
+      if (level2 <= NUM_BASE_LEVELS) {
+        cost += coeff_costs->base_eob_cost[1][level2 - 1] + av1_cost_literal(1);
+      } else {
+        const int br2 = get_br_ctx_eob(pos2, bhl, TX_CLASS_2D);
+        cost += coeff_costs->base_eob_cost[1][2] + av1_cost_literal(1) +
+                get_br_cost((tran_low_t)level2, coeff_costs->lps_cost[br2]);
+      }
+    }
+    cost += coeff_costs->base_cost[off1][4 + abs_v1];
+  } else {
+    const int pos2 = scan[2];
+    const tran_low_t v2 = qcoeff[pos2];
+    if ((unsigned int)(v2 + 2) > 4) return 0;
+    assert(off1 == (uint8_t)av1_nz_map_ctx_offset[tx_size][pos2]);
+    const int pos3 = scan[3];
+    const tran_low_t v3 = qcoeff[pos3];
+    int ctx_val2 = 0;
+    int ctx_val1 = 0;
+    const int eob_ctx3 = (tx_size == TX_4X4) ? 2 : 1;
+    assert(pos3 == 2 * pos2 || pos3 == 2 * pos1);
+    assert(get_lower_levels_ctx_eob(bhl, get_txb_wide(tx_size), 3) == eob_ctx3);
+    if (v3 != 0) {
+      const unsigned int level3 = (unsigned int)abs(v3);
+      if (level3 <= NUM_BASE_LEVELS) {
+        cost += coeff_costs->base_eob_cost[eob_ctx3][level3 - 1] +
+                av1_cost_literal(1);
+      } else {
+        const int br3 = get_br_ctx_eob(pos3, bhl, TX_CLASS_2D);
+        cost += coeff_costs->base_eob_cost[eob_ctx3][2] + av1_cost_literal(1) +
+                get_br_cost((tran_low_t)level3, coeff_costs->lps_cost[br3]);
+      }
+      const int c3_val = 1 + (level3 >= 3);
+      if (pos3 == 2 * pos2) {
+        ctx_val2 = c3_val;
+      } else {
+        ctx_val1 = c3_val;
+      }
+    }
+    cost += coeff_costs->base_cost[off1 + ctx_val2][4 + (unsigned int)abs(v2)] +
+            coeff_costs->base_cost[off1 + ctx_val1][4 + abs_v1];
+  }
+  if (!v0) {
+    cost += coeff_costs->base_cost[0][0];
+  } else {
+    cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][v0 < 0] +
+            coeff_costs->base_cost[0][(unsigned int)abs(v0)];
+  }
+  *out_cost = cost;
+  return 1;
+}
+
 #if defined(__GNUC__) || defined(__clang__)
 __attribute__((noinline))
 #endif
@@ -1780,16 +1965,29 @@ static int warehouse_efficients_txb_multi(
     const TXB_CTX *const txb_ctx, const int eob,
     const LV_MAP_COEFF_COST *const coeff_costs, const TX_TYPE tx_type,
     const TX_CLASS tx_class, int cost) {
-  const int width = get_txb_wide(tx_size);
-  const int height = get_txb_high(tx_size);
+  const int bhl = get_txb_bhl(tx_size);
   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
   const int16_t *const scan = scan_order->scan;
+  if (eob <= 4 && tx_class == TX_CLASS_2D) {
+    int fast_cost;
+    if (warehouse_efficients_2d_fast(qcoeff, scan, tx_size, bhl, eob, txb_ctx,
+                                     coeff_costs, cost, &fast_cost)) {
+      return fast_cost;
+    }
+  }
+  const int width = get_txb_wide(tx_size);
+  const int height = get_txb_high(tx_size);
   uint8_t levels_buf[TX_PAD_2D];
   uint8_t *const levels = set_levels(levels_buf, height);

-  av1_txb_init_levels(qcoeff, width, height, levels);
+  // Only narrow init_w when staying on the scalar loop; av1_get_nz_map_contexts
+  // scans all width columns up to width + 1.
+  const int init_w =
+      (tx_class == TX_CLASS_2D && eob <= ((width << bhl) >> 4) + 3)
+          ? get_init_levels_width_2d(width, bhl, eob)
+          : width;
+  av1_txb_init_levels(qcoeff, init_w, height, levels);

-  const int bhl = get_txb_bhl(tx_size);
   const intptr_t stride = height + TX_PAD_HOR;
   switch (tx_class) {
     case TX_CLASS_2D: