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