Commit 67d06006 authored by Dmitry Kovalev's avatar Dmitry Kovalev

Replacing vp9_read, vp9_read_literal, vp9_read_bit macros with functions.

This is the first CL with vp9_reader changes. All another macro
definitions will be replaced after.

Change-Id: I1c6bd9c9a612ec1663d484d6adb4fb720af54063
parent b800ec17
......@@ -77,7 +77,7 @@ TEST(VP9, TestBitIO) {
} 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;
......
......@@ -13,34 +13,29 @@
#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;
......@@ -50,18 +45,18 @@ void vp9_bool_decoder_fill(BOOL_DECODER *br) {
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;
......@@ -84,30 +79,29 @@ int vp9_inv_recenter_nonneg(int v, int m) {
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;
}
}
......@@ -119,10 +113,8 @@ int vp9_decode_unsigned_max(BOOL_DECODER *br, int max) {
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;
}
......@@ -21,32 +21,40 @@
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;
......@@ -55,7 +63,7 @@ static int decode_bool(BOOL_DECODER *br, int probability) {
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;
......@@ -83,12 +91,15 @@ static int decode_bool(BOOL_DECODER *br, int probability) {
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;
......
......@@ -1792,12 +1792,6 @@ int vp9_decode_frame(VP9D_COMP *pbi, const uint8_t **p_data_end) {
}
#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;
}
......@@ -17,9 +17,6 @@
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))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment