3 * common internal api header.
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
13 //#define ALT_BITSTREAM_WRITER
14 //#define ALIGNED_BITSTREAM_WRITER
16 #define ALT_BITSTREAM_READER
17 //#define LIBMPEG2_BITSTREAM_READER
18 //#define A32_BITSTREAM_READER
19 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
22 #define M_PI 3.14159265358979323846
25 #ifdef HAVE_AV_CONFIG_H
26 /* only include the following when compiling package */
47 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
50 #define AVOPTION_CODEC_BOOL(name, help, field) \
51 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
52 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
54 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
55 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
56 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
57 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
58 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
59 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
60 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
61 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
62 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
63 #define AVOPTION_END() AVOPTION_SUB(NULL)
67 extern const struct AVOption avoptions_common[3 + 5];
69 extern const struct AVOption avoptions_common[3];
71 extern const struct AVOption avoptions_workaround_bug[11];
73 #endif /* HAVE_AV_CONFIG_H */
75 /* Suppress restrict if it was not defined in config.h. */
81 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82 # define always_inline __attribute__((always_inline)) inline
84 # define always_inline inline
88 #ifndef attribute_used
89 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90 # define attribute_used __attribute__((used))
92 # define attribute_used
96 #ifndef EMULATE_INTTYPES
97 # include <inttypes.h>
99 typedef signed char int8_t;
100 typedef signed short int16_t;
101 typedef signed int int32_t;
102 typedef unsigned char uint8_t;
103 typedef unsigned short uint16_t;
104 typedef unsigned int uint32_t;
107 typedef signed __int64 int64_t;
108 typedef unsigned __int64 uint64_t;
109 # else /* other OS */
110 typedef signed long long int64_t;
111 typedef unsigned long long uint64_t;
112 # endif /* other OS */
113 #endif /* HAVE_INTTYPES_H */
116 #define INT16_MIN (-0x7fff-1)
120 #define INT16_MAX 0x7fff
124 #define INT64_MIN (-0x7fffffffffffffffLL-1)
128 #define INT64_MAX int64_t_C(9223372036854775807)
132 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
135 #ifdef EMULATE_FAST_INT
136 /* note that we don't emulate 64bit ints */
137 typedef signed char int_fast8_t;
138 typedef signed int int_fast16_t;
139 typedef signed int int_fast32_t;
140 typedef unsigned char uint_fast8_t;
141 typedef unsigned int uint_fast16_t;
142 typedef unsigned int uint_fast32_t;
146 # if INT_MAX != 2147483647
153 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
154 static inline float floorf(float f) {
163 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
164 # define int64_t_C(c) (c ## i64)
165 # define uint64_t_C(c) (c ## i64)
167 # ifdef HAVE_AV_CONFIG_H
168 # define inline __inline
172 # define int64_t_C(c) (c ## LL)
173 # define uint64_t_C(c) (c ## ULL)
174 # endif /* __MINGW32__ */
176 # ifdef HAVE_AV_CONFIG_H
181 # define snprintf _snprintf
182 # define vsnprintf _vsnprintf
185 /* CONFIG_WIN32 end */
186 #elif defined (CONFIG_OS2)
190 #define int64_t_C(c) (c ## LL)
191 #define uint64_t_C(c) (c ## ULL)
194 #ifdef HAVE_AV_CONFIG_H
196 #ifdef USE_FASTMEMCPY
197 #include "fastmemcpy.h"
202 #endif /* HAVE_AV_CONFIG_H */
210 #define int64_t_C(c) (c ## LL)
211 #define uint64_t_C(c) (c ## ULL)
214 #ifdef HAVE_AV_CONFIG_H
216 # ifdef USE_FASTMEMCPY
217 # include "fastmemcpy.h"
219 # endif /* HAVE_AV_CONFIG_H */
221 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
223 #ifdef HAVE_AV_CONFIG_H
227 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
228 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
229 # define MANGLE(a) "_" #a
231 # define MANGLE(a) #a
242 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
244 inline void dprintf(const char* fmt,...) {}
249 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
251 # define dprintf(fmt,...)
254 # endif /* !CONFIG_WIN32 */
256 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
258 //rounded divison & shift
259 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
261 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
262 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
264 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
265 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
267 extern const uint32_t inverse[256];
269 #if defined(ARCH_X86) || defined(ARCH_X86_64)
270 # define FASTDIV(a,b) \
275 :"=d"(ret),"=a"(dmy)\
276 :"1"(a),"g"(inverse[b])\
280 #elif defined(CONFIG_FASTDIV)
281 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
283 # define FASTDIV(a,b) ((a)/(b))
286 #if defined(ARCH_X86) || defined(ARCH_X86_64)
287 // avoid +32 for shift optimization (gcc should do that ...)
288 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
289 asm ("sarl %1, %0\n\t"
291 : "ic" ((uint8_t)(-s))
295 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
296 asm ("shrl %1, %0\n\t"
298 : "ic" ((uint8_t)(-s))
303 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
304 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
309 /* buf and buf_end must be present and used by every alternative writer. */
310 typedef struct PutBitContext {
311 #ifdef ALT_BITSTREAM_WRITER
312 uint8_t *buf, *buf_end;
317 uint8_t *buf, *buf_ptr, *buf_end;
321 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
324 s->buf_end = s->buf + buffer_size;
325 #ifdef ALT_BITSTREAM_WRITER
327 ((uint32_t*)(s->buf))[0]=0;
328 // memset(buffer, 0, buffer_size);
336 /* return the number of bits output */
337 static inline int put_bits_count(PutBitContext *s)
339 #ifdef ALT_BITSTREAM_WRITER
342 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
346 /* pad the end of the output stream with zeros */
347 static inline void flush_put_bits(PutBitContext *s)
349 #ifdef ALT_BITSTREAM_WRITER
352 s->bit_buf<<= s->bit_left;
353 while (s->bit_left < 32) {
354 /* XXX: should test end of buffer */
355 *s->buf_ptr++=s->bit_buf >> 24;
364 void align_put_bits(PutBitContext *s);
365 void put_string(PutBitContext * pbc, char *s, int put_zero);
368 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
369 typedef struct GetBitContext {
370 const uint8_t *buffer, *buffer_end;
371 #ifdef ALT_BITSTREAM_READER
373 #elif defined LIBMPEG2_BITSTREAM_READER
377 #elif defined A32_BITSTREAM_READER
378 uint32_t *buffer_ptr;
386 #define VLC_TYPE int16_t
390 VLC_TYPE (*table)[2]; ///< code, bits
391 int table_size, table_allocated;
394 typedef struct RL_VLC_ELEM {
401 #define UNALIGNED_STORES_ARE_BAD
404 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
405 #if defined(ARCH_X86) || defined(ARCH_X86_64)
406 # define unaligned32(a) (*(const uint32_t*)(a))
409 static inline uint32_t unaligned32(const void *v) {
412 } __attribute__((packed));
414 return ((const struct Unaligned *) v)->i;
416 # elif defined(__DECC)
417 static inline uint32_t unaligned32(const void *v) {
418 return *(const __unaligned uint32_t *) v;
421 static inline uint32_t unaligned32(const void *v) {
422 return *(const uint32_t *) v;
427 #ifndef ALT_BITSTREAM_WRITER
428 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
430 unsigned int bit_buf;
434 st_out_bit_counts[st_current_index] += n;
436 // printf("put_bits=%d %x\n", n, value);
437 assert(n == 32 || value < (1U << n));
439 bit_buf = s->bit_buf;
440 bit_left = s->bit_left;
442 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
445 bit_buf = (bit_buf<<n) | value;
449 bit_buf |= value >> (n - bit_left);
450 #ifdef UNALIGNED_STORES_ARE_BAD
451 if (3 & (intptr_t) s->buf_ptr) {
452 s->buf_ptr[0] = bit_buf >> 24;
453 s->buf_ptr[1] = bit_buf >> 16;
454 s->buf_ptr[2] = bit_buf >> 8;
455 s->buf_ptr[3] = bit_buf ;
458 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
459 //printf("bitbuf = %08x\n", bit_buf);
465 s->bit_buf = bit_buf;
466 s->bit_left = bit_left;
471 #ifdef ALT_BITSTREAM_WRITER
472 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
474 # ifdef ALIGNED_BITSTREAM_WRITER
475 # if defined(ARCH_X86) || defined(ARCH_X86_64)
477 "movl %0, %%ecx \n\t"
478 "xorl %%eax, %%eax \n\t"
479 "shrdl %%cl, %1, %%eax \n\t"
481 "movl %0, %%ecx \n\t"
482 "shrl $3, %%ecx \n\t"
483 "andl $0xFFFFFFFC, %%ecx \n\t"
485 "orl %1, (%2, %%ecx) \n\t"
488 "movl %%eax, 4(%2, %%ecx) \n\t"
489 : "=&r" (s->index), "=&r" (value)
490 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
495 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
499 ptr[0] |= be2me_32(value>>(index&31));
500 ptr[1] = be2me_32(value<<(32-(index&31)));
501 //if(n>24) printf("%d %d\n", n, value);
505 # else //ALIGNED_BITSTREAM_WRITER
506 # if defined(ARCH_X86) || defined(ARCH_X86_64)
508 "movl $7, %%ecx \n\t"
509 "andl %0, %%ecx \n\t"
510 "addl %3, %%ecx \n\t"
514 "movl %0, %%ecx \n\t"
515 "shrl $3, %%ecx \n\t"
516 "orl %1, (%%ecx, %2) \n\t"
518 "movl $0, 4(%%ecx, %2) \n\t"
519 : "=&r" (s->index), "=&r" (value)
520 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
525 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
527 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
529 //if(n>24) printf("%d %d\n", n, value);
533 # endif //!ALIGNED_BITSTREAM_WRITER
538 static inline uint8_t* pbBufPtr(PutBitContext *s)
540 #ifdef ALT_BITSTREAM_WRITER
541 return s->buf + (s->index>>3);
549 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
551 static inline void skip_put_bytes(PutBitContext *s, int n){
552 assert((put_bits_count(s)&7)==0);
553 #ifdef ALT_BITSTREAM_WRITER
554 FIXME may need some cleaning of the buffer
557 assert(s->bit_left==32);
563 * Changes the end of the buffer.
565 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
566 s->buf_end= s->buf + size;
569 /* Bitstream reader API docs:
571 abritary name which is used as prefix for the internal variables
576 OPEN_READER(name, gb)
577 loads gb into local variables
579 CLOSE_READER(name, gb)
580 stores local vars in gb
582 UPDATE_CACHE(name, gb)
583 refills the internal cache from the bitstream
584 after this call at least MIN_CACHE_BITS will be available,
587 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
589 SHOW_UBITS(name, gb, num)
590 will return the nest num bits
592 SHOW_SBITS(name, gb, num)
593 will return the nest num bits and do sign extension
595 SKIP_BITS(name, gb, num)
596 will skip over the next num bits
597 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
599 SKIP_CACHE(name, gb, num)
600 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
602 SKIP_COUNTER(name, gb, num)
603 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
605 LAST_SKIP_CACHE(name, gb, num)
606 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
608 LAST_SKIP_BITS(name, gb, num)
609 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
611 for examples see get_bits, show_bits, skip_bits, get_vlc
614 static inline int unaligned32_be(const void *v)
618 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
620 return be2me_32( unaligned32(v)); //original
624 #ifdef ALT_BITSTREAM_READER
625 # define MIN_CACHE_BITS 25
627 # define OPEN_READER(name, gb)\
628 int name##_index= (gb)->index;\
629 int name##_cache= 0;\
631 # define CLOSE_READER(name, gb)\
632 (gb)->index= name##_index;\
634 # define UPDATE_CACHE(name, gb)\
635 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
637 # define SKIP_CACHE(name, gb, num)\
638 name##_cache <<= (num);\
641 # define SKIP_COUNTER(name, gb, num)\
642 name##_index += (num);\
644 # define SKIP_BITS(name, gb, num)\
646 SKIP_CACHE(name, gb, num)\
647 SKIP_COUNTER(name, gb, num)\
650 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
651 # define LAST_SKIP_CACHE(name, gb, num) ;
653 # define SHOW_UBITS(name, gb, num)\
654 NEG_USR32(name##_cache, num)
656 # define SHOW_SBITS(name, gb, num)\
657 NEG_SSR32(name##_cache, num)
659 # define GET_CACHE(name, gb)\
660 ((uint32_t)name##_cache)
662 static inline int get_bits_count(GetBitContext *s){
665 #elif defined LIBMPEG2_BITSTREAM_READER
666 //libmpeg2 like reader
668 # define MIN_CACHE_BITS 17
670 # define OPEN_READER(name, gb)\
671 int name##_bit_count=(gb)->bit_count;\
672 int name##_cache= (gb)->cache;\
673 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
675 # define CLOSE_READER(name, gb)\
676 (gb)->bit_count= name##_bit_count;\
677 (gb)->cache= name##_cache;\
678 (gb)->buffer_ptr= name##_buffer_ptr;\
680 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
682 # define UPDATE_CACHE(name, gb)\
683 if(name##_bit_count >= 0){\
684 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
685 ((uint16_t*)name##_buffer_ptr)++;\
686 name##_bit_count-= 16;\
691 # define UPDATE_CACHE(name, gb)\
692 if(name##_bit_count >= 0){\
693 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
694 name##_buffer_ptr+=2;\
695 name##_bit_count-= 16;\
700 # define SKIP_CACHE(name, gb, num)\
701 name##_cache <<= (num);\
703 # define SKIP_COUNTER(name, gb, num)\
704 name##_bit_count += (num);\
706 # define SKIP_BITS(name, gb, num)\
708 SKIP_CACHE(name, gb, num)\
709 SKIP_COUNTER(name, gb, num)\
712 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
713 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
715 # define SHOW_UBITS(name, gb, num)\
716 NEG_USR32(name##_cache, num)
718 # define SHOW_SBITS(name, gb, num)\
719 NEG_SSR32(name##_cache, num)
721 # define GET_CACHE(name, gb)\
722 ((uint32_t)name##_cache)
724 static inline int get_bits_count(GetBitContext *s){
725 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
728 #elif defined A32_BITSTREAM_READER
730 # define MIN_CACHE_BITS 32
732 # define OPEN_READER(name, gb)\
733 int name##_bit_count=(gb)->bit_count;\
734 uint32_t name##_cache0= (gb)->cache0;\
735 uint32_t name##_cache1= (gb)->cache1;\
736 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
738 # define CLOSE_READER(name, gb)\
739 (gb)->bit_count= name##_bit_count;\
740 (gb)->cache0= name##_cache0;\
741 (gb)->cache1= name##_cache1;\
742 (gb)->buffer_ptr= name##_buffer_ptr;\
744 # define UPDATE_CACHE(name, gb)\
745 if(name##_bit_count > 0){\
746 const uint32_t next= be2me_32( *name##_buffer_ptr );\
747 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
748 name##_cache1 |= next<<name##_bit_count;\
749 name##_buffer_ptr++;\
750 name##_bit_count-= 32;\
753 #if defined(ARCH_X86) || defined(ARCH_X86_64)
754 # define SKIP_CACHE(name, gb, num)\
756 "shldl %2, %1, %0 \n\t"\
758 : "+r" (name##_cache0), "+r" (name##_cache1)\
759 : "Ic" ((uint8_t)num)\
762 # define SKIP_CACHE(name, gb, num)\
763 name##_cache0 <<= (num);\
764 name##_cache0 |= NEG_USR32(name##_cache1,num);\
765 name##_cache1 <<= (num);
768 # define SKIP_COUNTER(name, gb, num)\
769 name##_bit_count += (num);\
771 # define SKIP_BITS(name, gb, num)\
773 SKIP_CACHE(name, gb, num)\
774 SKIP_COUNTER(name, gb, num)\
777 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
778 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
780 # define SHOW_UBITS(name, gb, num)\
781 NEG_USR32(name##_cache0, num)
783 # define SHOW_SBITS(name, gb, num)\
784 NEG_SSR32(name##_cache0, num)
786 # define GET_CACHE(name, gb)\
789 static inline int get_bits_count(GetBitContext *s){
790 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
796 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
797 * if MSB not set it is negative
798 * @param n length in bits
801 static inline int get_xbits(GetBitContext *s, int n){
803 register int32_t cache;
806 cache = GET_CACHE(re,s);
807 if ((int32_t)cache<0) { //MSB=1
808 tmp = NEG_USR32(cache,n);
810 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
811 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
812 tmp = - NEG_USR32(~cache,n);
814 LAST_SKIP_BITS(re, s, n)
819 static inline int get_sbits(GetBitContext *s, int n){
823 tmp= SHOW_SBITS(re, s, n);
824 LAST_SKIP_BITS(re, s, n)
831 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
833 static inline unsigned int get_bits(GetBitContext *s, int n){
837 tmp= SHOW_UBITS(re, s, n);
838 LAST_SKIP_BITS(re, s, n)
843 unsigned int get_bits_long(GetBitContext *s, int n);
847 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
849 static inline unsigned int show_bits(GetBitContext *s, int n){
853 tmp= SHOW_UBITS(re, s, n);
854 // CLOSE_READER(re, s)
858 unsigned int show_bits_long(GetBitContext *s, int n);
860 static inline void skip_bits(GetBitContext *s, int n){
861 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
864 LAST_SKIP_BITS(re, s, n)
868 static inline unsigned int get_bits1(GetBitContext *s){
869 #ifdef ALT_BITSTREAM_READER
871 uint8_t result= s->buffer[ index>>3 ];
872 result<<= (index&0x07);
879 return get_bits(s, 1);
883 static inline unsigned int show_bits1(GetBitContext *s){
884 return show_bits(s, 1);
887 static inline void skip_bits1(GetBitContext *s){
892 * init GetBitContext.
893 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
894 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
895 * @param bit_size the size of the buffer in bits
897 static inline void init_get_bits(GetBitContext *s,
898 const uint8_t *buffer, int bit_size)
900 const int buffer_size= (bit_size+7)>>3;
903 s->size_in_bits= bit_size;
904 s->buffer_end= buffer + buffer_size;
905 #ifdef ALT_BITSTREAM_READER
907 #elif defined LIBMPEG2_BITSTREAM_READER
908 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
911 s->cache = (*buffer++)<<24;
912 s->buffer_ptr = buffer;
917 s->buffer_ptr = buffer;
921 #elif defined A32_BITSTREAM_READER
922 s->buffer_ptr = (uint32_t*)buffer;
933 #ifdef A32_BITSTREAM_READER
938 int check_marker(GetBitContext *s, const char *msg);
939 void align_get_bits(GetBitContext *s);
940 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
941 const void *bits, int bits_wrap, int bits_size,
942 const void *codes, int codes_wrap, int codes_size,
944 void free_vlc(VLC *vlc);
948 * if the vlc code is invalid and max_depth=1 than no bits will be removed
949 * if the vlc code is invalid and max_depth>1 than the number of bits removed
952 #define GET_VLC(code, name, gb, table, bits, max_depth)\
954 int n, index, nb_bits;\
956 index= SHOW_UBITS(name, gb, bits);\
957 code = table[index][0];\
958 n = table[index][1];\
960 if(max_depth > 1 && n < 0){\
961 LAST_SKIP_BITS(name, gb, bits)\
962 UPDATE_CACHE(name, gb)\
966 index= SHOW_UBITS(name, gb, nb_bits) + code;\
967 code = table[index][0];\
968 n = table[index][1];\
969 if(max_depth > 2 && n < 0){\
970 LAST_SKIP_BITS(name, gb, nb_bits)\
971 UPDATE_CACHE(name, gb)\
975 index= SHOW_UBITS(name, gb, nb_bits) + code;\
976 code = table[index][0];\
977 n = table[index][1];\
980 SKIP_BITS(name, gb, n)\
983 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
985 int n, index, nb_bits;\
987 index= SHOW_UBITS(name, gb, bits);\
988 level = table[index].level;\
989 n = table[index].len;\
991 if(max_depth > 1 && n < 0){\
992 LAST_SKIP_BITS(name, gb, bits)\
993 UPDATE_CACHE(name, gb)\
997 index= SHOW_UBITS(name, gb, nb_bits) + level;\
998 level = table[index].level;\
999 n = table[index].len;\
1001 run= table[index].run;\
1002 SKIP_BITS(name, gb, n)\
1005 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
1006 static inline int get_vlc(GetBitContext *s, VLC *vlc)
1009 VLC_TYPE (*table)[2]= vlc->table;
1014 GET_VLC(code, re, s, table, vlc->bits, 3)
1021 * parses a vlc code, faster then get_vlc()
1022 * @param bits is the number of bits which will be read at once, must be
1023 * identical to nb_bits in init_vlc()
1024 * @param max_depth is the number of times bits bits must be readed to completly
1025 * read the longest vlc code
1026 * = (max_vlc_length + bits - 1) / bits
1028 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1029 int bits, int max_depth)
1036 GET_VLC(code, re, s, table, bits, max_depth)
1046 static inline void print_bin(int bits, int n){
1049 for(i=n-1; i>=0; i--){
1050 printf("%d", (bits>>i)&1);
1056 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1057 int r= get_bits(s, n);
1060 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1063 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1064 int show= show_bits(s, 24);
1065 int pos= get_bits_count(s);
1066 int r= get_vlc2(s, table, bits, max_depth);
1067 int len= get_bits_count(s) - pos;
1068 int bits2= show>>(24-len);
1070 print_bin(bits2, len);
1072 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1075 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1076 int show= show_bits(s, n);
1077 int r= get_xbits(s, n);
1080 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1084 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1085 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1086 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1087 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1088 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1090 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1093 #define tprintf(...) {}
1096 /* define it to include statistics code (useful only for optimizing
1113 extern int st_current_index;
1114 extern unsigned int st_bit_counts[ST_NB];
1115 extern unsigned int st_out_bit_counts[ST_NB];
1117 void print_stats(void);
1120 /* misc math functions */
1121 extern const uint8_t ff_log2_tab[256];
1123 static inline int av_log2(unsigned int v)
1128 if (v & 0xffff0000) {
1136 n += ff_log2_tab[v];
1141 static inline int av_log2_16bit(unsigned int v)
1150 n += ff_log2_tab[v];
1156 static inline int mid_pred(int a, int b, int c)
1159 int t= (a-b)&((a-b)>>31);
1162 b-= (b-c)&((b-c)>>31);
1163 b+= (a-b)&((a-b)>>31);
1182 static inline int clip(int a, int amin, int amax)
1192 static inline int clip_uint8(int a)
1194 if (a&(~255)) return (-a)>>31;
1199 extern const uint8_t ff_sqrt_tab[128];
1201 int64_t ff_gcd(int64_t a, int64_t b);
1203 static inline int ff_sqrt(int a)
1209 if(a<128) return ff_sqrt_tab[a];
1211 for(s=15; s>=0; s--){
1212 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1222 * converts fourcc string to int
1224 static inline int ff_get_fourcc(const char *s){
1225 assert( strlen(s)==4 );
1227 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1230 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1231 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1234 #if defined(ARCH_X86) || defined(ARCH_X86_64)
1235 #define MASK_ABS(mask, level)\
1240 : "+a" (level), "=&d" (mask)\
1243 #define MASK_ABS(mask, level)\
1245 level= (level^mask)-mask;
1249 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1250 #define COPY3_IF_LT(x,y,a,b,c,d)\
1253 "cmovl %3, %0 \n\t"\
1254 "cmovl %4, %1 \n\t"\
1255 "cmovl %5, %2 \n\t"\
1256 : "+r" (x), "+r" (a), "+r" (c)\
1257 : "r" (y), "r" (b), "r" (d)\
1260 #define COPY3_IF_LT(x,y,a,b,c,d)\
1268 #if defined(ARCH_X86) || defined(ARCH_X86_64)
1269 static inline long long rdtsc(void)
1272 asm volatile( "rdtsc\n\t"
1278 #define START_TIMER \
1280 uint64_t tstart= rdtsc();\
1282 #define STOP_TIMER(id) \
1285 static uint64_t tsum=0;\
1286 static int tcount=0;\
1287 static int tskip_count=0;\
1288 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1289 tsum+= tend - tstart;\
1293 if(256*256*256*64%(tcount+tskip_count)==0){\
1294 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1299 #define STOP_TIMER(id) {}
1302 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1304 /* avoid usage of various functions */
1305 #define malloc please_use_av_malloc
1306 #define free please_use_av_free
1307 #define realloc please_use_av_realloc
1308 #define time time_is_forbidden_due_to_security_issues
1309 #define rand rand_is_forbidden_due_to_state_trashing
1310 #define srand srand_is_forbidden_due_to_state_trashing
1311 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1312 #define printf please_use_av_log
1313 #define fprintf please_use_av_log
1316 #define CHECKED_ALLOCZ(p, size)\
1318 p= av_mallocz(size);\
1319 if(p==NULL && (size)!=0){\
1325 #endif /* HAVE_AV_CONFIG_H */
1327 #endif /* COMMON_H */