Support per symbol cdf update in lv-map encoding

Allow per symbol cdf probability model update in the lv-map
encoding process.

Change-Id: Id0c4a659c04ffa981d8f04537eda2f1304d1682a
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index cb62253..a3d467f 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4490,10 +4490,14 @@
     MODE_INFO **mi = cm->mi_grid_visible + idx_str;
     PC_TREE *const pc_root = td->pc_root[cm->mib_size_log2 - MIN_MIB_SIZE_LOG2];
 
+#if CONFIG_LV_MAP && LV_MAP_PROB
+    av1_fill_coeff_costs(&td->mb, cm->fc);
+#else
     av1_fill_token_costs_from_cdf(x->token_head_costs,
                                   x->e_mbd.tile_ctx->coef_head_cdfs);
     av1_fill_token_costs_from_cdf(x->token_tail_costs,
                                   x->e_mbd.tile_ctx->coef_tail_cdfs);
+#endif
 
     if (sf->adaptive_pred_interp_filter) {
       for (i = 0; i < leaf_nodes; ++i)
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index d531573..0e3fa34 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -73,8 +73,10 @@
                           aom_writer *w, int blk_row, int blk_col, int block,
                           int plane, TX_SIZE tx_size, const tran_low_t *tcoeff,
                           uint16_t eob, TXB_CTX *txb_ctx) {
+#if !LV_MAP_PROB
   aom_prob *nz_map;
   aom_prob *eob_flag;
+#endif
   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
   const PLANE_TYPE plane_type = get_plane_type(plane);
   const TX_SIZE txs_ctx = get_txsize_context(tx_size);
@@ -93,7 +95,12 @@
   (void)blk_row;
   (void)blk_col;
 
+#if LV_MAP_PROB
+  aom_write_symbol(w, eob == 0,
+                   cm->fc->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2);
+#else
   aom_write(w, eob == 0, cm->fc->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx]);
+#endif
 
   if (eob == 0) return;
 #if CONFIG_TXK_SEL
@@ -101,8 +108,10 @@
                     get_min_tx_size(tx_size), w);
 #endif
 
+#if !LV_MAP_PROB
   nz_map = cm->fc->nz_map[txs_ctx][plane_type];
   eob_flag = cm->fc->eob_flag[txs_ctx][plane_type];
+#endif
 
   for (c = 0; c < eob; ++c) {
     int coeff_ctx = get_nz_map_ctx(tcoeff, scan[c], bwl, height, iscan);
@@ -113,17 +122,28 @@
 
     if (c == seg_eob - 1) break;
 
+#if LV_MAP_PROB
+    aom_write_symbol(w, is_nz,
+                     cm->fc->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2);
+#else
     aom_write(w, is_nz, nz_map[coeff_ctx]);
+#endif
 
     if (is_nz) {
+#if LV_MAP_PROB
+      aom_write_symbol(w, c == (eob - 1),
+                       cm->fc->eob_flag_cdf[txs_ctx][plane_type][eob_ctx], 2);
+#else
       aom_write(w, c == (eob - 1), eob_flag[eob_ctx]);
+#endif
     }
   }
 
   int i;
   for (i = 0; i < NUM_BASE_LEVELS; ++i) {
+#if !LV_MAP_PROB
     aom_prob *coeff_base = cm->fc->coeff_base[txs_ctx][plane_type][i];
-
+#endif
     update_eob = 0;
     for (c = eob - 1; c >= 0; --c) {
       tran_low_t v = tcoeff[scan[c]];
@@ -136,15 +156,32 @@
       ctx = get_base_ctx(tcoeff, scan[c], bwl, height, i + 1);
 
       if (level == i + 1) {
+#if LV_MAP_PROB
+        aom_write_symbol(
+            w, 1, cm->fc->coeff_base_cdf[txs_ctx][plane_type][i][ctx], 2);
+#else
         aom_write(w, 1, coeff_base[ctx]);
+#endif
         if (c == 0) {
+#if LV_MAP_PROB
+          aom_write_symbol(
+              w, sign, cm->fc->dc_sign_cdf[plane_type][txb_ctx->dc_sign_ctx],
+              2);
+#else
           aom_write(w, sign, cm->fc->dc_sign[plane_type][txb_ctx->dc_sign_ctx]);
+#endif
         } else {
           aom_write_bit(w, sign);
         }
         continue;
       }
+
+#if LV_MAP_PROB
+      aom_write_symbol(w, 0,
+                       cm->fc->coeff_base_cdf[txs_ctx][plane_type][i][ctx], 2);
+#else
       aom_write(w, 0, coeff_base[ctx]);
+#endif
       update_eob = AOMMAX(update_eob, c);
     }
   }
