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