Commit 5381b9a7a6 for aom

commit 5381b9a7a626cd76ef933db8be37fe3961236cd6
Author: Frank Galligan <fgalligan@google.com>
Date:   Tue Nov 17 06:28:44 2026 -0800

    av1: Speed up lowbd intra prediction facade

    Restructure low-bitdepth intra prediction to shrink the facade stack
    frame and specialize hot lowbd V/H/DC/filter-intra paths, while
    splitting HBD, CfL, palette, and non-CfL chroma dispatch out of line.
    Replace table-based has_top_right/has_bottom_left with arithmetic
    helpers, handle 8x8 directional zones out of line using existing RTCD
    edge filter/upsample, and preserve edge zero-padding.

    Pre-rebase speedup (Pixel 9 Tensor G4 Cortex-A720, 1T, realtime
    768x1024 intra, bit-exact): -4.35% paired median across 72 ABBA cases
    (9 clips x speeds 6/7 x q24/q36/q48/cbr500, 2 rounds; R1 -4.83%,
    R2 -3.88%). Pre-rebase x86-64 Clang (AMD EPYC 7B13, 1T, paired ABBA):
    -4.26% realtime median (-2.53% s6, -5.79% s7, 3 rounds); --allintra
    -3.15% s4 / -0.48% s6 (8b) and -0.97% s6 (10b); --good -0.81% s4 /
    -3.10% s6 (8b) and +1.19% s6 (10b); aomdec x20 (60f s7) 2.409s ->
    2.423s (MD5 exact).

    Size (reconintra.c.o vs main): pre-rebase ARM64 36566 B .text
    (+7460 B, +25.63%) / 0 B .data (-1543 B), total +16.50%; x86-64
    Clang (vs 3735896a0d) 57678 B .text (+9754 B, +20.35%) / 0 B .data
    (-1586 B), total +14.93%. Verified bit-exact across aomenc --rt 1T/8T,
    --allintra, and --good (8b/10b), test_libaom under Clang ASan+UBSan+
    alignment, IntSan, GCC 15.2 -m32/-O3 -Werror (default, dec-only,
    HBD=0, RT enc-only), and Clang strict.

    TAG=agy
    CONV=490488d4-7005-402a-8cc4-c245227f80fe
    Change-Id: Ic3e93c812eee51f20c316fa85883da7d12ec0b3d

diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index c4d9fcf44e..feefc8015c 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -11,6 +11,11 @@

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

 #include "config/aom_config.h"
 #include "config/aom_dsp_rtcd.h"
@@ -55,150 +60,102 @@ static const uint8_t extend_modes[INTRA_MODES] = {
 };
 #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
-// blocks in a 128x128 superblock, the index of the "o" block is 10 (in raster
-// order), so its flag is stored at the 3rd bit of the 2nd entry in the table,
-// i.e. (table[10 / 8] >> (10 % 8)) & 1.
-//       . . . .
-//       . . . .
-//       . . o .
-//       . . . .
-static uint8_t has_tr_4x4[128] = {
-  255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  255, 127, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  255, 127, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-  127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
-};
-static uint8_t has_tr_4x8[64] = {
-  255, 255, 255, 255, 119, 119, 119, 119, 127, 127, 127, 127, 119,
-  119, 119, 119, 255, 127, 255, 127, 119, 119, 119, 119, 127, 127,
-  127, 127, 119, 119, 119, 119, 255, 255, 255, 127, 119, 119, 119,
-  119, 127, 127, 127, 127, 119, 119, 119, 119, 255, 127, 255, 127,
-  119, 119, 119, 119, 127, 127, 127, 127, 119, 119, 119, 119,
-};
-static uint8_t has_tr_8x4[64] = {
-  255, 255, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
-  127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
-  255, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
-  127, 127, 0, 0, 85, 85, 0, 0, 119, 119, 0, 0, 85, 85, 0, 0,
-};
-static uint8_t has_tr_8x8[32] = {
-  255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
-  255, 127, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
-};
-static uint8_t has_tr_8x16[16] = {
-  255, 255, 119, 119, 127, 127, 119, 119,
-  255, 127, 119, 119, 127, 127, 119, 119,
-};
-static uint8_t has_tr_16x8[16] = {
-  255, 0, 85, 0, 119, 0, 85, 0, 127, 0, 85, 0, 119, 0, 85, 0,
-};
-static uint8_t has_tr_16x16[8] = {
-  255, 85, 119, 85, 127, 85, 119, 85,
-};
-static uint8_t has_tr_16x32[4] = { 255, 119, 127, 119 };
-static uint8_t has_tr_32x16[4] = { 15, 5, 7, 5 };
-static uint8_t has_tr_32x32[2] = { 95, 87 };
-static uint8_t has_tr_32x64[1] = { 127 };
-static uint8_t has_tr_64x32[1] = { 19 };
-static uint8_t has_tr_64x64[1] = { 7 };
-static uint8_t has_tr_64x128[1] = { 3 };
-static uint8_t has_tr_128x64[1] = { 1 };
-static uint8_t has_tr_128x128[1] = { 1 };
-static uint8_t has_tr_4x16[32] = {
-  255, 255, 255, 255, 127, 127, 127, 127, 255, 127, 255,
-  127, 127, 127, 127, 127, 255, 255, 255, 127, 127, 127,
-  127, 127, 255, 127, 255, 127, 127, 127, 127, 127,
-};
-static uint8_t has_tr_16x4[32] = {
-  255, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0,
-  127, 0, 0, 0, 85, 0, 0, 0, 119, 0, 0, 0, 85, 0, 0, 0,
-};
-static uint8_t has_tr_8x32[8] = {
-  255, 255, 127, 127, 255, 127, 127, 127,
-};
-static uint8_t has_tr_32x8[8] = {
-  15, 0, 5, 0, 7, 0, 5, 0,
-};
-static uint8_t has_tr_16x64[2] = { 255, 127 };
-static uint8_t has_tr_64x16[2] = { 3, 1 };
-
-static const uint8_t *const has_tr_tables[BLOCK_SIZES_ALL] = {
-  // 4X4
-  has_tr_4x4,
-  // 4X8,       8X4,            8X8
-  has_tr_4x8, has_tr_8x4, has_tr_8x8,
-  // 8X16,      16X8,           16X16
-  has_tr_8x16, has_tr_16x8, has_tr_16x16,
-  // 16X32,     32X16,          32X32
-  has_tr_16x32, has_tr_32x16, has_tr_32x32,
-  // 32X64,     64X32,          64X64
-  has_tr_32x64, has_tr_64x32, has_tr_64x64,
-  // 64x128,    128x64,         128x128
-  has_tr_64x128, has_tr_128x64, has_tr_128x128,
-  // 4x16,      16x4,            8x32
-  has_tr_4x16, has_tr_16x4, has_tr_8x32,
-  // 32x8,      16x64,           64x16
-  has_tr_32x8, has_tr_16x64, has_tr_64x16
-};
+// In quadtree Z-order (TL, TR, BL, BR), the top-right neighbor at
+// (rm - 1, cm + bw_unit) was coded earlier iff the horizontal split boundary
+// at rm is at least as coarse as the vertical boundary at cm + bw_unit, i.e.,
+// (rm & -rm) >= ((cm + bw_unit) & -(cm + bw_unit)). PARTITION_VERT_A and
+// PARTITION_VERT_B visit the final square split in column-major (TL, BL, TR,
+// BR) order, so BL blocks (rm & bh_unit) precede TR and lack top-right.
+static inline int has_top_right_in_sb(BLOCK_SIZE sb_size, BLOCK_SIZE bsize,
+                                      int mi_row, int mi_col,
+                                      PARTITION_TYPE partition) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+  const int bw_unit = mi_size_wide[bsize];
+  const int bh_unit = mi_size_high[bsize];
+  const int sb_mi_size = mi_size_high[sb_size];
+  const int rm = (mi_row & (sb_mi_size - 1)) & -bh_unit;
+  if (rm == 0) return 1;
+  const int cm = (mi_col & (sb_mi_size - 1)) & -bw_unit;
+  if (cm + bw_unit >= sb_mi_size) return 0;
+
+  if (bw_unit == bh_unit && bw_unit > 1 &&
+      (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) &&
+      (rm & bh_unit)) {
+    return 0;
+  }
+  return (unsigned int)(rm & -rm) >=
+         (unsigned int)((cm + bw_unit) & -(cm + bw_unit));
+}

-static uint8_t has_tr_vert_8x8[32] = {
-  255, 255, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0,
-  255, 127, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0,
-};
-static uint8_t has_tr_vert_16x16[8] = {
-  255, 0, 119, 0, 127, 0, 119, 0,
-};
-static uint8_t has_tr_vert_32x32[2] = { 15, 7 };
-static uint8_t has_tr_vert_64x64[1] = { 3 };
-
-// The _vert_* tables are like the ordinary tables above, but describe the
-// order we visit square blocks when doing a PARTITION_VERT_A or
-// PARTITION_VERT_B. This is the same order as normal except for on the last
-// split where we go vertically (TL, BL, TR, BR). We treat the rectangular block
-// as a pair of squares, which means that these tables work correctly for both
-// mixed vertical partition types.
-//
-// There are tables for each of the square sizes. Vertical rectangles (like
-// BLOCK_16X32) use their respective "non-vert" table
-static const uint8_t *const has_tr_vert_tables[BLOCK_SIZES] = {
-  // 4X4
-  NULL,
-  // 4X8,      8X4,         8X8
-  has_tr_4x8, NULL, has_tr_vert_8x8,
-  // 8X16,     16X8,        16X16
-  has_tr_8x16, NULL, has_tr_vert_16x16,
-  // 16X32,    32X16,       32X32
-  has_tr_16x32, NULL, has_tr_vert_32x32,
-  // 32X64,    64X32,       64X64
-  has_tr_32x64, NULL, has_tr_vert_64x64,
-  // 64x128,   128x64,      128x128
-  has_tr_64x128, NULL, has_tr_128x128
-};
+// Symmetrically, the bottom-left neighbor at (rm + bh_unit, cm - 1) was coded
+// earlier iff the vertical boundary at cm strictly exceeds the horizontal
+// boundary at rm + bh_unit: (cm & -cm) > ((rm + bh_unit) & -(rm + bh_unit)).
+// Under PARTITION_VERT_A / PARTITION_VERT_B column-major square order, TR
+// (!(rm & bh_unit) && (cm & bw_unit)) follows BL and thus has bottom-left.
+static inline int has_bottom_left_in_sb(BLOCK_SIZE sb_size, BLOCK_SIZE bsize,
+                                        int mi_row, int mi_col,
+                                        PARTITION_TYPE partition,
+                                        int bottom_left_count_unit, int row_off,
+                                        int ss_y) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+  const int bw_unit = mi_size_wide[bsize];
+  const int bh_unit = mi_size_high[bsize];
+  const int sb_mi_size = mi_size_high[sb_size];
+  const int rm = (mi_row & (sb_mi_size - 1)) & -bh_unit;
+  const int cm = (mi_col & (sb_mi_size - 1)) & -bw_unit;

-static const uint8_t *get_has_tr_table(PARTITION_TYPE partition,
-                                       BLOCK_SIZE bsize) {
-  const uint8_t *ret = NULL;
-  // If this is a mixed vertical partition, look up bsize in orders_vert.
-  if (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) {
-    assert(bsize < BLOCK_SIZES);
-    ret = has_tr_vert_tables[bsize];
-  } else {
-    ret = has_tr_tables[bsize];
+  if (cm == 0) {
+    const int row_off_in_sb = (rm >> ss_y) + row_off;
+    const int sb_height_unit = sb_mi_size >> ss_y;
+    return row_off_in_sb + bottom_left_count_unit < sb_height_unit;
+  }
+  if (rm + bh_unit >= sb_mi_size) return 0;
+
+  if (bw_unit == bh_unit && bw_unit > 1 &&
+      (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) &&
+      !(rm & bh_unit) && (cm & bw_unit)) {
+    return 1;
   }
-  assert(ret);
-  return ret;
+  return (unsigned int)(cm & -cm) >
+         (unsigned int)((rm + bh_unit) & -(rm + bh_unit));
+}
+
+// Fast-path availability when (ss_x | ss_y | col_off | row_off) == 0,
+// covering both luma blocks and 4:4:4 chroma blocks at zero offset.
+static inline int has_top_right_unsubsampled_origin(BLOCK_SIZE sb_size,
+                                                    BLOCK_SIZE bsize,
+                                                    int mi_row, int mi_col,
+                                                    PARTITION_TYPE partition,
+                                                    TX_SIZE txsz) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
+  assert((unsigned)txsz < TX_SIZES_ALL);
+  const int bw_unit = mi_size_wide[bsize];
+  const int top_right_count_unit = tx_size_wide_unit[txsz];
+  if (top_right_count_unit < bw_unit) return 1;
+  return has_top_right_in_sb(sb_size, bsize, mi_row, mi_col, partition);
+}
+
+static inline int has_bottom_left_unsubsampled_origin(BLOCK_SIZE sb_size,
+                                                      BLOCK_SIZE bsize,
+                                                      int mi_row, int mi_col,
+                                                      PARTITION_TYPE partition,
+                                                      TX_SIZE txsz) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
+  assert((unsigned)txsz < TX_SIZES_ALL);
+  const int bh_unit = mi_size_high[bsize];
+  const int bottom_left_count_unit = tx_size_high_unit[txsz];
+  if (bottom_left_count_unit < bh_unit) return 1;
+  return has_bottom_left_in_sb(sb_size, bsize, mi_row, mi_col, partition,
+                               bottom_left_count_unit, 0, 0);
 }

 static int has_top_right(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
                          int mi_col, int top_available, int right_available,
                          PARTITION_TYPE partition, TX_SIZE txsz, int row_off,
                          int col_off, int ss_x, int ss_y) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
   if (!top_available || !right_available) return 0;

   const int bw_unit = mi_size_wide[bsize];
@@ -215,7 +172,7 @@ static int has_top_right(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
         return 1;
       }
       const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
-      const int col_off_64 = col_off % plane_bw_unit_64;
+      const int col_off_64 = col_off & (plane_bw_unit_64 - 1);
       return col_off_64 + top_right_count_unit < plane_bw_unit_64;
     }
     return col_off + top_right_count_unit < plane_bw_unit;
