Bit accounting.

This patch adds bit account infrastructure to the bit reader API.
When configured with --enable-accounting, every bit reader API
function records the number of bits necessary to decoding a symbol.
Accounting symbol entries are collected in global accounting data
structure, that can be used to understand exactly where bits are
spent (http://aomanalyzer.org). The data structure is cleared and
reused each frame to reduce memory usage. When configured without
--enable-accounting, bit accounting does not incur any runtime
overhead.

All aom_read_xxx functions now have an additional string parameter
that specifies the symbol name. By default, the ACCT_STR macro is
used (which expands to __func__). For more precise accounting,
these should be replaced with more descriptive names.

Change-Id: Ia2e1343cb842c9391b12b77272587dfbe307a56d
diff --git a/aom_dsp/bitreader.h b/aom_dsp/bitreader.h
index 575fff2..bf26c84 100644
--- a/aom_dsp/bitreader.h
+++ b/aom_dsp/bitreader.h
@@ -28,6 +28,29 @@
 #include "aom_dsp/prob.h"
 #include "av1/common/odintrin.h"
 
+#if CONFIG_ACCOUNTING
+#include "av1/common/accounting.h"
+#define ACCT_STR_NAME acct_str
+#define ACCT_STR_PARAM , const char *ACCT_STR_NAME
+#define ACCT_STR_ARG(s) , s
+#else
+#define ACCT_STR_PARAM
+#define ACCT_STR_ARG(s)
+#endif
+
+#define aom_read(r, prob, ACCT_STR_NAME) \
+  aom_read_(r, prob ACCT_STR_ARG(ACCT_STR_NAME))
+#define aom_read_bit(r, ACCT_STR_NAME) \
+  aom_read_bit_(r ACCT_STR_ARG(ACCT_STR_NAME))
+#define aom_read_tree(r, tree, probs, ACCT_STR_NAME) \
+  aom_read_tree_(r, tree, probs ACCT_STR_ARG(ACCT_STR_NAME))
+#define aom_read_literal(r, bits, ACCT_STR_NAME) \
+  aom_read_literal_(r, bits ACCT_STR_ARG(ACCT_STR_NAME))
+#define aom_read_tree_bits(r, tree, probs, ACCT_STR_NAME) \
+  aom_read_tree_bits_(r, tree, probs ACCT_STR_ARG(ACCT_STR_NAME))
+#define aom_read_symbol(r, cdf, nsymbs, ACCT_STR_NAME) \
+  aom_read_symbol_(r, cdf, nsymbs ACCT_STR_ARG(ACCT_STR_NAME))
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -105,64 +128,100 @@
 #endif
 }
 
-static INLINE int aom_read(aom_reader *r, int prob) {
+#if CONFIG_ACCOUNTING
+static INLINE void aom_process_accounting(const aom_reader *r ACCT_STR_PARAM) {
+  if (r->accounting != NULL) {
+    uint32_t tell_frac;
+    tell_frac = aom_reader_tell_frac(r);
+    aom_accounting_record(r->accounting, ACCT_STR_NAME,
+                          tell_frac - r->accounting->last_tell_frac);
+    r->accounting->last_tell_frac = tell_frac;
+  }
+}
+#endif
+
+static INLINE int aom_read_(aom_reader *r, int prob ACCT_STR_PARAM) {
+  int ret;
 #if CONFIG_ANS
-  return uabs_read(r, prob);
+  ret = uabs_read(r, prob);
 #elif CONFIG_DAALA_EC
-  return aom_daala_read(r, prob);
+  ret = aom_daala_read(r, prob);
 #else
-  return aom_dk_read(r, prob);
+  ret = aom_dk_read(r, prob);
 #endif
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
+  return ret;
 }
 
-static INLINE int aom_read_bit(aom_reader *r) {
+static INLINE int aom_read_bit_(aom_reader *r ACCT_STR_PARAM) {
+  int ret;
 #if CONFIG_ANS
-  return uabs_read_bit(r);  // Non trivial optimization at half probability
+  ret = uabs_read_bit(r);  // Non trivial optimization at half probability
 #else
-  return aom_read(r, 128);  // aom_prob_half
+  ret = aom_read(r, 128, NULL);  // aom_prob_half
 #endif
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
+  return ret;
 }
 
-static INLINE int aom_read_literal(aom_reader *r, int bits) {
+static INLINE int aom_read_literal_(aom_reader *r, int bits ACCT_STR_PARAM) {
   int literal = 0, bit;
 
-  for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r) << bit;
-
+  for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r, NULL) << bit;
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
   return literal;
 }
 
-static INLINE int aom_read_tree_bits(aom_reader *r, const aom_tree_index *tree,
-                                     const aom_prob *probs) {
+static INLINE int aom_read_tree_bits_(aom_reader *r, const aom_tree_index *tree,
+                                      const aom_prob *probs ACCT_STR_PARAM) {
   aom_tree_index i = 0;
 
-  while ((i = tree[i + aom_read(r, probs[i >> 1])]) > 0) continue;
-
+  while ((i = tree[i + aom_read(r, probs[i >> 1], NULL)]) > 0) continue;
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
   return -i;
 }
 
-static INLINE int aom_read_tree(aom_reader *r, const aom_tree_index *tree,
-                                const aom_prob *probs) {
+static INLINE int aom_read_tree_(aom_reader *r, const aom_tree_index *tree,
+                                 const aom_prob *probs ACCT_STR_PARAM) {
+  int ret;
 #if CONFIG_DAALA_EC
-  return daala_read_tree_bits(r, tree, probs);
+  ret = daala_read_tree_bits(r, tree, probs);
 #else
-  return aom_read_tree_bits(r, tree, probs);
+  ret = aom_read_tree_bits(r, tree, probs, NULL);
 #endif
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
+  return ret;
 }
 
-static INLINE int aom_read_symbol(aom_reader *r, const aom_cdf_prob *cdf,
-                                  int nsymbs) {
+static INLINE int aom_read_symbol_(aom_reader *r, const aom_cdf_prob *cdf,
+                                   int nsymbs ACCT_STR_PARAM) {
+  int ret;
 #if CONFIG_RANS
   (void)nsymbs;
-  return rans_read(r, cdf);
+  ret = rans_read(r, cdf);
 #elif CONFIG_DAALA_EC
-  return daala_read_symbol(r, cdf, nsymbs);
+  ret = daala_read_symbol(r, cdf, nsymbs);
 #else
   (void)r;
   (void)cdf;
   (void)nsymbs;
   assert(0 && "Unsupported bitreader operation");
-  return -1;
+  ret = -1;
 #endif
+#if CONFIG_ACCOUNTING
+  if (ACCT_STR_NAME) aom_process_accounting(r, ACCT_STR_NAME);
+#endif
+  return ret;
 }
 
 #ifdef __cplusplus
diff --git a/aom_dsp/daalaboolreader.c b/aom_dsp/daalaboolreader.c
index 65f9d50..f0da8eb 100644
--- a/aom_dsp/daalaboolreader.c
+++ b/aom_dsp/daalaboolreader.c
@@ -18,6 +18,9 @@
   r->buffer_end = buffer + size;
   r->buffer = buffer;
   od_ec_dec_init(&r->ec, buffer, size - 1);
+#if CONFIG_ACCOUNTING
+  r->accounting = NULL;
+#endif
   return 0;
 }
 
diff --git a/aom_dsp/daalaboolreader.h b/aom_dsp/daalaboolreader.h
index 896ec8b..10dc391 100644
--- a/aom_dsp/daalaboolreader.h
+++ b/aom_dsp/daalaboolreader.h
@@ -12,8 +12,12 @@
 #ifndef AOM_DSP_DAALABOOLREADER_H_
 #define AOM_DSP_DAALABOOLREADER_H_
 
+#include "aom/aom_integer.h"
 #include "aom_dsp/entdec.h"
 #include "aom_dsp/prob.h"
+#if CONFIG_ACCOUNTING
+#include "av1/common/accounting.h"
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -23,6 +27,9 @@
   const uint8_t *buffer;
   const uint8_t *buffer_end;
   od_ec_dec ec;
+#if CONFIG_ACCOUNTING
+  Accounting *accounting;
+#endif
 };
 
 typedef struct daala_reader daala_reader;
diff --git a/aom_dsp/dkboolreader.c b/aom_dsp/dkboolreader.c
index 060c462..805ea29 100644
--- a/aom_dsp/dkboolreader.c
+++ b/aom_dsp/dkboolreader.c
@@ -37,6 +37,9 @@
     r->decrypt_cb = decrypt_cb;
     r->decrypt_state = decrypt_state;
     aom_dk_reader_fill(r);
+#if CONFIG_ACCOUNTING
+    r->accounting = NULL;
+#endif
     return aom_dk_read_bit(r) != 0;  // marker bit
   }
 }
