Update get_nz_map_ctx()

Extract the shared 2 neighbors { 0, 1 } and { 1, 0 }.

Change-Id: Ie1b52cb62519b53bfb5388af71ea637eab3fccc7
diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index ae5eeea..b4513fd 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -168,7 +168,7 @@
 }
 
 static INLINE int get_paded_idx(const int idx, const int bwl) {
-  return idx + TX_PAD_HOR * (idx >> bwl);
+  return idx + ((idx >> bwl) << TX_PAD_HOR_LOG2);
 }
 
 static INLINE int get_level_count(const uint8_t *const levels, const int stride,
@@ -426,50 +426,68 @@
   // , { 1, 1 }, { 1, 2 },
 };
 
+#define SIG_REF_DIFF_OFFSET_NUM 3
+
+static const int sig_ref_diff_offset[SIG_REF_DIFF_OFFSET_NUM][2] = {
+  { 1, 1 }, { 0, 2 }, { 2, 0 }
+};
+
+static const int sig_ref_diff_offset_vert[SIG_REF_DIFF_OFFSET_NUM][2] = {
+  { 2, 0 }, { 3, 0 }, { 4, 0 }
+};
+
+static const int sig_ref_diff_offset_horiz[SIG_REF_DIFF_OFFSET_NUM][2] = {
+  { 0, 2 }, { 0, 3 }, { 0, 4 }
+};
+
 #if USE_CAUSAL_BASE_CTX
 static INLINE int get_nz_mag(const uint8_t *const levels, const int bwl,
-                             const int row, const int col,
                              const TX_CLASS tx_class) {
-  int mag = 0;
-  for (int idx = 0; idx < SIG_REF_OFFSET_NUM; ++idx) {
+  int mag;
+
+  // Note: AOMMIN(level, 3) is useless for decoder since level < 3.
+  mag = AOMMIN(levels[1], 3);                         // { 0, 1 }
+  mag += AOMMIN(levels[(1 << bwl) + TX_PAD_HOR], 3);  // { 1, 0 }
+
+  for (int idx = 0; idx < SIG_REF_DIFF_OFFSET_NUM; ++idx) {
     const int row_offset =
-        ((tx_class == TX_CLASS_2D)
-             ? sig_ref_offset[idx][0]
-             : ((tx_class == TX_CLASS_VERT) ? sig_ref_offset_vert[idx][0]
-                                            : sig_ref_offset_horiz[idx][0]));
+        ((tx_class == TX_CLASS_2D) ? sig_ref_diff_offset[idx][0]
+                                   : ((tx_class == TX_CLASS_VERT)
+                                          ? sig_ref_diff_offset_vert[idx][0]
+                                          : sig_ref_diff_offset_horiz[idx][0]));
     const int col_offset =
-        ((tx_class == TX_CLASS_2D)
-             ? sig_ref_offset[idx][1]
-             : ((tx_class == TX_CLASS_VERT) ? sig_ref_offset_vert[idx][1]
-                                            : sig_ref_offset_horiz[idx][1]));
-    const int ref_row = row + row_offset;
-    const int ref_col = col + col_offset;
+        ((tx_class == TX_CLASS_2D) ? sig_ref_diff_offset[idx][1]
+                                   : ((tx_class == TX_CLASS_VERT)
+                                          ? sig_ref_diff_offset_vert[idx][1]
+                                          : sig_ref_diff_offset_horiz[idx][1]));
     const int nb_pos =
-        (ref_row << bwl) + (ref_row << TX_PAD_HOR_LOG2) + ref_col;
-    const int level = levels[nb_pos];
-    mag += AOMMIN(level, 3);
+        (row_offset << bwl) + (row_offset << TX_PAD_HOR_LOG2) + col_offset;
+    mag += AOMMIN(levels[nb_pos], 3);
   }
   return mag;
 }
 #endif
 
 static INLINE int get_nz_count(const uint8_t *const levels, const int bwl,
-                               const int row, const int col,
                                const TX_CLASS tx_class) {
-  const int stride = (1 << bwl) + TX_PAD_HOR;
-  int count = 0;
-  for (int idx = 0; idx < SIG_REF_OFFSET_NUM; ++idx) {
-    const int ref_row = row + ((tx_class == TX_CLASS_2D)
-                                   ? sig_ref_offset[idx][0]
+  int count;
+
+  count = (levels[1] != 0);                         // { 0, 1 }
+  count += (levels[(1 << bwl) + TX_PAD_HOR] != 0);  // { 1, 0 }
+
+  for (int idx = 0; idx < SIG_REF_DIFF_OFFSET_NUM; ++idx) {
+    const int row_offset =
+        ((tx_class == TX_CLASS_2D) ? sig_ref_diff_offset[idx][0]
                                    : ((tx_class == TX_CLASS_VERT)
-                                          ? sig_ref_offset_vert[idx][0]
-                                          : sig_ref_offset_horiz[idx][0]));
-    const int ref_col = col + ((tx_class == TX_CLASS_2D)
-                                   ? sig_ref_offset[idx][1]
+                                          ? sig_ref_diff_offset_vert[idx][0]
+                                          : sig_ref_diff_offset_horiz[idx][0]));
+    const int col_offset =
+        ((tx_class == TX_CLASS_2D) ? sig_ref_diff_offset[idx][1]
                                    : ((tx_class == TX_CLASS_VERT)
-                                          ? sig_ref_offset_vert[idx][1]
-                                          : sig_ref_offset_horiz[idx][1]));
-    const int nb_pos = ref_row * stride + ref_col;
+                                          ? sig_ref_diff_offset_vert[idx][1]
+                                          : sig_ref_diff_offset_horiz[idx][1]));
+    const int nb_pos =
+        (row_offset << bwl) + (row_offset << TX_PAD_HOR_LOG2) + col_offset;
     count += (levels[nb_pos] != 0);
   }
   return count;
@@ -522,10 +540,10 @@
 }
 
 static INLINE int get_nz_map_ctx(const uint8_t *const levels,
-                                 const int scan_idx, const int16_t *const scan,
-                                 const int bwl, const int height,
+                                 const int coeff_idx, const int bwl,
+                                 const int height,
 #if CONFIG_LV_MAP_MULTI
-                                 const int is_eob,
+                                 const int scan_idx, const int is_eob,
 #endif
                                  const TX_TYPE tx_type) {
 #if CONFIG_LV_MAP_MULTI
@@ -536,15 +554,12 @@
     return SIG_COEF_CONTEXTS - 1;
   }
 #endif
