Commit 8b61fdc261 for aom

commit 8b61fdc2615b86108d6e6e2f302e983e348576ae
Author: Frank Galligan <fgalligan@google.com>
Date:   Thu Nov 26 05:00:26 2026 -0800

    x86: optimize SSE2 nz_map contexts

    Rewrite av1_get_nz_map_contexts_sse2() with height-specialized 2D and
    1D H/V SSE2 kernels, peeled square 2D fast paths (4x4, 8x8, 16x16),
    sliding pair-sum reuse across columns, shift-in 21 offset
    propagation, and last_idx-bounded column early exit. Keep cold H/V
    and rectangular/large 2D paths out-of-line via AOM_NOINLINE.

    Speedup vs origin/main (AMD EPYC 7B13, --cpu-used=6, bit-exact):
      -1.40% (1T) / -5.18% (8T) 1080p allintra, -0.09% 720p good (8T).
    Binary size (x86_64 -O3): libaom.a .text +2.1 KiB (+0.03%),
      .rodata +0.2 KiB (+0.02%); stripped aomenc +0.0 KiB (+0.00%).
    Memory (peak RSS, 1080p allintra): 1T -928 KiB (-1.96%),
      8T +2164 KiB (+4.00%).

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

diff --git a/av1/encoder/x86/encodetxb_sse2.c b/av1/encoder/x86/encodetxb_sse2.c
index d4923674a9..0cfc19ce6a 100644
--- a/av1/encoder/x86/encodetxb_sse2.c
+++ b/av1/encoder/x86/encodetxb_sse2.c
@@ -13,348 +13,603 @@
 #include <emmintrin.h>  // SSE2

 #include "aom/aom_integer.h"
+#include "aom_dsp/aom_dsp_common.h"
 #include "aom_dsp/x86/mem_sse2.h"
 #include "av1/common/av1_common_int.h"
 #include "av1/common/txb_common.h"

-static inline void load_levels_4x4x5_sse2(const uint8_t *const src,
-                                          const int stride,
-                                          const ptrdiff_t *const offsets,
-                                          __m128i *const level) {
-  level[0] = load_8bit_4x4_to_1_reg_sse2(src + 1, stride);
-  level[1] = load_8bit_4x4_to_1_reg_sse2(src + stride, stride);
-  level[2] = load_8bit_4x4_to_1_reg_sse2(src + offsets[0], stride);
-  level[3] = load_8bit_4x4_to_1_reg_sse2(src + offsets[1], stride);
-  level[4] = load_8bit_4x4_to_1_reg_sse2(src + offsets[2], stride);
-}
-
-static inline void load_levels_8x2x5_sse2(const uint8_t *const src,
-                                          const int stride,
-                                          const ptrdiff_t *const offsets,
-                                          __m128i *const level) {
-  level[0] = load_8bit_8x2_to_1_reg_sse2(src + 1, stride);
-  level[1] = load_8bit_8x2_to_1_reg_sse2(src + stride, stride);
-  level[2] = load_8bit_8x2_to_1_reg_sse2(src + offsets[0], stride);
-  level[3] = load_8bit_8x2_to_1_reg_sse2(src + offsets[1], stride);
-  level[4] = load_8bit_8x2_to_1_reg_sse2(src + offsets[2], stride);
-}
-
-static inline void load_levels_16x1x5_sse2(const uint8_t *const src,
-                                           const int stride,
-                                           const ptrdiff_t *const offsets,
-                                           __m128i *const level) {
-  level[0] = _mm_loadu_si128((__m128i *)(src + 1));
-  level[1] = _mm_loadu_si128((__m128i *)(src + stride));
-  level[2] = _mm_loadu_si128((__m128i *)(src + offsets[0]));
-  level[3] = _mm_loadu_si128((__m128i *)(src + offsets[1]));
-  level[4] = _mm_loadu_si128((__m128i *)(src + offsets[2]));
-}
-
-static inline __m128i get_coeff_contexts_kernel_sse2(__m128i *const level) {
-  const __m128i const_3 = _mm_set1_epi8(3);
-  const __m128i const_4 = _mm_set1_epi8(4);
-  __m128i count;
-
-  count = _mm_min_epu8(level[0], const_3);
-  level[1] = _mm_min_epu8(level[1], const_3);
-  level[2] = _mm_min_epu8(level[2], const_3);
-  level[3] = _mm_min_epu8(level[3], const_3);
-  level[4] = _mm_min_epu8(level[4], const_3);
-  count = _mm_add_epi8(count, level[1]);
-  count = _mm_add_epi8(count, level[2]);
-  count = _mm_add_epi8(count, level[3]);
-  count = _mm_add_epi8(count, level[4]);
-  count = _mm_avg_epu8(count, _mm_setzero_si128());
+// Compute min((sum + 1) >> 1, 4) + pos_to_offset using avg(sum, 0) = (sum +
+// 1) >> 1 for 5 neighbor levels clamped to [0, 3] (sum <= 15).
+static inline __m128i finish_nz_ctx_sse2(const __m128i sum,
+                                         const __m128i const_4,
+                                         const __m128i zero,
+                                         const __m128i pos_to_offset) {
+  __m128i count = _mm_avg_epu8(sum, zero);
   count = _mm_min_epu8(count, const_4);
-  return count;
+  return _mm_add_epi8(count, pos_to_offset);
 }

-static inline void get_4_nz_map_contexts_2d(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
+// 4-high rectangular 2D blocks (TX_8X4, TX_16X4): pack 4 columns per XMM and
+// reuse sliding vertical pair sums p[c][r] = l[c][r] + l[c][r+1] so each
+// 5-neighbor sum is p[c][r+1] + p[c+1][r] + l[c+2][r].
+static inline void get_4_nz_map_contexts_2d(const uint8_t *const levels,
+                                            const int width, const int last_idx,
                                             int8_t *const coeff_contexts) {
   const int stride = 4 + TX_PAD_HOR;
-  const __m128i pos_to_offset_large = _mm_set1_epi8(21);
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos_large = _mm_set1_epi8(21);
   __m128i pos_to_offset =
-      (width == 4)
-          ? _mm_setr_epi8(0, 1, 6, 6, 1, 6, 6, 21, 6, 6, 21, 21, 6, 21, 21, 21)
-          : _mm_setr_epi8(0, 16, 16, 16, 16, 16, 16, 16, 6, 6, 21, 21, 6, 21,
-                          21, 21);
-  __m128i count;
-  __m128i level[5];
-  int8_t *cc = coeff_contexts;
-  int col = width;
+      _mm_setr_epi8(0, 16, 16, 16, 16, 16, 16, 16, 6, 6, 21, 21, 6, 21, 21, 21);

-  assert(!(width % 4));
+  int col = AOMMIN(width, (((last_idx + 2) >> 4) + 1) << 2);
+  assert(width > 4 && !(width % 4));
+  assert(col > 0 && !(col % 4));

+  const __m128i c01 = _mm_loadu_si128((const __m128i *)(levels + 0));
+  const __m128i c23 = _mm_loadu_si128((const __m128i *)(levels + 2 * stride));
+  __m128i cur =
+      _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                       _mm_castsi128_ps(c01), _mm_castsi128_ps(c23), 0x88)),
+                   const_3);
+  __m128i cur_p = _mm_add_epi8(cur, _mm_srli_epi32(cur, 8));
+
+  int8_t *cc = coeff_contexts;
+  const uint8_t *lv = levels + 4 * stride;
   do {
-    load_levels_4x4x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset);
-    _mm_store_si128((__m128i *)cc, count);
-    pos_to_offset = pos_to_offset_large;
-    levels += 4 * stride;
+    const __m128i n01 = _mm_loadu_si128((const __m128i *)(lv + 0));
+    const __m128i n23 = _mm_loadu_si128((const __m128i *)(lv + 2 * stride));
+    const __m128i nxt_c =
+        _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                         _mm_castsi128_ps(n01), _mm_castsi128_ps(n23), 0x88)),
+                     const_3);
+    const __m128i nxt_p = _mm_add_epi8(nxt_c, _mm_srli_epi32(nxt_c, 8));
+
+    const __m128i nxt1_p =
+        _mm_or_si128(_mm_srli_si128(cur_p, 4), _mm_slli_si128(nxt_p, 12));
+    const __m128i nxt2_c = _mm_castps_si128(
+        _mm_shuffle_ps(_mm_castsi128_ps(cur), _mm_castsi128_ps(nxt_c), 0x4e));
+
+    __m128i sum = _mm_add_epi8(_mm_srli_epi32(cur_p, 8), nxt1_p);
+    sum = _mm_add_epi8(sum, nxt2_c);
+    _mm_store_si128((__m128i *)cc,
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+
+    pos_to_offset = pos_large;
+    cur = nxt_c;
+    cur_p = nxt_p;
+    lv += 4 * stride;
     cc += 16;
     col -= 4;
-  } while (col);
+  } while (col != 0);

   coeff_contexts[0] = 0;
 }

