Commit 0f5ce56c25 for aom

commit 0f5ce56c2519c2fe7d2bcd170cf2fd69d99914e5
Author: Frank Galligan <fgalligan@google.com>
Date:   Tue Nov 17 06:28:43 2026 -0800

    av1,neon: Optimize txb init_levels & nz_map ctx

    Unroll av1_txb_init_levels_neon across block heights using
    saturating vqmovn_s32 loads (load_tran_low_to_s16q_sat),
    alignment-safe store_u8_4x1, and zero_bottom_pad_neon trailing pad
    stores (asserting TX_PAD_* geometry).

    Replace window reloads in get_4_nz_map_contexts_{2d,ver,hor},
    get_8_coeff_contexts_{2d,ver,hor}, and
    get_16n_coeff_contexts_{2d,ver,hor} with sliding NEON register shifts
    and extract get_4x4_nz_map_contexts_2d for the peeled TX_4X4 2D path.

    Prune column loops with eob-bounded n_cols (nz_diag_n_cols_2d for
    TX_CLASS_2D and closed-form column bounds for TX_CLASS_HORIZ/VERT):
    contexts outside reachable columns remain uninitialized, matching
    av1_get_nz_map_contexts_c. Each of the three optimizations
    independently improves Pixel 9 Cortex-A720 realtime encode time.

    Pixel 9 Tensor G4 Cortex-A720 (1T) kernel speedups:
    - EncodeTxbTest.DISABLED_SpeedTestGetNzMapContexts (full-eob DCT_DCT):
      4x4 -63.95% (2.77x), 8x8 -39.34% (1.65x), 16x16 -30.71% (1.44x),
      32x32 -30.75% (1.44x), 64x64 -30.68% (1.44x), -22.7%..-70.2% all 19.
    - EncodeTxbInitLevelTest.DISABLED_Speed: 4x4 -52.73% (2.12x),
      8x8 -33.31% (1.50x), 16x16 -15.59% (1.18x), 32x32 -8.01%..-9.00%,
      -6.18%..-52.73% across all 19 cases.

    Note: saturating tran_low_t via vqmovn_s32 also fixes a pre-existing
    high-bitdepth truncation bug in av1_txb_init_levels_neon for |coeff| >
    INT16_MAX (matching av1_txb_init_levels_c across all 8/10/12-bit
    inputs).

    Speedup (Pixel 9 Tensor G4 Cortex-A720, 1T, realtime 768x1024 intra,
    bit-exact): -2.07% (speed 6), -1.18% (speed 7) encode time
    (-1.65% overall).

    TAG=agy
    CONV=bc5228f2-7fa6-4522-aad6-34935d739e28
    Change-Id: I529c2cef913155009880dbe6ed38a62f79572d09

diff --git a/av1/encoder/arm/encodetxb_neon.c b/av1/encoder/arm/encodetxb_neon.c
index 1b5a656c96..6b9c8a157d 100644
--- a/av1/encoder/arm/encodetxb_neon.c
+++ b/av1/encoder/arm/encodetxb_neon.c
@@ -11,33 +11,75 @@

 #include <arm_neon.h>
 #include <assert.h>
-#include <math.h>
-#include <string.h>
+#include <stddef.h>
+#include <stdint.h>

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

+#include "aom_dsp/aom_dsp_common.h"
 #include "aom_dsp/arm/mem_neon.h"
+#include "aom_ports/mem.h"
 #include "av1/common/txb_common.h"
 #include "av1/encoder/encodetxb.h"

+static inline void zero_bottom_pad_neon(uint8_t *const pad, const int height) {
+  const uint8x16_t zeros_u8 = vdupq_n_u8(0);
+  if (height == 4) {
+    // TX_PAD_BOTTOM * (4 + TX_PAD_HOR) + TX_PAD_END == 48
+    vst1q_u8(pad, zeros_u8);
+    vst1q_u8(pad + 16, zeros_u8);
+    vst1q_u8(pad + 32, zeros_u8);
+  } else if (height == 8) {
+    // TX_PAD_BOTTOM * (8 + TX_PAD_HOR) + TX_PAD_END == 64
+    vst1q_u8(pad, zeros_u8);
+    vst1q_u8(pad + 16, zeros_u8);
+    vst1q_u8(pad + 32, zeros_u8);
+    vst1q_u8(pad + 48, zeros_u8);
+  } else if (height == 16) {
+    // TX_PAD_BOTTOM * (16 + TX_PAD_HOR) + TX_PAD_END == 96
+    vst1q_u8(pad, zeros_u8);
+    vst1q_u8(pad + 16, zeros_u8);
+    vst1q_u8(pad + 32, zeros_u8);
+    vst1q_u8(pad + 48, zeros_u8);
+    vst1q_u8(pad + 64, zeros_u8);
+    vst1q_u8(pad + 80, zeros_u8);
+  } else {
+    // TX_PAD_BOTTOM * (32 + TX_PAD_HOR) + TX_PAD_END == 160
+    assert(height == 32);
+    vst1q_u8(pad, zeros_u8);
+    vst1q_u8(pad + 16, zeros_u8);
+    vst1q_u8(pad + 32, zeros_u8);
+    vst1q_u8(pad + 48, zeros_u8);
+    vst1q_u8(pad + 64, zeros_u8);
+    vst1q_u8(pad + 80, zeros_u8);
+    vst1q_u8(pad + 96, zeros_u8);
+    vst1q_u8(pad + 112, zeros_u8);
+    vst1q_u8(pad + 128, zeros_u8);
+    vst1q_u8(pad + 144, zeros_u8);
+  }
+}
+
+static inline int16x8_t load_tran_low_to_s16q_sat(const tran_low_t *const buf) {
+  return vcombine_s16(vqmovn_s32(vld1q_s32(buf)),
+                      vqmovn_s32(vld1q_s32(buf + 4)));
+}
+
 void av1_txb_init_levels_neon(const tran_low_t *const coeff, const int width,
                               const int height, uint8_t *const levels) {
-  const int stride = height + TX_PAD_HOR;
-  memset(levels - TX_PAD_TOP * stride, 0,
-         sizeof(*levels) * TX_PAD_TOP * stride);
-  memset(levels + stride * width, 0,
-         sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
+  assert(TX_PAD_TOP == 0 && TX_PAD_HOR == 4 && TX_PAD_BOTTOM == 4 &&
+         TX_PAD_END == 16);
+  assert(width > 0);

   const int32x4_t zeros = vdupq_n_s32(0);
-  int i = 0;
   uint8_t *ls = levels;
   const tran_low_t *cf = coeff;
   if (height == 4) {
+    assert(!(width % 2));
+    const int stride = 4 + TX_PAD_HOR;
+    int col = width;
     do {
-      const int32x4_t coeffA = vld1q_s32(cf);
-      const int32x4_t coeffB = vld1q_s32(cf + height);
-      const int16x8_t coeffAB =
-          vcombine_s16(vqmovn_s32(coeffA), vqmovn_s32(coeffB));
+      const int16x8_t coeffAB = load_tran_low_to_s16q_sat(cf);
       const int16x8_t absAB = vqabsq_s16(coeffAB);
       const int8x8_t absABs = vqmovn_s16(absAB);
 #if AOM_ARCH_AARCH64
@@ -53,38 +95,70 @@ void av1_txb_init_levels_neon(const tran_low_t *const coeff, const int width,
 #endif
       vst1q_u8(ls, lsAB);
       ls += (stride << 1);
-      cf += (height << 1);
-      i += 2;
-    } while (i < width);
+      cf += 8;
+      col -= 2;
+    } while (col != 0);
+    zero_bottom_pad_neon(ls, 4);
   } else if (height == 8) {
+    assert(!(width % 2));
+    const int stride = 8 + TX_PAD_HOR;
+    int col = width;
     do {
-      const int16x8_t coeffAB = load_tran_low_to_s16q(cf);
+      const int16x8_t coeff0 = load_tran_low_to_s16q_sat(cf);
+      const int16x8_t coeff1 = load_tran_low_to_s16q_sat(cf + 8);
+      const int16x8_t abs0 = vqabsq_s16(coeff0);
+      const int16x8_t abs1 = vqabsq_s16(coeff1);
+      const uint8x16_t abs0_u8 = vreinterpretq_u8_s8(vcombine_s8(
+          vqmovn_s16(abs0), vreinterpret_s8_s32(vget_low_s32(zeros))));
+      const uint8x16_t abs1_u8 = vreinterpretq_u8_s8(vcombine_s8(
+          vqmovn_s16(abs1), vreinterpret_s8_s32(vget_low_s32(zeros))));
+      vst1q_u8(ls, abs0_u8);
+      vst1q_u8(ls + stride, abs1_u8);
+      ls += (stride << 1);
+      cf += 16;
+      col -= 2;
+    } while (col != 0);
+    zero_bottom_pad_neon(ls, 8);
+  } else if (height == 16) {
+    const int stride = 16 + TX_PAD_HOR;
+    int col = width;
+    do {
+      const int16x8_t coeffAB = load_tran_low_to_s16q_sat(cf);
+      const int16x8_t coeffCD = load_tran_low_to_s16q_sat(cf + 8);
       const int16x8_t absAB = vqabsq_s16(coeffAB);
-      const uint8x16_t absAB8 = vreinterpretq_u8_s8(vcombine_s8(
-          vqmovn_s16(absAB), vreinterpret_s8_s32(vget_low_s32(zeros))));
-      vst1q_u8(ls, absAB8);
+      const int16x8_t absCD = vqabsq_s16(coeffCD);
+      const uint8x16_t absABCD = vreinterpretq_u8_s8(
+          vcombine_s8(vqmovn_s16(absAB), vqmovn_s16(absCD)));
+      vst1q_u8(ls, absABCD);
+      store_u8_4x1(ls + 16, vreinterpret_u8_s32(vget_low_s32(zeros)));
+      cf += 16;
       ls += stride;
-      cf += height;
-      i += 1;
-    } while (i < width);
+    } while (--col != 0);
+    zero_bottom_pad_neon(ls, 16);
   } else {
+    assert(height == 32);
+    const int stride = 32 + TX_PAD_HOR;
+    int col = width;
     do {
-      int j = 0;
-      do {
-        const int16x8_t coeffAB = load_tran_low_to_s16q(cf);
-        const int16x8_t coeffCD = load_tran_low_to_s16q(cf + 8);
-        const int16x8_t absAB = vqabsq_s16(coeffAB);
-        const int16x8_t absCD = vqabsq_s16(coeffCD);
-        const uint8x16_t absABCD = vreinterpretq_u8_s8(
-            vcombine_s8(vqmovn_s16(absAB), vqmovn_s16(absCD)));
-        vst1q_u8((ls + j), absABCD);
-        j += 16;
-        cf += 16;
-      } while (j < height);
-      memset(ls + height, 0, sizeof(*levels) * TX_PAD_HOR);
+      const int16x8_t coeff0 = load_tran_low_to_s16q_sat(cf);
+      const int16x8_t coeff1 = load_tran_low_to_s16q_sat(cf + 8);
+      const int16x8_t coeff2 = load_tran_low_to_s16q_sat(cf + 16);
+      const int16x8_t coeff3 = load_tran_low_to_s16q_sat(cf + 24);
+      const int16x8_t abs0 = vqabsq_s16(coeff0);
+      const int16x8_t abs1 = vqabsq_s16(coeff1);
+      const int16x8_t abs2 = vqabsq_s16(coeff2);
+      const int16x8_t abs3 = vqabsq_s16(coeff3);
+      const uint8x16_t abs01 =
+          vreinterpretq_u8_s8(vcombine_s8(vqmovn_s16(abs0), vqmovn_s16(abs1)));
+      const uint8x16_t abs23 =
+          vreinterpretq_u8_s8(vcombine_s8(vqmovn_s16(abs2), vqmovn_s16(abs3)));
+      vst1q_u8(ls, abs01);
+      vst1q_u8(ls + 16, abs23);
+      store_u8_4x1(ls + 32, vreinterpret_u8_s32(vget_low_s32(zeros)));
+      cf += 32;
       ls += stride;
-      i += 1;
-    } while (i < width);
+    } while (--col != 0);
+    zero_bottom_pad_neon(ls, 32);
   }
 }

