Merge "Refactor ref mv stack system" into nextgenv2
diff --git a/configure b/configure
index 29b1da8..f94546e 100755
--- a/configure
+++ b/configure
@@ -281,6 +281,7 @@
     ext_interp
     ext_refs
     supertx
+    ans
 "
 CONFIG_LIST="
     dependency_tracking
diff --git a/test/test.mk b/test/test.mk
index 471f870..81f20fb 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -165,6 +165,7 @@
 
 LIBVPX_TEST_SRCS-yes                    += vp10_inv_txfm_test.cc
 LIBVPX_TEST_SRCS-$(CONFIG_VP10_ENCODER) += vp10_dct_test.cc
+LIBVPX_TEST_SRCS-$(CONFIG_ANS)          += vp10_ans_test.cc
 
 endif # VP10
 
diff --git a/test/vp10_ans_test.cc b/test/vp10_ans_test.cc
new file mode 100644
index 0000000..441583a
--- /dev/null
+++ b/test/vp10_ans_test.cc
@@ -0,0 +1,337 @@
+/*
+ *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <assert.h>
+#include <math.h>
+#include <stdio.h>
+#include <ctime>
+#include <utility>
+#include <vector>
+
+#include "third_party/googletest/src/include/gtest/gtest.h"
+
+#include "test/acm_random.h"
+#include "vp10/common/ans.h"
+#include "vp10/encoder/treewriter.h"
+#include "vpx_dsp/bitreader.h"
+#include "vpx_dsp/bitwriter.h"
+
+namespace {
+typedef std::vector<std::pair<uint8_t, bool> > PvVec;
+
+PvVec abs_encode_build_vals(int iters) {
+  PvVec ret;
+  libvpx_test::ACMRandom gen(0x30317076);
+  double entropy = 0;
+  for (int i = 0; i < iters; ++i) {
+    uint8_t p;
+    do {
+      p = gen.Rand8();
+    } while (p == 0);  // zero is not a valid coding probability
+    bool b = gen.Rand8() < p;
+    ret.push_back(std::make_pair(static_cast<uint8_t>(p), b));
+    double d = p / 256.;
+    entropy += -d * log2(d) - (1 - d) * log2(1 - d);
+  }
+  printf("entropy %f\n", entropy);
+  return ret;
+}
+
+bool check_rabs(const PvVec &pv_vec, uint8_t *buf) {
+  AnsCoder a;
+  ans_write_init(&a, buf);
+
+  std::clock_t start = std::clock();
+  for (PvVec::const_reverse_iterator it = pv_vec.rbegin(); it != pv_vec.rend();
+       ++it) {
+    rabs_write(&a, it->second, 256 - it->first);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  int offset = ans_write_end(&a);
+  bool okay = true;
+  AnsDecoder d;
+  if (ans_read_init(&d, buf, offset)) return false;
+  start = std::clock();
+  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
+    okay &= rabs_read(&d, 256 - it->first) == it->second;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  if (!okay) return false;
+  printf("rABS size %d enc_time %f dec_time %f\n", offset,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return ans_read_end(&d);
+}
+
+bool check_rabs_asc(const PvVec &pv_vec, uint8_t *buf) {
+  AnsCoder a;
+  ans_write_init(&a, buf);
+
+  std::clock_t start = std::clock();
+  for (PvVec::const_reverse_iterator it = pv_vec.rbegin(); it != pv_vec.rend();
+       ++it) {
+    rabs_asc_write(&a, it->second, 256 - it->first);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  int offset = ans_write_end(&a);
+  bool okay = true;
+  AnsDecoder d;
+  if (ans_read_init(&d, buf, offset)) return false;
+  start = std::clock();
+  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
+    okay &= rabs_asc_read(&d, 256 - it->first) == it->second;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  if (!okay) return false;
+  printf("rABS (asc) size %d enc_time %f dec_time %f\n", offset,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return ans_read_end(&d);
+}
+
+bool check_uabs(const PvVec &pv_vec, uint8_t *buf) {
+  AnsCoder a;
+  ans_write_init(&a, buf);
+
+  std::clock_t start = std::clock();
+  for (PvVec::const_reverse_iterator it = pv_vec.rbegin(); it != pv_vec.rend();
+       ++it) {
+    uabs_write(&a, it->second, 256 - it->first);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  int offset = ans_write_end(&a);
+  bool okay = true;
+  AnsDecoder d;
+  if (ans_read_init(&d, buf, offset)) return false;
+  start = std::clock();
+  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
+    okay &= uabs_read(&d, 256 - it->first) == it->second;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  if (!okay) return false;
+  printf("uABS size %d enc_time %f dec_time %f\n", offset,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return ans_read_end(&d);
+}
+
+bool check_vpxbool(const PvVec &pv_vec, uint8_t *buf) {
+  vpx_writer w;
+  vpx_reader r;
+  vpx_start_encode(&w, buf);
+
+  std::clock_t start = std::clock();
+  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
+    vpx_write(&w, it->second, 256 - it->first);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  vpx_stop_encode(&w);
+  bool okay = true;
+  vpx_reader_init(&r, buf, w.pos, NULL, NULL);
+  start = std::clock();
+  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
+    okay &= vpx_read(&r, 256 - it->first) == it->second;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  printf("VPX size %d enc_time %f dec_time %f\n", w.pos,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return okay;
+}
+
+const rans_sym rans_sym_tab[] = {
+    {70, 186}, {70, 116}, {100, 16}, {16, 0},
+};
+const int kDistinctSyms = sizeof(rans_sym_tab) / sizeof(rans_sym_tab[0]);
+
+std::vector<int> ans_encode_build_vals(const rans_sym *tab, int iters) {
+  std::vector<int> p_to_sym;
+  int i = 0;
+  while (p_to_sym.size() < 256) {
+    p_to_sym.insert(p_to_sym.end(), tab[i].prob, i);
+    ++i;
+  }
+  assert(p_to_sym.size() == 256);
+  std::vector<int> ret;
+  libvpx_test::ACMRandom gen(18543637);
+  for (int i = 0; i < iters; ++i) {
+    int sym = p_to_sym[gen.Rand8()];
+    ret.push_back(sym);
+  }
+  return ret;
+}
+
+void rans_build_dec_tab(const struct rans_sym sym_tab[],
+                        rans_dec_lut dec_tab) {
+  int val = 0;
+  int i;
+  for (i = ans_p8_precision - 1; i >= 0; --i) {
+    dec_tab[i].val = val;
+    dec_tab[i].prob = sym_tab[val].prob;
+    dec_tab[i].cum_prob = sym_tab[val].cum_prob;
+    if (i == sym_tab[val].cum_prob) ++val;
+  }
+}
+
+bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab,
+                uint8_t *buf) {
+  AnsCoder a;
+  ans_write_init(&a, buf);
+  rans_dec_lut dec_tab;
+  rans_build_dec_tab(tab, dec_tab);
+
+  std::clock_t start = std::clock();
+  for (std::vector<int>::const_reverse_iterator it = sym_vec.rbegin();
+       it != sym_vec.rend(); ++it) {
+    rans_write(&a, &tab[*it]);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  int offset = ans_write_end(&a);
+  bool okay = true;
+  AnsDecoder d;
+  if (ans_read_init(&d, buf, offset)) return false;
+  start = std::clock();
+  for (std::vector<int>::const_iterator it = sym_vec.begin();
+       it != sym_vec.end(); ++it) {
+    okay &= rans_read(&d, dec_tab) == *it;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  if (!okay) return false;
+  printf("rANS size %d enc_time %f dec_time %f\n", offset,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return ans_read_end(&d);
+}
+
+void build_tree(vpx_tree_index *tree, int num_syms) {
+  vpx_tree_index i;
+  int sym = 0;
+  for (i = 0; i < num_syms - 1; ++i) {
+    tree[2 * i] = sym--;
+    tree[2 * i + 1] = 2 * (i + 1);
+  }
+  tree[2 * i - 1] = sym;
+}
+
+// treep are the probabilites of tree nodes like:
+//          *
+//         / \
+//    -sym0  *
+//          / \
+//     -sym1  *
+//           / \
+//      -sym2  -sym3
+void tab2tree(const rans_sym *tab, int tab_size, vpx_prob *treep) {
+  const unsigned basep = 256;
+  unsigned pleft = basep;
+  for (int i = 0; i < tab_size - 1; ++i) {
+    unsigned prob = (tab[i].prob * basep + (basep / 2)) / pleft;
+    assert(prob > 0 && prob < 256);
+    treep[i] = prob;
+    pleft -= tab[i].prob;
+  }
+}
+
+struct sym_bools {
+  unsigned bits;
+  int len;
+};
+
+static void make_tree_bits_tab(sym_bools *tab, int num_syms) {
+  unsigned bits = 0;
+  int len = 0;
+  int i;
+  for (i = 0; i < num_syms - 1; ++i) {
+    bits *= 2;
+    ++len;
+    tab[i].bits = bits;
+    tab[i].len = len;
+    ++bits;
+  }
+  tab[i].bits = bits;
+  tab[i].len = len;
+}
+
+void build_tpb(vpx_prob probs[/*num_syms*/],
+               vpx_tree_index tree[/*2*num_syms*/],
+               sym_bools bit_len[/*num_syms*/],
+               const rans_sym sym_tab[/*num_syms*/], int num_syms) {
+  tab2tree(sym_tab, num_syms, probs);
+  build_tree(tree, num_syms);
+  make_tree_bits_tab(bit_len, num_syms);
+}
+
+bool check_vpxtree(const std::vector<int> &sym_vec, const rans_sym *sym_tab,
+                   uint8_t *buf) {
+  vpx_writer w;
+  vpx_reader r;
+  vpx_start_encode(&w, buf);
+
+  vpx_prob probs[kDistinctSyms];
+  vpx_tree_index tree[2 * kDistinctSyms];
+  sym_bools bit_len[kDistinctSyms];
+  build_tpb(probs, tree, bit_len, sym_tab, kDistinctSyms);
+
+  std::clock_t start = std::clock();
+  for (std::vector<int>::const_iterator it = sym_vec.begin();
+       it != sym_vec.end(); ++it) {
+    vp10_write_tree(&w, tree, probs, bit_len[*it].bits, bit_len[*it].len, 0);
+  }
+  std::clock_t enc_time = std::clock() - start;
+  vpx_stop_encode(&w);
+  vpx_reader_init(&r, buf, w.pos, NULL, NULL);
+  start = std::clock();
+  for (std::vector<int>::const_iterator it = sym_vec.begin();
+       it != sym_vec.end(); ++it) {
+    if (vpx_read_tree(&r, tree, probs) != *it) return false;
+  }
+  std::clock_t dec_time = std::clock() - start;
+  printf("VPXtree size %u enc_time %f dec_time %f\n", w.pos,
+         static_cast<float>(enc_time) / CLOCKS_PER_SEC,
+         static_cast<float>(dec_time) / CLOCKS_PER_SEC);
+  return true;
+}
+
+class Vp10AbsTest : public ::testing::Test {
+ protected:
+  static void SetUpTestCase() { pv_vec_ = abs_encode_build_vals(kNumBools); }
+  virtual void SetUp() { buf_ = new uint8_t[kNumBools / 8]; }
+  virtual void TearDown() { delete[] buf_; }
+  static const int kNumBools = 100000000;
+  static PvVec pv_vec_;
+  uint8_t *buf_;
+};
+PvVec Vp10AbsTest::pv_vec_;
+
+class Vp10AnsTest : public ::testing::Test {
+ protected:
+  static void SetUpTestCase() {
+    sym_vec_ = ans_encode_build_vals(rans_sym_tab, kNumSyms);
+  }
+  virtual void SetUp() { buf_ = new uint8_t[kNumSyms / 2]; }
+  virtual void TearDown() { delete[] buf_; }
+  static const int kNumSyms = 25000000;
+  static std::vector<int> sym_vec_;
+  uint8_t *buf_;
+};
+std::vector<int> Vp10AnsTest::sym_vec_;
+
+TEST_F(Vp10AbsTest, Vpxbool) { EXPECT_TRUE(check_vpxbool(pv_vec_, buf_)); }
+TEST_F(Vp10AbsTest, Rabs) { EXPECT_TRUE(check_rabs(pv_vec_, buf_)); }
+TEST_F(Vp10AbsTest, RabsAsc) { EXPECT_TRUE(check_rabs_asc(pv_vec_, buf_)); }
+TEST_F(Vp10AbsTest, Uabs) { EXPECT_TRUE(check_uabs(pv_vec_, buf_)); }
+
+TEST_F(Vp10AnsTest, Rans) {
+  EXPECT_TRUE(check_rans(sym_vec_, rans_sym_tab, buf_));
+}
+TEST_F(Vp10AnsTest, Vpxtree) {
+  EXPECT_TRUE(check_vpxtree(sym_vec_, rans_sym_tab, buf_));
+}
+}  // namespace
diff --git a/vp10/common/ans.h b/vp10/common/ans.h
new file mode 100644
index 0000000..a1862f3
--- /dev/null
+++ b/vp10/common/ans.h
@@ -0,0 +1,329 @@
+/*
+ *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VP10_COMMON_ANS_H_
+#define VP10_COMMON_ANS_H_
+// An implementation of Asymmetric Numeral Systems
+// http://arxiv.org/abs/1311.2540v2
+
+#include <assert.h>
+#include "./vpx_config.h"
+#include "vpx/vpx_integer.h"
+#include "vpx_ports/mem_ops.h"
+
+#define ANS_DIVIDE_BY_MULTIPLY 1
+#if ANS_DIVIDE_BY_MULTIPLY
+#include "vp10/common/divide.h"
+#define ANS_DIVREM(quotient, remainder, dividend, divisor) \
+  do { \
+    quotient = fastdiv(dividend, divisor); \
+    remainder = dividend - quotient * divisor; \
+  } while (0)
+#define ANS_DIV(dividend, divisor) \
+  fastdiv(dividend, divisor)
+#else
+#define ANS_DIVREM(quotient, remainder, dividend, divisor) \
+  do { \
+    quotient = dividend / divisor; \
+    remainder = dividend % divisor; \
+  } while (0)
+#define ANS_DIV(dividend, divisor) \
+    ((dividend) / (divisor))
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+struct AnsCoder {
+  uint8_t *buf;
+  int buf_offset;
+  uint32_t state;
+};
+
+struct AnsDecoder {
+  const uint8_t *buf;
+  int buf_offset;
+  uint32_t state;
+};
+
+typedef uint8_t AnsP8;
+#define ans_p8_precision 256u
+#define ans_p8_shift 8
+#define l_base (ans_p8_precision * 4)  // l_base % precision must be 0
+#define io_base 256
+// Range I = { l_base, l_base + 1, ..., l_base * io_base - 1 }
+
+static INLINE void ans_write_init(struct AnsCoder *const ans,
+                                  uint8_t *const buf) {
+  ans->buf = buf;
+  ans->buf_offset = 0;
+  ans->state = l_base;
+}
+
+static INLINE int ans_write_end(struct AnsCoder *const ans) {
+  uint32_t state;
+  assert(ans->state >= l_base);
+  assert(ans->state < l_base * io_base);
+  state = ans->state - l_base;
+  if (state < (1 << 6)) {
+    ans->buf[ans->buf_offset] = (0 << 6) + state;
+    return ans->buf_offset + 1;
+  } else if (state < (1 << 14)) {
+    mem_put_le16(ans->buf + ans->buf_offset, (1 << 14) + state);
+    return ans->buf_offset + 2;
+  } else {
+    mem_put_le24(ans->buf + ans->buf_offset, (1 << 23) + state);
+    return ans->buf_offset + 3;
+  }
+}
+
+// rABS with descending spread
+// p or p0 takes the place of l_s from the paper
+// ans_p8_precision is m
+static INLINE void rabs_desc_write(struct AnsCoder *ans, int val, AnsP8 p0) {
+  const AnsP8 p = ans_p8_precision - p0;
+  const unsigned l_s = val ? p : p0;
+  unsigned quot, rem;
+  if (ans->state >= l_base / ans_p8_precision * io_base * l_s) {
+    ans->buf[ans->buf_offset++] = ans->state % io_base;
+    ans->state /= io_base;
+  }
+  ANS_DIVREM(quot, rem, ans->state, l_s);
+  ans->state = quot * ans_p8_precision + rem + (val ? 0 : p);
+}
+
+#define ANS_IMPL1 0
+#define UNPREDICTABLE(x) x
+static INLINE int rabs_desc_read(struct AnsDecoder *ans, AnsP8 p0) {
+  int val;
+#if ANS_IMPL1
+  unsigned l_s;
+#else
+  unsigned quot, rem, x, xn;
+#endif
+  const AnsP8 p = ans_p8_precision - p0;
+  if (ans->state < l_base) {
+    ans->state = ans->state * io_base + ans->buf[--ans->buf_offset];
+  }
+#if ANS_IMPL1
+  val = ans->state % ans_p8_precision < p;
+  l_s = val ? p : p0;
+  ans->state = (ans->state / ans_p8_precision) * l_s +
+               ans->state % ans_p8_precision - (!val * p);
+#else
+  x = ans->state;
+  quot = x / ans_p8_precision;
+  rem = x % ans_p8_precision;
+  xn = quot * p;
+  val = rem < p;
+  if (UNPREDICTABLE(val)) {
+    ans->state = xn + rem;
+  } else {
+    // ans->state = quot * p0 + rem - p;
+    ans->state = x - xn - p;
+  }
+#endif
+  return val;
+}
+
+// rABS with ascending spread
+// p or p0 takes the place of l_s from the paper
+// ans_p8_precision is m
+static INLINE void rabs_asc_write(struct AnsCoder *ans, int val, AnsP8 p0) {
+  const AnsP8 p = ans_p8_precision - p0;
+  const unsigned l_s = val ? p : p0;
+  unsigned quot, rem;
+  if (ans->state >= l_base / ans_p8_precision * io_base * l_s) {
+    ans->buf[ans->buf_offset++] = ans->state % io_base;
+    ans->state /= io_base;
+  }
+  ANS_DIVREM(quot, rem, ans->state, l_s);
+  ans->state = quot * ans_p8_precision + rem + (val ? p0 : 0);
+}
+
+static INLINE int rabs_asc_read(struct AnsDecoder *ans, AnsP8 p0) {
+  int val;
+#if ANS_IMPL1
+  unsigned l_s;
+#else
+  unsigned quot, rem, x, xn;
+#endif
+  const AnsP8 p = ans_p8_precision - p0;
+  if (ans->state < l_base) {
+    ans->state = ans->state * io_base + ans->buf[--ans->buf_offset];
+  }
+#if ANS_IMPL1
+  val = ans->state % ans_p8_precision < p;
+  l_s = val ? p : p0;
+  ans->state = (ans->state / ans_p8_precision) * l_s +
+               ans->state % ans_p8_precision - (!val * p);
+#else
+  x = ans->state;
+  quot = x / ans_p8_precision;
+  rem = x % ans_p8_precision;
+  xn = quot * p;
+  val = rem >= p0;
+  if (UNPREDICTABLE(val)) {
+    ans->state = xn + rem - p0;
+  } else {
+    // ans->state = quot * p0 + rem - p0;
+    ans->state = x - xn;
+  }
+#endif
+  return val;
+}
+
+#define rabs_read rabs_desc_read
+#define rabs_write rabs_desc_write
+
+// uABS with normalization
+static INLINE void uabs_write(struct AnsCoder *ans, int val, AnsP8 p0) {
+  AnsP8 p = ans_p8_precision - p0;
+  const unsigned l_s = val ? p : p0;
+  if (ans->state >= l_base / ans_p8_precision * io_base * l_s) {
+    ans->buf[ans->buf_offset++] = ans->state % io_base;
+    ans->state /= io_base;
+  }
+  if (!val)
+    ans->state = ANS_DIV(ans->state * ans_p8_precision, p0);
+  else
+    ans->state = ANS_DIV((ans->state + 1) * ans_p8_precision + p - 1, p) - 1;
+}
+
+static INLINE int uabs_read(struct AnsDecoder *ans, AnsP8 p0) {
+  AnsP8 p = ans_p8_precision - p0;
+  int s;
+  // unsigned int xp1;
+  unsigned xp, sp;
+  unsigned state = ans->state;
+  if (state < l_base && ans->buf_offset > 0) {
+    state = state * io_base + ans->buf[--ans->buf_offset];
+  }
+  sp = state * p;
+  // xp1 = (sp + p) / ans_p8_precision;
+  xp = sp / ans_p8_precision;
+  // s = xp1 - xp;
+  s = (sp & 0xFF) >= p0;
+  if (UNPREDICTABLE(s))
+    ans->state = xp;
+  else
+    ans->state = state - xp;
+  return s;
+}
+
+static INLINE int uabs_read_bit(struct AnsDecoder *ans) {
+  int s;
+  unsigned state = ans->state;
+  if (state < l_base && ans->buf_offset > 0) {
+    state = state * io_base + ans->buf[--ans->buf_offset];
+  }
+  s = (int)(state & 1);
+  ans->state = state >> 1;
+  return s;
+}
+
+struct rans_sym {
+  AnsP8 prob;
+  AnsP8 cum_prob;  // not-inclusive
+};
+
+struct rans_dec_sym {
+  uint8_t val;
+  AnsP8 prob;
+  AnsP8 cum_prob;  // not-inclusive
+};
+
+typedef struct rans_dec_sym rans_dec_lut[ans_p8_precision];
+
+static INLINE void rans_build_dec_tab(const AnsP8 token_probs[],
+                                      rans_dec_lut dec_tab) {
+  int val = 0;
+  int cum_prob = 0;
+  int sym_end = token_probs[0];
+  int i;
+  for (i = 0; i < 256; ++i) {
+    if (i == sym_end) {
+      ++val;
+      cum_prob = sym_end;
+      sym_end += token_probs[val];
+    }
+    dec_tab[i].val = val;
+    dec_tab[i].prob = token_probs[val];
+    dec_tab[i].cum_prob = cum_prob;
+  }
+}
+
+// rANS with normalization
+// sym->prob takes the place of l_s from the paper
+// ans_p8_precision is m
+static INLINE void rans_write(struct AnsCoder *ans,
+                              const struct rans_sym *const sym) {
+  const AnsP8 p = sym->prob;
+  if (ans->state >= l_base / ans_p8_precision * io_base * p) {
+    ans->buf[ans->buf_offset++] = ans->state % io_base;
+    ans->state /= io_base;
+  }
+  ans->state =
+      (ans->state / p) * ans_p8_precision + ans->state % p + sym->cum_prob;
+}
+
+static INLINE int rans_read(struct AnsDecoder *ans,
+                            const rans_dec_lut tab) {
+  unsigned rem;
+  unsigned quo;
+  int val;
+  if (ans->state < l_base && ans->buf_offset > 0) {
+    ans->state = ans->state * io_base + ans->buf[--ans->buf_offset];
+  }
+  quo = ans->state / ans_p8_precision;
+  rem = ans->state % ans_p8_precision;
+  val = tab[rem].val;
+
+  ans->state = quo * tab[rem].prob + rem - tab[rem].cum_prob;
+  return val;
+}
+
+static INLINE int ans_read_init(struct AnsDecoder *const ans,
+                                const uint8_t *const buf,
+                                int offset) {
+  unsigned x;
+  if (offset < 1) return 1;
+  ans->buf = buf;
+  x = buf[offset - 1] >> 6;
+  if (x == 0) {
+    ans->buf_offset = offset - 1;
+    ans->state = buf[offset - 1] & 0x3F;
+  } else if (x == 1) {
+    if (offset < 2) return 1;
+    ans->buf_offset = offset - 2;
+    ans->state = mem_get_le16(buf + offset - 2) & 0x3FFF;
+  } else if (x == 2) {
+    if (offset < 3) return 1;
+    ans->buf_offset = offset - 3;
+    ans->state = mem_get_le24(buf + offset - 3) & 0x3FFFFF;
+  } else {
+    // x == 3 implies this byte is a superframe marker
+    return 1;
+  }
+  ans->state += l_base;
+  if (ans->state >= l_base * io_base)
+    return 1;
+  return 0;
+}
+
+static INLINE int ans_read_end(struct AnsDecoder *const ans) {
+  return ans->state == l_base;
+}
+#undef ANS_DIVREM
+#ifdef __cplusplus
+}  // extern "C"
+#endif  // __cplusplus
+#endif  // VP10_COMMON_ANS_H_
diff --git a/vp10/common/divide.c b/vp10/common/divide.c
new file mode 100644
index 0000000..00b43a0
--- /dev/null
+++ b/vp10/common/divide.c
@@ -0,0 +1,93 @@
+/*
+ *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "vp10/common/divide.h"
+
+/* Constants for divide by multiply for small divisors generated with:
+void init_fastdiv() {
+  int i;
+  for (i = 3; i < 256; ++i) {
+    const int s = 31 ^ __builtin_clz(2 * i + 1);
+    const unsigned long long base = (1ull << (sizeof(unsigned) * 8 + s)) - 1;
+    fastdiv_tab[i].mult = (base / i + 1) & 0xFFFFFFFF;
+    fastdiv_tab[i].shift = s;
+  }
+  for (i = 0; i < 8; ++i) {
+    fastdiv_tab[1 << i].mult = 0;
+    fastdiv_tab[1 << i].shift = i;
+  }
+}
+*/
+const struct fastdiv_elem vp10_fastdiv_tab[256] = {
+    {0, 0},          {0, 0},          {0, 1},          {1431655766, 2},
+    {0, 2},          {2576980378, 3}, {1431655766, 3}, {613566757, 3},
+    {0, 3},          {3340530120, 4}, {2576980378, 4}, {1952257862, 4},
+    {1431655766, 4}, {991146300, 4},  {613566757, 4},  {286331154, 4},
+    {0, 4},          {3789677026, 5}, {3340530120, 5}, {2938661835, 5},
+    {2576980378, 5}, {2249744775, 5}, {1952257862, 5}, {1680639377, 5},
+    {1431655766, 5}, {1202590843, 5}, {991146300, 5},  {795364315, 5},
+    {613566757, 5},  {444306962, 5},  {286331154, 5},  {138547333, 5},
+    {0, 5},          {4034666248, 6}, {3789677026, 6}, {3558687189, 6},
+    {3340530120, 6}, {3134165325, 6}, {2938661835, 6}, {2753184165, 6},
+    {2576980378, 6}, {2409371898, 6}, {2249744775, 6}, {2097542168, 6},
+    {1952257862, 6}, {1813430637, 6}, {1680639377, 6}, {1553498810, 6},
+    {1431655766, 6}, {1314785907, 6}, {1202590843, 6}, {1094795586, 6},
+    {991146300, 6},  {891408307, 6},  {795364315, 6},  {702812831, 6},
+    {613566757, 6},  {527452125, 6},  {444306962, 6},  {363980280, 6},
+    {286331154, 6},  {211227900, 6},  {138547333, 6},  {68174085, 6},
+    {0, 6},          {4162814457, 7}, {4034666248, 7}, {3910343360, 7},
+    {3789677026, 7}, {3672508268, 7}, {3558687189, 7}, {3448072337, 7},
+    {3340530120, 7}, {3235934265, 7}, {3134165325, 7}, {3035110223, 7},
+    {2938661835, 7}, {2844718599, 7}, {2753184165, 7}, {2663967058, 7},
+    {2576980378, 7}, {2492141518, 7}, {2409371898, 7}, {2328596727, 7},
+    {2249744775, 7}, {2172748162, 7}, {2097542168, 7}, {2024065048, 7},
+    {1952257862, 7}, {1882064321, 7}, {1813430637, 7}, {1746305385, 7},
+    {1680639377, 7}, {1616385542, 7}, {1553498810, 7}, {1491936009, 7},
+    {1431655766, 7}, {1372618415, 7}, {1314785907, 7}, {1258121734, 7},
+    {1202590843, 7}, {1148159575, 7}, {1094795586, 7}, {1042467791, 7},
+    {991146300, 7},  {940802361, 7},  {891408307, 7},  {842937507, 7},
+    {795364315, 7},  {748664025, 7},  {702812831, 7},  {657787785, 7},
+    {613566757, 7},  {570128403, 7},  {527452125, 7},  {485518043, 7},
+    {444306962, 7},  {403800345, 7},  {363980280, 7},  {324829460, 7},
+    {286331154, 7},  {248469183, 7},  {211227900, 7},  {174592167, 7},
+    {138547333, 7},  {103079216, 7},  {68174085, 7},   {33818641, 7},
+    {0, 7},          {4228378656, 8}, {4162814457, 8}, {4098251237, 8},
+    {4034666248, 8}, {3972037425, 8}, {3910343360, 8}, {3849563281, 8},
+    {3789677026, 8}, {3730665024, 8}, {3672508268, 8}, {3615188300, 8},
+    {3558687189, 8}, {3502987511, 8}, {3448072337, 8}, {3393925206, 8},
+    {3340530120, 8}, {3287871517, 8}, {3235934265, 8}, {3184703642, 8},
+    {3134165325, 8}, {3084305374, 8}, {3035110223, 8}, {2986566663, 8},
+    {2938661835, 8}, {2891383213, 8}, {2844718599, 8}, {2798656110, 8},
+    {2753184165, 8}, {2708291480, 8}, {2663967058, 8}, {2620200175, 8},
+    {2576980378, 8}, {2534297473, 8}, {2492141518, 8}, {2450502814, 8},
+    {2409371898, 8}, {2368739540, 8}, {2328596727, 8}, {2288934667, 8},
+    {2249744775, 8}, {2211018668, 8}, {2172748162, 8}, {2134925265, 8},
+    {2097542168, 8}, {2060591247, 8}, {2024065048, 8}, {1987956292, 8},
+    {1952257862, 8}, {1916962805, 8}, {1882064321, 8}, {1847555765, 8},
+    {1813430637, 8}, {1779682582, 8}, {1746305385, 8}, {1713292966, 8},
+    {1680639377, 8}, {1648338801, 8}, {1616385542, 8}, {1584774030, 8},
+    {1553498810, 8}, {1522554545, 8}, {1491936009, 8}, {1461638086, 8},
+    {1431655766, 8}, {1401984144, 8}, {1372618415, 8}, {1343553873, 8},
+    {1314785907, 8}, {1286310003, 8}, {1258121734, 8}, {1230216764, 8},
+    {1202590843, 8}, {1175239808, 8}, {1148159575, 8}, {1121346142, 8},
+    {1094795586, 8}, {1068504060, 8}, {1042467791, 8}, {1016683080, 8},
+    {991146300, 8},  {965853890, 8},  {940802361, 8},  {915988286, 8},
+    {891408307, 8},  {867059126, 8},  {842937507, 8},  {819040276, 8},
+    {795364315, 8},  {771906565, 8},  {748664025, 8},  {725633745, 8},
+    {702812831, 8},  {680198441, 8},  {657787785, 8},  {635578121, 8},
+    {613566757, 8},  {591751050, 8},  {570128403, 8},  {548696263, 8},
+    {527452125, 8},  {506393524, 8},  {485518043, 8},  {464823301, 8},
+    {444306962, 8},  {423966729, 8},  {403800345, 8},  {383805589, 8},
+    {363980280, 8},  {344322273, 8},  {324829460, 8},  {305499766, 8},
+    {286331154, 8},  {267321616, 8},  {248469183, 8},  {229771913, 8},
+    {211227900, 8},  {192835267, 8},  {174592167, 8},  {156496785, 8},
+    {138547333, 8},  {120742053, 8},  {103079216, 8},  {85557118, 8},
+    {68174085, 8},   {50928466, 8},   {33818641, 8},   {16843010, 8},
+};
diff --git a/vp10/common/divide.h b/vp10/common/divide.h
new file mode 100644
index 0000000..2f3c35c
--- /dev/null
+++ b/vp10/common/divide.h
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VP10_COMMON_DIVIDE_H_
+#define VP10_COMMON_DIVIDE_H_
+// An implemntation of the divide by multiply alogrithm
+// https://gmplib.org/~tege/divcnst-pldi94.pdf
+
+#include <limits.h>
+
+#include "./vpx_config.h"
+#include "vpx/vpx_integer.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+struct fastdiv_elem {
+  unsigned mult;
+  unsigned shift;
+};
+
+extern const struct fastdiv_elem vp10_fastdiv_tab[256];
+
+static INLINE unsigned fastdiv(unsigned x, int y) {
+  unsigned t =
+      ((uint64_t)x * vp10_fastdiv_tab[y].mult) >> (sizeof(x) * CHAR_BIT);
+  return (t + x) >> vp10_fastdiv_tab[y].shift;
+}
+#ifdef __cplusplus
+}  // extern "C"
+#endif  // __cplusplus
+#endif  // VP10_COMMON_DIVIDE_H_
diff --git a/vp10/common/entropy.c b/vp10/common/entropy.c
index 3da08a6..f60bcf5 100644
--- a/vp10/common/entropy.c
+++ b/vp10/common/entropy.c
@@ -133,7 +133,7 @@
   0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5
 };
 
