Add av1_get_base_level_counts()

which calculates base level counts in block.

Change-Id: Ia975faad4afbfc0c587bdcd403de3d4dc3f5f411
diff --git a/av1/common/txb_common.c b/av1/common/txb_common.c
index b89b40b..a5c8b7d 100644
--- a/av1/common/txb_common.c
+++ b/av1/common/txb_common.c
@@ -282,3 +282,17 @@
 
   return t;
 }
+
+void av1_get_base_level_counts(const uint8_t *const levels,
+                               const int level_minus_1, const int width,
+                               const int height, uint8_t *const level_counts) {
+  const int stride = width + TX_PAD_HOR;
+
+  for (int row = 0; row < height; ++row) {
+    for (int col = 0; col < width; ++col) {
+      level_counts[row * width + col] =
+          get_level_count(levels, stride, row, col, level_minus_1,
+                          base_ref_offset, BASE_CONTEXT_POSITION_NUM);
+    }
+  }
+}
diff --git a/av1/common/txb_common.h b/av1/common/txb_common.h
index 9f040e9..590228b 100644
--- a/av1/common/txb_common.h
+++ b/av1/common/txb_common.h
@@ -179,14 +179,14 @@
 
 static INLINE int get_base_ctx(const uint8_t *const levels,
                                const int c,  // raster order
-                               const int bwl, const int level_minus_1) {
+                               const int bwl, const int level_minus_1,
+                               const int count) {
   const int row = c >> bwl;
   const int col = c - (row << bwl);
   const int stride = (1 << bwl) + TX_PAD_HOR;
   int mag_count = 0;
   int nb_mag[3] = { 0 };
-  const int count = get_level_count(levels, stride, row, col, level_minus_1,
-                                    base_ref_offset, BASE_CONTEXT_POSITION_NUM);
+
   get_level_mag(levels, stride, row, col, nb_mag);
 
   for (int idx = 0; idx < 3; ++idx)
@@ -567,4 +567,8 @@
 
 void av1_init_lv_map(AV1_COMMON *cm);
 
+void av1_get_base_level_counts(const uint8_t *const levels,
+                               const int level_minus_1, const int width,
+                               const int height, uint8_t *const level_counts);
+
 #endif  // AV1_COMMON_TXB_COMMON_H_
diff --git a/av1/decoder/decodetxb.c b/av1/decoder/decodetxb.c
index f9c8710..1c9f3b1 100644
--- a/av1/decoder/decodetxb.c
+++ b/av1/decoder/decodetxb.c
@@ -205,15 +205,17 @@
 #if USE_CAUSAL_BASE_CTX
   update_eob = *eob - 1;
 #else
+  uint8_t level_counts[MAX_TX_SQUARE];
   int i;
   for (i = 0; i < NUM_BASE_LEVELS; ++i) {
+    av1_get_base_level_counts(levels, i, width, height, level_counts);
     for (c = *eob - 1; c >= 0; --c) {
       uint8_t *const level = &levels[get_paded_idx(scan[c], bwl)];
       int ctx;
 
       if (*level <= i) continue;
 
-      ctx = get_base_ctx(levels, scan[c], bwl, i);
+      ctx = get_base_ctx(levels, scan[c], bwl, i, level_counts[scan[c]]);
 
       if (av1_read_record_bin(
               counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx], 2,
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 85d8630..5fd2836 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -371,6 +371,7 @@
   }
   update_eob = eob - 1;
 #else
+  uint8_t level_counts[MAX_TX_SQUARE];
   for (int i = 1; i < eob; ++i) {
     c = eob - 1 - i;
     int coeff_ctx = get_nz_map_ctx(levels, c, scan, bwl, height, tx_type);
@@ -383,13 +384,14 @@
   }
 
   for (int i = 0; i < NUM_BASE_LEVELS; ++i) {
+    av1_get_base_level_counts(levels, i, width, height, level_counts);
     for (c = eob - 1; c >= 0; --c) {
       const tran_low_t level = abs(tcoeff[scan[c]]);
       int ctx;
 
       if (level <= i) continue;
 
-      ctx = get_base_ctx(levels, scan[c], bwl, i);
+      ctx = get_base_ctx(levels, scan[c], bwl, i, level_counts[scan[c]]);
 
       if (level == i + 1) {
         aom_write_bin(w, 1, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][i][ctx],
@@ -2096,14 +2098,16 @@
 
 #if !USE_CAUSAL_BASE_CTX
   // Reverse process order to handle coefficient level and sign.
+  uint8_t level_counts[MAX_TX_SQUARE];
   for (int i = 0; i < NUM_BASE_LEVELS; ++i) {
+    av1_get_base_level_counts(levels, i, width, height, level_counts);
     for (c = eob - 1; c >= 0; --c) {
       const tran_low_t level = abs(tcoeff[scan[c]]);
       int ctx;
 
       if (level <= i) continue;
 
-      ctx = get_base_ctx(levels, scan[c], bwl, i);
+      ctx = get_base_ctx(levels, scan[c], bwl, i, level_counts[scan[c]]);
 
       if (level == i + 1) {
         ++td->counts->coeff_base[txsize_ctx][plane_type][i][ctx][1];