-static inline void get_4_nz_map_contexts_ver(const uint8_t *levels,
-                                             const int width,
-                                             const ptrdiff_t *const offsets,
-                                             int8_t *coeff_contexts) {
+// 4-high H/V blocks: pack 4 columns per XMM and compute 1D neighbor sums.
+static inline void get_4_nz_map_contexts_hv(const uint8_t *const levels,
+                                            const int width, const int last_idx,
+                                            const int is_hor,
+                                            int8_t *const coeff_contexts) {
   const int stride = 4 + TX_PAD_HOR;
-  const __m128i pos_to_offset =
-      _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
-  __m128i count;
-  __m128i level[5];
-  int col = width;
-
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
   assert(!(width % 4));

-  do {
-    load_levels_4x4x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset);
-    _mm_store_si128((__m128i *)coeff_contexts, count);
-    levels += 4 * stride;
-    coeff_contexts += 16;
-    col -= 4;
-  } while (col);
+  const __m128i c01 = _mm_loadu_si128((const __m128i *)(levels + 0));
+  const __m128i c23 = _mm_loadu_si128((const __m128i *)(levels + 2 * stride));
+  __m128i cur =
+      _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                       _mm_castsi128_ps(c01), _mm_castsi128_ps(c23), 0x88)),
+                   const_3);
+
+  const uint8_t *lv = levels + 4 * stride;
+  int8_t *cc = coeff_contexts;
+  if (is_hor) {
+    __m128i pos_to_offset =
+        _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
+    const __m128i pos_large =
+        _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 10));
+    int col = AOMMIN(width, ((last_idx + 15) >> 4) << 2);
+    assert(col > 0 && !(col % 4));
+    do {
+      const __m128i n01 = _mm_loadu_si128((const __m128i *)(lv + 0));
+      const __m128i n23 = _mm_loadu_si128((const __m128i *)(lv + 2 * stride));
+      const __m128i nxt =
+          _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                           _mm_castsi128_ps(n01), _mm_castsi128_ps(n23), 0x88)),
+                       const_3);
+      const __m128i sh1 =
+          _mm_or_si128(_mm_srli_si128(cur, 4), _mm_slli_si128(nxt, 12));
+      const __m128i sh2 = _mm_castps_si128(
+          _mm_shuffle_ps(_mm_castsi128_ps(cur), _mm_castsi128_ps(nxt), 0x4e));
+      const __m128i sh3 =
+          _mm_or_si128(_mm_srli_si128(cur, 12), _mm_slli_si128(nxt, 4));
+      __m128i sum = _mm_add_epi8(_mm_srli_epi32(cur, 8), sh1);
+      sum = _mm_add_epi8(sum, sh2);
+      sum = _mm_add_epi8(sum, sh3);
+      sum = _mm_add_epi8(sum, nxt);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+      pos_to_offset = pos_large;
+      cur = nxt;
+      lv += 4 * stride;
+      cc += 16;
+      col -= 4;
+    } while (col != 0);
+  } else {
+    const __m128i pos_to_offset =
+        _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
+    int col = AOMMIN(width, (last_idx + 3) & ~3);
+    assert(col > 0 && !(col % 4));
+    do {
+      const __m128i n01 = _mm_loadu_si128((const __m128i *)(lv + 0));
+      const __m128i n23 = _mm_loadu_si128((const __m128i *)(lv + 2 * stride));
+      const __m128i nxt =
+          _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                           _mm_castsi128_ps(n01), _mm_castsi128_ps(n23), 0x88)),
+                       const_3);
+      const __m128i sh1 =
+          _mm_or_si128(_mm_srli_si128(cur, 4), _mm_slli_si128(nxt, 12));
+      const __m128i p = _mm_add_epi8(cur, _mm_srli_epi32(cur, 8));
+      const __m128i q = _mm_add_epi8(p, _mm_srli_epi32(p, 16));
+      const __m128i sum = _mm_add_epi8(_mm_srli_epi32(q, 8), sh1);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+      cur = nxt;
+      lv += 4 * stride;
+      cc += 16;
+      col -= 4;
+    } while (col != 0);
+  }
 }

-static inline void get_4_nz_map_contexts_hor(const uint8_t *levels,
-                                             const int width,
-                                             const ptrdiff_t *const offsets,
-                                             int8_t *coeff_contexts) {
-  const int stride = 4 + TX_PAD_HOR;
-  const __m128i pos_to_offset_large = _mm_set1_epi8(SIG_COEF_CONTEXTS_2D + 10);
-  __m128i pos_to_offset =
-      _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
-  __m128i count;
-  __m128i level[5];
-  int col = width;
+// 8-high rectangular 2D blocks (TX_4X8, TX_16X8, TX_32X8): process 2 columns
+// per iteration with early stop from last_idx.
+static inline void get_8_coeff_contexts_2d(const uint8_t *const levels,
+                                           const int width, const int last_idx,
+                                           int8_t *const coeff_contexts) {
+  const int stride = 8 + TX_PAD_HOR;
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  assert(width != 8 && !(width % 2));
+
+  int col;
+  __m128i pos0, pos1;
+  const __m128i pos2 = _mm_set1_epi8(21);
+  if (width < 8) {
+    // default_scan_4x8 reaches column 2 at scan index 3.
+    col = (last_idx <= 3) ? 2 : 4;
+    pos0 = _mm_setr_epi8(0, 11, 6, 6, 21, 21, 21, 21, 11, 11, 6, 21, 21, 21, 21,
+                         21);
+    pos1 = _mm_setr_epi8(11, 11, 21, 21, 21, 21, 21, 21, 11, 11, 21, 21, 21, 21,
+                         21, 21);
+  } else {
+    assert(width >= 16);
+    // default_scan_{16x8,32x8} enter columns 2, 4, 6, 8 at indices
+    // 5, 14, 27, 43, then advance 2 columns per 16-coefficient band.
+    if (last_idx <= 14) {
+      col = (last_idx <= 5) ? 2 : 4;
+    } else if (last_idx <= 43) {
+      col = (last_idx <= 27) ? 6 : 8;
+    } else {
+      col = AOMMIN(width, 10 + (((last_idx - 44) >> 4) << 1));
+    }
+    pos0 = _mm_set1_epi8(16);
+    pos1 = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21,
+                         21, 21);
+  }
+  assert(col > 0 && !(col % 2));

-  assert(!(width % 4));
+  const __m128i c01 =
+      _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(levels, stride), const_3);
+  __m128i p01 = _mm_add_epi8(c01, _mm_srli_epi64(c01, 8));

