Merge "Partition the ans experiment into 'ans' and 'rans'" into nextgenv2
diff --git a/aom_dsp/bitreader.h b/aom_dsp/bitreader.h
index d0282f5..ef2e5e9 100644
--- a/aom_dsp/bitreader.h
+++ b/aom_dsp/bitreader.h
@@ -206,7 +206,7 @@
 static INLINE int aom_read_symbol_(aom_reader *r, const aom_cdf_prob *cdf,
                                    int nsymbs ACCT_STR_PARAM) {
   int ret;
-#if CONFIG_ANS
+#if CONFIG_RANS
   (void)nsymbs;
   ret = rans_read(r, cdf);
 #elif CONFIG_DAALA_EC
diff --git a/aom_dsp/bitwriter.h b/aom_dsp/bitwriter.h
index 4b083fa..841a171 100644
--- a/aom_dsp/bitwriter.h
+++ b/aom_dsp/bitwriter.h
@@ -100,7 +100,7 @@
 
 static INLINE void aom_write_symbol(aom_writer *w, int symb,
                                     const aom_cdf_prob *cdf, int nsymbs) {
-#if CONFIG_ANS
+#if CONFIG_RANS
   struct rans_sym s;
   (void)nsymbs;
   assert(cdf);
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index e30ae71..7179e3d 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -406,16 +406,18 @@
   { 255, 246, 247, 255, 239, 255, 253, 255 },
 };
 
-#if CONFIG_ANS
-// Model obtained from a 2-sided zero-centerd distribuition derived
+#if CONFIG_RANS
+// Model obtained from a 2-sided zero-centered distribution derived
 // from a Pareto distribution. The cdf of the distribution is:
 // cdf(x) = 0.5 + 0.5 * sgn(x) * [1 - {alpha/(alpha + |x|)} ^ beta]
 //
-// For a given beta and a given probablity of the 1-node, the alpha
+// For a given beta and a given probability of the 1-node, the alpha
 // is first solved, and then the {alpha, beta} pair is used to generate
 // the probabilities for the rest of the nodes.
 //
-// beta = 8
+// The full source code of the generating program is available in:
+// tools/gen_constrained_tokenset.py
+//
 // Values for tokens ONE_TOKEN through CATEGORY6_TOKEN included here.
 // ZERO_TOKEN and EOB_TOKEN are coded as flags outside this coder.
 const aom_cdf_prob
@@ -676,7 +678,7 @@
       { 32512, 238, 11, 1, 1, 1, 1, 1, 1, 1 },
       { 32640, 117, 4, 1, 1, 1, 1, 1, 1, 1 },
     };
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 
 /* clang-format off */
 #if CONFIG_ENTROPY
@@ -2801,7 +2803,7 @@
   extend_to_full_distribution(&full[UNCONSTRAINED_NODES], model[PIVOT_NODE]);
 }
 
-#if CONFIG_ANS
+#if CONFIG_RANS
 static void build_token_cdfs(const aom_prob *pdf_model,
                              aom_cdf_prob cdf[ENTROPY_TOKENS]) {
   int i, sum = 0;
@@ -2818,12 +2820,11 @@
     for (i = 0; i < PLANE_TYPES; ++i)
       for (j = 0; j < REF_TYPES; ++j)
         for (k = 0; k < COEF_BANDS; ++k)
-          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
+          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
             build_token_cdfs(fc->coef_probs[t][i][j][k][l],
                              fc->coef_cdfs[t][i][j][k][l]);
-          }
 }
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 
 void av1_default_coef_probs(AV1_COMMON *cm) {
 #if CONFIG_ENTROPY
@@ -2836,9 +2837,9 @@
   av1_copy(cm->fc->coef_probs[TX_16X16], default_coef_probs_16x16);
   av1_copy(cm->fc->coef_probs[TX_32X32], default_coef_probs_32x32);
 #endif  // CONFIG_ENTROPY
-#if CONFIG_ANS
+#if CONFIG_RANS
   av1_coef_pareto_cdfs(cm->fc);
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 }
 
 static void adapt_coef_probs(AV1_COMMON *cm, TX_SIZE tx_size,
@@ -2905,9 +2906,9 @@
 #endif  // CONFIG_ENTROPY
   for (t = TX_4X4; t <= TX_32X32; t++)
     adapt_coef_probs(cm, t, count_sat, update_factor);
-#if CONFIG_ANS
+#if CONFIG_RANS
   av1_coef_pareto_cdfs(cm->fc);
-#endif
+#endif  // CONFIG_RANS
 }
 
 #if CONFIG_ENTROPY