-// Model obtained from a 2-sided zero-centerd distribuition derived
+// 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]
 //
@@ -405,6 +405,287 @@
   {255, 246, 247, 255, 239, 255, 253, 255},
 };
 
+#if CONFIG_ANS
+// Model obtained from a 2-sided zero-centerd distribuition 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
+// is first solved, and then the {alpha, beta} pair is used to generate
+// the probabilities for the rest of the nodes.
+//
+// beta = 8
+// Values for tokens ONE_TOKEN through CATEGORY6_TOKEN included here.
+// ZERO_TOKEN and EOB_TOKEN are coded as flags outside this coder.
+const vpx_prob vp10_pareto8_token_probs[COEFF_PROB_MODELS]
+                                       [ENTROPY_TOKENS - 2] = {
+  {1, 1, 1, 1, 2, 4, 8, 14, 26, 198},
+  {2, 2, 2, 2, 4, 7, 14, 26, 42, 155},
+  {3, 3, 3, 3, 6, 11, 20, 34, 51, 122},
+  {4, 4, 4, 4, 7, 14, 25, 41, 56, 97},
+  {5, 5, 5, 5, 9, 17, 30, 46, 58, 76},
+  {6, 6, 6, 5, 11, 20, 34, 50, 57, 61},
+  {7, 7, 7, 6, 12, 22, 37, 53, 56, 49},
+  {8, 8, 7, 7, 14, 25, 40, 54, 53, 40},
+  {9, 9, 8, 8, 15, 27, 43, 55, 50, 32},
+  {10, 10, 9, 9, 16, 29, 45, 55, 47, 26},
+  {11, 10, 10, 10, 18, 31, 47, 55, 43, 21},
+  {12, 11, 11, 10, 19, 32, 48, 55, 40, 18},
+  {13, 12, 12, 11, 20, 34, 49, 54, 37, 14},
+  {14, 13, 12, 12, 21, 35, 50, 53, 34, 12},
+  {15, 14, 13, 12, 22, 37, 51, 51, 31, 10},
+  {16, 15, 14, 13, 23, 38, 51, 50, 28, 8},
+  {17, 16, 15, 13, 24, 39, 51, 48, 26, 7},
+  {18, 17, 15, 14, 25, 40, 52, 46, 23, 6},
+  {19, 17, 16, 15, 26, 41, 51, 45, 21, 5},
+  {20, 18, 17, 15, 27, 42, 51, 43, 19, 4},
+  {21, 19, 17, 16, 28, 42, 51, 41, 18, 3},
+  {22, 20, 18, 16, 28, 43, 51, 39, 16, 3},
+  {23, 21, 19, 17, 29, 43, 50, 37, 14, 3},
+  {24, 22, 19, 17, 30, 44, 49, 36, 13, 2},
+  {25, 22, 20, 18, 30, 44, 49, 34, 12, 2},
+  {26, 23, 20, 18, 31, 44, 48, 33, 11, 2},
+  {27, 24, 21, 19, 31, 45, 47, 31, 10, 1},
+  {28, 25, 22, 19, 32, 45, 46, 29, 9, 1},
+  {29, 25, 22, 20, 32, 45, 46, 28, 8, 1},
+  {30, 26, 23, 20, 33, 45, 45, 26, 7, 1},
+  {31, 27, 23, 20, 33, 45, 44, 25, 7, 1},
+  {32, 27, 24, 21, 33, 45, 43, 24, 6, 1},
+  {33, 28, 24, 21, 34, 44, 42, 23, 6, 1},
+  {34, 29, 25, 21, 34, 44, 41, 22, 5, 1},
+  {35, 30, 25, 22, 34, 44, 40, 20, 5, 1},
+  {36, 30, 26, 22, 35, 44, 39, 19, 4, 1},
+  {37, 31, 26, 22, 35, 44, 38, 18, 4, 1},
+  {38, 32, 27, 22, 35, 43, 37, 17, 4, 1},
+  {39, 33, 27, 23, 35, 43, 36, 16, 3, 1},
+  {40, 33, 27, 23, 35, 43, 35, 16, 3, 1},
+  {41, 34, 28, 23, 35, 42, 34, 15, 3, 1},
+  {42, 35, 28, 23, 36, 42, 33, 14, 2, 1},
+  {43, 35, 29, 24, 35, 42, 32, 13, 2, 1},
+  {44, 36, 29, 24, 36, 41, 31, 12, 2, 1},
+  {45, 36, 29, 24, 36, 41, 30, 12, 2, 1},
+  {46, 37, 30, 24, 35, 40, 30, 11, 2, 1},
+  {47, 37, 30, 24, 36, 40, 29, 10, 2, 1},
+  {48, 38, 30, 24, 36, 40, 28, 10, 1, 1},
+  {49, 39, 31, 24, 36, 39, 27, 9, 1, 1},
+  {50, 39, 31, 25, 35, 39, 26, 9, 1, 1},
+  {51, 40, 31, 25, 36, 38, 25, 8, 1, 1},
+  {52, 40, 31, 25, 35, 38, 25, 8, 1, 1},
+  {53, 41, 32, 25, 35, 37, 24, 7, 1, 1},
+  {54, 41, 32, 25, 35, 37, 23, 7, 1, 1},
+  {55, 42, 32, 25, 35, 36, 22, 7, 1, 1},
+  {56, 42, 33, 25, 35, 35, 22, 6, 1, 1},
+  {57, 43, 33, 25, 34, 35, 21, 6, 1, 1},
+  {58, 43, 33, 25, 35, 34, 20, 6, 1, 1},
+  {59, 44, 33, 25, 34, 34, 20, 5, 1, 1},
+  {60, 45, 33, 25, 34, 33, 19, 5, 1, 1},
+  {61, 45, 33, 25, 34, 33, 18, 5, 1, 1},
+  {62, 45, 34, 25, 34, 32, 18, 4, 1, 1},
+  {63, 46, 34, 25, 33, 32, 17, 4, 1, 1},
+  {64, 46, 34, 25, 33, 31, 17, 4, 1, 1},
+  {65, 47, 34, 25, 33, 30, 16, 4, 1, 1},
+  {66, 47, 34, 25, 33, 30, 15, 4, 1, 1},
+  {67, 48, 34, 25, 33, 29, 15, 3, 1, 1},
+  {68, 48, 35, 25, 32, 29, 14, 3, 1, 1},
+  {69, 48, 35, 25, 32, 28, 14, 3, 1, 1},
+  {70, 49, 35, 25, 32, 27, 13, 3, 1, 1},
+  {71, 49, 35, 25, 31, 27, 13, 3, 1, 1},
+  {72, 49, 35, 25, 31, 27, 12, 3, 1, 1},
+  {73, 50, 35, 25, 31, 26, 12, 2, 1, 1},
+  {74, 50, 35, 25, 31, 25, 12, 2, 1, 1},
+  {75, 51, 35, 25, 30, 25, 11, 2, 1, 1},
+  {76, 51, 35, 25, 30, 24, 11, 2, 1, 1},
+  {77, 51, 35, 25, 30, 24, 10, 2, 1, 1},
+  {78, 52, 35, 24, 29, 24, 10, 2, 1, 1},
+  {79, 52, 35, 24, 29, 23, 10, 2, 1, 1},
+  {80, 52, 35, 24, 29, 23, 9, 2, 1, 1},
+  {81, 53, 35, 24, 28, 22, 9, 2, 1, 1},
+  {82, 53, 35, 24, 28, 22, 9, 1, 1, 1},
+  {83, 54, 35, 24, 28, 21, 8, 1, 1, 1},
+  {84, 54, 35, 24, 27, 21, 8, 1, 1, 1},
+  {85, 54, 35, 24, 27, 20, 8, 1, 1, 1},
+  {86, 54, 35, 24, 27, 20, 7, 1, 1, 1},
+  {87, 55, 35, 23, 27, 19, 7, 1, 1, 1},
+  {88, 55, 35, 23, 26, 19, 7, 1, 1, 1},
+  {89, 55, 35, 23, 26, 18, 7, 1, 1, 1},
+  {90, 55, 35, 23, 26, 18, 6, 1, 1, 1},
+  {91, 56, 35, 23, 25, 17, 6, 1, 1, 1},
+  {92, 56, 35, 22, 25, 17, 6, 1, 1, 1},
+  {93, 56, 35, 22, 24, 17, 6, 1, 1, 1},
+  {94, 57, 35, 22, 24, 16, 5, 1, 1, 1},
+  {95, 56, 35, 22, 24, 16, 5, 1, 1, 1},
+  {96, 57, 35, 22, 23, 15, 5, 1, 1, 1},
+  {97, 56, 35, 22, 23, 15, 5, 1, 1, 1},
+  {98, 57, 34, 21, 23, 15, 5, 1, 1, 1},
+  {99, 57, 35, 21, 23, 14, 4, 1, 1, 1},
+  {100, 58, 34, 21, 22, 14, 4, 1, 1, 1},
+  {101, 57, 34, 21, 22, 14, 4, 1, 1, 1},
+  {102, 58, 34, 21, 21, 13, 4, 1, 1, 1},
+  {103, 57, 34, 21, 21, 13, 4, 1, 1, 1},
+  {104, 57, 34, 20, 21, 13, 4, 1, 1, 1},
+  {105, 58, 34, 20, 20, 12, 4, 1, 1, 1},
+  {106, 58, 34, 20, 20, 12, 3, 1, 1, 1},
+  {107, 58, 33, 20, 20, 12, 3, 1, 1, 1},
+  {108, 59, 33, 20, 19, 11, 3, 1, 1, 1},
+  {109, 59, 33, 19, 19, 11, 3, 1, 1, 1},
+  {110, 58, 33, 19, 19, 11, 3, 1, 1, 1},
+  {111, 59, 33, 19, 18, 10, 3, 1, 1, 1},
+  {112, 58, 33, 19, 18, 10, 3, 1, 1, 1},
+  {113, 58, 32, 19, 18, 10, 3, 1, 1, 1},
+  {114, 59, 32, 18, 18, 10, 2, 1, 1, 1},
+  {115, 60, 32, 18, 17, 9, 2, 1, 1, 1},
+  {116, 59, 32, 18, 17, 9, 2, 1, 1, 1},
+  {117, 59, 32, 18, 16, 9, 2, 1, 1, 1},
+  {118, 59, 31, 18, 16, 9, 2, 1, 1, 1},
+  {119, 59, 32, 17, 16, 8, 2, 1, 1, 1},
+  {120, 59, 31, 17, 16, 8, 2, 1, 1, 1},
+  {121, 59, 31, 17, 15, 8, 2, 1, 1, 1},
+  {122, 59, 30, 17, 15, 8, 2, 1, 1, 1},
+  {123, 59, 30, 17, 15, 7, 2, 1, 1, 1},
+  {124, 59, 30, 16, 15, 7, 2, 1, 1, 1},
+  {125, 59, 30, 16, 14, 7, 2, 1, 1, 1},
+  {126, 59, 30, 16, 14, 7, 1, 1, 1, 1},
+  {127, 59, 30, 16, 14, 6, 1, 1, 1, 1},
+  {128, 59, 30, 16, 13, 6, 1, 1, 1, 1},
+  {129, 59, 30, 15, 13, 6, 1, 1, 1, 1},
+  {130, 59, 29, 15, 13, 6, 1, 1, 1, 1},
+  {131, 59, 29, 15, 12, 6, 1, 1, 1, 1},
+  {132, 59, 28, 15, 12, 6, 1, 1, 1, 1},
+  {133, 59, 28, 15, 12, 5, 1, 1, 1, 1},
+  {134, 59, 28, 14, 12, 5, 1, 1, 1, 1},
+  {135, 59, 28, 14, 11, 5, 1, 1, 1, 1},
+  {136, 58, 28, 14, 11, 5, 1, 1, 1, 1},
+  {137, 58, 27, 14, 11, 5, 1, 1, 1, 1},
+  {138, 58, 27, 13, 11, 5, 1, 1, 1, 1},
+  {139, 58, 27, 13, 11, 4, 1, 1, 1, 1},
+  {140, 58, 27, 13, 10, 4, 1, 1, 1, 1},
+  {141, 58, 26, 13, 10, 4, 1, 1, 1, 1},
+  {142, 57, 26, 13, 10, 4, 1, 1, 1, 1},
+  {143, 57, 26, 12, 10, 4, 1, 1, 1, 1},
+  {144, 57, 26, 12, 9, 4, 1, 1, 1, 1},
+  {145, 57, 25, 12, 9, 4, 1, 1, 1, 1},
+  {146, 57, 25, 12, 9, 3, 1, 1, 1, 1},
+  {147, 57, 25, 11, 9, 3, 1, 1, 1, 1},
+  {148, 57, 25, 11, 8, 3, 1, 1, 1, 1},
+  {149, 57, 24, 11, 8, 3, 1, 1, 1, 1},
+  {150, 56, 24, 11, 8, 3, 1, 1, 1, 1},
+  {151, 56, 23, 11, 8, 3, 1, 1, 1, 1},
+  {152, 56, 23, 10, 8, 3, 1, 1, 1, 1},
+  {153, 56, 23, 10, 7, 3, 1, 1, 1, 1},
+  {154, 55, 23, 10, 7, 3, 1, 1, 1, 1},
+  {155, 55, 22, 10, 7, 3, 1, 1, 1, 1},
+  {156, 55, 22, 10, 7, 2, 1, 1, 1, 1},
+  {157, 54, 22, 10, 7, 2, 1, 1, 1, 1},
+  {158, 54, 22, 9, 7, 2, 1, 1, 1, 1},
+  {159, 55, 21, 9, 6, 2, 1, 1, 1, 1},
+  {160, 54, 21, 9, 6, 2, 1, 1, 1, 1},
+  {161, 53, 21, 9, 6, 2, 1, 1, 1, 1},
+  {162, 53, 20, 9, 6, 2, 1, 1, 1, 1},
+  {163, 53, 20, 8, 6, 2, 1, 1, 1, 1},
+  {164, 53, 20, 8, 5, 2, 1, 1, 1, 1},
+  {165, 52, 20, 8, 5, 2, 1, 1, 1, 1},
+  {166, 52, 19, 8, 5, 2, 1, 1, 1, 1},
+  {167, 51, 19, 8, 5, 2, 1, 1, 1, 1},
+  {168, 51, 19, 7, 5, 2, 1, 1, 1, 1},
+  {169, 51, 19, 7, 5, 1, 1, 1, 1, 1},
+  {170, 51, 18, 7, 5, 1, 1, 1, 1, 1},
+  {171, 51, 18, 7, 4, 1, 1, 1, 1, 1},
+  {172, 50, 18, 7, 4, 1, 1, 1, 1, 1},
+  {173, 50, 17, 7, 4, 1, 1, 1, 1, 1},
+  {174, 49, 17, 7, 4, 1, 1, 1, 1, 1},
+  {175, 49, 17, 6, 4, 1, 1, 1, 1, 1},
+  {176, 49, 16, 6, 4, 1, 1, 1, 1, 1},
+  {177, 48, 16, 6, 4, 1, 1, 1, 1, 1},
+  {178, 47, 16, 6, 4, 1, 1, 1, 1, 1},
+  {179, 47, 16, 6, 3, 1, 1, 1, 1, 1},
+  {180, 47, 15, 6, 3, 1, 1, 1, 1, 1},
+  {181, 47, 15, 5, 3, 1, 1, 1, 1, 1},
+  {182, 46, 15, 5, 3, 1, 1, 1, 1, 1},
+  {183, 46, 14, 5, 3, 1, 1, 1, 1, 1},
+  {184, 45, 14, 5, 3, 1, 1, 1, 1, 1},
+  {185, 44, 14, 5, 3, 1, 1, 1, 1, 1},
+  {186, 44, 13, 5, 3, 1, 1, 1, 1, 1},
+  {187, 43, 13, 5, 3, 1, 1, 1, 1, 1},
+  {188, 44, 13, 4, 2, 1, 1, 1, 1, 1},
+  {189, 43, 13, 4, 2, 1, 1, 1, 1, 1},
+  {190, 43, 12, 4, 2, 1, 1, 1, 1, 1},
+  {191, 42, 12, 4, 2, 1, 1, 1, 1, 1},
+  {192, 41, 12, 4, 2, 1, 1, 1, 1, 1},
+  {193, 41, 11, 4, 2, 1, 1, 1, 1, 1},
+  {194, 40, 11, 4, 2, 1, 1, 1, 1, 1},
+  {195, 39, 11, 4, 2, 1, 1, 1, 1, 1},
+  {196, 39, 11, 3, 2, 1, 1, 1, 1, 1},
+  {197, 39, 10, 3, 2, 1, 1, 1, 1, 1},
+  {198, 38, 10, 3, 2, 1, 1, 1, 1, 1},
+  {199, 37, 10, 3, 2, 1, 1, 1, 1, 1},
+  {200, 37, 10, 3, 1, 1, 1, 1, 1, 1},
+  {201, 37, 9, 3, 1, 1, 1, 1, 1, 1},
+  {202, 36, 9, 3, 1, 1, 1, 1, 1, 1},
+  {203, 35, 9, 3, 1, 1, 1, 1, 1, 1},
+  {204, 35, 8, 3, 1, 1, 1, 1, 1, 1},
+  {205, 35, 8, 2, 1, 1, 1, 1, 1, 1},
+  {206, 34, 8, 2, 1, 1, 1, 1, 1, 1},
+  {207, 33, 8, 2, 1, 1, 1, 1, 1, 1},
+  {208, 32, 8, 2, 1, 1, 1, 1, 1, 1},
+  {209, 32, 7, 2, 1, 1, 1, 1, 1, 1},
+  {210, 31, 7, 2, 1, 1, 1, 1, 1, 1},
+  {211, 30, 7, 2, 1, 1, 1, 1, 1, 1},
+  {212, 30, 6, 2, 1, 1, 1, 1, 1, 1},
+  {213, 29, 6, 2, 1, 1, 1, 1, 1, 1},
+  {214, 28, 6, 2, 1, 1, 1, 1, 1, 1},
+  {215, 27, 6, 2, 1, 1, 1, 1, 1, 1},
+  {216, 27, 6, 1, 1, 1, 1, 1, 1, 1},
+  {217, 27, 5, 1, 1, 1, 1, 1, 1, 1},
+  {218, 26, 5, 1, 1, 1, 1, 1, 1, 1},
+  {219, 25, 5, 1, 1, 1, 1, 1, 1, 1},
+  {220, 24, 5, 1, 1, 1, 1, 1, 1, 1},
+  {221, 24, 4, 1, 1, 1, 1, 1, 1, 1},
+  {222, 23, 4, 1, 1, 1, 1, 1, 1, 1},
+  {223, 22, 4, 1, 1, 1, 1, 1, 1, 1},
+  {224, 21, 4, 1, 1, 1, 1, 1, 1, 1},
+  {225, 20, 4, 1, 1, 1, 1, 1, 1, 1},
+  {226, 20, 3, 1, 1, 1, 1, 1, 1, 1},
+  {227, 19, 3, 1, 1, 1, 1, 1, 1, 1},
+  {228, 18, 3, 1, 1, 1, 1, 1, 1, 1},
+  {229, 17, 3, 1, 1, 1, 1, 1, 1, 1},
+  {230, 16, 3, 1, 1, 1, 1, 1, 1, 1},
+  {231, 16, 2, 1, 1, 1, 1, 1, 1, 1},
+  {232, 15, 2, 1, 1, 1, 1, 1, 1, 1},
+  {233, 14, 2, 1, 1, 1, 1, 1, 1, 1},
+  {234, 13, 2, 1, 1, 1, 1, 1, 1, 1},
+  {235, 12, 2, 1, 1, 1, 1, 1, 1, 1},
+  {236, 11, 2, 1, 1, 1, 1, 1, 1, 1},
+  {237, 11, 1, 1, 1, 1, 1, 1, 1, 1},
+  {238, 10, 1, 1, 1, 1, 1, 1, 1, 1},
+  {239, 9, 1, 1, 1, 1, 1, 1, 1, 1},
+  {240, 8, 1, 1, 1, 1, 1, 1, 1, 1},
+  {241, 7, 1, 1, 1, 1, 1, 1, 1, 1},
+  {242, 6, 1, 1, 1, 1, 1, 1, 1, 1},
+  {243, 5, 1, 1, 1, 1, 1, 1, 1, 1},
+  {244, 4, 1, 1, 1, 1, 1, 1, 1, 1},
+  {245, 3, 1, 1, 1, 1, 1, 1, 1, 1},
+  {246, 2, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {247, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+};
+
+void vp10_build_pareto8_dec_tab(
+    const vpx_prob token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2],
+    rans_dec_lut dec_tab[COEFF_PROB_MODELS]) {
+  int p;
+  for (p = 0; p < COEFF_PROB_MODELS; ++p) {
+    rans_build_dec_tab(token_probs[p], dec_tab[p]);
+  }
+}
+#endif  // CONFIG_ANS
+
 static const vp10_coeff_probs_model default_coef_probs_4x4[PLANE_TYPES] = {
   {  // Y plane
     {  // Intra
diff --git a/vp10/common/entropy.h b/vp10/common/entropy.h
index c1de3b2..4da0bfb 100644
--- a/vp10/common/entropy.h
+++ b/vp10/common/entropy.h
@@ -14,6 +14,9 @@
 #include "vpx/vpx_integer.h"
 #include "vpx_dsp/prob.h"
 
+#if CONFIG_ANS
+#include "vp10/common/ans.h"
+#endif  // CONFIG_ANS
 #include "vp10/common/common.h"
 #include "vp10/common/enums.h"
 
@@ -163,6 +166,14 @@
 #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
 extern const vpx_tree_index vp10_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
 extern const vpx_prob vp10_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
+#if CONFIG_ANS
+extern const vpx_prob
+    vp10_pareto8_token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2];
+
+void vp10_build_pareto8_dec_tab(
+    const vpx_prob token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2],
+    rans_dec_lut dec_tab[COEFF_PROB_MODELS]);
+#endif  // CONFIG_ANS
 
 typedef vpx_prob vp10_coeff_probs_model[REF_TYPES][COEF_BANDS]
                                       [COEFF_CONTEXTS][UNCONSTRAINED_NODES];
diff --git a/vp10/decoder/decodeframe.c b/vp10/decoder/decodeframe.c
index 44b5bc4..2feda6b 100644
--- a/vp10/decoder/decodeframe.c
+++ b/vp10/decoder/decodeframe.c
@@ -350,7 +350,12 @@
 }
 
 static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
+#if CONFIG_ANS
+                                         const rans_dec_lut *const token_tab,
+                                                struct AnsDecoder *const r,
+#else
                                                 vpx_reader *r,
+#endif  // CONFIG_ANS
                                                 MB_MODE_INFO *const mbmi,
                                                 int plane,
                                                 int row, int col,
@@ -373,7 +378,11 @@
   if (!mbmi->skip) {
     TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
     const scan_order *sc = get_scan(tx_size, tx_type, 0);
-    const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size,
+    const int eob = vp10_decode_block_tokens(xd,
+#if CONFIG_ANS
+                                             token_tab,
+#endif  // CONFIG_ANS
+                                             plane, sc, col, row, tx_size,
                                              r, mbmi->segment_id);
     inverse_transform_block_intra(xd, plane, tx_type, tx_size,
                                   dst, pd->dst.stride, eob);
@@ -437,7 +446,13 @@
 }
 #endif  // CONFIG_VAR_TX
 
-static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r,
+static int reconstruct_inter_block(MACROBLOCKD *const xd,
+#if CONFIG_ANS
+                                   const rans_dec_lut *const token_tab,
+                                   struct AnsDecoder *const r,
+#else
+                                   vpx_reader *r,
+#endif
                                    MB_MODE_INFO *const mbmi, int plane,
                                    int row, int col, TX_SIZE tx_size) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
@@ -445,8 +460,12 @@
   int block_idx = (row << 1) + col;
   TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx, tx_size);
   const scan_order *sc = get_scan(tx_size, tx_type, 1);