+  int8_t *cc = coeff_contexts;
+  const uint8_t *lv = levels + 2 * stride;
   do {
-    load_levels_4x4x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset);
-    _mm_store_si128((__m128i *)coeff_contexts, count);
-    pos_to_offset = pos_to_offset_large;
-    levels += 4 * stride;
-    coeff_contexts += 16;
-    col -= 4;
-  } while (col);
+    const __m128i c23 =
+        _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(lv, stride), const_3);
+    const __m128i p23 = _mm_add_epi8(c23, _mm_srli_epi64(c23, 8));
+    const __m128i p12 = _mm_castps_si128(
+        _mm_shuffle_ps(_mm_castsi128_ps(p01), _mm_castsi128_ps(p23), 0x4e));
+
+    __m128i sum = _mm_add_epi8(_mm_srli_epi64(p01, 8), p12);
+    sum = _mm_add_epi8(sum, c23);
+    _mm_store_si128((__m128i *)cc,
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos0));
+
+    pos0 = pos1;
+    pos1 = pos2;
+    p01 = p23;
+    lv += 2 * stride;
+    cc += 16;
+    col -= 2;
+  } while (col != 0);
+
+  coeff_contexts[0] = 0;
 }

-static inline void get_8_coeff_contexts_2d(const uint8_t *levels,
-                                           const int width,
-                                           const ptrdiff_t *const offsets,
-                                           int8_t *coeff_contexts) {
+// 8-high H/V blocks: reuse sliding 2-column horizontal pair sums
+// s[k] = c[k] + c[k+1] or vertical prefix sums across 8x2 column pairs.
+static inline void get_8_coeff_contexts_hv(const uint8_t *const levels,
+                                           const int width, const int last_idx,
+                                           const int is_hor,
+                                           int8_t *const coeff_contexts) {
   const int stride = 8 + TX_PAD_HOR;
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  assert(!(width % 2));
+
+  __m128i c01 =
+      _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(levels, stride), const_3);
   int8_t *cc = coeff_contexts;
-  int col = width;
-  __m128i count;
-  __m128i level[5];
-  __m128i pos_to_offset[3];

-  assert(!(width % 2));
+  if (is_hor) {
+    __m128i pos_to_offset =
+        _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5);
+    const __m128i pos_large =
+        _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 10));
+    int col = AOMMIN(width, ((last_idx + 15) >> 4) << 1);
+    assert(col > 0 && !(col % 2));
+    __m128i c23 = _mm_min_epu8(
+        load_8bit_8x2_to_1_reg_sse2(levels + 2 * stride, stride), const_3);
+    __m128i s01 = _mm_add_epi8(c01, c23);
+    const uint8_t *lv = levels + 4 * stride;
+    do {
+      const __m128i c45 =
+          _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(lv, stride), const_3);
+      const __m128i s23 = _mm_add_epi8(c23, c45);
+      const __m128i mid = _mm_castps_si128(
+          _mm_shuffle_ps(_mm_castsi128_ps(s01), _mm_castsi128_ps(s23), 0x4e));
+      __m128i sum = _mm_add_epi8(_mm_srli_epi64(c01, 8), mid);
+      sum = _mm_add_epi8(sum, s23);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+      pos_to_offset = pos_large;
+      c01 = c23;
+      c23 = c45;
+      s01 = s23;
+      lv += 2 * stride;
+      cc += 16;
+      col -= 2;
+    } while (col != 0);
+  } else {
+    const __m128i pos_to_offset =
+        _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
+    int col = AOMMIN(width, (last_idx + 1) & ~1);
+    assert(col > 0 && !(col % 2));
+    const uint8_t *lv = levels + 2 * stride;
+    do {
+      const __m128i c23 =
+          _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(lv, stride), const_3);
+      const __m128i c12 = _mm_castps_si128(
+          _mm_shuffle_ps(_mm_castsi128_ps(c01), _mm_castsi128_ps(c23), 0x4e));
+      const __m128i p = _mm_add_epi8(c01, _mm_srli_epi64(c01, 8));
+      const __m128i q = _mm_add_epi8(p, _mm_srli_epi64(p, 16));
+      const __m128i sum = _mm_add_epi8(_mm_srli_epi64(q, 8), c12);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+      c01 = c23;
+      lv += 2 * stride;
+      cc += 16;
+      col -= 2;
+    } while (col != 0);
+  }
+}

-  if (width == 8) {
-    pos_to_offset[0] =
-        _mm_setr_epi8(0, 1, 6, 6, 21, 21, 21, 21, 1, 6, 6, 21, 21, 21, 21, 21);
-    pos_to_offset[1] = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21,
-                                     21, 21, 21, 21, 21);
-  } else if (width < 8) {
-    pos_to_offset[0] = _mm_setr_epi8(0, 11, 6, 6, 21, 21, 21, 21, 11, 11, 6, 21,
-                                     21, 21, 21, 21);
-    pos_to_offset[1] = _mm_setr_epi8(11, 11, 21, 21, 21, 21, 21, 21, 11, 11, 21,
-                                     21, 21, 21, 21, 21);
+// Minimum active columns needed for 2D default scan up to last_idx <= 36.
+static const uint8_t kLastIdxToCols16n[37] = { 1, 1, 2, 2, 3, 3, 3, 4, 4, 4,
+                                               4, 5, 5, 5, 5, 5, 6, 6, 6, 6,
+                                               6, 6, 7, 7, 7, 7, 7, 7, 7, 8,
+                                               8, 8, 8, 8, 8, 8, 8 };
+
+static inline int get_cols_16n_2d(const int width, const int last_idx) {
+  int col;
+  if (last_idx <= 36) {
+    col = kLastIdxToCols16n[last_idx];
+  } else if (last_idx <= 120) {
+    // Over 37 <= last_idx <= 120, 9 + (((last_idx - 25) * 21) >> 8) is a
+    // conservative branchless upper bound across all 16n 2D scans (square/tall
+    // enter diagonal k at k*(k+1)/2; wide 32x16/64x16 enter col 9 at 54).
+    col = 9 + (((last_idx - 25) * 21) >> 8);
+  } else if (last_idx <= 275) {
+    // Past index 120, at most one new column opens per 16-row step (`32x16`
+    // opens +1 col per 16 coeffs from 151; `32x32` zigzag opens column pairs
+    // back-to-back, hitting col 18 at idx 154 and col 20 at idx 191 cleanly).
+    col = 16 + ((last_idx - 120) >> 4);
   } else {
-    pos_to_offset[0] = _mm_setr_epi8(0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
-                                     16, 16, 16, 16, 16);
-    pos_to_offset[1] = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21,
-                                     21, 21, 21, 21, 21);
+    col = width;
   }
-  pos_to_offset[2] = _mm_set1_epi8(21);
+  return AOMMIN(width, col);
+}