diff --git a/av1/common/entropy.h b/av1/common/entropy.h
index 47825b4..15b50db 100644
--- a/av1/common/entropy.h
+++ b/av1/common/entropy.h
@@ -12,12 +12,13 @@
 #ifndef AV1_COMMON_ENTROPY_H_
 #define AV1_COMMON_ENTROPY_H_
 
+#include "./aom_config.h"
 #include "aom/aom_integer.h"
+#if CONFIG_RANS
+#include "aom_dsp/ans.h"
+#endif  // CONFIG_RANS
 #include "aom_dsp/prob.h"
 
-#if CONFIG_ANS
-#include "aom_dsp/ans.h"
-#endif  // CONFIG_ANS
 #include "av1/common/common.h"
 #include "av1/common/enums.h"
 
@@ -190,12 +191,12 @@
 #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
 extern const aom_tree_index av1_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
 extern const aom_prob av1_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
 typedef aom_cdf_prob coeff_cdf_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
                                     [ENTROPY_TOKENS];
 extern const aom_cdf_prob av1_pareto8_token_probs[COEFF_PROB_MODELS]
                                                  [ENTROPY_TOKENS - 2];
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 
 typedef aom_prob av1_coeff_probs_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS]
                                       [UNCONSTRAINED_NODES];
@@ -265,10 +266,10 @@
   return combine_entropy_contexts(above_ec, left_ec);
 }
 
-#if CONFIG_ANS
+#if CONFIG_RANS
 struct frame_contexts;
 void av1_coef_pareto_cdfs(struct frame_contexts *fc);
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 
 #if CONFIG_ENTROPY
 #define COEF_COUNT_SAT_BITS 5
diff --git a/av1/common/entropymode.h b/av1/common/entropymode.h
index 11e76a9..02643c0 100644
--- a/av1/common/entropymode.h
+++ b/av1/common/entropymode.h
@@ -56,9 +56,9 @@
   aom_prob partition_prob[PARTITION_CONTEXTS][PARTITION_TYPES - 1];
 #endif
   av1_coeff_probs_model coef_probs[TX_SIZES][PLANE_TYPES];
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
   coeff_cdf_model coef_cdfs[TX_SIZES][PLANE_TYPES];
-#endif
+#endif  // CONFIG_RANS
   aom_prob switchable_interp_prob[SWITCHABLE_FILTER_CONTEXTS]
                                  [SWITCHABLE_FILTERS - 1];
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 790a008..0fbf659 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1930,9 +1930,9 @@
   TX_SIZE tx_size;
   for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
     read_coef_probs_common(fc->coef_probs[tx_size], r);
-#if CONFIG_ANS
+#if CONFIG_RANS
   av1_coef_pareto_cdfs(fc);
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 }
 
 static void setup_segmentation(AV1_COMMON *const cm,
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index 1bd4c0d..7077788 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -76,11 +76,11 @@
   const aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
       fc->coef_probs[tx_size_ctx][type][ref];
   const aom_prob *prob;
-#if CONFIG_ANS
+#if CONFIG_RANS
   const aom_cdf_prob(*const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
       fc->coef_cdfs[tx_size_ctx][type][ref];
   const aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
   unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
   unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
   uint8_t token_cache[MAX_TX_SQUARE];
@@ -166,7 +166,7 @@
       dqv_val = &dq_val[band][0];
 #endif  // CONFIG_NEW_QUANT
     }
-#if CONFIG_ANS
+#if CONFIG_RANS
     cdf = &coef_cdfs[band][ctx];
     token = ONE_TOKEN +
             aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1, ACCT_STR);
