|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
# define BS_SUFFIX_LOWER _le |
|
# define BS_SUFFIX_UPPER LE |
|
#else |
|
# define BS_SUFFIX_LOWER _be |
|
# define BS_SUFFIX_UPPER BE |
|
#endif |
|
|
|
#define BS_JOIN(x, y, z) x ## y ## z |
|
#define BS_JOIN3(x, y, z) BS_JOIN(x, y, z) |
|
#define BS_FUNC(x) BS_JOIN3(bits_, x, BS_SUFFIX_LOWER) |
|
|
|
#define BSCTX BS_JOIN3(Bitstream, Context, BS_SUFFIX_UPPER) |
|
|
|
typedef struct BSCTX { |
|
uint64_t bits; |
|
const uint8_t *buffer, *buffer_end; |
|
const uint8_t *ptr; |
|
unsigned bits_valid; |
|
unsigned size_in_bits; |
|
} BSCTX; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(priv_refill_64)(BSCTX *bc) |
|
{ |
|
#if !UNCHECKED_BITSTREAM_READER |
|
if (bc->ptr >= bc->buffer_end) |
|
return -1; |
|
#endif |
|
|
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
bc->bits = AV_RL64(bc->ptr); |
|
#else |
|
bc->bits = AV_RB64(bc->ptr); |
|
#endif |
|
bc->ptr += 8; |
|
bc->bits_valid = 64; |
|
|
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(priv_refill_32)(BSCTX *bc) |
|
{ |
|
#if !UNCHECKED_BITSTREAM_READER |
|
if (bc->ptr >= bc->buffer_end) |
|
return -1; |
|
#endif |
|
|
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
bc->bits |= (uint64_t)AV_RL32(bc->ptr) << bc->bits_valid; |
|
#else |
|
bc->bits |= (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_valid); |
|
#endif |
|
bc->ptr += 4; |
|
bc->bits_valid += 32; |
|
|
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(init)(BSCTX *bc, const uint8_t *buffer, |
|
unsigned int bit_size) |
|
{ |
|
unsigned int buffer_size; |
|
|
|
if (bit_size > INT_MAX - 7 || !buffer) { |
|
bc->buffer = NULL; |
|
bc->ptr = NULL; |
|
bc->bits_valid = 0; |
|
return AVERROR_INVALIDDATA; |
|
} |
|
|
|
buffer_size = (bit_size + 7) >> 3; |
|
|
|
bc->buffer = buffer; |
|
bc->buffer_end = buffer + buffer_size; |
|
bc->ptr = bc->buffer; |
|
bc->size_in_bits = bit_size; |
|
bc->bits_valid = 0; |
|
bc->bits = 0; |
|
|
|
BS_FUNC(priv_refill_64)(bc); |
|
|
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(init8)(BSCTX *bc, const uint8_t *buffer, |
|
unsigned int byte_size) |
|
{ |
|
if (byte_size > INT_MAX / 8) |
|
return AVERROR_INVALIDDATA; |
|
return BS_FUNC(init)(bc, buffer, byte_size * 8); |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(tell)(const BSCTX *bc) |
|
{ |
|
return (bc->ptr - bc->buffer) * 8 - bc->bits_valid; |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(size)(const BSCTX *bc) |
|
{ |
|
return bc->size_in_bits; |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(left)(const BSCTX *bc) |
|
{ |
|
return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_valid; |
|
} |
|
|
|
static inline uint64_t BS_FUNC(priv_val_show)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n > 0 && n <= 64); |
|
|
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
return bc->bits & (UINT64_MAX >> (64 - n)); |
|
#else |
|
return bc->bits >> (64 - n); |
|
#endif |
|
} |
|
|
|
static inline void BS_FUNC(priv_skip_remaining)(BSCTX *bc, unsigned int n) |
|
{ |
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
bc->bits >>= n; |
|
#else |
|
bc->bits <<= n; |
|
#endif |
|
bc->bits_valid -= n; |
|
} |
|
|
|
static inline uint64_t BS_FUNC(priv_val_get)(BSCTX *bc, unsigned int n) |
|
{ |
|
uint64_t ret; |
|
|
|
av_assert2(n > 0 && n < 64); |
|
|
|
ret = BS_FUNC(priv_val_show)(bc, n); |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
|
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
static inline unsigned int BS_FUNC(read_bit)(BSCTX *bc) |
|
{ |
|
if (!bc->bits_valid && BS_FUNC(priv_refill_64)(bc) < 0) |
|
return 0; |
|
|
|
return BS_FUNC(priv_val_get)(bc, 1); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t BS_FUNC(read_nz)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n > 0 && n <= 32); |
|
|
|
if (n > bc->bits_valid) { |
|
if (BS_FUNC(priv_refill_32)(bc) < 0) |
|
bc->bits_valid = n; |
|
} |
|
|
|
return BS_FUNC(priv_val_get)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
static inline uint32_t BS_FUNC(read)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n <= 32); |
|
|
|
if (!n) |
|
return 0; |
|
|
|
return BS_FUNC(read_nz)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
static inline uint64_t BS_FUNC(read_63)(BSCTX *bc, unsigned int n) |
|
{ |
|
uint64_t ret = 0; |
|
unsigned left = 0; |
|
|
|
av_assert2(n <= 63); |
|
|
|
if (!n) |
|
return 0; |
|
|
|
if (n > bc->bits_valid) { |
|
left = bc->bits_valid; |
|
n -= left; |
|
|
|
if (left) |
|
ret = BS_FUNC(priv_val_get)(bc, left); |
|
|
|
if (BS_FUNC(priv_refill_64)(bc) < 0) |
|
bc->bits_valid = n; |
|
|
|
} |
|
|
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
ret = BS_FUNC(priv_val_get)(bc, n) << left | ret; |
|
#else |
|
ret = BS_FUNC(priv_val_get)(bc, n) | ret << n; |
|
#endif |
|
|
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
static inline uint64_t BS_FUNC(read_64)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n <= 64); |
|
|
|
if (n == 64) { |
|
uint64_t ret = BS_FUNC(read_63)(bc, 63); |
|
#ifdef BITSTREAM_TEMPLATE_LE |
|
return ret | ((uint64_t)BS_FUNC(read_bit)(bc) << 63); |
|
#else |
|
return (ret << 1) | (uint64_t)BS_FUNC(read_bit)(bc); |
|
#endif |
|
} |
|
return BS_FUNC(read_63)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int32_t BS_FUNC(read_signed_nz)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n > 0 && n <= 32); |
|
return sign_extend(BS_FUNC(read_nz)(bc, n), n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline int32_t BS_FUNC(read_signed)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n <= 32); |
|
|
|
if (!n) |
|
return 0; |
|
|
|
return BS_FUNC(read_signed_nz)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t BS_FUNC(peek_nz)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n > 0 && n <= 32); |
|
|
|
if (n > bc->bits_valid) |
|
BS_FUNC(priv_refill_32)(bc); |
|
|
|
return BS_FUNC(priv_val_show)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline uint32_t BS_FUNC(peek)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n <= 32); |
|
|
|
if (!n) |
|
return 0; |
|
|
|
return BS_FUNC(peek_nz)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(peek_signed_nz)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n > 0 && n <= 32); |
|
return sign_extend(BS_FUNC(peek_nz)(bc, n), n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(peek_signed)(BSCTX *bc, unsigned int n) |
|
{ |
|
av_assert2(n <= 32); |
|
|
|
if (!n) |
|
return 0; |
|
|
|
return BS_FUNC(peek_signed_nz)(bc, n); |
|
} |
|
|
|
|
|
|
|
|
|
static inline void BS_FUNC(skip)(BSCTX *bc, unsigned int n) |
|
{ |
|
if (n < bc->bits_valid) |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
else { |
|
n -= bc->bits_valid; |
|
bc->bits = 0; |
|
bc->bits_valid = 0; |
|
|
|
if (n >= 64) { |
|
unsigned int skip = n / 8; |
|
|
|
n -= skip * 8; |
|
bc->ptr += skip; |
|
} |
|
BS_FUNC(priv_refill_64)(bc); |
|
if (n) |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
static inline void BS_FUNC(seek)(BSCTX *bc, unsigned pos) |
|
{ |
|
bc->ptr = bc->buffer; |
|
bc->bits = 0; |
|
bc->bits_valid = 0; |
|
|
|
BS_FUNC(skip)(bc, pos); |
|
} |
|
|
|
|
|
|
|
|
|
static inline const uint8_t *BS_FUNC(align)(BSCTX *bc) |
|
{ |
|
unsigned int n = -BS_FUNC(tell)(bc) & 7; |
|
if (n) |
|
BS_FUNC(skip)(bc, n); |
|
return bc->buffer + (BS_FUNC(tell)(bc) >> 3); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(read_xbits)(BSCTX *bc, unsigned int n) |
|
{ |
|
int32_t cache = BS_FUNC(peek)(bc, 32); |
|
int sign = ~cache >> 31; |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
|
|
return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign; |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(decode012)(BSCTX *bc) |
|
{ |
|
if (!BS_FUNC(read_bit)(bc)) |
|
return 0; |
|
else |
|
return BS_FUNC(read_bit)(bc) + 1; |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(decode210)(BSCTX *bc) |
|
{ |
|
if (BS_FUNC(read_bit)(bc)) |
|
return 0; |
|
else |
|
return 2 - BS_FUNC(read_bit)(bc); |
|
} |
|
|
|
|
|
static inline int BS_FUNC(apply_sign)(BSCTX *bc, int val) |
|
{ |
|
int sign = BS_FUNC(read_signed)(bc, 1); |
|
return (val ^ sign) - sign; |
|
} |
|
|
|
static inline int BS_FUNC(skip_1stop_8data)(BSCTX *s) |
|
{ |
|
if (BS_FUNC(left)(s) <= 0) |
|
return AVERROR_INVALIDDATA; |
|
|
|
while (BS_FUNC(read_bit)(s)) { |
|
BS_FUNC(skip)(s, 8); |
|
if (BS_FUNC(left)(s) <= 0) |
|
return AVERROR_INVALIDDATA; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(priv_set_idx)(BSCTX *bc, int code, int *n, |
|
int *nb_bits, const VLCElem *table) |
|
{ |
|
unsigned idx; |
|
|
|
*nb_bits = -*n; |
|
idx = BS_FUNC(peek)(bc, *nb_bits) + code; |
|
*n = table[idx].len; |
|
|
|
return table[idx].sym; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int BS_FUNC(read_vlc)(BSCTX *bc, const VLCElem *table, |
|
int bits, int max_depth) |
|
{ |
|
int nb_bits; |
|
unsigned idx = BS_FUNC(peek)(bc, bits); |
|
int code = table[idx].sym; |
|
int n = table[idx].len; |
|
|
|
if (max_depth > 1 && n < 0) { |
|
BS_FUNC(priv_skip_remaining)(bc, bits); |
|
code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); |
|
if (max_depth > 2 && n < 0) { |
|
BS_FUNC(priv_skip_remaining)(bc, nb_bits); |
|
code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); |
|
} |
|
} |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
|
|
return code; |
|
} |
|
|
|
static inline int BS_FUNC(read_vlc_multi)(BSCTX *bc, uint8_t *dst, |
|
const VLC_MULTI_ELEM *const Jtable, |
|
const VLCElem *const table, |
|
const int bits, const int max_depth) |
|
{ |
|
unsigned idx = BS_FUNC(peek)(bc, bits); |
|
int ret, nb_bits, code, n = Jtable[idx].len; |
|
if (Jtable[idx].num) { |
|
AV_COPY64U(dst, Jtable[idx].val); |
|
ret = Jtable[idx].num; |
|
} else { |
|
code = table[idx].sym; |
|
n = table[idx].len; |
|
if (max_depth > 1 && n < 0) { |
|
BS_FUNC(priv_skip_remaining)(bc, bits); |
|
code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); |
|
if (max_depth > 2 && n < 0) { |
|
BS_FUNC(priv_skip_remaining)(bc, nb_bits); |
|
code = BS_FUNC(priv_set_idx)(bc, code, &n, &nb_bits, table); |
|
} |
|
} |
|
AV_WN16(dst, code); |
|
ret = n > 0; |
|
} |
|
BS_FUNC(priv_skip_remaining)(bc, n); |
|
|
|
return ret; |
|
} |
|
|
|
#undef BSCTX |
|
#undef BS_FUNC |
|
#undef BS_JOIN3 |
|
#undef BS_JOIN |
|
#undef BS_SUFFIX_UPPER |
|
#undef BS_SUFFIX_LOWER |
|
|