+enum {
+  kBandAll21 = 0,
+  kBandWideHi = 1,
+  kBandWide = 2,
+  kBandSquare = 3,
+  kBandTall = 4
+};
+
+// 16-row 2D band pass using sliding vertical pair sums p[c][r] = l[c][r] +
+// l[c][r+1] so each context sums p[c][r+1] + p[c+1][r] + l[c+2][r].
+// When zero_tail is 0 (32-high Band 0), t0..t2 bridge rows 16..17; when
+// zero_tail is 1 (16-high or 32-high Band 1), TX_PAD_HOR guarantees zero tail.
+static inline void run_2d_band16_sse2(const uint8_t *const lv_band,
+                                      const int stride, int col,
+                                      const int cc_stride, const int mode,
+                                      const int zero_tail,
+                                      int8_t *const cc_band) {
+  assert(col > 0);
+  assert(cc_stride == 16 || cc_stride == 32);
+  assert(mode >= kBandAll21 && mode <= kBandTall);
+  assert(zero_tail == 0 || zero_tail == 1);
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  __m128i pos0, pos1, pos2, top_21;
+
+  if (mode == kBandAll21) {
+    pos0 = _mm_set1_epi8(21);
+    pos1 = pos2 = top_21 = pos0;
+  } else if (mode == kBandWideHi) {
+    pos0 = pos1 = _mm_set1_epi8(16);
+    pos2 = _mm_set1_epi8(21);
+    top_21 = pos2;
+  } else if (mode == kBandWide) {
+    pos0 = pos1 = _mm_set1_epi8(16);
+    pos2 = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
+                         21, 21);
+    top_21 = _mm_slli_si128(_mm_cvtsi32_si128(21), 15);
+  } else if (mode == kBandSquare) {
+    pos0 = _mm_setr_epi8(0, 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
+                         21);
+    top_21 = _mm_slli_si128(_mm_cvtsi32_si128(21), 15);
+    pos1 = pos2 = pos0;
+  } else {
+    pos0 = _mm_setr_epi8(0, 11, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
+                         21, 21);
+    pos1 = _mm_setr_epi8(11, 11, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
+                         21, 21);
+    pos2 = _mm_setr_epi8(11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
+                         21, 21);
+    top_21 = pos2;
+  }
+
+  const __m128i cl0 =
+      _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv_band + 0)), const_3);
+  const __m128i cl1 = _mm_min_epu8(
+      _mm_loadu_si128((const __m128i *)(lv_band + stride)), const_3);
+  __m128i p0_s1, p1, p1_s1;
+  if (zero_tail) {
+    const __m128i p0 = _mm_add_epi8(cl0, _mm_srli_si128(cl0, 1));
+    p0_s1 = _mm_srli_si128(p0, 1);
+    p1 = _mm_add_epi8(cl1, _mm_srli_si128(cl1, 1));
+    p1_s1 = _mm_srli_si128(p1, 1);
+  } else {
+    const __m128i t0 =
+        _mm_min_epu8(_mm_cvtsi32_si128(loadu_int16(lv_band + 16)), const_3);
+    const __m128i p0 = _mm_add_epi8(
+        cl0, _mm_or_si128(_mm_srli_si128(cl0, 1), _mm_slli_si128(t0, 15)));
+    const __m128i pt0 = _mm_add_epi8(t0, _mm_srli_si128(t0, 1));
+    p0_s1 = _mm_or_si128(_mm_srli_si128(p0, 1), _mm_slli_si128(pt0, 15));
+
+    const __m128i t1 = _mm_min_epu8(
+        _mm_cvtsi32_si128(loadu_int16(lv_band + stride + 16)), const_3);
+    p1 = _mm_add_epi8(
+        cl1, _mm_or_si128(_mm_srli_si128(cl1, 1), _mm_slli_si128(t1, 15)));
+    const __m128i pt1 = _mm_add_epi8(t1, _mm_srli_si128(t1, 1));
+    p1_s1 = _mm_or_si128(_mm_srli_si128(p1, 1), _mm_slli_si128(pt1, 15));
+  }
+
+  const uint8_t *lv = lv_band + 2 * stride;
+  int8_t *cc = cc_band;
   do {
-    load_levels_8x2x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset[0]);
-    _mm_store_si128((__m128i *)cc, count);
-    pos_to_offset[0] = pos_to_offset[1];
-    pos_to_offset[1] = pos_to_offset[2];
-    levels += 2 * stride;
-    cc += 16;
-    col -= 2;
-  } while (col);
+    const __m128i cl2 =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv), const_3);
+    // Compute and store contexts before building p2/p2_s1 so p0_s1 and p1 die
+    // early and peak live XMM register pressure stays low.
+    __m128i sum = _mm_add_epi8(p0_s1, p1);
+    sum = _mm_add_epi8(sum, cl2);
+    _mm_store_si128((__m128i *)cc,
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos0));
+
+    __m128i p2, p2_s1;
+    if (zero_tail) {
+      p2 = _mm_add_epi8(cl2, _mm_srli_si128(cl2, 1));
+      p2_s1 = _mm_srli_si128(p2, 1);
+    } else {
+      const __m128i t2 =
+          _mm_min_epu8(_mm_cvtsi32_si128(loadu_int16(lv + 16)), const_3);
+      p2 = _mm_add_epi8(
+          cl2, _mm_or_si128(_mm_srli_si128(cl2, 1), _mm_slli_si128(t2, 15)));
+      const __m128i pt2 = _mm_add_epi8(t2, _mm_srli_si128(t2, 1));
+      p2_s1 = _mm_or_si128(_mm_srli_si128(p2, 1), _mm_slli_si128(pt2, 15));
+    }
+
+    if (mode == kBandSquare) {
+      pos0 = _mm_or_si128(_mm_srli_si128(pos0, 1), top_21);
+    } else if (mode == kBandWide) {
+      pos0 = pos1;
+      pos1 = pos2;
+      pos2 = _mm_or_si128(_mm_srli_si128(pos2, 1), top_21);
+    } else if (mode == kBandWideHi || mode == kBandTall) {
+      pos0 = pos1;
+      pos1 = pos2;
+    }
+    p0_s1 = p1_s1;
+    p1 = p2;
+    p1_s1 = p2_s1;
+    lv += stride;
+    cc += cc_stride;
+  } while (--col != 0);
+}

+static inline void get_16n_all_coeff_contexts_2d(
+    const uint8_t *const levels, const int real_width, const int real_height,
+    const int width, const int height, const int last_idx,
+    int8_t *const coeff_contexts) {
+  assert(height == 16 || height == 32);
+  const int col = get_cols_16n_2d(width, last_idx);
+  if (height == 16) {
+    const int stride = 16 + TX_PAD_HOR;
+    if (real_width < real_height) {
+      run_2d_band16_sse2(levels, stride, col, 16, kBandTall, 1, coeff_contexts);
+    } else {
+      assert(real_width > real_height);
+      run_2d_band16_sse2(levels, stride, col, 16, kBandWide, 1, coeff_contexts);
+    }
+  } else {
+    const int stride = 32 + TX_PAD_HOR;
+    if (real_width == real_height) {
+      run_2d_band16_sse2(levels, stride, col, 32, kBandSquare, 0,
+                         coeff_contexts);
+    } else if (real_width < real_height) {
+      run_2d_band16_sse2(levels, stride, col, 32, kBandTall, 0, coeff_contexts);
+    } else {
+      run_2d_band16_sse2(levels, stride, col, 32, kBandWide, 0, coeff_contexts);
+    }
+    // Row 16 first appears in default_scan at index 107 (8x32), 136 (32x32),
+    // and 151 (16x32), so last_idx < 107 never touches rows 16..31.
+    if (last_idx >= 107) {
+      if (real_width > real_height) {
+        run_2d_band16_sse2(levels + 16, stride, col, 32, kBandWideHi, 1,
+                           coeff_contexts + 16);
+      } else {
+        run_2d_band16_sse2(levels + 16, stride, col, 32, kBandAll21, 1,
+                           coeff_contexts + 16);
+      }
+    }
+  }
   coeff_contexts[0] = 0;
 }