@@ -263,7 +263,7 @@
         }
       }
     }
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 #if CONFIG_NEW_QUANT
     v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
     v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
@@ -368,13 +368,7 @@
 
 int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
                             const SCAN_ORDER *sc, int x, int y, TX_SIZE tx_size,
-                            TX_TYPE tx_type,
-#if CONFIG_ANS
-                            struct AnsDecoder *const r,
-#else
-                            aom_reader *r,
-#endif  // CONFIG_ANS
-                            int seg_id) {
+                            TX_TYPE tx_type, aom_reader *r, int seg_id) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
   const int16_t *const dequant = pd->seg_dequant[seg_id];
   const int ctx =
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 1de40fc..6326001 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -702,11 +702,11 @@
   while (p < stop && p->token != EOSB_TOKEN) {
     const int token = p->token;
     aom_tree_index index = 0;
-#if !CONFIG_ANS
+#if !CONFIG_RANS
     const struct av1_token *const coef_encoding = &av1_coef_encodings[token];
     int coef_value = coef_encoding->value;
     int coef_length = coef_encoding->len;
-#endif  // !CONFIG_ANS
+#endif  // !CONFIG_RANS
 #if CONFIG_AOM_HIGHBITDEPTH
     const av1_extra_bit *const extra_bits_av1 =
         (bit_depth == AOM_BITS_12)
@@ -718,7 +718,7 @@
     (void)bit_depth;
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
-#if CONFIG_ANS
+#if CONFIG_RANS
     /* skip one or two nodes */
     if (!p->skip_eob_node) aom_write(w, token != EOB_TOKEN, p->context_tree[0]);
 
@@ -751,7 +751,7 @@
         }
       }
     }
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 
     if (extra_bits_av1->base_val) {
       const int extra_bits = p->extra;
@@ -1661,7 +1661,7 @@
 #define write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \
                               mi_row, mi_col)                              \
   write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col)
-#endif  // CONFIG_ANS && CONFIG_SUPERTX
+#endif  // CONFIG_SUPERTX
 
 static void write_modes_b(AV1_COMP *cpi, const TileInfo *const tile,
                           aom_writer *w, const TOKENEXTRA **tok,
@@ -1675,11 +1675,11 @@
   MODE_INFO *m;
   int plane;
   int bh, bw;
-#if CONFIG_ANS
+#if CONFIG_RANS
   (void)tok;
   (void)tok_end;
   (void)plane;
-#endif  // !CONFIG_ANS
+#endif  // !CONFIG_RANS
 
   xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
   m = xd->mi[0];
@@ -1854,7 +1854,7 @@
 #define write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \
                                mi_row, mi_col, bsize)                       \
   write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, bsize)
-#endif  // CONFIG_ANS && CONFIG_SUPERTX
+#endif  // CONFIG_SUPERTX
 
 static void write_modes_sb(AV1_COMP *const cpi, const TileInfo *const tile,
                            aom_writer *const w, const TOKENEXTRA **tok,
@@ -2471,9 +2471,9 @@
   const TX_MODE tx_mode = cpi->common.tx_mode;
   const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
   TX_SIZE tx_size;
-#if CONFIG_ANS
+#if CONFIG_RANS
   int update = 0;
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 #if CONFIG_ENTROPY
   AV1_COMMON *cm = &cpi->common;
   SUBFRAME_STATS *subframe_stats = &cpi->subframe_stats;
@@ -2521,18 +2521,18 @@
 
         update_coef_probs_subframe(w, cpi, tx_size, cpi->branch_ct_buf,
                                    frame_coef_probs);
-#if CONFIG_ANS
+#if CONFIG_RANS
         update = 1;
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
       } else {
 #endif  // CONFIG_ENTROPY
         build_tree_distribution(cpi, tx_size, frame_branch_ct,
                                 frame_coef_probs);
         update_coef_probs_common(w, cpi, tx_size, frame_branch_ct,
                                  frame_coef_probs);
-#if CONFIG_ANS
+#if CONFIG_RANS
         update = 1;
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 #if CONFIG_ENTROPY
       }
 #endif  // CONFIG_ENTROPY
