Commit 776a4e2e8e for aom

commit 776a4e2e8e359f14b9ab1dbfbd0032f981f33d3d
Author: Frank Galligan <fgalligan@google.com>
Date:   Tue Nov 10 19:41:49 2026 -0800

    encodetxb: pack tcoeff sequentially via tc_pos

    Add a per-plane tc_pos[MAX_MB_PLANE] cursor on MACROBLOCK and advance
    it via advance_tc_pos() (by 1 for eob == 1, 2 for is_fast_eob2(), or
    max_eob otherwise) so tcoeff blocks are packed and read back-to-back
    in coding order while keeping CB_COEFF_BUFFER read-only during
    bitstream writing.

    Use aligned qcoeff instead of tcoeff for av1_txb_init_levels(),
    entropy context, and DC sign checks in record_txb_context_output() and
    update_and_record_txb_context_multi().

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
      -0.93% (1T) / -2.32% (8T) 1080p allintra,
      -0.87% (1T) / -5.04% (8T) 720p allintra, -0.40% 720p good (8T).
    Binary size (x86_64 -O3): libaom.a .text +0.3 KiB (+0.00%),
      .rodata +0.1 KiB (+0.01%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 1T -2156 KiB (-4.47%),
      8T -7796 KiB (-13.90%).

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

    Change-Id: Ie7b87a63f6a7397c5d2f410eed2f3e6b81decb59

diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 991ba72590..4f6ade0318 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1766,6 +1766,7 @@ static inline void write_modes(AV1_COMP *const cpi, ThreadData *const td,
     for (int mi_col = mi_col_start; mi_col < mi_col_end;
          mi_col += cm->seq_params->mib_size) {
       td->mb.cb_coef_buff = av1_get_cb_coeff_buffer(cpi, mi_row, mi_col);
+      av1_zero(td->mb.tc_pos);
       write_modes_sb(cpi, td, tile, w, &tok, tok_end, mi_row, mi_col,
                      cm->seq_params->sb_size);
     }
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index de08795970..ac2ad4e49d 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -951,6 +951,13 @@ typedef struct macroblock {
   CB_COEFF_BUFFER *cb_coef_buff;
   //! Offset of current coding block's coeff buffer relative to the sb.
   uint16_t cb_offset[PLANE_TYPES];
+  /*! \brief Per-plane cursor (in tran_low_t units) into cb_coef_buff.
+   *
+   * Reset per superblock in coding (when cb_offset[PLANE_TYPE_Y] == 0) and
+   * write_modes(). Coefficient writers and av1_write_coeffs_txb() must
+   * advance it in matching order, improving cache locality.
+   */
+  uint16_t tc_pos[MAX_MB_PLANE];

   //! Modified source and masks used for fast OBMC search.
   OBMCBuffer obmc_buffer;
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 7c0bf91b2c..ab95aba478 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -11,6 +11,7 @@

 #include "av1/encoder/encodetxb.h"

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

 #include "aom_ports/mem.h"
@@ -288,11 +289,38 @@ void av1_get_nz_map_contexts_c(const uint8_t *const levels,
   }
 }

+static inline tran_low_t *advance_tc_pos(const AV1_COMMON *const cm,
+                                         MACROBLOCK *const x, int plane,
+                                         int count) {
+  assert(plane >= 0 && plane < MAX_MB_PLANE);
+  const int max_sb_square =
+      (1 << num_pels_log2_lookup[cm->seq_params->sb_size]) >>
+      (plane ? (cm->seq_params->subsampling_x + cm->seq_params->subsampling_y)
+             : 0);
+  const int pos = x->tc_pos[plane];
+  assert(count > 0 && pos + count <= max_sb_square);
+  (void)max_sb_square;
+  x->tc_pos[plane] = (uint16_t)(pos + count);
+  return x->cb_coef_buff->tcoeff[plane] + pos;
+}
+
+static inline int is_fast_eob2(int eob, TX_CLASS tx_class, tran_low_t dc) {
+#if CONFIG_ENTROPY_STATS
+  (void)eob;
+  (void)tx_class;
+  (void)dc;
+  return 0;
+#else
+  return eob == 2 && tx_class == TX_CLASS_2D &&
+         (unsigned int)(dc + NUM_BASE_LEVELS) <= 2 * NUM_BASE_LEVELS;
+#endif
+}
+
 void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
                           aom_writer *w, int blk_row, int blk_col, int plane,
                           int block, TX_SIZE tx_size) {
   MACROBLOCKD *xd = &x->e_mbd;
-  const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
+  const CB_COEFF_BUFFER *const cb_coef_buff = x->cb_coef_buff;
   const PLANE_TYPE plane_type = get_plane_type(plane);
   const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
                          (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
@@ -323,24 +351,29 @@ void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
   const int height = get_txb_high(tx_size);
   uint8_t levels_buf[TX_PAD_2D];
   uint8_t *const levels = set_levels(levels_buf, height);
-  const tran_low_t *tcoeff_txb =
-      cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
-  const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
+  const tran_low_t *const tcoeff =
+      cb_coef_buff->tcoeff[plane] + x->tc_pos[plane];
   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
   const int16_t *const scan = scan_order->scan;
   assert(scan[0] == 0);
   DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
+  tran_low_t last_coeff;
   if (eob == 1) {
+    advance_tc_pos(cm, x, plane, 1);
     coeff_contexts[0] = 0;
-  } else if (eob == 2 && tx_class == TX_CLASS_2D &&
-             (unsigned int)(tcoeff[0] + NUM_BASE_LEVELS) <=
-                 2 * NUM_BASE_LEVELS) {
+    last_coeff = tcoeff[0];
+  } else if (is_fast_eob2(eob, tx_class, tcoeff[0])) {
+    advance_tc_pos(cm, x, plane, 2);
     coeff_contexts[0] = 0;
     coeff_contexts[scan[1]] = 1;
+    last_coeff = tcoeff[1];
   } else {
+    assert(width * height == av1_get_max_eob(tx_size));
+    advance_tc_pos(cm, x, plane, av1_get_max_eob(tx_size));
     av1_txb_init_levels(tcoeff, width, height, levels);
     av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
                             coeff_contexts);
+    last_coeff = tcoeff[scan[eob - 1]];
   }

   // The rest of the block only writes symbols, so keep the range coder state
@@ -408,7 +441,7 @@ void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
   {
     // The last coefficient is never 0 and has its own cdf.
     const int pos = scan[eob - 1];
-    const tran_low_t v = tcoeff[pos];
+    const tran_low_t v = last_coeff;
     const tran_low_t level = abs(v);
     nz_coeffs[num_nz++] = v;
     aom_write_symbol_state(
@@ -597,9 +630,9 @@ __attribute__((noinline))
 static uint8_t update_and_record_txb_context_multi(
     const AV1_COMP *cpi, ThreadData *td, FRAME_CONTEXT *ec_ctx,
     const tran_low_t *qcoeff, tran_low_t *tcoeff, uint8_t *entropy_ctx,
-    int block, int eob, int seg_eob, PLANE_TYPE plane_type, TX_SIZE tx_size,
-    TX_SIZE txsize_ctx, TX_CLASS tx_class, const SCAN_ORDER *scan_order,
-    int dc_sign_ctx, uint8_t allow_update_cdf) {
+    int block, int eob, int seg_eob, int fast_eob2, PLANE_TYPE plane_type,
+    TX_SIZE tx_size, TX_SIZE txsize_ctx, TX_CLASS tx_class,
+    const SCAN_ORDER *scan_order, int dc_sign_ctx, uint8_t allow_update_cdf) {
   const int bhl = get_txb_bhl(tx_size);
   const int width = get_txb_wide(tx_size);
   const int height = get_txb_high(tx_size);
@@ -614,22 +647,16 @@ static uint8_t update_and_record_txb_context_multi(
   uint8_t levels_buf[TX_PAD_2D];
   uint8_t *const levels = set_levels(levels_buf, height);
   DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
-  const int fast_eob2 =
-      eob == 2 && tx_class == TX_CLASS_2D &&
-      (unsigned int)(qcoeff[0] + NUM_BASE_LEVELS) <= 2 * NUM_BASE_LEVELS;
+  assert(fast_eob2 == is_fast_eob2(eob, tx_class, qcoeff[0]));
   if (fast_eob2) {
     assert(scan[0] == 0);
-#if CONFIG_ENTROPY_STATS
-    memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
-#else
     tcoeff[0] = qcoeff[0];
-    tcoeff[scan[1]] = qcoeff[scan[1]];
-#endif
+    tcoeff[1] = qcoeff[scan[1]];
     coeff_contexts[0] = 0;
     coeff_contexts[scan[1]] = 1;
   } else {
     memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
-    av1_txb_init_levels(tcoeff, width, height, levels);
+    av1_txb_init_levels(qcoeff, width, height, levels);
     av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
                             coeff_contexts);
   }
@@ -723,8 +750,8 @@ static uint8_t update_and_record_txb_context_multi(
   if (cpi->mt_info.pack_bs_mt_enabled) td->abs_sum_level += abs_sum;
   int dc_sign_mask = 0;
   // Update the context needed to code the DC sign (if applicable)
-  if (tcoeff[0] != 0) {
-    const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
+  if (qcoeff[0] != 0) {
+    const int dc_sign = (qcoeff[0] < 0) ? 1 : 0;
 #if CONFIG_ENTROPY_STATS
     ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
 #endif  // CONFIG_ENTROPY_STATS
@@ -794,9 +821,6 @@ static void update_and_record_txb_context_output(
                       cm->features.reduced_tx_set_used);
   const int segment_id = mbmi->segment_id;
   const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
-  tran_low_t *tcoeff_txb =
-      cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
-  tran_low_t *tcoeff = tcoeff_txb + block_offset;
   const TX_CLASS tx_class = tx_type_to_class[tx_type];

   update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
@@ -815,6 +839,7 @@ static void update_and_record_txb_context_output(

   uint8_t cul_level;
   if (eob == 1) {
+    tran_low_t *const tcoeff = advance_tc_pos(cm, x, plane, 1);
     const tran_low_t v = qcoeff[0];
     assert(v != 0);
     tcoeff[0] = v;
@@ -864,9 +889,13 @@ static void update_and_record_txb_context_output(
                           ((2 - dc_sign) << COEFF_CONTEXT_BITS));
   } else {
     const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
+    assert(seg_eob == av1_get_max_eob(tx_size));
+    const int fast_eob2 = is_fast_eob2(eob, tx_class, qcoeff[0]);
+    tran_low_t *const tcoeff =
+        advance_tc_pos(cm, x, plane, fast_eob2 ? 2 : seg_eob);
     cul_level = update_and_record_txb_context_multi(
         cpi, td, ec_ctx, qcoeff, tcoeff, entropy_ctx, block, eob, seg_eob,
-        plane_type, tx_size, txsize_ctx, tx_class, scan_order,
+        fast_eob2, plane_type, tx_size, txsize_ctx, tx_class, scan_order,
         txb_ctx.dc_sign_ctx, allow_update_cdf);
   }
   av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
@@ -920,38 +949,34 @@ static void record_txb_context_output(int plane, int block, int blk_row,
   const TX_TYPE tx_type =
       av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
                       cm->features.reduced_tx_set_used);
+  const TX_CLASS tx_class = tx_type_to_class[tx_type];
   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
   const int segment_id = mbmi->segment_id;
   const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
-  tran_low_t *tcoeff_txb =
-      cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
-  tran_low_t *tcoeff = tcoeff_txb + block_offset;
+  tran_low_t *tcoeff;
   const int16_t *const scan = scan_order->scan;
-#if !CONFIG_ENTROPY_STATS
   if (eob == 1) {
     assert(scan[0] == 0);
+    tcoeff = advance_tc_pos(cm, x, plane, 1);
     tcoeff[0] = qcoeff[0];
-  } else if (eob == 2 && tx_type_to_class[tx_type] == TX_CLASS_2D &&
-             (unsigned int)(qcoeff[0] + NUM_BASE_LEVELS) <=
-                 2 * NUM_BASE_LEVELS) {
+  } else if (is_fast_eob2(eob, tx_class, qcoeff[0])) {
     assert(scan[0] == 0);
+    tcoeff = advance_tc_pos(cm, x, plane, 2);
     tcoeff[0] = qcoeff[0];
-    tcoeff[scan[1]] = qcoeff[scan[1]];
+    tcoeff[1] = qcoeff[scan[1]];
   } else {
+    assert(seg_eob == av1_get_max_eob(tx_size));
+    tcoeff = advance_tc_pos(cm, x, plane, seg_eob);
     memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
   }
-#else
-  memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
-#endif

 #if CONFIG_ENTROPY_STATS
   uint8_t levels_buf[TX_PAD_2D];
   uint8_t *const levels = set_levels(levels_buf, height);
-  av1_txb_init_levels(tcoeff, width, height, levels);
+  av1_txb_init_levels(qcoeff, width, height, levels);
   update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
                        td->counts, 0 /*allow_update_cdf*/);

-  const TX_CLASS tx_class = tx_type_to_class[tx_type];
   const bool do_coeff_scan = true;
 #else
   const bool do_coeff_scan = cpi->mt_info.pack_bs_mt_enabled;
@@ -1006,16 +1031,16 @@ static void record_txb_context_output(int plane, int block, int blk_row,
 #endif
   }
   // Update the context needed to code the DC sign (if applicable)
-  if (tcoeff[0] != 0) {
+  if (qcoeff[0] != 0) {
     const int dc_sign_ctx = txb_ctx.dc_sign_ctx;
 #if CONFIG_ENTROPY_STATS
-    const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
+    const int dc_sign = (qcoeff[0] < 0) ? 1 : 0;
     ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
 #endif  // CONFIG_ENTROPY_STATS
     entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT;
   }
   const uint8_t cul_level =
-      av1_get_txb_entropy_context(tcoeff, scan_order, eob);
+      av1_get_txb_entropy_context(qcoeff, scan_order, eob);
   av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
                            blk_col, blk_row);
 }
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index d63c1c3b5b..ac9e1735aa 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -1456,6 +1456,9 @@ static void encode_b(const AV1_COMP *const cpi, TileDataEnc *tile_data,
   if (!dry_run) {
     set_cb_offsets(x->mbmi_ext_frame->cb_offset, x->cb_offset[PLANE_TYPE_Y],
                    x->cb_offset[PLANE_TYPE_UV]);
+    if (x->cb_offset[PLANE_TYPE_Y] == 0) {
+      av1_zero(x->tc_pos);
+    }
     assert(x->cb_offset[PLANE_TYPE_Y] <
            (1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]));
     assert(x->cb_offset[PLANE_TYPE_UV] <
@@ -2127,6 +2130,9 @@ static void encode_b_nonrd(const AV1_COMP *const cpi, TileDataEnc *tile_data,
   if (!dry_run) {
     set_cb_offsets(x->mbmi_ext_frame->cb_offset, x->cb_offset[PLANE_TYPE_Y],
                    x->cb_offset[PLANE_TYPE_UV]);
+    if (x->cb_offset[PLANE_TYPE_Y] == 0) {
+      av1_zero(x->tc_pos);
+    }
     assert(x->cb_offset[PLANE_TYPE_Y] <
            (1 << num_pels_log2_lookup[cpi->common.seq_params->sb_size]));
     assert(x->cb_offset[PLANE_TYPE_UV] <