@@ -113,22 +187,24 @@ static const DECLARE_ALIGNED(16, uint8_t, c_4_po_hor[16]) = {
   SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10
 };

-// get_8_coeff_contexts_2d coefficients:
-// if (width == 8)
-static const DECLARE_ALIGNED(16, uint8_t, c_8_po_2d_8[2][16]) = {
-  { 0, 1, 6, 6, 21, 21, 21, 21, 1, 6, 6, 21, 21, 21, 21, 21 },
-  { 6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21, 21, 21 }
-};
-// if (width < 8)
-static const DECLARE_ALIGNED(16, uint8_t, c_8_po_2d_l[2][16]) = {
-  { 0, 11, 6, 6, 21, 21, 21, 21, 11, 11, 6, 21, 21, 21, 21, 21 },
-  { 11, 11, 21, 21, 21, 21, 21, 21, 11, 11, 21, 21, 21, 21, 21, 21 }
-};
-
-// if (width > 8)
-static const DECLARE_ALIGNED(16, uint8_t, c_8_po_2d_g[2][16]) = {
-  { 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
-  { 6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21, 21, 21 }
+// get_8_coeff_contexts_2d coefficients, indexed by
+// (width >= 8) + (width > 8): 0: width < 8, 1: width == 8, 2: width > 8.
+static const DECLARE_ALIGNED(16, uint8_t, c_8_po_2d_all[3][2][16]) = {
+  // width < 8
+  {
+      { 0, 11, 6, 6, 21, 21, 21, 21, 11, 11, 6, 21, 21, 21, 21, 21 },
+      { 11, 11, 21, 21, 21, 21, 21, 21, 11, 11, 21, 21, 21, 21, 21, 21 },
+  },
+  // width == 8
+  {
+      { 0, 1, 6, 6, 21, 21, 21, 21, 1, 6, 6, 21, 21, 21, 21, 21 },
+      { 6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21, 21, 21 },
+  },
+  // width > 8
+  {
+      { 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
+      { 6, 6, 21, 21, 21, 21, 21, 21, 6, 21, 21, 21, 21, 21, 21, 21 },
+  },
 };

 // get_4_nz_map_contexts_ver coefficients:
@@ -143,27 +219,35 @@ static const DECLARE_ALIGNED(16, uint8_t, c_8_po_ver[16]) = {
   SIG_COEF_CONTEXTS_2D + 10, SIG_COEF_CONTEXTS_2D + 10
 };

-// get_16n_coeff_contexts_2d coefficients:
-// real_width == real_height
-static const DECLARE_ALIGNED(16, uint8_t, c_16_po_2d_e[4][16]) = {
-  { 0, 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 }
-};
-
-// real_width < real_height
-static const DECLARE_ALIGNED(16, uint8_t, c_16_po_2d_g[3][16]) = {
-  { 0, 11, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 11, 11, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 }
-};
-
-// real_width > real_height
-static const DECLARE_ALIGNED(16, uint8_t, c_16_po_2d_l[3][16]) = {
-  { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
-  { 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
-  { 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 }
+// get_16n_coeff_contexts_2d coefficients, indexed by
+// (real_width >= real_height) + (real_width > real_height):
+// 0: real_width < real_height, 1: real_width == real_height,
+// 2: real_width > real_height.
+static const DECLARE_ALIGNED(16, uint8_t, c_16_po_2d_all[3][5][16]) = {
+  // real_width < real_height
+  {
+      { 0, 11, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 11, 11, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 11, 11, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+  },
+  // real_width == real_height
+  {
+      { 0, 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 1, 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+  },
+  // real_width > real_height
+  {
+      { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
+      { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 },
+      { 6, 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 6, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+      { 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21 },
+  },
 };

 // get_16n_coeff_contexts_hor coefficients:
@@ -180,39 +264,10 @@ static const DECLARE_ALIGNED(16, uint8_t, c_16_po_ver[16]) = {

 // end of coefficients declaration area

+#if !AOM_ARCH_AARCH64
 static inline uint8x16_t load_8bit_4x4_to_1_reg(const uint8_t *const src,
                                                 const int byte_stride) {
-#if AOM_ARCH_AARCH64
-  uint32x4_t v_data = vld1q_u32((uint32_t *)src);
-  v_data = vld1q_lane_u32((uint32_t *)(src + 1 * byte_stride), v_data, 1);
-  v_data = vld1q_lane_u32((uint32_t *)(src + 2 * byte_stride), v_data, 2);
-  v_data = vld1q_lane_u32((uint32_t *)(src + 3 * byte_stride), v_data, 3);
-
-  return vreinterpretq_u8_u32(v_data);
-#else
   return load_unaligned_u8q(src, byte_stride);
-#endif
-}
-
-static inline uint8x16_t load_8bit_8x2_to_1_reg(const uint8_t *const src,
-                                                const int byte_stride) {
-#if AOM_ARCH_AARCH64
-  uint64x2_t v_data = vld1q_u64((uint64_t *)src);
-  v_data = vld1q_lane_u64((uint64_t *)(src + 1 * byte_stride), v_data, 1);
-
-  return vreinterpretq_u8_u64(v_data);
-#else
-  uint8x8_t v_data_low = vld1_u8(src);
-  uint8x8_t v_data_high = vld1_u8(src + byte_stride);
-
-  return vcombine_u8(v_data_low, v_data_high);
-#endif
-}
-
-static inline uint8x16_t load_8bit_16x1_to_1_reg(const uint8_t *const src,
-                                                 const int byte_stride) {
-  (void)byte_stride;
-  return vld1q_u8(src);
 }

 static inline void load_levels_4x4x5(const uint8_t *const src, const int stride,
@@ -224,28 +279,13 @@ static inline void load_levels_4x4x5(const uint8_t *const src, const int stride,
   level[3] = load_8bit_4x4_to_1_reg(&src[offsets[1]], stride);
   level[4] = load_8bit_4x4_to_1_reg(&src[offsets[2]], stride);
 }
+#endif  // !AOM_ARCH_AARCH64

-static inline void load_levels_8x2x5(const uint8_t *const src, const int stride,
-                                     const ptrdiff_t *const offsets,
-                                     uint8x16_t *const level) {
-  level[0] = load_8bit_8x2_to_1_reg(&src[1], stride);
-  level[1] = load_8bit_8x2_to_1_reg(&src[stride], stride);
-  level[2] = load_8bit_8x2_to_1_reg(&src[offsets[0]], stride);
-  level[3] = load_8bit_8x2_to_1_reg(&src[offsets[1]], stride);
-  level[4] = load_8bit_8x2_to_1_reg(&src[offsets[2]], stride);
-}
-
-static inline void load_levels_16x1x5(const uint8_t *const src,
-                                      const int stride,
-                                      const ptrdiff_t *const offsets,
-                                      uint8x16_t *const level) {
-  level[0] = load_8bit_16x1_to_1_reg(&src[1], stride);
-  level[1] = load_8bit_16x1_to_1_reg(&src[stride], stride);
-  level[2] = load_8bit_16x1_to_1_reg(&src[offsets[0]], stride);
-  level[3] = load_8bit_16x1_to_1_reg(&src[offsets[1]], stride);
-  level[4] = load_8bit_16x1_to_1_reg(&src[offsets[2]], stride);
+static inline uint8x16_t neon_vshr1_2d_u8x16(const uint8x16_t v) {
+  return vreinterpretq_u8_u64(vshrq_n_u64(vreinterpretq_u64_u8(v), 8));
 }

+#if !AOM_ARCH_AARCH64
 static inline uint8x16_t get_coeff_contexts_kernel(uint8x16_t *const level) {
   const uint8x16_t const_3 = vdupq_n_u8(3);
   const uint8x16_t const_4 = vdupq_n_u8(4);
@@ -265,378 +305,784 @@ static inline uint8x16_t get_coeff_contexts_kernel(uint8x16_t *const level) {
   count = vminq_u8(count, const_4);
   return count;
 }
+#endif  // !AOM_ARCH_AARCH64

-static inline void get_4_nz_map_contexts_2d(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
-                                            uint8_t *const coeff_contexts) {
+static AOM_FORCE_INLINE void get_4x4_nz_map_contexts_2d(
+    const uint8_t *const levels, uint8_t *const coeff_contexts) {
+  const uint8x16_t pos_to_offset = vld1q_u8(c_4_po_2d[0]);
+  uint8x16_t count;
+#if AOM_ARCH_AARCH64
+  const uint8x16_t v_0 = vld1q_u8(levels);
+  const uint8x16_t v_16 = vld1q_u8(levels + 16);
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  const uint32x4_t lo = vreinterpretq_u32_u8(
+      vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(vreinterpretq_u32_u8(v_0),
+                                               vreinterpretq_u32_u8(v_16))),
+               const_3));
+  const uint32x4_t zeros32 = vdupq_n_u32(0);
+  const uint32x4_t l0 = vshrq_n_u32(lo, 8);
+  const uint32x4_t l2 = vshrq_n_u32(lo, 16);
+  const uint32x4_t l1 = vextq_u32(lo, zeros32, 1);
+  const uint32x4_t l4 = vextq_u32(lo, zeros32, 2);
+  const uint32x4_t l3 = vextq_u32(l0, zeros32, 1);
+  count = vaddq_u8(
+      vreinterpretq_u8_u32(l0),
+      vaddq_u8(vaddq_u8(vreinterpretq_u8_u32(l1), vreinterpretq_u8_u32(l2)),
+               vaddq_u8(vreinterpretq_u8_u32(l3), vreinterpretq_u8_u32(l4))));
+  count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+#else
   const int stride = 4 + TX_PAD_HOR;
-  const uint8x16_t pos_to_offset_large = vdupq_n_u8(21);
-
-  uint8x16_t pos_to_offset =
-      (width == 4) ? vld1q_u8(c_4_po_2d[0]) : vld1q_u8(c_4_po_2d[1]);
+  const ptrdiff_t offsets[3] = { 0 * stride + 2, 1 * stride + 1,
+                                 2 * stride + 0 };
+  uint8x16_t level[5];
+  load_levels_4x4x5(levels, stride, offsets, level);
+  count = get_coeff_contexts_kernel(level);
+#endif
+  count = vaddq_u8(count, pos_to_offset);
+  vst1q_u8(coeff_contexts, count);
+  coeff_contexts[0] = 0;
+}

+static AOM_FORCE_INLINE void get_4_nz_map_contexts_2d(
+    const uint8_t *levels, const int n_cols, uint8_t *const coeff_contexts) {
+  const int stride = 4 + TX_PAD_HOR;
+  const uint8x16_t pos_to_offset_large = vdupq_n_u8(21);
+  // TX_4X4 is peeled into get_4x4_nz_map_contexts_2d(), so width > 4 here
+  // (n_cols may still be 4 for sparse 8x4/16x4 blocks).
+  uint8x16_t pos_to_offset = vld1q_u8(c_4_po_2d[1]);
   uint8x16_t count;
-  uint8x16_t level[5];
   uint8_t *cc = coeff_contexts;
+#if AOM_ARCH_AARCH64
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  uint32x4_t v_0 = vreinterpretq_u32_u8(vld1q_u8(levels));
+#else
+  uint8x16_t level[5];
+  const ptrdiff_t offsets[3] = { 0 * stride + 2, 1 * stride + 1,
+                                 2 * stride + 0 };
+#endif

-  assert(!(width % 4));
+  assert(n_cols > 0 && !(n_cols % 4));

-  int col = width;
+  int col = n_cols;
   do {
+#if AOM_ARCH_AARCH64
+    const uint32x4_t v_16 = vreinterpretq_u32_u8(vld1q_u8(levels + 16));
+    const uint32x4_t v_32 = vreinterpretq_u32_u8(vld1q_u8(levels + 32));
+    const uint32x4_t lo = vreinterpretq_u32_u8(
+        vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(v_0, v_16)), const_3));
+    const uint32x4_t lo32 = vreinterpretq_u32_u8(
+        vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(v_32, v_32)), const_3));
+    const uint32x4_t l0 = vshrq_n_u32(lo, 8);
+    const uint32x4_t l2 = vshrq_n_u32(lo, 16);
+    const uint32x4_t l1 = vextq_u32(lo, lo32, 1);
+    const uint32x4_t l4 = vextq_u32(lo, lo32, 2);
+    const uint32x4_t l3 = vextq_u32(l0, vshrq_n_u32(lo32, 8), 1);
+    count = vaddq_u8(
+        vreinterpretq_u8_u32(l0),
+        vaddq_u8(vaddq_u8(vreinterpretq_u8_u32(l1), vreinterpretq_u8_u32(l2)),
+                 vaddq_u8(vreinterpretq_u8_u32(l3), vreinterpretq_u8_u32(l4))));
+    count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+    v_0 = v_32;
+#else
     load_levels_4x4x5(levels, stride, offsets, level);
     count = get_coeff_contexts_kernel(level);
+#endif
     count = vaddq_u8(count, pos_to_offset);
     vst1q_u8(cc, count);
     pos_to_offset = pos_to_offset_large;
     levels += 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,
-                                             uint8_t *coeff_contexts) {
+static AOM_FORCE_INLINE void get_4_nz_map_contexts_ver(
+    const uint8_t *levels, const int n_cols, uint8_t *coeff_contexts) {
   const int stride = 4 + TX_PAD_HOR;
-
   const uint8x16_t pos_to_offset =
       vreinterpretq_u8_u32(vdupq_n_u32(SIG_COEF_CONTEXTS_2D_X4_051010));
-
   uint8x16_t count;
+#if AOM_ARCH_AARCH64
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  uint32x4_t v_0 = vreinterpretq_u32_u8(vld1q_u8(levels));
+#else
   uint8x16_t level[5];
+  const ptrdiff_t offsets[3] = { 2, 3, 4 };
+#endif

-  assert(!(width % 4));
+  assert(n_cols > 0 && !(n_cols % 4));

-  int col = width;
+  int col = n_cols;
   do {
+#if AOM_ARCH_AARCH64
+    const uint32x4_t v_16 = vreinterpretq_u32_u8(vld1q_u8(levels + 16));
+    const uint32x4_t v_32 =
+        vreinterpretq_u32_u8(vminq_u8(vld1q_u8(levels + 32), const_3));
+    const uint32x4_t lo = vreinterpretq_u32_u8(
+        vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(v_0, v_16)), const_3));
+    const uint32x4_t l0 = vshrq_n_u32(lo, 8);
+    const uint32x4_t l1 = vextq_u32(lo, v_32, 1);
+    const uint32x4_t l2 = vshrq_n_u32(lo, 16);
+    const uint32x4_t l3 = vshrq_n_u32(lo, 24);
+    count =
+        vaddq_u8(vaddq_u8(vreinterpretq_u8_u32(l0), vreinterpretq_u8_u32(l1)),
+                 vaddq_u8(vreinterpretq_u8_u32(l2), vreinterpretq_u8_u32(l3)));
+    count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+    v_0 = v_32;
+#else
     load_levels_4x4x5(levels, stride, offsets, level);
     count = get_coeff_contexts_kernel(level);
+#endif
     count = vaddq_u8(count, pos_to_offset);
     vst1q_u8(coeff_contexts, count);
     levels += 4 * stride;
     coeff_contexts += 16;
     col -= 4;
-  } while (col);
+  } while (col != 0);
 }

-static inline void get_4_nz_map_contexts_hor(const uint8_t *levels,
-                                             const int width,
-                                             const ptrdiff_t *const offsets,
-                                             uint8_t *coeff_contexts) {
+static AOM_FORCE_INLINE void get_4_nz_map_contexts_hor(
+    const uint8_t *levels, const int n_cols, uint8_t *coeff_contexts) {
   const int stride = 4 + TX_PAD_HOR;
   const uint8x16_t pos_to_offset_large = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 10);
-
   uint8x16_t pos_to_offset = vld1q_u8(c_4_po_hor);
-
   uint8x16_t count;
+#if AOM_ARCH_AARCH64
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  const uint32x4_t v_0 = vreinterpretq_u32_u8(vld1q_u8(levels));
+  const uint32x4_t v_16 = vreinterpretq_u32_u8(vld1q_u8(levels + 16));
+  uint32x4_t lo0 = vreinterpretq_u32_u8(
+      vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(v_0, v_16)), const_3));
+#else
   uint8x16_t level[5];
+  const ptrdiff_t offsets[3] = { 2 * stride, 3 * stride, 4 * stride };
+#endif

-  assert(!(width % 4));
+  assert(n_cols > 0 && !(n_cols % 4));

-  int col = width;
+  int col = n_cols;
   do {
+#if AOM_ARCH_AARCH64
+    const uint32x4_t v_32 = vreinterpretq_u32_u8(vld1q_u8(levels + 32));
+    const uint32x4_t v_48 = vreinterpretq_u32_u8(vld1q_u8(levels + 48));
+    const uint32x4_t lo1 = vreinterpretq_u32_u8(
+        vminq_u8(vreinterpretq_u8_u32(vuzp1q_u32(v_32, v_48)), const_3));
+    const uint32x4_t l0 = vshrq_n_u32(lo0, 8);
+    const uint32x4_t l1 = vextq_u32(lo0, lo1, 1);
+    const uint32x4_t l2 = vextq_u32(lo0, lo1, 2);
+    const uint32x4_t l3 = vextq_u32(lo0, lo1, 3);
+    count = vaddq_u8(
+        vreinterpretq_u8_u32(l0),
+        vaddq_u8(
+            vaddq_u8(vreinterpretq_u8_u32(l1), vreinterpretq_u8_u32(l2)),
+            vaddq_u8(vreinterpretq_u8_u32(l3), vreinterpretq_u8_u32(lo1))));
+    count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+    lo0 = lo1;
+#else
     load_levels_4x4x5(levels, stride, offsets, level);
     count = get_coeff_contexts_kernel(level);
+#endif
     count = vaddq_u8(count, pos_to_offset);
     vst1q_u8(coeff_contexts, count);
     pos_to_offset = pos_to_offset_large;
     levels += 4 * stride;
     coeff_contexts += 16;
     col -= 4;
-  } while (col);
+  } while (col != 0);
 }

-static inline void get_8_coeff_contexts_2d(const uint8_t *levels,
-                                           const int width,
-                                           const ptrdiff_t *const offsets,
-                                           uint8_t *coeff_contexts) {
+static AOM_FORCE_INLINE void get_8_coeff_contexts_2d(const uint8_t *levels,
+                                                     const int width,
+                                                     const int n_cols,
+                                                     uint8_t *coeff_contexts) {
   const int stride = 8 + TX_PAD_HOR;
   uint8_t *cc = coeff_contexts;
-  uint8x16_t count;
-  uint8x16_t level[5];
   uint8x16_t pos_to_offset[3];

-  assert(!(width % 2));
+  assert(n_cols > 0 && !(n_cols % 2));

-  if (width == 8) {
-    pos_to_offset[0] = vld1q_u8(c_8_po_2d_8[0]);
-    pos_to_offset[1] = vld1q_u8(c_8_po_2d_8[1]);
-  } else if (width < 8) {
-    pos_to_offset[0] = vld1q_u8(c_8_po_2d_l[0]);
-    pos_to_offset[1] = vld1q_u8(c_8_po_2d_l[1]);
-  } else {
-    pos_to_offset[0] = vld1q_u8(c_8_po_2d_g[0]);
-    pos_to_offset[1] = vld1q_u8(c_8_po_2d_g[1]);
-  }
+  const int w_idx = (width >= 8) + (width > 8);
+  pos_to_offset[0] = vld1q_u8(c_8_po_2d_all[w_idx][0]);
+  pos_to_offset[1] = vld1q_u8(c_8_po_2d_all[w_idx][1]);
   pos_to_offset[2] = vdupq_n_u8(21);

-  int col = width;
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  const uint8x16_t v01 =
+      vminq_u8(vcombine_u8(vld1_u8(levels), vld1_u8(levels + stride)), const_3);
+  const uint8x16_t s01_01 = vaddq_u8(v01, neon_vshr1_2d_u8x16(v01));
+  uint8x16_t s12_01 = neon_vshr1_2d_u8x16(s01_01);
+  uint8x16_t prev_s01 = s01_01;
+
+  int col = n_cols;
   do {
-    load_levels_8x2x5(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel(level);
-    count = vaddq_u8(count, pos_to_offset[0]);
-    vst1q_u8(cc, count);
+    const uint8x16_t v23 = vminq_u8(
+        vcombine_u8(vld1_u8(levels + 2 * stride), vld1_u8(levels + 3 * stride)),
+        const_3);
+    const uint8x16_t s01_23 = vaddq_u8(v23, neon_vshr1_2d_u8x16(v23));
+    const uint8x16_t s01_12 = vextq_u8(prev_s01, s01_23, 8);
+    const uint8x16_t sum = vaddq_u8(s12_01, vaddq_u8(s01_12, v23));
+    const uint8x16_t count = vminq_u8(vrshrq_n_u8(sum, 1), const_4);
+    vst1q_u8(cc, vaddq_u8(count, pos_to_offset[0]));
+    s12_01 = neon_vshr1_2d_u8x16(s01_23);
+    prev_s01 = s01_23;
     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);
+  } while (col != 0);

   coeff_contexts[0] = 0;
 }

-static inline void get_8_coeff_contexts_ver(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
-                                            uint8_t *coeff_contexts) {
+static AOM_FORCE_INLINE void get_8_coeff_contexts_ver(const uint8_t *levels,
+                                                      const int n_cols,
+                                                      uint8_t *coeff_contexts) {
   const int stride = 8 + TX_PAD_HOR;
-
   const uint8x16_t pos_to_offset = vld1q_u8(c_8_po_ver);
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);

-  uint8x16_t count;
-  uint8x16_t level[5];
-
-  assert(!(width % 2));
+  assert(n_cols > 0 && !(n_cols % 2));

-  int col = width;
+  uint8x16_t v01 =
+      vminq_u8(vcombine_u8(vld1_u8(levels), vld1_u8(levels + stride)), const_3);
+  int col = n_cols;
   do {
-    load_levels_8x2x5(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel(level);
-    count = vaddq_u8(count, pos_to_offset);
-    vst1q_u8(coeff_contexts, count);
+    const uint8x16_t v23 = vminq_u8(
+        vcombine_u8(vld1_u8(levels + 2 * stride), vld1_u8(levels + 3 * stride)),
+        const_3);
+    const uint8x16_t v12 = vextq_u8(v01, v23, 8);
+    const uint8x16_t r1 = neon_vshr1_2d_u8x16(v01);
+    const uint8x16_t r2 = neon_vshr1_2d_u8x16(r1);
+    const uint8x16_t s12 = vaddq_u8(r1, r2);
+    const uint8x16_t s34 =
+        vreinterpretq_u8_u64(vshrq_n_u64(vreinterpretq_u64_u8(s12), 16));
+    const uint8x16_t sum = vaddq_u8(v12, vaddq_u8(s12, s34));
+    const uint8x16_t count = vminq_u8(vrshrq_n_u8(sum, 1), const_4);
+    vst1q_u8(coeff_contexts, vaddq_u8(count, pos_to_offset));
+    v01 = v23;
     levels += 2 * stride;
     coeff_contexts += 16;
     col -= 2;
-  } while (col);
+  } while (col != 0);
 }

-static inline void get_8_coeff_contexts_hor(const uint8_t *levels,
-                                            const int width,
-                                            const ptrdiff_t *const offsets,
-                                            uint8_t *coeff_contexts) {
+static AOM_FORCE_INLINE void get_8_coeff_contexts_hor(const uint8_t *levels,
+                                                      const int n_cols,
+                                                      uint8_t *coeff_contexts) {
   const int stride = 8 + TX_PAD_HOR;
   const uint8x16_t pos_to_offset_large = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 10);
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);

   uint8x16_t pos_to_offset = vcombine_u8(vdup_n_u8(SIG_COEF_CONTEXTS_2D + 0),
                                          vdup_n_u8(SIG_COEF_CONTEXTS_2D + 5));

-  uint8x16_t count;
-  uint8x16_t level[5];
-
-  assert(!(width % 2));
+  assert(n_cols > 0 && !(n_cols % 2));

-  int col = width;
+  uint8x16_t v01 =
+      vminq_u8(vcombine_u8(vld1_u8(levels), vld1_u8(levels + stride)), const_3);
+  uint8x16_t v23 = vminq_u8(
+      vcombine_u8(vld1_u8(levels + 2 * stride), vld1_u8(levels + 3 * stride)),
+      const_3);
+  uint8x16_t s12 = vaddq_u8(vextq_u8(v01, v23, 8), v23);
+  int col = n_cols;
   do {
-    load_levels_8x2x5(levels, stride, offsets, level);
-    count = get_coeff_contexts_kernel(level);
-    count = vaddq_u8(count, pos_to_offset);
-    vst1q_u8(coeff_contexts, count);
+    const uint8x16_t v45 = vminq_u8(
+        vcombine_u8(vld1_u8(levels + 4 * stride), vld1_u8(levels + 5 * stride)),
+        const_3);
+    const uint8x16_t s34 = vaddq_u8(vextq_u8(v23, v45, 8), v45);
+    const uint8x16_t r1 = neon_vshr1_2d_u8x16(v01);
+    const uint8x16_t sum = vaddq_u8(r1, vaddq_u8(s12, s34));
+    const uint8x16_t count = vminq_u8(vrshrq_n_u8(sum, 1), const_4);
+    vst1q_u8(coeff_contexts, vaddq_u8(count, pos_to_offset));
+    v01 = v23;
+    v23 = v45;
+    s12 = s34;
     pos_to_offset = pos_to_offset_large;
     levels += 2 * stride;
     coeff_contexts += 16;
     col -= 2;
-  } while (col);
+  } while (col != 0);
 }

-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,
-                                             uint8_t *coeff_contexts) {
-  const int stride = height + TX_PAD_HOR;
+static AOM_FORCE_INLINE void get_16n_coeff_contexts_2d(
+    const uint8_t *levels, const int real_width, const int real_height,
+    const int n_cols, const int height, uint8_t *coeff_contexts) {
   uint8_t *cc = coeff_contexts;
-  int col = width;
+  int col = n_cols;
   uint8x16_t pos_to_offset[5];
-  uint8x16_t pos_to_offset_large[3];
   uint8x16_t count;
-  uint8x16_t level[5];

+  assert(n_cols > 0);
   assert(!(height % 16));

-  pos_to_offset_large[2] = vdupq_n_u8(21);
-  if (real_width == real_height) {
-    pos_to_offset[0] = vld1q_u8(c_16_po_2d_e[0]);
-    pos_to_offset[1] = vld1q_u8(c_16_po_2d_e[1]);
-    pos_to_offset[2] = vld1q_u8(c_16_po_2d_e[2]);
-    pos_to_offset[3] = vld1q_u8(c_16_po_2d_e[3]);
-    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] = vld1q_u8(c_16_po_2d_g[0]);
-    pos_to_offset[1] = vld1q_u8(c_16_po_2d_g[1]);
-    pos_to_offset[2] = pos_to_offset[3] = pos_to_offset[4] =
-        vld1q_u8(c_16_po_2d_g[2]);
-    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] = vld1q_u8(c_16_po_2d_l[0]);
-    pos_to_offset[2] = vld1q_u8(c_16_po_2d_l[1]);
-    pos_to_offset[3] = vld1q_u8(c_16_po_2d_l[2]);
-    pos_to_offset[4] = pos_to_offset_large[2];
-    pos_to_offset_large[0] = pos_to_offset_large[1] = vdupq_n_u8(16);
-  }
-
-  do {
-    int h = height;
+  const int w_idx = (real_width >= real_height) + (real_width > real_height);
+  pos_to_offset[0] = vld1q_u8(c_16_po_2d_all[w_idx][0]);
+  pos_to_offset[1] = vld1q_u8(c_16_po_2d_all[w_idx][1]);
+  pos_to_offset[2] = vld1q_u8(c_16_po_2d_all[w_idx][2]);
+  pos_to_offset[3] = vld1q_u8(c_16_po_2d_all[w_idx][3]);
+  pos_to_offset[4] = vld1q_u8(c_16_po_2d_all[w_idx][4]);

+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  const uint8x16_t zeros = vdupq_n_u8(0);
+  if (height == 16) {
+    const int stride = 16 + TX_PAD_HOR;
+    const uint8x16_t v_c0 = vminq_u8(vld1q_u8(levels), const_3);
+    const uint8x16_t v_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+    const uint8x16_t s01_c0 = vaddq_u8(v_c0, vextq_u8(v_c0, zeros, 1));
+    uint8x16_t s12_c0 = vextq_u8(s01_c0, zeros, 1);
+    uint8x16_t s01_c1 = vaddq_u8(v_c1, vextq_u8(v_c1, zeros, 1));
     do {
-      load_levels_16x1x5(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel(level);
+      const uint8x16_t v_c2 = vminq_u8(vld1q_u8(levels + 2 * stride), const_3);
+      count = vaddq_u8(s12_c0, vaddq_u8(s01_c1, v_c2));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
       count = vaddq_u8(count, pos_to_offset[0]);
       vst1q_u8(cc, count);
-      levels += 16;
+      s12_c0 = vextq_u8(s01_c1, zeros, 1);
+      s01_c1 = vaddq_u8(v_c2, vextq_u8(v_c2, zeros, 1));
+      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];
+      levels += stride;
       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);
+    } while (--col != 0);
+  } else {
+    assert(height == 32);
+    const int stride = 32 + TX_PAD_HOR;
+#if AOM_ARCH_AARCH64
+    const uint8x16_t pto_hi_2 = vdupq_n_u8(21);
+    uint8x16_t pto_hi_0 =
+        (real_width > real_height) ? pos_to_offset[0] : pto_hi_2;
+    uint8x16_t pto_hi_1 = pto_hi_0;
+    const uint8x16_t v0_c0 = vminq_u8(vld1q_u8(levels), const_3);
+    const uint8x16_t v1_c0 = vminq_u8(vld1q_u8(levels + 16), const_3);
+    const uint8x16_t v0_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+    const uint8x16_t v1_c1 = vminq_u8(vld1q_u8(levels + stride + 16), const_3);
+    const uint8x16_t s01_lo_c0 = vaddq_u8(v0_c0, vextq_u8(v0_c0, v1_c0, 1));
+    const uint8x16_t s01_hi_c0 = vaddq_u8(v1_c0, vextq_u8(v1_c0, zeros, 1));
+    uint8x16_t s12_lo_c0 = vextq_u8(s01_lo_c0, s01_hi_c0, 1);
+    uint8x16_t s12_hi_c0 = vextq_u8(s01_hi_c0, zeros, 1);
+    uint8x16_t s01_lo_c1 = vaddq_u8(v0_c1, vextq_u8(v0_c1, v1_c1, 1));
+    uint8x16_t s01_hi_c1 = vaddq_u8(v1_c1, vextq_u8(v1_c1, zeros, 1));
+    do {
+      const uint8x16_t v0_c2 = vminq_u8(vld1q_u8(levels + 2 * stride), const_3);
+      const uint8x16_t v1_c2 =
+          vminq_u8(vld1q_u8(levels + 2 * stride + 16), const_3);
+      uint8x16_t count_lo = vaddq_u8(s12_lo_c0, vaddq_u8(s01_lo_c1, v0_c2));
+      uint8x16_t count_hi = vaddq_u8(s12_hi_c0, vaddq_u8(s01_hi_c1, v1_c2));
+      count_lo = vminq_u8(vrshrq_n_u8(count_lo, 1), const_4);
+      count_hi = vminq_u8(vrshrq_n_u8(count_hi, 1), const_4);
+      vst1q_u8(cc, vaddq_u8(count_lo, pos_to_offset[0]));
+      vst1q_u8(cc + 16, vaddq_u8(count_hi, pto_hi_0));
+      s12_lo_c0 = vextq_u8(s01_lo_c1, s01_hi_c1, 1);
+      s12_hi_c0 = vextq_u8(s01_hi_c1, zeros, 1);
+      s01_lo_c1 = vaddq_u8(v0_c2, vextq_u8(v0_c2, v1_c2, 1));
+      s01_hi_c1 = vaddq_u8(v1_c2, vextq_u8(v1_c2, zeros, 1));
+      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];
+      pto_hi_0 = pto_hi_1;
+      pto_hi_1 = pto_hi_2;
+      levels += stride;
+      cc += 32;
+    } while (--col != 0);
+#else
+    // Sequential 16-row passes avoid exceeding ARMv7's 16 Q-registers.
+    const uint8_t *ls = levels;
+    const uint8x8_t const_3_d = vget_low_u8(const_3);
+    const uint8x16_t v0_c0 = vminq_u8(vld1q_u8(ls), const_3);
+    const uint8x8_t v0_c0_r16 = vmin_u8(vld1_u8(ls + 16), const_3_d);
+    const uint8x16_t v0_c1 = vminq_u8(vld1q_u8(ls + stride), const_3);
+    const uint8x8_t v0_c1_r16 = vmin_u8(vld1_u8(ls + stride + 16), const_3_d);
+    const uint8x16_t s01_c0 =
+        vaddq_u8(v0_c0, vextq_u8(v0_c0, vcombine_u8(v0_c0_r16, v0_c0_r16), 1));
+    const uint8x8_t s01_c0_r16 =
+        vadd_u8(v0_c0_r16, vext_u8(v0_c0_r16, v0_c0_r16, 1));
+    uint8x16_t s12_c0 =
+        vextq_u8(s01_c0, vcombine_u8(s01_c0_r16, s01_c0_r16), 1);
+    uint8x16_t s01_c1 =
+        vaddq_u8(v0_c1, vextq_u8(v0_c1, vcombine_u8(v0_c1_r16, v0_c1_r16), 1));
+    uint8x8_t s01_c1_r16 = vadd_u8(v0_c1_r16, vext_u8(v0_c1_r16, v0_c1_r16, 1));
+    do {
+      const uint8x16_t v_c2 = vminq_u8(vld1q_u8(ls + 2 * stride), const_3);
+      const uint8x8_t v_c2_r16 =
+          vmin_u8(vld1_u8(ls + 2 * stride + 16), const_3_d);
+      count = vaddq_u8(s12_c0, vaddq_u8(s01_c1, v_c2));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(cc, vaddq_u8(count, pos_to_offset[0]));
+      s12_c0 = vextq_u8(s01_c1, vcombine_u8(s01_c1_r16, s01_c1_r16), 1);
+      s01_c1 =
+          vaddq_u8(v_c2, vextq_u8(v_c2, vcombine_u8(v_c2_r16, v_c2_r16), 1));
+      s01_c1_r16 = vadd_u8(v_c2_r16, vext_u8(v_c2_r16, v_c2_r16, 1));
+      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];
+      ls += stride;
+      cc += 32;
+    } while (--col != 0);
+
+    ls = levels + 16;
+    cc = coeff_contexts;
+    col = n_cols;
+    const uint8x16_t pto_hi_2 = vdupq_n_u8(21);
+    uint8x16_t pto_hi_0 = vdupq_n_u8((real_width > real_height) ? 16 : 21);
+    uint8x16_t pto_hi_1 = pto_hi_0;
+    const uint8x16_t v1_c0 = vminq_u8(vld1q_u8(ls), const_3);
+    const uint8x16_t v1_c1 = vminq_u8(vld1q_u8(ls + stride), const_3);
+    const uint8x16_t s01_hi_c0 = vaddq_u8(v1_c0, vextq_u8(v1_c0, zeros, 1));
+    s12_c0 = vextq_u8(s01_hi_c0, zeros, 1);
+    s01_c1 = vaddq_u8(v1_c1, vextq_u8(v1_c1, zeros, 1));
+    do {
+      const uint8x16_t v_c2 = vminq_u8(vld1q_u8(ls + 2 * stride), const_3);
+      count = vaddq_u8(s12_c0, vaddq_u8(s01_c1, v_c2));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(cc + 16, vaddq_u8(count, pto_hi_0));
+      s12_c0 = vextq_u8(s01_c1, zeros, 1);
+      s01_c1 = vaddq_u8(v_c2, vextq_u8(v_c2, zeros, 1));
+      pto_hi_0 = pto_hi_1;
+      pto_hi_1 = pto_hi_2;
+      ls += stride;
+      cc += 32;
+    } while (--col != 0);
+#endif  // AOM_ARCH_AARCH64
+  }

   coeff_contexts[0] = 0;
 }

