Commit 702df7edcc for aom

commit 702df7edccc52550cdd758e3cb38044d05c7f850
Author: Frank Galligan <fgalligan@google.com>
Date:   Fri Nov 13 10:44:01 2026 -0800

    x86,intra: Add SSE2 direct LBD DC pred and test

    Specialize 8-bit direct DC intra prediction for SSE2 using
    _mm_sad_epu8 and local SSE2 block-fill helpers, register
    av1_predict_dc_block_lbd in av1_rtcd_defs.pl, and add
    SSE2.LowbdPredictDcBlockMatchesC and RTCD parity tests.

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

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

diff --git a/av1/av1.cmake b/av1/av1.cmake
index eb58e57553..dca5c005da 100644
--- a/av1/av1.cmake
+++ b/av1/av1.cmake
@@ -284,6 +284,7 @@ list(APPEND AOM_AV1_COMMON_INTRIN_SSE2
             "${AOM_ROOT}/av1/common/x86/convolve_2d_sse2.c"
             "${AOM_ROOT}/av1/common/x86/convolve_sse2.c"
             "${AOM_ROOT}/av1/common/x86/jnt_convolve_sse2.c"
+            "${AOM_ROOT}/av1/common/x86/reconintra_sse2.c"
             "${AOM_ROOT}/av1/common/x86/resize_sse2.c"
             "${AOM_ROOT}/av1/common/x86/wiener_convolve_sse2.c")

diff --git a/av1/common/av1_rtcd_defs.pl b/av1/common/av1_rtcd_defs.pl
index 59f37d1b94..9f7eee8c04 100644
--- a/av1/common/av1_rtcd_defs.pl
+++ b/av1/common/av1_rtcd_defs.pl
@@ -722,6 +722,8 @@ add_proto qw/void av1_filter_intra_edge/, "uint8_t *p, int sz, int strength";
 specialize qw/av1_filter_intra_edge sse4_1 neon/;
 add_proto qw/void av1_upsample_intra_edge/, "uint8_t *p, int sz";
 specialize qw/av1_upsample_intra_edge sse4_1 neon/;
+add_proto qw/void av1_predict_dc_block_lbd/, "const uint8_t *ref, int ref_stride, uint8_t *dst, int dst_stride, TX_SIZE tx_size, int txwpx, int txhpx, int n_top_px, int n_left_px";
+specialize qw/av1_predict_dc_block_lbd sse2/;

 if (aom_config("CONFIG_AV1_HIGHBITDEPTH") eq "yes") {
   add_proto qw/void av1_highbd_filter_intra_edge/, "uint16_t *p, int sz, int strength";
diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index 5008db6eca..c4d9fcf44e 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -1300,7 +1300,6 @@ static void build_directional_and_filter_intra_predictors(
                upsample_left, p_angle);
 }