diff --git a/aom_dsp/dkboolreader.h b/aom_dsp/dkboolreader.h
index 8fda909..5e77814 100644
--- a/aom_dsp/dkboolreader.h
+++ b/aom_dsp/dkboolreader.h
@@ -20,6 +20,9 @@
 #include "aom/aomdx.h"
 #include "aom/aom_integer.h"
 #include "aom_dsp/prob.h"
+#if CONFIG_ACCOUNTING
+#include "av1/common/accounting.h"
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -45,6 +48,9 @@
   aom_decrypt_cb decrypt_cb;
   void *decrypt_state;
   uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
+#if CONFIG_ACCOUNTING
+  Accounting *accounting;
+#endif
 };
 
 int aom_dk_reader_init(struct aom_dk_reader *r, const uint8_t *buffer,
diff --git a/av1/av1_common.mk b/av1/av1_common.mk
index 8702b6e..7517f7f 100644
--- a/av1/av1_common.mk
+++ b/av1/av1_common.mk
@@ -87,6 +87,10 @@
 AV1_COMMON_SRCS-yes += common/dering.c
 AV1_COMMON_SRCS-yes += common/dering.h
 endif
+ifeq ($(CONFIG_ACCOUNTING),yes)
+AV1_COMMON_SRCS-yes += common/accounting.h
+AV1_COMMON_SRCS-yes += common/accounting.c
+endif
 AV1_COMMON_SRCS-yes += common/odintrin.c
 AV1_COMMON_SRCS-yes += common/odintrin.h
 
diff --git a/av1/common/accounting.c b/av1/common/accounting.c
new file mode 100644
index 0000000..41280af
--- /dev/null
+++ b/av1/common/accounting.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "aom/aom_integer.h"
+#include "./accounting.h"
+
+static int aom_accounting_hash(const char *str) {
+  uint32_t val;
+  const unsigned char *ustr;
+  val = 0;
+  ustr = (const unsigned char *)str;
+  /* This is about the worst hash one can design, but it should be good enough
+     here. */
+  while (*ustr) val += *ustr++;
+  return val % AOM_ACCOUNTING_HASH_SIZE;
+}
+
+/* Dictionary lookup based on an open-addressing hash table. */
+int aom_accounting_dictionary_lookup(Accounting *accounting, const char *str) {
+  int hash;
+  int len;
+  AccountingDictionary *dictionary;
+  dictionary = &accounting->syms.dictionary;
+  hash = aom_accounting_hash(str);
+  while (accounting->hash_dictionary[hash] != -1) {
+    if (strcmp(dictionary->strs[accounting->hash_dictionary[hash]], str) == 0) {
+      return accounting->hash_dictionary[hash];
+    }
+    hash++;
+    if (hash == AOM_ACCOUNTING_HASH_SIZE) hash = 0;
+  }
+  /* No match found. */
+  assert(dictionary->num_strs + 1 < MAX_SYMBOL_TYPES);
+  accounting->hash_dictionary[hash] = dictionary->num_strs;
+  len = strlen(str);
+  dictionary->strs[dictionary->num_strs] = malloc(len + 1);
+  snprintf(dictionary->strs[dictionary->num_strs], len + 1, "%s", str);
+  dictionary->num_strs++;
+  return dictionary->num_strs - 1;
+}
+
+void aom_accounting_init(Accounting *accounting) {
+  int i;
+  accounting->num_syms_allocated = 1000;
+  accounting->syms.syms =
+      malloc(sizeof(AccountingSymbol) * accounting->num_syms_allocated);
+  accounting->syms.dictionary.num_strs = 0;
+  assert(AOM_ACCOUNTING_HASH_SIZE > 2 * MAX_SYMBOL_TYPES);
+  for (i = 0; i < AOM_ACCOUNTING_HASH_SIZE; i++)
+    accounting->hash_dictionary[i] = -1;
+  aom_accounting_reset(accounting);
+}
+
+void aom_accounting_reset(Accounting *accounting) {
+  accounting->syms.num_syms = 0;
+  accounting->context.x = -1;
+  accounting->context.y = -1;
+  accounting->last_tell_frac = 0;
+}
+
+void aom_accounting_clear(Accounting *accounting) {
+  int i;
+  AccountingDictionary *dictionary;
+  free(accounting->syms.syms);
+  dictionary = &accounting->syms.dictionary;
+  for (i = 0; i < dictionary->num_strs; i++) {
+    free(dictionary->strs[i]);
+  }
+}
+
+void aom_accounting_set_context(Accounting *accounting, int16_t x, int16_t y) {
+  accounting->context.x = x;
+  accounting->context.y = y;
+}
+
+void aom_accounting_record(Accounting *accounting, const char *str,
+                           uint32_t bits) {
+  AccountingSymbol sym;
+  // Reuse previous symbol if it has the same context and symbol id.
+  if (accounting->syms.num_syms) {
+    AccountingSymbol *last_sym;
+    last_sym = &accounting->syms.syms[accounting->syms.num_syms - 1];
+    if (memcmp(&last_sym->context, &accounting->context,
+               sizeof(AccountingSymbolContext)) == 0) {
+      uint32_t id;
+      id = aom_accounting_dictionary_lookup(accounting, str);
+      if (id == last_sym->id) {
+        last_sym->bits += bits;
+        last_sym->samples++;
+        return;
+      }
+    }
+  }
+  sym.context = accounting->context;
+  sym.samples = 1;
+  sym.bits = bits;
+  sym.id = aom_accounting_dictionary_lookup(accounting, str);
+  assert(sym.id <= 255);
+  if (accounting->syms.num_syms == accounting->num_syms_allocated) {
+    accounting->num_syms_allocated *= 2;
+    accounting->syms.syms =
+        realloc(accounting->syms.syms,
+                sizeof(AccountingSymbol) * accounting->num_syms_allocated);
+    assert(accounting->syms.syms != NULL);
+  }
+  accounting->syms.syms[accounting->syms.num_syms++] = sym;
+}
+
+void aom_accounting_dump(Accounting *accounting) {
+  int i;
+  AccountingSymbol *sym;
+  printf("----- %d -----\n", accounting->syms.num_syms);
+  for (i = 0; i < accounting->syms.num_syms; i++) {
+    sym = &accounting->syms.syms[i];
+    printf("%s x: %d, y: %d bits: %f samples: %d\n",
+           accounting->syms.dictionary.strs[sym->id], sym->context.x,
+           sym->context.y, (float)sym->bits / 8.0, sym->samples);
+  }
+}
diff --git a/av1/common/accounting.h b/av1/common/accounting.h
new file mode 100644
index 0000000..04be326
--- /dev/null
+++ b/av1/common/accounting.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+#ifndef AOM_ACCOUNTING_H_
+#define AOM_ACCOUNTING_H_
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+#define AOM_ACCOUNTING_HASH_SIZE (1021)
+
+/* Max number of entries for symbol types in the dictionary (increase as
+   necessary). */
+#define MAX_SYMBOL_TYPES (256)
+
+/*The resolution of fractional-precision bit usage measurements, i.e.,
+   3 => 1/8th bits.*/
+#define AOM_ACCT_BITRES (3)
+
+typedef struct {
+  int16_t x;
+  int16_t y;
+} AccountingSymbolContext;
+
+typedef struct {
+  AccountingSymbolContext context;
+  uint32_t id;
+  /** Number of bits in units of 1/8 bit. */
+  uint32_t bits;
+  uint32_t samples;
+} AccountingSymbol;
+
+/** Dictionary for translating strings into id. */
+typedef struct {
+  char *(strs[MAX_SYMBOL_TYPES]);
+  int num_strs;
+} AccountingDictionary;
+
+typedef struct {
+  /** All recorded symbols decoded. */
+  AccountingSymbol *syms;
+  /** Number of symbols actually recorded. */
+  int num_syms;
+  /** Dictionary for translating strings into id. */
+  AccountingDictionary dictionary;
+} AccountingSymbols;
+
+typedef struct {
+  AccountingSymbols syms;
+  /** Size allocated for symbols (not all may be used). */
+  int num_syms_allocated;
+  int16_t hash_dictionary[AOM_ACCOUNTING_HASH_SIZE];
+  AccountingSymbolContext context;
+  uint32_t last_tell_frac;
+} Accounting;
+
+void aom_accounting_init(Accounting *accounting);
+void aom_accounting_reset(Accounting *accounting);
+void aom_accounting_clear(Accounting *accounting);
+void aom_accounting_set_context(Accounting *accounting, int16_t x, int16_t y);
+int aom_accounting_dictionary_lookup(Accounting *accounting, const char *str);
+void aom_accounting_record(Accounting *accounting, const char *str,
+                           uint32_t bits);
+void aom_accounting_dump(Accounting *accounting);
+#ifdef __cplusplus
+}  // extern "C"
+#endif  // __cplusplus
+#endif  // AOM_ACCOUNTING_H_
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index a7fa1b2..5433813 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -53,6 +53,7 @@
 #include "av1/decoder/dsubexp.h"
 
 #define MAX_AV1_HEADER_SIZE 80
