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) {
00306 return 0;
00307 } else if (n <= MIN_CACHE_BITS)
00308 return get_bits(s, n);
00309 else {
00310 #ifdef BITSTREAM_READER_LE
00311 unsigned ret = get_bits(s, 16);
00312 return ret | (get_bits(s, n-16) << 16);
00313 #else
00314 unsigned ret = get_bits(s, 16) << (n-16);
00315 return ret | get_bits(s, n-16);
00316 #endif
00317 }
00318 }
00319
00323 static inline uint64_t get_bits64(GetBitContext *s, int n)
00324 {
00325 if (n <= 32) {
00326 return get_bits_long(s, n);
00327 } else {
00328 #ifdef BITSTREAM_READER_LE
00329 uint64_t ret = get_bits_long(s, 32);
00330 return ret | (uint64_t)get_bits_long(s, n - 32) << 32;
00331 #else
00332 uint64_t ret = (uint64_t)get_bits_long(s, n - 32) << 32;
00333 return ret | get_bits_long(s, 32);
00334 #endif
00335 }
00336 }
00337
00341 static inline int get_sbits_long(GetBitContext *s, int n)
00342 {
00343 return sign_extend(get_bits_long(s, n), n);
00344 }
00345
00349 static inline unsigned int show_bits_long(GetBitContext *s, int n)
00350 {
00351 if (n <= MIN_CACHE_BITS)
00352 return show_bits(s, n);
00353 else {
00354 GetBitContext gb = *s;
00355 return get_bits_long(&gb, n);
00356 }
00357 }
00358
00359 static inline int check_marker(GetBitContext *s, const char *msg)
00360 {
00361 int bit = get_bits1(s);
00362 if (!bit)
00363 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00364
00365 return bit;
00366 }
00367
00374 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
00375 int bit_size)
00376 {
00377 int buffer_size = (bit_size+7)>>3;
00378 if (buffer_size < 0 || bit_size < 0) {
00379 buffer_size = bit_size = 0;
00380 buffer = NULL;
00381 }
00382
00383 s->buffer = buffer;
00384 s->size_in_bits = bit_size;
00385 s->size_in_bits_plus8 = bit_size + 8;
00386 s->buffer_end = buffer + buffer_size;
00387 s->index = 0;
00388 }
00389
00390 static inline void align_get_bits(GetBitContext *s)
00391 {
00392 int n = -get_bits_count(s) & 7;
00393 if (n) skip_bits(s, n);
00394 }
00395
00396 #define init_vlc(vlc, nb_bits, nb_codes, \
00397 bits, bits_wrap, bits_size, \
00398 codes, codes_wrap, codes_size, \
00399 flags) \
00400 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
00401 bits, bits_wrap, bits_size, \
00402 codes, codes_wrap, codes_size, \
00403 NULL, 0, 0, flags)
00404
00405 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00406 const void *bits, int bits_wrap, int bits_size,
00407 const void *codes, int codes_wrap, int codes_size,
00408 const void *symbols, int symbols_wrap, int symbols_size,
00409 int flags);
00410 #define INIT_VLC_LE 2
00411 #define INIT_VLC_USE_NEW_STATIC 4
00412 void ff_free_vlc(VLC *vlc);
00413
00414 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \
00415 static VLC_TYPE table[static_size][2]; \
00416 (vlc)->table = table; \
00417 (vlc)->table_allocated = static_size; \
00418 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \
00419 } while (0)
00420
00421
00427 #define GET_VLC(code, name, gb, table, bits, max_depth) \
00428 do { \
00429 int n, nb_bits; \
00430 unsigned int index; \
00431 \
00432 index = SHOW_UBITS(name, gb, bits); \
00433 code = table[index][0]; \
00434 n = table[index][1]; \
00435 \
00436 if (max_depth > 1 && n < 0) { \
00437 LAST_SKIP_BITS(name, gb, bits); \
00438 UPDATE_CACHE(name, gb); \
00439 \
00440 nb_bits = -n; \
00441 \
00442 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00443 code = table[index][0]; \
00444 n = table[index][1]; \
00445 if (max_depth > 2 && n < 0) { \
00446 LAST_SKIP_BITS(name, gb, nb_bits); \
00447 UPDATE_CACHE(name, gb); \
00448 \
00449 nb_bits = -n; \
00450 \
00451 index = SHOW_UBITS(name, gb, nb_bits) + code; \
00452 code = table[index][0]; \
00453 n = table[index][1]; \
00454 } \
00455 } \
00456 SKIP_BITS(name, gb, n); \
00457 } while (0)
00458
00459 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
00460 do { \
00461 int n, nb_bits; \
00462 unsigned int index; \
00463 \
00464 index = SHOW_UBITS(name, gb, bits); \
00465 level = table[index].level; \
00466 n = table[index].len; \
00467 \
00468 if (max_depth > 1 && n < 0) { \
00469 SKIP_BITS(name, gb, bits); \
00470 if (need_update) { \
00471 UPDATE_CACHE(name, gb); \
00472 } \
00473 \
00474 nb_bits = -n; \
00475 \
00476 index = SHOW_UBITS(name, gb, nb_bits) + level; \
00477 level = table[index].level; \
00478 n = table[index].len; \
00479 } \
00480 run = table[index].run; \
00481 SKIP_BITS(name, gb, n); \
00482 } while (0)
00483
00484
00493 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00494 int bits, int max_depth)
00495 {
00496 int code;
00497
00498 OPEN_READER(re, s);
00499 UPDATE_CACHE(re, s);
00500
00501 GET_VLC(code, re, s, table, bits, max_depth);
00502
00503 CLOSE_READER(re, s);
00504 return code;
00505 }
00506
00507 static inline int decode012(GetBitContext *gb)
00508 {
00509 int n;
00510 n = get_bits1(gb);
00511 if (n == 0)
00512 return 0;
00513 else
00514 return get_bits1(gb) + 1;
00515 }
00516
00517 static inline int decode210(GetBitContext *gb)
00518 {
00519 if (get_bits1(gb))
00520 return 0;
00521 else
00522 return 2 - get_bits1(gb);
00523 }
00524
00525 static inline int get_bits_left(GetBitContext *gb)
00526 {
00527 return gb->size_in_bits - get_bits_count(gb);
00528 }
00529
00530
00531
00532 #ifdef TRACE
00533 static inline void print_bin(int bits, int n)
00534 {
00535 int i;
00536
00537 for (i = n-1; i >= 0; i--) {
00538 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00539 }
00540 for (i = n; i < 24; i++)
00541 av_log(NULL, AV_LOG_DEBUG, " ");
00542 }
00543
00544 static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
00545 const char *func, int line)
00546 {
00547 int r = get_bits(s, n);
00548
00549 print_bin(r, n);
00550 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
00551 r, n, r, get_bits_count(s)-n, file, func, line);
00552 return r;
00553 }
00554 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
00555 int bits, int max_depth, const char *file,
00556 const char *func, int line)
00557 {
00558 int show = show_bits(s, 24);
00559 int pos = get_bits_count(s);
00560 int r = get_vlc2(s, table, bits, max_depth);
00561 int len = get_bits_count(s) - pos;
00562 int bits2 = show >> (24-len);
00563
00564 print_bin(bits2, len);
00565
00566 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
00567 bits2, len, r, pos, file, func, line);
00568 return r;
00569 }
00570 static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
00571 const char *func, int line)
00572 {
00573 int show = show_bits(s, n);
00574 int r = get_xbits(s, n);
00575
00576 print_bin(show, n);
00577 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
00578 show, n, r, get_bits_count(s)-n, file, func, line);
00579 return r;
00580 }
00581
00582 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00583 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00584 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00585 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00586 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00587
00588 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00589
00590 #else //TRACE
00591 #define tprintf(p, ...) {}
00592 #endif
00593
00594 #endif