-  const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size, r,
-                                          mbmi->segment_id);
+  const int eob = vp10_decode_block_tokens(xd,
+#if CONFIG_ANS
+                                           token_tab,
+#endif
+                                           plane, sc, col, row, tx_size, r,
+                                           mbmi->segment_id);
 
   inverse_transform_block_inter(xd, plane, tx_size,
                             &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
@@ -1021,6 +1040,12 @@
       xd->mi[y * cm->mi_stride + x]->mbmi.tx_type = txfm;
 #endif
     }
+#if CONFIG_VAR_TX
+  xd->above_txfm_context = cm->above_txfm_context + mi_col;
+  xd->left_txfm_context = xd->left_txfm_context_buffer + (mi_row & 0x07);
+  set_txfm_ctx(xd->left_txfm_context, xd->mi[0]->mbmi.tx_size, bh);
+  set_txfm_ctx(xd->above_txfm_context, xd->mi[0]->mbmi.tx_size, bw);
+#endif
 }
 
 static void set_ref(VP10_COMMON *const cm, MACROBLOCKD *const xd,
@@ -1513,7 +1538,11 @@
                          int supertx_enabled,
 #endif  // CONFIG_SUPERTX
                          int mi_row, int mi_col,
-                         vpx_reader *r, BLOCK_SIZE bsize,
+                         vpx_reader *r,
+#if CONFIG_ANS
+                         struct AnsDecoder *const tok,
+#endif  // CONFIG_ANS
+                         BLOCK_SIZE bsize,
                          int bwl, int bhl) {
   VP10_COMMON *const cm = &pbi->common;
   const int less8x8 = bsize < BLOCK_8X8;
@@ -1573,7 +1602,13 @@
 
         for (row = 0; row < max_blocks_high; row += step)
           for (col = 0; col < max_blocks_wide; col += step)
-            predict_and_reconstruct_intra_block(xd, r, mbmi, plane,
+            predict_and_reconstruct_intra_block(xd,
+#if CONFIG_ANS
+                                                pbi->token_tab, tok,
+#else
+                                                r,
+#endif
+                                                mbmi, plane,
                                                 row, col, tx_size);
       }
     } else {
@@ -1621,7 +1656,13 @@
 
           for (row = 0; row < max_blocks_high; row += step)
             for (col = 0; col < max_blocks_wide; col += step)
-              eobtotal += reconstruct_inter_block(xd, r, mbmi, plane, row, col,
+              eobtotal += reconstruct_inter_block(xd,
+#if CONFIG_ANS
+                                                  pbi->token_tab, tok,
+#else
+                                                  r,
+#endif
+                                                  mbmi, plane, row, col,
                                                   tx_size);
 #endif
         }
@@ -1704,7 +1745,11 @@
                              int supertx_enabled,
 #endif
                              int mi_row, int mi_col,
-                             vpx_reader* r, BLOCK_SIZE bsize, int n4x4_l2) {
+                             vpx_reader* r,
+#if CONFIG_ANS
+                             struct AnsDecoder *const tok,
+#endif  // CONFIG_ANS
+                             BLOCK_SIZE bsize, int n4x4_l2) {
   VP10_COMMON *const cm = &pbi->common;
   const int n8x8_l2 = n4x4_l2 - 1;
   const int num_8x8_wh = 1 << n8x8_l2;
@@ -1777,7 +1822,11 @@
 #if CONFIG_SUPERTX
                  supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                 mi_row, mi_col, r, subsize, 1, 1);