+#define ACCT_STR __func__
 
 static int is_compound_reference_allowed(const AV1_COMMON *cm) {
   int i;
@@ -105,8 +106,8 @@
 }
 #else
 static TX_MODE read_tx_mode(aom_reader *r) {
-  TX_MODE tx_mode = aom_read_literal(r, 2);
-  if (tx_mode == ALLOW_32X32) tx_mode += aom_read_bit(r);
+  TX_MODE tx_mode = aom_read_literal(r, 2, ACCT_STR);
+  if (tx_mode == ALLOW_32X32) tx_mode += aom_read_bit(r, ACCT_STR);
   return tx_mode;
 }
 #endif
@@ -116,22 +117,22 @@
 
   for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
     for (j = TX_4X4; j < TX_SIZES - 3; ++j)
-      av1_diff_update_prob(r, &tx_probs->p8x8[i][j]);
+      av1_diff_update_prob(r, &tx_probs->p8x8[i][j], ACCT_STR);
 
   for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
     for (j = TX_4X4; j < TX_SIZES - 2; ++j)
-      av1_diff_update_prob(r, &tx_probs->p16x16[i][j]);
+      av1_diff_update_prob(r, &tx_probs->p16x16[i][j], ACCT_STR);
 
   for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
     for (j = TX_4X4; j < TX_SIZES - 1; ++j)
-      av1_diff_update_prob(r, &tx_probs->p32x32[i][j]);
+      av1_diff_update_prob(r, &tx_probs->p32x32[i][j], ACCT_STR);
 }
 
 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, aom_reader *r) {
   int i, j;
   for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) {
     for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
-      av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
+      av1_diff_update_prob(r, &fc->switchable_interp_prob[j][i], ACCT_STR);
 #if CONFIG_DAALA_EC
     av1_tree_to_cdf(av1_switchable_interp_tree, fc->switchable_interp_prob[j],
                     fc->switchable_interp_cdf[j]);
@@ -143,18 +144,18 @@
   int i;
 #if CONFIG_REF_MV
   for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
-    av1_diff_update_prob(r, &fc->newmv_prob[i]);
+    av1_diff_update_prob(r, &fc->newmv_prob[i], ACCT_STR);
   for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i)
-    av1_diff_update_prob(r, &fc->zeromv_prob[i]);
+    av1_diff_update_prob(r, &fc->zeromv_prob[i], ACCT_STR);
   for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
-    av1_diff_update_prob(r, &fc->refmv_prob[i]);
+    av1_diff_update_prob(r, &fc->refmv_prob[i], ACCT_STR);
   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    av1_diff_update_prob(r, &fc->drl_prob[i]);
+    av1_diff_update_prob(r, &fc->drl_prob[i], ACCT_STR);
 #else
   int j;
   for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
     for (j = 0; j < INTER_MODES - 1; ++j)
-      av1_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
+      av1_diff_update_prob(r, &fc->inter_mode_probs[i][j], ACCT_STR);
 #endif
 }
 
