Use boolcoder API instead of inlining

This patch changes the token packing to call the bool encoder API rather
than inlining it into the token packing function, and similarly removes
a special get_signed case from the detokenizer. This allows easier
experimentation with changing the bool coder as a whole.

Change-Id: I52c3625bbe4960b68cfb873b0e39ade0c82f9e91
diff --git a/vp9/decoder/vp9_detokenize.c b/vp9/decoder/vp9_detokenize.c
index 8fe2184..55ce017 100644
--- a/vp9/decoder/vp9_detokenize.c
+++ b/vp9/decoder/vp9_detokenize.c
@@ -60,26 +60,7 @@
 DECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]);
 
 static int get_signed(BOOL_DECODER *br, int value_to_sign) {
-  const int split = (br->range + 1) >> 1;
-  const VP9_BD_VALUE bigsplit = (VP9_BD_VALUE)split << (VP9_BD_VALUE_SIZE - 8);
-  int v;
-
-  if (br->count < 0)
-    vp9_bool_decoder_fill(br);
-
-  if (br->value < bigsplit) {
-    br->range = split;
-    v = value_to_sign;
-  } else {
-    br->range = br->range - split;
-    br->value = br->value - bigsplit;
-    v = -value_to_sign;
-  }
-  br->range += br->range;
-  br->value += br->value;
-  --br->count;
-
-  return v;
+  return decode_bool(br, 128) ? -value_to_sign : value_to_sign;
 }
 
 #define INCREMENT_COUNT(token)                     \
diff --git a/vp9/encoder/vp9_bitstream.c b/vp9/encoder/vp9_bitstream.c
index 498f647..54d44fa 100644
--- a/vp9/encoder/vp9_bitstream.c
+++ b/vp9/encoder/vp9_bitstream.c
@@ -364,11 +364,6 @@
 static void pack_mb_tokens(vp9_writer* const bc,
                            TOKENEXTRA **tp,
                            const TOKENEXTRA *const stop) {
-  unsigned int split;
-  unsigned int shift;
-  int count = bc->count;
-  unsigned int range = bc->range;
-  unsigned int lowvalue = bc->lowvalue;
   TOKENEXTRA *p = *tp;
 
   while (p < stop) {
@@ -394,42 +389,8 @@
 
     do {
       const int bb = (v >> --n) & 1;
-      split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
+      encode_bool(bc, bb, pp[i >> 1]);
       i = vp9_coef_tree[i + bb];
-
-      if (bb) {
-        lowvalue += split;
-        range = range - split;
-      } else {
-        range = split;
-      }
-
-      shift = vp9_norm[range];
-      range <<= shift;
-      count += shift;
-
-      if (count >= 0) {
-        int offset = shift - count;
-
-        if ((lowvalue << (offset - 1)) & 0x80000000) {
-          int x = bc->pos - 1;
-
-          while (x >= 0 && bc->buffer[x] == 0xff) {
-            bc->buffer[x] = (unsigned char)0;
-            x--;
-          }
-
-          bc->buffer[x] += 1;
-        }
-
-        bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
-        lowvalue <<= offset;
-        shift = count;
-        lowvalue &= 0xffffff;
-        count -= 8;
-      }
-
-      lowvalue <<= shift;
     } while (n);
 
 
@@ -444,87 +405,16 @@
 
         do {
           const int bb = (v >> --n) & 1;
-          split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
+          encode_bool(bc, bb, pp[i >> 1]);
           i = b->tree[i + bb];
-
-          if (bb) {
-            lowvalue += split;
-            range = range - split;
-          } else {
-            range = split;
-          }
-
-          shift = vp9_norm[range];
-          range <<= shift;
-          count += shift;
-
-          if (count >= 0) {
-            int offset = shift - count;
-
-            if ((lowvalue << (offset - 1)) & 0x80000000) {
-              int x = bc->pos - 1;
-
-              while (x >= 0 && bc->buffer[x] == 0xff) {
-                bc->buffer[x] = (unsigned char)0;
-                x--;
-              }
-
-              bc->buffer[x] += 1;
-            }
-
-            bc->buffer[bc->pos++] = (lowvalue >> (24 - offset));
-            lowvalue <<= offset;
-            shift = count;
-            lowvalue &= 0xffffff;
-            count -= 8;
-          }
-
-          lowvalue <<= shift;
         } while (n);
       }
 
-
-      {
-
-        split = (range + 1) >> 1;
-
-        if (e & 1) {
-          lowvalue += split;
-          range = range - split;
-        } else {
-          range = split;
-        }
-
-        range <<= 1;
-
-        if ((lowvalue & 0x80000000)) {
-          int x = bc->pos - 1;
-
-          while (x >= 0 && bc->buffer[x] == 0xff) {
-            bc->buffer[x] = (unsigned char)0;
-            x--;
-          }
-
-          bc->buffer[x] += 1;
-
-        }
-
-        lowvalue  <<= 1;
-
-        if (!++count) {
-          count = -8;
-          bc->buffer[bc->pos++] = (lowvalue >> 24);
-          lowvalue &= 0xffffff;
-        }
-      }
-
+      encode_bool(bc, e & 1, 128);
     }
     ++p;
   }
 
-  bc->count = count;
-  bc->lowvalue = lowvalue;
-  bc->range = range;
   *tp = p;
 }