Commit b4e6377a authored by Vittorio Giovara's avatar Vittorio Giovara

get_bits: Factor out leb parsing to dav1d_get_uleb128()

parent c45f6b37
......@@ -79,6 +79,27 @@ int dav1d_get_sbits(GetBits *const c, const unsigned n) {
return res >> shift;
}
unsigned dav1d_get_uleb128(GetBits *c) {
unsigned val = 0, more, i = 0;
do {
more = dav1d_get_bits(c, 1);
unsigned bits = dav1d_get_bits(c, 7);
if (i <= 3 || (i == 4 && bits < (1 << 4)))
val |= bits << (i * 7);
else if (bits) {
c->error = 1;
return 0;
}
if (more && ++i == 8) {
c->error = 1;
return 0;
}
} while (more);
return val;
}
unsigned dav1d_get_uniform(GetBits *const c, const unsigned max) {
// Output in range [0..max-1]
// max must be > 1, or else nothing is read from the bitstream
......
......@@ -41,6 +41,7 @@ typedef struct GetBits {
void dav1d_init_get_bits(GetBits *c, const uint8_t *data, size_t sz);
unsigned dav1d_get_bits(GetBits *c, unsigned n);
int dav1d_get_sbits(GetBits *c, unsigned n);
unsigned dav1d_get_uleb128(GetBits *c);
// Output in range 0..max-1
unsigned dav1d_get_uniform(GetBits *c, unsigned max);
......
......@@ -1187,17 +1187,9 @@ int dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in, int global) {
}
// obu length field
unsigned len = 0, more, i = 0;
unsigned len = 0;
if (has_length_field)
do {
more = dav1d_get_bits(&gb, 1);
unsigned bits = dav1d_get_bits(&gb, 7);
if (i <= 3 || (i == 4 && bits < (1 << 4)))
len |= bits << (i * 7);
else if (bits)
goto error;
if (more && ++i == 8) goto error;
} while (more);
len = dav1d_get_uleb128(&gb);
else
len = (int) in->sz - 1 - has_extension;
if (gb.error) goto error;
......
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