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
00352 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
00353 int bit_size)
00354 {
00355 int buffer_size = (bit_size+7)>>3;
00356 if (buffer_size < 0 || bit_size < 0) {
00357 buffer_size = bit_size = 0;
00358 buffer = NULL;
00359 }
00360
00361 s->buffer = buffer;
00362 s->size_in_bits = bit_size;
00363 #if !UNCHECKED_BITSTREAM_READER
00364 s->size_in_bits_plus8 = bit_size + 8;
00365 #endif
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 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 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 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