Add a CDF for coding delta_q.

Also remove forward updates for delta_q when EC_ADAPT
is enabled.

Change-Id: Idf71b57bfe7763bc60595bc45768e624dd7b67bd
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index 0c0f48d..732ddc3 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -627,6 +627,11 @@
 
 #if CONFIG_DELTA_Q
 static const aom_prob default_delta_q_probs[DELTA_Q_PROBS] = { 220, 220, 220 };
+#if CONFIG_EC_MULTISYMBOL
+static const aom_cdf_prob default_delta_q_cdf[CDF_SIZE(DELTA_Q_PROBS + 1)] = {
+  28160, 32120, 32677, 32768, 0
+};
+#endif
 #endif
 #if CONFIG_EC_MULTISYMBOL
 int av1_intra_mode_ind[INTRA_MODES];
@@ -1827,6 +1832,9 @@
 #endif
 #if CONFIG_DELTA_Q
   av1_copy(fc->delta_q_prob, default_delta_q_probs);
+#if CONFIG_EC_MULTISYMBOL
+  av1_copy(fc->delta_q_cdf, default_delta_q_cdf);
+#endif
 #endif
 }
 
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 80190c9..23cc2cb 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -254,6 +254,9 @@
   aom_cdf_prob kf_y_cdf[INTRA_MODES][INTRA_MODES][CDF_SIZE(INTRA_MODES)];
   aom_cdf_prob tx_size_cdf[MAX_TX_DEPTH][TX_SIZE_CONTEXTS]
                           [CDF_SIZE(MAX_TX_DEPTH + 1)];
+#if CONFIG_DELTA_Q
+  aom_cdf_prob delta_q_cdf[CDF_SIZE(DELTA_Q_PROBS + 1)];
+#endif
 #if !CONFIG_EXT_TX
   aom_cdf_prob intra_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES][CDF_SIZE(TX_TYPES)];
   aom_cdf_prob inter_ext_tx_cdf[EXT_TX_SIZES][CDF_SIZE(TX_TYPES)];
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 0a33c48..6e7de3d 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -4609,7 +4609,7 @@
   for (k = 0; k < SKIP_CONTEXTS; ++k)
     av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
 
-#if CONFIG_DELTA_Q
+#if CONFIG_DELTA_Q && !CONFIG_EC_ADAPT
   for (k = 0; k < DELTA_Q_PROBS; ++k)
     av1_diff_update_prob(&r, &fc->delta_q_prob[k], ACCT_STR);
 #endif
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index e13dfb9..eba62c9 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -67,16 +67,33 @@
   const int b_col = mi_col & MAX_MIB_MASK;
   const int b_row = mi_row & MAX_MIB_MASK;
   const int read_delta_q_flag = (b_col == 0 && b_row == 0);
-  int rem_bits, thr, bit = 1;
+  int rem_bits, thr;
+  int i, smallval;
+#if CONFIG_EC_ADAPT
+  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
+  (void)cm;
+#else
+  FRAME_CONTEXT *ec_ctx = cm->fc;
+#endif
 
   if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_q_flag) {
+#if !CONFIG_EC_MULTISYMBOL
+    int bit = 1;
     abs = 0;
     while (abs < DELTA_Q_SMALL && bit) {
-      bit = aom_read(r, cm->fc->delta_q_prob[abs], ACCT_STR);
-      if (counts) counts->delta_q[abs][bit]++;
+      bit = aom_read(r, ec_ctx->delta_q_prob[abs], ACCT_STR);
       abs += bit;
     }
-    if (abs == DELTA_Q_SMALL) {
+#else
+    abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
+#endif
+    smallval = (abs < DELTA_Q_SMALL);
+    if (counts) {
+      for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
+      if (smallval) counts->delta_q[abs][0]++;
+    }
+
+    if (!smallval) {
       rem_bits = aom_read_literal(r, 3, ACCT_STR);
       thr = (1 << rem_bits) + 1;
       abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index c39da5b..b057a56 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -556,18 +556,31 @@
 #endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
 
 #if CONFIG_DELTA_Q
-static void write_delta_qindex(const AV1_COMMON *cm, int delta_qindex,
-                               aom_writer *w) {
+static void write_delta_qindex(const AV1_COMMON *cm, const MACROBLOCKD *xd,
+                               int delta_qindex, aom_writer *w) {
   int sign = delta_qindex < 0;
   int abs = sign ? -delta_qindex : delta_qindex;
-  int rem_bits, thr, i = 0;
+  int rem_bits, thr;
   int smallval = abs < DELTA_Q_SMALL ? 1 : 0;
+#if CONFIG_EC_ADAPT
+  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
+  (void)cm;
+#else
+  FRAME_CONTEXT *ec_ctx = cm->fc;
+  (void)xd;
+#endif
 
+#if CONFIG_EC_MULTISYMBOL
+  aom_write_symbol(w, AOMMIN(abs, DELTA_Q_SMALL), ec_ctx->delta_q_cdf,
+                   DELTA_Q_PROBS + 1);
+#else
+  int i = 0;
   while (i < DELTA_Q_SMALL && i <= abs) {
     int bit = (i < abs);
-    aom_write(w, bit, cm->fc->delta_q_prob[i]);
+    aom_write(w, bit, ec_ctx->delta_q_prob[i]);
     i++;
   }
+#endif
 
   if (!smallval) {
     rem_bits = OD_ILOG_NZ(abs - 1) - 1;
@@ -580,6 +593,7 @@
   }
 }
 
+#if !CONFIG_EC_ADAPT
 static void update_delta_q_probs(AV1_COMMON *cm, aom_writer *w,
                                  FRAME_COUNTS *counts) {
   int k;
@@ -593,7 +607,8 @@
                               probwt);
   }
 }
-#endif
+#endif  // CONFIG_EC_ADAPT
+#endif  // CONFIG_DELTA_Q
 
 static void update_skip_probs(AV1_COMMON *cm, aom_writer *w,
                               FRAME_COUNTS *counts) {
@@ -1534,7 +1549,7 @@
     if ((bsize != BLOCK_64X64 || skip == 0) && super_block_upper_left) {
       int reduced_delta_qindex =
           (mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res;
-      write_delta_qindex(cm, reduced_delta_qindex, w);
+      write_delta_qindex(cm, xd, reduced_delta_qindex, w);
       xd->prev_qindex = mbmi->current_q_index;
     }
   }
@@ -1916,7 +1931,7 @@
     if ((bsize != BLOCK_64X64 || skip == 0) && super_block_upper_left) {
       int reduced_delta_qindex =
           (mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res;
-      write_delta_qindex(cm, reduced_delta_qindex, w);
+      write_delta_qindex(cm, xd, reduced_delta_qindex, w);
       xd->prev_qindex = mbmi->current_q_index;
     }
   }
@@ -4720,7 +4735,7 @@
 #endif
 
   update_skip_probs(cm, header_bc, counts);
-#if CONFIG_DELTA_Q
+#if !CONFIG_EC_ADAPT && CONFIG_DELTA_Q
   update_delta_q_probs(cm, header_bc, counts);
 #endif
 #if !CONFIG_EC_ADAPT