+                 mi_row, mi_col, r,
+#if CONFIG_ANS
+                 tok,
+#endif  // CONFIG_ANS
+                 subsize, 1, 1);
   } else {
     switch (partition) {
       case PARTITION_NONE:
@@ -1785,55 +1834,91 @@
 #if CONFIG_SUPERTX
                      supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                     mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2);
+                     mi_row, mi_col, r,
+#if CONFIG_ANS
+                     tok,
+#endif  // CONFIG_ANS
+                     subsize, n4x4_l2, n4x4_l2);
         break;
       case PARTITION_HORZ:
         decode_block(pbi, xd,
 #if CONFIG_SUPERTX
                      supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                     mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2);
+                     mi_row, mi_col, r,
+#if CONFIG_ANS
+                     tok,
+#endif  // CONFIG_ANS
+                     subsize, n4x4_l2, n8x8_l2);
         if (has_rows)
           decode_block(pbi, xd,
 #if CONFIG_SUPERTX
                        supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                       mi_row + hbs, mi_col, r, subsize, n4x4_l2, n8x8_l2);
+                       mi_row + hbs, mi_col, r,
+#if CONFIG_ANS
+                       tok,
+#endif  // CONFIG_ANS
+                       subsize, n4x4_l2, n8x8_l2);
         break;
       case PARTITION_VERT:
         decode_block(pbi, xd,
 #if CONFIG_SUPERTX
                      supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                     mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2);
