Remove special ANS initialization handling

Change-Id: I5d5a93b27a42d591ff41ff23c007fc2954d2f9a7
diff --git a/aom_dsp/bitwriter.h b/aom_dsp/bitwriter.h
index 68bc1c8..99f58ae 100644
--- a/aom_dsp/bitwriter.h
+++ b/aom_dsp/bitwriter.h
@@ -62,9 +62,8 @@
 
 static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
 #if CONFIG_ANS
-  (void)bc;
-  (void)buffer;
-  assert(0 && "buf_ans requires a more complicated startup procedure");
+  aom_buf_ans_alloc(bc, /* error context*/ NULL);
+  buf_ans_write_init(bc, buffer);
 #else
   aom_daala_start_encode(bc, buffer);
 #endif
@@ -72,8 +71,8 @@
 
 static INLINE void aom_stop_encode(aom_writer *bc) {
 #if CONFIG_ANS
-  (void)bc;
-  assert(0 && "buf_ans requires a more complicated shutdown procedure");
+  aom_buf_ans_flush(bc);
+  bc->pos = buf_ans_write_end(bc);
 #else
   aom_daala_stop_encode(bc);
 #endif
diff --git a/aom_dsp/buf_ans.c b/aom_dsp/buf_ans.c
index 8fe1ff7..f7703df 100644
--- a/aom_dsp/buf_ans.c
+++ b/aom_dsp/buf_ans.c
@@ -16,9 +16,8 @@
 #include "aom/internal/aom_codec_internal.h"
 
 void aom_buf_ans_alloc(struct BufAnsCoder *c,
-                       struct aom_internal_error_info *error, int size) {
+                       struct aom_internal_error_info *error) {
   c->error = error;
-  c->size = size;
   assert(c->size > 1);
   AOM_CHECK_MEM_ERROR(error, c->buf, aom_malloc(c->size * sizeof(*c->buf)));
   // Initialize to overfull to trigger the assert in write.
diff --git a/aom_dsp/buf_ans.h b/aom_dsp/buf_ans.h
index 0768506..f84ff3a 100644
--- a/aom_dsp/buf_ans.h
+++ b/aom_dsp/buf_ans.h
@@ -46,6 +46,7 @@
 #if ANS_MAX_SYMBOLS
   int window_size;
 #endif
+  int pos;  // Dummy variable to store the output buffer after closing
 };
 
 // Allocate a buffered ANS coder to store size symbols.
@@ -54,7 +55,7 @@
 // When ANS_MAX_SYMBOLS is turned off, size is merely an initial hint and the
 // buffer will grow on demand
 void aom_buf_ans_alloc(struct BufAnsCoder *c,
-                       struct aom_internal_error_info *error, int hint);
+                       struct aom_internal_error_info *error);
 
 void aom_buf_ans_free(struct BufAnsCoder *c);
 
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index c86ef3a..4235c49 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -40,9 +40,6 @@
 #include "av1/common/seg_common.h"
 #include "av1/common/tile_common.h"
 
-#if CONFIG_ANS
-#include "aom_dsp/buf_ans.h"
-#endif  // CONFIG_ANS
 #if CONFIG_LV_MAP
 #include "av1/encoder/encodetxb.h"
 #endif  // CONFIG_LV_MAP
