00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #ifndef FFMPEG_BITSTREAM_H
00027 #define FFMPEG_BITSTREAM_H
00028
00029 #include <stdint.h>
00030 #include <stdlib.h>
00031 #include <assert.h>
00032 #include "common.h"
00033 #include "bswap.h"
00034 #include "intreadwrite.h"
00035 #include "log.h"
00036
00037 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
00038 # define ALT_BITSTREAM_READER
00039 #endif
00040
00041
00042
00043 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
00044 # ifdef ARCH_ARMV4L
00045 # define A32_BITSTREAM_READER
00046 # else
00047 # define ALT_BITSTREAM_READER
00048
00049
00050 # endif
00051 #endif
00052 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
00053
00054 extern const uint8_t ff_reverse[256];
00055
00056 #if defined(ARCH_X86)
00057
00058 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
00059 asm ("sarl %1, %0\n\t"
00060 : "+r" (a)
00061 : "ic" ((uint8_t)(-s))
00062 );
00063 return a;
00064 }
00065 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
00066 asm ("shrl %1, %0\n\t"
00067 : "+r" (a)
00068 : "ic" ((uint8_t)(-s))
00069 );
00070 return a;
00071 }
00072 #else
00073 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
00074 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
00075 #endif
00076
00077
00078
00079
00080 typedef struct PutBitContext {
00081 #ifdef ALT_BITSTREAM_WRITER
00082 uint8_t *buf, *buf_end;
00083 int index;
00084 #else
00085 uint32_t bit_buf;
00086 int bit_left;
00087 uint8_t *buf, *buf_ptr, *buf_end;
00088 #endif
00089 } PutBitContext;
00090
00091 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
00092 {
00093 if(buffer_size < 0) {
00094 buffer_size = 0;
00095 buffer = NULL;
00096 }
00097
00098 s->buf = buffer;
00099 s->buf_end = s->buf + buffer_size;
00100 #ifdef ALT_BITSTREAM_WRITER
00101 s->index=0;
00102 ((uint32_t*)(s->buf))[0]=0;
00103
00104 #else
00105 s->buf_ptr = s->buf;
00106 s->bit_left=32;
00107 s->bit_buf=0;
00108 #endif
00109 }
00110
00111
00112 static inline int put_bits_count(PutBitContext *s)
00113 {
00114 #ifdef ALT_BITSTREAM_WRITER
00115 return s->index;
00116 #else
00117 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
00118 #endif
00119 }
00120
00121
00122 static inline void flush_put_bits(PutBitContext *s)
00123 {
00124 #ifdef ALT_BITSTREAM_WRITER
00125 align_put_bits(s);
00126 #else
00127 s->bit_buf<<= s->bit_left;
00128 while (s->bit_left < 32) {
00129
00130 *s->buf_ptr++=s->bit_buf >> 24;
00131 s->bit_buf<<=8;
00132 s->bit_left+=8;
00133 }
00134 s->bit_left=32;
00135 s->bit_buf=0;
00136 #endif
00137 }
00138
00139 void align_put_bits(PutBitContext *s);
00140 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
00141 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
00142
00143
00144
00145 typedef struct GetBitContext {
00146 const uint8_t *buffer, *buffer_end;
00147 #ifdef ALT_BITSTREAM_READER
00148 int index;
00149 #elif defined LIBMPEG2_BITSTREAM_READER
00150 uint8_t *buffer_ptr;
00151 uint32_t cache;
00152 int bit_count;
00153 #elif defined A32_BITSTREAM_READER
00154 uint32_t *buffer_ptr;
00155 uint32_t cache0;
00156 uint32_t cache1;
00157 int bit_count;
00158 #endif
00159 int size_in_bits;
00160 } GetBitContext;
00161
00162 #define VLC_TYPE int16_t
00163
00164 typedef struct VLC {
00165 int bits;
00166 VLC_TYPE (*table)[2];
00167 int table_size, table_allocated;
00168 } VLC;
00169
00170 typedef struct RL_VLC_ELEM {
00171 int16_t level;
00172 int8_t len;
00173 uint8_t run;
00174 } RL_VLC_ELEM;
00175
00176 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
00177 #define UNALIGNED_STORES_ARE_BAD
00178 #endif
00179
00180
00181 #if defined(ARCH_X86)
00182 # define unaligned16(a) (*(const uint16_t*)(a))
00183 # define unaligned32(a) (*(const uint32_t*)(a))
00184 # define unaligned64(a) (*(const uint64_t*)(a))
00185 #else
00186 # ifdef __GNUC__
00187 # define unaligned(x) \
00188 static inline uint##x##_t unaligned##x(const void *v) { \
00189 struct Unaligned { \
00190 uint##x##_t i; \
00191 } __attribute__((packed)); \
00192 \
00193 return ((const struct Unaligned *) v)->i; \
00194 }
00195 # elif defined(__DECC)
00196 # define unaligned(x) \
00197 static inline uint##x##_t unaligned##x(const void *v) { \
00198 return *(const __unaligned uint##x##_t *) v; \
00199 }
00200 # else
00201 # define unaligned(x) \
00202 static inline uint##x##_t unaligned##x(const void *v) { \
00203 return *(const uint##x##_t *) v; \
00204 }
00205 # endif
00206 unaligned(16)
00207 unaligned(32)
00208 unaligned(64)
00209 #undef unaligned
00210 #endif
00211
00212 #ifndef ALT_BITSTREAM_WRITER
00213 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00214 {
00215 unsigned int bit_buf;
00216 int bit_left;
00217
00218
00219 assert(n == 32 || value < (1U << n));
00220
00221 bit_buf = s->bit_buf;
00222 bit_left = s->bit_left;
00223
00224
00225
00226 if (n < bit_left) {
00227 bit_buf = (bit_buf<<n) | value;
00228 bit_left-=n;
00229 } else {
00230 bit_buf<<=bit_left;
00231 bit_buf |= value >> (n - bit_left);
00232 #ifdef UNALIGNED_STORES_ARE_BAD
00233 if (3 & (intptr_t) s->buf_ptr) {
00234 s->buf_ptr[0] = bit_buf >> 24;
00235 s->buf_ptr[1] = bit_buf >> 16;
00236 s->buf_ptr[2] = bit_buf >> 8;
00237 s->buf_ptr[3] = bit_buf ;
00238 } else
00239 #endif
00240 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
00241
00242 s->buf_ptr+=4;
00243 bit_left+=32 - n;
00244 bit_buf = value;
00245 }
00246
00247 s->bit_buf = bit_buf;
00248 s->bit_left = bit_left;
00249 }
00250 #endif
00251
00252
00253 #ifdef ALT_BITSTREAM_WRITER
00254 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00255 {
00256 # ifdef ALIGNED_BITSTREAM_WRITER
00257 # if defined(ARCH_X86)
00258 asm volatile(
00259 "movl %0, %%ecx \n\t"
00260 "xorl %%eax, %%eax \n\t"
00261 "shrdl %%cl, %1, %%eax \n\t"
00262 "shrl %%cl, %1 \n\t"
00263 "movl %0, %%ecx \n\t"
00264 "shrl $3, %%ecx \n\t"
00265 "andl $0xFFFFFFFC, %%ecx \n\t"
00266 "bswapl %1 \n\t"
00267 "orl %1, (%2, %%ecx) \n\t"
00268 "bswapl %%eax \n\t"
00269 "addl %3, %0 \n\t"
00270 "movl %%eax, 4(%2, %%ecx) \n\t"
00271 : "=&r" (s->index), "=&r" (value)
00272 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
00273 : "%eax", "%ecx"
00274 );
00275 # else
00276 int index= s->index;
00277 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
00278
00279 value<<= 32-n;
00280
00281 ptr[0] |= be2me_32(value>>(index&31));
00282 ptr[1] = be2me_32(value<<(32-(index&31)));
00283
00284 index+= n;
00285 s->index= index;
00286 # endif
00287 # else //ALIGNED_BITSTREAM_WRITER
00288 # if defined(ARCH_X86)
00289 asm volatile(
00290 "movl $7, %%ecx \n\t"
00291 "andl %0, %%ecx \n\t"
00292 "addl %3, %%ecx \n\t"
00293 "negl %%ecx \n\t"
00294 "shll %%cl, %1 \n\t"
00295 "bswapl %1 \n\t"
00296 "movl %0, %%ecx \n\t"
00297 "shrl $3, %%ecx \n\t"
00298 "orl %1, (%%ecx, %2) \n\t"
00299 "addl %3, %0 \n\t"
00300 "movl $0, 4(%%ecx, %2) \n\t"
00301 : "=&r" (s->index), "=&r" (value)
00302 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
00303 : "%ecx"
00304 );
00305 # else
00306 int index= s->index;
00307 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
00308
00309 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
00310 ptr[1] = 0;
00311
00312 index+= n;
00313 s->index= index;
00314 # endif
00315 # endif
00316 }
00317 #endif
00318
00319
00320 static inline uint8_t* pbBufPtr(PutBitContext *s)
00321 {
00322 #ifdef ALT_BITSTREAM_WRITER
00323 return s->buf + (s->index>>3);
00324 #else
00325 return s->buf_ptr;
00326 #endif
00327 }
00328
00333 static inline void skip_put_bytes(PutBitContext *s, int n){
00334 assert((put_bits_count(s)&7)==0);
00335 #ifdef ALT_BITSTREAM_WRITER
00336 FIXME may need some cleaning of the buffer
00337 s->index += n<<3;
00338 #else
00339 assert(s->bit_left==32);
00340 s->buf_ptr += n;
00341 #endif
00342 }
00343
00348 static inline void skip_put_bits(PutBitContext *s, int n){
00349 #ifdef ALT_BITSTREAM_WRITER
00350 s->index += n;
00351 #else
00352 s->bit_left -= n;
00353 s->buf_ptr-= s->bit_left>>5;
00354 s->bit_left &= 31;
00355 #endif
00356 }
00357
00361 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
00362 s->buf_end= s->buf + size;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 #ifdef ALT_BITSTREAM_READER
00411 # define MIN_CACHE_BITS 25
00412
00413 # define OPEN_READER(name, gb)\
00414 int name##_index= (gb)->index;\
00415 int name##_cache= 0;\
00416
00417 # define CLOSE_READER(name, gb)\
00418 (gb)->index= name##_index;\
00419
00420 # ifdef ALT_BITSTREAM_READER_LE
00421 # define UPDATE_CACHE(name, gb)\
00422 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
00423
00424 # define SKIP_CACHE(name, gb, num)\
00425 name##_cache >>= (num);
00426 # else
00427 # define UPDATE_CACHE(name, gb)\
00428 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
00429
00430 # define SKIP_CACHE(name, gb, num)\
00431 name##_cache <<= (num);
00432 # endif
00433
00434
00435 # define SKIP_COUNTER(name, gb, num)\
00436 name##_index += (num);\
00437
00438 # define SKIP_BITS(name, gb, num)\
00439 {\
00440 SKIP_CACHE(name, gb, num)\
00441 SKIP_COUNTER(name, gb, num)\
00442 }\
00443
00444 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
00445 # define LAST_SKIP_CACHE(name, gb, num) ;
00446
00447 # ifdef ALT_BITSTREAM_READER_LE
00448 # define SHOW_UBITS(name, gb, num)\
00449 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
00450
00451 # define SHOW_SBITS(name, gb, num)\
00452 NEG_SSR32((name##_cache)<<(32-(num)), num)
00453 # else
00454 # define SHOW_UBITS(name, gb, num)\
00455 NEG_USR32(name##_cache, num)
00456
00457 # define SHOW_SBITS(name, gb, num)\
00458 NEG_SSR32(name##_cache, num)
00459 # endif
00460
00461 # define GET_CACHE(name, gb)\
00462 ((uint32_t)name##_cache)
00463
00464 static inline int get_bits_count(GetBitContext *s){
00465 return s->index;
00466 }
00467
00468 static inline void skip_bits_long(GetBitContext *s, int n){
00469 s->index += n;
00470 }
00471
00472 #elif defined LIBMPEG2_BITSTREAM_READER
00473
00474
00475 # define MIN_CACHE_BITS 17
00476
00477 # define OPEN_READER(name, gb)\
00478 int name##_bit_count=(gb)->bit_count;\
00479 int name##_cache= (gb)->cache;\
00480 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00481
00482 # define CLOSE_READER(name, gb)\
00483 (gb)->bit_count= name##_bit_count;\
00484 (gb)->cache= name##_cache;\
00485 (gb)->buffer_ptr= name##_buffer_ptr;\
00486
00487 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
00488
00489 # define UPDATE_CACHE(name, gb)\
00490 if(name##_bit_count >= 0){\
00491 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
00492 name##_buffer_ptr += 2;\
00493 name##_bit_count-= 16;\
00494 }\
00495
00496 #else
00497
00498 # define UPDATE_CACHE(name, gb)\
00499 if(name##_bit_count >= 0){\
00500 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
00501 name##_buffer_ptr+=2;\
00502 name##_bit_count-= 16;\
00503 }\
00504
00505 #endif
00506
00507 # define SKIP_CACHE(name, gb, num)\
00508 name##_cache <<= (num);\
00509
00510 # define SKIP_COUNTER(name, gb, num)\
00511 name##_bit_count += (num);\
00512
00513 # define SKIP_BITS(name, gb, num)\
00514 {\
00515 SKIP_CACHE(name, gb, num)\
00516 SKIP_COUNTER(name, gb, num)\
00517 }\
00518
00519 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00520 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00521
00522 # define SHOW_UBITS(name, gb, num)\
00523 NEG_USR32(name##_cache, num)
00524
00525 # define SHOW_SBITS(name, gb, num)\
00526 NEG_SSR32(name##_cache, num)
00527
00528 # define GET_CACHE(name, gb)\
00529 ((uint32_t)name##_cache)
00530
00531 static inline int get_bits_count(GetBitContext *s){
00532 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
00533 }
00534
00535 static inline void skip_bits_long(GetBitContext *s, int n){
00536 OPEN_READER(re, s)
00537 re_bit_count += n;
00538 re_buffer_ptr += 2*(re_bit_count>>4);
00539 re_bit_count &= 15;
00540 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
00541 UPDATE_CACHE(re, s)
00542 CLOSE_READER(re, s)
00543 }
00544
00545 #elif defined A32_BITSTREAM_READER
00546
00547 # define MIN_CACHE_BITS 32
00548
00549 # define OPEN_READER(name, gb)\
00550 int name##_bit_count=(gb)->bit_count;\
00551 uint32_t name##_cache0= (gb)->cache0;\
00552 uint32_t name##_cache1= (gb)->cache1;\
00553 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00554
00555 # define CLOSE_READER(name, gb)\
00556 (gb)->bit_count= name##_bit_count;\
00557 (gb)->cache0= name##_cache0;\
00558 (gb)->cache1= name##_cache1;\
00559 (gb)->buffer_ptr= name##_buffer_ptr;\
00560
00561 # define UPDATE_CACHE(name, gb)\
00562 if(name##_bit_count > 0){\
00563 const uint32_t next= be2me_32( *name##_buffer_ptr );\
00564 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
00565 name##_cache1 |= next<<name##_bit_count;\
00566 name##_buffer_ptr++;\
00567 name##_bit_count-= 32;\
00568 }\
00569
00570 #if defined(ARCH_X86)
00571 # define SKIP_CACHE(name, gb, num)\
00572 asm(\
00573 "shldl %2, %1, %0 \n\t"\
00574 "shll %2, %1 \n\t"\
00575 : "+r" (name##_cache0), "+r" (name##_cache1)\
00576 : "Ic" ((uint8_t)(num))\
00577 );
00578 #else
00579 # define SKIP_CACHE(name, gb, num)\
00580 name##_cache0 <<= (num);\
00581 name##_cache0 |= NEG_USR32(name##_cache1,num);\
00582 name##_cache1 <<= (num);
00583 #endif
00584
00585 # define SKIP_COUNTER(name, gb, num)\
00586 name##_bit_count += (num);\
00587
00588 # define SKIP_BITS(name, gb, num)\
00589 {\
00590 SKIP_CACHE(name, gb, num)\
00591 SKIP_COUNTER(name, gb, num)\
00592 }\
00593
00594 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00595 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00596
00597 # define SHOW_UBITS(name, gb, num)\
00598 NEG_USR32(name##_cache0, num)
00599
00600 # define SHOW_SBITS(name, gb, num)\
00601 NEG_SSR32(name##_cache0, num)
00602
00603 # define GET_CACHE(name, gb)\
00604 (name##_cache0)
00605
00606 static inline int get_bits_count(GetBitContext *s){
00607 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
00608 }
00609
00610 static inline void skip_bits_long(GetBitContext *s, int n){
00611 OPEN_READER(re, s)
00612 re_bit_count += n;
00613 re_buffer_ptr += re_bit_count>>5;
00614 re_bit_count &= 31;
00615 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
00616 re_cache1 = 0;
00617 UPDATE_CACHE(re, s)
00618 CLOSE_READER(re, s)
00619 }
00620
00621 #endif
00622
00629 static inline int get_xbits(GetBitContext *s, int n){
00630 register int sign;
00631 register int32_t cache;
00632 OPEN_READER(re, s)
00633 UPDATE_CACHE(re, s)
00634 cache = GET_CACHE(re,s);
00635 sign=(~cache)>>31;
00636 LAST_SKIP_BITS(re, s, n)
00637 CLOSE_READER(re, s)
00638 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
00639 }
00640
00641 static inline int get_sbits(GetBitContext *s, int n){
00642 register int tmp;
00643 OPEN_READER(re, s)
00644 UPDATE_CACHE(re, s)
00645 tmp= SHOW_SBITS(re, s, n);
00646 LAST_SKIP_BITS(re, s, n)
00647 CLOSE_READER(re, s)
00648 return tmp;
00649 }
00650
00655 static inline unsigned int get_bits(GetBitContext *s, int n){
00656 register int tmp;
00657 OPEN_READER(re, s)
00658 UPDATE_CACHE(re, s)
00659 tmp= SHOW_UBITS(re, s, n);
00660 LAST_SKIP_BITS(re, s, n)
00661 CLOSE_READER(re, s)
00662 return tmp;
00663 }
00664
00669 static inline unsigned int show_bits(GetBitContext *s, int n){
00670 register int tmp;
00671 OPEN_READER(re, s)
00672 UPDATE_CACHE(re, s)
00673 tmp= SHOW_UBITS(re, s, n);
00674
00675 return tmp;
00676 }
00677
00678 static inline void skip_bits(GetBitContext *s, int n){
00679
00680 OPEN_READER(re, s)
00681 UPDATE_CACHE(re, s)
00682 LAST_SKIP_BITS(re, s, n)
00683 CLOSE_READER(re, s)
00684 }
00685
00686 static inline unsigned int get_bits1(GetBitContext *s){
00687 #ifdef ALT_BITSTREAM_READER
00688 int index= s->index;
00689 uint8_t result= s->buffer[ index>>3 ];
00690 #ifdef ALT_BITSTREAM_READER_LE
00691 result>>= (index&0x07);
00692 result&= 1;
00693 #else
00694 result<<= (index&0x07);
00695 result>>= 8 - 1;
00696 #endif
00697 index++;
00698 s->index= index;
00699
00700 return result;
00701 #else
00702 return get_bits(s, 1);
00703 #endif
00704 }
00705
00706 static inline unsigned int show_bits1(GetBitContext *s){
00707 return show_bits(s, 1);
00708 }
00709
00710 static inline void skip_bits1(GetBitContext *s){
00711 skip_bits(s, 1);
00712 }
00713
00717 static inline unsigned int get_bits_long(GetBitContext *s, int n){
00718 if(n<=17) return get_bits(s, n);
00719 else{
00720 #ifdef ALT_BITSTREAM_READER_LE
00721 int ret= get_bits(s, 16);
00722 return ret | (get_bits(s, n-16) << 16);
00723 #else
00724 int ret= get_bits(s, 16) << (n-16);
00725 return ret | get_bits(s, n-16);
00726 #endif
00727 }
00728 }
00729
00733 static inline unsigned int show_bits_long(GetBitContext *s, int n){
00734 if(n<=17) return show_bits(s, n);
00735 else{
00736 GetBitContext gb= *s;
00737 int ret= get_bits_long(s, n);
00738 *s= gb;
00739 return ret;
00740 }
00741 }
00742
00743 static inline int check_marker(GetBitContext *s, const char *msg)
00744 {
00745 int bit= get_bits1(s);
00746 if(!bit)
00747 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00748
00749 return bit;
00750 }
00751
00758 static inline void init_get_bits(GetBitContext *s,
00759 const uint8_t *buffer, int bit_size)
00760 {
00761 int buffer_size= (bit_size+7)>>3;
00762 if(buffer_size < 0 || bit_size < 0) {
00763 buffer_size = bit_size = 0;
00764 buffer = NULL;
00765 }
00766
00767 s->buffer= buffer;
00768 s->size_in_bits= bit_size;
00769 s->buffer_end= buffer + buffer_size;
00770 #ifdef ALT_BITSTREAM_READER
00771 s->index=0;
00772 #elif defined LIBMPEG2_BITSTREAM_READER
00773 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
00774 s->bit_count = 16 + 8*((intptr_t)buffer&1);
00775 skip_bits_long(s, 0);
00776 #elif defined A32_BITSTREAM_READER
00777 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
00778 s->bit_count = 32 + 8*((intptr_t)buffer&3);
00779 skip_bits_long(s, 0);
00780 #endif
00781 }
00782
00783 static inline void align_get_bits(GetBitContext *s)
00784 {
00785 int n= (-get_bits_count(s)) & 7;
00786 if(n) skip_bits(s, n);
00787 }
00788
00789 #define init_vlc(vlc, nb_bits, nb_codes,\
00790 bits, bits_wrap, bits_size,\
00791 codes, codes_wrap, codes_size,\
00792 flags)\
00793 init_vlc_sparse(vlc, nb_bits, nb_codes,\
00794 bits, bits_wrap, bits_size,\
00795 codes, codes_wrap, codes_size,\
00796 NULL, 0, 0, flags)
00797
00798 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00799 const void *bits, int bits_wrap, int bits_size,
00800 const void *codes, int codes_wrap, int codes_size,
00801 const void *symbols, int symbols_wrap, int symbols_size,
00802 int flags);
00803 #define INIT_VLC_USE_STATIC 1
00804 #define INIT_VLC_LE 2
00805 void free_vlc(VLC *vlc);
00806
00813 #define GET_VLC(code, name, gb, table, bits, max_depth)\
00814 {\
00815 int n, index, nb_bits;\
00816 \
00817 index= SHOW_UBITS(name, gb, bits);\
00818 code = table[index][0];\
00819 n = table[index][1];\
00820 \
00821 if(max_depth > 1 && n < 0){\
00822 LAST_SKIP_BITS(name, gb, bits)\
00823 UPDATE_CACHE(name, gb)\
00824 \
00825 nb_bits = -n;\
00826 \
00827 index= SHOW_UBITS(name, gb, nb_bits) + code;\
00828 code = table[index][0];\
00829 n = table[index][1];\
00830 if(max_depth > 2 && n < 0){\
00831 LAST_SKIP_BITS(name, gb, nb_bits)\
00832 UPDATE_CACHE(name, gb)\
00833 \
00834 nb_bits = -n;\
00835 \
00836 index= SHOW_UBITS(name, gb, nb_bits) + code;\
00837 code = table[index][0];\
00838 n = table[index][1];\
00839 }\
00840 }\
00841 SKIP_BITS(name, gb, n)\
00842 }
00843
00844 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
00845 {\
00846 int n, index, nb_bits;\
00847 \
00848 index= SHOW_UBITS(name, gb, bits);\
00849 level = table[index].level;\
00850 n = table[index].len;\
00851 \
00852 if(max_depth > 1 && n < 0){\
00853 SKIP_BITS(name, gb, bits)\
00854 if(need_update){\
00855 UPDATE_CACHE(name, gb)\
00856 }\
00857 \
00858 nb_bits = -n;\
00859 \
00860 index= SHOW_UBITS(name, gb, nb_bits) + level;\
00861 level = table[index].level;\
00862 n = table[index].len;\
00863 }\
00864 run= table[index].run;\
00865 SKIP_BITS(name, gb, n)\
00866 }
00867
00868
00877 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00878 int bits, int max_depth)
00879 {
00880 int code;
00881
00882 OPEN_READER(re, s)
00883 UPDATE_CACHE(re, s)
00884
00885 GET_VLC(code, re, s, table, bits, max_depth)
00886
00887 CLOSE_READER(re, s)
00888 return code;
00889 }
00890
00891
00892
00893 #ifdef TRACE
00894 static inline void print_bin(int bits, int n){
00895 int i;
00896
00897 for(i=n-1; i>=0; i--){
00898 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00899 }
00900 for(i=n; i<24; i++)
00901 av_log(NULL, AV_LOG_DEBUG, " ");
00902 }
00903
00904 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00905 int r= get_bits(s, n);
00906
00907 print_bin(r, n);
00908 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);
00909 return r;
00910 }
00911 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
00912 int show= show_bits(s, 24);
00913 int pos= get_bits_count(s);
00914 int r= get_vlc2(s, table, bits, max_depth);
00915 int len= get_bits_count(s) - pos;
00916 int bits2= show>>(24-len);
00917
00918 print_bin(bits2, len);
00919
00920 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
00921 return r;
00922 }
00923 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00924 int show= show_bits(s, n);
00925 int r= get_xbits(s, n);
00926
00927 print_bin(show, n);
00928 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);
00929 return r;
00930 }
00931
00932 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00933 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00934 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00935 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00936 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00937
00938 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00939
00940 #else //TRACE
00941 #define tprintf(p, ...) {}
00942 #endif
00943
00944 static inline int decode012(GetBitContext *gb){
00945 int n;
00946 n = get_bits1(gb);
00947 if (n == 0)
00948 return 0;
00949 else
00950 return get_bits1(gb) + 1;
00951 }
00952
00953 static inline int decode210(GetBitContext *gb){
00954 if (get_bits1(gb))
00955 return 0;
00956 else
00957 return 2 - get_bits1(gb);
00958 }
00959
00960 #endif