+                     mi_row, mi_col, r,
+#if CONFIG_ANS
+                     tok,
+#endif  // CONFIG_ANS
+                     subsize, n8x8_l2, n4x4_l2);
         if (has_cols)
           decode_block(pbi, xd,
 #if CONFIG_SUPERTX
                        supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                       mi_row, mi_col + hbs, r, subsize, n8x8_l2, n4x4_l2);
+                       mi_row, mi_col + hbs, r,
+#if CONFIG_ANS
+                       tok,
+#endif  // CONFIG_ANS
+                       subsize, n8x8_l2, n4x4_l2);
         break;
       case PARTITION_SPLIT:
         decode_partition(pbi, xd,
 #if CONFIG_SUPERTX
                          supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                         mi_row, mi_col, r, subsize, n8x8_l2);
+                         mi_row, mi_col, r,
+#if CONFIG_ANS
+                         tok,
+#endif  // CONFIG_ANS
+                         subsize, n8x8_l2);
         decode_partition(pbi, xd,
 #if CONFIG_SUPERTX
                          supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                         mi_row, mi_col + hbs, r, subsize, n8x8_l2);
+                         mi_row, mi_col + hbs, r,
+#if CONFIG_ANS
+                         tok,
+#endif  // CONFIG_ANS
+                         subsize, n8x8_l2);
         decode_partition(pbi, xd,
 #if CONFIG_SUPERTX
                          supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                         mi_row + hbs, mi_col, r, subsize, n8x8_l2);
+                         mi_row + hbs, mi_col, r,
+#if CONFIG_ANS
+                         tok,
+#endif  // CONFIG_ANS
+                         subsize, n8x8_l2);
         decode_partition(pbi, xd,
 #if CONFIG_SUPERTX
                          supertx_enabled,
 #endif  // CONFIG_SUPERTX
-                         mi_row + hbs, mi_col + hbs, r, subsize, n8x8_l2);
+                         mi_row + hbs, mi_col + hbs, r,
+#if CONFIG_ANS
+                         tok,
+#endif  // CONFIG_ANS
+                         subsize, n8x8_l2);
         break;
       default:
         assert(0 && "Invalid partition type");
@@ -1878,7 +1963,13 @@
 
         for (row = 0; row < max_blocks_high; row += step)
           for (col = 0; col < max_blocks_wide; col += step)
-            eobtotal += reconstruct_inter_block(xd, r, mbmi, i, row, col,
+            eobtotal += reconstruct_inter_block(xd,
+#if CONFIG_ANS
+                                                pbi->token_tab, tok,
+#else
+                                                r,
+#endif
+                                                mbmi, i, row, col,
                                                 tx_size);
       }
       if (!(subsize < BLOCK_8X8) && eobtotal == 0)
@@ -1898,13 +1989,13 @@
     dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
 }
 
-static void setup_token_decoder(const uint8_t *data,
-                                const uint8_t *data_end,
-                                size_t read_size,
-                                struct vpx_internal_error_info *error_info,
-                                vpx_reader *r,
-                                vpx_decrypt_cb decrypt_cb,
-                                void *decrypt_state) {
+static void setup_bool_decoder(const uint8_t *data,
+                               const uint8_t *data_end,
+                               const size_t read_size,
+                               struct vpx_internal_error_info *error_info,
+                               vpx_reader *r,
+                               vpx_decrypt_cb decrypt_cb,
+                               void *decrypt_state) {
   // Validate the calculated partition length. If the buffer
   // described by the partition can't be fully read, then restrict
   // it to the portion that can be (for EC mode) or throw an error.
@@ -1917,6 +2008,27 @@
                        "Failed to allocate bool decoder %d", 1);
 }
 
+static void setup_token_decoder(const uint8_t *data,
+                                const uint8_t *data_end,
+                                const size_t read_size,
+                                struct vpx_internal_error_info *error_info,
+                                struct AnsDecoder *const ans,
+                                vpx_decrypt_cb decrypt_cb,
+                                void *decrypt_state) {
+  (void) decrypt_cb;
+  (void) decrypt_state;
+  // Validate the calculated partition length. If the buffer
+  // described by the partition can't be fully read, then restrict
+  // it to the portion that can be (for EC mode) or throw an error.
+  if (!read_is_valid(data, read_size, data_end))
+    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
+                       "Truncated packet or corrupt tile length");
+
+  if (read_size > INT_MAX || ans_read_init(ans, data, (int)read_size))
+    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
+                       "Failed to allocate token decoder %d", 1);
+}
+
 static void read_coef_probs_common(vp10_coeff_probs_model *coef_probs,
                                    vpx_reader *r) {
   int i, j, k, l, m;
@@ -2394,6 +2506,7 @@
   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
       const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
+
       tile_data = pbi->tile_data + tile_cols * tile_row + tile_col;
       tile_data->cm = cm;
       tile_data->xd = pbi->mb;
@@ -2403,9 +2516,21 @@
               &cm->counts : NULL;
       vp10_zero(tile_data->dqcoeff);
       vp10_tile_init(&tile_data->xd.tile, tile_data->cm, tile_row, tile_col);
+#if !CONFIG_ANS
+      setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
+                         &tile_data->bit_reader, pbi->decrypt_cb,
+                         pbi->decrypt_state);
+#else
+      if (buf->size < 3 || !read_is_valid(buf->data, buf->size, data_end))
+        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
+                           "Truncated packet or corrupt tile length");
+      setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
+                         &tile_data->bit_reader, pbi->decrypt_cb,
+                         pbi->decrypt_state);
       setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
-                          &tile_data->bit_reader, pbi->decrypt_cb,
+                          &tile_data->token_ans, pbi->decrypt_cb,
                           pbi->decrypt_state);
+#endif
       vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
       tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0];
       tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1];
@@ -2434,6 +2559,9 @@
                            0,
 #endif
                            mi_row, mi_col, &tile_data->bit_reader,
+#if CONFIG_ANS
+                           &tile_data->token_ans,
+#endif  // CONFIG_ANS
                            BLOCK_64X64, 4);
         }
         pbi->mb.corrupted |= tile_data->xd.corrupted;
@@ -2491,7 +2619,11 @@
 
   if (cm->frame_parallel_decode)
     vp10_frameworker_broadcast(pbi->cur_buf, INT_MAX);
+#if CONFIG_ANS
+  return data_end;
+#else
   return vpx_reader_find_end(&tile_data->bit_reader);
+#endif
 }
 
 static int tile_worker_hook(TileWorkerData *const tile_data,
@@ -2521,6 +2653,9 @@
                        0,
 #endif
                        mi_row, mi_col, &tile_data->bit_reader,
+#if CONFIG_ANS
+                       &tile_data->token_ans,
+#endif  // CONFIG_ANS
                        BLOCK_64X64, 4);
     }
   }
@@ -2551,6 +2686,9 @@
   assert(tile_cols <= (1 << 6));
   assert(tile_rows == 1);
   (void)tile_rows;
+#if CONFIG_ANS
+  abort();  // FIXME: Tile parsing broken
+#endif
 
   // TODO(jzern): See if we can remove the restriction of passing in max
   // threads to the decoder.
@@ -2650,9 +2788,9 @@
       vp10_zero(tile_data->dqcoeff);
       vp10_tile_init(tile, cm, 0, buf->col);
       vp10_tile_init(&tile_data->xd.tile, cm, 0, buf->col);
-      setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
-                          &tile_data->bit_reader, pbi->decrypt_cb,
-                          pbi->decrypt_state);
+      setup_bool_decoder(buf->data, data_end, buf->size, &cm->error,
+                         &tile_data->bit_reader, pbi->decrypt_cb,
+                         pbi->decrypt_state);
       vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
       tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0];
       tile_data->xd.plane[1].color_index_map = tile_data->color_index_map[1];
diff --git a/vp10/decoder/decoder.c b/vp10/decoder/decoder.c
index 2dbadb3..329e54c 100644
--- a/vp10/decoder/decoder.c
+++ b/vp10/decoder/decoder.c
@@ -115,6 +115,9 @@
   cm->setup_mi = vp10_dec_setup_mi;
 
   vp10_loop_filter_init(cm);
+#if CONFIG_ANS
+  vp10_build_pareto8_dec_tab(vp10_pareto8_token_probs, pbi->token_tab);
+#endif  // CONFIG_ANS
 
   cm->error.setjmp = 0;
 
diff --git a/vp10/decoder/decoder.h b/vp10/decoder/decoder.h
index 72a6310..e590d8b 100644
--- a/vp10/decoder/decoder.h
+++ b/vp10/decoder/decoder.h
@@ -18,6 +18,9 @@
 #include "vpx_scale/yv12config.h"
 #include "vpx_util/vpx_thread.h"
 
+#if CONFIG_ANS
+#include "vp10/common/ans.h"
+#endif
 #include "vp10/common/thread_common.h"
 #include "vp10/common/onyxc_int.h"
 #include "vp10/common/ppflags.h"