@@ -223,167 +180,15 @@ static int has_top_right(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
     // All top-right pixels are in the block above, which is already available.
     if (col_off + top_right_count_unit < plane_bw_unit) return 1;

-    const int bw_in_mi_log2 = mi_size_wide_log2[bsize];
-    const int bh_in_mi_log2 = mi_size_high_log2[bsize];
-    const int sb_mi_size = mi_size_high[sb_size];
-    const int blk_row_in_sb = (mi_row & (sb_mi_size - 1)) >> bh_in_mi_log2;
-    const int blk_col_in_sb = (mi_col & (sb_mi_size - 1)) >> bw_in_mi_log2;
-
-    // Top row of superblock: so top-right pixels are in the top and/or
-    // top-right superblocks, both of which are already available.
-    if (blk_row_in_sb == 0) return 1;
-
-    // Rightmost column of superblock (and not the top row): so top-right pixels
-    // fall in the right superblock, which is not available yet.
-    if (((blk_col_in_sb + 1) << bw_in_mi_log2) >= sb_mi_size) {
-      return 0;
-    }
-
-    // General case (neither top row nor rightmost column): check if the
-    // top-right block is coded before the current block.
-    const int this_blk_index =
-        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
-        blk_col_in_sb + 0;
-    const int idx1 = this_blk_index / 8;
-    const int idx2 = this_blk_index % 8;
-    const uint8_t *has_tr_table = get_has_tr_table(partition, bsize);
-    return (has_tr_table[idx1] >> idx2) & 1;
-  }
-}
-
-// Similar to the has_tr_* tables, but store if the bottom-left reference
-// pixels are available.
-static uint8_t has_bl_4x4[128] = {
-  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85, 85,
-  85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  1,  0,  84, 85, 85, 85, 16, 17,
-  17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85, 85, 85, 16, 17, 17, 17, 84,
-  85, 85, 85, 0,  0,  0,  0,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85,
-  0,  1,  1,  1,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  1,
-  0,  84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  1,  1,  1,  84, 85,
-  85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0,  0,  0,  0,
-};
-static uint8_t has_bl_4x8[64] = {
-  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0,
-  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0,
-  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 1, 0,
-  16, 17, 17, 17, 0, 1, 1, 1, 16, 17, 17, 17, 0, 0, 0, 0,
-};
-static uint8_t has_bl_8x4[64] = {
-  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1,
-  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0,
-  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 1,
-  254, 255, 84, 85, 254, 255, 16, 17, 254, 255, 84, 85, 254, 255, 0, 0,
-};
-static uint8_t has_bl_8x8[32] = {
-  84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0,
-  84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0,
-};
-static uint8_t has_bl_8x16[16] = {
-  16, 17, 0, 1, 16, 17, 0, 0, 16, 17, 0, 1, 16, 17, 0, 0,
-};
-static uint8_t has_bl_16x8[16] = {
-  254, 84, 254, 16, 254, 84, 254, 0, 254, 84, 254, 16, 254, 84, 254, 0,
-};
-static uint8_t has_bl_16x16[8] = {
-  84, 16, 84, 0, 84, 16, 84, 0,
-};
-static uint8_t has_bl_16x32[4] = { 16, 0, 16, 0 };
-static uint8_t has_bl_32x16[4] = { 78, 14, 78, 14 };
-static uint8_t has_bl_32x32[2] = { 4, 4 };
-static uint8_t has_bl_32x64[1] = { 0 };
-static uint8_t has_bl_64x32[1] = { 34 };
-static uint8_t has_bl_64x64[1] = { 0 };
-static uint8_t has_bl_64x128[1] = { 0 };
-static uint8_t has_bl_128x64[1] = { 0 };
-static uint8_t has_bl_128x128[1] = { 0 };
-static uint8_t has_bl_4x16[32] = {
-  0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0,
-  0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0,
-};
-static uint8_t has_bl_16x4[32] = {
-  254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0,
-  254, 254, 254, 84, 254, 254, 254, 16, 254, 254, 254, 84, 254, 254, 254, 0,
-};
-static uint8_t has_bl_8x32[8] = {
-  0, 1, 0, 0, 0, 1, 0, 0,
-};
-static uint8_t has_bl_32x8[8] = {
-  238, 78, 238, 14, 238, 78, 238, 14,
-};
-static uint8_t has_bl_16x64[2] = { 0, 0 };
-static uint8_t has_bl_64x16[2] = { 42, 42 };
-
-static const uint8_t *const has_bl_tables[BLOCK_SIZES_ALL] = {
-  // 4X4
-  has_bl_4x4,
-  // 4X8,         8X4,         8X8
-  has_bl_4x8, has_bl_8x4, has_bl_8x8,
-  // 8X16,        16X8,        16X16
-  has_bl_8x16, has_bl_16x8, has_bl_16x16,
-  // 16X32,       32X16,       32X32
-  has_bl_16x32, has_bl_32x16, has_bl_32x32,
-  // 32X64,       64X32,       64X64
-  has_bl_32x64, has_bl_64x32, has_bl_64x64,
-  // 64x128,      128x64,      128x128
-  has_bl_64x128, has_bl_128x64, has_bl_128x128,
-  // 4x16,        16x4,        8x32
-  has_bl_4x16, has_bl_16x4, has_bl_8x32,
-  // 32x8,        16x64,       64x16
-  has_bl_32x8, has_bl_16x64, has_bl_64x16
-};
-
-static uint8_t has_bl_vert_8x8[32] = {
-  254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0,
-  254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0,
-};
-static uint8_t has_bl_vert_16x16[8] = {
-  254, 16, 254, 0, 254, 16, 254, 0,
-};
-static uint8_t has_bl_vert_32x32[2] = { 14, 14 };
-static uint8_t has_bl_vert_64x64[1] = { 2 };
-
-// The _vert_* tables are like the ordinary tables above, but describe the
-// order we visit square blocks when doing a PARTITION_VERT_A or
-// PARTITION_VERT_B. This is the same order as normal except for on the last
-// split where we go vertically (TL, BL, TR, BR). We treat the rectangular block
-// as a pair of squares, which means that these tables work correctly for both
-// mixed vertical partition types.
-//
-// There are tables for each of the square sizes. Vertical rectangles (like
-// BLOCK_16X32) use their respective "non-vert" table
-static const uint8_t *const has_bl_vert_tables[BLOCK_SIZES] = {
-  // 4X4
-  NULL,
-  // 4X8,     8X4,         8X8
-  has_bl_4x8, NULL, has_bl_vert_8x8,
-  // 8X16,    16X8,        16X16
-  has_bl_8x16, NULL, has_bl_vert_16x16,
-  // 16X32,   32X16,       32X32
-  has_bl_16x32, NULL, has_bl_vert_32x32,
-  // 32X64,   64X32,       64X64
-  has_bl_32x64, NULL, has_bl_vert_64x64,
-  // 64x128,  128x64,      128x128
-  has_bl_64x128, NULL, has_bl_128x128
-};
-
-static const uint8_t *get_has_bl_table(PARTITION_TYPE partition,
-                                       BLOCK_SIZE bsize) {
-  const uint8_t *ret = NULL;
-  // If this is a mixed vertical partition, look up bsize in orders_vert.
-  if (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) {
-    assert(bsize < BLOCK_SIZES);
-    ret = has_bl_vert_tables[bsize];
-  } else {
-    ret = has_bl_tables[bsize];
+    return has_top_right_in_sb(sb_size, bsize, mi_row, mi_col, partition);
   }
-  assert(ret);
-  return ret;
 }

 static int has_bottom_left(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
                            int mi_col, int bottom_available, int left_available,
                            PARTITION_TYPE partition, TX_SIZE txsz, int row_off,
                            int col_off, int ss_x, int ss_y) {
+  assert((unsigned)bsize < BLOCK_SIZES_ALL);
   if (!bottom_available || !left_available) return 0;

   // Special case for 128x* blocks, when col_off is half the block width.
@@ -391,11 +196,11 @@ static int has_bottom_left(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
   // raster order
   if (block_size_wide[bsize] > block_size_wide[BLOCK_64X64] && col_off > 0) {
     const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
-    const int col_off_64 = col_off % plane_bw_unit_64;
+    const int col_off_64 = col_off & (plane_bw_unit_64 - 1);
     if (col_off_64 == 0) {
       // We are at the left edge of top-right or bottom-right 64x* block.
       const int plane_bh_unit_64 = mi_size_high[BLOCK_64X64] >> ss_y;
-      const int row_off_64 = row_off % plane_bh_unit_64;
+      const int row_off_64 = row_off & (plane_bh_unit_64 - 1);
       const int plane_bh_unit =
           AOMMIN(mi_size_high[bsize] >> ss_y, plane_bh_unit_64);
       // Check if all bottom-left pixels are in the left 64x* block (which is
@@ -415,37 +220,8 @@ static int has_bottom_left(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
     // All bottom-left pixels are in the left block, which is already available.
     if (row_off + bottom_left_count_unit < plane_bh_unit) return 1;

-    const int bw_in_mi_log2 = mi_size_wide_log2[bsize];
-    const int bh_in_mi_log2 = mi_size_high_log2[bsize];
-    const int sb_mi_size = mi_size_high[sb_size];
-    const int blk_row_in_sb = (mi_row & (sb_mi_size - 1)) >> bh_in_mi_log2;
-    const int blk_col_in_sb = (mi_col & (sb_mi_size - 1)) >> bw_in_mi_log2;
-
-    // Leftmost column of superblock: so bottom-left pixels maybe in the left
-    // and/or bottom-left superblocks. But only the left superblock is
-    // available, so check if all required pixels fall in that superblock.
-    if (blk_col_in_sb == 0) {
-      const int blk_start_row_off =
-          blk_row_in_sb << (bh_in_mi_log2 + MI_SIZE_LOG2 - MI_SIZE_LOG2) >>
-          ss_y;
-      const int row_off_in_sb = blk_start_row_off + row_off;
-      const int sb_height_unit = sb_mi_size >> ss_y;
-      return row_off_in_sb + bottom_left_count_unit < sb_height_unit;
-    }
-
-    // Bottom row of superblock (and not the leftmost column): so bottom-left
-    // pixels fall in the bottom superblock, which is not available yet.
-    if (((blk_row_in_sb + 1) << bh_in_mi_log2) >= sb_mi_size) return 0;
-
-    // General case (neither leftmost column nor bottom row): check if the
-    // bottom-left block is coded before the current block.
-    const int this_blk_index =
-        ((blk_row_in_sb + 0) << (MAX_MIB_SIZE_LOG2 - bw_in_mi_log2)) +
-        blk_col_in_sb + 0;
-    const int idx1 = this_blk_index / 8;
-    const int idx2 = this_blk_index % 8;
-    const uint8_t *has_bl_table = get_has_bl_table(partition, bsize);
-    return (has_bl_table[idx1] >> idx2) & 1;
+    return has_bottom_left_in_sb(sb_size, bsize, mi_row, mi_col, partition,
+                                 bottom_left_count_unit, row_off, ss_y);
   }
 }

@@ -639,31 +415,6 @@ void av1_dr_prediction_z3_c(uint8_t *dst, ptrdiff_t stride, int bw, int bh,
   }
 }

-static void dr_predictor(uint8_t *dst, ptrdiff_t stride, TX_SIZE tx_size,
-                         const uint8_t *above, const uint8_t *left,
-                         int upsample_above, int upsample_left, int angle) {
-  const int dx = av1_get_dx(angle);
-  const int dy = av1_get_dy(angle);
-  const int bw = tx_size_wide[tx_size];
-  const int bh = tx_size_high[tx_size];
-  assert(angle > 0 && angle < 270);
-
-  if (angle > 0 && angle < 90) {
-    av1_dr_prediction_z1(dst, stride, bw, bh, above, left, upsample_above, dx,
-                         dy);
-  } else if (angle > 90 && angle < 180) {
-    av1_dr_prediction_z2(dst, stride, bw, bh, above, left, upsample_above,
-                         upsample_left, dx, dy);
-  } else if (angle > 180 && angle < 270) {
-    av1_dr_prediction_z3(dst, stride, bw, bh, above, left, upsample_left, dx,
-                         dy);
-  } else if (angle == 90) {
-    pred[V_PRED][tx_size](dst, stride, above, left);
-  } else if (angle == 180) {
-    pred[H_PRED][tx_size](dst, stride, above, left);
-  }
-}
-
 #if CONFIG_AV1_HIGHBITDEPTH
 // Directional prediction, zone 1: 0 < angle < 90
 void av1_highbd_dr_prediction_z1_c(uint16_t *dst, ptrdiff_t stride, int bw,
@@ -778,31 +529,6 @@ void av1_highbd_dr_prediction_z3_c(uint16_t *dst, ptrdiff_t stride, int bw,
   }
 }

-static void highbd_dr_predictor(uint16_t *dst, ptrdiff_t stride,
-                                TX_SIZE tx_size, const uint16_t *above,
-                                const uint16_t *left, int upsample_above,
-                                int upsample_left, int angle, int bd) {
-  const int dx = av1_get_dx(angle);
-  const int dy = av1_get_dy(angle);
-  const int bw = tx_size_wide[tx_size];
-  const int bh = tx_size_high[tx_size];
-  assert(angle > 0 && angle < 270);
-
-  if (angle > 0 && angle < 90) {
-    av1_highbd_dr_prediction_z1(dst, stride, bw, bh, above, left,
-                                upsample_above, dx, dy, bd);
-  } else if (angle > 90 && angle < 180) {
-    av1_highbd_dr_prediction_z2(dst, stride, bw, bh, above, left,
-                                upsample_above, upsample_left, dx, dy, bd);
-  } else if (angle > 180 && angle < 270) {
-    av1_highbd_dr_prediction_z3(dst, stride, bw, bh, above, left, upsample_left,
-                                dx, dy, bd);
-  } else if (angle == 90) {
-    pred_high[V_PRED][tx_size](dst, stride, above, left, bd);
-  } else if (angle == 180) {
-    pred_high[H_PRED][tx_size](dst, stride, above, left, bd);
-  }
-}
 #endif  // CONFIG_AV1_HIGHBITDEPTH

 DECLARE_ALIGNED(16, const int8_t,
@@ -957,75 +683,49 @@ static void highbd_filter_intra_predictor(uint16_t *dst, ptrdiff_t stride,
 }
 #endif  // CONFIG_AV1_HIGHBITDEPTH

-static int is_smooth(const MB_MODE_INFO *mbmi, int plane) {
+static inline int is_smooth(const MB_MODE_INFO *mbmi, int plane) {
+  static_assert(SMOOTH_V_PRED == SMOOTH_PRED + 1 &&
+                    SMOOTH_H_PRED == SMOOTH_PRED + 2 &&
+                    UV_SMOOTH_V_PRED == UV_SMOOTH_PRED + 1 &&
+                    UV_SMOOTH_H_PRED == UV_SMOOTH_PRED + 2,
+                "Smooth prediction modes must be contiguous in enum order");
   if (plane == 0) {
-    const PREDICTION_MODE mode = mbmi->mode;
-    return (mode == SMOOTH_PRED || mode == SMOOTH_V_PRED ||
-            mode == SMOOTH_H_PRED);
+    return (uint32_t)(mbmi->mode - SMOOTH_PRED) <=
+           (uint32_t)(SMOOTH_H_PRED - SMOOTH_PRED);
   } else {
     // uv_mode is not set for inter blocks, so need to explicitly
     // detect that case.
     if (is_inter_block(mbmi)) return 0;

-    const UV_PREDICTION_MODE uv_mode = mbmi->uv_mode;
-    return (uv_mode == UV_SMOOTH_PRED || uv_mode == UV_SMOOTH_V_PRED ||
-            uv_mode == UV_SMOOTH_H_PRED);
+    return (uint32_t)(mbmi->uv_mode - UV_SMOOTH_PRED) <=
+           (uint32_t)(UV_SMOOTH_H_PRED - UV_SMOOTH_PRED);
   }
 }

-static int get_intra_edge_filter_type(const MACROBLOCKD *xd, int plane) {
-  const MB_MODE_INFO *above;
-  const MB_MODE_INFO *left;
-
-  if (plane == 0) {
-    above = xd->above_mbmi;
-    left = xd->left_mbmi;
-  } else {
-    above = xd->chroma_above_mbmi;
-    left = xd->chroma_left_mbmi;
-  }
-
+static inline int get_intra_edge_filter_type(const MACROBLOCKD *xd, int plane) {
+  const MB_MODE_INFO *const above =
+      (plane == 0) ? xd->above_mbmi : xd->chroma_above_mbmi;
+  const MB_MODE_INFO *const left =
+      (plane == 0) ? xd->left_mbmi : xd->chroma_left_mbmi;
   return (above && is_smooth(above, plane)) || (left && is_smooth(left, plane));
 }

 static AOM_FORCE_INLINE int intra_edge_filter_strength(int bs0, int bs1,
                                                        int delta, int type) {
   const int d = abs(delta);
-  int strength = 0;
-
   const int blk_wh = bs0 + bs1;
   if (type == 0) {
-    if (blk_wh <= 8) {
-      if (d >= 56) strength = 1;
-    } else if (blk_wh <= 12) {
-      if (d >= 40) strength = 1;
-    } else if (blk_wh <= 16) {
-      if (d >= 40) strength = 1;
-    } else if (blk_wh <= 24) {
-      if (d >= 8) strength = 1;
-      if (d >= 16) strength = 2;
-      if (d >= 32) strength = 3;
-    } else if (blk_wh <= 32) {
-      if (d >= 1) strength = 1;
-      if (d >= 4) strength = 2;
-      if (d >= 32) strength = 3;
-    } else {
-      if (d >= 1) strength = 3;
-    }
+    if (blk_wh <= 8) return d >= 56;
+    if (blk_wh <= 16) return d >= 40;
+    if (blk_wh <= 24) return (d >= 8) + (d >= 16) + (d >= 32);
+    if (blk_wh <= 32) return (d >= 1) + (d >= 4) + (d >= 32);
+    return (d >= 1) * 3;
   } else {
-    if (blk_wh <= 8) {
-      if (d >= 40) strength = 1;
-      if (d >= 64) strength = 2;
-    } else if (blk_wh <= 16) {
-      if (d >= 20) strength = 1;
-      if (d >= 48) strength = 2;
-    } else if (blk_wh <= 24) {
-      if (d >= 4) strength = 3;
-    } else {
-      if (d >= 1) strength = 3;
-    }
+    if (blk_wh <= 8) return (d >= 40) + (d >= 64);
+    if (blk_wh <= 16) return (d >= 20) + (d >= 48);
+    if (blk_wh <= 24) return (d >= 4) * 3;
+    return (d >= 1) * 3;
   }
-  return strength;
 }

 void av1_filter_intra_edge_c(uint8_t *p, int sz, int strength) {
@@ -1084,9 +784,8 @@ void av1_upsample_intra_edge_c(uint8_t *p, int sz) {
   }
 }

-static AOM_FORCE_INLINE void copy_above_row_lbd(uint8_t *above_row,
-                                                const uint8_t *above_ref,
-                                                int n_copy) {
+static AOM_FORCE_INLINE void copy_above_row_lbd_partial(
+    uint8_t *above_row, const uint8_t *above_ref, int n_copy, int max_n) {
   if (n_copy == 4) {
     memcpy(above_row, above_ref, 4);
   } else if (n_copy == 8) {
@@ -1095,19 +794,49 @@ static AOM_FORCE_INLINE void copy_above_row_lbd(uint8_t *above_row,
     memcpy(above_row, above_ref, 16);
   } else if (n_copy == 32) {
     memcpy(above_row, above_ref, 32);
-  } else if (n_copy == 64) {
-    memcpy(above_row, above_ref, 64);
-  } else if (n_copy == 128) {
-    memcpy(above_row, above_ref, 128);
   } else {
+    assert(n_copy > 0 && n_copy <= max_n);
+    (void)max_n;
     int k = 0;
     for (; k + 4 <= n_copy; k += 4) memcpy(above_row + k, above_ref + k, 4);
     for (; k < n_copy; ++k) above_row[k] = above_ref[k];
   }
 }

-static AOM_FORCE_INLINE void fill_intra_edge_lbd(uint8_t *p, uint8_t val,
-                                                 int n) {
+static AOM_FORCE_INLINE void copy_above_row_lbd_64(uint8_t *above_row,
+                                                   const uint8_t *above_ref,
+                                                   int n_copy) {
+  if (n_copy == 64) {
+    memcpy(above_row, above_ref, 64);
+  } else {
+    copy_above_row_lbd_partial(above_row, above_ref, n_copy, 63);
+  }
+}
+
+static AOM_FORCE_INLINE void copy_above_row_lbd_128(uint8_t *above_row,
+                                                    const uint8_t *above_ref,
+                                                    int n_copy) {
+  if (n_copy == 128) {
+    memcpy(above_row, above_ref, 128);
+  } else if (n_copy == 64) {
+    memcpy(above_row, above_ref, 64);
+  } else {
+    copy_above_row_lbd_partial(above_row, above_ref, n_copy, 127);
+  }
+}
+
+static AOM_FORCE_INLINE void fill_intra_edge_lbd_tail(uint8_t *p, uint8_t val,
+                                                      int n, int max_n) {
+  assert(n > 0 && n <= max_n);
+  (void)max_n;
+  const uint32_t v32 = (uint32_t)val * 0x01010101U;
+  int k = 0;
+  for (; k + 4 <= n; k += 4) memcpy(p + k, &v32, 4);
+  for (; k < n; ++k) p[k] = val;
+}
+
+static AOM_FORCE_INLINE void fill_intra_edge_lbd_64(uint8_t *p, uint8_t val,
+                                                    int n) {
   if (n == 4) {
     memset(p, val, 4);
   } else if (n == 8) {
@@ -1118,138 +847,679 @@ static AOM_FORCE_INLINE void fill_intra_edge_lbd(uint8_t *p, uint8_t val,
     memset(p, val, 32);
   } else if (n == 64) {
     memset(p, val, 64);
-  } else if (n == 128) {
+  } else {
+    fill_intra_edge_lbd_tail(p, val, n, 64);
+  }
+}
+
+static AOM_FORCE_INLINE void fill_intra_edge_lbd_128(uint8_t *p, uint8_t val,
+                                                     int n) {
+  if (n == 128) {
     memset(p, val, 128);
+  } else if (n == 64) {
+    memset(p, val, 64);
+  } else if (n == 32) {
+    memset(p, val, 32);
+  } else if (n == 16) {
+    memset(p, val, 16);
+  } else if (n == 8) {
+    memset(p, val, 8);
+  } else if (n == 4) {
+    memset(p, val, 4);
   } else {
-    const uint32_t v32 = (uint32_t)val * 0x01010101U;
-    int k = 0;
-    for (; k + 4 <= n; k += 4) memcpy(p + k, &v32, 4);
-    for (; k < n; ++k) p[k] = val;
+    fill_intra_edge_lbd_tail(p, val, n, 128);
   }
 }

-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,
-    TX_SIZE tx_size, int disable_edge_filter, int n_top_px, int n_topright_px,
-    int n_left_px, int n_bottomleft_px, int intra_edge_filter_type) {
-  int i;
-  const uint8_t *above_ref = ref - ref_stride;
-  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 is_dr_mode = av1_is_directional_mode(mode);
-  const int use_filter_intra = filter_intra_mode != FILTER_INTRA_MODES;
-  assert(use_filter_intra || is_dr_mode);
-  (void)is_dr_mode;
-  const int need_above = use_filter_intra || (p_angle < 180);
-  const int need_left = use_filter_intra || (p_angle > 90);
-  const int need_above_left = 1;
+// Out-of-line strided column gather to keep slow/border paths compact.
+static AOM_NOINLINE void gather_left_col_lbd(uint8_t *left_col,
+                                             const uint8_t *left_ref,
+                                             int ref_stride, int n_copy) {
+  assert(n_copy > 0);
+#if !CONFIG_BIG_ENDIAN
+  const ptrdiff_t s1 = (ptrdiff_t)ref_stride;
+  const ptrdiff_t s2 = s1 * 2;
+  uint8_t *dst_p = left_col;
+  const uint8_t *ref_p = left_ref;
+  const int n8 = n_copy >> 3;
+  if (n8 > 0) {
+    const ptrdiff_t s4 = s1 * 4;
+    int i = 0;
+    do {
+      const uint8_t *const p2 = ref_p + s2;
+      const uint32_t lo = ((uint32_t)ref_p[0] | ((uint32_t)ref_p[s1] << 8)) |
+                          (((uint32_t)p2[0] | ((uint32_t)p2[s1] << 8)) << 16);
+      const uint8_t *const q0 = ref_p + s4;
+      const uint8_t *const q2 = q0 + s2;
+      const uint32_t hi = ((uint32_t)q0[0] | ((uint32_t)q0[s1] << 8)) |
+                          (((uint32_t)q2[0] | ((uint32_t)q2[s1] << 8)) << 16);
+      const uint64_t v = (uint64_t)lo | ((uint64_t)hi << 32);
+      memcpy(dst_p, &v, 8);
+      dst_p += 8;
+      ++i;
+      if ((i < n8) || (n_copy & 7)) ref_p = q0 + s4;
+    } while (i != n8);
+  }
+  if (n_copy & 4) {
+    const uint8_t *const r2 = ref_p + s2;
+    const uint32_t v = ((uint32_t)ref_p[0] | ((uint32_t)ref_p[s1] << 8)) |
+                       (((uint32_t)r2[0] | ((uint32_t)r2[s1] << 8)) << 16);
+    memcpy(dst_p, &v, 4);
+    dst_p += 4;
+    if (n_copy & 3) ref_p = r2 + s2;
+  }
+  if (n_copy & 2) {
+    dst_p[0] = ref_p[0];
+    dst_p[1] = ref_p[s1];
+    dst_p += 2;
+    if (n_copy & 1) ref_p += s2;
+  }
+  if (n_copy & 1) {
+    dst_p[0] = ref_p[0];
+  }
+#else
+  int i = 0;
+  do {
+    left_col[i] = left_ref[(ptrdiff_t)i * ref_stride];
+  } while (++i != n_copy);
+#endif
+}

-  assert(n_top_px >= 0);
-  assert(n_topright_px >= -1);
-  assert(n_left_px >= 0);
-  assert(n_bottomleft_px >= -1);
+static inline void gather_left_col_8_lbd(uint8_t *dst, const uint8_t *left_ref,
+                                         ptrdiff_t s1) {
+#if !CONFIG_BIG_ENDIAN
+  const ptrdiff_t s2 = s1 * 2;
+  const ptrdiff_t s4 = s1 * 4;
+  const uint8_t *const p2 = left_ref + s2;
+  const uint32_t lo = ((uint32_t)left_ref[0] | ((uint32_t)left_ref[s1] << 8)) |
+                      (((uint32_t)p2[0] | ((uint32_t)p2[s1] << 8)) << 16);
+  const uint8_t *const q0 = left_ref + s4;
+  const uint8_t *const q2 = q0 + s2;
+  const uint32_t hi = ((uint32_t)q0[0] | ((uint32_t)q0[s1] << 8)) |
+                      (((uint32_t)q2[0] | ((uint32_t)q2[s1] << 8)) << 16);
+  const uint64_t v = (uint64_t)lo | ((uint64_t)hi << 32);
+  memcpy(dst, &v, 8);
+#else
+  int i = 0;
+  do {
+    dst[i] = left_ref[(ptrdiff_t)i * s1];
+  } while (++i != 8);
+#endif
+}

-  if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
-    uint8_t val;
-    if (need_left) {
-      val = (n_top_px > 0) ? above_ref[0] : 129;
-    } else {
-      val = (n_left_px > 0) ? left_ref[0] : 127;
-    }
-    av1_fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
+static inline void gather_left_col_pow2_lbd(uint8_t *dst,
+                                            const uint8_t *left_ref, int stride,
+                                            int n) {
+  assert(n == 4 || n == 8 || n == 16 || n == 32 || n == 64);
+  const ptrdiff_t s1 = (ptrdiff_t)stride;
+  if (n == 4) {
+#if !CONFIG_BIG_ENDIAN
+    const uint8_t *const p2 = left_ref + s1 * 2;
+    const uint32_t v = ((uint32_t)left_ref[0] | ((uint32_t)left_ref[s1] << 8)) |
+                       (((uint32_t)p2[0] | ((uint32_t)p2[s1] << 8)) << 16);
+    memcpy(dst, &v, 4);
+#else
+    int i = 0;
+    do {
+      dst[i] = left_ref[(ptrdiff_t)i * s1];
+    } while (++i != 4);
+#endif
     return;
   }
+  const ptrdiff_t s8 = s1 * 8;
+  int r = 0;
+  for (;;) {
+    gather_left_col_8_lbd(dst + r, left_ref, s1);
+    r += 8;
+    if (r == n) break;
+    left_ref += s8;
+  }
+}

-  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;
+static AOM_FORCE_INLINE BLOCK_SIZE scale_chroma_bsize(BLOCK_SIZE bsize,
+                                                      int subsampling_x,
+                                                      int subsampling_y) {
+  assert(subsampling_x >= 0 && subsampling_x < 2);
+  assert(subsampling_y >= 0 && subsampling_y < 2);
+  BLOCK_SIZE bs = bsize;
+  switch (bsize) {
+    case BLOCK_4X4:
+      if (subsampling_x == 1 && subsampling_y == 1)
+        bs = BLOCK_8X8;
+      else if (subsampling_x == 1)
+        bs = BLOCK_8X4;
+      else if (subsampling_y == 1)
+        bs = BLOCK_4X8;
+      break;
+    case BLOCK_4X8:
+      if (subsampling_x == 1 && subsampling_y == 1)
+        bs = BLOCK_8X8;
+      else if (subsampling_x == 1)
+        bs = BLOCK_8X8;
+      else if (subsampling_y == 1)
+        bs = BLOCK_4X8;
+      break;
+    case BLOCK_8X4:
+      if (subsampling_x == 1 && subsampling_y == 1)
+        bs = BLOCK_8X8;
+      else if (subsampling_x == 1)
+        bs = BLOCK_8X4;
+      else if (subsampling_y == 1)
+        bs = BLOCK_8X8;
+      break;
+    case BLOCK_4X16:
+      if (subsampling_x == 1 && subsampling_y == 1)
+        bs = BLOCK_8X16;
+      else if (subsampling_x == 1)
+        bs = BLOCK_8X16;
+      else if (subsampling_y == 1)
+        bs = BLOCK_4X16;
+      break;
+    case BLOCK_16X4:
+      if (subsampling_x == 1 && subsampling_y == 1)
+        bs = BLOCK_16X8;
+      else if (subsampling_x == 1)
+        bs = BLOCK_16X4;
+      else if (subsampling_y == 1)
+        bs = BLOCK_16X8;
+      break;
+    default: break;
+  }
+  return bs;
+}

-  // NEED_LEFT
-  if (need_left) {
-    const int num_left_pixels_needed =
-        txhpx + (n_bottomleft_px >= 0 ? txwpx : 0);
-    if (n_left_px > 0) {
-      const int n_left_copy =
-          n_left_px + (n_bottomleft_px > 0 ? n_bottomleft_px : 0);
-      assert(n_left_copy > 0);
-#if defined(__GNUC__) && !defined(__clang__)
-#pragma GCC novector
-#endif
-      for (i = 0; i < n_left_copy; i++) left_col[i] = left_ref[i * ref_stride];
-      if (i < num_left_pixels_needed)
-        fill_intra_edge_lbd(&left_col[i], left_col[i - 1],
-                            num_left_pixels_needed - i);
-    } else {
-      const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
-      fill_intra_edge_lbd(left_col, val, num_left_pixels_needed);
+// Bit layout of packed DR metadata passed to out-of-line builders:
+//   [7:0]   tx_size (0 .. TX_SIZES_ALL - 1)
+//   [15:8]  plane (0 .. MAX_MB_PLANE - 1)
+//   [31:16] p_angle (0 .. 270)
+//   [32]    disable_edge_filter (0 or 1)
+//   [33]    ss_x (0 or 1)
+//   [34]    ss_y (0 or 1)
+//   [42:35] sb_size (BLOCK_64X64 or BLOCK_128X128)
+//   [50:43] col_off (0 .. 255, MI units)
+//   [58:51] row_off (0 .. 255, MI units)
+//   [61:59] filter_intra_mode (0 .. FILTER_INTRA_MODES) for generic DR, OR
+//           [59] has_edge_px (0 or 1) and [63:60] ext_px (0 .. 8) for 8x8 z1/z3
+static AOM_FORCE_INLINE uint64_t pack_dr_facade_meta(
+    TX_SIZE tx_size, int plane, int p_angle, int disable_edge_filter, int ss_x,
+    int ss_y, BLOCK_SIZE sb_size, int col_off, int row_off,
+    FILTER_INTRA_MODE filter_intra_mode) {
+  static_assert(TX_SIZES_ALL <= 0x100 && MAX_MB_PLANE <= 0x100 &&
+                    BLOCK_SIZES_ALL <= 0x100,
+                "DR packed metadata fields must fit in 8 bits");
+  static_assert(FILTER_INTRA_MODES <= 0x7,
+                "FILTER_INTRA_MODES must fit in 3 bits");
+  assert((unsigned)tx_size < TX_SIZES_ALL);
+  assert((unsigned)plane < MAX_MB_PLANE);
+  assert(p_angle >= 0 && p_angle <= 270);
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+  assert((unsigned)col_off <= 0xff && (unsigned)row_off <= 0xff);
+  assert((unsigned)filter_intra_mode <= FILTER_INTRA_MODES);
+  return (uint64_t)(uint8_t)tx_size | ((uint64_t)(uint8_t)plane << 8) |
+         ((uint64_t)(uint16_t)p_angle << 16) |
+         ((uint64_t)(uint8_t)(disable_edge_filter != 0) << 32) |
+         ((uint64_t)(uint8_t)(ss_x != 0) << 33) |
+         ((uint64_t)(uint8_t)(ss_y != 0) << 34) |
+         ((uint64_t)(uint8_t)sb_size << 35) |
+         ((uint64_t)(uint8_t)col_off << 43) |
+         ((uint64_t)(uint8_t)row_off << 51) |
+         ((uint64_t)(uint8_t)filter_intra_mode << 59);
+}
+
+static AOM_FORCE_INLINE uint64_t pack_dr_8x8_meta(
+    int plane, int p_angle, int disable_edge_filter, int ss_x, int ss_y,
+    BLOCK_SIZE sb_size, int col_off, int row_off, int has_edge_px, int ext_px) {
+  assert((unsigned)plane < MAX_MB_PLANE);
+  assert(p_angle >= 0 && p_angle <= 270);
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+  assert((unsigned)col_off <= 0xff && (unsigned)row_off <= 0xff);
+  assert(ext_px >= 0 && ext_px <= 8);
+  return (uint64_t)(uint8_t)TX_8X8 | ((uint64_t)(uint8_t)plane << 8) |
+         ((uint64_t)(uint16_t)p_angle << 16) |
+         ((uint64_t)(uint8_t)(disable_edge_filter != 0) << 32) |
+         ((uint64_t)(uint8_t)(ss_x != 0) << 33) |
+         ((uint64_t)(uint8_t)(ss_y != 0) << 34) |
+         ((uint64_t)(uint8_t)sb_size << 35) |
+         ((uint64_t)(uint8_t)col_off << 43) |
+         ((uint64_t)(uint8_t)row_off << 51) |
+         ((uint64_t)(uint8_t)(has_edge_px != 0) << 59) |
+         ((uint64_t)(uint8_t)ext_px << 60);
+}
+
+// Bit layout of packed DR pixel counts passed to generic DR builders:
+//   [15:0]  n_top_px (0 .. MAX_TX_SIZE)
+//   [31:16] n_left_px (0 .. MAX_TX_SIZE)
+//   [47:32] xr_clamped (0 .. MAX_TX_SIZE)
+//   [63:48] yd_clamped (0 .. MAX_TX_SIZE)
+static AOM_FORCE_INLINE uint64_t pack_dr_facade_px(int n_top_px, int n_left_px,
+                                                   int xr_clamped,
+                                                   int yd_clamped) {
+  assert(n_top_px >= 0 && n_top_px <= MAX_TX_SIZE);
+  assert(n_left_px >= 0 && n_left_px <= MAX_TX_SIZE);
+  assert(xr_clamped >= 0 && xr_clamped <= MAX_TX_SIZE);
+  assert(yd_clamped >= 0 && yd_clamped <= MAX_TX_SIZE);
+  return (uint64_t)(uint16_t)n_top_px | ((uint64_t)(uint16_t)n_left_px << 16) |
+         ((uint64_t)(uint16_t)xr_clamped << 32) |
+         ((uint64_t)(uint16_t)yd_clamped << 48);
+}
+
+// Pack (plane, p_angle, disable_edge_filter) for 8x8 z2 to fit in 6 GP args.
+static AOM_FORCE_INLINE uint32_t pack_dr_z2_8x8_meta(int plane, int p_angle,
+                                                     int disable_edge_filter) {
+  assert((unsigned)plane < MAX_MB_PLANE);
+  assert(p_angle > 90 && p_angle < 180);
+  return (uint32_t)(uint8_t)plane | ((uint32_t)(uint16_t)p_angle << 8) |
+         ((uint32_t)(uint8_t)(disable_edge_filter != 0) << 24);
+}
+
+// Pack (mode, tx_size, bit_depth) and (n_top_px, n_left_px) to keep both LBD
+// and HBD non-directional builders within 6 GP register arguments.
+static AOM_FORCE_INLINE uint32_t pack_non_dir_meta(PREDICTION_MODE mode,
+                                                   TX_SIZE tx_size,
+                                                   int bit_depth) {
+  assert((unsigned)mode < INTRA_MODES);
+  assert((unsigned)tx_size < TX_SIZES_ALL);
+  assert((unsigned)bit_depth <= 0xff);
+  return (uint32_t)(uint8_t)mode | ((uint32_t)(uint8_t)tx_size << 8) |
+         ((uint32_t)(uint8_t)bit_depth << 16);
+}
+
+static AOM_FORCE_INLINE uint32_t pack_non_dir_px(int n_top_px, int n_left_px) {
+  assert(n_top_px >= 0 && n_top_px <= MAX_TX_SIZE);
+  assert(n_left_px >= 0 && n_left_px <= MAX_TX_SIZE);
+  return (uint32_t)(uint16_t)n_top_px | ((uint32_t)(uint16_t)n_left_px << 16);
+}
+
+// Returns 1 iff intra_edge_filter_type (smooth vs non-smooth neighbor) can
+// affect intra_edge_filter_strength() or av1_use_intra_edge_upsample():
+// - blk_wh > 32: strength is (d >= 1) * 3 and upsample is 0 for both types.
+// - blk_wh == 8: upsample (0 < d < 40) matches; strength differs for d >= 40.
+// - 8 < blk_wh <= 16: upsample differs for d < 40 (1 vs 0); strength matches
+//   (1) for 40 <= d < 48 and differs for d >= 48 (1 vs 2) when edge_px > 0.
+// - 16 < blk_wh <= 32: upsample is 0; strength matches (3) for d >= 32 and
+//   differs for d < 32 when edge_px > 0.
+static inline int need_intra_edge_filter_type_check(int txwpx, int txhpx,
+                                                    int p_angle, int n_top_px,
+                                                    int n_left_px) {
+  const int blk_wh = txwpx + txhpx;
+  if (blk_wh > 32) return 0;
+  if (blk_wh <= 16) {
+    if (blk_wh != 8) {
+      const int da = abs(p_angle - 90);
+      const int dl = abs(p_angle - 180);
+      return (p_angle < 180 && (da < 40 || (n_top_px > 0 && da >= 48))) ||
+             (p_angle > 90 && (dl < 40 || (n_left_px > 0 && dl >= 48)));
     }
+    return (p_angle < 180 && n_top_px > 0 && abs(p_angle - 90) >= 40) ||
+           (p_angle > 90 && n_left_px > 0 && abs(p_angle - 180) >= 40);
   }
+  return (p_angle < 180 && n_top_px > 0 && abs(p_angle - 90) < 32) ||
+         (p_angle > 90 && n_left_px > 0 && abs(p_angle - 180) < 32);
+}

-  // NEED_ABOVE
-  if (need_above) {
-    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],
-                            num_top_pixels_needed - n_top_copy);
+// Shared out-of-line TR/BL availability helpers (2 args -> zero stack spills).
+static AOM_NOINLINE int has_top_right_available(const MACROBLOCKD *xd,
+                                                uint64_t meta) {
+  const TX_SIZE tx_size = (TX_SIZE)(meta & 0xff);
+  const int ss_x = (int)((meta >> 33) & 1);
+  const int ss_y = (int)((meta >> 34) & 1);
+  const BLOCK_SIZE sb_size = (BLOCK_SIZE)((meta >> 35) & 0xff);
+  const int col_off = (int)((meta >> 43) & 0xff);
+  const int row_off = (int)((meta >> 51) & 0xff);
+  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
+  const int txw = tx_size_wide_unit[tx_size];
+  if (mi_col + ((col_off + txw) << ss_x) >= xd->tile.mi_col_end) return 0;
+  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+  if ((ss_x | ss_y | col_off | row_off) == 0) {
+    return has_top_right_unsubsampled_origin(sb_size, mbmi->bsize, mi_row,
+                                             mi_col, mbmi->partition, tx_size);
+  }
+  BLOCK_SIZE bsize = mbmi->bsize;
+  if (ss_x || ss_y) bsize = scale_chroma_bsize(bsize, ss_x, ss_y);
+  return has_top_right(sb_size, bsize, mi_row, mi_col, 1, 1, mbmi->partition,
+                       tx_size, row_off, col_off, ss_x, ss_y);
+}
+
+static AOM_NOINLINE int has_bottom_left_available(const MACROBLOCKD *xd,
+                                                  uint64_t meta) {
+  const TX_SIZE tx_size = (TX_SIZE)(meta & 0xff);
+  const int ss_x = (int)((meta >> 33) & 1);
+  const int ss_y = (int)((meta >> 34) & 1);
+  const BLOCK_SIZE sb_size = (BLOCK_SIZE)((meta >> 35) & 0xff);
+  const int col_off = (int)((meta >> 43) & 0xff);
+  const int row_off = (int)((meta >> 51) & 0xff);
+  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
+  const int txh = tx_size_high_unit[tx_size];
+  if (mi_row + ((row_off + txh) << ss_y) >= xd->tile.mi_row_end) return 0;
+  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+  if ((ss_x | ss_y | col_off | row_off) == 0) {
+    return has_bottom_left_unsubsampled_origin(
+        sb_size, mbmi->bsize, mi_row, mi_col, mbmi->partition, tx_size);
+  }
+  BLOCK_SIZE bsize = mbmi->bsize;
+  if (ss_x || ss_y) bsize = scale_chroma_bsize(bsize, ss_x, ss_y);
+  return has_bottom_left(sb_size, bsize, mi_row, mi_col, 1, 1, mbmi->partition,
+                         tx_size, row_off, col_off, ss_x, ss_y);
+}
+
+// Separate stack frame for 8x8 zone-1 edge buffers off the facade.
+static AOM_NOINLINE void build_dr_z1_8x8_lbd(const MACROBLOCKD *xd,
+                                             const uint8_t *ref, int ref_stride,
+                                             uint8_t *dst, int dst_stride,
+                                             uint64_t meta) {
+  const int plane = (int)((meta >> 8) & 0xff);
+  const int p_angle = (int)((meta >> 16) & 0xffff);
+  assert(p_angle > 0 && p_angle < 90);
+  const int disable_edge_filter = (int)((meta >> 32) & 1);
+  const int n_left_nonzero = (int)((meta >> 59) & 1);
+  const int xr_clamped = AOMMIN((int)((meta >> 60) & 0xf), 8);
+
+  const uint8_t *const above_ref = ref - ref_stride;
+  const int n_topright_px =
+      (xr_clamped > 0 && has_top_right_available(xd, meta)) ? xr_clamped : 0;
+
+  // 16-byte prefix [-16..-1], 32 upsampled edge samples [0..31], 32-byte tail
+  // [32..63] (fits av1_upsample_intra_edge_sse4_1's p[-2..61] writes at sz=16).
+  DECLARE_ALIGNED(16, uint8_t, above_data[16 + 32 + 32]);
+  uint8_t *const above_row = above_data + 16;
+  memset(above_data, 0, 16);
+  memset(above_row + 16, 0, 48);
+  int upsample_above = 0;
+
+  assert(n_topright_px >= 0 && n_topright_px <= 8);
+  if (n_topright_px == 8) {
+    memcpy(above_row, above_ref, 16);
+  } else {
+    memcpy(above_row, above_ref, 8);
+    if (n_topright_px > 0) {
+      memcpy(above_row + 8, above_ref + 8, (size_t)n_topright_px);
+    }
+    memset(above_row + 8 + n_topright_px, above_ref[7 + n_topright_px],
+           (size_t)(8 - n_topright_px));
+  }
+  if (!disable_edge_filter) {
+    above_row[-1] = n_left_nonzero ? above_ref[-1] : above_ref[0];
+    const int d = 90 - p_angle;
+    const int type =
+        (d < 40 || d >= 48) ? get_intra_edge_filter_type(xd, plane) : 0;
+    if (type == 0) {
+      if (d < 40) {
+        upsample_above = 1;
+        av1_upsample_intra_edge(above_row, 16);
+      } else {
+        av1_filter_intra_edge(above_row - 1, 17, 1);
+      }
     } else {
-      const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
-      fill_intra_edge_lbd(above_row, val, num_top_pixels_needed);
+      const int s = (d >= 20) + (d >= 48);
+      if (s) av1_filter_intra_edge(above_row - 1, 17, s);
     }
   }
+  av1_dr_prediction_z1(dst, dst_stride, 8, 8, above_row, NULL, upsample_above,
+                       dr_intra_derivative[p_angle], 1);
+}

-  // Pure vertical (90 deg) and horizontal (180 deg) directional predictors do
-  // not require the top-left corner pixel or intra edge filtering.
-  if (!use_filter_intra) {
-    if (p_angle == 90) {
-      pred[V_PRED][tx_size](dst, dst_stride, above_row, left_col);
-      return;
-    }
-    if (p_angle == 180) {
-      pred[H_PRED][tx_size](dst, dst_stride, above_row, left_col);
+// Separate stack frame for 8x8 zone-2 edge buffers off the facade.
+static AOM_NOINLINE void build_dr_z2_8x8_lbd(const MACROBLOCKD *xd,
+                                             const uint8_t *ref, int ref_stride,
+                                             uint8_t *dst, int dst_stride,
+                                             uint32_t z2_meta) {
+  const int plane = (int)(z2_meta & 0xff);
+  const int p_angle = (int)((z2_meta >> 8) & 0xffff);
+  const int disable_edge_filter = (int)((z2_meta >> 24) & 1);
+  assert(p_angle > 90 && p_angle < 180);
+
+  const uint8_t *const above_ref = ref - ref_stride;
+  const uint8_t *const left_ref = ref - 1;
+  // 16-byte prefix [-16..-1], 8 edge samples, 24-byte tail [8..31] (exact fit
+  // for av1_dr_prediction_z2's left[-2..31] / above[-2..31] SIMD over-reads
+  // and av1_upsample_intra_edge_sse4_1's p[-2..29] writes at sz=8).
+  DECLARE_ALIGNED(16, uint8_t, left_data[16 + 8 + 24]);
+  DECLARE_ALIGNED(16, uint8_t, above_data[16 + 8 + 24]);
+  uint8_t *const above_row = above_data + 16;
+  uint8_t *const left_col = left_data + 16;
+  memset(above_data, 0, 16);
+  memset(above_row + 8, 0, 24);
+  memset(left_data, 0, 16);
+  memset(left_col + 8, 0, 24);
+  const uint8_t tl = above_ref[-1];
+
+  int upsample_above = 0;
+  int upsample_left = 0;
+  gather_left_col_pow2_lbd(left_col, left_ref, ref_stride, 8);
+  memcpy(above_row, above_ref, 8);
+  above_row[-1] = tl;
+  left_col[-1] = tl;
+  if (!disable_edge_filter) {
+    const int da = p_angle - 90;
+    const int dl = 180 - p_angle;
+    const int type = ((da < 40 || da >= 48) || (dl < 40 || dl >= 48))
+                         ? get_intra_edge_filter_type(xd, plane)
+                         : 0;
+    if (type == 0) {
+      if (da >= 40) {
+        av1_filter_intra_edge(above_row - 1, 9, 1);
+      } else {
+        upsample_above = 1;
+        av1_upsample_intra_edge(above_row, 8);
+      }
+      if (dl >= 40) {
+        av1_filter_intra_edge(left_col - 1, 9, 1);
+      } else {
+        upsample_left = 1;
+        av1_upsample_intra_edge(left_col, 8);
+      }
+    } else {
+      const int sa = (da >= 20) + (da >= 48);
+      if (sa) av1_filter_intra_edge(above_row - 1, 9, sa);
+      const int sl = (dl >= 20) + (dl >= 48);
+      if (sl) av1_filter_intra_edge(left_col - 1, 9, sl);
+    }
+  }
+  av1_dr_prediction_z2(
+      dst, dst_stride, 8, 8, above_row, left_col, upsample_above, upsample_left,
+      dr_intra_derivative[180 - p_angle], dr_intra_derivative[p_angle - 90]);
+}
+
+// Separate stack frame for 8x8 zone-3 edge buffers off the facade.
+static AOM_NOINLINE void build_dr_z3_8x8_lbd(const MACROBLOCKD *xd,
+                                             const uint8_t *ref, int ref_stride,
+                                             uint8_t *dst, int dst_stride,
+                                             uint64_t meta) {
+  const int plane = (int)((meta >> 8) & 0xff);
+  const int p_angle = (int)((meta >> 16) & 0xffff);
+  assert(p_angle > 180 && p_angle < 270);
+  const int disable_edge_filter = (int)((meta >> 32) & 1);
+  const int n_top_nonzero = (int)((meta >> 59) & 1);
+  const int yd_clamped = AOMMIN((int)((meta >> 60) & 0xf), 8);
+
+  const uint8_t *const above_ref = ref - ref_stride;
+  const uint8_t *const left_ref = ref - 1;
+  const int n_bottomleft_px =
+      (yd_clamped > 0 && has_bottom_left_available(xd, meta)) ? yd_clamped : 0;
+
+  // 16-byte prefix [-16..-1], 32 upsampled edge samples [0..31], 32-byte tail
+  // [32..63] (fits av1_upsample_intra_edge_sse4_1's p[-2..61] writes at sz=16).
+  DECLARE_ALIGNED(16, uint8_t, left_data[16 + 32 + 32]);
+  uint8_t *const left_col = left_data + 16;
+  memset(left_data, 0, 16);
+  memset(left_col + 16, 0, 48);
+  int upsample_left = 0;
+
+  assert(n_bottomleft_px >= 0 && n_bottomleft_px <= 8);
+  gather_left_col_pow2_lbd(left_col, left_ref, ref_stride, 8);
+  if (n_bottomleft_px == 8) {
+    gather_left_col_pow2_lbd(left_col + 8, left_ref + (ptrdiff_t)8 * ref_stride,
+                             ref_stride, 8);
+  } else if (n_bottomleft_px == 0) {
+    memset(left_col + 8, left_col[7], 8);
+  } else {
+    gather_left_col_lbd(left_col + 8, left_ref + (ptrdiff_t)8 * ref_stride,
+                        ref_stride, n_bottomleft_px);
+    memset(&left_col[8 + n_bottomleft_px], left_col[7 + n_bottomleft_px],
+           (size_t)(8 - n_bottomleft_px));
+  }
+  if (!disable_edge_filter) {
+    left_col[-1] = n_top_nonzero ? above_ref[-1] : left_ref[0];
+    const int d = p_angle - 180;
+    const int type =
+        (d < 40 || d >= 48) ? get_intra_edge_filter_type(xd, plane) : 0;
+    if (type == 0) {
+      if (d < 40) {
+        upsample_left = 1;
+        av1_upsample_intra_edge(left_col, 16);
+      } else {
+        av1_filter_intra_edge(left_col - 1, 17, 1);
+      }
+    } else {
+      const int s = (d >= 20) + (d >= 48);
+      if (s) av1_filter_intra_edge(left_col - 1, 17, s);
+    }
+  }
+  av1_dr_prediction_z3(dst, dst_stride, 8, 8, NULL, left_col, upsample_left, 1,
+                       dr_intra_derivative[270 - p_angle]);
+}
+
+// Unified out-of-line builder for non-8x8 DR, V/H borders, and filter-intra.
+static AOM_NOINLINE void build_directional_and_filter_intra_predictors(
+    const MACROBLOCKD *xd, const uint8_t *ref, int ref_stride, uint8_t *dst,
+    int dst_stride, uint64_t meta, uint64_t px_pack) {
+  const TX_SIZE tx_size = (TX_SIZE)(meta & 0xff);
+  const int plane = (int)((meta >> 8) & 0xff);
+  const int p_angle = (int)((meta >> 16) & 0xffff);
+  const int disable_edge_filter = (int)((meta >> 32) & 1);
+  const BLOCK_SIZE sb_size = (BLOCK_SIZE)((meta >> 35) & 0xff);
+  const FILTER_INTRA_MODE filter_intra_mode =
+      (FILTER_INTRA_MODE)((meta >> 59) & 0x7);
+  (void)sb_size;
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+
+  const int n_top_px = (int)(px_pack & 0xffff);
+  const int n_left_px = (int)((px_pack >> 16) & 0xffff);
+  const int xr_clamped = (int)((px_pack >> 32) & 0xffff);
+  const int yd_clamped = (int)((px_pack >> 48) & 0xffff);
+
+  const uint8_t *const above_ref = ref - ref_stride;
+  const uint8_t *const left_ref = ref - 1;
+  const int txwpx = tx_size_wide[tx_size];
+  const int txhpx = tx_size_high[tx_size];
+  const int use_filter_intra = filter_intra_mode != FILTER_INTRA_MODES;
+  assert(use_filter_intra || (p_angle > 0 && p_angle < 270));
+  const int need_above = use_filter_intra || (p_angle < 180);
+  const int need_left = use_filter_intra || (p_angle > 90);
+
+  assert(n_top_px >= 0);
+  assert(n_left_px >= 0);
+
+  if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
+    const uint8_t val = need_left ? ((n_top_px > 0) ? above_ref[0] : 129)
+                                  : ((n_left_px > 0) ? left_ref[0] : 127);
+    av1_fill_block_lbd(dst, dst_stride, val, txwpx, txhpx);
+    return;
+  }
+
+  int n_topright_px = -1;
+  int n_bottomleft_px = -1;
+  if (!use_filter_intra) {
+    if (p_angle < 90) {
+      n_topright_px = (xr_clamped > 0 && has_top_right_available(xd, meta))
+                          ? xr_clamped
+                          : 0;
+    } else if (p_angle > 180) {
+      n_bottomleft_px = (yd_clamped > 0 && has_bottom_left_available(xd, meta))
+                            ? yd_clamped
+                            : 0;
+    }
+  }
+
+  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;
+
+  // NEED_LEFT
+  if (need_left) {
+    const int num_left_pixels_needed =
+        txhpx + (n_bottomleft_px >= 0 ? txwpx : 0);
+    if (n_left_px > 0) {
+      const int n_left_copy = n_left_px + AOMMAX(n_bottomleft_px, 0);
+      gather_left_col_lbd(left_col, left_ref, ref_stride, n_left_copy);
+      if (n_left_copy < num_left_pixels_needed) {
+        fill_intra_edge_lbd_tail(&left_col[n_left_copy],
+                                 left_col[n_left_copy - 1],
+                                 num_left_pixels_needed - n_left_copy, 127);
+      }
+    } else {
+      const uint8_t val = (n_top_px > 0) ? above_ref[0] : 129;
+      fill_intra_edge_lbd_128(left_col, val, num_left_pixels_needed);
+    }
+  }
+
+  // NEED_ABOVE
+  if (need_above) {
+    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 + AOMMAX(n_topright_px, 0);
+      assert(n_top_copy > 0);
+      copy_above_row_lbd_128(above_row, above_ref, n_top_copy);
+      if (n_top_copy < num_top_pixels_needed) {
+        fill_intra_edge_lbd_tail(&above_row[n_top_copy],
+                                 above_row[n_top_copy - 1],
+                                 num_top_pixels_needed - n_top_copy, 127);
+      }
+    } else {
+      const uint8_t val = (n_left_px > 0) ? left_ref[0] : 127;
+      fill_intra_edge_lbd_128(above_row, val, num_top_pixels_needed);
+    }
+  }
+
+  // Pure vertical (90 deg) and horizontal (180 deg) directional predictors do
+  // not require the top-left corner pixel or intra edge filtering.
+  if (!use_filter_intra) {
+    if (p_angle == 90) {
+      pred[V_PRED][tx_size](dst, dst_stride, above_row, left_col);
+      return;
+    }
+    if (p_angle == 180) {
+      // aom_h_predictor_32x8_sse2 loads 16 bytes from left, and 4-row
+      // SSE2/SSSE3 H predictors load 8 bytes; zero tail avoids MSan reads.
+      if (txhpx <= 8) memset(left_col + txhpx, 0, 8);
+      pred[H_PRED][tx_size](dst, dst_stride, above_row, left_col);
       return;
     }
   }

-  // Zero the 16-byte prefix, any uninitialized top-right / bottom-left span
-  // (when n_topright_px < 0 or n_bottomleft_px < 0), and the SIMD over-read
-  // tail for Valgrind/MemorySanitizer.
+  // Zero the 16-byte prefix, unused span, and SIMD over-read tail for
+  // Valgrind/MemorySanitizer.
   const int tail_zero = (txwpx + txhpx == 128) ? 16 : 32;
-  memset(above_data, 0, 16);
-  memset(left_data, 0, 16);
+  uint8_t corner;
+  if (n_top_px > 0 && n_left_px > 0) {
+    corner = above_ref[-1];
+  } else if (n_top_px > 0) {
+    corner = above_ref[0];
+  } else if (n_left_px > 0) {
+    corner = left_ref[0];
+  } else {
+    corner = 128;
+  }
   if (need_above) {
     const int num_top_pixels_needed = txwpx + (n_topright_px >= 0 ? txhpx : 0);
-    assert(num_top_pixels_needed >= 0 &&
-           num_top_pixels_needed <= txwpx + txhpx);
+    memset(above_data, 0, 16);
     memset(above_row + num_top_pixels_needed, 0,
-           (txwpx + txhpx + tail_zero) - num_top_pixels_needed);
+           (size_t)((txwpx + txhpx + tail_zero) - num_top_pixels_needed));
+    above_row[-1] = corner;
   }
   if (need_left) {
     const int num_left_pixels_needed =
         txhpx + (n_bottomleft_px >= 0 ? txwpx : 0);
-    assert(num_left_pixels_needed >= 0 &&
-           num_left_pixels_needed <= txhpx + txwpx);
+    // dr_prediction_z2_WxH_neon unconditionally loads left_col[48..63] when
+    // bw == 16 (including TX_16X4 and TX_16X8).
+    const int left_zero_end = AOMMAX(txhpx + txwpx + tail_zero, MAX_TX_SIZE);
+    memset(left_data, 0, 16);
     memset(left_col + num_left_pixels_needed, 0,
-           (txhpx + txwpx + tail_zero) - num_left_pixels_needed);
-  }
-
-  if (need_above_left) {
-    if (n_top_px > 0 && n_left_px > 0) {
-      above_row[-1] = above_ref[-1];
-    } else if (n_top_px > 0) {
-      above_row[-1] = above_ref[0];
-    } else if (n_left_px > 0) {
-      above_row[-1] = left_ref[0];
-    } else {
-      above_row[-1] = 128;
-    }
-    left_col[-1] = above_row[-1];
+           (size_t)(left_zero_end - num_left_pixels_needed));
+    left_col[-1] = corner;
   }

   if (use_filter_intra) {
@@ -1258,46 +1528,60 @@ static void build_directional_and_filter_intra_predictors(
     return;
   }

-  assert(is_dr_mode);
   int upsample_above = 0;
   int upsample_left = 0;
   if (!disable_edge_filter) {
     const int need_right = p_angle < 90;
     const int need_bottom = p_angle > 180;
-    if (p_angle != 90 && p_angle != 180) {
-      assert(need_above_left);
-      const int ab_le = 1;
-      if (need_above && need_left && (txwpx + txhpx >= 24)) {
-        filter_intra_edge_corner(above_row, left_col);
-      }
-      if (need_above && n_top_px > 0) {
-        const int strength = intra_edge_filter_strength(
-            txwpx, txhpx, p_angle - 90, intra_edge_filter_type);
-        const int n_px = n_top_px + ab_le + (need_right ? txhpx : 0);
-        av1_filter_intra_edge(above_row - ab_le, n_px, strength);
+    const int intra_edge_filter_type =
+        need_intra_edge_filter_type_check(txwpx, txhpx, p_angle, n_top_px,
+                                          n_left_px)
+            ? get_intra_edge_filter_type(xd, plane)
+            : 0;
+    if (need_above && need_left && (txwpx + txhpx >= 24)) {
+      filter_intra_edge_corner(above_row, left_col);
+    }
+    if (need_above && n_top_px > 0) {
+      const int strength = intra_edge_filter_strength(
+          txwpx, txhpx, p_angle - 90, intra_edge_filter_type);
+      if (strength) {
+        const int n_px = n_top_px + 1 + (need_right ? txhpx : 0);
+        av1_filter_intra_edge(above_row - 1, n_px, strength);
       }
-      if (need_left && n_left_px > 0) {
-        const int strength = intra_edge_filter_strength(
-            txhpx, txwpx, p_angle - 180, intra_edge_filter_type);
-        const int n_px = n_left_px + ab_le + (need_bottom ? txwpx : 0);
-        av1_filter_intra_edge(left_col - ab_le, n_px, strength);
+    }
+    if (need_left && n_left_px > 0) {
+      const int strength = intra_edge_filter_strength(
+          txhpx, txwpx, p_angle - 180, intra_edge_filter_type);
+      if (strength) {
+        const int n_px = n_left_px + 1 + (need_bottom ? txwpx : 0);
+        av1_filter_intra_edge(left_col - 1, n_px, strength);
       }
     }
-    upsample_above = av1_use_intra_edge_upsample(txwpx, txhpx, p_angle - 90,
-                                                 intra_edge_filter_type);
-    if (need_above && upsample_above) {
-      const int n_px = txwpx + (need_right ? txhpx : 0);
-      av1_upsample_intra_edge(above_row, n_px);
+    upsample_above =
+        need_above && av1_use_intra_edge_upsample(txwpx, txhpx, p_angle - 90,
+                                                  intra_edge_filter_type);
+    if (upsample_above) {
+      av1_upsample_intra_edge(above_row, txwpx + (need_right ? txhpx : 0));
     }
-    upsample_left = av1_use_intra_edge_upsample(txhpx, txwpx, p_angle - 180,
-                                                intra_edge_filter_type);
-    if (need_left && upsample_left) {
-      const int n_px = txhpx + (need_bottom ? txwpx : 0);
-      av1_upsample_intra_edge(left_col, n_px);
+    upsample_left =
+        need_left && av1_use_intra_edge_upsample(txhpx, txwpx, p_angle - 180,
+                                                 intra_edge_filter_type);
+    if (upsample_left) {
+      av1_upsample_intra_edge(left_col, txhpx + (need_bottom ? txwpx : 0));
     }
   }
-  dr_predictor(dst, dst_stride, tx_size, above_row, left_col, upsample_above,
-               upsample_left, p_angle);
+  if (p_angle < 90) {
+    av1_dr_prediction_z1(dst, dst_stride, txwpx, txhpx, above_row, left_col,
+                         upsample_above, dr_intra_derivative[p_angle], 1);
+  } else if (p_angle > 180) {
+    av1_dr_prediction_z3(dst, dst_stride, txwpx, txhpx, above_row, left_col,
+                         upsample_left, 1, dr_intra_derivative[270 - p_angle]);
+  } else {
+    av1_dr_prediction_z2(dst, dst_stride, txwpx, txhpx, above_row, left_col,
+                         upsample_above, upsample_left,
+                         dr_intra_derivative[180 - p_angle],
+                         dr_intra_derivative[p_angle - 90]);
+  }
 }

 static AOM_FORCE_INLINE uint32_t sum_above_row_lbd(const uint8_t *above_ref,
@@ -1397,20 +1681,20 @@ static AOM_FORCE_INLINE void h_pred_direct_lbd(uint8_t *dst, int dst_stride,
   assert(txhpx >= 4 && (txhpx & 3) == 0);
   if (txwpx == 4) {
     do {
-      fill_intra_edge_lbd(dst, *left_ref, 4);
+      fill_intra_edge_lbd_64(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);
+      fill_intra_edge_lbd_64(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);
+      fill_intra_edge_lbd_64(dst, *left_ref, 16);
       dst += dst_stride;
       left_ref += ref_stride;
     } while (--txhpx != 0);
@@ -1419,45 +1703,50 @@ static AOM_FORCE_INLINE void h_pred_direct_lbd(uint8_t *dst, int dst_stride,

 // 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(
+// Out-of-line so 160B aligned stack buffers stay off the intra facade.
+static AOM_NOINLINE void build_non_directional_intra_predictors(
     const uint8_t *ref, int ref_stride, uint8_t *dst, int dst_stride,
-    PREDICTION_MODE mode, TX_SIZE tx_size, int n_top_px, int n_left_px) {
+    uint32_t mode_tx, uint32_t px_pack) {
+  const PREDICTION_MODE mode = (PREDICTION_MODE)(mode_tx & 0xff);
+  const TX_SIZE tx_size = (TX_SIZE)((mode_tx >> 8) & 0xff);
+  const int n_top_px = (int)(px_pack & 0xffff);
+  const int n_left_px = (int)((px_pack >> 16) & 0xffff);
   const uint8_t *above_ref = ref - ref_stride;
   const uint8_t *left_ref = ref - 1;
   const int txwpx = tx_size_wide[tx_size];
   const int txhpx = tx_size_high[tx_size];
-  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);

-  DECLARE_ALIGNED(16, uint8_t, left_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
-  DECLARE_ALIGNED(16, uint8_t, above_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
+  DECLARE_ALIGNED(16, uint8_t, left_data[16 + MAX_TX_SIZE]);
+  DECLARE_ALIGNED(16, uint8_t, above_data[16 + MAX_TX_SIZE]);
   uint8_t *const above_row = above_data + 16;
   uint8_t *const left_col = left_data + 16;

-  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 (UNLIKELY(i < txhpx))
-      fill_intra_edge_lbd(&left_col[i], left_col[i - 1], txhpx - i);
+  if (n_left_px == txhpx) {
+    gather_left_col_pow2_lbd(left_col, left_ref, ref_stride, txhpx);
+  } else if (n_left_px > 0) {
+    assert(n_left_px < txhpx);
+    gather_left_col_lbd(left_col, left_ref, ref_stride, n_left_px);
+    fill_intra_edge_lbd_tail(&left_col[n_left_px], left_col[n_left_px - 1],
+                             txhpx - n_left_px, 63);
   } 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);
+    fill_intra_edge_lbd_64(left_col, val, txhpx);
   }

-  if (LIKELY(n_top_px == txwpx)) {
-    copy_above_row_lbd(above_row, above_ref, txwpx);
+  if (n_top_px == txwpx) {
+    copy_above_row_lbd_64(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);
+    assert(n_top_px < txwpx);
+    copy_above_row_lbd_partial(above_row, above_ref, n_top_px, 63);
+    fill_intra_edge_lbd_tail(&above_row[n_top_px], above_row[n_top_px - 1],
+                             txwpx - n_top_px, 63);
   } 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);
+    fill_intra_edge_lbd_64(above_row, val, txwpx);
   }

   if (mode == PAETH_PRED) {
@@ -1565,12 +1854,25 @@ static AOM_FORCE_INLINE void copy_above_row_hbd(uint16_t *above_row,
   }
 }

-static void highbd_build_directional_and_filter_intra_predictors(
-    const uint8_t *ref8, int ref_stride, uint8_t *dst8, int dst_stride,
-    PREDICTION_MODE mode, int p_angle, FILTER_INTRA_MODE filter_intra_mode,
-    TX_SIZE tx_size, int disable_edge_filter, int n_top_px, int n_topright_px,
-    int n_left_px, int n_bottomleft_px, int intra_edge_filter_type,
-    int bit_depth) {
+// Out-of-line HBD builder so low-bitdepth callers avoid HBD stack frames.
+static AOM_NOINLINE void highbd_build_directional_and_filter_intra_predictors(
+    const MACROBLOCKD *xd, const uint8_t *ref8, int ref_stride, uint8_t *dst8,
+    int dst_stride, uint64_t meta, uint64_t px_pack) {
+  const TX_SIZE tx_size = (TX_SIZE)(meta & 0xff);
+  const int plane = (int)((meta >> 8) & 0xff);
+  const int p_angle = (int)((meta >> 16) & 0xffff);
+  const int disable_edge_filter = (int)((meta >> 32) & 1);
+  const BLOCK_SIZE sb_size = (BLOCK_SIZE)((meta >> 35) & 0xff);
+  const FILTER_INTRA_MODE filter_intra_mode =
+      (FILTER_INTRA_MODE)((meta >> 59) & 0x7);
+  (void)sb_size;
+  assert(sb_size == BLOCK_64X64 || sb_size == BLOCK_128X128);
+
+  const int n_top_px = (int)(px_pack & 0xffff);
+  const int n_left_px = (int)((px_pack >> 16) & 0xffff);
+  const int xr_clamped = (int)((px_pack >> 32) & 0xffff);
+  const int yd_clamped = (int)((px_pack >> 48) & 0xffff);
+
   int i;
   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
   const uint16_t *const ref = CONVERT_TO_SHORTPTR(ref8);
@@ -1578,19 +1880,15 @@ static void highbd_build_directional_and_filter_intra_predictors(
   const int txhpx = tx_size_high[tx_size];
   const uint16_t *above_ref = ref - ref_stride;
   const uint16_t *left_ref = ref - 1;
-  const int is_dr_mode = av1_is_directional_mode(mode);
   const int use_filter_intra = filter_intra_mode != FILTER_INTRA_MODES;
-  assert(use_filter_intra || is_dr_mode);
-  (void)is_dr_mode;
+  assert(use_filter_intra || (p_angle > 0 && p_angle < 270));
+  const int bit_depth = xd->bd;
   const int base = 128 << (bit_depth - 8);
   const int need_above = use_filter_intra || (p_angle < 180);
   const int need_left = use_filter_intra || (p_angle > 90);
-  const int need_above_left = 1;

   assert(n_top_px >= 0);
-  assert(n_topright_px >= -1);
   assert(n_left_px >= 0);
-  assert(n_bottomleft_px >= -1);

   if ((!need_above && n_left_px == 0) || (!need_left && n_top_px == 0)) {
     int val;
@@ -1606,6 +1904,20 @@ static void highbd_build_directional_and_filter_intra_predictors(
     return;
   }

+  int n_topright_px = -1;
+  int n_bottomleft_px = -1;
+  if (!use_filter_intra) {
+    if (p_angle < 90) {
+      n_topright_px = (xr_clamped > 0 && has_top_right_available(xd, meta))
+                          ? xr_clamped
+                          : 0;
+    } else if (p_angle > 180) {
+      n_bottomleft_px = (yd_clamped > 0 && has_bottom_left_available(xd, meta))
+                            ? yd_clamped
+                            : 0;
+    }
+  }
+
   DECLARE_ALIGNED(16, uint16_t, left_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
   DECLARE_ALIGNED(16, uint16_t, above_data[NUM_INTRA_NEIGHBOUR_PIXELS]);
   uint16_t *const above_row = above_data + 16;
@@ -1619,10 +1931,14 @@ static void highbd_build_directional_and_filter_intra_predictors(
       const int n_left_copy =
           n_left_px + (n_bottomleft_px > 0 ? n_bottomleft_px : 0);
       assert(n_left_copy > 0);
-#if defined(__GNUC__) && !defined(__clang__)
+      i = 0;
+#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \
+    __GNUC__ >= 14
 #pragma GCC novector
 #endif
-      for (i = 0; i < n_left_copy; i++) left_col[i] = left_ref[i * ref_stride];
+      do {
+        left_col[i] = left_ref[(ptrdiff_t)i * ref_stride];
+      } while (++i != n_left_copy);
       if (i < num_left_pixels_needed)
         aom_memset16(&left_col[i], left_col[i - 1], num_left_pixels_needed - i);
     } else {
@@ -1655,6 +1971,9 @@ static void highbd_build_directional_and_filter_intra_predictors(
       return;
     }
     if (p_angle == 180) {
+      // aom_highbd_h_predictor_8x4_sse2 loads 16 bytes from left; zero the
+      // unused 4-sample tail to avoid uninitialized stack reads.
+      if (txhpx == 4) memset(left_col + 4, 0, 4 * sizeof(left_col[0]));
       pred_high[H_PRED][tx_size](dst, dst_stride, above_row, left_col,
                                  bit_depth);
       return;
@@ -1662,37 +1981,36 @@ static void highbd_build_directional_and_filter_intra_predictors(
   }

   const int tail_zero = (txwpx + txhpx == 128) ? 16 : 32;
-  memset(above_data, 0, 16 * sizeof(above_data[0]));
-  memset(left_data, 0, 16 * sizeof(left_data[0]));
+  uint16_t corner;
+  if (n_top_px > 0 && n_left_px > 0) {
+    corner = above_ref[-1];
+  } else if (n_top_px > 0) {
+    corner = above_ref[0];
+  } else if (n_left_px > 0) {
+    corner = left_ref[0];
+  } else {
+    corner = (uint16_t)base;
+  }
   if (need_above) {
     const int num_top_pixels_needed = txwpx + (n_topright_px >= 0 ? txhpx : 0);
     assert(num_top_pixels_needed >= 0 &&
            num_top_pixels_needed <= txwpx + txhpx);
+    memset(above_data, 0, 16 * sizeof(above_data[0]));
     memset(above_row + num_top_pixels_needed, 0,
-           ((txwpx + txhpx + tail_zero) - num_top_pixels_needed) *
+           (size_t)((txwpx + txhpx + tail_zero) - num_top_pixels_needed) *
                sizeof(above_row[0]));
+    above_row[-1] = corner;
   }
   if (need_left) {
     const int num_left_pixels_needed =
         txhpx + (n_bottomleft_px >= 0 ? txwpx : 0);
     assert(num_left_pixels_needed >= 0 &&
            num_left_pixels_needed <= txhpx + txwpx);
+    memset(left_data, 0, 16 * sizeof(left_data[0]));
     memset(left_col + num_left_pixels_needed, 0,
-           ((txhpx + txwpx + tail_zero) - num_left_pixels_needed) *
+           (size_t)((txhpx + txwpx + tail_zero) - num_left_pixels_needed) *
                sizeof(left_col[0]));
-  }
-
-  if (need_above_left) {
-    if (n_top_px > 0 && n_left_px > 0) {
-      above_row[-1] = above_ref[-1];
-    } else if (n_top_px > 0) {
-      above_row[-1] = above_ref[0];
-    } else if (n_left_px > 0) {
-      above_row[-1] = left_ref[0];
-    } else {
-      above_row[-1] = base;
-    }
-    left_col[-1] = above_row[-1];
+    left_col[-1] = corner;
   }

   if (use_filter_intra) {
@@ -1701,55 +2019,78 @@ static void highbd_build_directional_and_filter_intra_predictors(
     return;
   }

-  assert(is_dr_mode);
+  assert(p_angle > 0 && p_angle < 270);
   int upsample_above = 0;
   int upsample_left = 0;
   if (!disable_edge_filter) {
     const int need_right = p_angle < 90;
     const int need_bottom = p_angle > 180;
-    if (p_angle != 90 && p_angle != 180) {
-      assert(need_above_left);
-      const int ab_le = 1;
-      if (need_above && need_left && (txwpx + txhpx >= 24)) {
-        highbd_filter_intra_edge_corner(above_row, left_col);
-      }
-      if (need_above && n_top_px > 0) {
-        const int strength = intra_edge_filter_strength(
-            txwpx, txhpx, p_angle - 90, intra_edge_filter_type);
-        const int n_px = n_top_px + ab_le + (need_right ? txhpx : 0);
-        av1_highbd_filter_intra_edge(above_row - ab_le, n_px, strength);
+    const int intra_edge_filter_type =
+        need_intra_edge_filter_type_check(txwpx, txhpx, p_angle, n_top_px,
+                                          n_left_px)
+            ? get_intra_edge_filter_type(xd, plane)
+            : 0;
+    if (need_above && need_left && (txwpx + txhpx >= 24)) {
+      highbd_filter_intra_edge_corner(above_row, left_col);
+    }
+    if (need_above && n_top_px > 0) {
+      const int strength = intra_edge_filter_strength(
+          txwpx, txhpx, p_angle - 90, intra_edge_filter_type);
+      if (strength) {
+        const int n_px = n_top_px + 1 + (need_right ? txhpx : 0);
+        av1_highbd_filter_intra_edge(above_row - 1, n_px, strength);
       }
-      if (need_left && n_left_px > 0) {
-        const int strength = intra_edge_filter_strength(
-            txhpx, txwpx, p_angle - 180, intra_edge_filter_type);
-        const int n_px = n_left_px + ab_le + (need_bottom ? txwpx : 0);
-        av1_highbd_filter_intra_edge(left_col - ab_le, n_px, strength);
+    }
+    if (need_left && n_left_px > 0) {
+      const int strength = intra_edge_filter_strength(
+          txhpx, txwpx, p_angle - 180, intra_edge_filter_type);
+      if (strength) {
+        const int n_px = n_left_px + 1 + (need_bottom ? txwpx : 0);
+        av1_highbd_filter_intra_edge(left_col - 1, n_px, strength);
       }
     }
-    upsample_above = av1_use_intra_edge_upsample(txwpx, txhpx, p_angle - 90,
-                                                 intra_edge_filter_type);
-    if (need_above && upsample_above) {
+    upsample_above =
+        need_above && av1_use_intra_edge_upsample(txwpx, txhpx, p_angle - 90,
+                                                  intra_edge_filter_type);
+    if (upsample_above) {
       const int n_px = txwpx + (need_right ? txhpx : 0);
       av1_highbd_upsample_intra_edge(above_row, n_px, bit_depth);
     }
-    upsample_left = av1_use_intra_edge_upsample(txhpx, txwpx, p_angle - 180,
-                                                intra_edge_filter_type);
-    if (need_left && upsample_left) {
+    upsample_left =
+        need_left && av1_use_intra_edge_upsample(txhpx, txwpx, p_angle - 180,
+                                                 intra_edge_filter_type);
+    if (upsample_left) {
       const int n_px = txhpx + (need_bottom ? txwpx : 0);
       av1_highbd_upsample_intra_edge(left_col, n_px, bit_depth);
     }
   }
-  highbd_dr_predictor(dst, dst_stride, tx_size, above_row, left_col,
-                      upsample_above, upsample_left, p_angle, bit_depth);
+  if (p_angle < 90) {
+    av1_highbd_dr_prediction_z1(dst, dst_stride, txwpx, txhpx, above_row,
+                                left_col, upsample_above,
+                                dr_intra_derivative[p_angle], 1, bit_depth);
+  } else if (p_angle > 180) {
+    av1_highbd_dr_prediction_z3(dst, dst_stride, txwpx, txhpx, above_row,
+                                left_col, upsample_left, 1,
+                                dr_intra_derivative[270 - p_angle], bit_depth);
+  } else {
+    av1_highbd_dr_prediction_z2(dst, dst_stride, txwpx, txhpx, above_row,
+                                left_col, upsample_above, upsample_left,
+                                dr_intra_derivative[180 - p_angle],
+                                dr_intra_derivative[p_angle - 90], bit_depth);
+  }
 }

 // For HBD encode/decode, 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 highbd_build_non_directional_intra_predictors(
+// SMOOTH_V and PAETH). Out-of-line so low-bitdepth paths avoid HBD frames.
+static AOM_NOINLINE void highbd_build_non_directional_intra_predictors(
     const uint8_t *ref8, int ref_stride, uint8_t *dst8, int dst_stride,
-    PREDICTION_MODE mode, TX_SIZE tx_size, int n_top_px, int n_left_px,
-    int bit_depth) {
+    uint32_t mode_tx_bd, uint32_t px_pack) {
+  const PREDICTION_MODE mode = (PREDICTION_MODE)(mode_tx_bd & 0xff);
+  const TX_SIZE tx_size = (TX_SIZE)((mode_tx_bd >> 8) & 0xff);
+  const int bit_depth = (int)((mode_tx_bd >> 16) & 0xff);
+  const int n_top_px = (int)(px_pack & 0xffff);
+  const int n_left_px = (int)((px_pack >> 16) & 0xffff);
   int i = 0;
   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
   const uint16_t *const ref = CONVERT_TO_SHORTPTR(ref8);
@@ -1788,10 +2129,14 @@ static void highbd_build_non_directional_intra_predictors(

   if (need_left) {
     if (n_left_px > 0) {
-#if defined(__GNUC__) && !defined(__clang__)
+      i = 0;
+#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && \
+    __GNUC__ >= 14
 #pragma GCC novector
 #endif
-      for (i = 0; i < n_left_px; i++) left_col[i] = left_ref[i * ref_stride];
+      do {
+        left_col[i] = left_ref[(ptrdiff_t)i * ref_stride];
+      } while (++i != n_left_px);
       if (i < txhpx) aom_memset16(&left_col[i], left_col[i - 1], txhpx - i);
     } else {
       const int val = (n_top_px > 0) ? above_ref[0] : base + 1;
@@ -1834,110 +2179,155 @@ static void highbd_build_non_directional_intra_predictors(
 }
 #endif  // CONFIG_AV1_HIGHBITDEPTH

-static AOM_FORCE_INLINE BLOCK_SIZE scale_chroma_bsize(BLOCK_SIZE bsize,
-                                                      int subsampling_x,
-                                                      int subsampling_y) {
-  assert(subsampling_x >= 0 && subsampling_x < 2);
-  assert(subsampling_y >= 0 && subsampling_y < 2);
-  BLOCK_SIZE bs = bsize;
-  switch (bsize) {
-    case BLOCK_4X4:
-      if (subsampling_x == 1 && subsampling_y == 1)
-        bs = BLOCK_8X8;
-      else if (subsampling_x == 1)
-        bs = BLOCK_8X4;
-      else if (subsampling_y == 1)
-        bs = BLOCK_4X8;
-      break;
-    case BLOCK_4X8:
-      if (subsampling_x == 1 && subsampling_y == 1)
-        bs = BLOCK_8X8;
-      else if (subsampling_x == 1)
-        bs = BLOCK_8X8;
-      else if (subsampling_y == 1)
-        bs = BLOCK_4X8;
-      break;
-    case BLOCK_8X4:
-      if (subsampling_x == 1 && subsampling_y == 1)
-        bs = BLOCK_8X8;
-      else if (subsampling_x == 1)
-        bs = BLOCK_8X4;
-      else if (subsampling_y == 1)
-        bs = BLOCK_8X8;
-      break;
-    case BLOCK_4X16:
-      if (subsampling_x == 1 && subsampling_y == 1)
-        bs = BLOCK_8X16;
-      else if (subsampling_x == 1)
-        bs = BLOCK_8X16;
-      else if (subsampling_y == 1)
-        bs = BLOCK_4X16;
-      break;
-    case BLOCK_16X4:
-      if (subsampling_x == 1 && subsampling_y == 1)
-        bs = BLOCK_16X8;
-      else if (subsampling_x == 1)
-        bs = BLOCK_16X4;
-      else if (subsampling_y == 1)
-        bs = BLOCK_16X8;
-      break;
-    default: break;
-  }
-  return bs;
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-__attribute__((noinline))
-#endif
-static void predict_palette_block(const MACROBLOCKD *xd, int wpx, int txwpx,
-                                  int txhpx, int x, int y, uint8_t *dst,
-                                  int dst_stride, int plane, int is_hbd) {
-  int r, c;
+// Out-of-line rare palette path to keep the intra facade stackless.
+static AOM_NOINLINE void predict_palette_block(const MACROBLOCKD *xd, int wpx,
+                                               uint8_t *dst, int dst_stride,
+                                               int plane, int txwpx, int txhpx,
+                                               int x, int y, bool is_hbd) {
   const MB_MODE_INFO *const mbmi = xd->mi[0];
-  const uint8_t *const map = xd->plane[plane != 0].color_index_map +
-                             xd->color_index_map_offset[plane != 0];
+  const uint8_t *map_row = xd->plane[plane != 0].color_index_map +
+                           xd->color_index_map_offset[plane != 0] +
+                           (ptrdiff_t)y * wpx + x;
   const uint16_t *const palette =
       mbmi->palette_mode_info.palette_colors + plane * PALETTE_MAX_SIZE;
+  assert(txhpx > 0 && txwpx > 0);
+#if CONFIG_AV1_HIGHBITDEPTH
   if (is_hbd) {
     uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
-    for (r = 0; r < txhpx; ++r) {
-      for (c = 0; c < txwpx; ++c) {
-        dst16[r * dst_stride + c] = palette[map[(r + y) * wpx + c + x]];
-      }
-    }
-  } else {
-    for (r = 0; r < txhpx; ++r) {
-      for (c = 0; c < txwpx; ++c) {
-        dst[r * dst_stride + c] = (uint8_t)palette[map[(r + y) * wpx + c + x]];
-      }
+    int r = txhpx;
+    for (;;) {
+      int c = 0;
+      do {
+        dst16[c] = palette[map_row[c]];
+      } while (++c != txwpx);
+      if (--r == 0) break;
+      dst16 += dst_stride;
+      map_row += wpx;
     }
+    return;
   }
+#else
+  (void)is_hbd;
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+  int r = txhpx;
+  for (;;) {
+    int c = 0;
+    do {
+      dst[c] = (uint8_t)palette[map_row[c]];
+    } while (++c != txwpx);
+    if (--r == 0) break;
+    dst += dst_stride;
+    map_row += wpx;
+  }
+}
+
+// Cross-file alignment invariant:
+// - txwpx >= 16: aom_{v,dc*}_predictor_{16,32,64}x*_sse2 use aligned
+//   _mm_load_si128 / movdqa loads from `above`, requiring 16-byte alignment.
+// - txwpx == 4: aom_v_predictor_4x*_sse2 dereference `*(uint32_t *)above` and
+//   ARM NEON load_u8_4x1() requires 4-byte alignment under
+//   -fsanitize=alignment.
+// - txwpx == 8: SSE2/NEON predictors use unaligned 64-bit loads.
+static AOM_FORCE_INLINE int is_above_aligned(const uint8_t *above, int txwpx) {
+  const uintptr_t above_align_mask =
+      (txwpx >= 16) ? 15u : ((txwpx == 4) ? 3u : 0u);
+  return ((uintptr_t)above & above_align_mask) == 0;
+}
+
+// Separate stack frame for left_col so interior DC stays off the facade.
+static AOM_NOINLINE void predict_dc_interior_lbd(const uint8_t *ref,
+                                                 int ref_stride, uint8_t *dst,
+                                                 int dst_stride,
+                                                 TX_SIZE tx_size) {
+  assert(is_above_aligned(ref - ref_stride, tx_size_wide[tx_size]));
+  const int txhpx = tx_size_high[tx_size];
+  DECLARE_ALIGNED(16, uint8_t, left_col[MAX_TX_SIZE]);
+  gather_left_col_pow2_lbd(left_col, ref - 1, ref_stride, txhpx);
+  dc_pred[1][1][tx_size](dst, dst_stride, ref - ref_stride, left_col);
 }

+// Separate stack frame for left_col so filter-intra stays off the facade.
+static AOM_NOINLINE void predict_filter_intra_interior_lbd(
+    const uint8_t *ref, int ref_stride, uint8_t *dst, int dst_stride,
+    TX_SIZE tx_size, FILTER_INTRA_MODE filter_intra_mode) {
+  const int txwpx = tx_size_wide[tx_size];
+  const int txhpx = tx_size_high[tx_size];
+  assert(txwpx <= 32 && txhpx <= 32);
+  // 64B matches siblings and avoids Clang 4x unroll bloat and GCC -DNDEBUG
+  // -Wstringop-overflow warnings without capping `txhpx`.
+  DECLARE_ALIGNED(16, uint8_t, left_col[MAX_TX_SIZE]);
+  gather_left_col_pow2_lbd(left_col, ref - 1, ref_stride, txhpx);
+  const uint8_t *const above_ref = ref - ref_stride;
+  // Stage the 5 valid bytes above[-1..3] defensively for 4-wide blocks so SIMD
+  // callers never read past a 4-pixel right tile or frame edge.
+  if (txwpx == 4) {
+    DECLARE_ALIGNED(16, uint8_t, above_buf[16]) = { 0 };
+    memcpy(above_buf, above_ref - 1, 5);
+    av1_filter_intra_predictor(dst, dst_stride, tx_size, above_buf + 1,
+                               left_col, filter_intra_mode);
+    return;
+  }
+  av1_filter_intra_predictor(dst, dst_stride, tx_size, above_ref, left_col,
+                             filter_intra_mode);
+}
+
+// Separate stack frame for left_col so H_PRED stays off the facade.
+static AOM_NOINLINE void predict_h_interior_lbd(const uint8_t *ref,
+                                                int ref_stride, uint8_t *dst,
+                                                int dst_stride,
+                                                TX_SIZE tx_size) {
+  const int txhpx = tx_size_high[tx_size];
+  assert(tx_size_wide[tx_size] >= 32);
+  assert(txhpx >= 8);
+  DECLARE_ALIGNED(16, uint8_t, left_col[MAX_TX_SIZE]);
+  // aom_h_predictor_32x8_sse2 loads 16 bytes from `left`; zero bytes 8..15 to
+  // avoid MSan reads.
+  if (txhpx == 8) memset(left_col + 8, 0, 8);
+  gather_left_col_pow2_lbd(left_col, ref - 1, ref_stride, txhpx);
+  pred[H_PRED][tx_size](dst, dst_stride, NULL, left_col);
+}
+
+static inline uint8_t *get_intra_dst_ptr(const struct macroblockd_plane *pd,
+                                         int blk_col, int blk_row) {
+  const int dst_stride = pd->dst.stride;
+  return &pd->dst
+              .buf[((ptrdiff_t)blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
+}
+
+// Dual-source sequence/block dispatch: when `has_seq_params` is true (facade
+// callers), `seq_params->sb_size`, `seq_params->enable_intra_edge_filter`, and
+// `mbmi->angle_delta` are loaded lazily after the fast paths return, and the
+// `*_arg` parameters are ignored. When `has_seq_params` is false
+// (`av1_predict_intra_block`), the `*_arg` parameters are used and
+// `seq_params` is NULL.
 static AOM_FORCE_INLINE void predict_intra_block_internal(
-    const MACROBLOCKD *xd, BLOCK_SIZE sb_size, int enable_intra_edge_filter,
-    int wpx, int hpx, TX_SIZE tx_size, PREDICTION_MODE mode, int angle_delta,
-    int use_palette, FILTER_INTRA_MODE filter_intra_mode, const uint8_t *ref,
-    int ref_stride, uint8_t *dst, int dst_stride, int col_off, int row_off,
-    int plane) {
+    const MACROBLOCKD *xd, bool is_hbd, bool has_seq_params,
+    const SequenceHeader *seq_params, BLOCK_SIZE sb_size_arg,
+    int enable_intra_edge_filter_arg, int wpx, int hpx, TX_SIZE tx_size,
+    PREDICTION_MODE mode, int angle_delta_arg, int use_palette,
+    FILTER_INTRA_MODE filter_intra_mode, const uint8_t *ref, int ref_stride,
+    uint8_t *dst, int dst_stride, int col_off, int row_off, int plane) {
+  assert((unsigned)tx_size < TX_SIZES_ALL);
+  assert((unsigned)plane < MAX_MB_PLANE);
+  assert(mode < INTRA_MODES);
+
   const MB_MODE_INFO *const mbmi = xd->mi[0];
   const int txwpx = tx_size_wide[tx_size];
   const int txhpx = tx_size_high[tx_size];
   const int x = col_off << MI_SIZE_LOG2;
   const int y = row_off << MI_SIZE_LOG2;
-  const int is_hbd = is_cur_buf_hbd(xd);
-
-  assert(mode < INTRA_MODES);

   if (UNLIKELY(use_palette)) {
-    predict_palette_block(xd, wpx, txwpx, txhpx, x, y, dst, dst_stride, plane,
+    predict_palette_block(xd, wpx, dst, dst_stride, plane, txwpx, txhpx, x, y,
                           is_hbd);
     return;
   }

   const struct macroblockd_plane *const pd = &xd->plane[plane];
-  const int ss_x = pd->subsampling_x;
-  const int ss_y = pd->subsampling_y;
+  // Luma subsampling is always 0; branching on `plane == AOM_PLANE_Y` enables
+  // constant folding in the luma facade.
+  const int ss_x = (plane == AOM_PLANE_Y) ? 0 : pd->subsampling_x;
+  const int ss_y = (plane == AOM_PLANE_Y) ? 0 : pd->subsampling_y;
   const int have_top =
       row_off || (ss_y ? xd->chroma_up_available : xd->up_available);
   const int have_left =
@@ -1949,17 +2339,40 @@ static AOM_FORCE_INLINE void predict_intra_block_internal(
   // Distance between the bottom edge of this prediction block to
   // the frame bottom edge
   const int yd = (xd->mb_to_bottom_edge >> (3 + ss_y)) + hpx - y - txhpx;
-  const int use_filter_intra = filter_intra_mode != FILTER_INTRA_MODES;
-  const int is_dr_mode = av1_is_directional_mode(mode);
-
   const int n_top_px = have_top ? clamp(xr + txwpx, 0, txwpx) : 0;
   const int n_left_px = have_left ? clamp(yd + txhpx, 0, txhpx) : 0;
-  if (!use_filter_intra && !is_dr_mode) {
+
+  if (filter_intra_mode != FILTER_INTRA_MODES) {
+    if (!is_hbd && n_top_px == txwpx && n_left_px == txhpx) {
+      predict_filter_intra_interior_lbd(ref, ref_stride, dst, dst_stride,
+                                        tx_size, filter_intra_mode);
+      return;
+    }
+    const BLOCK_SIZE sb_size =
+        has_seq_params ? seq_params->sb_size : sb_size_arg;
+    const uint64_t dr_meta =
+        pack_dr_facade_meta(tx_size, plane, 0, 0, ss_x, ss_y, sb_size, col_off,
+                            row_off, filter_intra_mode);
+    const uint64_t dr_px = pack_dr_facade_px(n_top_px, n_left_px, 0, 0);
+#if CONFIG_AV1_HIGHBITDEPTH
+    if (is_hbd) {
+      highbd_build_directional_and_filter_intra_predictors(
+          xd, ref, ref_stride, dst, dst_stride, dr_meta, dr_px);
+      return;
+    }
+#endif
+    build_directional_and_filter_intra_predictors(xd, ref, ref_stride, dst,
+                                                  dst_stride, dr_meta, dr_px);
+    return;
+  }
+
+  if (!av1_is_directional_mode(mode)) {
 #if CONFIG_AV1_HIGHBITDEPTH
     if (is_hbd) {
       highbd_build_non_directional_intra_predictors(
-          ref, ref_stride, dst, dst_stride, mode, tx_size, n_top_px, n_left_px,
-          xd->bd);
+          ref, ref_stride, dst, dst_stride,
+          pack_non_dir_meta(mode, tx_size, xd->bd),
+          pack_non_dir_px(n_top_px, n_left_px));
       return;
     }
 #endif  // CONFIG_AV1_HIGHBITDEPTH
@@ -1971,80 +2384,94 @@ static AOM_FORCE_INLINE void predict_intra_block_internal(
       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;
-  }
-
-  int p_angle = 0;
-  int n_topright_px = -1;
-  int n_bottomleft_px = -1;
-
-  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);
+    if (mode == DC_PRED && n_top_px == txwpx &&
+        is_above_aligned(ref - ref_stride, txwpx)) {
+      if (n_left_px == txhpx) {
+        predict_dc_interior_lbd(ref, ref_stride, dst, dst_stride, tx_size);
         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);
+      }
+      if (n_left_px == 0) {
+        dc_pred[0][1][tx_size](dst, dst_stride, ref - ref_stride, NULL);
         return;
       }
     }
-    if (p_angle < 90) {
-      n_topright_px = 0;
-      if (have_top && xr > 0) {
-        const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
-        const int txw = tx_size_wide_unit[tx_size];
-        if (mi_col + ((col_off + txw) << ss_x) < xd->tile.mi_col_end) {
-          const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
-          BLOCK_SIZE bsize = mbmi->bsize;
-          if (ss_x || ss_y) bsize = scale_chroma_bsize(bsize, ss_x, ss_y);
-          if (has_top_right(sb_size, bsize, mi_row, mi_col, 1, 1,
-                            mbmi->partition, tx_size, row_off, col_off, ss_x,
-                            ss_y)) {
-            n_topright_px = AOMMIN(xr, txwpx);
-          }
-        }
-      }
-    } else if (p_angle > 180) {
-      n_bottomleft_px = 0;
-      if (have_left && yd > 0) {
-        const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
-        const int txh = tx_size_high_unit[tx_size];
-        if (mi_row + ((row_off + txh) << ss_y) < xd->tile.mi_row_end) {
-          const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
-          BLOCK_SIZE bsize = mbmi->bsize;
-          if (ss_x || ss_y) bsize = scale_chroma_bsize(bsize, ss_x, ss_y);
-          if (has_bottom_left(sb_size, bsize, mi_row, mi_col, 1, 1,
-                              mbmi->partition, tx_size, row_off, col_off, ss_x,
-                              ss_y)) {
-            n_bottomleft_px = AOMMIN(yd, txhpx);
-          }
-        }
-      }
+    build_non_directional_intra_predictors(
+        ref, ref_stride, dst, dst_stride, pack_non_dir_meta(mode, tx_size, 0),
+        pack_non_dir_px(n_top_px, n_left_px));
+    return;
+  }
+
+  const int angle_delta =
+      has_seq_params ? mbmi->angle_delta[plane != AOM_PLANE_Y] * ANGLE_STEP
+                     : angle_delta_arg;
+  const int p_angle = mode_to_angle_map[mode] + angle_delta;
+  if (!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;
+    }
+    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_top_px == txwpx &&
+        is_above_aligned(ref - ref_stride, txwpx)) {
+      pred[V_PRED][tx_size](dst, dst_stride, ref - ref_stride, NULL);
+      return;
     }
+    if (p_angle == 180 && n_left_px == txhpx) {
+      predict_h_interior_lbd(ref, ref_stride, dst, dst_stride, tx_size);
+      return;
+    }
+  }
+  const int disable_edge_filter =
+      !(has_seq_params ? seq_params->enable_intra_edge_filter
+                       : enable_intra_edge_filter_arg);
+  if (!is_hbd && tx_size == TX_8X8 && p_angle > 90 && p_angle < 180 &&
+      n_top_px == 8 && n_left_px == 8) {
+    build_dr_z2_8x8_lbd(
+        xd, ref, ref_stride, dst, dst_stride,
+        pack_dr_z2_8x8_meta(plane, p_angle, disable_edge_filter));
+    return;
   }

-  const int disable_edge_filter = !enable_intra_edge_filter;
-  const int intra_edge_filter_type =
-      (!disable_edge_filter && !use_filter_intra && p_angle != 90 &&
-       p_angle != 180)
-          ? get_intra_edge_filter_type(xd, plane)
-          : 0;
+  const BLOCK_SIZE sb_size = has_seq_params ? seq_params->sb_size : sb_size_arg;
+  const int xr_clamped = (n_top_px == txwpx && xr > 0) ? AOMMIN(xr, txwpx) : 0;
+  const int yd_clamped = (n_left_px == txhpx && yd > 0) ? AOMMIN(yd, txhpx) : 0;
+
 #if CONFIG_AV1_HIGHBITDEPTH
   if (is_hbd) {
+    const uint64_t dr_meta = pack_dr_facade_meta(
+        tx_size, plane, p_angle, disable_edge_filter, ss_x, ss_y, sb_size,
+        col_off, row_off, FILTER_INTRA_MODES);
+    const uint64_t dr_px =
+        pack_dr_facade_px(n_top_px, n_left_px, xr_clamped, yd_clamped);
     highbd_build_directional_and_filter_intra_predictors(
-        ref, ref_stride, dst, dst_stride, mode, p_angle, filter_intra_mode,
-        tx_size, disable_edge_filter, n_top_px, n_topright_px, n_left_px,
-        n_bottomleft_px, intra_edge_filter_type, xd->bd);
+        xd, ref, ref_stride, dst, dst_stride, dr_meta, dr_px);
     return;
   }
 #endif
-  build_directional_and_filter_intra_predictors(
-      ref, ref_stride, dst, dst_stride, mode, p_angle, filter_intra_mode,
-      tx_size, disable_edge_filter, n_top_px, n_topright_px, n_left_px,
-      n_bottomleft_px, intra_edge_filter_type);
+  if (p_angle < 90 && tx_size == TX_8X8 && n_top_px == 8) {
+    build_dr_z1_8x8_lbd(
+        xd, ref, ref_stride, dst, dst_stride,
+        pack_dr_8x8_meta(plane, p_angle, disable_edge_filter, ss_x, ss_y,
+                         sb_size, col_off, row_off, n_left_px > 0, xr_clamped));
+    return;
+  }
+  if (p_angle > 180 && tx_size == TX_8X8 && n_left_px == 8) {
+    build_dr_z3_8x8_lbd(
+        xd, ref, ref_stride, dst, dst_stride,
+        pack_dr_8x8_meta(plane, p_angle, disable_edge_filter, ss_x, ss_y,
+                         sb_size, col_off, row_off, n_top_px > 0, yd_clamped));
+    return;
+  }
+  const uint64_t dr_meta =
+      pack_dr_facade_meta(tx_size, plane, p_angle, disable_edge_filter, ss_x,
+                          ss_y, sb_size, col_off, row_off, FILTER_INTRA_MODES);
+  const uint64_t dr_px =
+      pack_dr_facade_px(n_top_px, n_left_px, xr_clamped, yd_clamped);
+  build_directional_and_filter_intra_predictors(xd, ref, ref_stride, dst,
+                                                dst_stride, dr_meta, dr_px);
 }

 void av1_predict_intra_block(const MACROBLOCKD *xd, BLOCK_SIZE sb_size,
@@ -2055,24 +2482,21 @@ void av1_predict_intra_block(const MACROBLOCKD *xd, BLOCK_SIZE sb_size,
                              const uint8_t *ref, int ref_stride, uint8_t *dst,
                              int dst_stride, int col_off, int row_off,
                              int plane) {
-  predict_intra_block_internal(xd, sb_size, enable_intra_edge_filter, wpx, hpx,
-                               tx_size, mode, angle_delta, use_palette,
-                               filter_intra_mode, ref, ref_stride, dst,
-                               dst_stride, col_off, row_off, plane);
+  predict_intra_block_internal(
+      xd, is_cur_buf_hbd(xd), false, NULL, sb_size, enable_intra_edge_filter,
+      wpx, hpx, tx_size, mode, angle_delta, use_palette, filter_intra_mode, ref,
+      ref_stride, dst, 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) {
+// Cold chroma-from-luma path split out to keep the intra facade minimal.
+static AOM_NOINLINE void predict_intra_block_cfl(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 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;
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
 #if CONFIG_DEBUG
   assert(is_cfl_allowed(xd));
   const BLOCK_SIZE plane_bsize =
@@ -2087,13 +2511,13 @@ static AOM_NOINLINE void predict_cfl_block_facade(const AV1_COMMON *cm,
   }
 #endif
   CFL_CTX *const cfl = &xd->cfl;
-  CFL_PRED_TYPE pred_plane = get_cfl_pred_type(plane);
+  const 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);
+    const int use_palette = mbmi->palette_mode_info.palette_size[1] > 0;
+    av1_predict_intra_block(xd, BLOCK_INVALID, 0, 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;
@@ -2105,32 +2529,149 @@ static AOM_NOINLINE void predict_cfl_block_facade(const AV1_COMMON *cm,
 }
 #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) {
+// Out-of-line palette dispatch shared between luma and chroma facades.
+static AOM_NOINLINE void predict_intra_block_facade_palette(
+    const MACROBLOCKD *xd, int plane, int blk_col, int blk_row,
+    TX_SIZE tx_size) {
+  const struct macroblockd_plane *const pd = &xd->plane[plane];
+  const int dst_stride = pd->dst.stride;
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
+  predict_palette_block(xd, pd->width, dst, dst_stride, plane,
+                        tx_size_wide[tx_size], tx_size_high[tx_size],
+                        blk_col << MI_SIZE_LOG2, blk_row << MI_SIZE_LOG2,
+                        is_cur_buf_hbd(xd) != 0);
+}
+
+// Out-of-line filter-intra dispatch so the hot luma facade drops filter-intra
+// branches and register pressure (especially in realtime where it is unused).
+static AOM_NOINLINE void predict_intra_block_facade_filter_intra(
+    const AV1_COMMON *cm, const MACROBLOCKD *xd, int blk_col, int blk_row,
+    TX_SIZE tx_size) {
+  assert(!is_cur_buf_hbd(xd));
+  const FILTER_INTRA_MODE filter_intra_mode =
+      xd->mi[0]->filter_intra_mode_info.filter_intra_mode;
+  assert((unsigned)filter_intra_mode < FILTER_INTRA_MODES);
+  const struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
+  const int dst_stride = pd->dst.stride;
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
+  const int txwpx = tx_size_wide[tx_size];
+  const int txhpx = tx_size_high[tx_size];
+  const int x = blk_col << MI_SIZE_LOG2;
+  const int y = blk_row << MI_SIZE_LOG2;
+  const int n_top_px =
+      (blk_row || xd->up_available)
+          ? clamp((xd->mb_to_right_edge >> 3) + pd->width - x, 0, txwpx)
+          : 0;
+  const int n_left_px =
+      (blk_col || xd->left_available)
+          ? clamp((xd->mb_to_bottom_edge >> 3) + pd->height - y, 0, txhpx)
+          : 0;
+  if (n_top_px == txwpx && n_left_px == txhpx) {
+    predict_filter_intra_interior_lbd(dst, dst_stride, dst, dst_stride, tx_size,
+                                      filter_intra_mode);
+    return;
+  }
+  const uint64_t dr_meta = pack_dr_facade_meta(tx_size, AOM_PLANE_Y, 0, 0, 0, 0,
+                                               cm->seq_params->sb_size, blk_col,
+                                               blk_row, filter_intra_mode);
+  const uint64_t dr_px = pack_dr_facade_px(n_top_px, n_left_px, 0, 0);
+  build_directional_and_filter_intra_predictors(xd, dst, dst_stride, dst,
+                                                dst_stride, dr_meta, dr_px);
+}
+
+#if CONFIG_AV1_HIGHBITDEPTH
+// Out-of-line HBD facade path to keep low-bitdepth dispatch register-light.
+static AOM_NOINLINE void predict_intra_block_facade_hbd(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);
+  if (plane != AOM_PLANE_Y && mbmi->uv_mode == UV_CFL_PRED) {
+    predict_intra_block_cfl(xd, plane, blk_col, blk_row, tx_size);
     return;
   }
 #endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
-  struct macroblockd_plane *const pd = &xd->plane[plane];
+  if (mbmi->palette_mode_info.palette_size[plane != 0] > 0) {
+    predict_intra_block_facade_palette(xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+  const 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];
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
   const PREDICTION_MODE mode =
       (plane == AOM_PLANE_Y) ? mbmi->mode : get_uv_mode(mbmi->uv_mode);
-  const int use_palette = mbmi->palette_mode_info.palette_size[plane != 0] > 0;
   const FILTER_INTRA_MODE filter_intra_mode =
       (plane == AOM_PLANE_Y && mbmi->filter_intra_mode_info.use_filter_intra)
           ? mbmi->filter_intra_mode_info.filter_intra_mode
           : FILTER_INTRA_MODES;
-  const int angle_delta = mbmi->angle_delta[plane != AOM_PLANE_Y] * ANGLE_STEP;
-  const SequenceHeader *seq_params = cm->seq_params;
+  predict_intra_block_internal(xd, true, true, cm->seq_params, BLOCK_INVALID, 0,
+                               pd->width, pd->height, tx_size, mode, 0, 0,
+                               filter_intra_mode, dst, dst_stride, dst,
+                               dst_stride, blk_col, blk_row, plane);
+}
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+
+// Out-of-line chroma dispatch to keep the hot luma facade compact.
+static AOM_NOINLINE void predict_intra_block_facade_chroma(
+    const AV1_COMMON *cm, MACROBLOCKD *xd, int plane, int blk_col, int blk_row,
+    TX_SIZE tx_size) {
+  assert(!is_cur_buf_hbd(xd));
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+  if (mbmi->uv_mode == UV_CFL_PRED) {
+    predict_intra_block_cfl(xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+#endif  // !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+  if (mbmi->palette_mode_info.palette_size[1] > 0) {
+    predict_intra_block_facade_palette(xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+  const struct macroblockd_plane *const pd = &xd->plane[plane];
+  const int dst_stride = pd->dst.stride;
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
   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);
+      xd, false, true, cm->seq_params, BLOCK_INVALID, 0, pd->width, pd->height,
+      tx_size, get_uv_mode(mbmi->uv_mode), 0, 0, FILTER_INTRA_MODES, dst,
+      dst_stride, dst, dst_stride, blk_col, blk_row, plane);
+}
+
+void av1_predict_intra_block_facade(const AV1_COMMON *cm, MACROBLOCKD *xd,
+                                    int plane, int blk_col, int blk_row,
+                                    TX_SIZE tx_size) {
+  assert((unsigned)tx_size < TX_SIZES_ALL);
+  assert((unsigned)plane < MAX_MB_PLANE);
+#if CONFIG_AV1_HIGHBITDEPTH
+  if (is_cur_buf_hbd(xd)) {
+    predict_intra_block_facade_hbd(cm, xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+  if (plane != AOM_PLANE_Y) {
+    predict_intra_block_facade_chroma(cm, xd, plane, blk_col, blk_row, tx_size);
+    return;
+  }
+
+  const MB_MODE_INFO *const mbmi = xd->mi[0];
+  if (mbmi->palette_mode_info.palette_size[0] > 0) {
+    predict_intra_block_facade_palette(xd, AOM_PLANE_Y, blk_col, blk_row,
+                                       tx_size);
+    return;
+  }
+  if (mbmi->filter_intra_mode_info.use_filter_intra) {
+    predict_intra_block_facade_filter_intra(cm, xd, blk_col, blk_row, tx_size);
+    return;
+  }
+
+  const struct macroblockd_plane *const pd = &xd->plane[AOM_PLANE_Y];
+  const int dst_stride = pd->dst.stride;
+  uint8_t *const dst = get_intra_dst_ptr(pd, blk_col, blk_row);
+  predict_intra_block_internal(xd, false, true, cm->seq_params, BLOCK_INVALID,
+                               0, pd->width, pd->height, tx_size, mbmi->mode, 0,
+                               0, FILTER_INTRA_MODES, dst, dst_stride, dst,
+                               dst_stride, blk_col, blk_row, AOM_PLANE_Y);
 }

 void av1_init_intra_predictors(void) {
diff --git a/test/encode_internal_test.cc b/test/encode_internal_test.cc
index 39cf8208c0..11e5aec1af 100644
--- a/test/encode_internal_test.cc
+++ b/test/encode_internal_test.cc
@@ -10,18 +10,29 @@
  */

 #include <csetjmp>
+#include <cstddef>
 #include <cstdint>
+#include <cstdlib>
 #include <cstring>
 #include <memory>

 #include "gtest/gtest.h"

 #include "config/aom_config.h"
+#include "config/av1_rtcd.h"

+#include "aom/aom_image.h"
+#include "aom_dsp/aom_dsp_common.h"
 #include "aom_mem/aom_mem.h"
+#include "aom_ports/mem.h"
+#include "aom_scale/yv12config.h"
+#include "av1/common/av1_common_int.h"
 #include "av1/common/blockd.h"
+#include "av1/common/common_data.h"
+#include "av1/common/enums.h"
 #include "av1/common/reconintra.h"
 #include "av1/encoder/allintra_vis.h"
+#include "test/acm_random.h"

 namespace {

@@ -122,4 +133,1189 @@ TEST(EncodeInternal, Buganizer558417547) {
   aom_free(cpi_test->mb_weber_stats);
 }

+using AlignedU8Ptr = std::unique_ptr<uint8_t[], decltype(&aom_free)>;
+
+AlignedU8Ptr AllocAlignedU8(size_t count) {
+  return AlignedU8Ptr(static_cast<uint8_t *>(aom_memalign(16, count)),
+                      &aom_free);
+}
+
+#if CONFIG_AV1_HIGHBITDEPTH
+using AlignedU16Ptr = std::unique_ptr<uint16_t[], decltype(&aom_free)>;
+
+AlignedU16Ptr AllocAlignedU16(size_t count) {
+  return AlignedU16Ptr(
+      static_cast<uint16_t *>(aom_memalign(16, count * sizeof(uint16_t))),
+      &aom_free);
+}
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+
+int RefHasTopRightInSb(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
+                       int mi_col, PARTITION_TYPE partition) {
+  const int bw_unit = mi_size_wide[bsize];
+  const int bh_unit = mi_size_high[bsize];
+  const int sb_mi_size = mi_size_high[sb_size];
+  const int rm = (mi_row & (sb_mi_size - 1)) & -bh_unit;
+  if (rm == 0) return 1;
+  const int cm = (mi_col & (sb_mi_size - 1)) & -bw_unit;
+  if (cm + bw_unit >= sb_mi_size) return 0;
+
+  // Z-order inside SB: walk recursive split bit from coarsest to finest.
+  int size = sb_mi_size >> 1;
+  while (size >= bw_unit || size >= bh_unit) {
+    const int in_bot = (rm & size) != 0;
+    const int tr_crosses = ((cm + bw_unit) & size) != 0 && (cm & size) == 0;
+    const int tr_at_end = ((cm + bw_unit) & (size - 1)) == 0;
+    if (size == bw_unit && size == bh_unit && size > 1 &&
+        (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B)) {
+      if (in_bot) return 0;
+    }
+    if (tr_crosses && tr_at_end) {
+      // Crossing vertical boundary: top-right is coded iff rm is at a strictly
+      // coarser horizontal split.
+      return (rm & (size - 1)) == 0;
+    }
+    if (in_bot && (rm & (size - 1)) == 0) {
+      return 1;
+    }
+    size >>= 1;
+  }
+  return 0;
+}
+
+int RefHasBottomLeftInSb(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
+                         int mi_col, PARTITION_TYPE partition,
+                         int bottom_left_count_unit, int row_off, int ss_y) {
+  const int bw_unit = mi_size_wide[bsize];
+  const int bh_unit = mi_size_high[bsize];
+  const int sb_mi_size = mi_size_high[sb_size];
+  const int rm = (mi_row & (sb_mi_size - 1)) & -bh_unit;
+  const int cm = (mi_col & (sb_mi_size - 1)) & -bw_unit;
+
+  if (cm == 0) {
+    const int row_off_in_sb = (rm >> ss_y) + row_off;
+    const int sb_height_unit = sb_mi_size >> ss_y;
+    return row_off_in_sb + bottom_left_count_unit < sb_height_unit;
+  }
+  if (rm + bh_unit >= sb_mi_size) return 0;
+
+  // Z-order inside SB: walk split sizes from coarsest to finest.
+  int size = sb_mi_size >> 1;
+  while (size >= 1) {
+    const int in_top = (rm & size) == 0;
+    const int in_right = (cm & size) != 0;
+    const int bl_crosses_h = ((rm + bh_unit) & size) != 0 && in_top;
+    const int bl_crosses_v = in_right && ((cm - 1) & size) == 0;
+    if (size == bw_unit && size == bh_unit && size > 1 &&
+        (partition == PARTITION_VERT_A || partition == PARTITION_VERT_B) &&
+        in_top && in_right) {
+      return 1;
+    }
+    if (bl_crosses_h) return 0;
+    if (bl_crosses_v) return 1;
+    size >>= 1;
+  }
+  return 0;
+}
+
+int RefHasTopRight(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row, int mi_col,
+                   int mi_col_end, PARTITION_TYPE partition, TX_SIZE txsz,
+                   int row_off, int col_off, int ss_x, int ss_y) {
+  const int txw = tx_size_wide_unit[txsz];
+  if (mi_col + ((col_off + txw) << ss_x) >= mi_col_end) return 0;
+  const int bw_unit = mi_size_wide[bsize];
+  const int plane_bw_unit = AOMMAX(bw_unit >> ss_x, 1);
+  if (row_off > 0) {
+    if (block_size_wide[bsize] > block_size_wide[BLOCK_64X64]) {
+      if (row_off == mi_size_high[BLOCK_64X64] >> ss_y &&
+          col_off + txw == mi_size_wide[BLOCK_64X64] >> ss_x) {
+        return 1;
+      }
+      const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
+      const int col_off_64 = col_off % plane_bw_unit_64;
+      return col_off_64 + txw < plane_bw_unit_64;
+    }
+    return col_off + txw < plane_bw_unit;
+  }
+  if (col_off + txw < plane_bw_unit) return 1;
+  return RefHasTopRightInSb(sb_size, bsize, mi_row, mi_col, partition);
+}
+
+int RefHasBottomLeft(BLOCK_SIZE sb_size, BLOCK_SIZE bsize, int mi_row,
+                     int mi_col, int mi_row_end, PARTITION_TYPE partition,
+                     TX_SIZE txsz, int row_off, int col_off, int ss_x,
+                     int ss_y) {
+  const int txh = tx_size_high_unit[txsz];
+  if (mi_row + ((row_off + txh) << ss_y) >= mi_row_end) return 0;
+  if (block_size_wide[bsize] > block_size_wide[BLOCK_64X64] && col_off > 0) {
+    const int plane_bw_unit_64 = mi_size_wide[BLOCK_64X64] >> ss_x;
+    const int col_off_64 = col_off % plane_bw_unit_64;
+    if (col_off_64 == 0) {
+      const int plane_bh_unit_64 = mi_size_high[BLOCK_64X64] >> ss_y;
+      const int row_off_64 = row_off % plane_bh_unit_64;
+      const int plane_bh_unit =
+          AOMMIN(mi_size_high[bsize] >> ss_y, plane_bh_unit_64);
+      return row_off_64 + txh < plane_bh_unit;
+    }
+  }
+  if (col_off > 0) return 0;
+  const int bh_unit = mi_size_high[bsize];
+  const int plane_bh_unit = AOMMAX(bh_unit >> ss_y, 1);
+  if (row_off + txh < plane_bh_unit) return 1;
+  return RefHasBottomLeftInSb(sb_size, bsize, mi_row, mi_col, partition, txh,
+                              row_off, ss_y);
+}
+
+int OracleFilterStrength(int bs0, int bs1, int delta, int type) {
+  const int d = abs(delta);
+  const int blk_wh = bs0 + bs1;
+  if (type == 0) {
+    if (blk_wh <= 8) return d >= 56;
+    if (blk_wh <= 16) return d >= 40;
+    if (blk_wh <= 24) return (d >= 8) + (d >= 16) + (d >= 32);
+    if (blk_wh <= 32) return (d >= 1) + (d >= 4) + (d >= 32);
+    return (d >= 1) * 3;
+  }
+  if (blk_wh <= 8) return (d >= 40) + (d >= 64);
+  if (blk_wh <= 16) return (d >= 20) + (d >= 48);
+  if (blk_wh <= 24) return (d >= 4) * 3;
+  return (d >= 1) * 3;
+}
+
+void OracleFilterEdge(uint8_t *p, int sz, int strength) {
+  if (!strength) return;
+  static constexpr int kKernel[3][5] = {
+    { 0, 4, 8, 4, 0 },
+    { 0, 5, 6, 5, 0 },
+    { 2, 4, 4, 4, 2 },
+  };
+  uint8_t edge[129];
+  memcpy(edge, p, static_cast<size_t>(sz));
+  for (int i = 1; i < sz; ++i) {
+    int s = 0;
+    for (int j = 0; j < 5; ++j) {
+      int k = i - 2 + j;
+      if (k < 0) k = 0;
+      if (k > sz - 1) k = sz - 1;
+      s += edge[k] * kKernel[strength - 1][j];
+    }
+    p[i] = static_cast<uint8_t>((s + 8) >> 4);
+  }
+}
+
+void OracleUpsampleEdge(uint8_t *p, int sz) {
+  uint8_t in[19];
+  in[0] = p[-1];
+  in[1] = p[-1];
+  for (int i = 0; i < sz; ++i) in[i + 2] = p[i];
+  in[sz + 2] = p[sz - 1];
+  p[-2] = in[0];
+  for (int i = 0; i < sz; ++i) {
+    int s = -in[i] + 9 * in[i + 1] + 9 * in[i + 2] - in[i + 3];
+    s = (s + 8) >> 4;
+    if (s < 0) s = 0;
+    if (s > 255) s = 255;
+    p[2 * i - 1] = static_cast<uint8_t>(s);
+    p[2 * i] = in[i + 2];
+  }
+}
+
+void OracleDr8x8(uint8_t *dst, int dst_stride, const uint8_t *above_ref,
+                 const uint8_t *left_ref, int ref_stride, int p_angle,
+                 int enable_filter, int filter_type, int n_top_px,
+                 int n_topright_px, int n_left_px, int n_bottomleft_px) {
+  uint8_t above_buf[80] = { 0 };
+  uint8_t left_buf[80] = { 0 };
+  uint8_t *above = above_buf + 16;
+  uint8_t *left = left_buf + 16;
+
+  if (n_top_px > 0) {
+    const int n_copy = n_top_px + (n_topright_px > 0 ? n_topright_px : 0);
+    memcpy(above, above_ref, static_cast<size_t>(n_copy));
+    for (int i = n_copy; i < 16; ++i) above[i] = above[n_copy - 1];
+  } else {
+    memset(above, n_left_px > 0 ? left_ref[0] : 127, 16);
+  }
+  if (n_left_px > 0) {
+    const int n_copy = n_left_px + (n_bottomleft_px > 0 ? n_bottomleft_px : 0);
+    for (int i = 0; i < n_copy; ++i) left[i] = left_ref[i * ref_stride];
+    for (int i = n_copy; i < 16; ++i) left[i] = left[n_copy - 1];
+  } else {
+    memset(left, n_top_px > 0 ? above_ref[0] : 129, 16);
+  }
+  uint8_t tl = 128;
+  if (n_top_px > 0 && n_left_px > 0) {
+    tl = above_ref[-1];
+  } else if (n_top_px > 0) {
+    tl = above_ref[0];
+  } else if (n_left_px > 0) {
+    tl = left_ref[0];
+  }
+  above[-1] = tl;
+  left[-1] = tl;
+
+  int upsample_above = 0;
+  int upsample_left = 0;
+  if (enable_filter) {
+    const int need_above = p_angle < 180;
+    const int need_left = p_angle > 90;
+    const int need_right = p_angle < 90;
+    const int need_bottom = p_angle > 180;
+    if (need_above && n_top_px > 0) {
+      const int st = OracleFilterStrength(8, 8, p_angle - 90, filter_type);
+      OracleFilterEdge(above - 1, n_top_px + 1 + (need_right ? 8 : 0), st);
+    }
+    if (need_left && n_left_px > 0) {
+      const int st = OracleFilterStrength(8, 8, p_angle - 180, filter_type);
+      OracleFilterEdge(left - 1, n_left_px + 1 + (need_bottom ? 8 : 0), st);
+    }
+    if (need_above &&
+        av1_use_intra_edge_upsample(8, 8, p_angle - 90, filter_type)) {
+      upsample_above = 1;
+      OracleUpsampleEdge(above, 8 + (need_right ? 8 : 0));
+    }
+    if (need_left &&
+        av1_use_intra_edge_upsample(8, 8, p_angle - 180, filter_type)) {
+      upsample_left = 1;
+      OracleUpsampleEdge(left, 8 + (need_bottom ? 8 : 0));
+    }
+  }
+
+  if (p_angle > 0 && p_angle < 90) {
+    const int dx = dr_intra_derivative[p_angle];
+    const int max_base = 15 << upsample_above;
+    const int frac_bits = 6 - upsample_above;
+    const int base_inc = 1 << upsample_above;
+    for (int r = 0, x = dx; r < 8; ++r, x += dx) {
+      int base = x >> frac_bits;
+      const int shift = ((x << upsample_above) & 0x3F) >> 1;
+      for (int c = 0; c < 8; ++c, base += base_inc) {
+        if (base < max_base) {
+          const int v = above[base] * (32 - shift) + above[base + 1] * shift;
+          dst[r * dst_stride + c] = static_cast<uint8_t>((v + 16) >> 5);
+        } else {
+          dst[r * dst_stride + c] = above[max_base];
+        }
+      }
+    }
+  } else if (p_angle > 180 && p_angle < 270) {
+    const int dy = dr_intra_derivative[270 - p_angle];
+    const int max_base = 15 << upsample_left;
+    const int frac_bits = 6 - upsample_left;
+    const int base_inc = 1 << upsample_left;
+    for (int c = 0, y = dy; c < 8; ++c, y += dy) {
+      int base = y >> frac_bits;
+      const int shift = ((y << upsample_left) & 0x3F) >> 1;
+      for (int r = 0; r < 8; ++r, base += base_inc) {
+        if (base < max_base) {
+          const int v = left[base] * (32 - shift) + left[base + 1] * shift;
+          dst[r * dst_stride + c] = static_cast<uint8_t>((v + 16) >> 5);
+        } else {
+          dst[r * dst_stride + c] = left[max_base];
+        }
+      }
+    }
+  } else if (p_angle > 90 && p_angle < 180) {
+    const int dx = dr_intra_derivative[180 - p_angle];
+    const int dy = dr_intra_derivative[p_angle - 90];
+    const int min_bx = -(1 << upsample_above);
+    const int frac_x = 6 - upsample_above;
+    const int frac_y = 6 - upsample_left;
+    for (int r = 0; r < 8; ++r) {
+      for (int c = 0; c < 8; ++c) {
+        const int x = (c << 6) - (r + 1) * dx;
+        const int bx = x >> frac_x;
+        if (bx >= min_bx) {
+          const int sh = ((x * (1 << upsample_above)) & 0x3F) >> 1;
+          const int v = above[bx] * (32 - sh) + above[bx + 1] * sh;
+          dst[r * dst_stride + c] = static_cast<uint8_t>((v + 16) >> 5);
+        } else {
+          const int y = (r << 6) - (c + 1) * dy;
+          const int by = y >> frac_y;
+          const int sh = ((y * (1 << upsample_left)) & 0x3F) >> 1;
+          const int v = left[by] * (32 - sh) + left[by + 1] * sh;
+          dst[r * dst_stride + c] = static_cast<uint8_t>((v + 16) >> 5);
+        }
+      }
+    }
+  } else {
+    ADD_FAILURE() << "Unexpected directional angle: " << p_angle;
+  }
+}
+
+void OracleFilterIntraLbd(uint8_t *dst, int dst_stride, TX_SIZE tx_size,
+                          FILTER_INTRA_MODE fmode, const uint8_t *above_ref,
+                          const uint8_t *left_ref, int ref_stride, int n_top_px,
+                          int n_left_px) {
+  const int txw = tx_size_wide[tx_size];
+  const int txh = tx_size_high[tx_size];
+  alignas(16) uint8_t above_buf[16 + 64] = {};
+  alignas(16) uint8_t left_buf[16 + 64] = {};
+  uint8_t *above = above_buf + 16;
+  uint8_t *left = left_buf + 16;
+
+  if (n_top_px > 0) {
+    memcpy(above, above_ref, static_cast<size_t>(n_top_px));
+    for (int i = n_top_px; i < txw; ++i) {
+      above[i] = above[n_top_px - 1];
+    }
+  } else {
+    const uint8_t fill = (n_left_px > 0) ? left_ref[0] : 127;
+    memset(above, fill, static_cast<size_t>(txw));
+  }
+
+  if (n_left_px > 0) {
+    for (int i = 0; i < n_left_px; ++i) {
+      left[i] = left_ref[i * ref_stride];
+    }
+    for (int i = n_left_px; i < txh; ++i) {
+      left[i] = left[n_left_px - 1];
+    }
+  } else {
+    const uint8_t fill = (n_top_px > 0) ? above_ref[0] : 129;
+    memset(left, fill, static_cast<size_t>(txh));
+  }
+
+  uint8_t tl = 128;
+  if (n_top_px > 0 && n_left_px > 0) {
+    tl = above_ref[-1];
+  } else if (n_top_px > 0) {
+    tl = above_ref[0];
+  } else if (n_left_px > 0) {
+    tl = left_ref[0];
+  }
+  above[-1] = tl;
+  left[-1] = tl;
+
+  av1_filter_intra_predictor_c(dst, dst_stride, tx_size, above, left,
+                               static_cast<int>(fmode));
+}
+
+void FillPattern(uint8_t *buf, size_t size, int seed) {
+  libaom_test::ACMRandom rnd(seed);
+  for (size_t i = 0; i < size; ++i) {
+    buf[i] = rnd.Rand8();
+  }
+}
+
+TEST(EncodeInternal, IntraPredictAlignedVsUnaligned) {
+  av1_init_intra_predictors();
+
+  static constexpr TX_SIZE kTxSizes[] = {
+    TX_4X4,  TX_4X8, TX_8X8, TX_16X16, TX_32X16,
+#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+    TX_32X8,
+#endif
+  };
+  static constexpr PREDICTION_MODE kModes[] = {
+    V_PRED,    H_PRED,    DC_PRED,     D45_PRED,
+    D135_PRED, D203_PRED, SMOOTH_PRED, PAETH_PRED,
+  };
+  static constexpr int kAlignOffsets[] = { 0, 1, 2, 3, 4, 8, 12 };
+
+  constexpr int kStride = 128;
+  AlignedU8Ptr ref_mem = AllocAlignedU8(128 * 128);
+  AlignedU8Ptr out_aligned = AllocAlignedU8(64 * 128);
+  AlignedU8Ptr aligned_ref = AllocAlignedU8(96 * 128);
+  AlignedU8Ptr out_cur = AllocAlignedU8(64 * 128);
+  AlignedU8Ptr ref_out = AllocAlignedU8(32 * 128);
+  AlignedU8Ptr dst_buf = AllocAlignedU8(64 * 128);
+  ASSERT_NE(ref_mem, nullptr);
+  ASSERT_NE(out_aligned, nullptr);
+  ASSERT_NE(aligned_ref, nullptr);
+  ASSERT_NE(out_cur, nullptr);
+  ASSERT_NE(ref_out, nullptr);
+  ASSERT_NE(dst_buf, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  FillPattern(ref_mem.get(), 128 * 128,
+              libaom_test::ACMRandom::DeterministicSeed());
+
+  for (TX_SIZE tx_size : kTxSizes) {
+    const int txw = tx_size_wide[tx_size];
+    const int txh = tx_size_high[tx_size];
+    for (PREDICTION_MODE mode : kModes) {
+      for (int top_avail = 0; top_avail <= 1; ++top_avail) {
+        for (int left_avail = 0; left_avail <= 1; ++left_avail) {
+          for (int edge_filt = 0; edge_filt <= 1; ++edge_filt) {
+            memset(out_aligned.get(), 0, 64 * 128);
+            for (int align_off : kAlignOffsets) {
+              memset(xd.get(), 0, sizeof(MACROBLOCKD));
+              YV12_BUFFER_CONFIG cur_buf = {};
+              xd->cur_buf = &cur_buf;
+              MB_MODE_INFO mbmi = {};
+              mbmi.bsize = BLOCK_64X64;
+              mbmi.partition = PARTITION_NONE;
+              mbmi.mode = mode;
+              MB_MODE_INFO *mbmi_ptr = &mbmi;
+              xd->mi = &mbmi_ptr;
+              xd->bd = 8;
+              xd->up_available = (top_avail != 0);
+              xd->left_available = (left_avail != 0);
+              xd->tile.mi_row_end = 64;
+              xd->tile.mi_col_end = 64;
+              xd->mb_to_left_edge = -8 * 8;
+              xd->mb_to_top_edge = -8 * 8;
+              xd->mb_to_right_edge = 64 * 8;
+              xd->mb_to_bottom_edge = 64 * 8;
+
+              // Copy reference window so ref - ref_stride has exact alignment
+              // `align_off` mod 16 while holding identical reference pixels.
+              memset(aligned_ref.get(), 0, 96 * 128);
+              const int base_row = 16;
+              const int base_col = 16 + align_off;
+              for (int r = -1; r < 64; ++r) {
+                memcpy(&aligned_ref[(base_row + r) * kStride + base_col - 1],
+                       &ref_mem[(16 + r) * kStride + 16 - 1], 80);
+              }
+              const uint8_t *ref_ptr =
+                  &aligned_ref[base_row * kStride + base_col];
+              memset(out_cur.get(), 0, 64 * 128);
+              av1_predict_intra_block(xd.get(), BLOCK_64X64, edge_filt, 64, 64,
+                                      tx_size, mode, 0, 0, FILTER_INTRA_MODES,
+                                      ref_ptr, kStride, out_cur.get(), kStride,
+                                      0, 0, AOM_PLANE_Y);
+
+              if (align_off == 0) {
+                memcpy(out_aligned.get(), out_cur.get(), 64 * 128);
+                if (mode == V_PRED && top_avail) {
+                  for (int r = 0; r < txh; ++r) {
+                    ASSERT_EQ(memcmp(&out_cur[r * kStride], ref_ptr - kStride,
+                                     static_cast<size_t>(txw)),
+                              0)
+                        << "V_PRED mismatch tx=" << static_cast<int>(tx_size);
+                  }
+                } else if (mode == H_PRED && left_avail) {
+                  for (int r = 0; r < txh; ++r) {
+                    for (int c = 0; c < txw; ++c) {
+                      ASSERT_EQ(out_cur[r * kStride + c],
+                                ref_ptr[r * kStride - 1])
+                          << "H_PRED mismatch tx=" << static_cast<int>(tx_size);
+                    }
+                  }
+                } else if (mode == DC_PRED) {
+                  int sum = 0;
+                  int count = 0;
+                  if (top_avail) {
+                    for (int c = 0; c < txw; ++c) sum += ref_ptr[-kStride + c];
+                    count += txw;
+                  }
+                  if (left_avail) {
+                    for (int r = 0; r < txh; ++r) {
+                      sum += ref_ptr[r * kStride - 1];
+                    }
+                    count += txh;
+                  }
+                  const uint8_t expected =
+                      (count > 0)
+                          ? static_cast<uint8_t>((sum + (count >> 1)) / count)
+                          : 128;
+                  for (int r = 0; r < txh; ++r) {
+                    for (int c = 0; c < txw; ++c) {
+                      ASSERT_EQ(out_cur[r * kStride + c], expected)
+                          << "DC_PRED mismatch tx=" << static_cast<int>(tx_size)
+                          << " top=" << top_avail << " left=" << left_avail;
+                    }
+                  }
+                }
+              } else {
+                for (int r = 0; r < txh; ++r) {
+                  ASSERT_EQ(
+                      memcmp(&out_aligned[r * kStride], &out_cur[r * kStride],
+                             static_cast<size_t>(txw)),
+                      0)
+                      << "align_off=" << align_off
+                      << " tx=" << static_cast<int>(tx_size)
+                      << " mode=" << static_cast<int>(mode)
+                      << " top=" << top_avail << " left=" << left_avail;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  // Also test av1_predict_intra_block_facade across valid transform-grid
+  // blk_col values (multiples of tx_size_wide_unit[tx_size]) so 4-wide tx
+  // exercises +0/+4/+8/+12 mod 16, 8-wide exercises +0/+8 mod 16, and 16-wide
+  // stays 16-byte aligned as required by x86 SSE2 stores.
+  static constexpr TX_SIZE kFacadeTx[] = { TX_4X4, TX_4X8, TX_8X8, TX_16X16 };
+  static constexpr PREDICTION_MODE kFacadeModes[] = { V_PRED, H_PRED, DC_PRED,
+                                                      PAETH_PRED };
+  for (TX_SIZE tx_size : kFacadeTx) {
+    const int txw = tx_size_wide[tx_size];
+    const int txh = tx_size_high[tx_size];
+    const int step_unit = tx_size_wide_unit[tx_size];
+    const int blk_row = tx_size_high_unit[tx_size];
+    for (PREDICTION_MODE mode : kFacadeModes) {
+      memset(ref_out.get(), 0, 32 * 128);
+      for (int blk_col = 0; blk_col < 8; blk_col += step_unit) {
+        memcpy(dst_buf.get(), ref_mem.get(), 64 * kStride);
+
+        memset(cm.get(), 0, sizeof(AV1_COMMON));
+        SequenceHeader seq = {};
+        seq.sb_size = BLOCK_64X64;
+        seq.enable_intra_edge_filter = 1;
+        cm->seq_params = &seq;
+
+        memset(xd.get(), 0, sizeof(MACROBLOCKD));
+        YV12_BUFFER_CONFIG cur_buf = {};
+        xd->cur_buf = &cur_buf;
+        MB_MODE_INFO mbmi = {};
+        mbmi.bsize = BLOCK_32X32;
+        mbmi.partition = PARTITION_NONE;
+        mbmi.mode = mode;
+        mbmi.filter_intra_mode_info.filter_intra_mode = FILTER_INTRA_MODES;
+        MB_MODE_INFO *mbmi_ptr = &mbmi;
+        xd->mi = &mbmi_ptr;
+        xd->bd = 8;
+        xd->up_available = true;
+        xd->left_available = true;
+        xd->tile.mi_row_end = 64;
+        xd->tile.mi_col_end = 64;
+        xd->mb_to_left_edge = -8 * 8;
+        xd->mb_to_top_edge = -8 * 8;
+        xd->mb_to_right_edge = 128 * 8;
+        xd->mb_to_bottom_edge = 128 * 8;
+        xd->plane[0].dst.buf = &dst_buf[16 * kStride + 16];
+        xd->plane[0].dst.stride = kStride;
+        xd->plane[0].width = 32;
+        xd->plane[0].height = 32;
+
+        // Normalize reference pixels for this (blk_col, blk_row) so every
+        // blk_col sees identical above/left boundary samples.
+        uint8_t *blk_dst =
+            &xd->plane[0]
+                 .dst.buf[(blk_row * kStride + blk_col) << MI_SIZE_LOG2];
+        memcpy(blk_dst - kStride - 1, &ref_mem[15 * kStride + 15], 40);
+        for (int r = 0; r < txh; ++r) {
+          blk_dst[r * kStride - 1] = ref_mem[(16 + r) * kStride + 15];
+        }
+
+        av1_predict_intra_block_facade(cm.get(), xd.get(), AOM_PLANE_Y, blk_col,
+                                       blk_row, tx_size);
+        if (blk_col == 0) {
+          for (int r = 0; r < txh; ++r) {
+            memcpy(&ref_out[r * kStride], &blk_dst[r * kStride],
+                   static_cast<size_t>(txw));
+          }
+        } else {
+          for (int r = 0; r < txh; ++r) {
+            ASSERT_EQ(memcmp(&ref_out[r * kStride], &blk_dst[r * kStride],
+                             static_cast<size_t>(txw)),
+                      0)
+                << "facade blk_col=" << blk_col
+                << " tx=" << static_cast<int>(tx_size)
+                << " mode=" << static_cast<int>(mode);
+          }
+        }
+      }
+    }
+  }
+}
+
+TEST(EncodeInternal, IntraPredictFacadeTopRightBottomLeft) {
+  av1_init_intra_predictors();
+
+  struct Case {
+    BLOCK_SIZE sb_size;
+    BLOCK_SIZE bsize;
+    PARTITION_TYPE partition;
+    TX_SIZE tx_size;
+    int plane;
+    int ss_x;
+    int ss_y;
+    int mi_row;
+    int mi_col;
+    int row_off;
+    int col_off;
+    int mi_row_end;
+    int mi_col_end;
+  };
+
+  // Fields: { sb_size, bsize, partition, tx_size, plane, ss_x, ss_y, mi_row,
+  //           mi_col, row_off, col_off, mi_row_end, mi_col_end }
+  static constexpr Case kCases[] = {
+    // PARTITION_VERT_A / VERT_B square BL blocks must report has_top_right == 0
+    // (catches broken `(rm & bh_unit)` in has_top_right_in_sb).
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_VERT_A, TX_8X8, 0, 0, 0, 2, 0, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_VERT_B, TX_8X8, 0, 0, 0, 2, 4, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_VERT_A, TX_16X16, 0, 0, 0, 4, 0, 0, 0,
+      64, 64 },
+    { BLOCK_128X128, BLOCK_32X32, PARTITION_VERT_B, TX_32X32, 0, 0, 0, 8, 0, 0,
+      0, 64, 64 },
+    // PARTITION_VERT_A / VERT_B square TR blocks must report
+    // has_bottom_left == 1 (catches broken `!(rm & bh_unit) && (cm &
+    // bw_unit)`).
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_VERT_A, TX_8X8, 0, 0, 0, 0, 2, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_VERT_B, TX_16X16, 0, 0, 0, 0, 4, 0, 0,
+      64, 64 },
+    // Standard Z-order contrast: PARTITION_NONE at same coords flips TR/BL.
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 2, 0, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 0, 2, 0, 0, 64,
+      64 },
+    // General Z-order formula case where has_bottom_left_in_sb returns 1.
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 0, 4, 0, 0, 64,
+      64 },
+    // SB top row & rightmost column boundaries.
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 0, 14, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 2, 14, 0, 0, 64,
+      64 },
+    // Leftmost SB column BL vs bottommost SB row.
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_NONE, TX_16X16, 0, 0, 0, 12, 0, 0, 0,
+      64, 64 },
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_NONE, TX_16X16, 0, 0, 0, 12, 4, 0, 0,
+      64, 64 },
+    // Tile boundaries.
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 0, 6, 0, 0, 64,
+      8 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_NONE, TX_8X8, 0, 0, 0, 6, 0, 0, 0, 8,
+      64 },
+    // BLOCK_128X128 special center case & 64x* BL col_off_64 == 0.
+    { BLOCK_128X128, BLOCK_128X128, PARTITION_NONE, TX_16X16, 0, 0, 0, 0, 0, 16,
+      12, 64, 64 },
+    { BLOCK_128X128, BLOCK_128X128, PARTITION_NONE, TX_16X16, 0, 0, 0, 0, 0, 12,
+      16, 64, 64 },
+    { BLOCK_128X128, BLOCK_128X128, PARTITION_NONE, TX_16X16, 0, 0, 0, 0, 0, 16,
+      16, 64, 64 },
+    // 4:2:0 chroma subsampled offsets & 4:4:4 chroma origin fast path.
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_VERT_A, TX_8X8, 1, 1, 1, 4, 0, 0, 0,
+      64, 64 },
+    { BLOCK_64X64, BLOCK_16X16, PARTITION_VERT_B, TX_8X8, 1, 1, 1, 0, 4, 0, 0,
+      64, 64 },
+    { BLOCK_64X64, BLOCK_32X32, PARTITION_NONE, TX_8X8, 1, 1, 1, 0, 0, 2, 2, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_VERT_A, TX_8X8, 1, 0, 0, 2, 0, 0, 0, 64,
+      64 },
+    { BLOCK_64X64, BLOCK_8X8, PARTITION_VERT_A, TX_8X8, 1, 0, 0, 0, 2, 0, 0, 64,
+      64 },
+  };
+
+  constexpr int kStride = 256;
+  AlignedU8Ptr buf = AllocAlignedU8(256 * 256);
+  ASSERT_NE(buf, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  for (int idx = 0; idx < NELEMENTS(kCases); ++idx) {
+    const Case &tc = kCases[idx];
+    const int txw = tx_size_wide[tc.tx_size];
+    const int txh = tx_size_high[tc.tx_size];
+    const int x = tc.col_off << MI_SIZE_LOG2;
+    const int y = tc.row_off << MI_SIZE_LOG2;
+
+    for (int test_tr = 0; test_tr <= 1; ++test_tr) {
+      memset(buf.get(), 10, 256 * 256);
+      memset(cm.get(), 0, sizeof(AV1_COMMON));
+      SequenceHeader seq = {};
+      seq.sb_size = tc.sb_size;
+      seq.enable_intra_edge_filter = 0;
+      cm->seq_params = &seq;
+
+      memset(xd.get(), 0, sizeof(MACROBLOCKD));
+      YV12_BUFFER_CONFIG cur_buf = {};
+      xd->cur_buf = &cur_buf;
+      MB_MODE_INFO mbmi = {};
+      mbmi.bsize = tc.bsize;
+      mbmi.partition = tc.partition;
+      mbmi.mode = test_tr ? D45_PRED : D203_PRED;
+      mbmi.uv_mode = test_tr ? UV_D45_PRED : UV_D203_PRED;
+      mbmi.filter_intra_mode_info.filter_intra_mode = FILTER_INTRA_MODES;
+      MB_MODE_INFO *mbmi_ptr = &mbmi;
+      xd->mi = &mbmi_ptr;
+      xd->bd = 8;
+      xd->up_available = true;
+      xd->left_available = true;
+      xd->chroma_up_available = true;
+      xd->chroma_left_available = true;
+      xd->tile.mi_row_end = tc.mi_row_end;
+      xd->tile.mi_col_end = tc.mi_col_end;
+      xd->mb_to_left_edge = -(tc.mi_col * MI_SIZE * 8);
+      xd->mb_to_top_edge = -(tc.mi_row * MI_SIZE * 8);
+      xd->mb_to_right_edge = 512 * 8;
+      xd->mb_to_bottom_edge = 512 * 8;
+      xd->plane[tc.plane].subsampling_x = tc.ss_x;
+      xd->plane[tc.plane].subsampling_y = tc.ss_y;
+      xd->plane[tc.plane].width = 128;
+      xd->plane[tc.plane].height = 128;
+      xd->plane[tc.plane].dst.stride = kStride;
+      xd->plane[tc.plane].dst.buf = buf.get() + 16 * kStride + 16;
+
+      uint8_t *blk_dst = xd->plane[tc.plane].dst.buf + y * kStride + x;
+      if (test_tr) {
+        memset(blk_dst - kStride + txw, 200, static_cast<size_t>(txw));
+        av1_predict_intra_block_facade(cm.get(), xd.get(), tc.plane, tc.col_off,
+                                       tc.row_off, tc.tx_size);
+        const int expected_tr = RefHasTopRight(
+            tc.sb_size, tc.bsize, tc.mi_row, tc.mi_col, tc.mi_col_end,
+            tc.partition, tc.tx_size, tc.row_off, tc.col_off, tc.ss_x, tc.ss_y);
+        EXPECT_EQ(blk_dst[txw - 1], expected_tr ? 200 : 10)
+            << "TR mismatch at case " << idx;
+        if (idx == 3) {
+          EXPECT_EQ(expected_tr, 0);
+          EXPECT_EQ(RefHasTopRight(tc.sb_size, tc.bsize, tc.mi_row, tc.mi_col,
+                                   tc.mi_col_end, PARTITION_NONE, tc.tx_size,
+                                   tc.row_off, tc.col_off, tc.ss_x, tc.ss_y),
+                    1);
+        }
+      } else {
+        for (int r = txh; r < 2 * txh; ++r) {
+          blk_dst[r * kStride - 1] = 200;
+        }
+        av1_predict_intra_block_facade(cm.get(), xd.get(), tc.plane, tc.col_off,
+                                       tc.row_off, tc.tx_size);
+        const int expected_bl = RefHasBottomLeft(
+            tc.sb_size, tc.bsize, tc.mi_row, tc.mi_col, tc.mi_row_end,
+            tc.partition, tc.tx_size, tc.row_off, tc.col_off, tc.ss_x, tc.ss_y);
+        EXPECT_EQ(blk_dst[(txh - 1) * kStride + (txw - 1)],
+                  expected_bl ? 200 : 10)
+            << "BL mismatch at case " << idx;
+        if (idx == 8) {
+          EXPECT_EQ(expected_bl, 1);
+        }
+      }
+    }
+  }
+}
+
+TEST(EncodeInternal, IntraPredict8x8ZonesAndEdgeFilter) {
+  av1_init_intra_predictors();
+
+  constexpr int kStride = 128;
+  AlignedU8Ptr ref_mem = AllocAlignedU8(64 * 128);
+  AlignedU8Ptr dst_buf = AllocAlignedU8(64 * 128);
+  ASSERT_NE(ref_mem, nullptr);
+  ASSERT_NE(dst_buf, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  FillPattern(ref_mem.get(), 64 * 128,
+              libaom_test::ACMRandom::DeterministicSeed() + 1);
+
+  struct ZoneCase {
+    PREDICTION_MODE mode;
+    int angle_delta;
+  };
+  static constexpr ZoneCase kZoneCases[] = {
+    { D45_PRED, 0 },   { D45_PRED, -3 }, { D67_PRED, 0 },  { D67_PRED, 3 },
+    { D113_PRED, -2 }, { D135_PRED, 0 }, { D157_PRED, 2 }, { D203_PRED, -3 },
+    { D203_PRED, 0 },  { D203_PRED, 3 },
+  };
+
+  for (const ZoneCase &zc : kZoneCases) {
+    const int p_angle =
+        mode_to_angle_map[zc.mode] + zc.angle_delta * ANGLE_STEP;
+    const int avail_cfgs = (p_angle < 90 || p_angle > 180) ? 2 : 1;
+    for (int edge_filt = 0; edge_filt <= 1; ++edge_filt) {
+      for (int smooth_cfg = 0; smooth_cfg <= 2; ++smooth_cfg) {
+        for (int tr_bl = 0; tr_bl <= 2; ++tr_bl) {
+          const int avail_ext = (tr_bl == 0) ? 0 : (tr_bl == 1 ? 4 : 8);
+          for (int avail_idx = 0; avail_idx < avail_cfgs; ++avail_idx) {
+            const int up_avail = (avail_idx == 1 && p_angle > 180) ? 0 : 1;
+            const int left_avail = (avail_idx == 1 && p_angle < 90) ? 0 : 1;
+
+            memcpy(dst_buf.get(), ref_mem.get(), 64 * 128);
+
+            memset(cm.get(), 0, sizeof(AV1_COMMON));
+            SequenceHeader seq = {};
+            seq.sb_size = BLOCK_64X64;
+            seq.enable_intra_edge_filter = static_cast<uint8_t>(edge_filt);
+            cm->seq_params = &seq;
+
+            MB_MODE_INFO above_mbmi = {};
+            above_mbmi.mode = (smooth_cfg == 1) ? SMOOTH_PRED : DC_PRED;
+            MB_MODE_INFO left_mbmi = {};
+            left_mbmi.mode = (smooth_cfg == 2) ? SMOOTH_V_PRED : V_PRED;
+
+            memset(xd.get(), 0, sizeof(MACROBLOCKD));
+            YV12_BUFFER_CONFIG cur_buf = {};
+            xd->cur_buf = &cur_buf;
+            MB_MODE_INFO mbmi = {};
+            mbmi.bsize = BLOCK_8X8;
+            mbmi.partition = PARTITION_NONE;
+            mbmi.mode = zc.mode;
+            mbmi.angle_delta[0] = static_cast<int8_t>(zc.angle_delta);
+            mbmi.filter_intra_mode_info.filter_intra_mode = FILTER_INTRA_MODES;
+            MB_MODE_INFO *mbmi_ptr = &mbmi;
+            xd->mi = &mbmi_ptr;
+            xd->above_mbmi = up_avail ? &above_mbmi : nullptr;
+            xd->left_mbmi = left_avail ? &left_mbmi : nullptr;
+            xd->bd = 8;
+            xd->up_available = (up_avail != 0);
+            xd->left_available = (left_avail != 0);
+            xd->tile.mi_row_end = 64;
+            xd->tile.mi_col_end = 64;
+            xd->mb_to_left_edge = -16 * MI_SIZE * 8;
+            xd->mb_to_top_edge = -4 * MI_SIZE * 8;
+            xd->mb_to_right_edge = avail_ext * 8;
+            xd->mb_to_bottom_edge = avail_ext * 8;
+            xd->plane[0].width = 8;
+            xd->plane[0].height = 8;
+            xd->plane[0].dst.stride = kStride;
+            xd->plane[0].dst.buf = dst_buf.get() + 16 * kStride + 16;
+
+            const int filter_type = ((up_avail && smooth_cfg == 1) ||
+                                     (left_avail && smooth_cfg == 2))
+                                        ? 1
+                                        : 0;
+            const uint8_t *orig_ref = &ref_mem[16 * kStride + 16];
+            uint8_t oracle[8 * 8] = {};
+            OracleDr8x8(oracle, 8, orig_ref - kStride, orig_ref - 1, kStride,
+                        p_angle, edge_filt, filter_type, up_avail ? 8 : 0,
+                        (up_avail && p_angle < 90) ? avail_ext : -1,
+                        left_avail ? 8 : 0,
+                        (left_avail && p_angle > 180) ? avail_ext : -1);
+
+            av1_predict_intra_block_facade(cm.get(), xd.get(), AOM_PLANE_Y, 0,
+                                           0, TX_8X8);
+            uint8_t *blk_dst = xd->plane[0].dst.buf;
+            for (int r = 0; r < 8; ++r) {
+              ASSERT_EQ(memcmp(&blk_dst[r * kStride], &oracle[r * 8], 8), 0)
+                  << "mode=" << static_cast<int>(zc.mode)
+                  << " delta=" << zc.angle_delta << " filt=" << edge_filt
+                  << " smooth_cfg=" << smooth_cfg << " ext=" << avail_ext
+                  << " up=" << up_avail << " left=" << left_avail;
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+TEST(EncodeInternal, IntraPredictFilterIntraInteriorAndBorder) {
+  av1_init_intra_predictors();
+
+  constexpr int kStride = 128;
+  AlignedU8Ptr ref_mem = AllocAlignedU8(128 * 128);
+  AlignedU8Ptr facade_buf = AllocAlignedU8(128 * 128);
+  AlignedU8Ptr direct_out = AllocAlignedU8(64 * 128);
+  AlignedU8Ptr oracle_out = AllocAlignedU8(64 * 128);
+  ASSERT_NE(ref_mem, nullptr);
+  ASSERT_NE(facade_buf, nullptr);
+  ASSERT_NE(direct_out, nullptr);
+  ASSERT_NE(oracle_out, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  FillPattern(ref_mem.get(), 128 * 128,
+              libaom_test::ACMRandom::DeterministicSeed() + 2);
+
+  static constexpr TX_SIZE kFilterTx[] = {
+    TX_4X4,  TX_8X8, TX_16X16, TX_32X32, TX_4X8, TX_8X16,
+#if !CONFIG_REALTIME_ONLY || CONFIG_AV1_DECODER
+    TX_32X8,
+#endif
+  };
+
+  for (TX_SIZE tx_size : kFilterTx) {
+    const int txw = tx_size_wide[tx_size];
+    const int txh = tx_size_high[tx_size];
+    for (int fmode = 0; fmode < FILTER_INTRA_MODES; ++fmode) {
+      const FILTER_INTRA_MODE fi_mode = static_cast<FILTER_INTRA_MODE>(fmode);
+      for (int cfg = 0; cfg < 5; ++cfg) {
+        // cfg 0: interior; 1: no above; 2: no left;
+        // 3: partial right/bottom; 4: neither above nor left available.
+        const int top_avail = (cfg != 1 && cfg != 4);
+        const int left_avail = (cfg != 2 && cfg != 4);
+        const int wpx = (cfg == 3) ? (txw - 2) : 64;
+        const int hpx = (cfg == 3) ? (txh - 2) : 64;
+        const int n_top_px = top_avail ? (cfg == 3 ? txw - 2 : txw) : 0;
+        const int n_left_px = left_avail ? (cfg == 3 ? txh - 2 : txh) : 0;
+
+        memcpy(facade_buf.get(), ref_mem.get(), 128 * 128);
+
+        memset(cm.get(), 0, sizeof(AV1_COMMON));
+        SequenceHeader seq = {};
+        seq.sb_size = BLOCK_64X64;
+        seq.enable_intra_edge_filter = 1;
+        cm->seq_params = &seq;
+
+        memset(xd.get(), 0, sizeof(MACROBLOCKD));
+        YV12_BUFFER_CONFIG cur_buf = {};
+        xd->cur_buf = &cur_buf;
+        MB_MODE_INFO mbmi = {};
+        mbmi.bsize = BLOCK_32X32;
+        mbmi.partition = PARTITION_NONE;
+        mbmi.mode = DC_PRED;
+        mbmi.filter_intra_mode_info.use_filter_intra = 1;
+        mbmi.filter_intra_mode_info.filter_intra_mode = fi_mode;
+        MB_MODE_INFO *mbmi_ptr = &mbmi;
+        xd->mi = &mbmi_ptr;
+        xd->bd = 8;
+        xd->up_available = (top_avail != 0);
+        xd->left_available = (left_avail != 0);
+        xd->tile.mi_row_end = 64;
+        xd->tile.mi_col_end = 64;
+        xd->mb_to_left_edge = -8 * 8;
+        xd->mb_to_top_edge = -8 * 8;
+        xd->mb_to_right_edge = 0;
+        xd->mb_to_bottom_edge = 0;
+        xd->plane[0].width = wpx;
+        xd->plane[0].height = hpx;
+        xd->plane[0].dst.stride = kStride;
+        xd->plane[0].dst.buf = facade_buf.get() + 16 * kStride + 16;
+
+        memset(oracle_out.get(), 0, 64 * 128);
+        OracleFilterIntraLbd(oracle_out.get(), kStride, tx_size, fi_mode,
+                             &ref_mem[15 * kStride + 16],
+                             &ref_mem[16 * kStride + 15], kStride, n_top_px,
+                             n_left_px);
+
+        av1_predict_intra_block_facade(cm.get(), xd.get(), AOM_PLANE_Y, 0, 0,
+                                       tx_size);
+
+        memset(direct_out.get(), 0, 64 * 128);
+        av1_predict_intra_block(xd.get(), BLOCK_64X64, 0, wpx, hpx, tx_size,
+                                DC_PRED, 0, 0, fi_mode,
+                                &ref_mem[16 * kStride + 16], kStride,
+                                direct_out.get(), kStride, 0, 0, AOM_PLANE_Y);
+
+        for (int r = 0; r < txh; ++r) {
+          ASSERT_EQ(memcmp(&xd->plane[0].dst.buf[r * kStride],
+                           &oracle_out[r * kStride], static_cast<size_t>(txw)),
+                    0)
+              << "filter_intra facade vs oracle tx="
+              << static_cast<int>(tx_size) << " fmode=" << fmode
+              << " cfg=" << cfg;
+          ASSERT_EQ(memcmp(&direct_out[r * kStride], &oracle_out[r * kStride],
+                           static_cast<size_t>(txw)),
+                    0)
+              << "filter_intra direct vs oracle tx="
+              << static_cast<int>(tx_size) << " fmode=" << fmode
+              << " cfg=" << cfg;
+        }
+      }
+    }
+  }
+}
+
+TEST(EncodeInternal, IntraPredictPartialBorderTx64) {
+  av1_init_intra_predictors();
+
+  constexpr int kStride = 256;
+  constexpr int kRows = 160;
+  AlignedU8Ptr ref_mem = AllocAlignedU8(kRows * kStride);
+  AlignedU8Ptr facade_buf = AllocAlignedU8(kRows * kStride);
+  AlignedU8Ptr direct_out = AllocAlignedU8(64 * kStride);
+  ASSERT_NE(ref_mem, nullptr);
+  ASSERT_NE(facade_buf, nullptr);
+  ASSERT_NE(direct_out, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  for (int i = 0; i < kRows * kStride; ++i) {
+    ref_mem[i] = static_cast<uint8_t>((i * 53 + 17) & 0xff);
+  }
+
+  static constexpr int kWpx[] = { 8, 40, 56, 80, 96 };
+  static constexpr int kHpx[] = { 16, 48, 80, 96 };
+  static constexpr PREDICTION_MODE kModes[] = { DC_PRED, D45_PRED, D203_PRED };
+
+  for (int wpx : kWpx) {
+    for (int hpx : kHpx) {
+      for (PREDICTION_MODE mode : kModes) {
+        for (int edge_filt = 0; edge_filt <= 1; ++edge_filt) {
+          memcpy(facade_buf.get(), ref_mem.get(), kRows * kStride);
+          memset(direct_out.get(), 0, 64 * kStride);
+
+          memset(cm.get(), 0, sizeof(AV1_COMMON));
+          SequenceHeader seq = {};
+          seq.sb_size = BLOCK_64X64;
+          seq.enable_intra_edge_filter = static_cast<uint8_t>(edge_filt);
+          cm->seq_params = &seq;
+
+          memset(xd.get(), 0, sizeof(MACROBLOCKD));
+          YV12_BUFFER_CONFIG cur_buf = {};
+          xd->cur_buf = &cur_buf;
+          MB_MODE_INFO mbmi = {};
+          mbmi.bsize = BLOCK_64X64;
+          mbmi.partition = PARTITION_NONE;
+          mbmi.mode = mode;
+          mbmi.filter_intra_mode_info.filter_intra_mode = FILTER_INTRA_MODES;
+          MB_MODE_INFO *mbmi_ptr = &mbmi;
+          xd->mi = &mbmi_ptr;
+          xd->bd = 8;
+          xd->up_available = true;
+          xd->left_available = true;
+          xd->tile.mi_row_end = 64;
+          xd->tile.mi_col_end = 64;
+          xd->mb_to_left_edge = -8 * 8;
+          xd->mb_to_top_edge = -8 * 8;
+          xd->mb_to_right_edge = 0;
+          xd->mb_to_bottom_edge = 0;
+          xd->plane[0].width = wpx;
+          xd->plane[0].height = hpx;
+          xd->plane[0].dst.stride = kStride;
+          xd->plane[0].dst.buf = facade_buf.get() + 16 * kStride + 16;
+
+          av1_predict_intra_block_facade(cm.get(), xd.get(), AOM_PLANE_Y, 0, 0,
+                                         TX_64X64);
+
+          const uint8_t *ref = &ref_mem[16 * kStride + 16];
+          av1_predict_intra_block(xd.get(), BLOCK_64X64, edge_filt, wpx, hpx,
+                                  TX_64X64, mode, 0, 0, FILTER_INTRA_MODES, ref,
+                                  kStride, direct_out.get(), kStride, 0, 0,
+                                  AOM_PLANE_Y);
+
+          for (int r = 0; r < 64; ++r) {
+            ASSERT_EQ(memcmp(&xd->plane[0].dst.buf[r * kStride],
+                             &direct_out[r * kStride], 64),
+                      0)
+                << "TX_64X64 border facade vs direct wpx=" << wpx
+                << " hpx=" << hpx << " mode=" << static_cast<int>(mode)
+                << " filt=" << edge_filt << " r=" << r;
+          }
+
+          if (mode == DC_PRED) {
+            const int n_top = AOMMIN(wpx, 64);
+            const int n_left = AOMMIN(hpx, 64);
+            int sum = 0;
+            for (int c = 0; c < n_top; ++c) sum += ref[-kStride + c];
+            sum += (64 - n_top) * ref[-kStride + n_top - 1];
+            for (int r = 0; r < n_left; ++r) sum += ref[r * kStride - 1];
+            sum += (64 - n_left) * ref[(n_left - 1) * kStride - 1];
+            const uint8_t expected_dc = static_cast<uint8_t>((sum + 64) >> 7);
+            for (int r = 0; r < 64; ++r) {
+              for (int c = 0; c < 64; ++c) {
+                ASSERT_EQ(xd->plane[0].dst.buf[r * kStride + c], expected_dc)
+                    << "TX_64X64 border DC_PRED wpx=" << wpx << " hpx=" << hpx;
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+#if CONFIG_AV1_HIGHBITDEPTH
+TEST(EncodeInternal, IntraPredictHbdFacade) {
+  av1_init_intra_predictors();
+
+  constexpr int kStride = 128;
+  AlignedU16Ptr buf = AllocAlignedU16(64 * kStride);
+  AlignedU16Ptr facade_buf = AllocAlignedU16(64 * kStride);
+  AlignedU16Ptr direct_out = AllocAlignedU16(16 * kStride);
+  ASSERT_NE(buf, nullptr);
+  ASSERT_NE(facade_buf, nullptr);
+  ASSERT_NE(direct_out, nullptr);
+  std::unique_ptr<AV1_COMMON> cm(new AV1_COMMON());
+  std::unique_ptr<MACROBLOCKD> xd(new MACROBLOCKD());
+  for (int i = 0; i < 64 * kStride; ++i) {
+    buf[i] = static_cast<uint16_t>((i * 37) & 0x3ff);
+  }
+
+  static constexpr PREDICTION_MODE kHbdModes[] = {
+    DC_PRED, V_PRED, H_PRED, D45_PRED, D135_PRED, D203_PRED, SMOOTH_PRED,
+  };
+  static constexpr int kHbdCoords[][2] = { { 0, 0 }, { 4, 4 } };
+
+  for (PREDICTION_MODE mode : kHbdModes) {
+    const int max_delta_idx = av1_is_directional_mode(mode) ? 1 : 0;
+    for (int delta_idx = 0; delta_idx <= max_delta_idx; ++delta_idx) {
+      const int angle_delta = (delta_idx == 0) ? 0 : 2;
+      for (const auto &coord : kHbdCoords) {
+        const int blk_col = coord[0];
+        const int blk_row = coord[1];
+        for (int edge_filt = 0; edge_filt <= 1; ++edge_filt) {
+          memcpy(facade_buf.get(), buf.get(), 64 * kStride * sizeof(uint16_t));
+          memset(direct_out.get(), 0, 16 * kStride * sizeof(uint16_t));
+
+          memset(cm.get(), 0, sizeof(AV1_COMMON));
+          SequenceHeader seq = {};
+          seq.sb_size = BLOCK_64X64;
+          seq.enable_intra_edge_filter = static_cast<uint8_t>(edge_filt);
+          cm->seq_params = &seq;
+
+          memset(xd.get(), 0, sizeof(MACROBLOCKD));
+          YV12_BUFFER_CONFIG cur_buf = {};
+          cur_buf.flags = YV12_FLAG_HIGHBITDEPTH;
+          xd->cur_buf = &cur_buf;
+          MB_MODE_INFO mbmi = {};
+          mbmi.bsize = BLOCK_64X64;
+          mbmi.partition = PARTITION_NONE;
+          mbmi.mode = mode;
+          mbmi.angle_delta[0] = static_cast<int8_t>(angle_delta);
+          mbmi.filter_intra_mode_info.filter_intra_mode = FILTER_INTRA_MODES;
+          MB_MODE_INFO *mbmi_ptr = &mbmi;
+          xd->mi = &mbmi_ptr;
+          xd->bd = 10;
+          xd->up_available = true;
+          xd->left_available = true;
+          xd->tile.mi_row_end = 64;
+          xd->tile.mi_col_end = 64;
+          xd->mb_to_left_edge = -8 * 8;
+          xd->mb_to_top_edge = -8 * 8;
+          xd->mb_to_right_edge = 64 * 8;
+          xd->mb_to_bottom_edge = 64 * 8;
+          xd->plane[0].width = 64;
+          xd->plane[0].height = 64;
+          xd->plane[0].dst.stride = kStride;
+          xd->plane[0].dst.buf =
+              CONVERT_TO_BYTEPTR(facade_buf.get() + 16 * kStride + 16);
+
+          av1_predict_intra_block_facade(cm.get(), xd.get(), AOM_PLANE_Y,
+                                         blk_col, blk_row, TX_16X16);
+
+          const uint16_t *ref16 = buf.get() +
+                                  (16 + (blk_row << MI_SIZE_LOG2)) * kStride +
+                                  16 + (blk_col << MI_SIZE_LOG2);
+          av1_predict_intra_block(xd.get(), BLOCK_64X64, edge_filt, 64, 64,
+                                  TX_16X16, mode, angle_delta * ANGLE_STEP, 0,
+                                  FILTER_INTRA_MODES, CONVERT_TO_BYTEPTR(ref16),
+                                  kStride, CONVERT_TO_BYTEPTR(direct_out.get()),
+                                  kStride, blk_col, blk_row, AOM_PLANE_Y);
+
+          const uint16_t *f_ptr =
+              CONVERT_TO_SHORTPTR(xd->plane[0].dst.buf) +
+              ((blk_row * kStride + blk_col) << MI_SIZE_LOG2);
+          for (int r = 0; r < 16; ++r) {
+            ASSERT_EQ(memcmp(&f_ptr[r * kStride], &direct_out[r * kStride],
+                             16 * sizeof(uint16_t)),
+                      0)
+                << "HBD facade vs direct mode=" << static_cast<int>(mode)
+                << " delta=" << angle_delta << " col=" << blk_col
+                << " row=" << blk_row << " filt=" << edge_filt;
+          }
+
+          if (angle_delta == 0) {
+            if (mode == V_PRED) {
+              for (int r = 0; r < 16; ++r) {
+                ASSERT_EQ(memcmp(&f_ptr[r * kStride], ref16 - kStride,
+                                 16 * sizeof(uint16_t)),
+                          0)
+                    << "HBD V_PRED col=" << blk_col << " row=" << blk_row;
+              }
+            } else if (mode == H_PRED) {
+              for (int r = 0; r < 16; ++r) {
+                for (int c = 0; c < 16; ++c) {
+                  ASSERT_EQ(f_ptr[r * kStride + c], ref16[r * kStride - 1])
+                      << "HBD H_PRED col=" << blk_col << " row=" << blk_row;
+                }
+              }
+            } else if (mode == DC_PRED) {
+              int sum = 0;
+              for (int c = 0; c < 16; ++c) sum += ref16[-kStride + c];
+              for (int r = 0; r < 16; ++r) sum += ref16[r * kStride - 1];
+              const uint16_t expected_dc =
+                  static_cast<uint16_t>((sum + 16) >> 5);
+              for (int r = 0; r < 16; ++r) {
+                for (int c = 0; c < 16; ++c) {
+                  ASSERT_EQ(f_ptr[r * kStride + c], expected_dc)
+                      << "HBD DC_PRED col=" << blk_col << " row=" << blk_row;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+
 }  // namespace