@@ -159,7 +196,12 @@
     if (level <= NUM_BASE_LEVELS) continue;
 
     if (c == 0) {
+#if LV_MAP_PROB
+      aom_write_symbol(
+          w, sign, cm->fc->dc_sign_cdf[plane_type][txb_ctx->dc_sign_ctx], 2);
+#else
       aom_write(w, sign, cm->fc->dc_sign[plane_type][txb_ctx->dc_sign_ctx]);
+#endif
     } else {
       aom_write_bit(w, sign);
     }
@@ -168,10 +210,20 @@
     ctx = get_br_ctx(tcoeff, scan[c], bwl, height);
     for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
       if (level == (idx + 1 + NUM_BASE_LEVELS)) {
+#if LV_MAP_PROB
+        aom_write_symbol(w, 1, cm->fc->coeff_lps_cdf[txs_ctx][plane_type][ctx],
+                         2);
+#else
         aom_write(w, 1, cm->fc->coeff_lps[txs_ctx][plane_type][ctx]);
+#endif
         break;
       }
+#if LV_MAP_PROB
+      aom_write_symbol(w, 0, cm->fc->coeff_lps_cdf[txs_ctx][plane_type][ctx],
+                       2);
+#else
       aom_write(w, 0, cm->fc->coeff_lps[txs_ctx][plane_type][ctx]);
+#endif
     }
     if (idx < COEFF_BASE_RANGE) continue;
 
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 4003366..419a47f 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -452,16 +452,45 @@
 }
 
 #if CONFIG_LV_MAP
+#if !LV_MAP_PROB
 static void get_rate_cost(aom_prob p, int cost[2]) {
   cost[0] = av1_cost_bit(p, 0);
   cost[1] = av1_cost_bit(p, 1);
 }
+#endif  // !LV_MAP_PROB
 
 void av1_fill_coeff_costs(MACROBLOCK *x, FRAME_CONTEXT *fc) {
   for (TX_SIZE tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
     for (int plane = 0; plane < PLANE_TYPES; ++plane) {
       LV_MAP_COEFF_COST *pcost = &x->coeff_costs[tx_size][plane];
 
+#if LV_MAP_PROB
+      for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->txb_skip_cost[ctx],
+                                 fc->txb_skip_cdf[tx_size][ctx], NULL);
+
+      for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->nz_map_cost[ctx],
+                                 fc->nz_map_cdf[tx_size][plane][ctx], NULL);
+
+      for (int ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->eob_cost[ctx],
+                                 fc->eob_flag_cdf[tx_size][plane][ctx], NULL);
+
+      for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
+                                 fc->dc_sign_cdf[plane][ctx], NULL);
+
+      for (int layer = 0; layer < NUM_BASE_LEVELS; ++layer)
+        for (int ctx = 0; ctx < COEFF_BASE_CONTEXTS; ++ctx)
+          av1_cost_tokens_from_cdf(
+              pcost->base_cost[layer][ctx],
+              fc->coeff_base_cdf[tx_size][plane][layer][ctx], NULL);
+
+      for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
+        av1_cost_tokens_from_cdf(pcost->lps_cost[ctx],
+                                 fc->coeff_lps_cdf[tx_size][plane][ctx], NULL);
+#else
       for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
         get_rate_cost(fc->txb_skip[tx_size][ctx], pcost->txb_skip_cost[ctx]);
 
@@ -481,6 +510,7 @@
 
       for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx)
         get_rate_cost(fc->coeff_lps[tx_size][plane][ctx], pcost->lps_cost[ctx]);
+#endif
     }
   }
 }