EC_ADAPT: do faster adaptation initially.

Symbol count is maintained in the position after the
last CDF value.

Change-Id: I1882e48bfb1404ea7b83ef410298484939601796
diff --git a/aom_dsp/prob.c b/aom_dsp/prob.c
index 7d51dbb..75a0d3f 100644
--- a/aom_dsp/prob.c
+++ b/aom_dsp/prob.c
@@ -205,6 +205,10 @@
   for (i = 1; i < nsymbs; i++) {
     cdf[i] = cdf[i - 1] + cdf[i];
   }
+// Store symbol count at the end of the CDF
+#if CONFIG_EC_ADAPT
+  cdf[nsymbs] = 0;
+#endif
   return nsymbs;
 }
 
diff --git a/aom_dsp/prob.h b/aom_dsp/prob.h
index 14d3bd3..c98586c 100644
--- a/aom_dsp/prob.h
+++ b/aom_dsp/prob.h
@@ -138,7 +138,7 @@
 
 #if CONFIG_EC_ADAPT
 static INLINE void update_cdf(aom_cdf_prob *cdf, int val, int nsymbs) {
-  const int rate = 4 + get_msb(nsymbs);
+  const int rate = 4 + (cdf[nsymbs] > 31) + get_msb(nsymbs);
   const int rate2 = 12 - rate;
   int i, tmp;
   int diff;
@@ -162,6 +162,7 @@
     cdf[i] += diff;
   }
 #endif
+  cdf[nsymbs]++;
 }
 #endif
 
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index 08ea1a5..46dade6 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -207,13 +207,13 @@
 
 #if CONFIG_EC_MULTISYMBOL
 typedef aom_cdf_prob coeff_cdf_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
-                                    [ENTROPY_TOKENS];
+                                    [ENTROPY_TOKENS + CONFIG_EC_ADAPT];
 typedef aom_prob av1_blockz_probs_model[REF_TYPES][BLOCKZ_CONTEXTS];
 typedef unsigned int av1_blockz_count_model[REF_TYPES][BLOCKZ_CONTEXTS][2];
 extern const aom_cdf_prob av1_pareto8_token_probs[COEFF_PROB_MODELS]
                                                  [ENTROPY_TOKENS - 2];
-extern const aom_cdf_prob av1_pareto8_tail_cdfs[COEFF_PROB_MODELS]
-                                               [ENTROPY_TOKENS - 3];
+extern const aom_cdf_prob av1_pareto8_tail_probs[COEFF_PROB_MODELS]
+                                                [ENTROPY_TOKENS - 3];
 struct frame_contexts;
 void av1_coef_pareto_cdfs(struct frame_contexts *fc);
 #endif  // CONFIG_EC_MULTISYMBOL
diff --git a/av1/common/entropymode.c b/av1/common/entropymode.c
index fb8a3ac..1ae3a52 100644
--- a/av1/common/entropymode.c
+++ b/av1/common/entropymode.c
@@ -17,7 +17,8 @@
 #include "av1/common/seg_common.h"
 
 #if CONFIG_EC_MULTISYMBOL
-aom_cdf_prob av1_kf_y_mode_cdf[INTRA_MODES][INTRA_MODES][INTRA_MODES];
+aom_cdf_prob av1_kf_y_mode_cdf[INTRA_MODES][INTRA_MODES]
+                              [INTRA_MODES + CONFIG_EC_ADAPT];
 #endif
 
 #if CONFIG_ALT_INTRA
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index c21454d..8dfae58 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -231,19 +231,22 @@
   aom_prob switchable_restore_prob[RESTORE_SWITCHABLE_TYPES - 1];
 #endif  // CONFIG_LOOP_RESTORATION
 #if CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob y_mode_cdf[BLOCK_SIZE_GROUPS][INTRA_MODES];
-  aom_cdf_prob uv_mode_cdf[INTRA_MODES][INTRA_MODES];
-  aom_cdf_prob partition_cdf[PARTITION_CONTEXTS][PARTITION_TYPES];
+  aom_cdf_prob y_mode_cdf[BLOCK_SIZE_GROUPS][INTRA_MODES + CONFIG_EC_ADAPT];
+  aom_cdf_prob uv_mode_cdf[INTRA_MODES][INTRA_MODES + CONFIG_EC_ADAPT];
+  aom_cdf_prob partition_cdf[PARTITION_CONTEXTS]
+                            [PARTITION_TYPES + CONFIG_EC_ADAPT];
   aom_cdf_prob switchable_interp_cdf[SWITCHABLE_FILTER_CONTEXTS]