@@ -173,8 +174,9 @@
 static REFERENCE_MODE read_frame_reference_mode(const AV1_COMMON *cm,
                                                 aom_reader *r) {
   if (is_compound_reference_allowed(cm)) {
-    return aom_read_bit(r)
-               ? (aom_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
+    return aom_read_bit(r, ACCT_STR)
+               ? (aom_read_bit(r, ACCT_STR) ? REFERENCE_MODE_SELECT
+                                            : COMPOUND_REFERENCE)
                : SINGLE_REFERENCE;
   } else {
     return SINGLE_REFERENCE;
@@ -188,24 +190,23 @@
 
   if (cm->reference_mode == REFERENCE_MODE_SELECT)
     for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
-      av1_diff_update_prob(r, &fc->comp_inter_prob[i]);
+      av1_diff_update_prob(r, &fc->comp_inter_prob[i], ACCT_STR);
 
   if (cm->reference_mode != COMPOUND_REFERENCE)
     for (i = 0; i < REF_CONTEXTS; ++i)
       for (j = 0; j < (SINGLE_REFS - 1); ++j)
-        av1_diff_update_prob(r, &fc->single_ref_prob[i][j]);
+        av1_diff_update_prob(r, &fc->single_ref_prob[i][j], ACCT_STR);
 
   if (cm->reference_mode != SINGLE_REFERENCE)
 #if CONFIG_EXT_REFS
     for (i = 0; i < REF_CONTEXTS; ++i) {
       for (j = 0; j < (FWD_REFS - 1); ++j)
-        av1_diff_update_prob(r, &fc->comp_fwdref_prob[i][j]);
-      for (j = 0; j < (BWD_REFS - 1); ++j)
-        av1_diff_update_prob(r, &fc->comp_bwdref_prob[i][j]);
+        av1_diff_update_prob(r, &fc->comp_fwdref_prob[i][j], ACCT_STR);
+      for (j = 0; j < (BWD_REFS - 1); ++j) (r, &fc->comp_bwdref_prob[i][j]);
     }
 #else
     for (i = 0; i < REF_CONTEXTS; ++i)
-      av1_diff_update_prob(r, &fc->comp_ref_prob[i]);
+      av1_diff_update_prob(r, &fc->comp_ref_prob[i], ACCT_STR);
 #endif  // CONFIG_EXT_REFS
 }
 
@@ -213,9 +214,10 @@
   int i;
   for (i = 0; i < n; ++i)
 #if CONFIG_MISC_FIXES
-    av1_diff_update_prob(r, &p[i]);
+    av1_diff_update_prob(r, &p[i], ACCT_STR);
 #else
-    if (aom_read(r, MV_UPDATE_PROB)) p[i] = (aom_read_literal(r, 7) << 1) | 1;
+    if (aom_read(r, MV_UPDATE_PROB, ACCT_STR))
+      p[i] = (aom_read_literal(r, 7, ACCT_STR) << 1) | 1;
 #endif
 }
 
@@ -469,6 +471,9 @@
   const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
   const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
 
+#if CONFIG_ACCOUNTING
+  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
+#endif
   MB_MODE_INFO *mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
                                    y_mis, bwl, bhl);
 
@@ -599,14 +604,14 @@
   if (has_rows && has_cols)
 #if CONFIG_DAALA_EC
     p = (PARTITION_TYPE)aom_read_symbol(r, cm->fc->partition_cdf[ctx],
-                                        PARTITION_TYPES);
+                                        PARTITION_TYPES, ACCT_STR);
 #else
-    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs);
+    p = (PARTITION_TYPE)aom_read_tree(r, av1_partition_tree, probs, ACCT_STR);
 #endif
   else if (!has_rows && has_cols)
-    p = aom_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
+    p = aom_read(r, probs[1], ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
   else if (has_rows && !has_cols)
-    p = aom_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
+    p = aom_read(r, probs[2], ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
   else
     p = PARTITION_SPLIT;
 
@@ -705,13 +710,13 @@
                                    aom_reader *r) {
   int i, j, k, l, m;
 
-  if (aom_read_bit(r))
+  if (aom_read_bit(r, ACCT_STR))
     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 (m = 0; m < UNCONSTRAINED_NODES; ++m)
-              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
+              av1_diff_update_prob(r, &coef_probs[i][j][k][l][m], ACCT_STR);
 }
 
 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, aom_reader *r) {
@@ -1266,7 +1271,9 @@
         aom_memalign(32, tile_cols * tile_rows * (sizeof(*pbi->tile_data))));
     pbi->total_tiles = tile_rows * tile_cols;
   }
-
+#if CONFIG_ACCOUNTING
+  aom_accounting_reset(&pbi->accounting);
+#endif
   // Load all tile information into tile_data.
   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
@@ -1284,6 +1291,9 @@
       setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
                           &tile_data->bit_reader, pbi->decrypt_cb,
                           pbi->decrypt_state);
+#if CONFIG_ACCOUNTING
+      tile_data->bit_reader.accounting = &pbi->accounting;
+#endif
       av1_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
 #if CONFIG_PALETTE
       tile_data->xd.plane[0].color_index_map = tile_data->color_index_map[0];
@@ -1301,6 +1311,10 @@
         const int col =
             pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
         tile_data = pbi->tile_data + tile_cols * tile_row + col;
+#if CONFIG_ACCOUNTING
+        tile_data->bit_reader.accounting->last_tell_frac =
+            aom_reader_tell_frac(&tile_data->bit_reader);
+#endif
         av1_tile_set_col(&tile, tile_data->cm, col);
         av1_zero(tile_data->xd.left_context);
         av1_zero(tile_data->xd.left_seg_context);
@@ -1342,6 +1356,10 @@
     }
   }
 
+#if CONFIG_ACCOUNTING
+// aom_accounting_dump(&pbi->accounting);
+#endif
+
   // Loopfilter remaining rows in the frame.
   if (cm->lf.filter_level && !cm->skip_loop_filter) {
     LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
@@ -1956,11 +1974,11 @@
 
 static void read_ext_tx_probs(FRAME_CONTEXT *fc, aom_reader *r) {
   int i, j, k;
-  if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
+  if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
     for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
       for (j = 0; j < TX_TYPES; ++j) {
         for (k = 0; k < TX_TYPES - 1; ++k)
-          av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k]);
+          av1_diff_update_prob(r, &fc->intra_ext_tx_prob[i][j][k], ACCT_STR);
 #if CONFIG_DAALA_EC
         av1_tree_to_cdf(av1_ext_tx_tree, fc->intra_ext_tx_prob[i][j],
                         fc->intra_ext_tx_cdf[i][j]);
@@ -1968,10 +1986,10 @@
       }
     }
   }
-  if (aom_read(r, GROUP_DIFF_UPDATE_PROB)) {
+  if (aom_read(r, GROUP_DIFF_UPDATE_PROB, ACCT_STR)) {
     for (i = TX_4X4; i < EXT_TX_SIZES; ++i) {
       for (k = 0; k < TX_TYPES - 1; ++k)
-        av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k]);
+        av1_diff_update_prob(r, &fc->inter_ext_tx_prob[i][k], ACCT_STR);
 #if CONFIG_DAALA_EC
       av1_tree_to_cdf(av1_ext_tx_tree, fc->inter_ext_tx_prob[i],
                       fc->inter_ext_tx_cdf[i]);
@@ -2002,16 +2020,16 @@
   read_coef_probs(fc, cm->tx_mode, &r);
 
   for (k = 0; k < SKIP_CONTEXTS; ++k)
-    av1_diff_update_prob(&r, &fc->skip_probs[k]);
+    av1_diff_update_prob(&r, &fc->skip_probs[k], ACCT_STR);
 
 #if CONFIG_MISC_FIXES
   if (cm->seg.enabled && cm->seg.update_map) {
     if (cm->seg.temporal_update) {
       for (k = 0; k < PREDICTION_PROBS; k++)
-        av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k]);
+        av1_diff_update_prob(&r, &cm->fc->seg.pred_probs[k], ACCT_STR);
     }
     for (k = 0; k < MAX_SEGMENTS - 1; k++)
-      av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k]);
+      av1_diff_update_prob(&r, &cm->fc->seg.tree_probs[k], ACCT_STR);
 #if CONFIG_DAALA_EC
     av1_tree_to_cdf(av1_segment_tree, cm->fc->seg.tree_probs,
                     cm->fc->seg.tree_cdf);
@@ -2020,11 +2038,11 @@
 
   for (j = 0; j < INTRA_MODES; j++)
     for (i = 0; i < INTRA_MODES - 1; ++i)