-static inline void get_8_coeff_contexts_ver(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
-                                            int8_t *coeff_contexts) {
-  const int stride = 8 + TX_PAD_HOR;
-  const __m128i pos_to_offset =
+static inline void get_32h_coeff_contexts_ver(const uint8_t *const levels,
+                                              const int stride, int col,
+                                              const int h_rows,
+                                              int8_t *const coeff_contexts) {
+  assert(col > 0);
+  assert(h_rows == 16 || h_rows == 32);
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos_lo =
       _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
+                    SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
-  int col = width;
-  __m128i count;
-  __m128i level[5];
-
-  assert(!(width % 2));
-
-  do {
-    load_levels_8x2x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset);
-    _mm_store_si128((__m128i *)coeff_contexts, count);
-    levels += 2 * stride;
-    coeff_contexts += 16;
-    col -= 2;
-  } while (col);
-}
-
-static inline void get_8_coeff_contexts_hor(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
-                                            int8_t *coeff_contexts) {
-  const int stride = 8 + TX_PAD_HOR;
-  const __m128i pos_to_offset_large = _mm_set1_epi8(SIG_COEF_CONTEXTS_2D + 10);
-  __m128i pos_to_offset =
-      _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 0,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5,
-                    SIG_COEF_CONTEXTS_2D + 5, SIG_COEF_CONTEXTS_2D + 5);
-  int col = width;
-  __m128i count;
-  __m128i level[5];
-
-  assert(!(width % 2));
-
-  do {
-    load_levels_8x2x5_sse2(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel_sse2(level);
-    count = _mm_add_epi8(count, pos_to_offset);
-    _mm_store_si128((__m128i *)coeff_contexts, count);
-    pos_to_offset = pos_to_offset_large;
-    levels += 2 * stride;
-    coeff_contexts += 16;
-    col -= 2;
-  } while (col);
-}
-
-static inline void get_16n_coeff_contexts_2d(const uint8_t *levels,
-                                             const int real_width,
-                                             const int real_height,
-                                             const int width, const int height,
-                                             const ptrdiff_t *const offsets,
-                                             int8_t *coeff_contexts) {
-  const int stride = height + TX_PAD_HOR;
+  const uint8_t *lv = levels;
   int8_t *cc = coeff_contexts;
-  int col = width;
-  __m128i pos_to_offset[5];
-  __m128i pos_to_offset_large[3];
-  __m128i count;
-  __m128i level[5];
-
-  assert(!(height % 16));
-
-  pos_to_offset_large[2] = _mm_set1_epi8(21);
-  if (real_width == real_height) {
-    pos_to_offset[0] = _mm_setr_epi8(0, 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21);
-    pos_to_offset[1] = _mm_setr_epi8(1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[2] = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[3] = _mm_setr_epi8(6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[4] = pos_to_offset_large[0] = pos_to_offset_large[1] =
-        pos_to_offset_large[2];
-  } else if (real_width < real_height) {
-    pos_to_offset[0] = _mm_setr_epi8(0, 11, 6, 6, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[1] = _mm_setr_epi8(11, 11, 6, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[2] = pos_to_offset[3] = pos_to_offset[4] = _mm_setr_epi8(
-        11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21);
-    pos_to_offset_large[0] = pos_to_offset_large[1] = pos_to_offset_large[2];
-  } else {  // real_width > real_height
-    pos_to_offset[0] = pos_to_offset[1] = _mm_setr_epi8(
-        16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16);
-    pos_to_offset[2] = _mm_setr_epi8(6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[3] = _mm_setr_epi8(6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
-                                     21, 21, 21, 21, 21);
-    pos_to_offset[4] = pos_to_offset_large[2];
-    pos_to_offset_large[0] = pos_to_offset_large[1] = _mm_set1_epi8(16);
-  }
-
-  do {
-    int h = height;
-
+  if (h_rows == 16) {
     do {
-      load_levels_16x1x5_sse2(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel_sse2(level);
-      count = _mm_add_epi8(count, pos_to_offset[0]);
-      _mm_store_si128((__m128i *)cc, count);
-      levels += 16;
-      cc += 16;
-      h -= 16;
-      pos_to_offset[0] = pos_to_offset_large[0];
-    } while (h);
-
-    pos_to_offset[0] = pos_to_offset[1];
-    pos_to_offset[1] = pos_to_offset[2];
-    pos_to_offset[2] = pos_to_offset[3];
-    pos_to_offset[3] = pos_to_offset[4];
-    pos_to_offset_large[0] = pos_to_offset_large[1];
-    pos_to_offset_large[1] = pos_to_offset_large[2];
-    levels += TX_PAD_HOR;
-  } while (--col);
-
-  coeff_contexts[0] = 0;
+      const __m128i m1 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 1)), const_3);
+      const __m128i m2 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 2)), const_3);
+      const __m128i m3 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 3)), const_3);
+      const __m128i m4 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 4)), const_3);
+      const __m128i nxt = _mm_min_epu8(
+          _mm_loadu_si128((const __m128i *)(lv + stride)), const_3);
+      const __m128i sum = _mm_add_epi8(
+          _mm_add_epi8(_mm_add_epi8(m1, m2), _mm_add_epi8(m3, m4)), nxt);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_lo));
+      lv += stride;
+      cc += 32;
+    } while (--col != 0);
+  } else {
+    const __m128i pos_hi = _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 10));
+    __m128i cur_hi =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 16)), const_3);
+    do {
+      const __m128i m1 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 1)), const_3);
+      const __m128i m2 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 2)), const_3);
+      const __m128i m3 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 3)), const_3);
+      const __m128i m4 =
+          _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv + 4)), const_3);
+      const __m128i nxt_lo = _mm_min_epu8(
+          _mm_loadu_si128((const __m128i *)(lv + stride)), const_3);
+      const __m128i sum_lo = _mm_add_epi8(
+          _mm_add_epi8(_mm_add_epi8(m1, m2), _mm_add_epi8(m3, m4)), nxt_lo);
+      _mm_store_si128((__m128i *)cc,
+                      finish_nz_ctx_sse2(sum_lo, const_4, zero, pos_lo));
+
+      // Band 1 is followed by TX_PAD_HOR zeros, so shift prefix sums suffice.
+      const __m128i nxt_hi = _mm_min_epu8(
+          _mm_loadu_si128((const __m128i *)(lv + stride + 16)), const_3);
+      const __m128i p = _mm_add_epi8(cur_hi, _mm_srli_si128(cur_hi, 1));
+      const __m128i q = _mm_add_epi8(p, _mm_srli_si128(p, 2));
+      const __m128i sum_hi = _mm_add_epi8(_mm_srli_si128(q, 1), nxt_hi);
+      _mm_store_si128((__m128i *)(cc + 16),
+                      finish_nz_ctx_sse2(sum_hi, const_4, zero, pos_hi));
+      cur_hi = nxt_hi;
+      lv += stride;
+      cc += 32;
+    } while (--col != 0);
+  }
 }

