Commit b8021a8186 for aom

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

    av1: Optimize simple_facade and inv transforms

    Split update_coeff_simple_facade by sharpness != 0 for branchless
    accumulation of |qc| <= 1 costs, pre-shift qmatrix weights in
    get_coeff_dist_scaled_qm, and fuse rectangular scaling and 2-row
    prediction reconstruction into the AVX2 and SSSE3 inverse transforms.

    Benchmarking results (incremental vs prev CL / cumulative vs baseline):
    - Stock avifenc still-image (Default 1T): -1.43% / -35.93% encode time
    - Stock avifenc still-image (Default 8T): -0.32% / -28.65% encode time
    - 48-image multi-rate corpus (Full Suite 1T): -1.81% / -36.32% time
    - Bit-exact output (1T and 8T)

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

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

diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index 698d3edd9f..344c5aa2c6 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -124,8 +124,8 @@ static AOM_FORCE_INLINE int get_br_ctx_stride(const uint8_t *const levels,
                                               const int bhl,
                                               const intptr_t stride,
                                               const TX_CLASS tx_class) {
-  const int col = c >> bhl;
-  const uint8_t *const lv = levels + c + (col << TX_PAD_HOR_LOG2);
+  const intptr_t col = (intptr_t)c >> bhl;
+  const uint8_t *const lv = levels + (intptr_t)c + (col << TX_PAD_HOR_LOG2);
   const uint8_t *const lvs = lv + stride;
   int mag = lv[1] + lvs[0];
   switch (tx_class) {
@@ -134,7 +134,7 @@ static AOM_FORCE_INLINE int get_br_ctx_stride(const uint8_t *const levels,
       mag = AOMMIN((mag + 1) >> 1, 6);
       if (c == 0) return mag;
       const int row = c & ((int)stride - (TX_PAD_HOR + 1));
-      if ((row | col) < 2) return mag + 7;
+      if ((row | (int)col) < 2) return mag + 7;
       break;
     }
     case TX_CLASS_HORIZ:
@@ -169,8 +169,8 @@ static AOM_FORCE_INLINE int get_br_ctx_simple_stride(
     const int c,  // raster order > 0
     const int bhl, const intptr_t stride, const TX_CLASS tx_class) {
   assert(c > 0);
-  const int col = c >> bhl;
-  const uint8_t *const lv = levels + c + (col << TX_PAD_HOR_LOG2);
+  const intptr_t col = (intptr_t)c >> bhl;
+  const uint8_t *const lv = levels + (intptr_t)c + (col << TX_PAD_HOR_LOG2);
   const uint8_t *const lvs = lv + stride;
   int mag = lv[1] + lvs[0];
   switch (tx_class) {
@@ -178,7 +178,7 @@ static AOM_FORCE_INLINE int get_br_ctx_simple_stride(
       const int row = c & ((int)stride - (TX_PAD_HOR + 1));
       mag += lvs[1];
       mag = AOMMIN((mag + 1) >> 1, 6);
-      return mag + (((row | col) < 2) ? 7 : 14);
+      return mag + (((row | (int)col) < 2) ? 7 : 14);
     }
     case TX_CLASS_HORIZ:
       mag += lvs[stride];
diff --git a/av1/common/x86/av1_inv_txfm_avx2.c b/av1/common/x86/av1_inv_txfm_avx2.c
index 8136091a02..cabe69d176 100644
--- a/av1/common/x86/av1_inv_txfm_avx2.c
+++ b/av1/common/x86/av1_inv_txfm_avx2.c
@@ -1662,8 +1662,9 @@ static inline void lowbd_inv_txfm2d_add_no_identity_avx2(
     const __m256i res = _mm256_broadcastw_epi16(dc);
     for (int i = 0; i < buf_size_w_div16; i++) {
       uint8_t *out_col = output + 16 * i;
-      for (int r = 0; r < txfm_size_row; ++r) {
-        write_recon_w16_avx2(res, out_col + r * stride);
+      for (int r = 0; r < txfm_size_row; r += 2) {
+        write_recon_w16x2_avx2(res, res, out_col + r * stride,
+                               out_col + (r + 1) * stride);
       }
     }
     return;
@@ -1692,10 +1693,19 @@ static inline void lowbd_inv_txfm2d_add_no_identity_avx2(
   const __m256i scale0 = _mm256_set1_epi16(1 << (15 + shift[0]));
   for (int i = 0; i < buf_size_nonzero_h_div16; i++) {
     __m256i buf0[64];
-    load_buffer_32bit_to_16bit_w16_avx2(input + 16 * i, input_stride, buf0,
-                                        buf_size_nonzero_w);
+    const int32_t *in_ptr = input + 16 * i;
     if (rect_type == 1 || rect_type == -1) {
-      round_shift_avx2(buf0, buf0, buf_size_nonzero_w);  // rect special code
+      // Fuse 32-to-16-bit coefficient packing with rectangular 1/sqrt(2)
+      // scaling in a single pass.
+      const __m256i rect_scale = _mm256_set1_epi16(NewInvSqrt2 * 8);
+      for (int j = 0; j < buf_size_nonzero_w; ++j) {
+        buf0[j] = _mm256_mulhrs_epi16(
+            load_32bit_to_16bit_w16_avx2(in_ptr + j * input_stride),
+            rect_scale);
+      }
+    } else {
+      load_buffer_32bit_to_16bit_w16_avx2(in_ptr, input_stride, buf0,
+                                          buf_size_nonzero_w);
     }
     row_txfm(buf0, buf0);
     for (int j = 0; j < txfm_size_col; ++j) {
@@ -1716,18 +1726,23 @@ static inline void lowbd_inv_txfm2d_add_no_identity_avx2(
       }
     }
   }
+  // Perform 1D column transforms and fuse the final rounding shift with
+  // 2-row-at-a-time reconstruction writes.
   const __m256i scale1 = _mm256_set1_epi16(1 << (15 - 4));
   for (int i = 0; i < buf_size_w_div16; i++) {
     __m256i *buf1_cur = buf1 + i * txfm_size_row;
     col_txfm(buf1_cur, buf1_cur);
-    for (int j = 0; j < txfm_size_row; ++j) {
-      buf1_cur[j] = _mm256_mulhrs_epi16(buf1_cur[j], scale1);
+
+    uint8_t *out = output + 16 * i;
+    int j = ud_flip ? (txfm_size_row - 1) : 0;
+    const int step = ud_flip ? -1 : 1;
+    for (int r = 0; r < txfm_size_row; r += 2, j += 2 * step) {
+      const __m256i res0 = _mm256_mulhrs_epi16(buf1_cur[j], scale1);
+      const __m256i res1 = _mm256_mulhrs_epi16(buf1_cur[j + step], scale1);
+      write_recon_w16x2_avx2(res0, res1, out + r * stride,
+                             out + (r + 1) * stride);
     }
   }
-  for (int i = 0; i < buf_size_w_div16; i++) {
-    lowbd_write_buffer_16xn_avx2(buf1 + i * txfm_size_row, output + 16 * i,
-                                 stride, ud_flip, txfm_size_row);
-  }
 }

 static inline void iidentity_row_16xn_avx2(__m256i *out, const int32_t *input,
diff --git a/av1/common/x86/av1_inv_txfm_avx2.h b/av1/common/x86/av1_inv_txfm_avx2.h
index b4e4384d38..cd624291d1 100644
--- a/av1/common/x86/av1_inv_txfm_avx2.h
+++ b/av1/common/x86/av1_inv_txfm_avx2.h
@@ -11,6 +11,7 @@
 #ifndef AOM_AV1_COMMON_X86_AV1_INV_TXFM_AVX2_H_
 #define AOM_AV1_COMMON_X86_AV1_INV_TXFM_AVX2_H_

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

 #include "config/aom_config.h"
@@ -51,13 +52,29 @@ static inline void write_recon_w16_avx2(__m256i res, uint8_t *output) {
   _mm_storeu_si128((__m128i *)(output), y);
 }

+// Add two 16-pixel residual rows (res0, res1) to prediction rows (out0, out1),
+// pack to unsigned 8-bit pixels with a single vpackuswb + vpermq pair, and
+// store both reconstructed rows.
+static inline void write_recon_w16x2_avx2(__m256i res0, __m256i res1,
+                                          uint8_t *out0, uint8_t *out1) {
+  const __m128i pred0 = _mm_loadu_si128((__m128i const *)out0);
+  const __m128i pred1 = _mm_loadu_si128((__m128i const *)out1);
+  const __m256i u0 = _mm256_adds_epi16(_mm256_cvtepu8_epi16(pred0), res0);
+  const __m256i u1 = _mm256_adds_epi16(_mm256_cvtepu8_epi16(pred1), res1);
+  const __m256i y = _mm256_permute4x64_epi64(_mm256_packus_epi16(u0, u1), 0xd8);
+  _mm_storeu_si128((__m128i *)out0, _mm256_castsi256_si128(y));
+  _mm_storeu_si128((__m128i *)out1, _mm256_extracti128_si256(y, 1));
+}
+
 static inline void lowbd_write_buffer_16xn_avx2(__m256i *in, uint8_t *output,
                                                 int stride, int flipud,
                                                 int height) {
+  assert(height >= 2 && (height & 1) == 0);
   int j = flipud ? (height - 1) : 0;
   const int step = flipud ? -1 : 1;
-  for (int i = 0; i < height; ++i, j += step) {
-    write_recon_w16_avx2(in[j], output + i * stride);
+  for (int i = 0; i < height; i += 2, j += 2 * step) {
+    write_recon_w16x2_avx2(in[j], in[j + step], output + i * stride,
+                           output + (i + 1) * stride);
   }
 }

diff --git a/av1/common/x86/av1_inv_txfm_ssse3.c b/av1/common/x86/av1_inv_txfm_ssse3.c
index 0a83c8c672..dd5268abd7 100644
--- a/av1/common/x86/av1_inv_txfm_ssse3.c
+++ b/av1/common/x86/av1_inv_txfm_ssse3.c
@@ -2294,18 +2294,6 @@ static inline __m128i lowbd_get_recon_16x16_sse2(const __m128i pred,
   return _mm_packus_epi16(x0, x1);
 }

-static inline void lowbd_write_buffer_16xn_sse2(__m128i *in, uint8_t *output,
-                                                int stride, int flipud,
-                                                int height) {
-  int j = flipud ? (height - 1) : 0;
-  const int step = flipud ? -1 : 1;
-  for (int i = 0; i < height; ++i, j += step) {
-    __m128i v = _mm_loadu_si128((__m128i const *)(output + i * stride));
-    __m128i u = lowbd_get_recon_16x16_sse2(v, in[j], in[j + height]);
-    _mm_storeu_si128((__m128i *)(output + i * stride), u);
-  }
-}
-
 static inline void round_shift_ssse3(const __m128i *input, __m128i *output,
                                      int size) {
   const __m128i scale = _mm_set1_epi16(NewInvSqrt2 * 8);
@@ -2382,10 +2370,18 @@ static inline void lowbd_inv_txfm2d_add_no_identity_ssse3(
   get_flip_cfg(tx_type, &ud_flip, &lr_flip);
   for (int i = 0; i < buf_size_nonzero_h_div8; i++) {
     __m128i buf0[64];
-    load_buffer_32bit_to_16bit(input + 8 * i, input_stride, buf0,
-                               buf_size_nonzero_w);
+    const int32_t *in_ptr = input + 8 * i;
     if (rect_type == 1 || rect_type == -1) {
-      round_shift_ssse3(buf0, buf0, buf_size_nonzero_w);  // rect special code
+      // Fuse 32-to-16-bit coefficient packing with rectangular 1/sqrt(2)
+      // scaling in a single pass.
+      const __m128i rect_scale = _mm_set1_epi16(NewInvSqrt2 * 8);
+      for (int j = 0; j < buf_size_nonzero_w; ++j) {
+        buf0[j] = _mm_mulhrs_epi16(
+            load_32bit_to_16bit(in_ptr + j * input_stride), rect_scale);
+      }
+    } else {
+      load_buffer_32bit_to_16bit(in_ptr, input_stride, buf0,
+                                 buf_size_nonzero_w);
     }
     row_txfm(buf0, buf0);
     round_shift_16bit_ssse3(buf0, txfm_size_col, shift[0]);
@@ -2403,19 +2399,38 @@ static inline void lowbd_inv_txfm2d_add_no_identity_ssse3(
       }
     }
   }
-  for (int i = 0; i < buf_size_w_div8; i++) {
-    col_txfm(buf1 + i * txfm_size_row, buf1 + i * txfm_size_row);
-    round_shift_16bit_ssse3(buf1 + i * txfm_size_row, txfm_size_row, -4);
-  }
-
+  // Perform 1D column transforms and fuse the final rounding shift with
+  // prediction reconstruction.
+  const __m128i scale1 = _mm_set1_epi16(1 << (15 - 4));
   if (txfm_size_col >= 16) {
     for (int i = 0; i < (txfm_size_col >> 4); i++) {
-      lowbd_write_buffer_16xn_sse2(buf1 + i * txfm_size_row * 2,
-                                   output + 16 * i, stride, ud_flip,
-                                   txfm_size_row);
+      __m128i *col0 = buf1 + i * txfm_size_row * 2;
+      __m128i *col1 = col0 + txfm_size_row;
+      col_txfm(col0, col0);
+      col_txfm(col1, col1);
+
+      int j = ud_flip ? (txfm_size_row - 1) : 0;
+      const int step = ud_flip ? -1 : 1;
+      uint8_t *out = output + 16 * i;
+      for (int r = 0; r < txfm_size_row; ++r, j += step) {
+        const __m128i v = _mm_loadu_si128((__m128i const *)(out + r * stride));
+        const __m128i res0 = _mm_mulhrs_epi16(col0[j], scale1);
+        const __m128i res1 = _mm_mulhrs_epi16(col1[j], scale1);
+        const __m128i u = lowbd_get_recon_16x16_sse2(v, res0, res1);
+        _mm_storeu_si128((__m128i *)(out + r * stride), u);
+      }
     }
   } else if (txfm_size_col == 8) {
-    lowbd_write_buffer_8xn_sse2(buf1, output, stride, ud_flip, txfm_size_row);
+    col_txfm(buf1, buf1);
+
+    int j = ud_flip ? (txfm_size_row - 1) : 0;
+    const int step = ud_flip ? -1 : 1;
+    for (int r = 0; r < txfm_size_row; ++r, j += step) {
+      const __m128i v = _mm_loadl_epi64((__m128i const *)(output + r * stride));
+      const __m128i res = _mm_mulhrs_epi16(buf1[j], scale1);
+      const __m128i u = lowbd_get_recon_8x8_sse2(v, res);
+      _mm_storel_epi64((__m128i *)(output + r * stride), u);
+    }
   }
 }

@@ -2495,10 +2510,18 @@ void av1_lowbd_inv_txfm2d_add_v_identity_ssse3(const int32_t *input,
   get_flip_cfg(tx_type, &ud_flip, &lr_flip);
   for (int i = 0; i < buf_size_h_div8; i++) {
     __m128i buf0[64];
-    load_buffer_32bit_to_16bit(input + i * 8, input_stride, buf0,
-                               buf_size_nonzero_w);
+    const int32_t *in_ptr = input + i * 8;
     if (rect_type == 1 || rect_type == -1) {
-      round_shift_ssse3(buf0, buf0, buf_size_nonzero_w);  // rect special code
+      // Fuse 32-to-16-bit coefficient packing with rectangular 1/sqrt(2)
+      // scaling in a single pass.
+      const __m128i rect_scale = _mm_set1_epi16(NewInvSqrt2 * 8);
+      for (int j = 0; j < buf_size_nonzero_w; ++j) {
+        buf0[j] = _mm_mulhrs_epi16(
+            load_32bit_to_16bit(in_ptr + j * input_stride), rect_scale);
+      }
+    } else {
+      load_buffer_32bit_to_16bit(in_ptr, input_stride, buf0,
+                                 buf_size_nonzero_w);
     }
     row_txfm(buf0, buf0);
     round_shift_16bit_ssse3(buf0, txfm_size_col, shift[0]);
diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index 1d86ea6c57..cce0927e2e 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -58,10 +58,10 @@ static AOM_FORCE_INLINE void update_coeff_last(
     dist_diff_low_0 =
         ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
   } else {
-    const int64_t qm_val = qmatrix[ci];
-    const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
-    dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
-    dist_diff_low_0 = get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0;
+    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;
   }

   const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
@@ -132,13 +132,13 @@ static AOM_FORCE_INLINE void update_coeff_dc(
               ? 0
               : (((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift)));
     } else {
-      const int64_t qm_val = qmatrix[0];
-      const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
-      dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
+      const int64_t scaled_qm = (int64_t)qmatrix[0] << 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 =
           (abs_qc == 1)
               ? 0
-              : (get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0);
+              : (get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0);
     }

     const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
@@ -193,8 +193,6 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
   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]);
-  const int bhl = ctx->bhl;
-  uint8_t *levels = ctx->levels;
   int rate, rate_low;
   if (abs_qc == 2) {
     const int *const base_cost = txb_costs->base_cost[coeff_ctx];
@@ -202,15 +200,22 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
     if (abs_dqc < abs_tqc) return rate;
     rate_low = base_cost[5];
   } else {
-    const int br_ctx =
-        get_br_ctx_simple_stride(levels, ci, bhl, ctx->stride, tx_class);
+    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];
-    rate = base_rate + get_br_cost(abs_qc, lps_cost);
-    if (abs_dqc < abs_tqc) return rate;
-    rate_low = (abs_qc == 3) ? base_cost[6]
-                             : (base_rate + get_br_cost(abs_qc - 1, lps_cost));
+    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)]);
+    } 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);
+    }
   }

   // update_coeff_simple_gt1 is only invoked for AC coefficients (si > 0), so
@@ -231,9 +236,9 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
     dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
                       (1LL << (2 * shift));
   } else {
-    const int64_t qm_val = qmatrix[ci];
-    dist_diff_0 = get_coeff_dist_qm(abs_tqc, abs_dqc, shift, qm_val);
-    dist_diff_low_0 = get_coeff_dist_qm(abs_tqc, abs_dqc_low, shift, qm_val);
+    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 int64_t rdmult = ctx->rdmult;
   const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
@@ -242,7 +247,7 @@ static int update_coeff_simple_gt1(int ci, tran_low_t qc, int coeff_ctx,
     const int sign = (qc < 0) ? 1 : 0;
     ctx->qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
     ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
-    levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
+    ctx->levels[get_padded_idx(ci, ctx->bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
     return rate_low;
   }
   return rate;
@@ -273,9 +278,9 @@ static int update_coeff_simple_nz(int ci, tran_low_t qc, int coeff_ctx,
     dist_diff_0 = ((int64_t)abs_dqc * (abs_dqc - ((int64_t)abs_tqc * 2))) *
                   (1LL << (2 * shift));
   } else {
-    const int64_t qm_val = qmatrix[ci];
-    const int64_t dist0 = get_coeff_dist_qm(abs_tqc, 0, shift, qm_val);
-    dist_diff_0 = get_coeff_dist_qm(abs_tqc, abs_dqc, shift, qm_val) - dist0;
+    const int64_t scaled_qm = (int64_t)qmatrix[ci] << shift;
+    const int64_t dist0 = get_coeff_dist_scaled_qm(abs_tqc, 0, scaled_qm);
+    dist_diff_0 = get_coeff_dist_scaled_qm(abs_tqc, abs_dqc, scaled_qm) - dist0;
   }
   const int rate_low = base_cost[0];
   const int64_t rdmult = ctx->rdmult;
@@ -304,14 +309,14 @@ static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,

   int64_t dist;
   int64_t dist0 = 0;
-  int64_t qm_val = 0;
+  int64_t scaled_qm = 0;
   if (qmatrix == NULL) {
     const int64_t tqc2 = (int64_t)tqc * 2;
     dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
   } else {
-    qm_val = qmatrix[ci];
-    dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
-    dist = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
+    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;
@@ -360,7 +365,7 @@ static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,
       const int64_t tqc2 = (int64_t)tqc * 2;
       dist_low = ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
     } else {
-      dist_low = get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0;
+      dist_low = get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm) - dist0;
     }
   }

@@ -558,27 +563,42 @@ static AOM_FORCE_INLINE void update_coeff_simple_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 >= 1) {
-    int ci;
-    tran_low_t qc;
-    int coeff_ctx;
-    do {
-      ci = scan[s];
-      assert(ci > 0);
-      coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
-                                                   nz_map_ctx, tx_class);
-      qc = qcoeff[ci];
-      if (qc == 0) {
+  if (sharpness != 0) {
+    while (s >= 1) {
+      int ci;
+      tran_low_t qc;
+      int coeff_ctx;
+      do {
+        ci = scan[s];
+        assert(ci > 0);
+        coeff_ctx = get_lower_levels_ctx_simple_fast(levels, 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;
+      local_rate += update_coeff_simple_gt1(ci, qc, coeff_ctx, tx_class, ctx);
+      --s;
+    }
+  } else {
+    while (s >= 1) {
+      int ci;
+      tran_low_t qc;
+      int coeff_ctx;
+      do {
+        ci = scan[s];
+        assert(ci > 0);
+        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];
-      } else if (sharpness != 0 && (unsigned int)(qc + 1) <= 2) {
-        local_rate += base_cost[coeff_ctx][5];
-      } else {
-        break;
-      }
-    } while (--s >= 1);
-    if (s < 1) break;
-    local_rate += update_coeff_simple_nz(ci, qc, coeff_ctx, tx_class, ctx);
-    --s;
+      } while (--s >= 1);
+      if (s < 1) break;
+      local_rate += update_coeff_simple_nz(ci, qc, coeff_ctx, tx_class, ctx);
+      --s;
+    }
   }
   ctx->accu_rate = local_rate;
   *si = 0;
@@ -695,9 +715,9 @@ int av1_optimize_txb_multi(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
       accu_dist +=
           ((int64_t)dqc * (dqc - (int64_t)tqc * 2)) * (1LL << (2 * shift));
     } else {
-      const int64_t qm_val = qmatrix[ci];
-      accu_dist += get_coeff_dist_qm(tqc, dqc, shift, qm_val) -
-                   get_coeff_dist_qm(tqc, 0, shift, qm_val);
+      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;
   }
@@ -900,10 +920,11 @@ int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
       dist_diff_low_0 =
           ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
     } else {
-      const int64_t qm_val = qmatrix[0];
-      const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
-      dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
-      dist_diff_low_0 = get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0;
+      const int64_t scaled_qm = (int64_t)qmatrix[0] << 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;
     }
     const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
     const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
diff --git a/av1/encoder/txb_rdopt_utils.h b/av1/encoder/txb_rdopt_utils.h
index 1b4dd28611..eddd5f94f2 100644
--- a/av1/encoder/txb_rdopt_utils.h
+++ b/av1/encoder/txb_rdopt_utils.h
@@ -47,6 +47,13 @@ static inline int64_t get_coeff_dist_qm(tran_low_t tcoeff, tran_low_t dqcoeff,
   return (diff * diff + (1 << (2 * AOM_QM_BITS - 1))) >> (2 * AOM_QM_BITS);
 }

+static inline int64_t get_coeff_dist_scaled_qm(tran_low_t tcoeff,
+                                               tran_low_t dqcoeff,
+                                               int64_t scaled_qm) {
+  const int64_t diff = (int64_t)(tcoeff - dqcoeff) * scaled_qm;
+  return (diff * diff + (1 << (2 * AOM_QM_BITS - 1))) >> (2 * AOM_QM_BITS);
+}
+
 static inline int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
                                      int shift, const qm_val_t *qmatrix,
                                      int coeff_idx) {