} else if (bit_method == 3) {
bit = bit_rnd(2);
}
- GTEST_ASSERT_EQ(decode_bool(&br, probas[i]), bit)
+ GTEST_ASSERT_EQ(vp9_read(&br, probas[i]), bit)
<< "pos: " << i << " / " << bits_to_test
<< " bit_method: " << bit_method
<< " method: " << method;
#include "vp9/decoder/vp9_dboolhuff.h"
-int vp9_start_decode(BOOL_DECODER *br,
- const unsigned char *source,
- unsigned int source_sz) {
- br->user_buffer_end = source + source_sz;
- br->user_buffer = source;
+int vp9_start_decode(BOOL_DECODER *br, const uint8_t *buffer, size_t size) {
+ br->buffer_end = buffer + size;
+ br->buffer = buffer;
br->value = 0;
br->count = -8;
br->range = 255;
- if (source_sz && !source)
+ if (size && !buffer)
return 1;
- /* Populate the buffer */
- vp9_bool_decoder_fill(br);
-
+ vp9_reader_fill(br);
return 0;
}
-
-void vp9_bool_decoder_fill(BOOL_DECODER *br) {
- const unsigned char *bufptr = br->user_buffer;
- const unsigned char *bufend = br->user_buffer_end;
+void vp9_reader_fill(BOOL_DECODER *br) {
+ const uint8_t *const buffer_end = br->buffer_end;
+ const uint8_t *buffer = br->buffer;
VP9_BD_VALUE value = br->value;
int count = br->count;
int shift = VP9_BD_VALUE_SIZE - 8 - (count + 8);
int loop_end = 0;
- int bits_left = (int)((bufend - bufptr)*CHAR_BIT);
- int x = shift + CHAR_BIT - bits_left;
+ const int bits_left = (int)((buffer_end - buffer)*CHAR_BIT);
+ const int x = shift + CHAR_BIT - bits_left;
if (x >= 0) {
count += VP9_LOTS_OF_BITS;
if (x < 0 || bits_left) {
while (shift >= loop_end) {
count += CHAR_BIT;
- value |= (VP9_BD_VALUE)*bufptr++ << shift;
+ value |= (VP9_BD_VALUE)*buffer++ << shift;
shift -= CHAR_BIT;
}
}
- br->user_buffer = bufptr;
+ br->buffer = buffer;
br->value = value;
br->count = count;
}
-static int get_unsigned_bits(unsigned num_values) {
+static int get_unsigned_bits(unsigned int num_values) {
int cat = 0;
if (num_values <= 1)
return 0;
int vp9_decode_uniform(BOOL_DECODER *br, int n) {
int v;
- int l = get_unsigned_bits(n);
- int m = (1 << l) - n;
- if (!l) return 0;
- v = decode_value(br, l - 1);
- if (v < m)
- return v;
- else
- return (v << 1) - m + decode_value(br, 1);
+ const int l = get_unsigned_bits(n);
+ const int m = (1 << l) - n;
+ if (!l)
+ return 0;
+
+ v = vp9_read_literal(br, l - 1);
+ return v < m ? v : (v << 1) - m + vp9_read_bit(br);
}
int vp9_decode_term_subexp(BOOL_DECODER *br, int k, int num_syms) {
int i = 0, mk = 0, word;
while (1) {
- int b = (i ? k + i - 1 : k);
- int a = (1 << b);
+ const int b = i ? k + i - 1 : k;
+ const int a = 1 << b;
if (num_syms <= mk + 3 * a) {
word = vp9_decode_uniform(br, num_syms - mk) + mk;
break;
} else {
- if (decode_value(br, 1)) {
+ if (vp9_read_bit(br)) {
i++;
mk += a;
} else {
- word = decode_value(br, b) + mk;
+ word = vp9_read_literal(br, b) + mk;
break;
}
}
int data = 0, bit = 0, lmax = max;
while (lmax) {
- data |= decode_bool(br, 128) << bit++;
+ data |= vp9_read_bit(br) << bit++;
lmax >>= 1;
}
- if (data > max)
- return max;
- return data;
+ return data > max ? max : data;
}
typedef size_t VP9_BD_VALUE;
#define VP9_BD_VALUE_SIZE ((int)sizeof(VP9_BD_VALUE)*CHAR_BIT)
-/*This is meant to be a large, positive constant that can still be efficiently
- loaded as an immediate (on platforms like ARM, for example).
- Even relatively modest values like 100 would work fine.*/
-#define VP9_LOTS_OF_BITS (0x40000000)
+
+// This is meant to be a large, positive constant that can still be efficiently
+// loaded as an immediate (on platforms like ARM, for example).
+// Even relatively modest values like 100 would work fine.
+#define VP9_LOTS_OF_BITS 0x40000000
typedef struct {
- const unsigned char *user_buffer_end;
- const unsigned char *user_buffer;
- VP9_BD_VALUE value;
- int count;
- unsigned int range;
+ const uint8_t *buffer_end;
+ const uint8_t *buffer;
+ VP9_BD_VALUE value;
+ int count;
+ unsigned int range;
} BOOL_DECODER;
DECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]);
-int vp9_start_decode(BOOL_DECODER *br,
- const unsigned char *source,
- unsigned int source_sz);
+int vp9_start_decode(BOOL_DECODER *br, const uint8_t *buffer, size_t size);
-void vp9_bool_decoder_fill(BOOL_DECODER *br);
+void vp9_reader_fill(BOOL_DECODER *br);
int vp9_decode_uniform(BOOL_DECODER *br, int n);
int vp9_decode_term_subexp(BOOL_DECODER *br, int k, int num_syms);
int vp9_inv_recenter_nonneg(int v, int m);
-static int decode_bool(BOOL_DECODER *br, int probability) {
+static INLINE const uint8_t *vp9_reader_find_end(BOOL_DECODER *br) {
+ // Find the end of the coded buffer
+ while (br->count > CHAR_BIT && br->count < VP9_BD_VALUE_SIZE) {
+ br->count -= CHAR_BIT;
+ br->buffer--;
+ }
+ return br->buffer;
+}
+
+static int vp9_read(BOOL_DECODER *br, int probability) {
unsigned int bit = 0;
VP9_BD_VALUE value;
VP9_BD_VALUE bigsplit;
unsigned int split = 1 + (((br->range - 1) * probability) >> 8);
if (br->count < 0)
- vp9_bool_decoder_fill(br);
+ vp9_reader_fill(br);
value = br->value;
count = br->count;
return bit;
}
-static int decode_value(BOOL_DECODER *br, int bits) {
- int z = 0;
- int bit;
+static int vp9_read_bit(BOOL_DECODER *r) {
+ return vp9_read(r, 128); // vp9_prob_half
+}
+
+static int vp9_read_literal(BOOL_DECODER *br, int bits) {
+ int z = 0, bit;
for (bit = bits - 1; bit >= 0; bit--) {
- z |= decode_bool(br, 0x80) << bit;
+ z |= vp9_read_bit(br) << bit;
}
return z;
}
#endif
- // Find the end of the coded buffer
- while (residual_bc.count > CHAR_BIT &&
- residual_bc.count < VP9_BD_VALUE_SIZE) {
- residual_bc.count -= CHAR_BIT;
- residual_bc.user_buffer--;
- }
- *p_data_end = residual_bc.user_buffer;
+ *p_data_end = vp9_reader_find_end(&residual_bc);
return 0;
}
typedef BOOL_DECODER vp9_reader;
-#define vp9_read decode_bool
-#define vp9_read_literal decode_value
-#define vp9_read_bit(r) vp9_read(r, vp9_prob_half)
#define vp9_read_prob(r) ((vp9_prob)vp9_read_literal(r, 8))
#define vp9_read_and_apply_sign(r, value) (vp9_read_bit(r) ? -(value) : (value))