-      av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i]);
+      av1_diff_update_prob(&r, &fc->uv_mode_prob[j][i], ACCT_STR);
 
   for (j = 0; j < PARTITION_CONTEXTS; ++j) {
     for (i = 0; i < PARTITION_TYPES - 1; ++i)
-      av1_diff_update_prob(&r, &fc->partition_prob[j][i]);
+      av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
 #if CONFIG_DAALA_EC
     av1_tree_to_cdf(av1_partition_tree, fc->partition_prob[j],
                     fc->partition_cdf[j]);
@@ -2038,7 +2056,7 @@
     for (k = 0; k < INTRA_MODES; k++)
       for (j = 0; j < INTRA_MODES; j++)
         for (i = 0; i < INTRA_MODES - 1; ++i)
-          av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i]);
+          av1_diff_update_prob(&r, &cm->kf_y_prob[k][j][i], ACCT_STR);
 #endif
   } else {
 #if !CONFIG_REF_MV
@@ -2050,14 +2068,14 @@
     for (j = 0; j < BLOCK_SIZES; ++j)
       if (is_motion_variation_allowed_bsize(j)) {
         for (i = 0; i < MOTION_MODES - 1; ++i)
-          av1_diff_update_prob(&r, &fc->motion_mode_prob[j][i]);
+          av1_diff_update_prob(&r, &fc->motion_mode_prob[j][i], ACCT_STR);
       }
 #endif  // CONFIG_MOTION_VAR
 
     if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
 
     for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
-      av1_diff_update_prob(&r, &fc->intra_inter_prob[i]);
+      av1_diff_update_prob(&r, &fc->intra_inter_prob[i], ACCT_STR);
 
 #if !CONFIG_MISC_FIXES
     cm->reference_mode = read_frame_reference_mode(cm, &r);
@@ -2068,12 +2086,12 @@
 
     for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
       for (i = 0; i < INTRA_MODES - 1; ++i)
-        av1_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
+        av1_diff_update_prob(&r, &fc->y_mode_prob[j][i], ACCT_STR);
 
 #if !CONFIG_MISC_FIXES
     for (j = 0; j < PARTITION_CONTEXTS; ++j) {
       for (i = 0; i < PARTITION_TYPES - 1; ++i)
-        av1_diff_update_prob(&r, &fc->partition_prob[j][i]);
+        av1_diff_update_prob(&r, &fc->partition_prob[j][i], ACCT_STR);
 #if CONFIG_DAALA_EC
       av1_tree_to_cdf(av1_partition_tree, fc->partition_prob[j],
                       fc->partition_cdf[j]);
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index f977bbc..65f6d66 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -25,8 +25,10 @@
 
 #include "aom_dsp/aom_dsp_common.h"
 
+#define ACCT_STR __func__
+
 static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
-  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p);
+  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p, ACCT_STR);
 }
 
 static PREDICTION_MODE read_intra_mode_y(AV1_COMMON *cm, MACROBLOCKD *xd,
@@ -55,7 +57,7 @@
   int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
   aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];
 
-  if (aom_read(r, mode_prob) == 0) {
+  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
     if (counts) ++counts->newmv_mode[mode_ctx][0];
     return NEWMV;
   }
@@ -66,7 +68,7 @@
   mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
 
   mode_prob = cm->fc->zeromv_prob[mode_ctx];
-  if (aom_read(r, mode_prob) == 0) {
+  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
     if (counts) ++counts->zeromv_mode[mode_ctx][0];
     return ZEROMV;
   }
@@ -79,7 +81,7 @@
   if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
 
   mode_prob = cm->fc->refmv_prob[mode_ctx];
-  if (aom_read(r, mode_prob) == 0) {
+  if (aom_read(r, mode_prob) == 0, ACCT_STR) {
     if (counts) ++counts->refmv_mode[mode_ctx][0];
     return NEARESTMV;
   } else {
@@ -90,8 +92,8 @@
   // Invalid prediction mode.
   assert(0);
 #else
-  const int mode =
-      aom_read_tree(r, av1_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
+  const int mode = aom_read_tree(r, av1_inter_mode_tree,
+                                 cm->fc->inter_mode_probs[ctx], ACCT_STR);
   FRAME_COUNTS *counts = xd->counts;
   if (counts) ++counts->inter_mode[ctx][mode];
 
@@ -111,7 +113,7 @@
       if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
         uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
         aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
-        if (!aom_read(r, drl_prob)) {
+        if (!aom_read(r, drl_prob), ACCT_STR) {
           mbmi->ref_mv_idx = idx;
           if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
           return;
@@ -131,7 +133,7 @@
       if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
         uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
         aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
-        if (!aom_read(r, drl_prob)) {
+        if (!aom_read(r, drl_prob), ACCT_STR) {
           mbmi->ref_mv_idx = idx - 1;
           if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
           return;
@@ -151,8 +153,9 @@
     int motion_mode;
     FRAME_COUNTS *counts = xd->counts;
 
-    motion_mode = aom_read_tree(r, av1_motion_mode_tree,
-                                cm->fc->motion_mode_prob[mbmi->sb_type]);
+    motion_mode =
+        aom_read_tree(r, av1_motion_mode_tree,
+                      cm->fc->motion_mode_prob[mbmi->sb_type], ACCT_STR);
     if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
     return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
   } else {
@@ -164,9 +167,9 @@
 static int read_segment_id(aom_reader *r,
                            const struct segmentation_probs *segp) {
 #if CONFIG_DAALA_EC
-  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS);
+  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
 #else
-  return aom_read_tree(r, av1_segment_tree, segp->tree_probs);
+  return aom_read_tree(r, av1_segment_tree, segp->tree_probs, ACCT_STR);
 #endif
 }
 
@@ -175,11 +178,11 @@
   FRAME_COUNTS *counts = xd->counts;
   const int ctx = get_tx_size_context(xd);
   const aom_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
-  TX_SIZE tx_size = aom_read(r, tx_probs[0]) ? TX_8X8 : TX_4X4;
+  TX_SIZE tx_size = aom_read(r, tx_probs[0], ACCT_STR) ? TX_8X8 : TX_4X4;
   if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
-    tx_size += aom_read(r, tx_probs[1]);
+    tx_size += aom_read(r, tx_probs[1], ACCT_STR);
     if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
-      tx_size += aom_read(r, tx_probs[2]);
+      tx_size += aom_read(r, tx_probs[2], ACCT_STR);
   }
 
   if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
@@ -298,7 +301,7 @@
   if (seg->temporal_update) {
     const int ctx = av1_get_pred_context_seg_id(xd);
     const aom_prob pred_prob = segp->pred_probs[ctx];
-    mbmi->seg_id_predicted = aom_read(r, pred_prob);
+    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
 #if CONFIG_MISC_FIXES
     if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
 #endif
@@ -326,7 +329,7 @@
     return 1;
   } else {
     const int ctx = av1_get_skip_context(xd);
-    const int skip = aom_read(r, cm->fc->skip_probs[ctx]);
+    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
     FRAME_COUNTS *counts = xd->counts;
     if (counts) ++counts->skip[ctx][skip];
     return skip;
@@ -337,10 +340,10 @@
 static INLINE int read_uniform(aom_reader *r, int n) {
   const int l = get_unsigned_bits(n);
   const int m = (1 << l) - n;
-  const int v = aom_read_literal(r, l - 1);
+  const int v = aom_read_literal(r, l - 1, ACCT_STR);
 
   assert(l != 0);
-  return (v < m) ? v : ((v << 1) - m + aom_read_literal(r, 1));
+  return (v < m) ? v : ((v << 1) - m + aom_read_literal(r, 1), ACCT_STR);
 }
 #endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
 
@@ -478,9 +481,9 @@
       !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
     FRAME_COUNTS *counts = xd->counts;
     TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
-    mbmi->tx_type =
-        aom_read_tree(r, av1_ext_tx_tree,
-                      cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom]);
+    mbmi->tx_type = aom_read_tree(
+        r, av1_ext_tx_tree,
+        cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom], ACCT_STR);
     if (counts)
       ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
   } else {
@@ -491,29 +494,31 @@
 static int read_mv_component(aom_reader *r, const nmv_component *mvcomp,
                              int usehp) {
   int mag, d, fr, hp;
-  const int sign = aom_read(r, mvcomp->sign);
-  const int mv_class = aom_read_tree(r, av1_mv_class_tree, mvcomp->classes);
+  const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
+  const int mv_class =
+      aom_read_tree(r, av1_mv_class_tree, mvcomp->classes, ACCT_STR);
   const int class0 = mv_class == MV_CLASS_0;
 
   // Integer part
   if (class0) {
-    d = aom_read_tree(r, av1_mv_class0_tree, mvcomp->class0);
+    d = aom_read_tree(r, av1_mv_class0_tree, mvcomp->class0, ACCT_STR);
     mag = 0;
   } else {
     int i;
     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
 
     d = 0;
-    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i]) << i;
+    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
     mag = CLASS0_SIZE << (mv_class + 2);
   }
 
   // Fractional part
   fr = aom_read_tree(r, av1_mv_fp_tree,
-                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
+                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp, ACCT_STR);
 
   // High precision part (if hp is not used, the default value of the hp is 1)
-  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
+  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
+             : 1;
 
   // Result
   mag += ((d << 3) | (fr << 1) | hp) + 1;
@@ -524,7 +529,7 @@
                            const nmv_context *ctx, nmv_context_counts *counts,
                            int allow_hp) {
   const MV_JOINT_TYPE joint_type =
-      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints);
+      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints, ACCT_STR);
   const int use_hp = allow_hp && av1_use_mv_hp(ref);
   MV diff = { 0, 0 };
 
@@ -546,7 +551,7 @@
   if (cm->reference_mode == REFERENCE_MODE_SELECT) {
     const int ctx = av1_get_reference_mode_context(cm, xd);
     const REFERENCE_MODE mode =
-        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx]);
+        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
     FRAME_COUNTS *counts = xd->counts;
     if (counts) ++counts->comp_inter[ctx][mode];
     return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