-static inline void get_16n_coeff_contexts_ver(const uint8_t *levels,
+static inline void get_16n_coeff_contexts_ver(const uint8_t *const levels,
                                               const int width, const int height,
-                                              const ptrdiff_t *const offsets,
-                                              int8_t *coeff_contexts) {
+                                              const int last_idx,
+                                              int8_t *const coeff_contexts) {
+  assert(height == 16 || height == 32);
   const int stride = height + TX_PAD_HOR;
-  const __m128i pos_to_offset_large =
-      _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
+  int col = AOMMIN(width, last_idx);
+  assert(col > 0);
+  if (height == 32) {
+    const int h_rows = (last_idx > (width << 4)) ? 32 : 16;
+    get_32h_coeff_contexts_ver(levels, stride, col, h_rows, coeff_contexts);
+    return;
+  }
+
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos_lo =
+      _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
@@ -362,144 +617,329 @@ static inline void get_16n_coeff_contexts_ver(const uint8_t *levels,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
                     SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
-  __m128i count;
-  __m128i level[5];
-  int col = width;
-
-  assert(!(height % 16));
-
+  const uint8_t *lv = levels;
+  int8_t *cc = coeff_contexts;
+  __m128i cur = _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv), const_3);
+  lv += stride;
   do {
-    __m128i pos_to_offset =
-        _mm_setr_epi8(SIG_COEF_CONTEXTS_2D + 0, SIG_COEF_CONTEXTS_2D + 5,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10,
-                      SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10);
-    int h = height;
+    const __m128i nxt =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv), const_3);
+    const __m128i p = _mm_add_epi8(cur, _mm_srli_si128(cur, 1));
+    const __m128i q = _mm_add_epi8(p, _mm_srli_si128(p, 2));
+    const __m128i sum = _mm_add_epi8(_mm_srli_si128(q, 1), nxt);
+    _mm_store_si128((__m128i *)cc,
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos_lo));
+    cur = nxt;
+    lv += stride;
+    cc += 16;
+  } while (--col != 0);
+}

-    do {
-      load_levels_16x1x5_sse2(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel_sse2(level);
-      count = _mm_add_epi8(count, pos_to_offset);
-      _mm_store_si128((__m128i *)coeff_contexts, count);
-      pos_to_offset = pos_to_offset_large;
-      levels += 16;
-      coeff_contexts += 16;
-      h -= 16;
-    } while (h);
-
-    levels += TX_PAD_HOR;
-  } while (--col);
+// 16-row horizontal band pass: reuse sliding column pair sums
+// p[k] = c[k] + c[k+1] so sum = l[c][r+1] + p[c+1] + p[c+3].
+static inline void get_16n_hor_band_sse2(const uint8_t *const lv_b,
+                                         const int stride, int col,
+                                         const int height, int8_t *const cc_b) {
+  assert(col > 0);
+  assert(height == 16 || height == 32);
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos2 = _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 10));
+  __m128i pos0 = _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 0));
+  __m128i pos1 = _mm_set1_epi8((int8_t)(SIG_COEF_CONTEXTS_2D + 5));
+  __m128i c1 =
+      _mm_min_epu8(_mm_loadu_si128((const __m128i *)(lv_b + stride)), const_3);
+  __m128i c2 = _mm_min_epu8(
+      _mm_loadu_si128((const __m128i *)(lv_b + 2 * stride)), const_3);
+  __m128i c3 = _mm_min_epu8(
+      _mm_loadu_si128((const __m128i *)(lv_b + 3 * stride)), const_3);
+  __m128i p12 = _mm_add_epi8(c1, c2);
+  __m128i p23 = _mm_add_epi8(c2, c3);
+  const uint8_t *lv0 = lv_b + 1;
+  const uint8_t *lv4 = lv_b + 4 * stride;
+  int8_t *cc = cc_b;
+  do {
+    const __m128i c4 =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv4), const_3);
+    const __m128i p34 = _mm_add_epi8(c3, c4);
+    const __m128i l0 =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv0), const_3);
+    __m128i sum = _mm_add_epi8(l0, p12);
+    sum = _mm_add_epi8(sum, p34);
+    _mm_store_si128((__m128i *)cc,
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos0));
+    pos0 = pos1;
+    pos1 = pos2;
+    c3 = c4;
+    p12 = p23;
+    p23 = p34;
+    lv0 += stride;
+    lv4 += stride;
+    cc += height;
+  } while (--col != 0);
 }

