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