@@ -2557,9 +2557,9 @@
     av1_copy(cm->counts.eob_branch, eob_counts_copy);
   }
 #endif  // CONFIG_ENTROPY
-#if CONFIG_ANS
+#if CONFIG_RANS
   if (update) av1_coef_pareto_cdfs(cpi->common.fc);
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
 }
 
 #if CONFIG_LOOP_RESTORATION
diff --git a/av1/encoder/tokenize.c b/av1/encoder/tokenize.c
index e95e52b..561beed 100644
--- a/av1/encoder/tokenize.c
+++ b/av1/encoder/tokenize.c
@@ -336,12 +336,12 @@
 };
 #endif
 
-#if !CONFIG_ANS
+#if !CONFIG_RANS
 const struct av1_token av1_coef_encodings[ENTROPY_TOKENS] = {
   { 2, 2 },  { 6, 3 },   { 28, 5 },  { 58, 6 },  { 59, 6 },  { 60, 6 },
   { 61, 6 }, { 124, 7 }, { 125, 7 }, { 126, 7 }, { 127, 7 }, { 0, 1 }
 };
-#endif  // !CONFIG_ANS
+#endif  // !CONFIG_RANS
 
 struct tokenize_b_args {
   const AV1_COMP *cpi;
@@ -386,17 +386,17 @@
 }
 
 static INLINE void add_token(TOKENEXTRA **t, const aom_prob *context_tree,
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
                              const aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS],
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
                              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;
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
   (*t)->token_cdf = token_cdf;
-#endif  // CONFIG_ANS
+#endif  // CONFIG_RANS
   (*t)->skip_eob_node = skip_eob_node;
   (*t)++;
   ++counts[token];
@@ -489,10 +489,10 @@
   aom_prob(*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
       cpi->common.fc->coef_probs[txsize_sqr_map[tx_size]][type][ref];
 #endif  // CONFIG_ENTROPY
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
   aom_cdf_prob(*const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
       cpi->common.fc->coef_cdfs[tx_size][type][ref];
-#endif  // CONFIG_ANS
+#endif
   unsigned int(*const eob_branch)[COEFF_CONTEXTS] =
       td->counts->eob_branch[txsize_sqr_map[tx_size]][type][ref];
   const uint8_t *const band = get_band_translate(tx_size);
@@ -513,9 +513,9 @@
     av1_get_token_extra(v, &token, &extra);
 
     add_token(&t, coef_probs[band[c]][pt],
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
               (const aom_cdf_prob(*)[ENTROPY_TOKENS]) & coef_cdfs[band[c]][pt],
-#endif  // CONFIG_ANS
+#endif
               extra, (uint8_t)token, (uint8_t)skip_eob, counts[band[c]][pt]);
 
     token_cache[scan[c]] = av1_pt_energy_class[token];
@@ -525,7 +525,7 @@
   }
   if (c < seg_eob) {
     add_token(&t, coef_probs[band[c]][pt],
-#if CONFIG_ANS || CONFIG_DAALA_EC
+#if CONFIG_RANS || CONFIG_DAALA_EC
               NULL,
 #endif
               0, EOB_TOKEN, 0, counts[band[c]][pt]);
diff --git a/av1/encoder/tokenize.h b/av1/encoder/tokenize.h
index e869a19..ae896a6 100644
--- a/av1/encoder/tokenize.h
+++ b/av1/encoder/tokenize.h
@@ -46,9 +46,9 @@
 
 extern const aom_tree_index av1_coef_tree[];
 extern const aom_tree_index av1_coef_con_tree[];
-#if !CONFIG_ANS
+#if !CONFIG_RANS
 extern const struct av1_token av1_coef_encodings[];
-#endif  // !CONFIG_ANS
+#endif  // !CONFIG_RANS
 
 int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
 int av1_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
diff --git a/configure b/configure
index c396632..7901ef3 100755
--- a/configure
+++ b/configure
@@ -271,6 +271,7 @@
     new_quant
     supertx
     ans
+    rans
     loop_restoration
     ext_partition
     ext_partition_types