-  const int coeff_idx = scan[scan_idx];
-  const int row = coeff_idx >> bwl;
-  const int col = coeff_idx - (row << bwl);
   const TX_CLASS tx_class = tx_type_to_class[tx_type];
   const int stats =
 #if USE_CAUSAL_BASE_CTX
-      get_nz_mag(levels, bwl, row, col, tx_class);
+      get_nz_mag(levels + get_paded_idx(coeff_idx, bwl), bwl, tx_class);
 #else
-      get_nz_count(levels, bwl, row, col, tx_class);
+      get_nz_count(levels + get_paded_idx(coeff_idx, bwl), bwl, tx_class);
 #endif
   return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, height, tx_class);
 }
diff --git a/av1/decoder/decodetxb.c b/av1/decoder/decodetxb.c
index 45213af..ab6eb43 100644
--- a/av1/decoder/decodetxb.c
+++ b/av1/decoder/decodetxb.c
@@ -164,7 +164,7 @@
     const int pos = scan[c];
 #if CONFIG_LV_MAP_MULTI
     const int coeff_ctx =
-        get_nz_map_ctx(levels, c, scan, bwl, height, c == *eob - 1, tx_type);
+        get_nz_map_ctx(levels, pos, bwl, height, c, c == *eob - 1, tx_type);
 #if USE_BASE_EOB_ALPHABET
     aom_cdf_prob *cdf;
     int nsymbs;
@@ -214,7 +214,7 @@
     }
 #else
     int is_nz;
-    const int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
 
     if (c < *eob - 1) {
       is_nz = av1_read_record_bin(
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index c78c291..3faab7c 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -269,8 +269,8 @@
     }
   }
 #else
-  int coeff_ctx = get_nz_map_ctx(levels, scan_idx, scan, txb_info->bwl,
-                                 txb_info->height, txb_info->tx_type);
+  const int coeff_ctx = get_nz_map_ctx(levels, coeff_idx, txb_info->bwl,
+                                       txb_info->height, txb_info->tx_type);
   if ((stats->rd_low < stats->rd) && (stats->low_qc == 0)) {
     stats->nz_rate = txb_costs->nz_map_cost[coeff_ctx][0];
   } else {
@@ -387,7 +387,7 @@
 
 #if CONFIG_LV_MAP_MULTI
     coeff_ctx =
-        get_nz_map_ctx(levels, c, scan, bwl, height, c == eob - 1, tx_type);
+        get_nz_map_ctx(levels, pos, bwl, height, c, c == eob - 1, tx_type);
     const tran_low_t v = tcoeff[pos];
 #if USE_BASE_EOB_ALPHABET
     if (c == eob - 1) {
@@ -407,7 +407,7 @@
                      ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], 4);
 #endif
 #else
-    coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
     const tran_low_t v = tcoeff[pos];
     const int is_nz = (v != 0);
 