-static inline void get_16n_coeff_contexts_hor(const uint8_t *levels,
+static inline void get_16n_coeff_contexts_hor(const uint8_t *const levels,
                                               const int width, const int height,
-                                              const ptrdiff_t *const offsets,
-                                              int8_t *coeff_contexts) {
+                                              const int last_idx,
+                                              int8_t *const coeff_contexts) {
+  assert(height == 16 || height == 32);
   const int stride = height + TX_PAD_HOR;
-  __m128i pos_to_offset[3];
-  __m128i count;
-  __m128i level[5];
-  int col = width;
+  const int shift = 4 + (height >> 5);
+  const int col = AOMMIN(width, (last_idx + height - 1) >> shift);
+  get_16n_hor_band_sse2(levels, stride, col, height, coeff_contexts);
+  if (height == 32 && last_idx > 16) {
+    // mcol_scan enters rows 16..31 of column c at index 32*c + 16.
+    const int col1 = AOMMIN(width, (last_idx + 15) >> 5);
+    get_16n_hor_band_sse2(levels + 16, stride, col1, height,
+                          coeff_contexts + 16);
+  }
+}
+
+// Fast 4x4 2D path: compute all 16 contexts in a single XMM pass.
+static inline void get_4x4_2d_sse2(const uint8_t *const levels,
+                                   int8_t *const coeff_contexts) {
+  const int stride = 4 + TX_PAD_HOR;
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos_to_offset =
+      _mm_setr_epi8(0, 1, 6, 6, 1, 6, 6, 21, 6, 6, 21, 21, 6, 21, 21, 21);
+
+  const __m128i c01 = _mm_loadu_si128((const __m128i *)(levels + 0));
+  const __m128i c23 = _mm_loadu_si128((const __m128i *)(levels + 2 * stride));

-  assert(!(height % 16));
+  const __m128i cur =
+      _mm_min_epu8(_mm_castps_si128(_mm_shuffle_ps(
+                       _mm_castsi128_ps(c01), _mm_castsi128_ps(c23), 0x88)),
+                   const_3);

-  pos_to_offset[0] = _mm_set1_epi8(SIG_COEF_CONTEXTS_2D + 0);
-  pos_to_offset[1] = _mm_set1_epi8(SIG_COEF_CONTEXTS_2D + 5);
-  pos_to_offset[2] = _mm_set1_epi8(SIG_COEF_CONTEXTS_2D + 10);
+  const __m128i pair = _mm_add_epi8(cur, _mm_srli_epi32(cur, 8));
+  const __m128i pair_down1 = _mm_srli_epi32(pair, 8);

+  __m128i sum = _mm_add_epi8(pair_down1, _mm_srli_si128(pair, 4));
+  sum = _mm_add_epi8(sum, _mm_srli_si128(cur, 8));
+
+  _mm_store_si128((__m128i *)coeff_contexts,
+                  finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset));
+  coeff_contexts[0] = 0;
+}
+
+// Fast 8x8 2D path: progressively skip upper columns based on last_idx.
+static inline void get_8x8_2d_sse2(const uint8_t *const levels,
+                                   const int last_idx,
+                                   int8_t *const coeff_contexts) {
+  const int stride = 8 + TX_PAD_HOR;
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i pos_to_offset_0 =
+      _mm_setr_epi8(0, 1, 6, 6, 21, 21, 21, 21, 1, 6, 6, 21, 21, 21, 21, 21);
+
+  const __m128i c01 =
+      _mm_min_epu8(load_8bit_8x2_to_1_reg_sse2(levels, stride), const_3);
+  const __m128i c23 = _mm_min_epu8(
+      load_8bit_8x2_to_1_reg_sse2(levels + 2 * stride, stride), const_3);
+
+  const __m128i p01 = _mm_add_epi8(c01, _mm_srli_epi64(c01, 8));
+  const __m128i p23 = _mm_add_epi8(c23, _mm_srli_epi64(c23, 8));
+  const __m128i p12 = _mm_castps_si128(
+      _mm_shuffle_ps(_mm_castsi128_ps(p01), _mm_castsi128_ps(p23), 0x4e));
+
+  __m128i sum = _mm_add_epi8(_mm_srli_epi64(p01, 8), p12);
+  sum = _mm_add_epi8(sum, c23);
+  _mm_store_si128((__m128i *)coeff_contexts,
+                  finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset_0));
+
+  // default_scan_8x8 enters columns 2, 4, 6 at scan indices 5, 14, 27.
+  if (last_idx > 5) {
+    const __m128i pos_to_offset_1 = _mm_setr_epi8(
+        6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21, 21, 21);
+    const __m128i c45 = _mm_min_epu8(
+        load_8bit_8x2_to_1_reg_sse2(levels + 4 * stride, stride), const_3);
+    const __m128i p45 = _mm_add_epi8(c45, _mm_srli_epi64(c45, 8));
+    const __m128i p34 = _mm_castps_si128(
+        _mm_shuffle_ps(_mm_castsi128_ps(p23), _mm_castsi128_ps(p45), 0x4e));
+
+    sum = _mm_add_epi8(_mm_srli_epi64(p23, 8), p34);
+    sum = _mm_add_epi8(sum, c45);
+    _mm_store_si128((__m128i *)(coeff_contexts + 16),
+                    finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset_1));
+
+    if (last_idx > 14) {
+      const __m128i pos_to_offset_2 = _mm_set1_epi8(21);
+      const __m128i c67 = _mm_min_epu8(
+          load_8bit_8x2_to_1_reg_sse2(levels + 6 * stride, stride), const_3);
+      const __m128i p67 = _mm_add_epi8(c67, _mm_srli_epi64(c67, 8));
+      const __m128i p56 = _mm_castps_si128(
+          _mm_shuffle_ps(_mm_castsi128_ps(p45), _mm_castsi128_ps(p67), 0x4e));
+
+      sum = _mm_add_epi8(_mm_srli_epi64(p45, 8), p56);
+      sum = _mm_add_epi8(sum, c67);
+      _mm_store_si128((__m128i *)(coeff_contexts + 32),
+                      finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset_2));
+
+      if (last_idx > 27) {
+        sum = _mm_add_epi8(_mm_srli_epi64(p67, 8), _mm_srli_si128(p67, 8));
+        _mm_store_si128(
+            (__m128i *)(coeff_contexts + 48),
+            finish_nz_ctx_sse2(sum, const_4, zero, pos_to_offset_2));
+      }
+    }
+  }
+
+  coeff_contexts[0] = 0;
+}
+
+// Fast 16x16 2D path: shift-in 21 offset vector across columns.
+static inline void get_16x16_2d_sse2(const uint8_t *const levels,
+                                     const int last_idx,
+                                     int8_t *const coeff_contexts) {
+  const int stride = 16 + TX_PAD_HOR;
+  int col;
+  if (last_idx <= 36) {
+    col = kLastIdxToCols16n[last_idx];
+  } else if (last_idx <= 120) {
+    // Triangular diagonal bounds k*(k+1)/2: default_scan_16x16 enters cols
+    // 9, 11, 13, 14, 15 at indices 45, 66, 91, 119, 120.
+    if (last_idx <= 66) {
+      col = (last_idx <= 45) ? 9 : 11;
+    } else {
+      col = (last_idx <= 91) ? 13 : ((last_idx <= 119) ? 14 : 15);
+    }
+  } else {
+    col = 16;
+  }
+  assert(col > 0);
+
+  const __m128i const_3 = _mm_set1_epi8(3);
+  const __m128i const_4 = _mm_set1_epi8(4);
+  const __m128i zero = _mm_setzero_si128();
+  const __m128i top_21 = _mm_slli_si128(_mm_cvtsi32_si128(21), 15);
+  __m128i pos =
+      _mm_setr_epi8(0, 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21);
+
+  const __m128i c0 =
+      _mm_min_epu8(_mm_loadu_si128((const __m128i *)(levels + 0)), const_3);
+  const __m128i c1 = _mm_min_epu8(
+      _mm_loadu_si128((const __m128i *)(levels + stride)), const_3);
+  __m128i p0 = _mm_add_epi8(c0, _mm_srli_si128(c0, 1));
+  __m128i p1 = _mm_add_epi8(c1, _mm_srli_si128(c1, 1));
+
+  int8_t *cc = coeff_contexts;
+  const uint8_t *lv = levels + 2 * stride;
   do {
-    int h = height;
+    const __m128i c2 =
+        _mm_min_epu8(_mm_loadu_si128((const __m128i *)lv), const_3);
+    const __m128i p2 = _mm_add_epi8(c2, _mm_srli_si128(c2, 1));
+    __m128i sum = _mm_add_epi8(_mm_srli_si128(p0, 1), p1);
+    sum = _mm_add_epi8(sum, c2);
+    _mm_store_si128((__m128i *)cc, finish_nz_ctx_sse2(sum, const_4, zero, pos));
+    pos = _mm_or_si128(_mm_srli_si128(pos, 1), top_21);
+    p0 = p1;
+    p1 = p2;
+    lv += stride;
+    cc += 16;
+  } while (--col != 0);

-    do {
-      load_levels_16x1x5_sse2(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel_sse2(level);
-      count = _mm_add_epi8(count, pos_to_offset[0]);
-      _mm_store_si128((__m128i *)coeff_contexts, count);
-      levels += 16;
-      coeff_contexts += 16;
-      h -= 16;
-    } while (h);
-
-    pos_to_offset[0] = pos_to_offset[1];
-    pos_to_offset[1] = pos_to_offset[2];
-    levels += TX_PAD_HOR;
-  } while (--col);
+  coeff_contexts[0] = 0;
 }

