• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/bitstream.h

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #ifndef AVCODEC_BITSTREAM_H
00027 #define AVCODEC_BITSTREAM_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 
00037 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
00038 #   define ALT_BITSTREAM_READER
00039 #endif
00040 
00041 //#define ALT_BITSTREAM_WRITER
00042 //#define ALIGNED_BITSTREAM_WRITER
00043 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
00044 #   if ARCH_ARM
00045 #       define A32_BITSTREAM_READER
00046 #   else
00047 #       define ALT_BITSTREAM_READER
00048 //#define LIBMPEG2_BITSTREAM_READER
00049 //#define A32_BITSTREAM_READER
00050 #   endif
00051 #endif
00052 
00053 extern const uint8_t ff_reverse[256];
00054 
00055 #if ARCH_X86
00056 // avoid +32 for shift optimization (gcc should do that ...)
00057 static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
00058     __asm__ ("sarl %1, %0\n\t"
00059          : "+r" (a)
00060          : "ic" ((uint8_t)(-s))
00061     );
00062     return a;
00063 }
00064 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
00065     __asm__ ("shrl %1, %0\n\t"
00066          : "+r" (a)
00067          : "ic" ((uint8_t)(-s))
00068     );
00069     return a;
00070 }
00071 #else
00072 #    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
00073 #    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
00074 #endif
00075 
00076 /* bit output */
00077 
00078 /* buf and buf_end must be present and used by every alternative writer. */
00079 typedef struct PutBitContext {
00080 #ifdef ALT_BITSTREAM_WRITER
00081     uint8_t *buf, *buf_end;
00082     int index;
00083 #else
00084     uint32_t bit_buf;
00085     int bit_left;
00086     uint8_t *buf, *buf_ptr, *buf_end;
00087 #endif
00088     int size_in_bits;
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->size_in_bits= 8*buffer_size;
00099     s->buf = buffer;
00100     s->buf_end = s->buf + buffer_size;
00101 #ifdef ALT_BITSTREAM_WRITER
00102     s->index=0;
00103     ((uint32_t*)(s->buf))[0]=0;
00104 //    memset(buffer, 0, buffer_size);
00105 #else
00106     s->buf_ptr = s->buf;
00107     s->bit_left=32;
00108     s->bit_buf=0;
00109 #endif
00110 }
00111 
00112 /* return the number of bits output */
00113 static inline int put_bits_count(PutBitContext *s)
00114 {
00115 #ifdef ALT_BITSTREAM_WRITER
00116     return s->index;
00117 #else
00118     return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
00119 #endif
00120 }
00121 
00122 /* pad the end of the output stream with zeros */
00123 static inline void flush_put_bits(PutBitContext *s)
00124 {
00125 #ifdef ALT_BITSTREAM_WRITER
00126     align_put_bits(s);
00127 #else
00128 #ifndef BITSTREAM_WRITER_LE
00129     s->bit_buf<<= s->bit_left;
00130 #endif
00131     while (s->bit_left < 32) {
00132         /* XXX: should test end of buffer */
00133 #ifdef BITSTREAM_WRITER_LE
00134         *s->buf_ptr++=s->bit_buf;
00135         s->bit_buf>>=8;
00136 #else
00137         *s->buf_ptr++=s->bit_buf >> 24;
00138         s->bit_buf<<=8;
00139 #endif
00140         s->bit_left+=8;
00141     }
00142     s->bit_left=32;
00143     s->bit_buf=0;
00144 #endif
00145 }
00146 
00147 void align_put_bits(PutBitContext *s);
00148 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
00149 void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
00150 
00151 /* bit input */
00152 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
00153 typedef struct GetBitContext {
00154     const uint8_t *buffer, *buffer_end;
00155 #ifdef ALT_BITSTREAM_READER
00156     int index;
00157 #elif defined LIBMPEG2_BITSTREAM_READER
00158     uint8_t *buffer_ptr;
00159     uint32_t cache;
00160     int bit_count;
00161 #elif defined A32_BITSTREAM_READER
00162     uint32_t *buffer_ptr;
00163     uint32_t cache0;
00164     uint32_t cache1;
00165     int bit_count;
00166 #endif
00167     int size_in_bits;
00168 } GetBitContext;
00169 
00170 #define VLC_TYPE int16_t
00171 
00172 typedef struct VLC {
00173     int bits;
00174     VLC_TYPE (*table)[2]; 
00175     int table_size, table_allocated;
00176 } VLC;
00177 
00178 typedef struct RL_VLC_ELEM {
00179     int16_t level;
00180     int8_t len;
00181     uint8_t run;
00182 } RL_VLC_ELEM;
00183 
00184 #ifndef ALT_BITSTREAM_WRITER
00185 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00186 {
00187     unsigned int bit_buf;
00188     int bit_left;
00189 
00190     //    printf("put_bits=%d %x\n", n, value);
00191     assert(n == 32 || value < (1U << n));
00192 
00193     bit_buf = s->bit_buf;
00194     bit_left = s->bit_left;
00195 
00196     //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
00197     /* XXX: optimize */
00198 #ifdef BITSTREAM_WRITER_LE
00199     bit_buf |= value << (32 - bit_left);
00200     if (n >= bit_left) {
00201 #if !HAVE_FAST_UNALIGNED
00202         if (3 & (intptr_t) s->buf_ptr) {
00203             AV_WL32(s->buf_ptr, bit_buf);
00204         } else
00205 #endif
00206         *(uint32_t *)s->buf_ptr = le2me_32(bit_buf);
00207         s->buf_ptr+=4;
00208         bit_buf = (bit_left==32)?0:value >> bit_left;
00209         bit_left+=32;
00210     }
00211     bit_left-=n;
00212 #else
00213     if (n < bit_left) {
00214         bit_buf = (bit_buf<<n) | value;
00215         bit_left-=n;
00216     } else {
00217         bit_buf<<=bit_left;
00218         bit_buf |= value >> (n - bit_left);
00219 #if !HAVE_FAST_UNALIGNED
00220         if (3 & (intptr_t) s->buf_ptr) {
00221             AV_WB32(s->buf_ptr, bit_buf);
00222         } else
00223 #endif
00224         *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
00225         //printf("bitbuf = %08x\n", bit_buf);
00226         s->buf_ptr+=4;
00227         bit_left+=32 - n;
00228         bit_buf = value;
00229     }
00230 #endif
00231 
00232     s->bit_buf = bit_buf;
00233     s->bit_left = bit_left;
00234 }
00235 #endif
00236 
00237 
00238 #ifdef ALT_BITSTREAM_WRITER
00239 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
00240 {
00241 #    ifdef ALIGNED_BITSTREAM_WRITER
00242 #        if ARCH_X86
00243     __asm__ volatile(
00244         "movl %0, %%ecx                 \n\t"
00245         "xorl %%eax, %%eax              \n\t"
00246         "shrdl %%cl, %1, %%eax          \n\t"
00247         "shrl %%cl, %1                  \n\t"
00248         "movl %0, %%ecx                 \n\t"
00249         "shrl $3, %%ecx                 \n\t"
00250         "andl $0xFFFFFFFC, %%ecx        \n\t"
00251         "bswapl %1                      \n\t"
00252         "orl %1, (%2, %%ecx)            \n\t"
00253         "bswapl %%eax                   \n\t"
00254         "addl %3, %0                    \n\t"
00255         "movl %%eax, 4(%2, %%ecx)       \n\t"
00256         : "=&r" (s->index), "=&r" (value)
00257         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
00258         : "%eax", "%ecx"
00259     );
00260 #        else
00261     int index= s->index;
00262     uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
00263 
00264     value<<= 32-n;
00265 
00266     ptr[0] |= be2me_32(value>>(index&31));
00267     ptr[1]  = be2me_32(value<<(32-(index&31)));
00268 //if(n>24) printf("%d %d\n", n, value);
00269     index+= n;
00270     s->index= index;
00271 #        endif
00272 #    else //ALIGNED_BITSTREAM_WRITER
00273 #        if ARCH_X86
00274     __asm__ volatile(
00275         "movl $7, %%ecx                 \n\t"
00276         "andl %0, %%ecx                 \n\t"
00277         "addl %3, %%ecx                 \n\t"
00278         "negl %%ecx                     \n\t"
00279         "shll %%cl, %1                  \n\t"
00280         "bswapl %1                      \n\t"
00281         "movl %0, %%ecx                 \n\t"
00282         "shrl $3, %%ecx                 \n\t"
00283         "orl %1, (%%ecx, %2)            \n\t"
00284         "addl %3, %0                    \n\t"
00285         "movl $0, 4(%%ecx, %2)          \n\t"
00286         : "=&r" (s->index), "=&r" (value)
00287         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
00288         : "%ecx"
00289     );
00290 #        else
00291     int index= s->index;
00292     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
00293 
00294     ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
00295     ptr[1] = 0;
00296 //if(n>24) printf("%d %d\n", n, value);
00297     index+= n;
00298     s->index= index;
00299 #        endif
00300 #    endif //!ALIGNED_BITSTREAM_WRITER
00301 }
00302 #endif
00303 
00304 static inline void put_sbits(PutBitContext *pb, int bits, int32_t val)
00305 {
00306     assert(bits >= 0 && bits <= 31);
00307 
00308     put_bits(pb, bits, val & ((1<<bits)-1));
00309 }
00310 
00311 
00312 static inline uint8_t* pbBufPtr(PutBitContext *s)
00313 {
00314 #ifdef ALT_BITSTREAM_WRITER
00315         return s->buf + (s->index>>3);
00316 #else
00317         return s->buf_ptr;
00318 #endif
00319 }
00320 
00325 static inline void skip_put_bytes(PutBitContext *s, int n){
00326         assert((put_bits_count(s)&7)==0);
00327 #ifdef ALT_BITSTREAM_WRITER
00328         FIXME may need some cleaning of the buffer
00329         s->index += n<<3;
00330 #else
00331         assert(s->bit_left==32);
00332         s->buf_ptr += n;
00333 #endif
00334 }
00335 
00340 static inline void skip_put_bits(PutBitContext *s, int n){
00341 #ifdef ALT_BITSTREAM_WRITER
00342     s->index += n;
00343 #else
00344     s->bit_left -= n;
00345     s->buf_ptr-= s->bit_left>>5;
00346     s->bit_left &= 31;
00347 #endif
00348 }
00349 
00353 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
00354     s->buf_end= s->buf + size;
00355 }
00356 
00357 /* Bitstream reader API docs:
00358 name
00359     arbitrary name which is used as prefix for the internal variables
00360 
00361 gb
00362     getbitcontext
00363 
00364 OPEN_READER(name, gb)
00365     loads gb into local variables
00366 
00367 CLOSE_READER(name, gb)
00368     stores local vars in gb
00369 
00370 UPDATE_CACHE(name, gb)
00371     refills the internal cache from the bitstream
00372     after this call at least MIN_CACHE_BITS will be available,
00373 
00374 GET_CACHE(name, gb)
00375     will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
00376 
00377 SHOW_UBITS(name, gb, num)
00378     will return the next num bits
00379 
00380 SHOW_SBITS(name, gb, num)
00381     will return the next num bits and do sign extension
00382 
00383 SKIP_BITS(name, gb, num)
00384     will skip over the next num bits
00385     note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
00386 
00387 SKIP_CACHE(name, gb, num)
00388     will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
00389 
00390 SKIP_COUNTER(name, gb, num)
00391     will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
00392 
00393 LAST_SKIP_CACHE(name, gb, num)
00394     will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
00395 
00396 LAST_SKIP_BITS(name, gb, num)
00397     is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
00398 
00399 for examples see get_bits, show_bits, skip_bits, get_vlc
00400 */
00401 
00402 #ifdef ALT_BITSTREAM_READER
00403 #   define MIN_CACHE_BITS 25
00404 
00405 #   define OPEN_READER(name, gb)\
00406         int name##_index= (gb)->index;\
00407         int name##_cache= 0;\
00408 
00409 #   define CLOSE_READER(name, gb)\
00410         (gb)->index= name##_index;\
00411 
00412 # ifdef ALT_BITSTREAM_READER_LE
00413 #   define UPDATE_CACHE(name, gb)\
00414         name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
00415 
00416 #   define SKIP_CACHE(name, gb, num)\
00417         name##_cache >>= (num);
00418 # else
00419 #   define UPDATE_CACHE(name, gb)\
00420         name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
00421 
00422 #   define SKIP_CACHE(name, gb, num)\
00423         name##_cache <<= (num);
00424 # endif
00425 
00426 // FIXME name?
00427 #   define SKIP_COUNTER(name, gb, num)\
00428         name##_index += (num);\
00429 
00430 #   define SKIP_BITS(name, gb, num)\
00431         {\
00432             SKIP_CACHE(name, gb, num)\
00433             SKIP_COUNTER(name, gb, num)\
00434         }\
00435 
00436 #   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
00437 #   define LAST_SKIP_CACHE(name, gb, num) ;
00438 
00439 # ifdef ALT_BITSTREAM_READER_LE
00440 #   define SHOW_UBITS(name, gb, num)\
00441         ((name##_cache) & (NEG_USR32(0xffffffff,num)))
00442 
00443 #   define SHOW_SBITS(name, gb, num)\
00444         NEG_SSR32((name##_cache)<<(32-(num)), num)
00445 # else
00446 #   define SHOW_UBITS(name, gb, num)\
00447         NEG_USR32(name##_cache, num)
00448 
00449 #   define SHOW_SBITS(name, gb, num)\
00450         NEG_SSR32(name##_cache, num)
00451 # endif
00452 
00453 #   define GET_CACHE(name, gb)\
00454         ((uint32_t)name##_cache)
00455 
00456 static inline int get_bits_count(GetBitContext *s){
00457     return s->index;
00458 }
00459 
00460 static inline void skip_bits_long(GetBitContext *s, int n){
00461     s->index += n;
00462 }
00463 
00464 #elif defined LIBMPEG2_BITSTREAM_READER
00465 //libmpeg2 like reader
00466 
00467 #   define MIN_CACHE_BITS 17
00468 
00469 #   define OPEN_READER(name, gb)\
00470         int name##_bit_count=(gb)->bit_count;\
00471         int name##_cache= (gb)->cache;\
00472         uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00473 
00474 #   define CLOSE_READER(name, gb)\
00475         (gb)->bit_count= name##_bit_count;\
00476         (gb)->cache= name##_cache;\
00477         (gb)->buffer_ptr= name##_buffer_ptr;\
00478 
00479 #   define UPDATE_CACHE(name, gb)\
00480     if(name##_bit_count >= 0){\
00481         name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
00482         name##_buffer_ptr+=2;\
00483         name##_bit_count-= 16;\
00484     }\
00485 
00486 #   define SKIP_CACHE(name, gb, num)\
00487         name##_cache <<= (num);\
00488 
00489 #   define SKIP_COUNTER(name, gb, num)\
00490         name##_bit_count += (num);\
00491 
00492 #   define SKIP_BITS(name, gb, num)\
00493         {\
00494             SKIP_CACHE(name, gb, num)\
00495             SKIP_COUNTER(name, gb, num)\
00496         }\
00497 
00498 #   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00499 #   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00500 
00501 #   define SHOW_UBITS(name, gb, num)\
00502         NEG_USR32(name##_cache, num)
00503 
00504 #   define SHOW_SBITS(name, gb, num)\
00505         NEG_SSR32(name##_cache, num)
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->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
00512 }
00513 
00514 static inline void skip_bits_long(GetBitContext *s, int n){
00515     OPEN_READER(re, s)
00516     re_bit_count += n;
00517     re_buffer_ptr += 2*(re_bit_count>>4);
00518     re_bit_count &= 15;
00519     re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
00520     UPDATE_CACHE(re, s)
00521     CLOSE_READER(re, s)
00522 }
00523 
00524 #elif defined A32_BITSTREAM_READER
00525 
00526 #   define MIN_CACHE_BITS 32
00527 
00528 #   define OPEN_READER(name, gb)\
00529         int name##_bit_count=(gb)->bit_count;\
00530         uint32_t name##_cache0= (gb)->cache0;\
00531         uint32_t name##_cache1= (gb)->cache1;\
00532         uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
00533 
00534 #   define CLOSE_READER(name, gb)\
00535         (gb)->bit_count= name##_bit_count;\
00536         (gb)->cache0= name##_cache0;\
00537         (gb)->cache1= name##_cache1;\
00538         (gb)->buffer_ptr= name##_buffer_ptr;\
00539 
00540 #   define UPDATE_CACHE(name, gb)\
00541     if(name##_bit_count > 0){\
00542         const uint32_t next= be2me_32( *name##_buffer_ptr );\
00543         name##_cache0 |= NEG_USR32(next,name##_bit_count);\
00544         name##_cache1 |= next<<name##_bit_count;\
00545         name##_buffer_ptr++;\
00546         name##_bit_count-= 32;\
00547     }\
00548 
00549 #if ARCH_X86
00550 #   define SKIP_CACHE(name, gb, num)\
00551         __asm__(\
00552             "shldl %2, %1, %0          \n\t"\
00553             "shll %2, %1               \n\t"\
00554             : "+r" (name##_cache0), "+r" (name##_cache1)\
00555             : "Ic" ((uint8_t)(num))\
00556            );
00557 #else
00558 #   define SKIP_CACHE(name, gb, num)\
00559         name##_cache0 <<= (num);\
00560         name##_cache0 |= NEG_USR32(name##_cache1,num);\
00561         name##_cache1 <<= (num);
00562 #endif
00563 
00564 #   define SKIP_COUNTER(name, gb, num)\
00565         name##_bit_count += (num);\
00566 
00567 #   define SKIP_BITS(name, gb, num)\
00568         {\
00569             SKIP_CACHE(name, gb, num)\
00570             SKIP_COUNTER(name, gb, num)\
00571         }\
00572 
00573 #   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
00574 #   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
00575 
00576 #   define SHOW_UBITS(name, gb, num)\
00577         NEG_USR32(name##_cache0, num)
00578 
00579 #   define SHOW_SBITS(name, gb, num)\
00580         NEG_SSR32(name##_cache0, num)
00581 
00582 #   define GET_CACHE(name, gb)\
00583         (name##_cache0)
00584 
00585 static inline int get_bits_count(GetBitContext *s){
00586     return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
00587 }
00588 
00589 static inline void skip_bits_long(GetBitContext *s, int n){
00590     OPEN_READER(re, s)
00591     re_bit_count += n;
00592     re_buffer_ptr += re_bit_count>>5;
00593     re_bit_count &= 31;
00594     re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
00595     re_cache1 = 0;
00596     UPDATE_CACHE(re, s)
00597     CLOSE_READER(re, s)
00598 }
00599 
00600 #endif
00601 
00608 static inline int get_xbits(GetBitContext *s, int n){
00609     register int sign;
00610     register int32_t cache;
00611     OPEN_READER(re, s)
00612     UPDATE_CACHE(re, s)
00613     cache = GET_CACHE(re,s);
00614     sign=(~cache)>>31;
00615     LAST_SKIP_BITS(re, s, n)
00616     CLOSE_READER(re, s)
00617     return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
00618 }
00619 
00620 static inline int get_sbits(GetBitContext *s, int n){
00621     register int tmp;
00622     OPEN_READER(re, s)
00623     UPDATE_CACHE(re, s)
00624     tmp= SHOW_SBITS(re, s, n);
00625     LAST_SKIP_BITS(re, s, n)
00626     CLOSE_READER(re, s)
00627     return tmp;
00628 }
00629 
00634 static inline unsigned int get_bits(GetBitContext *s, int n){
00635     register int tmp;
00636     OPEN_READER(re, s)
00637     UPDATE_CACHE(re, s)
00638     tmp= SHOW_UBITS(re, s, n);
00639     LAST_SKIP_BITS(re, s, n)
00640     CLOSE_READER(re, s)
00641     return tmp;
00642 }
00643 
00648 static inline unsigned int show_bits(GetBitContext *s, int n){
00649     register int tmp;
00650     OPEN_READER(re, s)
00651     UPDATE_CACHE(re, s)
00652     tmp= SHOW_UBITS(re, s, n);
00653 //    CLOSE_READER(re, s)
00654     return tmp;
00655 }
00656 
00657 static inline void skip_bits(GetBitContext *s, int n){
00658  //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
00659     OPEN_READER(re, s)
00660     UPDATE_CACHE(re, s)
00661     LAST_SKIP_BITS(re, s, n)
00662     CLOSE_READER(re, s)
00663 }
00664 
00665 static inline unsigned int get_bits1(GetBitContext *s){
00666 #ifdef ALT_BITSTREAM_READER
00667     int index= s->index;
00668     uint8_t result= s->buffer[ index>>3 ];
00669 #ifdef ALT_BITSTREAM_READER_LE
00670     result>>= (index&0x07);
00671     result&= 1;
00672 #else
00673     result<<= (index&0x07);
00674     result>>= 8 - 1;
00675 #endif
00676     index++;
00677     s->index= index;
00678 
00679     return result;
00680 #else
00681     return get_bits(s, 1);
00682 #endif
00683 }
00684 
00685 static inline unsigned int show_bits1(GetBitContext *s){
00686     return show_bits(s, 1);
00687 }
00688 
00689 static inline void skip_bits1(GetBitContext *s){
00690     skip_bits(s, 1);
00691 }
00692 
00696 static inline unsigned int get_bits_long(GetBitContext *s, int n){
00697     if(n<=17) return get_bits(s, n);
00698     else{
00699 #ifdef ALT_BITSTREAM_READER_LE
00700         int ret= get_bits(s, 16);
00701         return ret | (get_bits(s, n-16) << 16);
00702 #else
00703         int ret= get_bits(s, 16) << (n-16);
00704         return ret | get_bits(s, n-16);
00705 #endif
00706     }
00707 }
00708 
00712 static inline unsigned int show_bits_long(GetBitContext *s, int n){
00713     if(n<=17) return show_bits(s, n);
00714     else{
00715         GetBitContext gb= *s;
00716         int ret= get_bits_long(s, n);
00717         *s= gb;
00718         return ret;
00719     }
00720 }
00721 
00722 static inline int check_marker(GetBitContext *s, const char *msg)
00723 {
00724     int bit= get_bits1(s);
00725     if(!bit)
00726         av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
00727 
00728     return bit;
00729 }
00730 
00737 static inline void init_get_bits(GetBitContext *s,
00738                    const uint8_t *buffer, int bit_size)
00739 {
00740     int buffer_size= (bit_size+7)>>3;
00741     if(buffer_size < 0 || bit_size < 0) {
00742         buffer_size = bit_size = 0;
00743         buffer = NULL;
00744     }
00745 
00746     s->buffer= buffer;
00747     s->size_in_bits= bit_size;
00748     s->buffer_end= buffer + buffer_size;
00749 #ifdef ALT_BITSTREAM_READER
00750     s->index=0;
00751 #elif defined LIBMPEG2_BITSTREAM_READER
00752     s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
00753     s->bit_count = 16 + 8*((intptr_t)buffer&1);
00754     skip_bits_long(s, 0);
00755 #elif defined A32_BITSTREAM_READER
00756     s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
00757     s->bit_count = 32 + 8*((intptr_t)buffer&3);
00758     skip_bits_long(s, 0);
00759 #endif
00760 }
00761 
00762 static inline void align_get_bits(GetBitContext *s)
00763 {
00764     int n= (-get_bits_count(s)) & 7;
00765     if(n) skip_bits(s, n);
00766 }
00767 
00768 #define init_vlc(vlc, nb_bits, nb_codes,\
00769                  bits, bits_wrap, bits_size,\
00770                  codes, codes_wrap, codes_size,\
00771                  flags)\
00772         init_vlc_sparse(vlc, nb_bits, nb_codes,\
00773                  bits, bits_wrap, bits_size,\
00774                  codes, codes_wrap, codes_size,\
00775                  NULL, 0, 0, flags)
00776 
00777 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
00778              const void *bits, int bits_wrap, int bits_size,
00779              const void *codes, int codes_wrap, int codes_size,
00780              const void *symbols, int symbols_wrap, int symbols_size,
00781              int flags);
00782 #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
00783 #define INIT_VLC_LE         2
00784 #define INIT_VLC_USE_NEW_STATIC 4
00785 void free_vlc(VLC *vlc);
00786 
00787 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
00788 {\
00789     static VLC_TYPE table[static_size][2];\
00790     (vlc)->table= table;\
00791     (vlc)->table_allocated= static_size;\
00792     init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
00793 }
00794 
00795 
00802 #define GET_VLC(code, name, gb, table, bits, max_depth)\
00803 {\
00804     int n, index, nb_bits;\
00805 \
00806     index= SHOW_UBITS(name, gb, bits);\
00807     code = table[index][0];\
00808     n    = table[index][1];\
00809 \
00810     if(max_depth > 1 && n < 0){\
00811         LAST_SKIP_BITS(name, gb, bits)\
00812         UPDATE_CACHE(name, gb)\
00813 \
00814         nb_bits = -n;\
00815 \
00816         index= SHOW_UBITS(name, gb, nb_bits) + code;\
00817         code = table[index][0];\
00818         n    = table[index][1];\
00819         if(max_depth > 2 && n < 0){\
00820             LAST_SKIP_BITS(name, gb, nb_bits)\
00821             UPDATE_CACHE(name, gb)\
00822 \
00823             nb_bits = -n;\
00824 \
00825             index= SHOW_UBITS(name, gb, nb_bits) + code;\
00826             code = table[index][0];\
00827             n    = table[index][1];\
00828         }\
00829     }\
00830     SKIP_BITS(name, gb, n)\
00831 }
00832 
00833 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
00834 {\
00835     int n, index, nb_bits;\
00836 \
00837     index= SHOW_UBITS(name, gb, bits);\
00838     level = table[index].level;\
00839     n     = table[index].len;\
00840 \
00841     if(max_depth > 1 && n < 0){\
00842         SKIP_BITS(name, gb, bits)\
00843         if(need_update){\
00844             UPDATE_CACHE(name, gb)\
00845         }\
00846 \
00847         nb_bits = -n;\
00848 \
00849         index= SHOW_UBITS(name, gb, nb_bits) + level;\
00850         level = table[index].level;\
00851         n     = table[index].len;\
00852     }\
00853     run= table[index].run;\
00854     SKIP_BITS(name, gb, n)\
00855 }
00856 
00857 
00866 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
00867                                   int bits, int max_depth)
00868 {
00869     int code;
00870 
00871     OPEN_READER(re, s)
00872     UPDATE_CACHE(re, s)
00873 
00874     GET_VLC(code, re, s, table, bits, max_depth)
00875 
00876     CLOSE_READER(re, s)
00877     return code;
00878 }
00879 
00880 //#define TRACE
00881 
00882 #ifdef TRACE
00883 static inline void print_bin(int bits, int n){
00884     int i;
00885 
00886     for(i=n-1; i>=0; i--){
00887         av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
00888     }
00889     for(i=n; i<24; i++)
00890         av_log(NULL, AV_LOG_DEBUG, " ");
00891 }
00892 
00893 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00894     int r= get_bits(s, n);
00895 
00896     print_bin(r, n);
00897     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);
00898     return r;
00899 }
00900 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
00901     int show= show_bits(s, 24);
00902     int pos= get_bits_count(s);
00903     int r= get_vlc2(s, table, bits, max_depth);
00904     int len= get_bits_count(s) - pos;
00905     int bits2= show>>(24-len);
00906 
00907     print_bin(bits2, len);
00908 
00909     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
00910     return r;
00911 }
00912 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
00913     int show= show_bits(s, n);
00914     int r= get_xbits(s, n);
00915 
00916     print_bin(show, n);
00917     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);
00918     return r;
00919 }
00920 
00921 #define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00922 #define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00923 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00924 #define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00925 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00926 
00927 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
00928 
00929 #else //TRACE
00930 #define tprintf(p, ...) {}
00931 #endif
00932 
00933 static inline int decode012(GetBitContext *gb){
00934     int n;
00935     n = get_bits1(gb);
00936     if (n == 0)
00937         return 0;
00938     else
00939         return get_bits1(gb) + 1;
00940 }
00941 
00942 static inline int decode210(GetBitContext *gb){
00943     if (get_bits1(gb))
00944         return 0;
00945     else
00946         return 2 - get_bits1(gb);
00947 }
00948 
00949 static inline int get_bits_left(GetBitContext *gb)
00950 {
00951     return gb->size_in_bits - get_bits_count(gb);
00952 }
00953 
00954 #endif /* AVCODEC_BITSTREAM_H */

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1