Commit 9dad4f7d49 for aom

commit 9dad4f7d4933e77e47180801ea54fb3190e9d332
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 26 04:18:00 2026 -0800

    txb_rdopt: filter update_coeff_dc with margin

    Add an approximate rate-distortion margin fast path to update_coeff_dc()
    sharing a factored dist_term across both quantization matrix paths. For
    qmatrix != NULL, skip exact get_coeff_dist_scaled_qm() calls outside the
    [0, 1 << RDDIV_BITS] rounding margin and fall back to cold
    eval_better_qm_exact() near the boundary. For qmatrix == NULL, compare
    approx_rd against the factored dd_pos fast path and evaluate the exact
    rate tie-breaker only when approx_rd == dd_pos.

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
      -2.05% (1T) / -3.21% (8T) 1080p allintra, -0.37% (1T) / -1.00% (8T)
      720p allintra, -0.27% 720p good (1T).
    Binary size (x86_64 -O3): libaom.a .text +0.1 KiB (+0.00%),
      .rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 1T +3304 KiB (+8.06%),
      8T +0 KiB (+0.00%).

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

    Change-Id: Iee21ee6c3bd330fb169fe00656621fd022dfdc61

diff --git a/av1/encoder/txb_rdopt.c b/av1/encoder/txb_rdopt.c
index 42d00b8328..c3673495c0 100644
--- a/av1/encoder/txb_rdopt.c
+++ b/av1/encoder/txb_rdopt.c
@@ -78,6 +78,19 @@ static AOM_FORCE_INLINE void update_coeff_last(
   }
 }

+// Cold [0, 1 << RDDIV_BITS] tie-breaker helper kept out of line to keep
+// update_coeff_dc() compact.
+static AOM_NOINLINE int eval_better_qm_exact(tran_low_t tqc, tran_low_t dqc,
+                                             tran_low_t dqc_low,
+                                             int64_t scaled_qm, int rate,
+                                             int rate_low, int64_t rdmult) {
+  const int64_t dist_diff = get_coeff_dist_scaled_qm(tqc, dqc, scaled_qm) -
+                            get_coeff_dist_scaled_qm(tqc, dqc_low, scaled_qm);
+  const int64_t rd_thresh =
+      RDCOST(rdmult, rate_low, 0) - RDCOST(rdmult, rate, 0);
+  return dist_diff * (1 << RDDIV_BITS) > rd_thresh;
+}
+
 // Specialized trellis update for the DC coefficient (si == 0) when it is not
 // the last coefficient in the block.
 static AOM_FORCE_INLINE void update_coeff_dc(
@@ -123,28 +136,45 @@ static AOM_FORCE_INLINE void update_coeff_dc(
       }
     }

-    int64_t dist_diff_0, dist_diff_low_0;
+    const int64_t approx_rd =
+        ((int64_t)(rate - rate_low) * rdmult) >> AV1_PROB_COST_SHIFT;
+    const int64_t sum_term = (int64_t)tqc * 2 - (int64_t)dqc - (int64_t)dqc_low;
+    const int64_t dist_term = ((int64_t)dqc - (int64_t)dqc_low) * sum_term;
+    int better;
     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 =
-          (abs_qc == 1)
-              ? 0
-              : (((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift)));
+      const int64_t dd_pos = dist_term * (1LL << (2 * shift + RDDIV_BITS));
+      if (approx_rd >= dd_pos) {
+        if (approx_rd > dd_pos) {
+          better = 1;
+        } else {
+          better =
+              RDCOST(rdmult, rate, 0) - RDCOST(rdmult, rate_low, 0) > dd_pos;
+        }
+      } else {
+        better = 0;
+      }
     } else {
-      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_scaled_qm(tqc, dqc_low, scaled_qm) - dist0);
+      const int scaled_qm = qmatrix[0] << shift;
+      const int sq_qm = scaled_qm * scaled_qm;
+      const int64_t approx_dd = (dist_term * sq_qm) >> (2 * AOM_QM_BITS);
+      // Floor-shifted diffs underestimate rounded values by 0 or 1: both
+      // R1 - R0 - approx_rd and D0 - D1 - approx_dd lie in {0, 1}. For exact
+      // rd - rd_low = margin + e_R - e_D * (1 << RDDIV_BITS), margin < 0
+      // guarantees exact <= 0 and margin > (1 << RDDIV_BITS) guarantees > 0.
+      const int64_t margin = approx_rd - (approx_dd * (1 << RDDIV_BITS));
+      if (margin >= 0) {
+        if (margin > (1 << RDDIV_BITS)) {
+          better = 1;
+        } else {
+          better = eval_better_qm_exact(tqc, dqc, dqc_low, scaled_qm, rate,
+                                        rate_low, rdmult);
+        }
+      } else {
+        better = 0;
+      }
     }

-    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) {
+    if (better) {
       qcoeff[0] = qc_low;
       dqcoeff[0] = dqc_low;
       levels[0] = AOMMIN(abs_qc_low, INT8_MAX);