-                                    [SWITCHABLE_FILTERS];
-  aom_cdf_prob inter_mode_cdf[INTER_MODE_CONTEXTS][INTER_MODES];
+                                    [SWITCHABLE_FILTERS + CONFIG_EC_ADAPT];
+  aom_cdf_prob inter_mode_cdf[INTER_MODE_CONTEXTS]
+                             [INTER_MODES + CONFIG_EC_ADAPT];
   /* Keep track of kf_y_cdf here, as this makes handling
      multiple copies for adaptation in tiles easier */
-  aom_cdf_prob kf_y_cdf[INTRA_MODES][INTRA_MODES][INTRA_MODES];
-
+  aom_cdf_prob kf_y_cdf[INTRA_MODES][INTRA_MODES]
+                       [INTRA_MODES + CONFIG_EC_ADAPT];
 #if !CONFIG_EXT_TX
-  aom_cdf_prob intra_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES][TX_TYPES];
-  aom_cdf_prob inter_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES];
+  aom_cdf_prob intra_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES]
+                               [TX_TYPES + CONFIG_EC_ADAPT];
+  aom_cdf_prob inter_ext_tx_cdf[EXT_TX_SIZES][TX_TYPES + CONFIG_EC_ADAPT];
 #endif
 #endif
 #if CONFIG_DELTA_Q
@@ -372,7 +375,8 @@
                                         [INTRA_MODES - 1];
 #if CONFIG_EC_MULTISYMBOL
 // CDF version of 'av1_kf_y_mode_prob'.
-extern aom_cdf_prob av1_kf_y_mode_cdf[INTRA_MODES][INTRA_MODES][INTRA_MODES];
+extern aom_cdf_prob av1_kf_y_mode_cdf[INTRA_MODES][INTRA_MODES]
+                                     [INTRA_MODES + CONFIG_EC_ADAPT];
 #endif
 #if CONFIG_PALETTE
 extern const aom_prob av1_default_palette_y_mode_prob[PALETTE_BLOCK_SIZES]
diff --git a/av1/common/entropymv.h b/av1/common/entropymv.h
index 32c4d1d..2715c54 100644
--- a/av1/common/entropymv.h
+++ b/av1/common/entropymv.h
@@ -85,15 +85,15 @@
   aom_prob sign;
   aom_prob classes[MV_CLASSES - 1];
 #if CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob class_cdf[MV_CLASSES];
+  aom_cdf_prob class_cdf[MV_CLASSES + CONFIG_EC_ADAPT];
 #endif
   aom_prob class0[CLASS0_SIZE - 1];
   aom_prob bits[MV_OFFSET_BITS];
   aom_prob class0_fp[CLASS0_SIZE][MV_FP_SIZE - 1];
   aom_prob fp[MV_FP_SIZE - 1];
 #if CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob class0_fp_cdf[CLASS0_SIZE][MV_FP_SIZE];
-  aom_cdf_prob fp_cdf[MV_FP_SIZE];
+  aom_cdf_prob class0_fp_cdf[CLASS0_SIZE][MV_FP_SIZE + CONFIG_EC_ADAPT];
+  aom_cdf_prob fp_cdf[MV_FP_SIZE + CONFIG_EC_ADAPT];
 #endif
   aom_prob class0_hp;
   aom_prob hp;
@@ -102,7 +102,7 @@
 typedef struct {
   aom_prob joints[MV_JOINTS - 1];
 #if CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob joint_cdf[MV_JOINTS];
+  aom_cdf_prob joint_cdf[MV_JOINTS + CONFIG_EC_ADAPT];
 #endif
   nmv_component comps[2];
 } nmv_context;
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index abd9315..e1a9850 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -82,12 +82,14 @@
   int band, c = 0;
   const int tx_size_ctx = txsize_sqr_map[tx_size];
 #if CONFIG_NEW_TOKENSET