@@ -574,30 +579,34 @@
       const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
       // Read forward references.
       const int ctx_fwd = av1_get_pred_context_comp_fwdref_p(cm, xd);
-      const int bit_fwd = aom_read(r, fc->comp_fwdref_prob[ctx_fwd][0]);
+      const int bit_fwd =
+          aom_read(r, fc->comp_fwdref_prob[ctx_fwd][0], ACCT_STR);
       if (counts) ++counts->comp_fwdref[ctx_fwd][0][bit_fwd];
       if (!bit_fwd) {
         const int ctx_fwd1 = av1_get_pred_context_comp_fwdref_p1(cm, xd);
-        const int bit_fwd1 = aom_read(r, fc->comp_fwdref_prob[ctx_fwd1][1]);
+        const int bit_fwd1 =
+            aom_read(r, fc->comp_fwdref_prob[ctx_fwd1][1], ACCT_STR);
         if (counts) ++counts->comp_fwdref[ctx_fwd1][1][bit_fwd1];
         ref_frame[!idx] = cm->comp_fwd_ref[bit_fwd1 ? 0 : 1];
       } else {
         const int ctx_fwd2 = av1_get_pred_context_comp_fwdref_p2(cm, xd);
-        const int bit_fwd2 = aom_read(r, fc->comp_fwdref_prob[ctx_fwd2][2]);
+        const int bit_fwd2 =
+            aom_read(r, fc->comp_fwdref_prob[ctx_fwd2][2], ACCT_STR);
         if (counts) ++counts->comp_fwdref[ctx_fwd2][2][bit_fwd2];
         ref_frame[!idx] = cm->comp_fwd_ref[bit_fwd2 ? 3 : 2];
       }
       // Read backward references.
       {
         const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
-        const int bit_bwd = aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0]);
+        const int bit_bwd =
+            aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0], ACCT_STR);
         if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
         ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
       }
 #else
       const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
       const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
-      const int bit = aom_read(r, fc->comp_ref_prob[ctx]);
+      const int bit = aom_read(r, fc->comp_ref_prob[ctx], ACCT_STR);
       if (counts) ++counts->comp_ref[ctx][bit];
       ref_frame[idx] = cm->comp_fixed_ref;
       ref_frame[!idx] = cm->comp_var_ref[bit];
@@ -605,36 +614,36 @@
     } else if (mode == SINGLE_REFERENCE) {
 #if CONFIG_EXT_REFS
       const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
-      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0]);
+      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0], ACCT_STR);
       if (counts) ++counts->single_ref[ctx0][0][bit0];
       if (bit0) {
         const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
-        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1]);
+        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1], ACCT_STR);
         if (counts) ++counts->single_ref[ctx1][1][bit1];
         ref_frame[0] = bit1 ? ALTREF_FRAME : BWDREF_FRAME;
       } else {
         const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
-        const int bit2 = aom_read(r, fc->single_ref_prob[ctx2][2]);
+        const int bit2 = aom_read(r, fc->single_ref_prob[ctx2][2], ACCT_STR);
         if (counts) ++counts->single_ref[ctx2][2][bit2];
         if (!bit2) {
           const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
-          const int bit3 = aom_read(r, fc->single_ref_prob[ctx3][3]);
+          const int bit3 = aom_read(r, fc->single_ref_prob[ctx3][3], ACCT_STR);
           if (counts) ++counts->single_ref[ctx3][3][bit3];
           ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
         } else {
           const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
-          const int bit4 = aom_read(r, fc->single_ref_prob[ctx4][4]);
+          const int bit4 = aom_read(r, fc->single_ref_prob[ctx4][4], ACCT_STR);
           if (counts) ++counts->single_ref[ctx4][4][bit4];
           ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
         }
       }
 #else
       const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
-      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0]);
+      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0], ACCT_STR);
       if (counts) ++counts->single_ref[ctx0][0][bit0];
       if (bit0) {
         const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
-        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1]);
+        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1], ACCT_STR);
         if (counts) ++counts->single_ref[ctx1][1][bit1];
         ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
       } else {
@@ -661,10 +670,12 @@
 #if CONFIG_DAALA_EC
       const InterpFilter type =
           (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
-              r, cm->fc->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS)];
+              r, cm->fc->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
+              ACCT_STR)];
 #else
       const InterpFilter type = (InterpFilter)aom_read_tree(
-          r, av1_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
+          r, av1_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx],
+          ACCT_STR);
 #endif
       FRAME_COUNTS *counts = xd->counts;
       if (counts) ++counts->switchable_interp[ctx][type];
@@ -806,7 +817,7 @@
     return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
   } else {
     const int ctx = av1_get_intra_inter_context(xd);
-    const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx]);
+    const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
     FRAME_COUNTS *counts = xd->counts;
     if (counts) ++counts->intra_inter[ctx][is_inter];
     return is_inter;
@@ -1049,21 +1060,23 @@
     if (inter_block) {
 #if CONFIG_DAALA_EC
       mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
-          r, cm->fc->inter_ext_tx_cdf[mbmi->tx_size], TX_TYPES)];
+          r, cm->fc->inter_ext_tx_cdf[mbmi->tx_size], TX_TYPES, ACCT_STR)];
 #else
