ans: Give buf_ans ownership of the AnsCoder

Change-Id: I509bbba0d84c1d378044e2c612dd48cd8f99848d
diff --git a/aom_dsp/buf_ans.h b/aom_dsp/buf_ans.h
index e499200..0a1de1a 100644
--- a/aom_dsp/buf_ans.h
+++ b/aom_dsp/buf_ans.h
@@ -39,8 +39,10 @@
 struct BufAnsCoder {
   struct aom_internal_error_info *error;
   struct buffered_ans_symbol *buf;
+  struct AnsCoder ans;
   int size;
   int offset;
+  int output_bytes;
 };
 
 void aom_buf_ans_alloc(struct BufAnsCoder *c,
@@ -50,8 +52,11 @@
 
 void aom_buf_ans_grow(struct BufAnsCoder *c);
 
-static INLINE void buf_ans_write_reset(struct BufAnsCoder *const c) {
+static INLINE void buf_ans_write_init(struct BufAnsCoder *const c,
+                                      uint8_t *const output_buffer) {
   c->offset = 0;
+  c->output_bytes = 0;
+  ans_write_init(&c->ans, output_buffer);
 }
 
 static INLINE void buf_uabs_write(struct BufAnsCoder *const c, uint8_t val,
@@ -78,20 +83,21 @@
   ++c->offset;
 }
 
-static INLINE void buf_ans_flush(const struct BufAnsCoder *const c,
-                                 struct AnsCoder *ans) {
+static INLINE void buf_ans_flush(struct BufAnsCoder *const c) {
   int offset;
   for (offset = c->offset - 1; offset >= 0; --offset) {
     if (c->buf[offset].method == ANS_METHOD_RANS) {
       struct rans_sym sym;
       sym.prob = c->buf[offset].prob;
       sym.cum_prob = c->buf[offset].val_start;
-      rans_write(ans, &sym);
+      rans_write(&c->ans, &sym);
     } else {
-      uabs_write(ans, (uint8_t)c->buf[offset].val_start,
+      uabs_write(&c->ans, (uint8_t)c->buf[offset].val_start,
                  (AnsP8)c->buf[offset].prob);
     }
   }
+  c->offset = 0;
+  c->output_bytes += ans_write_end(&c->ans);
 }
 
 static INLINE void buf_uabs_write_bit(struct BufAnsCoder *c, int bit) {
@@ -106,6 +112,11 @@
   for (bit = bits - 1; bit >= 0; bit--)
     buf_uabs_write_bit(c, 1 & (literal >> bit));
 }
+
+static INLINE int buf_ans_write_end(struct BufAnsCoder *const c) {
+  assert(c->offset == 0);
+  return c->output_bytes;
+}
 #ifdef __cplusplus
 }  // extern "C"
 #endif  // __cplusplus