EC_ADAPT: Perform backwards updates directly on CDFs.

The initial CDF for each frame is stored in
the frame context. CDFs for actual coding are
stored in the tile structures, and these are
what get adapted. The initial CDF is replaced
by an average CDF derived from these tile CDFs.
This is carried forward to future frames when
backward adaptation is on.

CDFs are no longer set from the 8 bit probabilities
in backwards adaptation.

For now, 8 bit probabilities are maintained for
use in the encoder and for symbols which do not
have a CDF.

Change-Id: I106b30510bfad1fa57d077f7702acc1864378a09
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index ccf485f..00ae1fd 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -4832,6 +4832,7 @@
 #endif  // CONFIG_GLOBAL_MOTION
   }
 #if CONFIG_EC_MULTISYMBOL
+#if !CONFIG_EC_ADAPT
 #if CONFIG_NEW_TOKENSET
   av1_coef_head_cdfs(fc);
 #endif
@@ -4844,6 +4845,7 @@
 #if CONFIG_EC_MULTISYMBOL
   av1_set_mode_cdfs(cm);
 #endif
+#endif  // !CONFIG_EC_ADAPT
 #endif
 #if CONFIG_ANS
   aom_buf_ans_flush(header_bc);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index b3adff5..dc1e91d 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4680,6 +4680,17 @@
 }
 #endif  // DUMP_RECON_FRAMES
 
+#if CONFIG_EC_ADAPT
+
+static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
+                                      const int tile_cols,
+                                      FRAME_CONTEXT *ec_ctxs[]) {
+  int i;
+  for (i = 0; i < tile_rows * tile_cols; ++i)
+    ec_ctxs[i] = &cpi->tile_data[i].tctx;
+}
+
+#endif
 static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
                                       uint8_t *dest,
                                       unsigned int *frame_flags) {
@@ -4687,6 +4698,9 @@
   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   struct segmentation *const seg = &cm->seg;
   TX_SIZE t;
+#if CONFIG_EC_ADAPT
+  FRAME_CONTEXT *tile_ctxs[MAX_TILE_ROWS * MAX_TILE_COLS];
+#endif
 #if CONFIG_XIPHRC
   int frame_type;
   int drop_this_frame = 0;
@@ -4966,6 +4980,13 @@
 #endif  // CONFIG_SUBFRAME_PROB_UPDATE
     av1_adapt_coef_probs(cm);
     av1_adapt_intra_frame_probs(cm);
+#if CONFIG_EC_ADAPT
+    make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
+    av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs,
+                               cm->tile_rows * cm->tile_cols);
+    av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs,
+                                cm->tile_rows * cm->tile_cols);
+#endif
 #if CONFIG_ADAPT_SCAN
     av1_adapt_scan_order(cm);
 #endif  // CONFIG_ADAPT_SCAN
@@ -4975,6 +4996,12 @@
     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
       av1_adapt_inter_frame_probs(cm);
       av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
+#if CONFIG_EC_ADAPT
+      av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
+                                  cm->tile_rows * cm->tile_cols);
+      av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs,
+                               cm->tile_rows * cm->tile_cols);
+#endif
     }
   }