-      mbmi->tx_type = aom_read_tree(r, av1_ext_tx_tree,
-                                    cm->fc->inter_ext_tx_prob[mbmi->tx_size]);
+      mbmi->tx_type =
+          aom_read_tree(r, av1_ext_tx_tree,
+                        cm->fc->inter_ext_tx_prob[mbmi->tx_size], ACCT_STR);
 #endif
       if (counts) ++counts->inter_ext_tx[mbmi->tx_size][mbmi->tx_type];
     } else {
       const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
 #if CONFIG_DAALA_EC
       mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
-          r, cm->fc->intra_ext_tx_cdf[mbmi->tx_size][tx_type_nom], TX_TYPES)];
+          r, cm->fc->intra_ext_tx_cdf[mbmi->tx_size][tx_type_nom], TX_TYPES,
+          ACCT_STR)];
 #else
-      mbmi->tx_type =
-          aom_read_tree(r, av1_ext_tx_tree,
-                        cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom]);
+      mbmi->tx_type = aom_read_tree(
+          r, av1_ext_tx_tree,
+          cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom], ACCT_STR);
 #endif
       if (counts)
         ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 2c38cca..a259641 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -119,7 +119,9 @@
 #if CONFIG_AOM_QM
   aom_qm_init(cm);
 #endif
-
+#if CONFIG_ACCOUNTING
+  aom_accounting_init(&pbi->accounting);
+#endif
   cm->error.setjmp = 0;
 
   aom_get_worker_interface()->init(&pbi->lf_worker);
@@ -147,6 +149,10 @@
     av1_loop_filter_dealloc(&pbi->lf_row_sync);
   }
 
+#if CONFIG_ACCOUNTING
+  aom_accounting_clear(&pbi->accounting);
+#endif
+
   aom_free(pbi);
 }
 
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index 20f410d..01d2792 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -22,6 +22,9 @@
 #include "av1/common/thread_common.h"
 #include "av1/common/onyxc_int.h"
 #include "av1/decoder/dthread.h"
+#if CONFIG_ACCOUNTING
+#include "av1/common/accounting.h"
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -84,6 +87,9 @@
   int inv_tile_order;
   int need_resync;   // wait for key/intra-only frame.
   int hold_ref_buf;  // hold the reference buffer.
+#if CONFIG_ACCOUNTING
+  Accounting accounting;
+#endif
 } AV1Decoder;
 
 int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
diff --git a/av1/decoder/detokenize.c b/av1/decoder/detokenize.c
index f7d3f61..9f920f2 100644
--- a/av1/decoder/detokenize.c
+++ b/av1/decoder/detokenize.c
@@ -21,6 +21,8 @@
 
 #include "av1/decoder/detokenize.h"
 
+#define ACCT_STR __func__
+
 #define EOB_CONTEXT_NODE 0
 #define ZERO_CONTEXT_NODE 1
 #define ONE_CONTEXT_NODE 2
@@ -40,7 +42,7 @@
 
 static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
   int i, val = 0;
-  for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i]);
+  for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i], ACCT_STR);
   return val;
 }
 
@@ -130,12 +132,12 @@
     band = *band_translate++;
     prob = coef_probs[band][ctx];
     if (counts) ++eob_branch_count[band][ctx];
-    if (!aom_read(r, prob[EOB_CONTEXT_NODE])) {
+    if (!aom_read(r, prob[EOB_CONTEXT_NODE], ACCT_STR)) {
       INCREMENT_COUNT(EOB_MODEL_TOKEN);
       break;
     }
 
-    while (!aom_read(r, prob[ZERO_CONTEXT_NODE])) {
+    while (!aom_read(r, prob[ZERO_CONTEXT_NODE], ACCT_STR)) {
       INCREMENT_COUNT(ZERO_TOKEN);
       dqv = dq[1];
       token_cache[scan[c]] = 0;
@@ -148,8 +150,8 @@
 
 #if CONFIG_RANS || CONFIG_DAALA_EC
     cdf = &coef_cdfs[band][ctx];
-    token =
-        ONE_TOKEN + aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1);
+    token = ONE_TOKEN +
+            aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1, ACCT_STR);
     INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
     switch (token) {
       case ONE_TOKEN:
@@ -198,14 +200,14 @@
       }
     }
 #else  // CONFIG_RANS