-// Note: levels[] must be in the range [0, 127], inclusive.
+// Keep cold H/V paths out-of-line to reduce code bloat and register pressure
+// in the hot square 2D fast paths of av1_get_nz_map_contexts_sse2().
+static AOM_NOINLINE void get_nz_map_contexts_hv_sse2(
+    const uint8_t *const levels, const int16_t *const scan, const int last_idx,
+    const TX_SIZE tx_size, const TX_CLASS tx_class,
+    int8_t *const coeff_contexts) {
+  assert(tx_class == TX_CLASS_HORIZ || tx_class == TX_CLASS_VERT);
+  const int width = get_txb_wide(tx_size);
+  const int height = get_txb_high(tx_size);
+  const int is_hor = (tx_class == TX_CLASS_HORIZ);
+  if (height == 4) {
+    get_4_nz_map_contexts_hv(levels, width, last_idx, is_hor, coeff_contexts);
+  } else if (height == 8) {
+    get_8_coeff_contexts_hv(levels, width, last_idx, is_hor, coeff_contexts);
+  } else if (is_hor) {
+    get_16n_coeff_contexts_hor(levels, width, height, last_idx, coeff_contexts);
+  } else {
+    get_16n_coeff_contexts_ver(levels, width, height, last_idx, coeff_contexts);
+  }
+  const int n_coeffs = width * height;
+  coeff_contexts[scan[last_idx]] =
+      (int8_t)(1 + (last_idx > (n_coeffs >> 3)) + (last_idx > (n_coeffs >> 2)));
+}
+
+// Keep rectangular and >16x16 2D paths out-of-line from small square 2D paths.
+static AOM_NOINLINE void get_nz_map_contexts_rect_large_sse2(
+    const uint8_t *const levels, const int16_t *const scan, const int last_idx,
+    const TX_SIZE tx_size, int8_t *const coeff_contexts) {
+  assert(tx_size != TX_4X4 && tx_size != TX_8X8 && tx_size != TX_16X16);
+  const int width = get_txb_wide(tx_size);
+  const int height = get_txb_high(tx_size);
+  if (height == 4) {
+    get_4_nz_map_contexts_2d(levels, width, last_idx, coeff_contexts);
+  } else if (height == 8) {
+    get_8_coeff_contexts_2d(levels, width, last_idx, coeff_contexts);
+  } else {
+    const int real_width = tx_size_wide[tx_size];
+    const int real_height = tx_size_high[tx_size];
+    get_16n_all_coeff_contexts_2d(levels, real_width, real_height, width,
+                                  height, last_idx, coeff_contexts);
+  }
+  const int n_coeffs = width * height;
+  coeff_contexts[scan[last_idx]] =
+      (int8_t)(1 + (last_idx > (n_coeffs >> 3)) + (last_idx > (n_coeffs >> 2)));
+}
+
+// Note: levels[] must be in the range [0, 127], inclusive; TX_PAD_HOR padding
+// bytes after each column and TX_PAD_BOTTOM trailing columns must be zero,
+// coeff_contexts must be 16-byte aligned, and scan must match
+// av1_scan_orders[tx_size][tx_type].scan.
 void av1_get_nz_map_contexts_sse2(const uint8_t *const levels,
                                   const int16_t *const scan, const int eob,
                                   const TX_SIZE tx_size,
                                   const TX_CLASS tx_class,
                                   int8_t *const coeff_contexts) {
+  static_assert(TX_PAD_TOP == 0 && TX_PAD_HOR == 4 && TX_PAD_BOTTOM >= 4,
+                "Unexpected TX_PAD_* layout for SSE2 nz_map kernels.");
+  assert(tx_size < TX_SIZES_ALL);
+  assert(tx_class < TX_CLASSES);
+  assert(eob >= 1 && eob <= get_txb_wide(tx_size) * get_txb_high(tx_size));
+  assert(!((intptr_t)coeff_contexts & 0xf));
+
   const int last_idx = eob - 1;
-  if (!last_idx) {
+  if (last_idx == 0) {
     coeff_contexts[0] = 0;
     return;
   }
+  if (last_idx == 1 && tx_class == TX_CLASS_2D) {
+    assert(scan[0] == 0);
+    coeff_contexts[0] = 0;
+    coeff_contexts[scan[1]] = 1;
+    return;
+  }

-  const int real_width = tx_size_wide[tx_size];
-  const int real_height = tx_size_high[tx_size];
-  const int width = get_txb_wide(tx_size);
-  const int height = get_txb_high(tx_size);
-  const int stride = height + TX_PAD_HOR;
-  ptrdiff_t offsets[3];
-
-  /* coeff_contexts must be 16 byte aligned. */
-  assert(!((intptr_t)coeff_contexts & 0xf));
-
-  if (tx_class == TX_CLASS_2D) {
-    offsets[0] = 0 * stride + 2;
-    offsets[1] = 1 * stride + 1;
-    offsets[2] = 2 * stride + 0;
-
-    if (height == 4) {
-      get_4_nz_map_contexts_2d(levels, width, offsets, coeff_contexts);
-    } else if (height == 8) {
-      get_8_coeff_contexts_2d(levels, width, offsets, coeff_contexts);
-    } else if (height == 16) {
-      get_16n_coeff_contexts_2d(levels, real_width, real_height, width, height,
-                                offsets, coeff_contexts);
-    } else {
-      get_16n_coeff_contexts_2d(levels, real_width, real_height, width, height,
-                                offsets, coeff_contexts);
-    }
-  } else if (tx_class == TX_CLASS_HORIZ) {
-    offsets[0] = 2 * stride;
-    offsets[1] = 3 * stride;
-    offsets[2] = 4 * stride;
-    if (height == 4) {
-      get_4_nz_map_contexts_hor(levels, width, offsets, coeff_contexts);
-    } else if (height == 8) {
-      get_8_coeff_contexts_hor(levels, width, offsets, coeff_contexts);
-    } else {
-      get_16n_coeff_contexts_hor(levels, width, height, offsets,
-                                 coeff_contexts);
-    }
-  } else {  // TX_CLASS_VERT
-    offsets[0] = 2;
-    offsets[1] = 3;
-    offsets[2] = 4;
-    if (height == 4) {
-      get_4_nz_map_contexts_ver(levels, width, offsets, coeff_contexts);
-    } else if (height == 8) {
-      get_8_coeff_contexts_ver(levels, width, offsets, coeff_contexts);
-    } else {
-      get_16n_coeff_contexts_ver(levels, width, height, offsets,
-                                 coeff_contexts);
-    }
+  if (tx_class != TX_CLASS_2D) {
+    get_nz_map_contexts_hv_sse2(levels, scan, last_idx, tx_size, tx_class,
+                                coeff_contexts);
+    return;
   }

-  const int bhl = get_txb_bhl(tx_size);
-  const int pos = scan[last_idx];
-  if (last_idx <= (width << bhl) / 8)
-    coeff_contexts[pos] = 1;
-  else if (last_idx <= (width << bhl) / 4)
-    coeff_contexts[pos] = 2;
-  else
-    coeff_contexts[pos] = 3;
+  if (tx_size == TX_4X4) {
+    get_4x4_2d_sse2(levels, coeff_contexts);
+    coeff_contexts[scan[last_idx]] =
+        (int8_t)(1 + (last_idx > 2) + (last_idx > 4));
+  } else if (tx_size == TX_8X8) {
+    get_8x8_2d_sse2(levels, last_idx, coeff_contexts);
+    coeff_contexts[scan[last_idx]] =
+        (int8_t)(1 + (last_idx > 8) + (last_idx > 16));
+  } else if (tx_size == TX_16X16) {
+    get_16x16_2d_sse2(levels, last_idx, coeff_contexts);
+    coeff_contexts[scan[last_idx]] =
+        (int8_t)(1 + (last_idx > 32) + (last_idx > 64));
+  } else {
+    get_nz_map_contexts_rect_large_sse2(levels, scan, last_idx, tx_size,
+                                        coeff_contexts);
+  }
 }