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 "libavutil/common.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/log.h"
00033 #include "mathops.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef UNCHECKED_BITSTREAM_READER
00049 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
00050 #endif
00051
00052 typedef struct GetBitContext {
00053 const uint8_t *buffer, *buffer_end;
00054 int index;
00055 int size_in_bits;
00056 #if !UNCHECKED_BITSTREAM_READER
00057 int size_in_bits_plus8;
00058 #endif
00059 } GetBitContext;
00060
00061 #define VLC_TYPE int16_t
00062
00063 typedef struct VLC {
00064 int bits;
00065 VLC_TYPE (*table)[2];
00066 int table_size, table_allocated;
00067 } VLC;
00068
00069 typedef struct RL_VLC_ELEM {
00070 int16_t level;
00071 int8_t len;
00072 uint8_t run;
00073 } RL_VLC_ELEM;
00074
00075
00076
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 #ifdef LONG_BITSTREAM_READER
00118 # define MIN_CACHE_BITS 32
00119 #else
00120 # define MIN_CACHE_BITS 25
00121 #endif
00122
00123 #if UNCHECKED_BITSTREAM_READER
00124 #define OPEN_READER(name, gb) \
00125 unsigned int name##_index = (gb)->index; \
00126 unsigned int av_unused name##_cache = 0
00127
00128 #define HAVE_BITS_REMAINING(name, gb) 1
00129 #else
00130 #define OPEN_READER(name, gb) \
00131 unsigned int name##_index = (gb)->index; \
00132 unsigned int av_unused name##_cache = 0; \
00133 unsigned int av_unused name##_size_plus8 = \
00134 (gb)->size_in_bits_plus8
00135
00136 #define HAVE_BITS_REMAINING(name, gb) \
00137 name##_index < name##_size_plus8
00138 #endif
00139
00140 #define CLOSE_READER(name, gb) (gb)->index = name##_index
00141
00142 #ifdef BITSTREAM_READER_LE
00143
00144 # ifdef LONG_BITSTREAM_READER
00145 # define UPDATE_CACHE(name, gb) name##_cache = \
00146 AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
00147 # else
00148 # define UPDATE_CACHE(name, gb) name##_cache = \
00149 AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
00150 # endif
00151
00152 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
00153
00154 #else
00155
00156 # ifdef LONG_BITSTREAM_READER
00157 # define UPDATE_CACHE(name, gb) name##_cache = \
00158 AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
00159 # else
00160 # define UPDATE_CACHE(name, gb) name##_cache = \
00161 AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
00162 # endif
00163
00164 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
00165
00166 #endif
00167
00168 #if UNCHECKED_BITSTREAM_READER
00169 # define SKIP_COUNTER(name, gb, num) name##_index += (num)
00170 #else
00171 # define SKIP_COUNTER(name, gb, num) \
00172 name##_index = FFMIN(name##_size_plus8, name##_index + (num))
00173 #endif
00174
00175 #define SKIP_BITS(name, gb, num) do { \
00176 SKIP_CACHE(name, gb, num); \
00177 SKIP_COUNTER(name, gb, num); \
00178 } while (0)
00179
00180 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
00181
00182 #ifdef BITSTREAM_READER_LE
00183 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
00184 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
00185 #else
00186 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
00187 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
00188 #endif
00189
00190 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
00191
00192 static inline int get_bits_count(const GetBitContext *s)
00193 {
00194 return s->index;
00195 }
00196
00197 static inline void skip_bits_long(GetBitContext *s, int n){
00198 #if UNCHECKED_BITSTREAM_READER
00199 s->index += n;
00200 #else
00201 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
00202 #endif
00203 }
00204
00210 static inline int get_xbits(GetBitContext *s, int n)
00211 {
00212 register int sign;
00213 register int32_t cache;
00214 OPEN_READER(re, s);
00215 UPDATE_CACHE(re, s);
00216 cache = GET_CACHE(re, s);
00217 sign = ~cache >> 31;
00218 LAST_SKIP_BITS(re, s, n);
00219 CLOSE_READER(re, s);
00220 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
00221 }
00222
00223 static inline int get_sbits(GetBitContext *s, int n)
00224 {
00225 register int tmp;
00226 OPEN_READER(re, s);
00227 UPDATE_CACHE(re, s);
00228 tmp = SHOW_SBITS(re, s, n);
00229 LAST_SKIP_BITS(re, s, n);
00230 CLOSE_READER(re, s);
00231 return tmp;
00232 }
00233
00237 static inline unsigned int get_bits(GetBitContext *s, int n)
00238 {
00239 register int tmp;
00240 OPEN_READER(re, s);
00241 UPDATE_CACHE(re, s);
00242 tmp = SHOW_UBITS(re, s, n);
00243 LAST_SKIP_BITS(re, s, n);
00244 CLOSE_READER(re, s);
00245 return tmp;
00246 }
00247
00251 static inline unsigned int show_bits(GetBitContext *s, int n)
00252 {
00253 register int tmp;
00254 OPEN_READER(re, s);
00255 UPDATE_CACHE(re, s);
00256 tmp = SHOW_UBITS(re, s, n);
00257 return tmp;
00258 }
00259
00260 static inline void skip_bits(GetBitContext *s, int n)
00261 {
00262 OPEN_READER(re, s);
00263 UPDATE_CACHE(re, s);
00264 LAST_SKIP_BITS(re, s, n);
00265 CLOSE_READER(re, s);
00266 }
00267
00268 static inline unsigned int get_bits1(GetBitContext *s)
00269 {
00270 unsigned int index = s->index;
00271 uint8_t result = s->buffer[index>>3];
00272 #ifdef BITSTREAM_READER_LE
00273 result >>= index & 7;
00274 result &= 1;
00275 #else
00276 result <<= index & 7;
00277 result >>= 8 - 1;
00278 #endif
00279 #if !UNCHECKED_BITSTREAM_READER
00280 if (s->index < s->size_in_bits_plus8)
00281 #endif
00282 index++;
00283 s->index = index;
00284
00285 return result;
00286 }
00287
00288 static inline unsigned int show_bits1(GetBitContext *s)
00289 {
00290 return show_bits(s, 1);
00291 }
00292
00293 static inline void skip_bits1(GetBitContext *s)
00294 {
00295 skip_bits(s, 1);
00296 }
00297
00301 static inline unsigned int get_bits_long(GetBitContext *s, int n)
00302 {
00303 if (n <= MIN_CACHE_BITS)
00304 return get_bits(s, n);
00305 else {
00306 #ifdef BITSTREAM_READER_LE
00307 int ret = get_bits(s, 16);
00308 return ret | (get_bits(s, n-16) << 16);
00309 #else
00310 int ret = get_bits(s, 16) << (n-16);
00311 return ret | get_bits(s, n-16);
00312 #endif
00313 }
00314 }
00315
00319 static inline int get_sbits_long(GetBitContext *s, int n)
00320 {
00321 return sign_extend(get_bits_long(s, n), n);
00322 }
00323
00327 static inline unsigned int show_bits_long(GetBitContext *s, int n)
00328 {
00329 if (n <= MIN_CACHE_BITS)
00330 return show_bits(s, n);
00331 else {
00332 GetBitContext gb = *s;
00333 return get_bits_long(&gb, n);
00334 }
00335 }
00336
00337 static inline int check_marker(GetBitContext *s, const char *msg)
00338 {
00339 int bit = get_bits1(s);
00340 if (!bit)
00341 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00342
00343 return bit;
00344 }
00345
00354 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
00355 int bit_size)
00356 {
00357 int buffer_size;
00358 int ret = 0;
00359
00360 if (bit_size > INT_MAX - 7 || bit_size <= 0) {
00361 buffer_size = bit_size = 0;
00362 buffer = NULL;
00363 ret = AVERROR_INVALIDDATA;
00364 }
00365
00366 buffer_size = (bit_size + 7) >> 3;
00367
00368 s->buffer = buffer;
00369 s->size_in_bits = bit_size;
00370 #if !UNCHECKED_BITSTREAM_READER
00371 s->size_in_bits_plus8 = bit_size + 8;
00372 #endif
00373 s->buffer_end = buffer + buffer_size;
00374 s->index = 0;
00375 return ret;
00376 }
00377
00378 static inline void align_get_bits(GetBitContext *s)
00379 {
00380 int n = -get_bits_count(s) & 7;
00381 if (n) skip_bits(s, n);
00382 }
00383
00384 #define init_vlc(vlc, nb_bits, nb_codes, \
00385 bits, bits_wrap, bits_size, \
00386 codes, codes_wrap, codes_size, \
00387 flags) \
00388 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
00389 bits, bits_wrap, bits_size, \
00390 codes, codes_wrap, codes_size, \
00391 NULL, 0, 0, flags)
00392
00393 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00394 const void *bits, int bits_wrap, int bits_size,
00395 const void *codes, int codes_wrap, int codes_size,
00396 const void *symbols, int symbols_wrap, int symbols_size,
00397 int flags);
00398 #define INIT_VLC_LE 2
00399 #define INIT_VLC_USE_NEW_STATIC 4
00400 void ff_free_vlc(VLC *vlc);
00401
00402 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
00403 static VLC_TYPE table[static_size][2]; \
00404 (vlc)->table = table; \
00405 (vlc)->table_allocated = static_size; \
00406 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
00407 } while (0)
00408
00409
00415 #define GET_VLC(code, name, gb, table, bits, max_depth) \
00416 do { \
00417 int n, nb_bits; \
00418 unsigned int index; \
00419 \
00420 index = SHOW_UBITS(name, gb, bits); \
00421 code = table[index][0]; \
00422 n = table[index][1]; \
00423 \
00424 if (max_depth > 1 && n < 0) { \
00425 LAST_SKIP_BITS(name, gb, bits); \
00426 UPDATE_CACHE(name, gb); \
00427 \
00428 nb_bits = -n; \
00429 \
00430 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00431 code = table[index][0]; \
00432 n = table[index][1]; \
00433 if (max_depth > 2 && n < 0) { \
00434 LAST_SKIP_BITS(name, gb, nb_bits); \
00435 UPDATE_CACHE(name, gb); \
00436 \
00437 nb_bits = -n; \
00438 \
00439 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00440 code = table[index][0]; \
00441 n = table[index][1]; \
00442 } \
00443 } \
00444 SKIP_BITS(name, gb, n); \
00445 } while (0)
00446
00447 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
00448 do { \
00449 int n, nb_bits; \
00450 unsigned int index; \
00451 \
00452 index = SHOW_UBITS(name, gb, bits); \
00453 level = table[index].level; \
00454 n = table[index].len; \
00455 \
00456 if (max_depth > 1 && n < 0) { \
00457 SKIP_BITS(name, gb, bits); \
00458 if (need_update) { \
00459 UPDATE_CACHE(name, gb); \
00460 } \
00461 \
00462 nb_bits = -n; \
00463 \
00464 index = SHOW_UBITS(name, gb, nb_bits) + level; \
00465 level = table[index].level; \
00466 n = table[index].len; \
00467 } \
00468 run = table[index].run; \
00469 SKIP_BITS(name, gb, n); \
00470 } while (0)
00471
00472
00481 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00482 int bits, int max_depth)
00483 {
00484 int code;
00485
00486 OPEN_READER(re, s);
00487 UPDATE_CACHE(re, s);
00488
00489 GET_VLC(code, re, s, table, bits, max_depth);
00490
00491 CLOSE_READER(re, s);
00492 return code;
00493 }
00494
00495 static inline int decode012(GetBitContext *gb)
00496 {
00497 int n;
00498 n = get_bits1(gb);
00499 if (n == 0)
00500 return 0;
00501 else
00502 return get_bits1(gb) + 1;
00503 }
00504
00505 static inline int decode210(GetBitContext *gb)
00506 {
00507 if (get_bits1(gb))
00508 return 0;
00509 else
00510 return 2 - get_bits1(gb);
00511 }
00512
00513 static inline int get_bits_left(GetBitContext *gb)
00514 {
00515 return gb->size_in_bits - get_bits_count(gb);
00516 }
00517
00518
00519
00520 #ifdef TRACE
00521 static inline void print_bin(int bits, int n)
00522 {
00523 int i;
00524
00525 for (i = n-1; i >= 0; i--) {
00526 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00527 }
00528 for (i = n; i < 24; i++)
00529 av_log(NULL, AV_LOG_DEBUG, " ");
00530 }
00531
00532 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
00533 const char *func, int line)
00534 {
00535 int r = get_bits(s, n);
00536
00537 print_bin(r, n);
00538 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
00539 r, n, r, get_bits_count(s)-n, file, func, line);
00540 return r;
00541 }
00542 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
00543 int bits, int max_depth, char *file,
00544 const char *func, int line)
00545 {
00546 int show = show_bits(s, 24);
00547 int pos = get_bits_count(s);
00548 int r = get_vlc2(s, table, bits, max_depth);
00549 int len = get_bits_count(s) - pos;
00550 int bits2 = show >> (24-len);
00551
00552 print_bin(bits2, len);
00553
00554 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
00555 bits2, len, r, pos, file, func, line);
00556 return r;
00557 }
00558 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
00559 const char *func, int line)
00560 {
00561 int show = show_bits(s, n);
00562 int r = get_xbits(s, n);
00563
00564 print_bin(show, n);
00565 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
00566 show, n, r, get_bits_count(s)-n, file, func, line);
00567 return r;
00568 }
00569
00570 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00571 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00572 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00573 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00574 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00575
00576 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00577
00578 #else //TRACE
00579 #define tprintf(p, ...) {}
00580 #endif
00581
00582 #endif