00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #ifndef AVCODEC_GET_BITS_H
00027 #define AVCODEC_GET_BITS_H
00028
00029 #include <stdint.h>
00030 #include <stdlib.h>
00031 #include <assert.h>
00032 #include "libavutil/bswap.h"
00033 #include "libavutil/common.h"
00034 #include "libavutil/intreadwrite.h"
00035 #include "libavutil/log.h"
00036 #include "mathops.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef UNCHECKED_BITSTREAM_READER
00052 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
00053 #endif
00054
00055
00056
00057 typedef struct GetBitContext {
00058 const uint8_t *buffer, *buffer_end;
00059 int index;
00060 int size_in_bits;
00061 int size_in_bits_plus8;
00062 } GetBitContext;
00063
00064 #define VLC_TYPE int16_t
00065
00066 typedef struct VLC {
00067 int bits;
00068 VLC_TYPE (*table)[2];
00069 int table_size, table_allocated;
00070 } VLC;
00071
00072 typedef struct RL_VLC_ELEM {
00073 int16_t level;
00074 int8_t len;
00075 uint8_t run;
00076 } RL_VLC_ELEM;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 #ifdef LONG_BITSTREAM_READER
00124 # define MIN_CACHE_BITS 32
00125 #else
00126 # define MIN_CACHE_BITS 25
00127 #endif
00128
00129 # define OPEN_READER(name, gb) \
00130 unsigned int name##_index = (gb)->index; \
00131 av_unused unsigned int name##_cache
00132
00133 # define CLOSE_READER(name, gb) (gb)->index = name##_index
00134
00135 # ifdef ALT_BITSTREAM_READER_LE
00136 # ifdef LONG_BITSTREAM_READER
00137 # define UPDATE_CACHE(name, gb) \
00138 name##_cache = AV_RL64((gb)->buffer+(name##_index>>3)) >> (name##_index&0x07)
00139 # else
00140 # define UPDATE_CACHE(name, gb) \
00141 name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07)
00142 # endif
00143
00144 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
00145 # else
00146 # ifdef LONG_BITSTREAM_READER
00147 # define UPDATE_CACHE(name, gb) \
00148 name##_cache = AV_RB64((gb)->buffer+(name##_index >> 3)) >> (32 - (name##_index & 0x07))
00149 # else
00150 # define UPDATE_CACHE(name, gb) \
00151 name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07)
00152 # endif
00153
00154 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
00155 # endif
00156
00157
00158 #if UNCHECKED_BITSTREAM_READER
00159 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
00160 #else
00161 # define SKIP_COUNTER(name, gb, num) \
00162 name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
00163 #endif
00164
00165 # define SKIP_BITS(name, gb, num) do { \
00166 SKIP_CACHE(name, gb, num); \
00167 SKIP_COUNTER(name, gb, num); \
00168 } while (0)
00169
00170 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
00171 # define LAST_SKIP_CACHE(name, gb, num)
00172
00173 # ifdef ALT_BITSTREAM_READER_LE
00174 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
00175
00176 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
00177 # else
00178 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
00179
00180 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
00181 # endif
00182
00183 # define GET_CACHE(name, gb) ((uint32_t)name##_cache)
00184
00185 static inline int get_bits_count(const GetBitContext *s){
00186 return s->index;
00187 }
00188
00189 static inline void skip_bits_long(GetBitContext *s, int n){
00190 #if UNCHECKED_BITSTREAM_READER
00191 s->index += n;
00192 #else
00193 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
00194 #endif
00195 }
00196
00203 static inline int get_xbits(GetBitContext *s, int n){
00204 register int sign;
00205 register int32_t cache;
00206 OPEN_READER(re, s);
00207 UPDATE_CACHE(re, s);
00208 cache = GET_CACHE(re, s);
00209 sign = ~cache >> 31;
00210 LAST_SKIP_BITS(re, s, n);
00211 CLOSE_READER(re, s);
00212 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
00213 }
00214
00215 static inline int get_sbits(GetBitContext *s, int n){
00216 register int tmp;
00217 OPEN_READER(re, s);
00218 UPDATE_CACHE(re, s);
00219 tmp = SHOW_SBITS(re, s, n);
00220 LAST_SKIP_BITS(re, s, n);
00221 CLOSE_READER(re, s);
00222 return tmp;
00223 }
00224
00228 static inline unsigned int get_bits(GetBitContext *s, int n){
00229 register int tmp;
00230 OPEN_READER(re, s);
00231 UPDATE_CACHE(re, s);
00232 tmp = SHOW_UBITS(re, s, n);
00233 LAST_SKIP_BITS(re, s, n);
00234 CLOSE_READER(re, s);
00235 return tmp;
00236 }
00237
00241 static inline unsigned int show_bits(GetBitContext *s, int n){
00242 register int tmp;
00243 OPEN_READER(re, s);
00244 UPDATE_CACHE(re, s);
00245 tmp = SHOW_UBITS(re, s, n);
00246 return tmp;
00247 }
00248
00249 static inline void skip_bits(GetBitContext *s, int n){
00250
00251 OPEN_READER(re, s);
00252 UPDATE_CACHE(re, s);
00253 LAST_SKIP_BITS(re, s, n);
00254 CLOSE_READER(re, s);
00255 }
00256
00257 static inline unsigned int get_bits1(GetBitContext *s){
00258 unsigned int index = s->index;
00259 uint8_t result = s->buffer[index>>3];
00260 #ifdef ALT_BITSTREAM_READER_LE
00261 result >>= index & 7;
00262 result &= 1;
00263 #else
00264 result <<= index & 7;
00265 result >>= 8 - 1;
00266 #endif
00267 #if !UNCHECKED_BITSTREAM_READER
00268 if (s->index < s->size_in_bits_plus8)
00269 #endif
00270 index++;
00271 s->index = index;
00272
00273 return result;
00274 }
00275
00276 static inline unsigned int show_bits1(GetBitContext *s){
00277 return show_bits(s, 1);
00278 }
00279
00280 static inline void skip_bits1(GetBitContext *s){
00281 skip_bits(s, 1);
00282 }
00283
00287 static inline unsigned int get_bits_long(GetBitContext *s, int n){
00288 if (n <= MIN_CACHE_BITS) return get_bits(s, n);
00289 else {
00290 #ifdef ALT_BITSTREAM_READER_LE
00291 int ret = get_bits(s, 16);
00292 return ret | (get_bits(s, n-16) << 16);
00293 #else
00294 int ret = get_bits(s, 16) << (n-16);
00295 return ret | get_bits(s, n-16);
00296 #endif
00297 }
00298 }
00299
00303 static inline int get_sbits_long(GetBitContext *s, int n) {
00304 return sign_extend(get_bits_long(s, n), n);
00305 }
00306
00310 static inline unsigned int show_bits_long(GetBitContext *s, int n){
00311 if (n <= MIN_CACHE_BITS) return show_bits(s, n);
00312 else {
00313 GetBitContext gb = *s;
00314 return get_bits_long(&gb, n);
00315 }
00316 }
00317
00318 static inline int check_marker(GetBitContext *s, const char *msg)
00319 {
00320 int bit = get_bits1(s);
00321 if (!bit)
00322 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00323
00324 return bit;
00325 }
00326
00336 static inline void init_get_bits(GetBitContext *s,
00337 const uint8_t *buffer, int bit_size)
00338 {
00339 int buffer_size = (bit_size+7)>>3;
00340 if (buffer_size < 0 || bit_size < 0) {
00341 buffer_size = bit_size = 0;
00342 buffer = NULL;
00343 }
00344
00345 s->buffer = buffer;
00346 s->size_in_bits = bit_size;
00347 s->size_in_bits_plus8 = bit_size + 8;
00348 s->buffer_end = buffer + buffer_size;
00349 s->index = 0;
00350 }
00351
00352 static inline void align_get_bits(GetBitContext *s)
00353 {
00354 int n = -get_bits_count(s) & 7;
00355 if (n) skip_bits(s, n);
00356 }
00357
00358 #define init_vlc(vlc, nb_bits, nb_codes, \
00359 bits, bits_wrap, bits_size, \
00360 codes, codes_wrap, codes_size, \
00361 flags) \
00362 init_vlc_sparse(vlc, nb_bits, nb_codes, \
00363 bits, bits_wrap, bits_size, \
00364 codes, codes_wrap, codes_size, \
00365 NULL, 0, 0, flags)
00366
00367 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00368 const void *bits, int bits_wrap, int bits_size,
00369 const void *codes, int codes_wrap, int codes_size,
00370 const void *symbols, int symbols_wrap, int symbols_size,
00371 int flags);
00372 #define INIT_VLC_LE 2
00373 #define INIT_VLC_USE_NEW_STATIC 4
00374 void free_vlc(VLC *vlc);
00375
00376 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
00377 static VLC_TYPE table[static_size][2]; \
00378 (vlc)->table = table; \
00379 (vlc)->table_allocated = static_size; \
00380 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
00381 } while (0)
00382
00383
00390 #define GET_VLC(code, name, gb, table, bits, max_depth) do { \
00391 int n, nb_bits; \
00392 unsigned int index; \
00393 \
00394 index = SHOW_UBITS(name, gb, bits); \
00395 code = table[index][0]; \
00396 n = table[index][1]; \
00397 \
00398 if (max_depth > 1 && n < 0) { \
00399 LAST_SKIP_BITS(name, gb, bits); \
00400 UPDATE_CACHE(name, gb); \
00401 \
00402 nb_bits = -n; \
00403 \
00404 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00405 code = table[index][0]; \
00406 n = table[index][1]; \
00407 if (max_depth > 2 && n < 0) { \
00408 LAST_SKIP_BITS(name, gb, nb_bits); \
00409 UPDATE_CACHE(name, gb); \
00410 \
00411 nb_bits = -n; \
00412 \
00413 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00414 code = table[index][0]; \
00415 n = table[index][1]; \
00416 } \
00417 } \
00418 SKIP_BITS(name, gb, n); \
00419 } while (0)
00420
00421 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \
00422 int n, nb_bits; \
00423 unsigned int index; \
00424 \
00425 index = SHOW_UBITS(name, gb, bits); \
00426 level = table[index].level; \
00427 n = table[index].len; \
00428 \
00429 if (max_depth > 1 && n < 0) { \
00430 SKIP_BITS(name, gb, bits); \
00431 if (need_update) { \
00432 UPDATE_CACHE(name, gb); \
00433 } \
00434 \
00435 nb_bits = -n; \
00436 \
00437 index = SHOW_UBITS(name, gb, nb_bits) + level; \
00438 level = table[index].level; \
00439 n = table[index].len; \
00440 } \
00441 run = table[index].run; \
00442 SKIP_BITS(name, gb, n); \
00443 } while (0)
00444
00445
00454 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00455 int bits, int max_depth)
00456 {
00457 int code;
00458
00459 OPEN_READER(re, s);
00460 UPDATE_CACHE(re, s);
00461
00462 GET_VLC(code, re, s, table, bits, max_depth);
00463
00464 CLOSE_READER(re, s);
00465 return code;
00466 }
00467
00468 static inline int decode012(GetBitContext *gb){
00469 int n;
00470 n = get_bits1(gb);
00471 if (n == 0)
00472 return 0;
00473 else
00474 return get_bits1(gb) + 1;
00475 }
00476
00477 static inline int decode210(GetBitContext *gb){
00478 if (get_bits1(gb))
00479 return 0;
00480 else
00481 return 2 - get_bits1(gb);
00482 }
00483
00484 static inline int get_bits_left(GetBitContext *gb)
00485 {
00486 return gb->size_in_bits - get_bits_count(gb);
00487 }
00488
00489
00490
00491 #ifdef TRACE
00492 static inline void print_bin(int bits, int n){
00493 int i;
00494
00495 for (i = n-1; i >= 0; i--) {
00496 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00497 }
00498 for (i = n; i < 24; i++)
00499 av_log(NULL, AV_LOG_DEBUG, " ");
00500 }
00501
00502 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
00503 const char *func, int line){
00504 int r = get_bits(s, n);
00505
00506 print_bin(r, n);
00507 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
00508 r, n, r, get_bits_count(s)-n, file, func, line);
00509 return r;
00510 }
00511 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
00512 int bits, int max_depth, char *file,
00513 const char *func, int line){
00514 int show = show_bits(s, 24);
00515 int pos = get_bits_count(s);
00516 int r = get_vlc2(s, table, bits, max_depth);
00517 int len = get_bits_count(s) - pos;
00518 int bits2 = show >> (24-len);
00519
00520 print_bin(bits2, len);
00521
00522 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
00523 bits2, len, r, pos, file, func, line);
00524 return r;
00525 }
00526 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
00527 const char *func, int line){
00528 int show = show_bits(s, n);
00529 int r = get_xbits(s, n);
00530
00531 print_bin(show, n);
00532 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
00533 show, n, r, get_bits_count(s)-n, file, func, line);
00534 return r;
00535 }
00536
00537 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00538 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00539 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00540 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00541 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00542
00543 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00544
00545 #else //TRACE
00546 #define tprintf(p, ...) {}
00547 #endif
00548
00549 #endif