27
27
#define AVCODEC_GET_BITS_H
29
29
#include <stdint.h>
32
#include "libavutil/bswap.h"
33
30
#include "libavutil/common.h"
34
31
#include "libavutil/intreadwrite.h"
35
32
#include "libavutil/log.h"
36
33
#include "mathops.h"
38
#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
39
# define ALT_BITSTREAM_READER
42
#if !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
43
# if ARCH_ARM && !HAVE_FAST_UNALIGNED
44
# define A32_BITSTREAM_READER
46
# define ALT_BITSTREAM_READER
47
//#define A32_BITSTREAM_READER
52
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
36
* Safe bitstream reading:
37
* optionally, the get_bits API can check to ensure that we
38
* don't read past input buffer boundaries. This is protected
39
* with CONFIG_SAFE_BITSTREAM_READER at the global level, and
40
* then below that with UNCHECKED_BITSTREAM_READER at the per-
41
* decoder level. This means that decoders that check internally
42
* can "#define UNCHECKED_BITSTREAM_READER 1" to disable
44
* Boundary checking causes a minor performance penalty so for
45
* applications that won't want/need this, it can be disabled
46
* globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
48
#ifndef UNCHECKED_BITSTREAM_READER
49
#define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
53
52
typedef struct GetBitContext {
54
53
const uint8_t *buffer, *buffer_end;
55
#ifdef ALT_BITSTREAM_READER
57
#elif defined A32_BITSTREAM_READER
56
#if !UNCHECKED_BITSTREAM_READER
57
int size_in_bits_plus8;
66
61
#define VLC_TYPE int16_t
113
108
SKIP_COUNTER(name, gb, num)
114
109
will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
116
LAST_SKIP_CACHE(name, gb, num)
117
will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
119
111
LAST_SKIP_BITS(name, gb, num)
120
is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
112
like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
122
114
for examples see get_bits, show_bits, skip_bits, get_vlc
125
#ifdef ALT_BITSTREAM_READER
117
#ifdef LONG_BITSTREAM_READER
118
# define MIN_CACHE_BITS 32
126
120
# define MIN_CACHE_BITS 25
128
# define OPEN_READER(name, gb) \
123
#define OPEN_READER(name, gb) \
129
124
unsigned int name##_index = (gb)->index; \
130
125
unsigned int av_unused name##_cache = 0
132
# define CLOSE_READER(name, gb) (gb)->index = name##_index
134
# ifdef ALT_BITSTREAM_READER_LE
135
# define UPDATE_CACHE(name, gb) \
136
name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07)
138
# define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
140
# define UPDATE_CACHE(name, gb) \
141
name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07)
143
# define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
127
#define CLOSE_READER(name, gb) (gb)->index = name##_index
129
#ifdef BITSTREAM_READER_LE
131
# ifdef LONG_BITSTREAM_READER
132
# define UPDATE_CACHE(name, gb) name##_cache = \
133
AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
135
# define UPDATE_CACHE(name, gb) name##_cache = \
136
AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
139
# define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
143
# ifdef LONG_BITSTREAM_READER
144
# define UPDATE_CACHE(name, gb) name##_cache = \
145
AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
147
# define UPDATE_CACHE(name, gb) name##_cache = \
148
AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
151
# define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
155
#if UNCHECKED_BITSTREAM_READER
147
156
# define SKIP_COUNTER(name, gb, num) name##_index += (num)
158
# define SKIP_COUNTER(name, gb, num) \
159
name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
149
# define SKIP_BITS(name, gb, num) do { \
162
#define SKIP_BITS(name, gb, num) do { \
150
163
SKIP_CACHE(name, gb, num); \
151
164
SKIP_COUNTER(name, gb, num); \
154
# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
155
# define LAST_SKIP_CACHE(name, gb, num)
167
#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
157
# ifdef ALT_BITSTREAM_READER_LE
169
#ifdef BITSTREAM_READER_LE
158
170
# define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
160
171
# define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
162
173
# define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
164
174
# define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
167
# define GET_CACHE(name, gb) ((uint32_t)name##_cache)
169
static inline int get_bits_count(const GetBitContext *s){
177
#define GET_CACHE(name, gb) ((uint32_t)name##_cache)
179
static inline int get_bits_count(const GetBitContext *s)
173
184
static inline void skip_bits_long(GetBitContext *s, int n){
185
#if UNCHECKED_BITSTREAM_READER
177
#elif defined A32_BITSTREAM_READER
179
# define MIN_CACHE_BITS 32
181
# define OPEN_READER(name, gb) \
182
int name##_bit_count = (gb)->bit_count; \
183
uint32_t name##_cache0 = (gb)->cache0; \
184
uint32_t name##_cache1 = (gb)->cache1; \
185
uint32_t *name##_buffer_ptr = (gb)->buffer_ptr
187
# define CLOSE_READER(name, gb) do { \
188
(gb)->bit_count = name##_bit_count; \
189
(gb)->cache0 = name##_cache0; \
190
(gb)->cache1 = name##_cache1; \
191
(gb)->buffer_ptr = name##_buffer_ptr; \
194
# define UPDATE_CACHE(name, gb) do { \
195
if(name##_bit_count > 0){ \
196
const uint32_t next = av_be2ne32(*name##_buffer_ptr); \
197
name##_cache0 |= NEG_USR32(next, name##_bit_count); \
198
name##_cache1 |= next << name##_bit_count; \
199
name##_buffer_ptr++; \
200
name##_bit_count -= 32; \
205
# define SKIP_CACHE(name, gb, num) \
206
__asm__("shldl %2, %1, %0 \n\t" \
208
: "+r" (name##_cache0), "+r" (name##_cache1) \
209
: "Ic" ((uint8_t)(num)))
211
# define SKIP_CACHE(name, gb, num) do { \
212
name##_cache0 <<= (num); \
213
name##_cache0 |= NEG_USR32(name##_cache1,num); \
214
name##_cache1 <<= (num); \
218
# define SKIP_COUNTER(name, gb, num) name##_bit_count += (num)
220
# define SKIP_BITS(name, gb, num) do { \
221
SKIP_CACHE(name, gb, num); \
222
SKIP_COUNTER(name, gb, num); \
225
# define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
226
# define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
228
# define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache0, num)
230
# define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache0, num)
232
# define GET_CACHE(name, gb) name##_cache0
234
static inline int get_bits_count(const GetBitContext *s) {
235
return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
238
static inline void skip_bits_long(GetBitContext *s, int n){
241
re_buffer_ptr += re_bit_count>>5;
243
re_cache0 = av_be2ne32(re_buffer_ptr[-1]) << re_bit_count;
188
s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
252
193
* read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
253
194
* if MSB not set it is negative
254
195
* @param n length in bits
257
static inline int get_xbits(GetBitContext *s, int n){
197
static inline int get_xbits(GetBitContext *s, int n)
258
199
register int sign;
259
200
register int32_t cache;
260
201
OPEN_READER(re, s);
303
static inline void skip_bits(GetBitContext *s, int n){
304
//Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
247
static inline void skip_bits(GetBitContext *s, int n)
305
249
OPEN_READER(re, s);
306
250
UPDATE_CACHE(re, s);
307
251
LAST_SKIP_BITS(re, s, n);
308
252
CLOSE_READER(re, s);
311
static inline unsigned int get_bits1(GetBitContext *s){
312
#ifdef ALT_BITSTREAM_READER
255
static inline unsigned int get_bits1(GetBitContext *s)
313
257
unsigned int index = s->index;
314
258
uint8_t result = s->buffer[index>>3];
315
#ifdef ALT_BITSTREAM_READER_LE
259
#ifdef BITSTREAM_READER_LE
316
260
result >>= index & 7;
319
263
result <<= index & 7;
320
264
result >>= 8 - 1;
266
#if !UNCHECKED_BITSTREAM_READER
267
if (s->index < s->size_in_bits_plus8)
323
270
s->index = index;
327
return get_bits(s, 1);
331
static inline unsigned int show_bits1(GetBitContext *s){
275
static inline unsigned int show_bits1(GetBitContext *s)
332
277
return show_bits(s, 1);
335
static inline void skip_bits1(GetBitContext *s){
280
static inline void skip_bits1(GetBitContext *s)
342
static inline unsigned int get_bits_long(GetBitContext *s, int n){
343
if (n <= MIN_CACHE_BITS) return get_bits(s, n);
288
static inline unsigned int get_bits_long(GetBitContext *s, int n)
290
if (n <= MIN_CACHE_BITS)
291
return get_bits(s, n);
345
#ifdef ALT_BITSTREAM_READER_LE
293
#ifdef BITSTREAM_READER_LE
346
294
int ret = get_bits(s, 16);
347
295
return ret | (get_bits(s, n-16) << 16);