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 */
46 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
49 #define AVOPTION_CODEC_BOOL(name, help, field) \
50 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
51 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
53 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
55 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
57 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
59 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
60 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
61 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
62 #define AVOPTION_END() AVOPTION_SUB(NULL)
66 extern const struct AVOption avoptions_common[3 + 5];
68 extern const struct AVOption avoptions_common[3];
70 extern const struct AVOption avoptions_workaround_bug[11];
72 #endif /* HAVE_AV_CONFIG_H */
74 /* Suppress restrict if it was not defined in config.h. */
80 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
81 # define always_inline __attribute__((always_inline)) inline
83 # define always_inline inline
87 #ifndef attribute_used
88 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
89 # define attribute_used __attribute__((used))
91 # define attribute_used
95 #ifndef EMULATE_INTTYPES
96 # include <inttypes.h>
98 typedef signed char int8_t;
99 typedef signed short int16_t;
100 typedef signed int int32_t;
101 typedef unsigned char uint8_t;
102 typedef unsigned short uint16_t;
103 typedef unsigned int uint32_t;
106 typedef signed __int64 int64_t;
107 typedef unsigned __int64 uint64_t;
108 # else /* other OS */
109 typedef signed long long int64_t;
110 typedef unsigned long long uint64_t;
111 # endif /* other OS */
112 #endif /* HAVE_INTTYPES_H */
115 #define INT64_MAX int64_t_C(9223372036854775807)
119 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
122 #ifdef EMULATE_FAST_INT
123 /* note that we don't emulate 64bit ints */
124 typedef signed char int_fast8_t;
125 typedef signed int int_fast16_t;
126 typedef signed int int_fast32_t;
127 typedef unsigned char uint_fast8_t;
128 typedef unsigned int uint_fast16_t;
129 typedef unsigned int uint_fast32_t;
132 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
133 static inline float floorf(float f) {
142 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
143 # define int64_t_C(c) (c ## i64)
144 # define uint64_t_C(c) (c ## i64)
146 # ifdef HAVE_AV_CONFIG_H
147 # define inline __inline
151 # define int64_t_C(c) (c ## LL)
152 # define uint64_t_C(c) (c ## ULL)
153 # endif /* __MINGW32__ */
155 # ifdef HAVE_AV_CONFIG_H
160 # define snprintf _snprintf
161 # define vsnprintf _vsnprintf
164 /* CONFIG_WIN32 end */
165 #elif defined (CONFIG_OS2)
169 #define int64_t_C(c) (c ## LL)
170 #define uint64_t_C(c) (c ## ULL)
173 #ifdef HAVE_AV_CONFIG_H
175 #ifdef USE_FASTMEMCPY
176 #include "fastmemcpy.h"
181 #endif /* HAVE_AV_CONFIG_H */
189 #define int64_t_C(c) (c ## LL)
190 #define uint64_t_C(c) (c ## ULL)
193 #ifdef HAVE_AV_CONFIG_H
195 # ifdef USE_FASTMEMCPY
196 # include "fastmemcpy.h"
198 # endif /* HAVE_AV_CONFIG_H */
200 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
202 #ifdef HAVE_AV_CONFIG_H
206 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
207 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
208 # define MANGLE(a) "_" #a
210 # define MANGLE(a) #a
221 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
223 inline void dprintf(const char* fmt,...) {}
228 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
230 # define dprintf(fmt,...)
233 # endif /* !CONFIG_WIN32 */
235 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
237 //rounded divison & shift
238 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
240 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
241 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
243 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
244 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
246 extern const uint32_t inverse[256];
249 # define FASTDIV(a,b) \
254 :"=d"(ret),"=a"(dmy)\
255 :"1"(a),"g"(inverse[b])\
259 #elif defined(CONFIG_FASTDIV)
260 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
262 # define FASTDIV(a,b) ((a)/(b))
266 // avoid +32 for shift optimization (gcc should do that ...)
267 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
268 asm ("sarl %1, %0\n\t"
270 : "ic" ((uint8_t)(-s))
274 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
275 asm ("shrl %1, %0\n\t"
277 : "ic" ((uint8_t)(-s))
282 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
283 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
288 struct PutBitContext;
290 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
292 /* buf and buf_end must be present and used by every alternative writer. */
293 typedef struct PutBitContext {
294 #ifdef ALT_BITSTREAM_WRITER
295 uint8_t *buf, *buf_end;
300 uint8_t *buf, *buf_ptr, *buf_end;
304 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
307 s->buf_end = s->buf + buffer_size;
308 #ifdef ALT_BITSTREAM_WRITER
310 ((uint32_t*)(s->buf))[0]=0;
311 // memset(buffer, 0, buffer_size);
319 /* return the number of bits output */
320 static inline int put_bits_count(PutBitContext *s)
322 #ifdef ALT_BITSTREAM_WRITER
325 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
329 /* pad the end of the output stream with zeros */
330 static inline void flush_put_bits(PutBitContext *s)
332 #ifdef ALT_BITSTREAM_WRITER
335 s->bit_buf<<= s->bit_left;
336 while (s->bit_left < 32) {
337 /* XXX: should test end of buffer */
338 *s->buf_ptr++=s->bit_buf >> 24;
347 void align_put_bits(PutBitContext *s);
348 void put_string(PutBitContext * pbc, char *s, int put_zero);
351 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
352 typedef struct GetBitContext {
353 const uint8_t *buffer, *buffer_end;
354 #ifdef ALT_BITSTREAM_READER
356 #elif defined LIBMPEG2_BITSTREAM_READER
360 #elif defined A32_BITSTREAM_READER
361 uint32_t *buffer_ptr;
369 #define VLC_TYPE int16_t
373 VLC_TYPE (*table)[2]; ///< code, bits
374 int table_size, table_allocated;
377 typedef struct RL_VLC_ELEM {
384 #define UNALIGNED_STORES_ARE_BAD
387 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
389 # define unaligned32(a) (*(uint32_t*)(a))
392 static inline uint32_t unaligned32(const void *v) {
395 } __attribute__((packed));
397 return ((const struct Unaligned *) v)->i;
399 # elif defined(__DECC)
400 static inline uint32_t unaligned32(const void *v) {
401 return *(const __unaligned uint32_t *) v;
404 static inline uint32_t unaligned32(const void *v) {
405 return *(const uint32_t *) v;
410 #ifndef ALT_BITSTREAM_WRITER
411 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
413 unsigned int bit_buf;
417 st_out_bit_counts[st_current_index] += n;
419 // printf("put_bits=%d %x\n", n, value);
420 assert(n == 32 || value < (1U << n));
422 bit_buf = s->bit_buf;
423 bit_left = s->bit_left;
425 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
428 bit_buf = (bit_buf<<n) | value;
432 bit_buf |= value >> (n - bit_left);
433 #ifdef UNALIGNED_STORES_ARE_BAD
434 if (3 & (intptr_t) s->buf_ptr) {
435 s->buf_ptr[0] = bit_buf >> 24;
436 s->buf_ptr[1] = bit_buf >> 16;
437 s->buf_ptr[2] = bit_buf >> 8;
438 s->buf_ptr[3] = bit_buf ;
441 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
442 //printf("bitbuf = %08x\n", bit_buf);
448 s->bit_buf = bit_buf;
449 s->bit_left = bit_left;
454 #ifdef ALT_BITSTREAM_WRITER
455 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
457 # ifdef ALIGNED_BITSTREAM_WRITER
460 "movl %0, %%ecx \n\t"
461 "xorl %%eax, %%eax \n\t"
462 "shrdl %%cl, %1, %%eax \n\t"
464 "movl %0, %%ecx \n\t"
465 "shrl $3, %%ecx \n\t"
466 "andl $0xFFFFFFFC, %%ecx \n\t"
468 "orl %1, (%2, %%ecx) \n\t"
471 "movl %%eax, 4(%2, %%ecx) \n\t"
472 : "=&r" (s->index), "=&r" (value)
473 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
478 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
482 ptr[0] |= be2me_32(value>>(index&31));
483 ptr[1] = be2me_32(value<<(32-(index&31)));
484 //if(n>24) printf("%d %d\n", n, value);
488 # else //ALIGNED_BITSTREAM_WRITER
491 "movl $7, %%ecx \n\t"
492 "andl %0, %%ecx \n\t"
493 "addl %3, %%ecx \n\t"
497 "movl %0, %%ecx \n\t"
498 "shrl $3, %%ecx \n\t"
499 "orl %1, (%%ecx, %2) \n\t"
501 "movl $0, 4(%%ecx, %2) \n\t"
502 : "=&r" (s->index), "=&r" (value)
503 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
508 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
510 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
512 //if(n>24) printf("%d %d\n", n, value);
516 # endif //!ALIGNED_BITSTREAM_WRITER
521 static inline uint8_t* pbBufPtr(PutBitContext *s)
523 #ifdef ALT_BITSTREAM_WRITER
524 return s->buf + (s->index>>3);
532 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
534 static inline void skip_put_bytes(PutBitContext *s, int n){
535 assert((put_bits_count(s)&7)==0);
536 #ifdef ALT_BITSTREAM_WRITER
537 FIXME may need some cleaning of the buffer
540 assert(s->bit_left==32);
546 * Changes the end of the buffer.
548 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
549 s->buf_end= s->buf + size;
552 /* Bitstream reader API docs:
554 abritary name which is used as prefix for the internal variables
559 OPEN_READER(name, gb)
560 loads gb into local variables
562 CLOSE_READER(name, gb)
563 stores local vars in gb
565 UPDATE_CACHE(name, gb)
566 refills the internal cache from the bitstream
567 after this call at least MIN_CACHE_BITS will be available,
570 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
572 SHOW_UBITS(name, gb, num)
573 will return the nest num bits
575 SHOW_SBITS(name, gb, num)
576 will return the nest num bits and do sign extension
578 SKIP_BITS(name, gb, num)
579 will skip over the next num bits
580 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
582 SKIP_CACHE(name, gb, num)
583 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
585 SKIP_COUNTER(name, gb, num)
586 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
588 LAST_SKIP_CACHE(name, gb, num)
589 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
591 LAST_SKIP_BITS(name, gb, num)
592 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
594 for examples see get_bits, show_bits, skip_bits, get_vlc
597 static inline int unaligned32_be(const void *v)
601 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
603 return be2me_32( unaligned32(v)); //original
607 #ifdef ALT_BITSTREAM_READER
608 # define MIN_CACHE_BITS 25
610 # define OPEN_READER(name, gb)\
611 int name##_index= (gb)->index;\
612 int name##_cache= 0;\
614 # define CLOSE_READER(name, gb)\
615 (gb)->index= name##_index;\
617 # define UPDATE_CACHE(name, gb)\
618 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
620 # define SKIP_CACHE(name, gb, num)\
621 name##_cache <<= (num);\
624 # define SKIP_COUNTER(name, gb, num)\
625 name##_index += (num);\
627 # define SKIP_BITS(name, gb, num)\
629 SKIP_CACHE(name, gb, num)\
630 SKIP_COUNTER(name, gb, num)\
633 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
634 # define LAST_SKIP_CACHE(name, gb, num) ;
636 # define SHOW_UBITS(name, gb, num)\
637 NEG_USR32(name##_cache, num)
639 # define SHOW_SBITS(name, gb, num)\
640 NEG_SSR32(name##_cache, num)
642 # define GET_CACHE(name, gb)\
643 ((uint32_t)name##_cache)
645 static inline int get_bits_count(GetBitContext *s){
648 #elif defined LIBMPEG2_BITSTREAM_READER
649 //libmpeg2 like reader
651 # define MIN_CACHE_BITS 17
653 # define OPEN_READER(name, gb)\
654 int name##_bit_count=(gb)->bit_count;\
655 int name##_cache= (gb)->cache;\
656 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
658 # define CLOSE_READER(name, gb)\
659 (gb)->bit_count= name##_bit_count;\
660 (gb)->cache= name##_cache;\
661 (gb)->buffer_ptr= name##_buffer_ptr;\
663 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
665 # define UPDATE_CACHE(name, gb)\
666 if(name##_bit_count >= 0){\
667 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
668 ((uint16_t*)name##_buffer_ptr)++;\
669 name##_bit_count-= 16;\
674 # define UPDATE_CACHE(name, gb)\
675 if(name##_bit_count >= 0){\
676 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
677 name##_buffer_ptr+=2;\
678 name##_bit_count-= 16;\
683 # define SKIP_CACHE(name, gb, num)\
684 name##_cache <<= (num);\
686 # define SKIP_COUNTER(name, gb, num)\
687 name##_bit_count += (num);\
689 # define SKIP_BITS(name, gb, num)\
691 SKIP_CACHE(name, gb, num)\
692 SKIP_COUNTER(name, gb, num)\
695 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
696 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
698 # define SHOW_UBITS(name, gb, num)\
699 NEG_USR32(name##_cache, num)
701 # define SHOW_SBITS(name, gb, num)\
702 NEG_SSR32(name##_cache, num)
704 # define GET_CACHE(name, gb)\
705 ((uint32_t)name##_cache)
707 static inline int get_bits_count(GetBitContext *s){
708 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
711 #elif defined A32_BITSTREAM_READER
713 # define MIN_CACHE_BITS 32
715 # define OPEN_READER(name, gb)\
716 int name##_bit_count=(gb)->bit_count;\
717 uint32_t name##_cache0= (gb)->cache0;\
718 uint32_t name##_cache1= (gb)->cache1;\
719 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
721 # define CLOSE_READER(name, gb)\
722 (gb)->bit_count= name##_bit_count;\
723 (gb)->cache0= name##_cache0;\
724 (gb)->cache1= name##_cache1;\
725 (gb)->buffer_ptr= name##_buffer_ptr;\
727 # define UPDATE_CACHE(name, gb)\
728 if(name##_bit_count > 0){\
729 const uint32_t next= be2me_32( *name##_buffer_ptr );\
730 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
731 name##_cache1 |= next<<name##_bit_count;\
732 name##_buffer_ptr++;\
733 name##_bit_count-= 32;\
737 # define SKIP_CACHE(name, gb, num)\
739 "shldl %2, %1, %0 \n\t"\
741 : "+r" (name##_cache0), "+r" (name##_cache1)\
742 : "Ic" ((uint8_t)num)\
745 # define SKIP_CACHE(name, gb, num)\
746 name##_cache0 <<= (num);\
747 name##_cache0 |= NEG_USR32(name##_cache1,num);\
748 name##_cache1 <<= (num);
751 # define SKIP_COUNTER(name, gb, num)\
752 name##_bit_count += (num);\
754 # define SKIP_BITS(name, gb, num)\
756 SKIP_CACHE(name, gb, num)\
757 SKIP_COUNTER(name, gb, num)\
760 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
761 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
763 # define SHOW_UBITS(name, gb, num)\
764 NEG_USR32(name##_cache0, num)
766 # define SHOW_SBITS(name, gb, num)\
767 NEG_SSR32(name##_cache0, num)
769 # define GET_CACHE(name, gb)\
772 static inline int get_bits_count(GetBitContext *s){
773 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
779 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
780 * if MSB not set it is negative
781 * @param n length in bits
784 static inline int get_xbits(GetBitContext *s, int n){
786 register int32_t cache;
789 cache = GET_CACHE(re,s);
790 if ((int32_t)cache<0) { //MSB=1
791 tmp = NEG_USR32(cache,n);
793 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
794 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
795 tmp = - NEG_USR32(~cache,n);
797 LAST_SKIP_BITS(re, s, n)
802 static inline int get_sbits(GetBitContext *s, int n){
806 tmp= SHOW_SBITS(re, s, n);
807 LAST_SKIP_BITS(re, s, n)
814 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
816 static inline unsigned int get_bits(GetBitContext *s, int n){
820 tmp= SHOW_UBITS(re, s, n);
821 LAST_SKIP_BITS(re, s, n)
826 unsigned int get_bits_long(GetBitContext *s, int n);
830 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
832 static inline unsigned int show_bits(GetBitContext *s, int n){
836 tmp= SHOW_UBITS(re, s, n);
837 // CLOSE_READER(re, s)
841 unsigned int show_bits_long(GetBitContext *s, int n);
843 static inline void skip_bits(GetBitContext *s, int n){
844 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
847 LAST_SKIP_BITS(re, s, n)
851 static inline unsigned int get_bits1(GetBitContext *s){
852 #ifdef ALT_BITSTREAM_READER
854 uint8_t result= s->buffer[ index>>3 ];
855 result<<= (index&0x07);
862 return get_bits(s, 1);
866 static inline unsigned int show_bits1(GetBitContext *s){
867 return show_bits(s, 1);
870 static inline void skip_bits1(GetBitContext *s){
875 * init GetBitContext.
876 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
877 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
878 * @param bit_size the size of the buffer in bits
880 static inline void init_get_bits(GetBitContext *s,
881 const uint8_t *buffer, int bit_size)
883 const int buffer_size= (bit_size+7)>>3;
886 s->size_in_bits= bit_size;
887 s->buffer_end= buffer + buffer_size;
888 #ifdef ALT_BITSTREAM_READER
890 #elif defined LIBMPEG2_BITSTREAM_READER
891 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
894 s->cache = (*buffer++)<<24;
895 s->buffer_ptr = buffer;
900 s->buffer_ptr = buffer;
904 #elif defined A32_BITSTREAM_READER
905 s->buffer_ptr = (uint32_t*)buffer;
916 #ifdef A32_BITSTREAM_READER
921 int check_marker(GetBitContext *s, const char *msg);
922 void align_get_bits(GetBitContext *s);
923 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
924 const void *bits, int bits_wrap, int bits_size,
925 const void *codes, int codes_wrap, int codes_size);
926 void free_vlc(VLC *vlc);
930 * if the vlc code is invalid and max_depth=1 than no bits will be removed
931 * if the vlc code is invalid and max_depth>1 than the number of bits removed
934 #define GET_VLC(code, name, gb, table, bits, max_depth)\
936 int n, index, nb_bits;\
938 index= SHOW_UBITS(name, gb, bits);\
939 code = table[index][0];\
940 n = table[index][1];\
942 if(max_depth > 1 && n < 0){\
943 LAST_SKIP_BITS(name, gb, bits)\
944 UPDATE_CACHE(name, gb)\
948 index= SHOW_UBITS(name, gb, nb_bits) + code;\
949 code = table[index][0];\
950 n = table[index][1];\
951 if(max_depth > 2 && n < 0){\
952 LAST_SKIP_BITS(name, gb, nb_bits)\
953 UPDATE_CACHE(name, gb)\
957 index= SHOW_UBITS(name, gb, nb_bits) + code;\
958 code = table[index][0];\
959 n = table[index][1];\
962 SKIP_BITS(name, gb, n)\
965 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
967 int n, index, nb_bits;\
969 index= SHOW_UBITS(name, gb, bits);\
970 level = table[index].level;\
971 n = table[index].len;\
973 if(max_depth > 1 && n < 0){\
974 LAST_SKIP_BITS(name, gb, bits)\
975 UPDATE_CACHE(name, gb)\
979 index= SHOW_UBITS(name, gb, nb_bits) + level;\
980 level = table[index].level;\
981 n = table[index].len;\
983 run= table[index].run;\
984 SKIP_BITS(name, gb, n)\
987 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
988 static inline int get_vlc(GetBitContext *s, VLC *vlc)
991 VLC_TYPE (*table)[2]= vlc->table;
996 GET_VLC(code, re, s, table, vlc->bits, 3)
1003 * parses a vlc code, faster then get_vlc()
1004 * @param bits is the number of bits which will be read at once, must be
1005 * identical to nb_bits in init_vlc()
1006 * @param max_depth is the number of times bits bits must be readed to completly
1007 * read the longest vlc code
1008 * = (max_vlc_length + bits - 1) / bits
1010 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1011 int bits, int max_depth)
1018 GET_VLC(code, re, s, table, bits, max_depth)
1028 static inline void print_bin(int bits, int n){
1031 for(i=n-1; i>=0; i--){
1032 printf("%d", (bits>>i)&1);
1038 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1039 int r= get_bits(s, n);
1042 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1045 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1046 int show= show_bits(s, 24);
1047 int pos= get_bits_count(s);
1048 int r= get_vlc2(s, table, bits, max_depth);
1049 int len= get_bits_count(s) - pos;
1050 int bits2= show>>(24-len);
1052 print_bin(bits2, len);
1054 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1057 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1058 int show= show_bits(s, n);
1059 int r= get_xbits(s, n);
1062 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1066 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1067 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1068 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1069 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1070 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1072 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1075 #define tprintf(...) {}
1078 /* define it to include statistics code (useful only for optimizing
1095 extern int st_current_index;
1096 extern unsigned int st_bit_counts[ST_NB];
1097 extern unsigned int st_out_bit_counts[ST_NB];
1099 void print_stats(void);
1102 /* misc math functions */
1103 extern const uint8_t ff_log2_tab[256];
1105 static inline int av_log2(unsigned int v)
1110 if (v & 0xffff0000) {
1118 n += ff_log2_tab[v];
1123 static inline int av_log2_16bit(unsigned int v)
1132 n += ff_log2_tab[v];
1138 static inline int mid_pred(int a, int b, int c)
1141 int t= (a-b)&((a-b)>>31);
1144 b-= (b-c)&((b-c)>>31);
1145 b+= (a-b)&((a-b)>>31);
1164 static inline int clip(int a, int amin, int amax)
1174 static inline int clip_uint8(int a)
1176 if (a&(~255)) return (-a)>>31;
1181 extern const uint8_t ff_sqrt_tab[128];
1183 int64_t ff_gcd(int64_t a, int64_t b);
1185 static inline int ff_sqrt(int a)
1191 if(a<128) return ff_sqrt_tab[a];
1193 for(s=15; s>=0; s--){
1194 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1204 * converts fourcc string to int
1206 static inline int ff_get_fourcc(const char *s){
1207 assert( strlen(s)==4 );
1209 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1212 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1213 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1217 #define MASK_ABS(mask, level)\
1222 : "+a" (level), "=&d" (mask)\
1225 #define MASK_ABS(mask, level)\
1227 level= (level^mask)-mask;
1231 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1232 #define COPY3_IF_LT(x,y,a,b,c,d)\
1235 "cmovl %3, %0 \n\t"\
1236 "cmovl %4, %1 \n\t"\
1237 "cmovl %5, %2 \n\t"\
1238 : "+r" (x), "+r" (a), "+r" (c)\
1239 : "r" (y), "r" (b), "r" (d)\
1242 #define COPY3_IF_LT(x,y,a,b,c,d)\
1251 static inline long long rdtsc()
1254 asm volatile( "rdtsc\n\t"
1260 #define START_TIMER \
1262 uint64_t tstart= rdtsc();\
1264 #define STOP_TIMER(id) \
1267 static uint64_t tsum=0;\
1268 static int tcount=0;\
1269 static int tskip_count=0;\
1270 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1271 tsum+= tend - tstart;\
1275 if(256*256*256*64%(tcount+tskip_count)==0){\
1276 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1281 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1283 /* avoid usage of various functions */
1284 #define malloc please_use_av_malloc
1285 #define free please_use_av_free
1286 #define realloc please_use_av_realloc
1287 #define time time_is_forbidden_due_to_security_issues
1288 #define rand rand_is_forbidden_due_to_state_trashing
1289 #define srand srand_is_forbidden_due_to_state_trashing
1290 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1291 #define printf please_use_av_log
1292 #define fprintf please_use_av_log
1295 #define CHECKED_ALLOCZ(p, size)\
1297 p= av_mallocz(size);\
1298 if(p==NULL && (size)!=0){\
1304 #endif /* HAVE_AV_CONFIG_H */
1306 #endif /* COMMON_H */