-  aom_cdf_prob(*coef_head_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(
+      *coef_head_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_head_cdfs[tx_size_ctx][type][ref];
-  aom_cdf_prob(*coef_tail_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(
+      *coef_tail_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_tail_cdfs[tx_size_ctx][type][ref];
-  aom_cdf_prob(*cdf_head)[ENTROPY_TOKENS];
-  aom_cdf_prob(*cdf_tail)[ENTROPY_TOKENS];
+  aom_cdf_prob(*cdf_head)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
+  aom_cdf_prob(*cdf_tail)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
   int val = 0;
   unsigned int *blockz_count;
 #else
@@ -95,13 +97,13 @@
       ec_ctx->coef_probs[tx_size_ctx][type][ref];
   const aom_prob *prob;
 #if CONFIG_EC_ADAPT
-  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_cdfs[tx_size][type][ref];
-  aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
+  aom_cdf_prob(*cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
 #elif CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_cdfs[tx_size_ctx][type][ref];
-  aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
+  aom_cdf_prob(*cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
 #endif  // CONFIG_EC_ADAPT
 #endif  // CONFIG_NEW_TOKENSET
   unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] = NULL;
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index 9f23b04..c9a6550 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -361,10 +361,10 @@
 }
 
 #if CONFIG_NEW_TOKENSET
-static INLINE void add_token(TOKENEXTRA **t,
-                             aom_cdf_prob (*tail_cdf)[ENTROPY_TOKENS],
-                             aom_cdf_prob (*head_cdf)[ENTROPY_TOKENS],
-                             int is_eob, int32_t extra, uint8_t token) {
+static INLINE void add_token(
+    TOKENEXTRA **t, aom_cdf_prob (*tail_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT],
+    aom_cdf_prob (*head_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT], int is_eob,
+    int32_t extra, uint8_t token) {
   (*t)->token = token;
   (*t)->extra = extra;
   (*t)->tail_cdf = tail_cdf;
@@ -374,12 +374,12 @@
 }
 
 #else
-static INLINE void add_token(TOKENEXTRA **t, const aom_prob *context_tree,
+static INLINE void add_token(
+    TOKENEXTRA **t, const aom_prob *context_tree,
 #if CONFIG_EC_MULTISYMBOL
-                             aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS],
+    aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT],
 #endif  // CONFIG_EC_MULTISYMBOL
-                             int32_t extra, uint8_t token,
-                             uint8_t skip_eob_node, unsigned int *counts) {
+    int32_t extra, uint8_t token, uint8_t skip_eob_node, unsigned int *counts) {
   (*t)->token = token;
   (*t)->extra = extra;
   (*t)->context_tree = context_tree;
@@ -489,16 +489,19 @@
   FRAME_CONTEXT *ec_ctx = cpi->common.fc;
 #endif
 #if CONFIG_NEW_TOKENSET
-  aom_cdf_prob(*const coef_head_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(
+      *const coef_head_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_head_cdfs[tx_size][type][ref];
-  aom_cdf_prob(*const coef_tail_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(
+      *const coef_tail_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_tail_cdfs[tx_size][type][ref];
   unsigned int(*const blockz_count)[2] =
       td->counts->blockz_count[txsize_sqr_map[tx_size]][type][ref];
   int is_eob;
 #else
 #if CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob(*const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
+  aom_cdf_prob(
+      *const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS + CONFIG_EC_ADAPT] =
       ec_ctx->coef_cdfs[tx_size][type][ref];
 #endif
   int skip_eob = 0;
diff --git a/av1/encoder/tokenize.h b/av1/encoder/tokenize.h
index 678aaa5..51a98a5 100644
--- a/av1/encoder/tokenize.h
+++ b/av1/encoder/tokenize.h
@@ -36,11 +36,11 @@
 
 typedef struct {
 #if CONFIG_NEW_TOKENSET
-  aom_cdf_prob (*tail_cdf)[ENTROPY_TOKENS];
-  aom_cdf_prob (*head_cdf)[ENTROPY_TOKENS];
+  aom_cdf_prob (*tail_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
+  aom_cdf_prob (*head_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
   int is_eob;
 #elif CONFIG_EC_MULTISYMBOL
-  aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS];
+  aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
 #endif
   const aom_prob *context_tree;
   EXTRABIT extra;