@@ -433,7 +433,7 @@
   for (int i = 1; i < eob; ++i) {
     c = eob - 1 - i;
     const int pos = scan[c];
-    const int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
     const tran_low_t v = tcoeff[pos];
     const int is_nz = (v != 0);
 
@@ -715,7 +715,7 @@
     const int level = abs(v);
 #if CONFIG_LV_MAP_MULTI
     coeff_ctx =
-        get_nz_map_ctx(levels, c, scan, bwl, height, c == eob - 1, tx_type);
+        get_nz_map_ctx(levels, pos, bwl, height, c, c == eob - 1, tx_type);
 #if USE_BASE_EOB_ALPHABET
     if (c == eob - 1) {
       cost += coeff_costs
@@ -729,12 +729,12 @@
 #endif
 #else  // CONFIG_LV_MAP_MULTI
 #if USE_CAUSAL_BASE_CTX
-    coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
 #endif
 
     if (c < eob - 1) {
 #if !USE_CAUSAL_BASE_CTX
-      int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+      const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
 #endif
       cost += coeff_costs->nz_map_cost[coeff_ctx][is_nz];
     }
@@ -866,8 +866,9 @@
     const int row = coeff_idx >> bwl;
     const int col = coeff_idx - (row << bwl);
 
-    txb_cache->nz_count_arr[coeff_idx] = get_nz_count(
-        levels, bwl, row, col, tx_type_to_class[txb_info->tx_type]);
+    txb_cache->nz_count_arr[coeff_idx] =
+        get_nz_count(levels + get_paded_idx(coeff_idx, bwl), bwl,
+                     tx_type_to_class[txb_info->tx_type]);
 
     txb_cache->nz_ctx_arr[coeff_idx] = get_nz_map_ctx_from_stats(
 #if USE_CAUSAL_BASE_CTX
@@ -1494,11 +1495,6 @@
             txb_cache->nz_count_arr[nb_coeff_idx],
 #endif
             nb_coeff_idx, txb_info->bwl, txb_info->height, txb_info->tx_type);
-        // int ref_ctx = get_nz_map_ctx(txb_info->levels, nb_coeff_idx,
-        // txb_info->bwl, tx_type);
-        // if (ref_ctx != txb_cache->nz_ctx_arr[nb_coeff_idx])
-        //   printf("nz ctx %d ref_ctx %d\n",
-        //   txb_cache->nz_ctx_arr[nb_coeff_idx], ref_ctx);
       }
     }
   }
@@ -1590,9 +1586,9 @@
   const int pos = scan[scan_idx];
 
 #if CONFIG_LV_MAP_MULTI
-  int coeff_ctx =
-      get_nz_map_ctx(txb_info->levels, scan_idx, scan, txb_info->bwl,
-                     txb_info->height, is_eob, txb_info->tx_type);
+  const int coeff_ctx =
+      get_nz_map_ctx(txb_info->levels, pos, txb_info->bwl, txb_info->height,
+                     scan_idx, is_eob, txb_info->tx_type);
 #if USE_BASE_EOB_ALPHABET
   if (is_eob) {
     cost +=
@@ -1607,15 +1603,13 @@
 #endif
 #else
 #if USE_CAUSAL_BASE_CTX
-  int coeff_ctx =
-      get_nz_map_ctx(txb_info->levels, scan_idx, scan, txb_info->bwl,
-                     txb_info->height, txb_info->tx_type);
+  const int coeff_ctx = get_nz_map_ctx(txb_info->levels, pos, txb_info->bwl,
+                                       txb_info->height, txb_info->tx_type);
 #endif
   if (scan_idx < txb_info->eob - 1) {
 #if !USE_CAUSAL_BASE_CTX
-    int coeff_ctx =
-        get_nz_map_ctx(txb_info->levels, scan_idx, scan, txb_info->bwl,
-                       txb_info->height, txb_info->tx_type);
+    const int coeff_ctx = get_nz_map_ctx(txb_info->levels, pos, txb_info->bwl,
+                                         txb_info->height, txb_info->tx_type);
 #endif
     cost += txb_costs->nz_map_cost[coeff_ctx][is_nz];
   }
@@ -2292,7 +2286,7 @@
     (void)is_nz;
     (void)nz_map_count;
     coeff_ctx =
-        get_nz_map_ctx(levels, c, scan, bwl, height, c == eob - 1, tx_type);
+        get_nz_map_ctx(levels, pos, bwl, height, c, c == eob - 1, tx_type);
 #if USE_BASE_EOB_ALPHABET
     if (c == eob - 1) {
       update_cdf(ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type]
@@ -2321,7 +2315,7 @@
                AOMMIN(abs(v), 3), 4);
 #endif
 #elif USE_CAUSAL_BASE_CTX
-    coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
 
     if (c < eob - 1) {
       ++(*nz_map_count)[coeff_ctx][is_nz];
@@ -2344,7 +2338,7 @@
       }
     }
 #else
-    int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
+    const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, tx_type);
 
     if (c == eob - 1) continue;