Use txb_entropy_ctx to store entropy ctx of lv_map

1) Add txb_entropy_ctx into MACROBLOCK_PLANE and PICK_MODE_CONTEXT

2) Add av1_get_txb_entropy_context() to compute the entropy context

3) Compute and sore the entropy context before av1_xform_quant()
return

Change-Id: Ia2170523af3163b9456f7c6a305c1e77ad2b23be
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 056e740..28b7e10 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -45,6 +45,9 @@
   tran_low_t *qcoeff;
   tran_low_t *coeff;
   uint16_t *eobs;
+#if CONFIG_LV_MAP
+  uint8_t *txb_entropy_ctx;
+#endif
   struct buf_2d src;
 
   // Quantizer setings
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c
index 61738da..4c7d6ff 100644
--- a/av1/encoder/context_tree.c
+++ b/av1/encoder/context_tree.c
@@ -52,6 +52,12 @@
                     aom_memalign(32, num_pix * sizeof(*ctx->dqcoeff[i])));
     CHECK_MEM_ERROR(cm, ctx->eobs[i],
                     aom_memalign(32, num_blk * sizeof(*ctx->eobs[i])));
+#if CONFIG_LV_MAP
+    CHECK_MEM_ERROR(
+        cm, ctx->txb_entropy_ctx[i],
+        aom_memalign(32, num_blk * sizeof(*ctx->txb_entropy_ctx[i])));
+#endif
+
 #if CONFIG_PVQ
     CHECK_MEM_ERROR(cm, ctx->pvq_ref_coeff[i],
                     aom_memalign(32, num_pix * sizeof(*ctx->pvq_ref_coeff[i])));
@@ -88,6 +94,10 @@
 #endif
     aom_free(ctx->eobs[i]);
     ctx->eobs[i] = 0;
+#if CONFIG_LV_MAP
+    aom_free(ctx->txb_entropy_ctx[i]);
+    ctx->txb_entropy_ctx[i] = 0;
+#endif
   }
 
 #if CONFIG_PALETTE
diff --git a/av1/encoder/context_tree.h b/av1/encoder/context_tree.h
index 7496d11..6795412 100644
--- a/av1/encoder/context_tree.h
+++ b/av1/encoder/context_tree.h
@@ -42,6 +42,9 @@
   tran_low_t *pvq_ref_coeff[MAX_MB_PLANE];
 #endif
   uint16_t *eobs[MAX_MB_PLANE];
+#if CONFIG_LV_MAP
+  uint8_t *txb_entropy_ctx[MAX_MB_PLANE];
+#endif
 
   int num_4x4_blk;
   int skip;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 55a0a91..67c04a1 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1137,6 +1137,9 @@
     pd[i].pvq_ref_coeff = ctx->pvq_ref_coeff[i];
 #endif
     p[i].eobs = ctx->eobs[i];
+#if CONFIG_LV_MAP
+    p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
+#endif  // CONFIG_LV_MAP
   }
 #if CONFIG_PALETTE
   for (i = 0; i < 2; ++i) pd[i].color_index_map = ctx->color_index_map[i];
@@ -1856,6 +1859,9 @@
     pd[i].pvq_ref_coeff = ctx->pvq_ref_coeff[i];
 #endif
     p[i].eobs = ctx->eobs[i];
+#if CONFIG_LV_MAP
+    p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
+#endif
   }
 
 #if CONFIG_PALETTE
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index bd01860..cac0abf 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -25,6 +25,9 @@
 
 #include "av1/encoder/av1_quantize.h"
 #include "av1/encoder/encodemb.h"
+#if CONFIG_LV_MAP
+#include "av1/encoder/encodetxb.h"
+#endif
 #include "av1/encoder/hybrid_fwd_txfm.h"
 #include "av1/encoder/rd.h"
 #include "av1/encoder/tokenize.h"
@@ -633,6 +636,10 @@
         av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
       }
     }
+#if CONFIG_LV_MAP
+    p->txb_entropy_ctx[block] =
+        (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
+#endif  // CONFIG_LV_MAP
     return;
   }
 #endif  // CONFIG_AOM_HIGHBITDEPTH
@@ -645,7 +652,11 @@
       av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
     }
   }
-#else  // #if !CONFIG_PVQ
+#if CONFIG_LV_MAP
+  p->txb_entropy_ctx[block] =
+      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
+#endif  // CONFIG_LV_MAP
+#else   // #if !CONFIG_PVQ
   (void)xform_quant_idx;
 #if CONFIG_AOM_HIGHBITDEPTH
   fwd_txfm_param.bd = xd->bd;
@@ -1015,7 +1026,11 @@
   (void)tx_size;
   struct macroblock_plane *p = &x->plane[plane];
 
+#if !CONFIG_LV_MAP
   *a = *l = p->eobs[block] > 0;
+#else   // !CONFIG_LV_MAP
+  *a = *l = p->txb_entropy_ctx[block];
+#endif  // !CONFIG_LV_MAP
 
 #if CONFIG_VAR_TX || CONFIG_LV_MAP
   int i;
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 9dfced2..67c733c 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -367,6 +367,21 @@
   int rate;
 } TxbParams;
 
+int av1_get_txb_entropy_context(const tran_low_t *qcoeff,
+                                const SCAN_ORDER *scan_order, int eob) {
+  const int16_t *scan = scan_order->scan;
+  int cul_level = 0;
+  int c;
+  for (c = 0; c < eob; ++c) {
+    cul_level += abs(qcoeff[scan[c]]);
+  }
+
+  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
+  set_dc_sign(&cul_level, qcoeff[0]);
+
+  return cul_level;
+}
+
 static void update_txb_context(int plane, int block, int blk_row, int blk_col,
                                BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                                void *arg) {
@@ -385,20 +400,9 @@
   const TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
   const SCAN_ORDER *const scan_order =
       get_scan(cm, tx_size, tx_type, is_inter_block(mbmi));
-  const int16_t *scan = scan_order->scan;
   (void)plane_bsize;
 
-  int cul_level = 0;
-  int c;
-  for (c = 0; c < eob; ++c) {
-    cul_level += abs(qcoeff[scan[c]]);
-  }
-
-  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
-
-  // DC value
-  set_dc_sign(&cul_level, qcoeff[0]);
-
+  int cul_level = av1_get_txb_entropy_context(qcoeff, scan_order, eob);
   av1_set_contexts(xd, pd, plane, tx_size, cul_level, blk_col, blk_row);
 }
 
diff --git a/av1/encoder/encodetxb.h b/av1/encoder/encodetxb.h
index 8692c59..dd51f04 100644
--- a/av1/encoder/encodetxb.h
+++ b/av1/encoder/encodetxb.h
@@ -32,6 +32,8 @@
                           TXB_CTX *txb_ctx);
 void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
                          aom_writer *w, int plane);
+int av1_get_txb_entropy_context(const tran_low_t *qcoeff,
+                                const SCAN_ORDER *scan_order, int eob);
 void av1_update_txb_context(const AV1_COMP *cpi, ThreadData *td,
                             RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate,
                             const int mi_row, const int mi_col);
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 5fbf239..74c8491 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -585,6 +585,9 @@
     pd[i].pvq_ref_coeff = ctx->pvq_ref_coeff[i];
 #endif
     p[i].eobs = ctx->eobs[i];
+#if CONFIG_LV_MAP
+    p[i].txb_entropy_ctx = ctx->txb_entropy_ctx[i];
+#endif
   }
 
   av1_init_mv_probs(cm);