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