-static inline void get_16n_coeff_contexts_ver(const uint8_t *levels,
-                                              const int width, const int height,
-                                              const ptrdiff_t *const offsets,
-                                              uint8_t *coeff_contexts) {
-  const int stride = height + TX_PAD_HOR;
-
+static AOM_FORCE_INLINE void get_16n_coeff_contexts_ver(
+    const uint8_t *levels, const int n_cols, const int height,
+    uint8_t *coeff_contexts) {
+  const uint8x16_t pos_to_offset = vld1q_u8(c_16_po_ver);
   const uint8x16_t pos_to_offset_large = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 10);
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
+  const uint8x16_t zeros = vdupq_n_u8(0);

-  uint8x16_t count;
-  uint8x16_t level[5];
-
+  assert(n_cols > 0);
   assert(!(height % 16));

-  int col = width;
-  do {
-    uint8x16_t pos_to_offset = vld1q_u8(c_16_po_ver);
-
-    int h = height;
+  int col = n_cols;
+  if (height == 16) {
+    const int stride = 16 + TX_PAD_HOR;
+    uint8x16_t v_c0 = vminq_u8(vld1q_u8(levels), const_3);
     do {
-      load_levels_16x1x5(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel(level);
-      count = vaddq_u8(count, pos_to_offset);
-      vst1q_u8(coeff_contexts, count);
-      pos_to_offset = pos_to_offset_large;
-      levels += 16;
+      const uint8x16_t v_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+      const uint8x16_t s12 =
+          vaddq_u8(vextq_u8(v_c0, zeros, 1), vextq_u8(v_c0, zeros, 2));
+      const uint8x16_t s34 = vextq_u8(s12, zeros, 2);
+      uint8x16_t count = vaddq_u8(v_c1, vaddq_u8(s12, s34));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(coeff_contexts, vaddq_u8(count, pos_to_offset));
+      v_c0 = v_c1;
+      levels += stride;
       coeff_contexts += 16;
-      h -= 16;
-    } while (h);
-
-    levels += TX_PAD_HOR;
-  } while (--col);
+    } while (--col != 0);
+  } else {
+    assert(height == 32);
+    const int stride = 32 + TX_PAD_HOR;
+    uint8x16_t v0_c0 = vminq_u8(vld1q_u8(levels), const_3);
+    uint8x16_t v1_c0 = vminq_u8(vld1q_u8(levels + 16), const_3);
+    do {
+      const uint8x16_t v0_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+      const uint8x16_t v1_c1 =
+          vminq_u8(vld1q_u8(levels + stride + 16), const_3);
+      const uint8x16_t s12_lo =
+          vaddq_u8(vextq_u8(v0_c0, v1_c0, 1), vextq_u8(v0_c0, v1_c0, 2));
+      const uint8x16_t s12_hi =
+          vaddq_u8(vextq_u8(v1_c0, zeros, 1), vextq_u8(v1_c0, zeros, 2));
+      const uint8x16_t s34_lo = vextq_u8(s12_lo, s12_hi, 2);
+      uint8x16_t count_lo = vaddq_u8(v0_c1, vaddq_u8(s12_lo, s34_lo));
+      count_lo = vminq_u8(vrshrq_n_u8(count_lo, 1), const_4);
+      vst1q_u8(coeff_contexts, vaddq_u8(count_lo, pos_to_offset));
+
+      const uint8x16_t s34_hi = vextq_u8(s12_hi, zeros, 2);
+      uint8x16_t count_hi = vaddq_u8(v1_c1, vaddq_u8(s12_hi, s34_hi));
+      count_hi = vminq_u8(vrshrq_n_u8(count_hi, 1), const_4);
+      vst1q_u8(coeff_contexts + 16, vaddq_u8(count_hi, pos_to_offset_large));
+
+      v0_c0 = v0_c1;
+      v1_c0 = v1_c1;
+      levels += stride;
+      coeff_contexts += 32;
+    } while (--col != 0);
+  }
 }

