Account for cdf model update in lv-map coding

Make the lv-map rate-distortion optimization account for the
per symbol cdf update. This improves the level map coding
performance by 0.4%.

Change-Id: I1cbecafbad4e6642abc3782c9741636d92d87eb7
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index a3d467f..04265fd 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4491,7 +4491,7 @@
     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);
+    av1_fill_coeff_costs(&td->mb, xd->tile_ctx);
 #else
     av1_fill_token_costs_from_cdf(x->token_head_costs,
                                   x->e_mbd.tile_ctx->coef_head_cdfs);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 1a0716e..972c9bf 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4370,6 +4370,7 @@
     }
 
     // transform / motion compensation build reconstruction frame
+    save_coding_context(cpi);
     av1_encode_frame(cpi);
 
     // Update the skip mb flag probabilities based on the distribution
@@ -4382,8 +4383,7 @@
     // accurate estimate of output frame size to determine if we need
     // to recode.
     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
-      save_coding_context(cpi);
-
+      restore_coding_context(cpi);
       av1_pack_bitstream(cpi, dest, size);
 
       rc->projected_frame_size = (int)(*size) << 3;
@@ -4905,6 +4905,7 @@
     cpi->rc.is_last_bipred_frame = 0;
     cpi->rc.is_bipred_frame = 0;
 
+    restore_coding_context(cpi);
     // Build the bitstream
     av1_pack_bitstream(cpi, dest, size);
 
diff --git a/av1/encoder/encodetxb.c b/av1/encoder/encodetxb.c
index 0e3fa34..864672e 100644
--- a/av1/encoder/encodetxb.c
+++ b/av1/encoder/encodetxb.c
@@ -1551,12 +1551,17 @@
   unsigned int(*nz_map_count)[SIG_COEF_CONTEXTS][2];
 
   TX_SIZE txsize_ctx = get_txsize_context(tx_size);
+  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
 
   nz_map_count = &td->counts->nz_map[txsize_ctx][plane_type];
 
   memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
 
   ++td->counts->txb_skip[txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
+#if LV_MAP_PROB
+  update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx], eob == 0,
+             2);
+#endif
   x->mbmi_ext->txb_skip_ctx[plane][block] = txb_ctx.txb_skip_ctx;
 
   x->mbmi_ext->eobs[plane][block] = eob;
@@ -1580,9 +1585,16 @@
     if (c == seg_eob - 1) break;
 
     ++(*nz_map_count)[coeff_ctx][is_nz];
+#if LV_MAP_PROB
+    update_cdf(ec_ctx->nz_map_cdf[txsize_ctx][plane_type][coeff_ctx], is_nz, 2);
+#endif
 
     if (is_nz) {
       ++td->counts->eob_flag[txsize_ctx][plane_type][eob_ctx][c == (eob - 1)];
+#if LV_MAP_PROB
+      update_cdf(ec_ctx->eob_flag_cdf[txsize_ctx][plane_type][eob_ctx],
+                 c == (eob - 1), 2);
+#endif
     }
   }
 
@@ -1600,16 +1612,26 @@
 
       if (level == i + 1) {
         ++td->counts->coeff_base[txsize_ctx][plane_type][i][ctx][1];
+#if LV_MAP_PROB
+        update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][i][ctx], 1,
+                   2);
+#endif
         if (c == 0) {
           int dc_sign_ctx = txb_ctx.dc_sign_ctx;
 
           ++td->counts->dc_sign[plane_type][dc_sign_ctx][v < 0];
+#if LV_MAP_PROB
+          update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], v < 0, 2);
+#endif
           x->mbmi_ext->dc_sign_ctx[plane][block] = dc_sign_ctx;
         }
         cul_level += level;
         continue;
       }
       ++td->counts->coeff_base[txsize_ctx][plane_type][i][ctx][0];
+#if LV_MAP_PROB
+      update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][i][ctx], 0, 2);
+#endif
       update_eob = AOMMAX(update_eob, c);
     }
   }
@@ -1627,6 +1649,9 @@
       int dc_sign_ctx = txb_ctx.dc_sign_ctx;
 
       ++td->counts->dc_sign[plane_type][dc_sign_ctx][v < 0];
+#if LV_MAP_PROB
+      update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], v < 0, 2);
+#endif
       x->mbmi_ext->dc_sign_ctx[plane][block] = dc_sign_ctx;
     }
 
@@ -1635,9 +1660,15 @@
     for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) {
       if (level == (idx + 1 + NUM_BASE_LEVELS)) {
         ++td->counts->coeff_lps[txsize_ctx][plane_type][ctx][1];
+#if LV_MAP_PROB
+        update_cdf(ec_ctx->coeff_lps_cdf[txsize_ctx][plane_type][ctx], 1, 2);
+#endif
         break;
       }
       ++td->counts->coeff_lps[txsize_ctx][plane_type][ctx][0];
+#if LV_MAP_PROB
+      update_cdf(ec_ctx->coeff_lps_cdf[txsize_ctx][plane_type][ctx], 0, 2);
+#endif
     }
     if (idx < COEFF_BASE_RANGE) continue;