-#if HAVE_SSE2
 static AOM_FORCE_INLINE uint32_t sum_above_row_lbd(const uint8_t *above_ref,
                                                    int txwpx) {
   uint32_t sum = 0;
@@ -1310,10 +1309,10 @@ static AOM_FORCE_INLINE uint32_t sum_above_row_lbd(const uint8_t *above_ref,

 // Computes 8-bit DC_PRED directly from the reference boundary pixels without
 // staging above_row and left_col buffers on the stack.
-static void predict_dc_block_lbd_c(const uint8_t *ref, int ref_stride,
-                                   uint8_t *dst, int dst_stride,
-                                   TX_SIZE tx_size, int txwpx, int txhpx,
-                                   int n_top_px, int n_left_px) {
+void av1_predict_dc_block_lbd_c(const uint8_t *ref, int ref_stride,
+                                uint8_t *dst, int dst_stride, TX_SIZE tx_size,
+                                int txwpx, int txhpx, int n_top_px,
+                                int n_left_px) {
   assert((unsigned int)tx_size < TX_SIZES_ALL);
   assert(txwpx == tx_size_wide[tx_size] && txhpx == tx_size_high[tx_size]);
   assert(n_top_px >= 0 && n_top_px <= txwpx);
@@ -1349,7 +1348,6 @@ static void predict_dc_block_lbd_c(const uint8_t *ref, int ref_stride,
       tx_size, txwpx, txhpx, sum_top, sum_left, n_top_px, n_left_px);
   av1_fill_block_lbd(dst, dst_stride, dc_val, txwpx, txhpx);
 }
-#endif  // HAVE_SSE2

 // Directly broadcasts the top reference row or left reference column into dst
 // for pure 90-degree (V_PRED) and 180-degree (H_PRED) blocks with width < 32
@@ -1968,8 +1966,8 @@ static AOM_FORCE_INLINE void predict_intra_block_internal(
 #if HAVE_SSE2
     // Keep RTCD dc_pred for non-SSE2/NEON targets and wide >= 32 AVX2 blocks.
     if (mode == DC_PRED && txwpx < 32) {
-      predict_dc_block_lbd_c(ref, ref_stride, dst, dst_stride, tx_size, txwpx,
-                             txhpx, n_top_px, n_left_px);
+      av1_predict_dc_block_lbd(ref, ref_stride, dst, dst_stride, tx_size, txwpx,
+                               txhpx, n_top_px, n_left_px);
       return;
     }
 #endif  // HAVE_SSE2
diff --git a/av1/common/x86/reconintra_sse2.c b/av1/common/x86/reconintra_sse2.c
new file mode 100644
index 0000000000..1ac046078c
--- /dev/null
+++ b/av1/common/x86/reconintra_sse2.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2026, Alliance for Open Media. All rights reserved.
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#include "config/aom_config.h"
+#include "config/av1_rtcd.h"
+
+#include <assert.h>
+#include <emmintrin.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "aom_dsp/aom_dsp_common.h"
+#include "av1/common/enums.h"
+#include "av1/common/reconintra.h"
+
+// Computes the horizontal sum of txwpx 8-bit pixels in above_ref using SSE2
+// _mm_sad_epu8 against zero.
+static AOM_FORCE_INLINE uint32_t
+sum_above_row_lbd_sse2(const uint8_t *above_ref, int txwpx) {
+  const __m128i zero = _mm_setzero_si128();
+  if (LIKELY(txwpx <= 16)) {
+    if (txwpx == 4) {
+      uint32_t v;
+      memcpy(&v, above_ref, 4);
+      const __m128i s = _mm_sad_epu8(_mm_cvtsi32_si128((int)v), zero);
+      return (uint32_t)_mm_cvtsi128_si32(s);
+    } else if (txwpx == 8) {
+      const __m128i s =
+          _mm_sad_epu8(_mm_loadl_epi64((const __m128i *)above_ref), zero);
+      return (uint32_t)_mm_cvtsi128_si32(s);
+    } else {
+      assert(txwpx == 16);
+      const __m128i v = _mm_loadu_si128((const __m128i *)above_ref);
+      const __m128i s = _mm_sad_epu8(v, zero);
+      return (uint32_t)_mm_cvtsi128_si32(s) + (uint32_t)_mm_extract_epi16(s, 4);
+    }
+  }
+  // Widths 32/64 kept for full RTCD TX_SIZES_ALL coverage.
+  if (txwpx == 32) {
+    const __m128i v0 = _mm_loadu_si128((const __m128i *)above_ref);
+    const __m128i v1 = _mm_loadu_si128((const __m128i *)(above_ref + 16));
+    const __m128i s =
+        _mm_add_epi64(_mm_sad_epu8(v0, zero), _mm_sad_epu8(v1, zero));
+    return (uint32_t)_mm_cvtsi128_si32(s) + (uint32_t)_mm_extract_epi16(s, 4);
+  }
+  assert(txwpx == 64);
+  const __m128i v0 = _mm_loadu_si128((const __m128i *)above_ref);
+  const __m128i v1 = _mm_loadu_si128((const __m128i *)(above_ref + 16));
+  const __m128i v2 = _mm_loadu_si128((const __m128i *)(above_ref + 32));
+  const __m128i v3 = _mm_loadu_si128((const __m128i *)(above_ref + 48));
+
+  const __m128i s01 =
+      _mm_add_epi64(_mm_sad_epu8(v0, zero), _mm_sad_epu8(v1, zero));
+  const __m128i s23 =
+      _mm_add_epi64(_mm_sad_epu8(v2, zero), _mm_sad_epu8(v3, zero));
+  const __m128i s = _mm_add_epi64(s01, s23);
+  return (uint32_t)_mm_cvtsi128_si32(s) + (uint32_t)_mm_extract_epi16(s, 4);
+}
+
+static AOM_FORCE_INLINE void fill_block_lbd_sse2(uint8_t *dst,
+                                                 ptrdiff_t dst_stride,
+                                                 uint8_t val, int txwpx,
+                                                 int txhpx) {
+  assert(txhpx >= 4);
+  if (txwpx == 4) {
+    assert((txhpx & 3) == 0);
+    const uint32_t v = (uint32_t)val * 0x01010101u;
+    // Avoid GCC over-unrolling the 4-row fill loop.
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      memcpy(dst, &v, 4);
+      memcpy(dst + dst_stride, &v, 4);
+      dst += dst_stride * 2;
+      memcpy(dst, &v, 4);
+      memcpy(dst + dst_stride, &v, 4);
+      dst += dst_stride * 2;
+      txhpx -= 4;
+    } while (txhpx != 0);
+    return;
+  } else if (txwpx == 8) {
+    assert((txhpx & 3) == 0);
+    const uint64_t v = (uint64_t)val * 0x0101010101010101ULL;
+    // Avoid GCC over-unrolling the 4-row fill loop.
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      memcpy(dst, &v, 8);
+      memcpy(dst + dst_stride, &v, 8);
+      dst += dst_stride * 2;
+      memcpy(dst, &v, 8);
+      memcpy(dst + dst_stride, &v, 8);
+      dst += dst_stride * 2;
+      txhpx -= 4;
+    } while (txhpx != 0);
+    return;
+  }
+  const __m128i v = _mm_set1_epi8((char)val);
+  if (LIKELY(txwpx == 16)) {
+    assert((txhpx & 3) == 0);
+    // Avoid GCC over-unrolling the 4-row fill loop.
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      _mm_storeu_si128((__m128i *)dst, v);
+      _mm_storeu_si128((__m128i *)(dst + dst_stride), v);
+      dst += dst_stride * 2;
+      _mm_storeu_si128((__m128i *)dst, v);
+      _mm_storeu_si128((__m128i *)(dst + dst_stride), v);
+      dst += dst_stride * 2;
+      txhpx -= 4;
+    } while (txhpx != 0);
+  } else if (txwpx == 32) {
+    // Widths 32/64 kept for full RTCD TX_SIZES_ALL coverage.
+    assert((txhpx & 1) == 0);
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      _mm_storeu_si128((__m128i *)dst, v);
+      _mm_storeu_si128((__m128i *)(dst + 16), v);
+      _mm_storeu_si128((__m128i *)(dst + dst_stride), v);
+      _mm_storeu_si128((__m128i *)(dst + dst_stride + 16), v);
+      dst += dst_stride * 2;
+      txhpx -= 2;
+    } while (txhpx != 0);
+  } else {
+    assert(txwpx == 64);
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 8
+#pragma GCC unroll 1
+#endif
+    do {
+      _mm_storeu_si128((__m128i *)dst, v);
+      _mm_storeu_si128((__m128i *)(dst + 16), v);
+      _mm_storeu_si128((__m128i *)(dst + 32), v);
+      _mm_storeu_si128((__m128i *)(dst + 48), v);
+      dst += dst_stride;
+    } while (--txhpx != 0);
+  }
+}
+
+// Computes 8-bit DC_PRED directly from the reference boundary pixels without
+// staging above_row and left_col buffers on the stack.
+void av1_predict_dc_block_lbd_sse2(const uint8_t *ref, int ref_stride,
+                                   uint8_t *dst, int dst_stride,
+                                   TX_SIZE tx_size, int txwpx, int txhpx,
+                                   int n_top_px, int n_left_px) {
+  assert((unsigned int)tx_size < TX_SIZES_ALL);
+  assert(txwpx == tx_size_wide[tx_size] && txhpx == tx_size_high[tx_size]);
+  assert(n_top_px >= 0 && n_top_px <= txwpx);
+  assert(n_left_px >= 0 && n_left_px <= txhpx);
+  uint32_t sum_top = 0;
+  if (n_top_px > 0) {
+    const uint8_t *const above_ref = ref - ref_stride;
+    if (LIKELY(n_top_px == txwpx)) {
+      sum_top = sum_above_row_lbd_sse2(above_ref, txwpx);
+    } else {
+      for (int i = 0; i < n_top_px; ++i) sum_top += above_ref[i];
+      sum_top +=
+          (uint32_t)above_ref[n_top_px - 1] * (uint32_t)(txwpx - n_top_px);
+    }
+  }
+
+  uint32_t sum_left = 0;
+  if (n_left_px > 0) {
+    const uint8_t *const left_ref = ref - 1;
+    if (LIKELY(n_left_px == txhpx)) {
+      sum_left = av1_sum_left_col_lbd(left_ref, ref_stride, txhpx);
+    } else {
+      uint8_t last_val = 0;
+      for (int i = 0; i < n_left_px; ++i) {
+        last_val = left_ref[(ptrdiff_t)i * ref_stride];
+        sum_left += last_val;
+      }
+      sum_left += (uint32_t)last_val * (uint32_t)(txhpx - n_left_px);
+    }
+  }
+
+  const uint8_t dc_val = av1_dc_val_from_sums_lbd(
+      tx_size, txwpx, txhpx, sum_top, sum_left, n_top_px, n_left_px);
+  fill_block_lbd_sse2(dst, dst_stride, dc_val, txwpx, txhpx);
+}
diff --git a/test/intrapred_test.cc b/test/intrapred_test.cc
index ffe73fce29..f985961acf 100644
--- a/test/intrapred_test.cc
+++ b/test/intrapred_test.cc
@@ -9,12 +9,16 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */

+#include <cstdint>
+#include <cstring>
 #include <string>
+#include <vector>

 #include "gtest/gtest.h"

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

 #include "test/acm_random.h"
 #include "test/register_state_check.h"
@@ -582,4 +586,119 @@ INSTANTIATE_TEST_SUITE_P(SSE2, HighbdIntraPredTest,
                          ::testing::ValuesIn(HighbdIntraPredTestVectorSse2));
 #endif  // HAVE_SSE2
 #endif  // CONFIG_AV1_HIGHBITDEPTH
+
+using PredictDcBlockFunc = void (*)(const uint8_t *ref, int ref_stride,
+                                    uint8_t *dst, int dst_stride,
+                                    TX_SIZE tx_size, int txwpx, int txhpx,
+                                    int n_top_px, int n_left_px);
+
+uint8_t ReferenceLowbdDcValue(const uint8_t *ref, int ref_stride, int txwpx,
+                              int txhpx, int n_top_px, int n_left_px) {
+  int sum = 0;
+  int count = 0;
+  if (n_top_px > 0) {
+    const uint8_t *above = ref - ref_stride;
+    for (int c = 0; c < txwpx; ++c) {
+      sum += above[c < n_top_px ? c : n_top_px - 1];
+    }
+    count += txwpx;
+  }
+  if (n_left_px > 0) {
+    const uint8_t *left = ref - 1;
+    for (int r = 0; r < txhpx; ++r) {
+      sum += left[(r < n_left_px ? r : n_left_px - 1) * ref_stride];
+    }
+    count += txhpx;
+  }
+  if (count == 0) return 128;
+  return static_cast<uint8_t>((sum + (count >> 1)) / count);
+}
+
+void CheckLowbdPredictDcBlockMatchesC(PredictDcBlockFunc fn) {
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  constexpr int kBufSize = 96 * 96;
+  std::vector<uint8_t> rnd_buf(kBufSize);
+  for (int idx = 0; idx < kBufSize; ++idx) {
+    rnd_buf[idx] = rnd.Rand8();
+  }
+  std::vector<uint8_t> ref_buf(kBufSize);
+  std::vector<uint8_t> dst_c(kBufSize);
+  std::vector<uint8_t> dst_test(kBufSize);
+
+  for (int tx = 0; tx < TX_SIZES_ALL; ++tx) {
+    const TX_SIZE tx_size = static_cast<TX_SIZE>(tx);
+    const int txwpx = tx_size_wide[tx_size];
+    const int txhpx = tx_size_high[tx_size];
+    const int ref_stride = txwpx + 19;
+    const int dst_stride = txwpx + 23;
+    const int top_cases[] = { 0, 1, txwpx / 2, txwpx };
+    const int left_cases[] = { 0, 1, txhpx / 2, txhpx };
+
+    for (int align_off = 0; align_off <= 3; ++align_off) {
+      const uint8_t *ref = ref_buf.data() + ref_stride + 16 + align_off;
+      for (int n_top_px : top_cases) {
+        for (int n_left_px : left_cases) {
+          for (int pat = 0; pat < 3; ++pat) {
+            if (pat == 0) {
+              memset(ref_buf.data(), 0, kBufSize);
+            } else if (pat == 1) {
+              memset(ref_buf.data(), 255, kBufSize);
+            } else {
+              memcpy(ref_buf.data(), rnd_buf.data(), kBufSize);
+            }
+            memset(dst_c.data(), 0xA5, kBufSize);
+            memset(dst_test.data(), 0xA5, kBufSize);
+            uint8_t *out_c = dst_c.data() + dst_stride + 16 + align_off;
+            uint8_t *out_test = dst_test.data() + dst_stride + 16 + align_off;
+            const uint8_t *const cur_ref =
+                (n_top_px == 0 && n_left_px == 0 && pat == 0) ? ref_buf.data()
+                                                              : ref;
+            const uint8_t expected_dc = ReferenceLowbdDcValue(
+                cur_ref, ref_stride, txwpx, txhpx, n_top_px, n_left_px);
+
+            API_REGISTER_STATE_CHECK(av1_predict_dc_block_lbd_c(
+                cur_ref, ref_stride, out_c, dst_stride, tx_size, txwpx, txhpx,
+                n_top_px, n_left_px));
+            API_REGISTER_STATE_CHECK(fn(cur_ref, ref_stride, out_test,
+                                        dst_stride, tx_size, txwpx, txhpx,
+                                        n_top_px, n_left_px));
+
+            for (int c = -1; c <= txwpx; ++c) {
+              ASSERT_EQ(out_c[-dst_stride + c], 0xA5);
+              ASSERT_EQ(out_test[-dst_stride + c], 0xA5);
+              ASSERT_EQ(out_c[txhpx * dst_stride + c], 0xA5);
+              ASSERT_EQ(out_test[txhpx * dst_stride + c], 0xA5);
+            }
+            for (int r = 0; r < txhpx; ++r) {
+              ASSERT_EQ(out_c[r * dst_stride - 1], 0xA5);
+              ASSERT_EQ(out_test[r * dst_stride - 1], 0xA5);
+              for (int c = 0; c < txwpx; ++c) {
+                ASSERT_EQ(out_c[r * dst_stride + c], expected_dc)
+                    << "tx_size=" << tx << " align=" << align_off
+                    << " n_top=" << n_top_px << " n_left=" << n_left_px
+                    << " pat=" << pat << " r=" << r << " c=" << c;
+                ASSERT_EQ(out_test[r * dst_stride + c], expected_dc)
+                    << "tx_size=" << tx << " align=" << align_off
+                    << " n_top=" << n_top_px << " n_left=" << n_left_px
+                    << " pat=" << pat << " r=" << r << " c=" << c;
+              }
+              ASSERT_EQ(out_c[r * dst_stride + txwpx], 0xA5);
+              ASSERT_EQ(out_test[r * dst_stride + txwpx], 0xA5);
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+TEST(LowbdPredictDcBlockTest, RtcdMatchesC) {
+  CheckLowbdPredictDcBlockMatchesC(av1_predict_dc_block_lbd);
+}
+
+#if HAVE_SSE2
+TEST(SSE2, LowbdPredictDcBlockMatchesC) {
+  CheckLowbdPredictDcBlockMatchesC(av1_predict_dc_block_lbd_sse2);
+}
+#endif  // HAVE_SSE2
 }  // namespace