-static inline void get_16n_coeff_contexts_hor(const uint8_t *levels,
-                                              const int width, const int height,
-                                              const ptrdiff_t *const offsets,
-                                              uint8_t *coeff_contexts) {
-  const int stride = height + TX_PAD_HOR;
-
+static AOM_FORCE_INLINE void get_16n_coeff_contexts_hor(
+    const uint8_t *levels, const int n_cols, const int height,
+    uint8_t *coeff_contexts) {
+  const uint8x16_t const_3 = vdupq_n_u8(3);
+  const uint8x16_t const_4 = vdupq_n_u8(4);
   uint8x16_t pos_to_offset[3];
   uint8x16_t count;
-  uint8x16_t level[5];

+  assert(n_cols > 0);
   assert(!(height % 16));

   pos_to_offset[0] = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 0);
   pos_to_offset[1] = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 5);
   pos_to_offset[2] = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 10);

-  int col = width;
-  do {
-    int h = height;
+  int col = n_cols;
+  if (height == 16) {
+    const int stride = 16 + TX_PAD_HOR;
+    const uint8x16_t zeros = vdupq_n_u8(0);
+    uint8x16_t v_c0 = vminq_u8(vld1q_u8(levels), const_3);
+    uint8x16_t v_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+    uint8x16_t v_c2 = vminq_u8(vld1q_u8(levels + 2 * stride), const_3);
+    uint8x16_t v_c3 = vminq_u8(vld1q_u8(levels + 3 * stride), const_3);
     do {
-      load_levels_16x1x5(levels, stride, offsets, level);
-      count = get_coeff_contexts_kernel(level);
-      count = vaddq_u8(count, pos_to_offset[0]);
-      vst1q_u8(coeff_contexts, count);
-      levels += 16;
+      const uint8x16_t v_c4 = vminq_u8(vld1q_u8(levels + 4 * stride), const_3);
+      count = vaddq_u8(vextq_u8(v_c0, zeros, 1),
+                       vaddq_u8(vaddq_u8(v_c1, v_c2), vaddq_u8(v_c3, v_c4)));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(coeff_contexts, vaddq_u8(count, pos_to_offset[0]));
+      v_c0 = v_c1;
+      v_c1 = v_c2;
+      v_c2 = v_c3;
+      v_c3 = v_c4;
+      pos_to_offset[0] = pos_to_offset[1];
+      pos_to_offset[1] = pos_to_offset[2];
+      levels += stride;
       coeff_contexts += 16;
-      h -= 16;
-    } while (h);
+    } while (--col != 0);
+  } else {
+    assert(height == 32);
+    const int stride = 32 + TX_PAD_HOR;
+#if AOM_ARCH_AARCH64
+    const uint8x16_t zeros = vdupq_n_u8(0);
+    uint8x16_t v0_c0 = vminq_u8(vld1q_u8(levels), const_3);
+    uint8x16_t v1_c0 = vminq_u8(vld1q_u8(levels + 16), const_3);
+    uint8x16_t v0_c1 = vminq_u8(vld1q_u8(levels + stride), const_3);
+    uint8x16_t v1_c1 = vminq_u8(vld1q_u8(levels + stride + 16), const_3);
+    uint8x16_t v0_c2 = vminq_u8(vld1q_u8(levels + 2 * stride), const_3);
+    uint8x16_t v1_c2 = vminq_u8(vld1q_u8(levels + 2 * stride + 16), const_3);
+    uint8x16_t v0_c3 = vminq_u8(vld1q_u8(levels + 3 * stride), const_3);
+    uint8x16_t v1_c3 = vminq_u8(vld1q_u8(levels + 3 * stride + 16), const_3);
+    do {
+      const uint8x16_t v0_c4 = vminq_u8(vld1q_u8(levels + 4 * stride), const_3);
+      const uint8x16_t v1_c4 =
+          vminq_u8(vld1q_u8(levels + 4 * stride + 16), const_3);
+      uint8x16_t count_lo =
+          vaddq_u8(vextq_u8(v0_c0, v1_c0, 1),
+                   vaddq_u8(vaddq_u8(v0_c1, v0_c2), vaddq_u8(v0_c3, v0_c4)));
+      uint8x16_t count_hi =
+          vaddq_u8(vextq_u8(v1_c0, zeros, 1),
+                   vaddq_u8(vaddq_u8(v1_c1, v1_c2), vaddq_u8(v1_c3, v1_c4)));
+      count_lo = vminq_u8(vrshrq_n_u8(count_lo, 1), const_4);
+      count_hi = vminq_u8(vrshrq_n_u8(count_hi, 1), const_4);
+      vst1q_u8(coeff_contexts, vaddq_u8(count_lo, pos_to_offset[0]));
+      vst1q_u8(coeff_contexts + 16, vaddq_u8(count_hi, pos_to_offset[0]));
+      v0_c0 = v0_c1;
+      v1_c0 = v1_c1;
+      v0_c1 = v0_c2;
+      v1_c1 = v1_c2;
+      v0_c2 = v0_c3;
+      v1_c2 = v1_c3;
+      v0_c3 = v0_c4;
+      v1_c3 = v1_c4;
+      pos_to_offset[0] = pos_to_offset[1];
+      pos_to_offset[1] = pos_to_offset[2];
+      levels += stride;
+      coeff_contexts += 32;
+    } while (--col != 0);
+#else
+    // Sequential 16-row passes avoid exceeding ARMv7's 16 Q-registers.
+    const uint8_t *ls = levels;
+    uint8_t *cc = coeff_contexts;
+    uint8x16_t v_c0 = vminq_u8(vld1q_u8(ls), const_3);
+    uint8x16_t v_c1 = vminq_u8(vld1q_u8(ls + stride), const_3);
+    uint8x16_t v_c2 = vminq_u8(vld1q_u8(ls + 2 * stride), const_3);
+    uint8x16_t v_c3 = vminq_u8(vld1q_u8(ls + 3 * stride), const_3);
+    do {
+      const uint8x16_t v_c0_hi = vminq_u8(vld1q_u8(ls + 16), const_3);
+      const uint8x16_t v_c4 = vminq_u8(vld1q_u8(ls + 4 * stride), const_3);
+      count = vaddq_u8(vextq_u8(v_c0, v_c0_hi, 1),
+                       vaddq_u8(vaddq_u8(v_c1, v_c2), vaddq_u8(v_c3, v_c4)));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(cc, vaddq_u8(count, pos_to_offset[0]));
+      v_c0 = v_c1;
+      v_c1 = v_c2;
+      v_c2 = v_c3;
+      v_c3 = v_c4;
+      pos_to_offset[0] = pos_to_offset[1];
+      pos_to_offset[1] = pos_to_offset[2];
+      ls += stride;
+      cc += 32;
+    } while (--col != 0);
+
+    const uint8x16_t zeros = vdupq_n_u8(0);
+    pos_to_offset[0] = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 0);
+    pos_to_offset[1] = vdupq_n_u8(SIG_COEF_CONTEXTS_2D + 5);
+    ls = levels + 16;
+    cc = coeff_contexts;
+    col = n_cols;
+    v_c0 = vminq_u8(vld1q_u8(ls), const_3);
+    v_c1 = vminq_u8(vld1q_u8(ls + stride), const_3);
+    v_c2 = vminq_u8(vld1q_u8(ls + 2 * stride), const_3);
+    v_c3 = vminq_u8(vld1q_u8(ls + 3 * stride), const_3);
+    do {
+      const uint8x16_t v_c4 = vminq_u8(vld1q_u8(ls + 4 * stride), const_3);
+      count = vaddq_u8(vextq_u8(v_c0, zeros, 1),
+                       vaddq_u8(vaddq_u8(v_c1, v_c2), vaddq_u8(v_c3, v_c4)));
+      count = vminq_u8(vrshrq_n_u8(count, 1), const_4);
+      vst1q_u8(cc + 16, vaddq_u8(count, pos_to_offset[0]));
+      v_c0 = v_c1;
+      v_c1 = v_c2;
+      v_c2 = v_c3;
+      v_c3 = v_c4;
+      pos_to_offset[0] = pos_to_offset[1];
+      pos_to_offset[1] = pos_to_offset[2];
+      ls += stride;
+      cc += 32;
+    } while (--col != 0);
+#endif  // AOM_ARCH_AARCH64
+  }
+}