@@ -3780,11 +3777,7 @@
                             unsigned int *max_tile_size,
                             unsigned int *max_tile_col_size) {
   const AV1_COMMON *const cm = &cpi->common;
-#if CONFIG_ANS
-  struct BufAnsCoder *buf_ans = &cpi->buf_ans;
-#else
   aom_writer mode_bc;
-#endif  // CONFIG_ANS
   int tile_row, tile_col;
   TOKENEXTRA *(*const tok_buffers)[MAX_TILE_COLS] = cpi->tile_tok;
   TileBufferEnc(*const tile_buffers)[MAX_TILE_COLS] = cpi->tile_buffers;
@@ -3857,19 +3850,14 @@
         cpi->td.mb.pvq_q = &this_tile->pvq_q;
         cpi->td.mb.daala_enc.state.adapt = &this_tile->tctx.pvq_context;
 #endif  // CONFIG_PVQ
-#if !CONFIG_ANS
+#if CONFIG_ANS
+        mode_bc.size = 1 << cpi->common.ans_window_size_log2;
+#endif
         aom_start_encode(&mode_bc, buf->data + data_offset);
         write_modes(cpi, &tile_info, &mode_bc, &tok, tok_end);
         assert(tok == tok_end);
         aom_stop_encode(&mode_bc);
         tile_size = mode_bc.pos;
-#else
-        buf_ans_write_init(buf_ans, buf->data + data_offset);
-        write_modes(cpi, &tile_info, buf_ans, &tok, tok_end);
-        assert(tok == tok_end);
-        aom_buf_ans_flush(buf_ans);
-        tile_size = buf_ans_write_end(buf_ans);
-#endif  // !CONFIG_ANS
 #if CONFIG_PVQ
         cpi->td.mb.pvq_q = NULL;
 #endif
@@ -4033,22 +4021,17 @@
         cpi->td.mb.daala_enc.state.adapt = &this_tile->tctx.pvq_context;
 #endif  // CONFIG_PVQ
 #if CONFIG_ANS
-        buf_ans_write_init(buf_ans, dst + total_size);
-        write_modes(cpi, &tile_info, buf_ans, &tok, tok_end);
-        assert(tok == tok_end);
-        aom_buf_ans_flush(buf_ans);
-        tile_size = buf_ans_write_end(buf_ans);
-#else
-      aom_start_encode(&mode_bc, dst + total_size);
-      write_modes(cpi, &tile_info, &mode_bc, &tok, tok_end);
+        mode_bc.size = 1 << cpi->common.ans_window_size_log2;
+#endif  // CONFIG_ANS
+        aom_start_encode(&mode_bc, dst + total_size);
+        write_modes(cpi, &tile_info, &mode_bc, &tok, tok_end);
 #if !CONFIG_LV_MAP
 #if !CONFIG_PVQ
-      assert(tok == tok_end);
+        assert(tok == tok_end);
 #endif  // !CONFIG_PVQ
 #endif  // !CONFIG_LV_MAP
-      aom_stop_encode(&mode_bc);
-      tile_size = mode_bc.pos;
-#endif  // CONFIG_ANS
+        aom_stop_encode(&mode_bc);
+        tile_size = mode_bc.pos;
 #if CONFIG_PVQ
         cpi->td.mb.pvq_q = NULL;
 #endif
@@ -4635,15 +4618,12 @@
   (void)i;
   (void)fc;
 
-#if CONFIG_ANS
-  int header_size;
-  header_bc = &cpi->buf_ans;
-  buf_ans_write_init(header_bc, data);
-#else
   aom_writer real_header_bc;
   header_bc = &real_header_bc;
-  aom_start_encode(header_bc, data);
+#if CONFIG_ANS
+  header_bc->size = 1 << cpi->common.ans_window_size_log2;
 #endif
+  aom_start_encode(header_bc, data);
 
 #if CONFIG_LOOP_RESTORATION
   encode_restoration(cm, header_bc);
@@ -4788,16 +4768,9 @@
     write_global_motion(cpi, header_bc);
 #endif  // CONFIG_GLOBAL_MOTION
   }
-#if CONFIG_ANS
-  aom_buf_ans_flush(header_bc);
-  header_size = buf_ans_write_end(header_bc);
-  assert(header_size <= 0xffff);
-  return header_size;
-#else
   aom_stop_encode(header_bc);
   assert(header_bc->pos <= 0xffff);
   return header_bc->pos;
-#endif  // CONFIG_ANS
 }
 
 static int choose_size_bytes(uint32_t size, int spare_msbs) {
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index a9fac7e..6ddefe2 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -835,9 +835,6 @@
     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
     CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
                     aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
-#if CONFIG_ANS && !ANS_MAX_SYMBOLS
-    aom_buf_ans_alloc(&cpi->buf_ans, &cm->error, (int)tokens);
-#endif  // CONFIG_ANS
   }
 
   av1_setup_pc_tree(&cpi->common, &cpi->td);
@@ -2390,14 +2387,8 @@
 #if CONFIG_HIGHBITDEPTH
   highbd_set_var_fns(cpi);
 #endif
-
 #if CONFIG_ANS && ANS_MAX_SYMBOLS
   cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
-  if (cpi->buf_ans.size != (1 << cpi->common.ans_window_size_log2)) {
-    aom_buf_ans_free(&cpi->buf_ans);
-    aom_buf_ans_alloc(&cpi->buf_ans, &cpi->common.error,
-                      1 << cpi->common.ans_window_size_log2);
-  }
 #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
 }
 
diff --git a/test/ans_test.cc b/test/ans_test.cc
index a553a9e..fd460f4 100644
--- a/test/ans_test.cc
+++ b/test/ans_test.cc
@@ -51,7 +51,8 @@
 
 bool check_rabs(const PvVec &pv_vec, uint8_t *buf) {
   BufAnsCoder a;
-  aom_buf_ans_alloc(&a, NULL, kBufAnsSize);
+  a.size = kBufAnsSize;
+  aom_buf_ans_alloc(&a, NULL);
   buf_ans_write_init(&a, buf);
 
   std::clock_t start = std::clock();
@@ -125,7 +126,8 @@
 bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab,
                 uint8_t *buf) {
   BufAnsCoder a;
-  aom_buf_ans_alloc(&a, NULL, kBufAnsSize);
+  a.size = kBufAnsSize;
+  aom_buf_ans_alloc(&a, NULL);
   buf_ans_write_init(&a, buf);
   aom_cdf_prob dec_tab[kRansSymbols];
   rans_build_dec_tab(tab, dec_tab);