@@ -31,6 +34,9 @@
 typedef struct TileData {
   VP10_COMMON *cm;
   vpx_reader bit_reader;
+#if CONFIG_ANS
+  struct AnsDecoder token_ans;
+#endif  // CONFIG_ANS
   DECLARE_ALIGNED(16, MACROBLOCKD, xd);
   /* dqcoeff are shared by all the planes. So planes must be decoded serially */
   DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
@@ -40,6 +46,9 @@
 typedef struct TileWorkerData {
   struct VP10Decoder *pbi;
   vpx_reader bit_reader;
+#if CONFIG_ANS
+  struct AnsDecoder token_ans;
+#endif  // CONFIG_ANS
   FRAME_COUNTS counts;
   DECLARE_ALIGNED(16, MACROBLOCKD, xd);
   /* dqcoeff are shared by all the planes. So planes must be decoded serially */
@@ -80,6 +89,9 @@
   int inv_tile_order;
   int need_resync;  // wait for key/intra-only frame.
   int hold_ref_buf;  // hold the reference buffer.
+#if CONFIG_ANS
+  rans_dec_lut token_tab[COEFF_PROB_MODELS];
+#endif  // CONFIG_ANS
 } VP10Decoder;
 
 int vp10_receive_compressed_data(struct VP10Decoder *pbi,
diff --git a/vp10/decoder/detokenize.c b/vp10/decoder/detokenize.c
index 011c45a..c5dec87 100644
--- a/vp10/decoder/detokenize.c
+++ b/vp10/decoder/detokenize.c
@@ -11,6 +11,7 @@
 #include "vpx_mem/vpx_mem.h"
 #include "vpx_ports/mem.h"
 
+#include "vp10/common/ans.h"
 #include "vp10/common/blockd.h"
 #include "vp10/common/common.h"
 #include "vp10/common/entropy.h"
@@ -38,6 +39,7 @@
        ++coef_counts[band][ctx][token];                     \
   } while (0)
 
+#if !CONFIG_ANS
 static INLINE int read_coeff(const vpx_prob *probs, int n, vpx_reader *r) {
   int i, val = 0;
   for (i = 0; i < n; ++i)
@@ -207,6 +209,175 @@
 
   return c;
 }
+#else  // !CONFIG_ANS
+static INLINE int read_coeff(const vpx_prob *const probs, int n,
+                             struct AnsDecoder *const ans) {
+  int i, val = 0;
+  for (i = 0; i < n; ++i)
+    val = (val << 1) | uabs_read(ans, probs[i]);
+  return val;
+}
+
+static int decode_coefs_ans(const MACROBLOCKD *const xd,
+                            const rans_dec_lut *const token_tab,
+                            PLANE_TYPE type,
+                            tran_low_t *dqcoeff, TX_SIZE tx_size,
+                            const int16_t *dq,
+                            int ctx, const int16_t *scan, const int16_t *nb,
+                            struct AnsDecoder *const ans) {
+  FRAME_COUNTS *counts = xd->counts;
+  const int max_eob = 16 << (tx_size << 1);
+  const FRAME_CONTEXT *const fc = xd->fc;
+  const int ref = is_inter_block(&xd->mi[0]->mbmi);
+  int band, c = 0;
+  const vpx_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
+      fc->coef_probs[tx_size][type][ref];
+  const vpx_prob *prob;
+  unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
+  unsigned int (*eob_branch_count)[COEFF_CONTEXTS];
+  uint8_t token_cache[32 * 32];
+  const uint8_t *band_translate = get_band_translate(tx_size);
+  const int dq_shift = (tx_size == TX_32X32);
+  int v, token;
+  int16_t dqv = dq[0];
+  const uint8_t *cat1_prob;
+  const uint8_t *cat2_prob;
+  const uint8_t *cat3_prob;
+  const uint8_t *cat4_prob;
+  const uint8_t *cat5_prob;
+  const uint8_t *cat6_prob;
+
+  if (counts) {
+    coef_counts = counts->coef[tx_size][type][ref];
+    eob_branch_count = counts->eob_branch[tx_size][type][ref];
+  }
+
+#if CONFIG_VP9_HIGHBITDEPTH
+  if (xd->bd > VPX_BITS_8) {
+    if (xd->bd == VPX_BITS_10) {
+      cat1_prob = vp10_cat1_prob_high10;
+      cat2_prob = vp10_cat2_prob_high10;
+      cat3_prob = vp10_cat3_prob_high10;
+      cat4_prob = vp10_cat4_prob_high10;
+      cat5_prob = vp10_cat5_prob_high10;
+      cat6_prob = vp10_cat6_prob_high10;
+    } else {
+      cat1_prob = vp10_cat1_prob_high12;
+      cat2_prob = vp10_cat2_prob_high12;
+      cat3_prob = vp10_cat3_prob_high12;
+      cat4_prob = vp10_cat4_prob_high12;
+      cat5_prob = vp10_cat5_prob_high12;
+      cat6_prob = vp10_cat6_prob_high12;
+    }
+  } else {
+    cat1_prob = vp10_cat1_prob;
+    cat2_prob = vp10_cat2_prob;
+    cat3_prob = vp10_cat3_prob;
+    cat4_prob = vp10_cat4_prob;
+    cat5_prob = vp10_cat5_prob;
+    cat6_prob = vp10_cat6_prob;
+  }
+#else
+  cat1_prob = vp10_cat1_prob;
+  cat2_prob = vp10_cat2_prob;
+  cat3_prob = vp10_cat3_prob;
+  cat4_prob = vp10_cat4_prob;
+  cat5_prob = vp10_cat5_prob;
+  cat6_prob = vp10_cat6_prob;
+#endif
+
+  while (c < max_eob) {
+    int val = -1;
+    band = *band_translate++;
+    prob = coef_probs[band][ctx];
+    if (counts)
+      ++eob_branch_count[band][ctx];
+    if (!uabs_read(ans, prob[EOB_CONTEXT_NODE])) {
+      INCREMENT_COUNT(EOB_MODEL_TOKEN);
+      break;
+    }
+
+    while (!uabs_read(ans, prob[ZERO_CONTEXT_NODE])) {
+      INCREMENT_COUNT(ZERO_TOKEN);
+      dqv = dq[1];
+      token_cache[scan[c]] = 0;
+      ++c;
+      if (c >= max_eob)
+        return c;  // zero tokens at the end (no eob token)
+      ctx = get_coef_context(nb, token_cache, c);
+      band = *band_translate++;
+      prob = coef_probs[band][ctx];
+    }
+
+    token = ONE_TOKEN + rans_read(ans, token_tab[prob[PIVOT_NODE] - 1]);
+    INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
+    switch (token) {
+      case ONE_TOKEN:
+      case TWO_TOKEN:
+      case THREE_TOKEN:
+      case FOUR_TOKEN:
+        val = token;
+        break;
+      case CATEGORY1_TOKEN:
+        val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, ans);
+        break;
+      case CATEGORY2_TOKEN:
+        val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, ans);
+        break;
+      case CATEGORY3_TOKEN:
+        val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, ans);
+        break;
+      case CATEGORY4_TOKEN:
+        val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, ans);
+        break;
+      case CATEGORY5_TOKEN:
+        val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, ans);
+        break;
+      case CATEGORY6_TOKEN:
+        {
+          const int skip_bits = TX_SIZES - 1 - tx_size;
+          const uint8_t *cat6p = cat6_prob + skip_bits;
+#if CONFIG_VP9_HIGHBITDEPTH
+        switch (xd->bd) {
+          case VPX_BITS_8:
+            val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans);
+            break;
+          case VPX_BITS_10:
+            val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, ans);
+            break;
+          case VPX_BITS_12:
+            val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, ans);
+            break;
+          default:
+            assert(0);
+            return -1;
+        }
+#else
+        val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans);
+#endif
+        }
+        break;
+    }
+    v = (val * dqv) >> dq_shift;
+#if CONFIG_COEFFICIENT_RANGE_CHECKING
+#if CONFIG_VP9_HIGHBITDEPTH
+    dqcoeff[scan[c]] = highbd_check_range((uabs_read_bit(ans) ? -v : v),
+                                          xd->bd);
+#else
+    dqcoeff[scan[c]] = check_range(uabs_read_bit(ans) ? -v : v);
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+#else
+    dqcoeff[scan[c]] = uabs_read_bit(ans) ? -v : v;
+#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
+    token_cache[scan[c]] = vp10_pt_energy_class[token];
+    ++c;
+    ctx = get_coef_context(nb, token_cache, c);
+    dqv = dq[1];
+  }
+
+  return c;
+}
+#endif  // !CONFIG_ANS
 
 // TODO(slavarnway): Decode version of vp10_set_context.  Modify vp10_set_context
 // after testing is complete, then delete this version.
@@ -280,18 +451,32 @@
   }
 }
 
-int vp10_decode_block_tokens(MACROBLOCKD *xd,
-                            int plane, const scan_order *sc,
-                            int x, int y,
-                            TX_SIZE tx_size, vpx_reader *r,
-                            int seg_id) {
+int vp10_decode_block_tokens(MACROBLOCKD *const xd,
+#if CONFIG_ANS
+                             const rans_dec_lut *const token_tab,
+#endif  // CONFIG_ANS
+                             int plane, const scan_order *sc,
+                             int x, int y,
+                             TX_SIZE tx_size,
+#if CONFIG_ANS
+                             struct AnsDecoder *const r,
+#else
+                             vpx_reader *r,
+#endif  // CONFIG_ANS
+                             int seg_id) {
   struct macroblockd_plane *const pd = &xd->plane[plane];
   const int16_t *const dequant = pd->seg_dequant[seg_id];
   const int ctx = get_entropy_context(tx_size, pd->above_context + x,
                                                pd->left_context + y);
+#if !CONFIG_ANS
   const int eob = decode_coefs(xd, pd->plane_type,
                                pd->dqcoeff, tx_size,
                                dequant, ctx, sc->scan, sc->neighbors, r);
+#else
+  const int eob = decode_coefs_ans(xd, token_tab, pd->plane_type,
+                                   pd->dqcoeff, tx_size,
+                                   dequant, ctx, sc->scan, sc->neighbors, r);
+#endif  // !CONFIG_ANS
   dec_set_contexts(xd, pd, tx_size, eob > 0, x, y);
   return eob;
 }
diff --git a/vp10/decoder/detokenize.h b/vp10/decoder/detokenize.h
index d2677f6..f87c6f0 100644
--- a/vp10/decoder/detokenize.h
+++ b/vp10/decoder/detokenize.h
@@ -12,8 +12,8 @@
 #ifndef VP10_DECODER_DETOKENIZE_H_
 #define VP10_DECODER_DETOKENIZE_H_
 
-#include "vpx_dsp/bitreader.h"
 #include "vp10/decoder/decoder.h"
+#include "vp10/common/ans.h"
 #include "vp10/common/scan.h"
 
 #ifdef __cplusplus
@@ -22,11 +22,19 @@
 
 void vp10_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
                                 vpx_reader *r);
-int vp10_decode_block_tokens(MACROBLOCKD *xd,
-                            int plane, const scan_order *sc,
-                            int x, int y,
-                            TX_SIZE tx_size, vpx_reader *r,
-                            int seg_id);
+int vp10_decode_block_tokens(MACROBLOCKD *const xd,
+#if CONFIG_ANS
+                             const rans_dec_lut *const token_tab,
+#endif  // CONFIG_ANS
+                             int plane, const scan_order *sc,
+                             int x, int y,
+                             TX_SIZE tx_size,
+#if CONFIG_ANS
+                             struct AnsDecoder *const r,
+#else
+                             vpx_reader *r,
+#endif  // CONFIG_ANS
+                             int seg_id);
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/vp10/encoder/bitstream.c b/vp10/encoder/bitstream.c
index 4c0c6af..8aed44e 100644
--- a/vp10/encoder/bitstream.c
+++ b/vp10/encoder/bitstream.c
@@ -402,6 +402,7 @@
 }
 #endif  // CONFIG_SUPERTX
 
+#if !CONFIG_ANS
 static void pack_mb_tokens(vpx_writer *w,
                            TOKENEXTRA **tp, const TOKENEXTRA *const stop,
                            vpx_bit_depth_t bit_depth, const TX_SIZE tx) {
@@ -486,6 +487,71 @@
 
   *tp = p;
 }