-    if (!aom_read(r, prob[ONE_CONTEXT_NODE])) {
+    if (!aom_read(r, prob[ONE_CONTEXT_NODE], ACCT_STR)) {
       INCREMENT_COUNT(ONE_TOKEN);
       token = ONE_TOKEN;
       val = 1;
     } else {
       INCREMENT_COUNT(TWO_TOKEN);
       token = aom_read_tree(r, av1_coef_con_tree,
-                            av1_pareto8_full[prob[PIVOT_NODE] - 1]);
+                            av1_pareto8_full[prob[PIVOT_NODE] - 1], ACCT_STR);
       switch (token) {
         case TWO_TOKEN:
         case THREE_TOKEN:
@@ -260,12 +262,13 @@
     v = (val * dqv) >> dq_shift;
 #if CONFIG_COEFFICIENT_RANGE_CHECKING
 #if CONFIG_AOM_HIGHBITDEPTH
-    dqcoeff[scan[c]] = highbd_check_range((aom_read_bit(r) ? -v : v), xd->bd);
+    dqcoeff[scan[c]] =
+        highbd_check_range((aom_read_bit(r, ACCT_STR) ? -v : v), xd->bd);
 #else
-    dqcoeff[scan[c]] = check_range(aom_read_bit(r) ? -v : v);
+    dqcoeff[scan[c]] = check_range(aom_read_bit(r, ACCT_STR) ? -v : v);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 #else
-    dqcoeff[scan[c]] = aom_read_bit(r) ? -v : v;
+    dqcoeff[scan[c]] = aom_read_bit(r, ACCT_STR) ? -v : v;
 #endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
     token_cache[scan[c]] = av1_pt_energy_class[token];
     ++c;
diff --git a/av1/decoder/dsubexp.c b/av1/decoder/dsubexp.c
index 5d941c0..64f26a1 100644
--- a/av1/decoder/dsubexp.c
+++ b/av1/decoder/dsubexp.c
@@ -21,11 +21,16 @@
   return (v & 1) ? m - ((v + 1) >> 1) : m + (v >> 1);
 }
 
-static int decode_uniform(aom_reader *r) {
+#define decode_uniform(r, ACCT_STR_NAME) \
+  decode_uniform_(r ACCT_STR_ARG(ACCT_STR_NAME))
+#define decode_term_subexp(r, ACCT_STR_NAME) \
+  decode_term_subexp_(r ACCT_STR_ARG(ACCT_STR_NAME))
+
+static int decode_uniform_(aom_reader *r ACCT_STR_PARAM) {
   const int l = 8;
   const int m = (1 << l) - 191 + CONFIG_MISC_FIXES;
-  const int v = aom_read_literal(r, l - 1);
-  return v < m ? v : (v << 1) - m + aom_read_bit(r);
+  const int v = aom_read_literal(r, l - 1, ACCT_STR_NAME);
+  return v < m ? v : (v << 1) - m + aom_read_bit(r, ACCT_STR_NAME);
 }
 
 static int inv_remap_prob(int v, int m) {
@@ -62,16 +67,19 @@
   }
 }
 
-static int decode_term_subexp(aom_reader *r) {
-  if (!aom_read_bit(r)) return aom_read_literal(r, 4);
-  if (!aom_read_bit(r)) return aom_read_literal(r, 4) + 16;
-  if (!aom_read_bit(r)) return aom_read_literal(r, 5) + 32;
-  return decode_uniform(r) + 64;
+static int decode_term_subexp_(aom_reader *r ACCT_STR_PARAM) {
+  if (!aom_read_bit(r, ACCT_STR_NAME))
+    return aom_read_literal(r, 4, ACCT_STR_NAME);
+  if (!aom_read_bit(r, ACCT_STR_NAME))
+    return aom_read_literal(r, 4, ACCT_STR_NAME) + 16;
+  if (!aom_read_bit(r, ACCT_STR_NAME))
+    return aom_read_literal(r, 5, ACCT_STR_NAME) + 32;
+  return decode_uniform(r, ACCT_STR_NAME) + 64;
 }
 
-void av1_diff_update_prob(aom_reader *r, aom_prob *p) {
-  if (aom_read(r, DIFF_UPDATE_PROB)) {
-    const int delp = decode_term_subexp(r);
+void av1_diff_update_prob_(aom_reader *r, aom_prob *p ACCT_STR_PARAM) {
+  if (aom_read(r, DIFF_UPDATE_PROB, ACCT_STR_NAME)) {
+    const int delp = decode_term_subexp(r, ACCT_STR_NAME);
     *p = (aom_prob)inv_remap_prob(delp, *p);
   }
 }
diff --git a/av1/decoder/dsubexp.h b/av1/decoder/dsubexp.h
index acc38ba..1ddc27a 100644
--- a/av1/decoder/dsubexp.h
+++ b/av1/decoder/dsubexp.h
@@ -18,7 +18,13 @@
 extern "C" {
 #endif
 
-void av1_diff_update_prob(aom_reader *r, aom_prob *p);
+#if CONFIG_ACCOUNTING
+#define av1_diff_update_prob(r, p, str) av1_diff_update_prob_(r, p, str)
+#else
+#define av1_diff_update_prob(r, p, str) av1_diff_update_prob_(r, p)
+#endif
+
+void av1_diff_update_prob_(aom_reader *r, aom_prob *p ACCT_STR_PARAM);
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/configure b/configure
index a679c26..9e1e05b 100755
--- a/configure
+++ b/configure
@@ -53,6 +53,7 @@
   ${toggle_temporal_denoising}    enable temporal denoising and disable the spatial denoiser
   ${toggle_webm_io}               enable input from and output to WebM container
   ${toggle_libyuv}                enable libyuv
+  ${toggle_accounting}            enable bit accounting
 
 Codecs:
   Codecs can be selectively enabled or disabled individually, or by family:
@@ -310,6 +311,7 @@
     unit_tests
     webm_io
     libyuv
+    accounting
     decode_perf_tests
     encode_perf_tests
     multi_res_encoding
@@ -366,6 +368,7 @@
     unit_tests
     webm_io
     libyuv
+    accounting
     decode_perf_tests
     encode_perf_tests
     multi_res_encoding
diff --git a/test/accounting_test.cc b/test/accounting_test.cc
new file mode 100644
index 0000000..122f9b8
--- /dev/null
+++ b/test/accounting_test.cc
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+*/
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "third_party/googletest/src/include/gtest/gtest.h"
+
+#include "test/acm_random.h"
+#include "aom/aom_integer.h"
+#include "aom_dsp/bitreader.h"
+#include "aom_dsp/bitwriter.h"
+
+using libaom_test::ACMRandom;
+
+TEST(AV1, TestAccounting) {
+  const int kBufferSize = 10000;
+  const int kSymbols = 1024;
+  aom_writer bw;
+  uint8_t bw_buffer[kBufferSize];
+  aom_start_encode(&bw, bw_buffer);
+  for (int i = 0; i < kSymbols; i++) {
+    aom_write(&bw, 0, 32);
+    aom_write(&bw, 0, 32);
+    aom_write(&bw, 0, 32);
+  }
+  aom_stop_encode(&bw);
+  aom_reader br;
+  aom_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
+
+  Accounting accounting;
+  aom_accounting_init(&accounting);
+  br.accounting = &accounting;
+  for (int i = 0; i < kSymbols; i++) {
+    aom_read(&br, 32, "A");
+  }
+  // Consecutive symbols that are the same are coalesced.
+  GTEST_ASSERT_EQ(accounting.syms.num_syms, 1);
+  GTEST_ASSERT_EQ(accounting.syms.syms[0].samples, (unsigned int)kSymbols);
+
+  aom_accounting_reset(&accounting);
+  GTEST_ASSERT_EQ(accounting.syms.num_syms, 0);
+
+  // Should record 2 * kSymbols accounting symbols.
+  aom_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
+  br.accounting = &accounting;
+  for (int i = 0; i < kSymbols; i++) {
+    aom_read(&br, 32, "A");
+    aom_read(&br, 32, "B");
+    aom_read(&br, 32, "B");
+  }
+  GTEST_ASSERT_EQ(accounting.syms.num_syms, kSymbols * 2);
+  uint32_t tell_frac = aom_reader_tell_frac(&br);
+  for (int i = 0; i < accounting.syms.num_syms; i++) {
+    tell_frac -= accounting.syms.syms[i].bits;
+  }
+  GTEST_ASSERT_EQ(tell_frac, 0U);
+
+  GTEST_ASSERT_EQ(aom_accounting_dictionary_lookup(&accounting, "A"),
+                  aom_accounting_dictionary_lookup(&accounting, "A"));
+
+  // Check for collisions. The current aom_accounting_hash function returns
+  // the same hash code for AB and BA.
+  GTEST_ASSERT_NE(aom_accounting_dictionary_lookup(&accounting, "AB"),
+                  aom_accounting_dictionary_lookup(&accounting, "BA"));
+}
diff --git a/test/boolcoder_test.cc b/test/boolcoder_test.cc
index 2217226..4418bd3 100644
--- a/test/boolcoder_test.cc
+++ b/test/boolcoder_test.cc
@@ -89,7 +89,7 @@
           } else if (bit_method == 3) {
             bit = bit_rnd(2);
           }
-          GTEST_ASSERT_EQ(aom_read(&br, probas[i]), bit)
+          GTEST_ASSERT_EQ(aom_read(&br, probas[i], NULL), bit)
               << "pos: " << i << " / " << kBitsToTest
               << " bit_method: " << bit_method << " method: " << method;
         }
@@ -125,7 +125,7 @@
     GTEST_ASSERT_GE(aom_reader_tell(&br), 0);
     GTEST_ASSERT_LE(aom_reader_tell(&br), 1);
     for (int i = 0; i < kSymbols; i++) {
-      aom_read(&br, p);
+      aom_read(&br, p, NULL);
       ptrdiff_t tell = aom_reader_tell(&br);
       ptrdiff_t tell_frac = aom_reader_tell_frac(&br);
       GTEST_ASSERT_GE(tell, last_tell) << "tell: " << tell
diff --git a/test/test.mk b/test/test.mk
index 3575810..1be0bc5 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -95,6 +95,9 @@
 LIBAOM_TEST_SRCS-yes                   += ans_test.cc
 else
 LIBAOM_TEST_SRCS-yes                   += boolcoder_test.cc
+ifeq ($(CONFIG_ACCOUNTING),yes)
+LIBAOM_TEST_SRCS-yes                   += accounting_test.cc
+endif
 endif
 LIBAOM_TEST_SRCS-yes                   += divu_small_test.cc
 LIBAOM_TEST_SRCS-yes                   += encoder_parms_get_to_decoder.cc