Commit 5fab5b9fe2 for aom

commit 5fab5b9fe238626c367a8a6c8f11e15c74a9ea00
Author: Frank Galligan <fgalligan@google.com>
Date:   Fri Nov 13 10:44:00 2026 -0800

    av1: Direct LBD intra predictors in C

    Predict 8-bit DC_PRED (HAVE_SSE2 && txwpx < 32), vertical (90-degree),
    and horizontal (180-degree) blocks directly from reference pointers
    without stack staging buffers, outline predict_cfl_block_facade, add
    pure C av1_fill_block_lbd in reconintra.h, and optimize single-tx intra
    encoding (out-of-line encode_block_intra) and context skips in
    encodemb.c.

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

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

diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index e4fe34a540..5008db6eca 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -37,6 +37,7 @@ enum {
 #define MAX_UPSAMPLE_SZ 16
 #define NUM_INTRA_NEIGHBOUR_PIXELS (MAX_TX_SIZE * 2 + 32)

+#if CONFIG_AV1_HIGHBITDEPTH
 static const uint8_t extend_modes[INTRA_MODES] = {
   NEED_ABOVE | NEED_LEFT,                   // DC
   NEED_ABOVE,                               // V
@@ -52,6 +53,7 @@ static const uint8_t extend_modes[INTRA_MODES] = {
   NEED_LEFT | NEED_ABOVE,                   // SMOOTH_H
   NEED_LEFT | NEED_ABOVE | NEED_ABOVELEFT,  // PAETH
 };
+#endif  // CONFIG_AV1_HIGHBITDEPTH

 // Tables to store if the top-right reference pixels are available. The flags
 // are represented with bits, packed into 8-bit integers. E.g., for the 32x32
@@ -1126,37 +1128,6 @@ static AOM_FORCE_INLINE void fill_intra_edge_lbd(uint8_t *p, uint8_t val,
   }
 }

-static AOM_FORCE_INLINE void fill_block_lbd(uint8_t *dst, int dst_stride,
-                                            uint8_t val, int txwpx, int txhpx) {
-  if (txwpx == 4) {
-    for (int i = 0; i < txhpx; ++i) {
-      memset(dst, val, 4);
-      dst += dst_stride;
-    }
-  } else if (txwpx == 8) {
-    for (int i = 0; i < txhpx; ++i) {
-      memset(dst, val, 8);
-      dst += dst_stride;
-    }
-  } else if (txwpx == 16) {
-    for (int i = 0; i < txhpx; ++i) {
-      memset(dst, val, 16);
-      dst += dst_stride;
-    }
-  } else if (txwpx == 32) {
-    for (int i = 0; i < txhpx; ++i) {
-      memset(dst, val, 32);
-      dst += dst_stride;
-    }
-  } else {
-    assert(txwpx == 64);
-    for (int i = 0; i < txhpx; ++i) {
-      memset(dst, val, 64);
-      dst += dst_stride;
-    }
-  }
-}
-
 static void build_directional_and_filter_intra_predictors(
     const uint8_t *ref, int ref_stride, uint8_t *dst, int dst_stride,
     PREDICTION_MODE mode, int p_angle, FILTER_INTRA_MODE filter_intra_mode,
@@ -1187,7 +1158,7 @@ static void build_directional_and_filter_intra_predictors(
     } else {
       val = (n_left_px > 0) ? left_ref[0] : 127;
     }
-    fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
+    av1_fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
     return;
   }

@@ -1222,6 +1193,7 @@ static void build_directional_and_filter_intra_predictors(
     const int num_top_pixels_needed = txwpx + (n_topright_px >= 0 ? txhpx : 0);
     if (n_top_px > 0) {
       const int n_top_copy = n_top_px + (n_topright_px > 0 ? n_topright_px : 0);
+      assert(n_top_copy > 0);
       copy_above_row_lbd(above_row, above_ref, n_top_copy);
       if (n_top_copy < num_top_pixels_needed)
         fill_intra_edge_lbd(&above_row[n_top_copy], above_row[n_top_copy - 1],
@@ -1328,6 +1300,125 @@ static void build_directional_and_filter_intra_predictors(
                upsample_left, p_angle);
 }

+#if HAVE_SSE2
+static AOM_FORCE_INLINE uint32_t sum_above_row_lbd(const uint8_t *above_ref,
+                                                   int txwpx) {
+  uint32_t sum = 0;
+  for (int i = 0; i < txwpx; ++i) sum += above_ref[i];
+  return sum;
+}
+
+// Computes 8-bit DC_PRED directly from the reference boundary pixels without
+// staging above_row and left_col buffers on the stack.
+static void predict_dc_block_lbd_c(const uint8_t *ref, int ref_stride,
+                                   uint8_t *dst, int dst_stride,
+                                   TX_SIZE tx_size, int txwpx, int txhpx,
+                                   int n_top_px, int n_left_px) {
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  assert(txwpx == tx_size_wide[tx_size] && txhpx == tx_size_high[tx_size]);
+  assert(n_top_px >= 0 && n_top_px <= txwpx);
+  assert(n_left_px >= 0 && n_left_px <= txhpx);
+  uint32_t sum_top = 0;
+  if (n_top_px > 0) {
+    const uint8_t *const above_ref = ref - ref_stride;
+    if (LIKELY(n_top_px == txwpx)) {
+      sum_top = sum_above_row_lbd(above_ref, txwpx);
+    } else {
+      for (int i = 0; i < n_top_px; ++i) sum_top += above_ref[i];
+      sum_top +=
+          (uint32_t)above_ref[n_top_px - 1] * (uint32_t)(txwpx - n_top_px);
+    }
+  }
+
+  uint32_t sum_left = 0;
+  if (n_left_px > 0) {
+    const uint8_t *const left_ref = ref - 1;
+    if (LIKELY(n_left_px == txhpx)) {
+      sum_left = av1_sum_left_col_lbd(left_ref, ref_stride, txhpx);
+    } else {
+      uint8_t last_val = 0;
+      for (int i = 0; i < n_left_px; ++i) {
+        last_val = left_ref[(ptrdiff_t)i * ref_stride];
+        sum_left += last_val;
+      }
+      sum_left += (uint32_t)last_val * (uint32_t)(txhpx - n_left_px);
+    }
+  }
+
+  const uint8_t dc_val = av1_dc_val_from_sums_lbd(
+      tx_size, txwpx, txhpx, sum_top, sum_left, n_top_px, n_left_px);
+  av1_fill_block_lbd(dst, dst_stride, dc_val, txwpx, txhpx);
+}
+#endif  // HAVE_SSE2
+
+// Directly broadcasts the top reference row or left reference column into dst
+// for pure 90-degree (V_PRED) and 180-degree (H_PRED) blocks with width < 32
+// when all boundary pixels are in-bounds. Larger widths route via RTCD
+// predictors so AVX2 256-bit predictors are used when available.
+static AOM_FORCE_INLINE void v_pred_direct_lbd(uint8_t *dst, int dst_stride,
+                                               const uint8_t *above_ref,
+                                               int txwpx, int txhpx) {
+  assert(txwpx <= 16);
+  assert(txhpx >= 4 && (txhpx & 1) == 0);
+  if (txwpx == 4) {
+    uint32_t v;
+    memcpy(&v, above_ref, 4);
+    do {
+      memcpy(dst, &v, 4);
+      memcpy(dst + dst_stride, &v, 4);
+      dst += dst_stride * 2;
+      txhpx -= 2;
+    } while (txhpx != 0);
+  } else if (txwpx == 8) {
+    uint64_t v;
+    memcpy(&v, above_ref, 8);
+    do {
+      memcpy(dst, &v, 8);
+      memcpy(dst + dst_stride, &v, 8);
+      dst += dst_stride * 2;
+      txhpx -= 2;
+    } while (txhpx != 0);
+  } else {
+    assert(txwpx == 16);
+    uint8_t v[16];
+    memcpy(v, above_ref, 16);
+    do {
+      memcpy(dst, v, 16);
+      memcpy(dst + dst_stride, v, 16);
+      dst += dst_stride * 2;
+      txhpx -= 2;
+    } while (txhpx != 0);
+  }
+}
+
+static AOM_FORCE_INLINE void h_pred_direct_lbd(uint8_t *dst, int dst_stride,
+                                               const uint8_t *left_ref,
+                                               int ref_stride, int txwpx,
+                                               int txhpx) {
+  assert(txwpx <= 16);
+  assert(txhpx >= 4 && (txhpx & 3) == 0);
+  if (txwpx == 4) {
+    do {
+      fill_intra_edge_lbd(dst, *left_ref, 4);
+      dst += dst_stride;
+      left_ref += ref_stride;
+    } while (--txhpx != 0);
+  } else if (txwpx == 8) {
+    do {
+      fill_intra_edge_lbd(dst, *left_ref, 8);
+      dst += dst_stride;
+      left_ref += ref_stride;
+    } while (--txhpx != 0);
+  } else {
+    assert(txwpx == 16);
+    do {
+      fill_intra_edge_lbd(dst, *left_ref, 16);
+      dst += dst_stride;
+      left_ref += ref_stride;
+    } while (--txhpx != 0);
+  }
+}
+
 // This function generates the pred data of a given block for non-directional
 // intra prediction modes (i.e., DC, SMOOTH, SMOOTH_H, SMOOTH_V and PAETH).
 static void build_non_directional_intra_predictors(
@@ -1337,59 +1428,41 @@ static void build_non_directional_intra_predictors(
   const uint8_t *left_ref = ref - 1;
   const int txwpx = tx_size_wide[tx_size];
   const int txhpx = tx_size_high[tx_size];
-  const int need_left = extend_modes[mode] & NEED_LEFT;
-  const int need_above = extend_modes[mode] & NEED_ABOVE;
-  const int need_above_left = extend_modes[mode] & NEED_ABOVELEFT;
   int i = 0;
   assert(n_top_px >= 0);
   assert(n_left_px >= 0);
   assert(mode == DC_PRED || mode == SMOOTH_PRED || mode == SMOOTH_V_PRED ||
          mode == SMOOTH_H_PRED || mode == PAETH_PRED);

-  if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
-    uint8_t val = 0;
-    if (need_left) {
-      val = (n_top_px > 0) ? above_ref[0] : 129;
-    } else {
-      val = (n_left_px > 0) ? left_ref[0] : 127;
-    }
-    fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
-    return;
-  }
-
   DECLARE_ALIGNED(16, uint8_t, left_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
   DECLARE_ALIGNED(16, uint8_t, above_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
   uint8_t *const above_row = above_data + 16;
   uint8_t *const left_col = left_data + 16;

-  if (need_left) {
-    if (n_left_px > 0) {
+  if (n_left_px > 0) {
 #if defined(__GNUC__) && !defined(__clang__)
 #pragma GCC novector
 #endif
-      for (i = 0; i < n_left_px; i++) left_col[i] = left_ref[i * ref_stride];
-      if (i < txhpx)
-        fill_intra_edge_lbd(&left_col[i], left_col[i - 1], txhpx - i);
-    } else {
-      const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
-      fill_intra_edge_lbd(left_col, val, txhpx);
-    }
+    for (i = 0; i < n_left_px; i++) left_col[i] = left_ref[i * ref_stride];
+    if (UNLIKELY(i < txhpx))
+      fill_intra_edge_lbd(&left_col[i], left_col[i - 1], txhpx - i);
+  } else if (mode != DC_PRED) {
+    const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
+    fill_intra_edge_lbd(left_col, val, txhpx);
   }

-  if (need_above) {
-    if (LIKELY(n_top_px == txwpx)) {
-      copy_above_row_lbd(above_row, above_ref, txwpx);
-    } else if (n_top_px > 0) {
-      copy_above_row_lbd(above_row, above_ref, n_top_px);
-      fill_intra_edge_lbd(&above_row[n_top_px], above_row[n_top_px - 1],
-                          txwpx - n_top_px);
-    } else {
-      const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
-      fill_intra_edge_lbd(above_row, val, txwpx);
-    }
+  if (LIKELY(n_top_px == txwpx)) {
+    copy_above_row_lbd(above_row, above_ref, txwpx);
+  } else if (n_top_px > 0) {
+    copy_above_row_lbd(above_row, above_ref, n_top_px);
+    fill_intra_edge_lbd(&above_row[n_top_px], above_row[n_top_px - 1],
+                        txwpx - n_top_px);
+  } else if (mode != DC_PRED) {
+    const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
+    fill_intra_edge_lbd(above_row, val, txwpx);
   }

-  if (need_above_left) {
+  if (mode == PAETH_PRED) {
     // aom_paeth_predictor_{4x4,8x4}_ssse3 load 8 bytes from left and above
     // before shuffling with a runtime mask; zero bytes 4..7 to avoid
     // uninitialized reads under Valgrind/MemorySanitizer (see aomedia:2752).
@@ -1892,6 +1965,14 @@ static AOM_FORCE_INLINE void predict_intra_block_internal(
       return;
     }
 #endif  // CONFIG_AV1_HIGHBITDEPTH
+#if HAVE_SSE2
+    // Keep RTCD dc_pred for non-SSE2/NEON targets and wide >= 32 AVX2 blocks.
+    if (mode == DC_PRED && txwpx < 32) {
+      predict_dc_block_lbd_c(ref, ref_stride, dst, dst_stride, tx_size, txwpx,
+                             txhpx, n_top_px, n_left_px);
+      return;
+    }
+#endif  // HAVE_SSE2
     build_non_directional_intra_predictors(ref, ref_stride, dst, dst_stride,
                                            mode, tx_size, n_top_px, n_left_px);
     return;
@@ -1903,6 +1984,15 @@ static AOM_FORCE_INLINE void predict_intra_block_internal(

   if (is_dr_mode) {
     p_angle = mode_to_angle_map[mode] + angle_delta;
+    if (!use_filter_intra && !is_hbd) {
+      if (p_angle == 90 && LIKELY(n_top_px == txwpx) && txwpx < 32) {
+        v_pred_direct_lbd(dst, dst_stride, ref - ref_stride, txwpx, txhpx);
+        return;
+      } else if (p_angle == 180 && LIKELY(n_left_px == txhpx) && txwpx < 32) {
+        h_pred_direct_lbd(dst, dst_stride, ref - 1, ref_stride, txwpx, txhpx);
+        return;
+      }
+    }
     if (p_angle < 90) {
       n_topright_px = 0;
       if (have_top && xr > 0) {
@@ -1973,10 +2063,60 @@ void av1_predict_intra_block(const MACROBLOCKD *xd, BLOCK_SIZE sb_size,
                                dst_stride, col_off, row_off, plane);
 }

+#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+// Keep CfL prediction cold/out-of-line so non-CfL blocks stay compact.
+static AOM_NOINLINE void predict_cfl_block_facade(const AV1_COMMON *cm,
+                                                  MACROBLOCKD *xd, int plane,
+                                                  int blk_col, int blk_row,
+                                                  TX_SIZE tx_size) {
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+  struct macroblockd_plane *const pd = &xd->plane[plane];
+  const int dst_stride = pd->dst.stride;
+  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
+  const int use_palette = mbmi->palette_mode_info.palette_size[plane != 0] > 0;
+  const SequenceHeader *seq_params = cm->seq_params;
+#if CONFIG_DEBUG
+  assert(is_cfl_allowed(xd));
+  const BLOCK_SIZE plane_bsize =
+      get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
+  (void)plane_bsize;
+  assert(plane_bsize < BLOCK_SIZES_ALL);
+  if (!xd->lossless[mbmi->segment_id]) {
+    assert(blk_col == 0);
+    assert(blk_row == 0);
+    assert(block_size_wide[plane_bsize] == tx_size_wide[tx_size]);
+    assert(block_size_high[plane_bsize] == tx_size_high[tx_size]);
+  }
+#endif
+  CFL_CTX *const cfl = &xd->cfl;
+  CFL_PRED_TYPE pred_plane = get_cfl_pred_type(plane);
+  if (!cfl->dc_pred_is_cached[pred_plane]) {
+    predict_intra_block_internal(
+        xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
+        pd->width, pd->height, tx_size, DC_PRED, 0, use_palette,
+        FILTER_INTRA_MODES, dst, dst_stride, dst, dst_stride, blk_col, blk_row,
+        plane);
+    if (cfl->use_dc_pred_cache) {
+      cfl_store_dc_pred(xd, dst, pred_plane, tx_size_wide[tx_size]);
+      cfl->dc_pred_is_cached[pred_plane] = true;
+    }
+  } else {
+    cfl_load_dc_pred(xd, dst, dst_stride, tx_size, pred_plane);
+  }
+  av1_cfl_predict_block(xd, dst, dst_stride, tx_size, plane);
+}
+#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+
 void av1_predict_intra_block_facade(const AV1_COMMON *cm, MACROBLOCKD *xd,
                                     int plane, int blk_col, int blk_row,
                                     TX_SIZE tx_size) {
   const MB_MODE_INFO *const mbmi = xd->mi[0];
+#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+  if (UNLIKELY(plane != AOM_PLANE_Y && mbmi->uv_mode == UV_CFL_PRED)) {
+    predict_cfl_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
   struct macroblockd_plane *const pd = &xd->plane[plane];
   const int dst_stride = pd->dst.stride;
   uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
@@ -1989,41 +2129,6 @@ void av1_predict_intra_block_facade(const AV1_COMMON *cm, MACROBLOCKD *xd,
           : FILTER_INTRA_MODES;
   const int angle_delta = mbmi->angle_delta[plane != AOM_PLANE_Y] * ANGLE_STEP;
   const SequenceHeader *seq_params = cm->seq_params;
-
-#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
-  if (UNLIKELY(plane != AOM_PLANE_Y && mbmi->uv_mode == UV_CFL_PRED)) {
-#if CONFIG_DEBUG
-    assert(is_cfl_allowed(xd));
-    const BLOCK_SIZE plane_bsize =
-        get_plane_block_size(mbmi->bsize, pd->subsampling_x, pd->subsampling_y);
-    (void)plane_bsize;
-    assert(plane_bsize < BLOCK_SIZES_ALL);
-    if (!xd->lossless[mbmi->segment_id]) {
-      assert(blk_col == 0);
-      assert(blk_row == 0);
-      assert(block_size_wide[plane_bsize] == tx_size_wide[tx_size]);
-      assert(block_size_high[plane_bsize] == tx_size_high[tx_size]);
-    }
-#endif
-    CFL_CTX *const cfl = &xd->cfl;
-    CFL_PRED_TYPE pred_plane = get_cfl_pred_type(plane);
-    if (!cfl->dc_pred_is_cached[pred_plane]) {
-      predict_intra_block_internal(
-          xd, seq_params->sb_size, seq_params->enable_intra_edge_filter,
-          pd->width, pd->height, tx_size, mode, angle_delta, use_palette,
-          filter_intra_mode, dst, dst_stride, dst, dst_stride, blk_col, blk_row,
-          plane);
-      if (cfl->use_dc_pred_cache) {
-        cfl_store_dc_pred(xd, dst, pred_plane, tx_size_wide[tx_size]);
-        cfl->dc_pred_is_cached[pred_plane] = true;
-      }
-    } else {
-      cfl_load_dc_pred(xd, dst, dst_stride, tx_size, pred_plane);
-    }
-    av1_cfl_predict_block(xd, dst, dst_stride, tx_size, plane);
-    return;
-  }
-#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
   predict_intra_block_internal(
       xd, seq_params->sb_size, seq_params->enable_intra_edge_filter, pd->width,
       pd->height, tx_size, mode, angle_delta, use_palette, filter_intra_mode,
diff --git a/av1/common/reconintra.h b/av1/common/reconintra.h
index 0910a68bd6..05b94165ae 100644
--- a/av1/common/reconintra.h
+++ b/av1/common/reconintra.h
@@ -12,7 +12,11 @@
 #ifndef AOM_AV1_COMMON_RECONINTRA_H_
 #define AOM_AV1_COMMON_RECONINTRA_H_

+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
 #include <stdlib.h>
+#include <string.h>

 #include "aom/aom_integer.h"
 #include "av1/common/av1_common_int.h"
@@ -152,6 +156,111 @@ static inline int av1_use_intra_edge_upsample(int bs0, int bs1, int delta,
   if (d == 0 || d >= 40) return 0;
   return type ? (blk_wh <= 8) : (blk_wh <= 16);
 }
+static AOM_FORCE_INLINE uint32_t av1_sum_left_col_lbd(const uint8_t *left_ref,
+                                                      ptrdiff_t ref_stride,
+                                                      int txhpx) {
+  assert(txhpx >= 4 && (txhpx & 3) == 0);
+  const ptrdiff_t s2 = ref_stride * 2;
+  const ptrdiff_t s3 = ref_stride * 3;
+  const ptrdiff_t s4 = ref_stride * 4;
+  uint32_t sum0 = (uint32_t)left_ref[0] + (uint32_t)left_ref[ref_stride];
+  uint32_t sum1 = (uint32_t)left_ref[s2] + (uint32_t)left_ref[s3];
+  while ((txhpx -= 4) != 0) {
+    left_ref += s4;
+    sum0 += (uint32_t)left_ref[0] + (uint32_t)left_ref[ref_stride];
+    sum1 += (uint32_t)left_ref[s2] + (uint32_t)left_ref[s3];
+  }
+  return sum0 + sum1;
+}
+
+static AOM_FORCE_INLINE void av1_fill_block_lbd(uint8_t *dst,
+                                                ptrdiff_t dst_stride,
+                                                uint8_t val, int txwpx,
+                                                int txhpx) {
+  assert(txhpx >= 4);
+  if (txwpx == 4) {
+    assert((txhpx & 3) == 0);
+    const uint32_t v = (uint32_t)val * 0x01010101u;
+    // Avoid GCC over-unrolling the 4-row fill loop.
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      memcpy(dst, &v, 4);
+      memcpy(dst + dst_stride, &v, 4);
+      dst += dst_stride * 2;
+      memcpy(dst, &v, 4);
+      memcpy(dst + dst_stride, &v, 4);
+      dst += dst_stride * 2;
+      txhpx -= 4;
+    } while (txhpx != 0);
+  } else if (txwpx == 8) {
+    assert((txhpx & 3) == 0);
+    const uint64_t v = (uint64_t)val * 0x0101010101010101ULL;
+    // Avoid GCC over-unrolling the 4-row fill loop.
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      memcpy(dst, &v, 8);
+      memcpy(dst + dst_stride, &v, 8);
+      dst += dst_stride * 2;
+      memcpy(dst, &v, 8);
+      memcpy(dst + dst_stride, &v, 8);
+      dst += dst_stride * 2;
+      txhpx -= 4;
+    } while (txhpx != 0);
+  } else if (txwpx == 16) {
+    do {
+      memset(dst, val, 16);
+      dst += dst_stride;
+    } while (--txhpx != 0);
+  } else if (txwpx == 32) {
+    do {
+      memset(dst, val, 32);
+      dst += dst_stride;
+    } while (--txhpx != 0);
+  } else {
+    assert(txwpx == 64);
+    do {
+      memset(dst, val, 64);
+      dst += dst_stride;
+    } while (--txhpx != 0);
+  }
+}
+
+static AOM_FORCE_INLINE uint8_t av1_dc_val_from_sums_lbd(
+    TX_SIZE tx_size, int txwpx, int txhpx, uint32_t sum_top, uint32_t sum_left,
+    int n_top_px, int n_left_px) {
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  if (n_top_px > 0 && n_left_px > 0) {
+    // Reciprocal multiplication tables to divide by (txwpx + txhpx) for square,
+    // 1:2 / 2:1, and 1:4 / 4:1 transform blocks.
+    static const uint8_t dc_shift1_tab[TX_SIZES_ALL] = {
+      2, 3, 4, 5, 6,  // 4x4, 8x8, 16x16, 32x32, 64x64
+      2, 2, 3, 3, 4, 4,
+      5, 5,             // 4x8, 8x4, 8x16, 16x8, 16x32, 32x16, 32x64, 64x32
+      2, 2, 3, 3, 4, 4  // 4x16, 16x4, 8x32, 32x8, 16x64, 64x16
+    };
+    static const uint16_t dc_mult_tab[TX_SIZES_ALL] = {
+      0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x5556, 0x5556,
+      0x5556, 0x5556, 0x5556, 0x5556, 0x5556, 0x5556, 0x3334,
+      0x3334, 0x3334, 0x3334, 0x3334, 0x3334
+    };
+    const uint32_t total =
+        sum_top + sum_left + (uint32_t)((txwpx + txhpx) >> 1);
+    return (
+        uint8_t)(((total >> dc_shift1_tab[tx_size]) * dc_mult_tab[tx_size]) >>
+                 16);
+  } else if (n_top_px > 0) {
+    const int log2_w = tx_size_wide_log2[tx_size];
+    return (uint8_t)((sum_top + (uint32_t)(txwpx >> 1)) >> log2_w);
+  } else if (n_left_px > 0) {
+    const int log2_h = tx_size_high_log2[tx_size];
+    return (uint8_t)((sum_left + (uint32_t)(txhpx >> 1)) >> log2_h);
+  }
+  return 128;
+}
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 73d1a7b628..58d228602f 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -13,6 +13,7 @@
 #include "config/av1_rtcd.h"
 #include "config/aom_dsp_rtcd.h"

+#include "aom_dsp/aom_dsp_common.h"
 #include "aom_dsp/bitwriter.h"
 #include "aom_dsp/quantize.h"
 #include "aom_mem/aom_mem.h"
@@ -638,9 +639,10 @@ void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
   }
 }

-static void encode_block_intra(int plane, int block, int blk_row, int blk_col,
-                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
-                               void *arg) {
+// Keep intra block encoding out-of-line so callers stay compact.
+static AOM_NOINLINE void encode_block_intra(int plane, int block, int blk_row,
+                                            int blk_col, BLOCK_SIZE plane_bsize,
+                                            TX_SIZE tx_size, void *arg) {
   struct encode_b_args *const args = arg;
   const AV1_COMP *const cpi = args->cpi;
   const AV1_COMMON *const cm = &cpi->common;
@@ -732,10 +734,12 @@ static void encode_block_intra_and_set_context(int plane, int block,
   encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);

   struct encode_b_args *const args = arg;
-  MACROBLOCK *x = args->x;
-  ENTROPY_CONTEXT *a = &args->ta[blk_col];
-  ENTROPY_CONTEXT *l = &args->tl[blk_row];
-  av1_set_txb_context(x, plane, block, tx_size, a, l);
+  if (is_trellis_used(args->enable_optimize_b, args->dry_run)) {
+    MACROBLOCK *x = args->x;
+    ENTROPY_CONTEXT *a = &args->ta[blk_col];
+    ENTROPY_CONTEXT *l = &args->tl[blk_row];
+    av1_set_txb_context(x, plane, block, tx_size, a, l);
+  }
 }

 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
@@ -748,13 +752,27 @@ void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
   const struct macroblockd_plane *const pd = &xd->plane[plane];
   const int ss_x = pd->subsampling_x;
   const int ss_y = pd->subsampling_y;
-  ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
-  ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
+  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
+  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
+  if (plane_bsize == txsize_to_bsize[tx_size] && xd->mb_to_right_edge >= 0 &&
+      xd->mb_to_bottom_edge >= 0) {
+    struct encode_b_args arg = { cpi,
+                                 x,
+                                 NULL,
+                                 pd->above_entropy_context,
+                                 pd->left_entropy_context,
+                                 dry_run,
+                                 enable_optimize_b };
+    encode_block_intra(plane, 0, 0, 0, plane_bsize, tx_size, &arg);
+    return;
+  }
+
+  ENTROPY_CONTEXT ta[MAX_MIB_SIZE];
+  ENTROPY_CONTEXT tl[MAX_MIB_SIZE];
   struct encode_b_args arg = {
     cpi, x, NULL, ta, tl, dry_run, enable_optimize_b
   };
-  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
-  if (enable_optimize_b) {
+  if (is_trellis_used(enable_optimize_b, dry_run)) {
     av1_get_entropy_contexts(plane_bsize, pd, ta, tl);
   }
   av1_foreach_transformed_block_in_plane(