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 <stdlib.h>
00031 #include <assert.h>
00032 #include "libavutil/bswap.h"
00033 #include "libavutil/common.h"
00034 #include "libavutil/intreadwrite.h"
00035 #include "libavutil/log.h"
00036 #include "mathops.h"
00037
00038 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
00039 # define ALT_BITSTREAM_READER
00040 #endif
00041
00042 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
00043 # if ARCH_ARM && !HAVE_FAST_UNALIGNED
00044 # define A32_BITSTREAM_READER
00045 # else
00046 # define ALT_BITSTREAM_READER
00047
00048
00049 # endif
00050 #endif
00051
00052
00053
00054 typedef struct GetBitContext {
00055 const uint8_t *buffer, *buffer_end;
00056 #ifdef ALT_BITSTREAM_READER
00057 int index;
00058 #elif defined LIBMPEG2_BITSTREAM_READER
00059 uint8_t *buffer_ptr;
00060 uint32_t cache;
00061 int bit_count;
00062 #elif defined A32_BITSTREAM_READER
00063 uint32_t *buffer_ptr;
00064 uint32_t cache0;
00065 uint32_t cache1;
00066 int bit_count;
00067 #endif
00068 int size_in_bits;
00069 } GetBitContext;
00070
00071 #define VLC_TYPE int16_t
00072
00073 typedef struct VLC {
00074 int bits;
00075 VLC_TYPE (*table)[2];
00076 int table_size, table_allocated;
00077 } VLC;
00078
00079 typedef struct RL_VLC_ELEM {
00080 int16_t level;
00081 int8_t len;
00082 uint8_t run;
00083 } RL_VLC_ELEM;
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
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 #ifdef ALT_BITSTREAM_READER
00131 # define MIN_CACHE_BITS 25
00132
00133 # define OPEN_READER(name, gb)\
00134 unsigned int name##_index= (gb)->index;\
00135 int name##_cache= 0;\
00136
00137 # define CLOSE_READER(name, gb)\
00138 (gb)->index= name##_index;\
00139
00140 # ifdef ALT_BITSTREAM_READER_LE
00141 # define UPDATE_CACHE(name, gb)\
00142 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
00143
00144 # define SKIP_CACHE(name, gb, num)\
00145 name##_cache >>= (num);
00146 # else
00147 # define UPDATE_CACHE(name, gb)\
00148 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
00149
00150 # define SKIP_CACHE(name, gb, num)\
00151 name##_cache <<= (num);
00152 # endif
00153
00154
00155 # define SKIP_COUNTER(name, gb, num)\
00156 name##_index += (num);\
00157
00158 # define SKIP_BITS(name, gb, num)\
00159 {\
00160 SKIP_CACHE(name, gb, num)\
00161 SKIP_COUNTER(name, gb, num)\
00162 }\
00163
00164 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
00165 # define LAST_SKIP_CACHE(name, gb, num) ;
00166
00167 # ifdef ALT_BITSTREAM_READER_LE
00168 # define SHOW_UBITS(name, gb, num)\
00169 zero_extend(name##_cache, num)
00170
00171 # define SHOW_SBITS(name, gb, num)\
00172 sign_extend(name##_cache, num)
00173 # else
00174 # define SHOW_UBITS(name, gb, num)\
00175 NEG_USR32(name##_cache, num)
00176
00177 # define SHOW_SBITS(name, gb, num)\
00178 NEG_SSR32(name##_cache, num)
00179 # endif
00180
00181 # define GET_CACHE(name, gb)\
00182 ((uint32_t)name##_cache)
00183
00184 static inline int get_bits_count(const GetBitContext *s){
00185 return s->index;
00186 }
00187
00188 static inline void skip_bits_long(GetBitContext *s, int n){
00189 s->index += n;
00190 }
00191
00192 #elif defined LIBMPEG2_BITSTREAM_READER
00193
00194
00195 # define MIN_CACHE_BITS 17
00196
00197 # define OPEN_READER(name, gb)\
00198 int name##_bit_count=(gb)->bit_count;\
00199 int name##_cache= (gb)->cache;\
00200 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00201
00202 # define CLOSE_READER(name, gb)\
00203 (gb)->bit_count= name##_bit_count;\
00204 (gb)->cache= name##_cache;\
00205 (gb)->buffer_ptr= name##_buffer_ptr;\
00206
00207 # define UPDATE_CACHE(name, gb)\
00208 if(name##_bit_count >= 0){\
00209 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
00210 name##_buffer_ptr+=2;\
00211 name##_bit_count-= 16;\
00212 }\
00213
00214 # define SKIP_CACHE(name, gb, num)\
00215 name##_cache <<= (num);\
00216
00217 # define SKIP_COUNTER(name, gb, num)\
00218 name##_bit_count += (num);\
00219
00220 # define SKIP_BITS(name, gb, num)\
00221 {\
00222 SKIP_CACHE(name, gb, num)\
00223 SKIP_COUNTER(name, gb, num)\
00224 }\
00225
00226 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00227 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00228
00229 # define SHOW_UBITS(name, gb, num)\
00230 NEG_USR32(name##_cache, num)
00231
00232 # define SHOW_SBITS(name, gb, num)\
00233 NEG_SSR32(name##_cache, num)
00234
00235 # define GET_CACHE(name, gb)\
00236 ((uint32_t)name##_cache)
00237
00238 static inline int get_bits_count(const GetBitContext *s){
00239 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
00240 }
00241
00242 static inline void skip_bits_long(GetBitContext *s, int n){
00243 OPEN_READER(re, s)
00244 re_bit_count += n;
00245 re_buffer_ptr += 2*(re_bit_count>>4);
00246 re_bit_count &= 15;
00247 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
00248 UPDATE_CACHE(re, s)
00249 CLOSE_READER(re, s)
00250 }
00251
00252 #elif defined A32_BITSTREAM_READER
00253
00254 # define MIN_CACHE_BITS 32
00255
00256 # define OPEN_READER(name, gb)\
00257 int name##_bit_count=(gb)->bit_count;\
00258 uint32_t name##_cache0= (gb)->cache0;\
00259 uint32_t name##_cache1= (gb)->cache1;\
00260 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00261
00262 # define CLOSE_READER(name, gb)\
00263 (gb)->bit_count= name##_bit_count;\
00264 (gb)->cache0= name##_cache0;\
00265 (gb)->cache1= name##_cache1;\
00266 (gb)->buffer_ptr= name##_buffer_ptr;\
00267
00268 # define UPDATE_CACHE(name, gb)\
00269 if(name##_bit_count > 0){\
00270 const uint32_t next= be2me_32( *name##_buffer_ptr );\
00271 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
00272 name##_cache1 |= next<<name##_bit_count;\
00273 name##_buffer_ptr++;\
00274 name##_bit_count-= 32;\
00275 }\
00276
00277 #if ARCH_X86
00278 # define SKIP_CACHE(name, gb, num)\
00279 __asm__(\
00280 "shldl %2, %1, %0 \n\t"\
00281 "shll %2, %1 \n\t"\
00282 : "+r" (name##_cache0), "+r" (name##_cache1)\
00283 : "Ic" ((uint8_t)(num))\
00284 );
00285 #else
00286 # define SKIP_CACHE(name, gb, num)\
00287 name##_cache0 <<= (num);\
00288 name##_cache0 |= NEG_USR32(name##_cache1,num);\
00289 name##_cache1 <<= (num);
00290 #endif
00291
00292 # define SKIP_COUNTER(name, gb, num)\
00293 name##_bit_count += (num);\
00294
00295 # define SKIP_BITS(name, gb, num)\
00296 {\
00297 SKIP_CACHE(name, gb, num)\
00298 SKIP_COUNTER(name, gb, num)\
00299 }\
00300
00301 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00302 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00303
00304 # define SHOW_UBITS(name, gb, num)\
00305 NEG_USR32(name##_cache0, num)
00306
00307 # define SHOW_SBITS(name, gb, num)\
00308 NEG_SSR32(name##_cache0, num)
00309
00310 # define GET_CACHE(name, gb)\
00311 (name##_cache0)
00312
00313 static inline int get_bits_count(const GetBitContext *s){
00314 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
00315 }
00316
00317 static inline void skip_bits_long(GetBitContext *s, int n){
00318 OPEN_READER(re, s)
00319 re_bit_count += n;
00320 re_buffer_ptr += re_bit_count>>5;
00321 re_bit_count &= 31;
00322 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
00323 re_cache1 = 0;
00324 UPDATE_CACHE(re, s)
00325 CLOSE_READER(re, s)
00326 }
00327
00328 #endif
00329
00336 static inline int get_xbits(GetBitContext *s, int n){
00337 register int sign;
00338 register int32_t cache;
00339 OPEN_READER(re, s)
00340 UPDATE_CACHE(re, s)
00341 cache = GET_CACHE(re,s);
00342 sign=(~cache)>>31;
00343 LAST_SKIP_BITS(re, s, n)
00344 CLOSE_READER(re, s)
00345 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
00346 }
00347
00348 static inline int get_sbits(GetBitContext *s, int n){
00349 register int tmp;
00350 OPEN_READER(re, s)
00351 UPDATE_CACHE(re, s)
00352 tmp= SHOW_SBITS(re, s, n);
00353 LAST_SKIP_BITS(re, s, n)
00354 CLOSE_READER(re, s)
00355 return tmp;
00356 }
00357
00362 static inline unsigned int get_bits(GetBitContext *s, int n){
00363 register int tmp;
00364 OPEN_READER(re, s)
00365 UPDATE_CACHE(re, s)
00366 tmp= SHOW_UBITS(re, s, n);
00367 LAST_SKIP_BITS(re, s, n)
00368 CLOSE_READER(re, s)
00369 return tmp;
00370 }
00371
00376 static inline unsigned int show_bits(GetBitContext *s, int n){
00377 register int tmp;
00378 OPEN_READER(re, s)
00379 UPDATE_CACHE(re, s)
00380 tmp= SHOW_UBITS(re, s, n);
00381
00382 return tmp;
00383 }
00384
00385 static inline void skip_bits(GetBitContext *s, int n){
00386
00387 OPEN_READER(re, s)
00388 UPDATE_CACHE(re, s)
00389 LAST_SKIP_BITS(re, s, n)
00390 CLOSE_READER(re, s)
00391 }
00392
00393 static inline unsigned int get_bits1(GetBitContext *s){
00394 #ifdef ALT_BITSTREAM_READER
00395 unsigned int index= s->index;
00396 uint8_t result= s->buffer[ index>>3 ];
00397 #ifdef ALT_BITSTREAM_READER_LE
00398 result>>= (index&0x07);
00399 result&= 1;
00400 #else
00401 result<<= (index&0x07);
00402 result>>= 8 - 1;
00403 #endif
00404 index++;
00405 s->index= index;
00406
00407 return result;
00408 #else
00409 return get_bits(s, 1);
00410 #endif
00411 }
00412
00413 static inline unsigned int show_bits1(GetBitContext *s){
00414 return show_bits(s, 1);
00415 }
00416
00417 static inline void skip_bits1(GetBitContext *s){
00418 skip_bits(s, 1);
00419 }
00420
00424 static inline unsigned int get_bits_long(GetBitContext *s, int n){
00425 if(n<=MIN_CACHE_BITS) return get_bits(s, n);
00426 else{
00427 #ifdef ALT_BITSTREAM_READER_LE
00428 int ret= get_bits(s, 16);
00429 return ret | (get_bits(s, n-16) << 16);
00430 #else
00431 int ret= get_bits(s, 16) << (n-16);
00432 return ret | get_bits(s, n-16);
00433 #endif
00434 }
00435 }
00436
00440 static inline int get_sbits_long(GetBitContext *s, int n) {
00441 return sign_extend(get_bits_long(s, n), n);
00442 }
00443
00447 static inline unsigned int show_bits_long(GetBitContext *s, int n){
00448 if(n<=MIN_CACHE_BITS) return show_bits(s, n);
00449 else{
00450 GetBitContext gb= *s;
00451 return get_bits_long(&gb, n);
00452 }
00453 }
00454
00455 static inline int check_marker(GetBitContext *s, const char *msg)
00456 {
00457 int bit= get_bits1(s);
00458 if(!bit)
00459 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00460
00461 return bit;
00462 }
00463
00473 static inline void init_get_bits(GetBitContext *s,
00474 const uint8_t *buffer, int bit_size)
00475 {
00476 int buffer_size= (bit_size+7)>>3;
00477 if(buffer_size < 0 || bit_size < 0) {
00478 buffer_size = bit_size = 0;
00479 buffer = NULL;
00480 }
00481
00482 s->buffer= buffer;
00483 s->size_in_bits= bit_size;
00484 s->buffer_end= buffer + buffer_size;
00485 #ifdef ALT_BITSTREAM_READER
00486 s->index=0;
00487 #elif defined LIBMPEG2_BITSTREAM_READER
00488 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
00489 s->bit_count = 16 + 8*((intptr_t)buffer&1);
00490 skip_bits_long(s, 0);
00491 #elif defined A32_BITSTREAM_READER
00492 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
00493 s->bit_count = 32 + 8*((intptr_t)buffer&3);
00494 skip_bits_long(s, 0);
00495 #endif
00496 }
00497
00498 static inline void align_get_bits(GetBitContext *s)
00499 {
00500 int n= (-get_bits_count(s)) & 7;
00501 if(n) skip_bits(s, n);
00502 }
00503
00504 #define init_vlc(vlc, nb_bits, nb_codes,\
00505 bits, bits_wrap, bits_size,\
00506 codes, codes_wrap, codes_size,\
00507 flags)\
00508 init_vlc_sparse(vlc, nb_bits, nb_codes,\
00509 bits, bits_wrap, bits_size,\
00510 codes, codes_wrap, codes_size,\
00511 NULL, 0, 0, flags)
00512
00513 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00514 const void *bits, int bits_wrap, int bits_size,
00515 const void *codes, int codes_wrap, int codes_size,
00516 const void *symbols, int symbols_wrap, int symbols_size,
00517 int flags);
00518 #define INIT_VLC_LE 2
00519 #define INIT_VLC_USE_NEW_STATIC 4
00520 void free_vlc(VLC *vlc);
00521
00522 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
00523 {\
00524 static VLC_TYPE table[static_size][2];\
00525 (vlc)->table= table;\
00526 (vlc)->table_allocated= static_size;\
00527 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
00528 }
00529
00530
00537 #define GET_VLC(code, name, gb, table, bits, max_depth)\
00538 {\
00539 int n, nb_bits;\
00540 unsigned int index;\
00541 \
00542 index= SHOW_UBITS(name, gb, bits);\
00543 code = table[index][0];\
00544 n = table[index][1];\
00545 \
00546 if(max_depth > 1 && n < 0){\
00547 LAST_SKIP_BITS(name, gb, bits)\
00548 UPDATE_CACHE(name, gb)\
00549 \
00550 nb_bits = -n;\
00551 \
00552 index= SHOW_UBITS(name, gb, nb_bits) + code;\
00553 code = table[index][0];\
00554 n = table[index][1];\
00555 if(max_depth > 2 && n < 0){\
00556 LAST_SKIP_BITS(name, gb, nb_bits)\
00557 UPDATE_CACHE(name, gb)\
00558 \
00559 nb_bits = -n;\
00560 \
00561 index= SHOW_UBITS(name, gb, nb_bits) + code;\
00562 code = table[index][0];\
00563 n = table[index][1];\
00564 }\
00565 }\
00566 SKIP_BITS(name, gb, n)\
00567 }
00568
00569 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
00570 {\
00571 int n, nb_bits;\
00572 unsigned int index;\
00573 \
00574 index= SHOW_UBITS(name, gb, bits);\
00575 level = table[index].level;\
00576 n = table[index].len;\
00577 \
00578 if(max_depth > 1 && n < 0){\
00579 SKIP_BITS(name, gb, bits)\
00580 if(need_update){\
00581 UPDATE_CACHE(name, gb)\
00582 }\
00583 \
00584 nb_bits = -n;\
00585 \
00586 index= SHOW_UBITS(name, gb, nb_bits) + level;\
00587 level = table[index].level;\
00588 n = table[index].len;\
00589 }\
00590 run= table[index].run;\
00591 SKIP_BITS(name, gb, n)\
00592 }
00593
00594
00603 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00604 int bits, int max_depth)
00605 {
00606 int code;
00607
00608 OPEN_READER(re, s)
00609 UPDATE_CACHE(re, s)
00610
00611 GET_VLC(code, re, s, table, bits, max_depth)
00612
00613 CLOSE_READER(re, s)
00614 return code;
00615 }
00616
00617
00618
00619 #ifdef TRACE
00620 static inline void print_bin(int bits, int n){
00621 int i;
00622
00623 for(i=n-1; i>=0; i--){
00624 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00625 }
00626 for(i=n; i<24; i++)
00627 av_log(NULL, AV_LOG_DEBUG, " ");
00628 }
00629
00630 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00631 int r= get_bits(s, n);
00632
00633 print_bin(r, n);
00634 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
00635 return r;
00636 }
00637 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
00638 int show= show_bits(s, 24);
00639 int pos= get_bits_count(s);
00640 int r= get_vlc2(s, table, bits, max_depth);
00641 int len= get_bits_count(s) - pos;
00642 int bits2= show>>(24-len);
00643
00644 print_bin(bits2, len);
00645
00646 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
00647 return r;
00648 }
00649 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00650 int show= show_bits(s, n);
00651 int r= get_xbits(s, n);
00652
00653 print_bin(show, n);
00654 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
00655 return r;
00656 }
00657
00658 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00659 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00660 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00661 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00662 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00663
00664 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00665
00666 #else //TRACE
00667 #define tprintf(p, ...) {}
00668 #endif
00669
00670 static inline int decode012(GetBitContext *gb){
00671 int n;
00672 n = get_bits1(gb);
00673 if (n == 0)
00674 return 0;
00675 else
00676 return get_bits1(gb) + 1;
00677 }
00678
00679 static inline int decode210(GetBitContext *gb){
00680 if (get_bits1(gb))
00681 return 0;
00682 else
00683 return 2 - get_bits1(gb);
00684 }
00685
00686 static inline int get_bits_left(GetBitContext *gb)
00687 {
00688 return gb->size_in_bits - get_bits_count(gb);
00689 }
00690
00691 #endif