+#else
+// This function serializes the tokens backwards both in token order and
+// bit order in each token.
+static void pack_mb_tokens_ans(struct AnsCoder *const ans,
+                               const TOKENEXTRA *const start,
+                               const TOKENEXTRA *const stop,
+                               vpx_bit_depth_t bit_depth) {
+  const TOKENEXTRA *p;
+  TX_SIZE tx_size = TX_SIZES;
+
+  for (p = stop - 1; p >= start; --p) {
+    const int t = p->token;
+    if (t == EOSB_TOKEN) {
+      tx_size = (TX_SIZE)p->extra;
+    } else {
+#if CONFIG_VP9_HIGHBITDEPTH
+    const vp10_extra_bit *const b =
+      (bit_depth == VPX_BITS_12) ? &vp10_extra_bits_high12[t] :
+      (bit_depth == VPX_BITS_10) ? &vp10_extra_bits_high10[t] :
+      &vp10_extra_bits[t];
+#else
+    const vp10_extra_bit *const b = &vp10_extra_bits[t];
+    (void) bit_depth;
+#endif  // CONFIG_VP9_HIGHBITDEPTH
+
+    if (t != EOB_TOKEN && t != ZERO_TOKEN) {
+      // Write extra bits first
+      const int e = p->extra;
+      const int l = b->len;
+      const int skip_bits = (t == CATEGORY6_TOKEN) ? TX_SIZES - 1 - tx_size : 0;
+      assert(tx_size < TX_SIZES);
+      uabs_write(ans, e & 1, 128);
+      if (l) {
+        const int v = e >> 1;
+        int n;
+        for (n = 0; n < l - skip_bits; ++n) {
+          const int bb = (v >> n) & 1;
+          uabs_write(ans, bb, b->prob[l - 1 - n]);
+        }
+        for (; n < l; ++n) {
+          assert(((v >> n) & 1) == 0);
+        }
+      }
+
+      {
+        struct rans_sym s;
+        int j;
+        const vpx_prob *token_probs =
+            vp10_pareto8_token_probs[p->context_tree[PIVOT_NODE] - 1];
+        s.cum_prob = 0;
+        for (j = ONE_TOKEN; j < t; ++j) {
+          s.cum_prob += token_probs[j - ONE_TOKEN];
+        }
+        s.prob = token_probs[t - ONE_TOKEN];
+        rans_write(ans, &s);
+      }
+    }
+    if (t != EOB_TOKEN)
+      uabs_write(ans, t != ZERO_TOKEN, p->context_tree[1]);
+    if (!p->skip_eob_node)
+      uabs_write(ans, t != EOB_TOKEN, p->context_tree[0]);
+  }
+  }
+}
+#endif  // !CONFIG_ANS
 
 #if CONFIG_VAR_TX
 static void pack_txb_tokens(vpx_writer *w,
@@ -973,6 +1039,11 @@
   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
   MODE_INFO *m;
   int plane;
+#if CONFIG_ANS
+  (void) tok;
+  (void) tok_end;
+  (void) plane;
+#endif  // !CONFIG_ANS
 
   xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
   m = xd->mi[0];
@@ -1008,6 +1079,7 @@
   if (supertx_enabled) return;
 #endif  // CONFIG_SUPERTX
 
+#if !CONFIG_ANS
   if (!m->mbmi.skip) {
     assert(*tok < tok_end);
     for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
@@ -1054,6 +1126,7 @@
       (*tok)++;
     }
   }
+#endif
 }
 
 static void write_partition(const VP10_COMMON *const cm,
@@ -1692,7 +1765,10 @@
 static size_t encode_tiles(VP10_COMP *cpi, uint8_t *data_ptr,
                            unsigned int *max_tile_sz) {
   VP10_COMMON *const cm = &cpi->common;
-  vpx_writer residual_bc;
+  vpx_writer mode_bc;
+#if CONFIG_ANS
+  struct AnsCoder token_ans;
+#endif
   int tile_row, tile_col;
   TOKENEXTRA *tok_end;
   size_t total_size = 0;
@@ -1710,32 +1786,49 @@
   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
       int tile_idx = tile_row * tile_cols + tile_col;
+      int put_tile_size = tile_col < tile_cols - 1 || tile_row < tile_rows - 1;
+      uint8_t *const mode_data_start =
+          data_ptr + total_size + (put_tile_size ? 4 : 0);
+      int token_section_size;
       TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
 
       tok_end = cpi->tile_tok[tile_row][tile_col] +
           cpi->tok_count[tile_row][tile_col];
 
-      if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1)
-        vpx_start_encode(&residual_bc, data_ptr + total_size + 4);
-      else
-        vpx_start_encode(&residual_bc, data_ptr + total_size);
+      vpx_start_encode(&mode_bc, mode_data_start);
 
+#if !CONFIG_ANS
+      (void) token_section_size;
       write_modes(cpi, &cpi->tile_data[tile_idx].tile_info,
-                  &residual_bc, &tok, tok_end);
+                  &mode_bc, &tok, tok_end);
       assert(tok == tok_end);
-      vpx_stop_encode(&residual_bc);
-      if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) {
+      vpx_stop_encode(&mode_bc);
+      if (put_tile_size) {
         unsigned int tile_sz;
 
         // size of this tile
-        assert(residual_bc.pos > 0);
-        tile_sz = residual_bc.pos - 1;
+        assert(mode_bc.pos > 0);
+        tile_sz = mode_bc.pos - 1;
         mem_put_le32(data_ptr + total_size, tile_sz);
         max_tile = max_tile > tile_sz ? max_tile : tile_sz;
         total_size += 4;
       }
-
-      total_size += residual_bc.pos;
+      total_size += mode_bc.pos;
+#else
+      write_modes(cpi, &cpi->tile_data[tile_idx].tile_info, &mode_bc,
+                  NULL, NULL);
+      vpx_stop_encode(&mode_bc);
+      ans_write_init(&token_ans, mode_data_start + mode_bc.pos);
+      pack_mb_tokens_ans(&token_ans, tok, tok_end, cm->bit_depth);
+      token_section_size = ans_write_end(&token_ans);
+      if (put_tile_size) {
+        // size of this tile
+        mem_put_be32(data_ptr + total_size,
+                     4 + mode_bc.pos + token_section_size);
+        total_size += 4;
+      }
+      total_size += mode_bc.pos + token_section_size;
+#endif  // !CONFIG_ANS
     }
   }
   *max_tile_sz = max_tile;
diff --git a/vp10/encoder/cost.c b/vp10/encoder/cost.c
index aab8263..ded51d3 100644
--- a/vp10/encoder/cost.c
+++ b/vp10/encoder/cost.c
@@ -10,6 +10,7 @@
 #include <assert.h>
 
 #include "vp10/encoder/cost.h"
+#include "vp10/common/entropy.h"
 
 const unsigned int vp10_prob_cost[256] = {
   2047, 2047, 1791, 1641, 1535, 1452, 1385, 1328, 1279, 1235, 1196, 1161,
@@ -51,6 +52,22 @@
   }
 }
 
+#if CONFIG_ANS
+void vp10_cost_tokens_ans(int *costs, const vpx_prob *tree_probs,
+                          const vpx_prob *token_probs, int skip_eob) {
+  int c_tree = 0;  // Cost of the "tree" nodes EOB and ZERO.
+  int i;
+  costs[EOB_TOKEN] = vp10_cost_bit(tree_probs[0], 0);
+  if (!skip_eob)
+    c_tree = vp10_cost_bit(tree_probs[0], 1);
+  costs[ZERO_TOKEN] = c_tree + vp10_cost_bit(tree_probs[1], 0);
+  c_tree += vp10_cost_bit(tree_probs[1], 1);
+  for (i = ONE_TOKEN; i <= CATEGORY6_TOKEN; ++i) {
+    costs[i] = c_tree + vp10_cost_bit(token_probs[i - ONE_TOKEN], 0);
+  }
+}
+#endif  // CONFIG_ANS
+
 void vp10_cost_tokens(int *costs, const vpx_prob *probs, vpx_tree tree) {
   cost(costs, tree, probs, 0, 0);
 }
diff --git a/vp10/encoder/cost.h b/vp10/encoder/cost.h
index b9619c6..551e4e5 100644
--- a/vp10/encoder/cost.h
+++ b/vp10/encoder/cost.h
@@ -48,6 +48,11 @@
 void vp10_cost_tokens(int *costs, const vpx_prob *probs, vpx_tree tree);
 void vp10_cost_tokens_skip(int *costs, const vpx_prob *probs, vpx_tree tree);
 
+#if CONFIG_ANS
+void vp10_cost_tokens_ans(int *costs, const vpx_prob *tree_probs,
+                          const vpx_prob *token_probs, int skip_eob);
+#endif
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/vp10/encoder/rd.c b/vp10/encoder/rd.c
index a1fd00d..991c713 100644
--- a/vp10/encoder/rd.c
+++ b/vp10/encoder/rd.c
@@ -136,12 +136,21 @@
       for (j = 0; j < REF_TYPES; ++j)
         for (k = 0; k < COEF_BANDS; ++k)
           for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
+#if CONFIG_ANS
+            const vpx_prob *const tree_probs = p[t][i][j][k][l];
+            vpx_prob pivot = tree_probs[PIVOT_NODE];
+            vp10_cost_tokens_ans((int *)c[t][i][j][k][0][l], tree_probs,
+                                 vp10_pareto8_token_probs[pivot - 1], 0);
+            vp10_cost_tokens_ans((int *)c[t][i][j][k][1][l], tree_probs,
+                                 vp10_pareto8_token_probs[pivot - 1], 1);
+#else
             vpx_prob probs[ENTROPY_NODES];
             vp10_model_to_full_probs(p[t][i][j][k][l], probs);
             vp10_cost_tokens((int *)c[t][i][j][k][0][l], probs,
                             vp10_coef_tree);
             vp10_cost_tokens_skip((int *)c[t][i][j][k][1][l], probs,
                                  vp10_coef_tree);
+#endif  // CONFIG_ANS
             assert(c[t][i][j][k][0][l][EOB_TOKEN] ==
                    c[t][i][j][k][1][l][EOB_TOKEN]);
           }
diff --git a/vp10/encoder/tokenize.c b/vp10/encoder/tokenize.c
index 64211a9..d1b4785 100644
--- a/vp10/encoder/tokenize.c
+++ b/vp10/encoder/tokenize.c
@@ -431,11 +431,12 @@
 };
 #endif
 
+#if !CONFIG_ANS
 const struct vp10_token vp10_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
 
 struct tokenize_b_args {
   VP10_COMP *cpi;
@@ -783,6 +784,14 @@
       vp10_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
                                               &arg);
       (*t)->token = EOSB_TOKEN;
+#if CONFIG_ANS
+      // TODO(aconverse): clip the number of bits in tokenize_b
+      // Smuggle TX_SIZE in the unused extrabits field so the ANS encoder
+      // knows the maximum number of extrabits to write at the end of the block
+      // (where it starts).
+      (*t)->extra = (EXTRABIT)(plane ? get_uv_tx_size(mbmi, &xd->plane[plane])
+                                     : mbmi->tx_size);
+#endif  // CONFIG_ANS
       (*t)++;
     }
   } else {
diff --git a/vp10/encoder/tokenize.h b/vp10/encoder/tokenize.h
index c68e6f2..c03ec02 100644
--- a/vp10/encoder/tokenize.h
+++ b/vp10/encoder/tokenize.h
@@ -43,7 +43,9 @@
 
 extern const vpx_tree_index vp10_coef_tree[];
 extern const vpx_tree_index vp10_coef_con_tree[];
+#if !CONFIG_ANS
 extern const struct vp10_token vp10_coef_encodings[];
+#endif  // !CONFIG_ANS
 
 int vp10_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
 int vp10_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
diff --git a/vp10/vp10_common.mk b/vp10/vp10_common.mk
index f8c2112..bc3d84a 100644
--- a/vp10/vp10_common.mk
+++ b/vp10/vp10_common.mk
@@ -74,6 +74,9 @@
 VP10_COMMON_SRCS-yes += common/vp10_inv_txfm2d.h
 VP10_COMMON_SRCS-yes += common/vp10_inv_txfm2d.c
 VP10_COMMON_SRCS-yes += common/vp10_inv_txfm2d_cfg.h
+VP10_COMMON_SRCS-$(CONFIG_ANS) += common/ans.h
+VP10_COMMON_SRCS-$(CONFIG_ANS) += common/divide.h
+VP10_COMMON_SRCS-$(CONFIG_ANS) += common/divide.c
 
 VP10_COMMON_SRCS-$(CONFIG_VP9_POSTPROC) += common/postproc.h
 VP10_COMMON_SRCS-$(CONFIG_VP9_POSTPROC) += common/postproc.c