-    pos_to_offset[0] = pos_to_offset[1];
-    pos_to_offset[1] = pos_to_offset[2];
-    levels += TX_PAD_HOR;
-  } while (--col);
+// Upper bound on (scan[c] >> bhl) + 1 for c < active_eob (rounded up to a
+// whole column count) over default_scan_{8x16,8x32,16x16,16x32,32x16,32x32}
+// (all TX_CLASS_2D blocks use the default diagonal scan; 8x16/8x32 with
+// active_eob <= 28 also use this table), indexed by (active_eob - 1) >> 3 for
+// active_eob <= 8 * sizeof(c_diag_cols_by8) (496).
+static const uint8_t c_diag_cols_by8[62] = {
+  4,  6,  7,  8,  9,  10, 11, 11, 12, 13, 13, 14, 14, 15, 15, 16,
+  16, 17, 17, 18, 18, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23,
+  23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 27, 28,
+  28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31
+};
+
+// Returns a column-granular upper bound on the active column count required
+// for coeff_contexts[scan[c]] across 0 <= c < active_eob in TX_CLASS_2D
+// (all TX_CLASS_2D transform types use default_scan_*). Contexts outside
+// the computed columns remain uninitialized, matching
+// av1_get_nz_map_contexts_c.
+static inline int nz_diag_n_cols_2d(const int active_eob, const int width,
+                                    const int height) {
+  assert(active_eob >= 1);
+  if (height == 4) {
+    // default_scan_{8x4,16x4} enter col 4 at active_eob=14, col 8 at 30, and
+    // col 12 at 46, aligning directly with 4-column granularity.
+    return AOMMIN(width, 4 + (((active_eob + 2) >> 4) << 2));
+  }
+  if (height == 8) {
+    if (width == 4) return 4;
+    // default_scan_{8x8,16x8,32x8} reach cols 2, 4, 6, 8 at active_eob =
+    // 6, 15, 28, 44; thereafter +2 cols every 16 coeffs (16x8/32x8).
+    if (active_eob <= 5) return 2;
+    if (active_eob <= 14) return 4;
+    if (active_eob <= 27) return 6;
+    if (width == 8 || active_eob <= 43) return 8;
+    return AOMMIN(width, 8 + (((active_eob - 28) >> 4) << 1));
+  }
+  if (active_eob <= 6) return (active_eob <= 3) ? 2 : 3;
+  // default_scan_4x16 reaches col 3 (4 cols) at active_eob=7.
+  if (width == 4) return 4;
+  // default_scan_{8x16,8x32} reach col 7 (8 cols) by active_eob=29.
+  if (width == 8 && active_eob > 28) return 8;
+  // default_scan_32x16 enters col 16 at active_eob=152, then +1 col per 16
+  // coeffs.
+  if (height == 16 && active_eob > 151) {
+    return AOMMIN(width, 16 + ((active_eob - 136) >> 4));
+  }
+  // 32x32 reaches col 32 at active_eob=497 (after 8*62=496 coeffs).
+  if (active_eob > 8 * (int)sizeof(c_diag_cols_by8)) return width;
+  return AOMMIN(width, c_diag_cols_by8[(active_eob - 1) >> 3]);
 }

-// Note: levels[] must be in the range [0, 127], inclusive.
+// Note: levels[] must be in the range [0, 127], inclusive; TX_PAD_HOR padding
+// lanes at levels[col * stride + height .. col * stride + stride - 1] and
+// TX_PAD_BOTTOM trailing columns at
+// levels[width * stride .. (width + TX_PAD_BOTTOM) * stride - 1] must be zero,
+// and scan must match av1_scan_orders[tx_size][tx_type].scan.
 void av1_get_nz_map_contexts_neon(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) {
+  assert(TX_PAD_HOR == 4 && TX_PAD_BOTTOM == 4);
+  assert(eob >= 1);
+  assert(tx_size < TX_SIZES_ALL);
   const int last_idx = eob - 1;
   if (!last_idx) {
     coeff_contexts[0] = 0;
     return;
   }
+  if (last_idx == 1 && tx_class == TX_CLASS_2D) {
+    coeff_contexts[0] = 0;
+    coeff_contexts[scan[1]] = 1;
+    return;
+  }

   uint8_t *const coefficients = (uint8_t *const)coeff_contexts;

-  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 (tx_size == TX_4X4 && tx_class == TX_CLASS_2D) {
+    assert(eob <= 16);
+    get_4x4_nz_map_contexts_2d(levels, coefficients);
+    const int pos = scan[last_idx];
+    coeff_contexts[pos] = (last_idx <= 2) ? 1 : ((last_idx <= 4) ? 2 : 3);
+    return;
+  }
+
+  const int width = get_txb_wide(tx_size);
+  const int height = get_txb_high(tx_size);
+  const int bhl = get_txb_bhl(tx_size);
+  assert(eob <= (width << bhl));

+  if (tx_class == TX_CLASS_2D) {
     if (height == 4) {
-      get_4_nz_map_contexts_2d(levels, width, offsets, coefficients);
-    } else if (height == 8) {
-      get_8_coeff_contexts_2d(levels, width, offsets, coefficients);
+      assert(width > 4);
+      const int n_cols = nz_diag_n_cols_2d(last_idx, width, 4);
+      get_4_nz_map_contexts_2d(levels, n_cols, coefficients);
     } else {
-      get_16n_coeff_contexts_2d(levels, real_width, real_height, width, height,
-                                offsets, coefficients);
+      const int n_cols = nz_diag_n_cols_2d(last_idx, width, height);
+      if (height == 8) {
+        get_8_coeff_contexts_2d(levels, width, n_cols, coefficients);
+      } else {
+        const int real_width = tx_size_wide[tx_size];
+        const int real_height = tx_size_high[tx_size];
+        get_16n_coeff_contexts_2d(levels, real_width, real_height, n_cols,
+                                  height, coefficients);
+      }
     }
   } else if (tx_class == TX_CLASS_HORIZ) {
-    offsets[0] = 2 * stride;
-    offsets[1] = 3 * stride;
-    offsets[2] = 4 * stride;
+    const int n_cols = ((last_idx - 1) >> bhl) + 1;
     if (height == 4) {
-      get_4_nz_map_contexts_hor(levels, width, offsets, coefficients);
+      get_4_nz_map_contexts_hor(levels, (n_cols + 3) & ~3, coefficients);
     } else if (height == 8) {
-      get_8_coeff_contexts_hor(levels, width, offsets, coefficients);
+      get_8_coeff_contexts_hor(levels, (n_cols + 1) & ~1, coefficients);
     } else {
-      get_16n_coeff_contexts_hor(levels, width, height, offsets, coefficients);
+      get_16n_coeff_contexts_hor(levels, n_cols, height, coefficients);
     }
-  } else {  // TX_CLASS_VERT
-    offsets[0] = 2;
-    offsets[1] = 3;
-    offsets[2] = 4;
+  } else {
+    assert(tx_class == TX_CLASS_VERT);
+    const int n_cols = AOMMIN(width, last_idx);
     if (height == 4) {
-      get_4_nz_map_contexts_ver(levels, width, offsets, coefficients);
+      get_4_nz_map_contexts_ver(levels, (n_cols + 3) & ~3, coefficients);
     } else if (height == 8) {
-      get_8_coeff_contexts_ver(levels, width, offsets, coefficients);
+      get_8_coeff_contexts_ver(levels, (n_cols + 1) & ~1, coefficients);
     } else {
-      get_16n_coeff_contexts_ver(levels, width, height, offsets, coefficients);
+      get_16n_coeff_contexts_ver(levels, n_cols, height, coefficients);
     }
   }

-  const int bhl = get_txb_bhl(tx_size);
   const int pos = scan[last_idx];
   if (last_idx <= (width << bhl) / 8)
     coeff_contexts[pos] = 1;
diff --git a/test/encodetxb_test.cc b/test/encodetxb_test.cc
index 1a44adec11..97c880b3ab 100644
--- a/test/encodetxb_test.cc
+++ b/test/encodetxb_test.cc
@@ -58,12 +58,22 @@ class EncodeTxbTest : public ::testing::TestWithParam<GetNzMapContextsFunc> {
     aom_free(coeff_contexts_);
   }

-  void GetNzMapContextsRun() {
-    const int kNumTests = 10;
+  void GetNzMapContextsRun(bool sparse = false) {
+    const int kNumTests = sparse ? 4 : 10;
     int result = 0;

-    for (int is_inter = 0; is_inter < 2; ++is_inter) {
-      for (int tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) {
+    static constexpr TX_TYPE kSparseTxTypes[] = { DCT_DCT, V_DCT, H_DCT };
+    const int num_inter = sparse ? 1 : 2;
+    const int num_tx_types = sparse
+                                 ? static_cast<int>(sizeof(kSparseTxTypes) /
+                                                    sizeof(kSparseTxTypes[0]))
+                                 : static_cast<int>(TX_TYPES);
+
+    for (int is_inter = 0; is_inter < num_inter; ++is_inter) {
+      for (int tx_type_idx = 0; tx_type_idx < num_tx_types; ++tx_type_idx) {
+        const int tx_type = sparse
+                                ? static_cast<int>(kSparseTxTypes[tx_type_idx])
+                                : tx_type_idx;
         const TX_CLASS tx_class = tx_type_to_class[tx_type];
         for (int tx_size = TX_4X4; tx_size < TX_SIZES_ALL; ++tx_size) {
           const int bhl = get_txb_bhl((TX_SIZE)tx_size);
@@ -76,12 +86,19 @@ class EncodeTxbTest : public ::testing::TestWithParam<GetNzMapContextsFunc> {
           levels_ = set_levels(levels_buf_, height);
           for (int i = 0; i < kNumTests && !result; ++i) {
             for (int eob = 1; eob <= width * height && !result; ++eob) {
-              InitDataWithEob(scan, bhl, eob);
+              InitDataWithEob(scan, bhl, eob, sparse);

               av1_get_nz_map_contexts_c(levels_, scan, eob, (TX_SIZE)tx_size,
                                         tx_class, coeff_contexts_ref_);
-              get_nz_map_contexts_func_(levels_, scan, eob, (TX_SIZE)tx_size,
-                                        tx_class, coeff_contexts_);
+              if (is_inter == 0 && i == 0 &&
+                  (eob <= 4 || eob == width * height || (eob & 63) == 0)) {
+                API_REGISTER_STATE_CHECK(get_nz_map_contexts_func_(
+                    levels_, scan, eob, (TX_SIZE)tx_size, tx_class,
+                    coeff_contexts_));
+              } else {
+                get_nz_map_contexts_func_(levels_, scan, eob, (TX_SIZE)tx_size,
+                                          tx_class, coeff_contexts_);
+              }

               result = Compare(scan, eob);

@@ -145,14 +162,15 @@ class EncodeTxbTest : public ::testing::TestWithParam<GetNzMapContextsFunc> {
   }

  private:
-  void InitDataWithEob(const int16_t *const scan, const int bhl,
-                       const int eob) {
+  void InitDataWithEob(const int16_t *const scan, const int bhl, const int eob,
+                       bool sparse = false) {
     memset(levels_buf_, 0, sizeof(levels_buf_));
     memset(coeff_contexts_, 0, sizeof(*coeff_contexts_) * MAX_TX_SQUARE);

     for (int c = 0; c < eob; ++c) {
       levels_[get_padded_idx(scan[c], bhl)] =
-          static_cast<uint8_t>(clamp(rnd_.Rand8(), 0, INT8_MAX));
+          sparse ? static_cast<uint8_t>(rnd_.Rand8() & 3)
+                 : static_cast<uint8_t>(clamp(rnd_.Rand8(), 0, INT8_MAX));
       coeff_contexts_[scan[c]] = static_cast<int8_t>(rnd_.Rand16() >> 1);
     }

@@ -188,6 +206,8 @@ GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EncodeTxbTest);

 TEST_P(EncodeTxbTest, GetNzMapContexts) { GetNzMapContextsRun(); }

+TEST_P(EncodeTxbTest, GetNzMapContextsSparse) { GetNzMapContextsRun(true); }
+
 TEST_P(EncodeTxbTest, DISABLED_SpeedTestGetNzMapContexts) {
   SpeedTestGetNzMapContextsRun();
 }
@@ -218,6 +238,7 @@ class EncodeTxbInitLevelTest
  public:
   ~EncodeTxbInitLevelTest() override = default;
   void RunTest(av1_txb_init_levels_func test_func, int tx_size, int is_speed);
+  void RunHighBdTest(av1_txb_init_levels_func test_func, int tx_size);
 };
 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EncodeTxbInitLevelTest);

@@ -288,10 +309,118 @@ void EncodeTxbInitLevelTest::RunTest(av1_txb_init_levels_func test_func,
   }
 }

+void EncodeTxbInitLevelTest::RunHighBdTest(av1_txb_init_levels_func test_func,
+                                           int tx_size) {
+  const int width = get_txb_wide((TX_SIZE)tx_size);
+  const int height = get_txb_high((TX_SIZE)tx_size);
+  tran_low_t coeff[MAX_TX_SQUARE];
+
+  uint8_t levels_buf[2][TX_PAD_2D];
+  alignas(4) uint8_t levels_unaligned_buf[TX_PAD_2D + 2];
+  uint8_t *const levels0 = set_levels(levels_buf[0], height);
+  uint8_t *const levels1 = set_levels(levels_buf[1], height);
+  uint8_t *const levels_unaligned =
+      set_levels(levels_unaligned_buf + 1, height);
+
+  constexpr int kMaxHighBdCoeff = (1 << 18) - 1;
+  constexpr tran_low_t kExtremeCoeffs[16] = { 0,
+                                              127,
+                                              -127,
+                                              128,
+                                              -128,
+                                              INT16_MAX,
+                                              -INT16_MAX,
+                                              32768,
+                                              65535,
+                                              65536,
+                                              kMaxHighBdCoeff,
+                                              INT32_MAX,
+                                              -1,
+                                              -255,
+                                              -16384,
+                                              -32767 };
+#if HAVE_NEON
+  // Modes 2-3 exercise negative coeffs <= INT16_MIN. Gate on NEON because x86
+  // av1_txb_init_levels_{sse4_1,avx2} pass _mm_abs_epi16(INT16_MIN) into signed
+  // _mm_packs_epi16, saturating to -128 instead of 127.
+  constexpr tran_low_t kNegExtremeCoeffs[16] = {
+    -1,     -127,   -128,      -255,           -INT16_MAX,       INT16_MIN,
+    -32769, -65535, -65536,    -131072,        -kMaxHighBdCoeff, -INT32_MAX,
+    32768,  65536,  INT32_MAX, kMaxHighBdCoeff
+  };
+  const int num_modes = (test_func == &av1_txb_init_levels_neon) ? 4 : 2;
+#else
+  const int num_modes = 2;
+#endif
+
+  ACMRandom rnd(ACMRandom::DeterministicSeed());
+  for (int mode = 0; mode < num_modes; ++mode) {
+    for (int i = 0; i < width * height; ++i) {
+      if (mode == 0) {
+        coeff[i] = kExtremeCoeffs[i % 16];
+      } else if (mode == 1) {
+        const uint32_t hi = rnd.Rand16() & 7;
+        const uint32_t lo = rnd.Rand16();
+        const uint32_t mag = (hi << 16) | lo;
+        const uint32_t span =
+            static_cast<uint32_t>(kMaxHighBdCoeff) + INT16_MAX + 1u;
+        coeff[i] = static_cast<tran_low_t>(mag % span) - INT16_MAX;
+#if HAVE_NEON
+      } else if (mode == 2) {
+        coeff[i] = kNegExtremeCoeffs[i % 16];
+      } else {
+        const uint32_t hi = rnd.Rand16() & 7;
+        const uint32_t lo = rnd.Rand16();
+        const uint32_t mag = (hi << 16) | lo;
+        coeff[i] = static_cast<tran_low_t>(mag) - kMaxHighBdCoeff;
+#endif
+      }
+    }
+    const uint8_t canary = rnd.Rand8();
+    levels_unaligned_buf[0] = canary;
+    levels_unaligned_buf[TX_PAD_2D + 1] = canary;
+    for (int i = 0; i < TX_PAD_2D; ++i) {
+      const uint8_t init_byte = rnd.Rand8();
+      levels_buf[0][i] = init_byte;
+      levels_buf[1][i] = init_byte;
+      levels_unaligned_buf[i + 1] = init_byte;
+    }
+    av1_txb_init_levels_c(coeff, width, height, levels0);
+    API_REGISTER_STATE_CHECK(test_func(coeff, width, height, levels1));
+    API_REGISTER_STATE_CHECK(test_func(coeff, width, height, levels_unaligned));
+    ASSERT_EQ(canary, levels_unaligned_buf[0]);
+    ASSERT_EQ(canary, levels_unaligned_buf[TX_PAD_2D + 1]);
+    const int stride = height + TX_PAD_HOR;
+    for (int c = 0; c < width + TX_PAD_VER; ++c) {
+      for (int r = 0; r < stride; ++r) {
+        ASSERT_EQ(levels_buf[0][c * stride + r], levels_buf[1][c * stride + r])
+            << "mode " << mode << " [" << c << "," << r << "] " << width << "x"
+            << height;
+        ASSERT_EQ(levels_buf[0][c * stride + r],
+                  levels_unaligned_buf[1 + c * stride + r])
+            << "unaligned mode " << mode << " [" << c << "," << r << "] "
+            << width << "x" << height;
+      }
+    }
+    int tail_start = (width + TX_PAD_VER) * stride + TX_PAD_END;
+#if HAVE_NEON
+    if (test_func == &av1_txb_init_levels_neon) tail_start -= TX_PAD_END;
+#endif
+    for (int i = tail_start; i < TX_PAD_2D; ++i) {
+      ASSERT_EQ(levels_buf[0][i], levels_buf[1][i]) << "tail " << i;
+      ASSERT_EQ(levels_buf[0][i], levels_unaligned_buf[1 + i]) << "tail " << i;
+    }
+  }
+}
+
 TEST_P(EncodeTxbInitLevelTest, match) {
   RunTest(GET_PARAM(0), GET_PARAM(1), 0);
 }

+TEST_P(EncodeTxbInitLevelTest, MatchHighBitdepth) {
+  RunHighBdTest(GET_PARAM(0), GET_PARAM(1));
+}
+
 TEST_P(EncodeTxbInitLevelTest, DISABLED_Speed) {
   RunTest(GET_PARAM(0), GET_PARAM(1), 1);
 }