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. */
79 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80 # define always_inline __attribute__((always_inline)) inline
82 # define always_inline inline
85 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
86 # define attribute_used __attribute__((used))
88 # define attribute_used
91 #ifndef EMULATE_INTTYPES
92 # include <inttypes.h>
94 typedef signed char int8_t;
95 typedef signed short int16_t;
96 typedef signed int int32_t;
97 typedef unsigned char uint8_t;
98 typedef unsigned short uint16_t;
99 typedef unsigned int uint32_t;
102 typedef signed __int64 int64_t;
103 typedef unsigned __int64 uint64_t;
104 # else /* other OS */
105 typedef signed long long int64_t;
106 typedef unsigned long long uint64_t;
107 # endif /* other OS */
108 #endif /* HAVE_INTTYPES_H */
111 #define INT64_MAX int64_t_C(9223372036854775807)
114 #ifdef EMULATE_FAST_INT
115 /* note that we don't emulate 64bit ints */
116 typedef signed char int_fast8_t;
117 typedef signed int int_fast16_t;
118 typedef signed int int_fast32_t;
119 typedef unsigned char uint_fast8_t;
120 typedef unsigned int uint_fast16_t;
121 typedef unsigned int uint_fast32_t;
124 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
125 static inline float floorf(float f) {
135 # define int64_t_C(c) (c ## i64)
136 # define uint64_t_C(c) (c ## i64)
138 # ifdef HAVE_AV_CONFIG_H
139 # define inline __inline
143 # define int64_t_C(c) (c ## LL)
144 # define uint64_t_C(c) (c ## ULL)
145 # endif /* __MINGW32__ */
147 # ifdef HAVE_AV_CONFIG_H
152 # define snprintf _snprintf
153 # define vsnprintf _vsnprintf
156 /* CONFIG_WIN32 end */
157 #elif defined (CONFIG_OS2)
161 #define int64_t_C(c) (c ## LL)
162 #define uint64_t_C(c) (c ## ULL)
165 #ifdef HAVE_AV_CONFIG_H
167 #ifdef USE_FASTMEMCPY
168 #include "fastmemcpy.h"
173 #endif /* HAVE_AV_CONFIG_H */
181 #define int64_t_C(c) (c ## LL)
182 #define uint64_t_C(c) (c ## ULL)
185 #ifdef HAVE_AV_CONFIG_H
187 # ifdef USE_FASTMEMCPY
188 # include "fastmemcpy.h"
190 # endif /* HAVE_AV_CONFIG_H */
192 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
194 #ifdef HAVE_AV_CONFIG_H
198 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
199 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
200 # define MANGLE(a) "_" #a
202 # define MANGLE(a) #a
213 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
215 inline void dprintf(const char* fmt,...) {}
220 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
222 # define dprintf(fmt,...)
225 # endif /* !CONFIG_WIN32 */
227 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
229 //rounded divison & shift
230 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
232 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
233 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
235 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
236 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
238 extern const uint32_t inverse[256];
241 # define FASTDIV(a,b) \
246 :"=d"(ret),"=a"(dmy)\
247 :"1"(a),"g"(inverse[b])\
251 #elif defined(CONFIG_FASTDIV)
252 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
254 # define FASTDIV(a,b) ((a)/(b))
258 // avoid +32 for shift optimization (gcc should do that ...)
259 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
260 asm ("sarl %1, %0\n\t"
262 : "ic" ((uint8_t)(-s))
266 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
267 asm ("shrl %1, %0\n\t"
269 : "ic" ((uint8_t)(-s))
274 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
275 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
280 struct PutBitContext;
282 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
284 /* buf and buf_end must be present and used by every alternative writer. */
285 typedef struct PutBitContext {
286 #ifdef ALT_BITSTREAM_WRITER
287 uint8_t *buf, *buf_end;
292 uint8_t *buf, *buf_ptr, *buf_end;
296 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
299 s->buf_end = s->buf + buffer_size;
300 #ifdef ALT_BITSTREAM_WRITER
302 ((uint32_t*)(s->buf))[0]=0;
303 // memset(buffer, 0, buffer_size);
311 /* return the number of bits output */
312 static inline int put_bits_count(PutBitContext *s)
314 #ifdef ALT_BITSTREAM_WRITER
317 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
321 /* pad the end of the output stream with zeros */
322 static inline void flush_put_bits(PutBitContext *s)
324 #ifdef ALT_BITSTREAM_WRITER
327 s->bit_buf<<= s->bit_left;
328 while (s->bit_left < 32) {
329 /* XXX: should test end of buffer */
330 *s->buf_ptr++=s->bit_buf >> 24;
339 void align_put_bits(PutBitContext *s);
340 void put_string(PutBitContext * pbc, char *s, int put_zero);
343 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
344 typedef struct GetBitContext {
345 const uint8_t *buffer, *buffer_end;
346 #ifdef ALT_BITSTREAM_READER
348 #elif defined LIBMPEG2_BITSTREAM_READER
352 #elif defined A32_BITSTREAM_READER
353 uint32_t *buffer_ptr;
361 #define VLC_TYPE int16_t
365 VLC_TYPE (*table)[2]; ///< code, bits
366 int table_size, table_allocated;
369 typedef struct RL_VLC_ELEM {
376 #define UNALIGNED_STORES_ARE_BAD
379 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
381 # define unaligned32(a) (*(uint32_t*)(a))
384 static inline uint32_t unaligned32(const void *v) {
387 } __attribute__((packed));
389 return ((const struct Unaligned *) v)->i;
391 # elif defined(__DECC)
392 static inline uint32_t unaligned32(const void *v) {
393 return *(const __unaligned uint32_t *) v;
396 static inline uint32_t unaligned32(const void *v) {
397 return *(const uint32_t *) v;
402 #ifndef ALT_BITSTREAM_WRITER
403 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
405 unsigned int bit_buf;
409 st_out_bit_counts[st_current_index] += n;
411 // printf("put_bits=%d %x\n", n, value);
412 assert(n == 32 || value < (1U << n));
414 bit_buf = s->bit_buf;
415 bit_left = s->bit_left;
417 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
420 bit_buf = (bit_buf<<n) | value;
424 bit_buf |= value >> (n - bit_left);
425 #ifdef UNALIGNED_STORES_ARE_BAD
426 if (3 & (int) s->buf_ptr) {
427 s->buf_ptr[0] = bit_buf >> 24;
428 s->buf_ptr[1] = bit_buf >> 16;
429 s->buf_ptr[2] = bit_buf >> 8;
430 s->buf_ptr[3] = bit_buf ;
433 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
434 //printf("bitbuf = %08x\n", bit_buf);
440 s->bit_buf = bit_buf;
441 s->bit_left = bit_left;
446 #ifdef ALT_BITSTREAM_WRITER
447 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
449 # ifdef ALIGNED_BITSTREAM_WRITER
452 "movl %0, %%ecx \n\t"
453 "xorl %%eax, %%eax \n\t"
454 "shrdl %%cl, %1, %%eax \n\t"
456 "movl %0, %%ecx \n\t"
457 "shrl $3, %%ecx \n\t"
458 "andl $0xFFFFFFFC, %%ecx \n\t"
460 "orl %1, (%2, %%ecx) \n\t"
463 "movl %%eax, 4(%2, %%ecx) \n\t"
464 : "=&r" (s->index), "=&r" (value)
465 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
470 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
474 ptr[0] |= be2me_32(value>>(index&31));
475 ptr[1] = be2me_32(value<<(32-(index&31)));
476 //if(n>24) printf("%d %d\n", n, value);
480 # else //ALIGNED_BITSTREAM_WRITER
483 "movl $7, %%ecx \n\t"
484 "andl %0, %%ecx \n\t"
485 "addl %3, %%ecx \n\t"
489 "movl %0, %%ecx \n\t"
490 "shrl $3, %%ecx \n\t"
491 "orl %1, (%%ecx, %2) \n\t"
493 "movl $0, 4(%%ecx, %2) \n\t"
494 : "=&r" (s->index), "=&r" (value)
495 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
500 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
502 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
504 //if(n>24) printf("%d %d\n", n, value);
508 # endif //!ALIGNED_BITSTREAM_WRITER
513 static inline uint8_t* pbBufPtr(PutBitContext *s)
515 #ifdef ALT_BITSTREAM_WRITER
516 return s->buf + (s->index>>3);
524 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
526 static inline void skip_put_bytes(PutBitContext *s, int n){
527 assert((put_bits_count(s)&7)==0);
528 #ifdef ALT_BITSTREAM_WRITER
529 FIXME may need some cleaning of the buffer
532 assert(s->bit_left==32);
538 * Changes the end of the buffer.
540 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
541 s->buf_end= s->buf + size;
544 /* Bitstream reader API docs:
546 abritary name which is used as prefix for the internal variables
551 OPEN_READER(name, gb)
552 loads gb into local variables
554 CLOSE_READER(name, gb)
555 stores local vars in gb
557 UPDATE_CACHE(name, gb)
558 refills the internal cache from the bitstream
559 after this call at least MIN_CACHE_BITS will be available,
562 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
564 SHOW_UBITS(name, gb, num)
565 will return the nest num bits
567 SHOW_SBITS(name, gb, num)
568 will return the nest num bits and do sign extension
570 SKIP_BITS(name, gb, num)
571 will skip over the next num bits
572 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
574 SKIP_CACHE(name, gb, num)
575 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
577 SKIP_COUNTER(name, gb, num)
578 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
580 LAST_SKIP_CACHE(name, gb, num)
581 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
583 LAST_SKIP_BITS(name, gb, num)
584 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
586 for examples see get_bits, show_bits, skip_bits, get_vlc
589 static inline int unaligned32_be(const void *v)
593 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
595 return be2me_32( unaligned32(v)); //original
599 #ifdef ALT_BITSTREAM_READER
600 # define MIN_CACHE_BITS 25
602 # define OPEN_READER(name, gb)\
603 int name##_index= (gb)->index;\
604 int name##_cache= 0;\
606 # define CLOSE_READER(name, gb)\
607 (gb)->index= name##_index;\
609 # define UPDATE_CACHE(name, gb)\
610 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
612 # define SKIP_CACHE(name, gb, num)\
613 name##_cache <<= (num);\
616 # define SKIP_COUNTER(name, gb, num)\
617 name##_index += (num);\
619 # define SKIP_BITS(name, gb, num)\
621 SKIP_CACHE(name, gb, num)\
622 SKIP_COUNTER(name, gb, num)\
625 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
626 # define LAST_SKIP_CACHE(name, gb, num) ;
628 # define SHOW_UBITS(name, gb, num)\
629 NEG_USR32(name##_cache, num)
631 # define SHOW_SBITS(name, gb, num)\
632 NEG_SSR32(name##_cache, num)
634 # define GET_CACHE(name, gb)\
635 ((uint32_t)name##_cache)
637 static inline int get_bits_count(GetBitContext *s){
640 #elif defined LIBMPEG2_BITSTREAM_READER
641 //libmpeg2 like reader
643 # define MIN_CACHE_BITS 17
645 # define OPEN_READER(name, gb)\
646 int name##_bit_count=(gb)->bit_count;\
647 int name##_cache= (gb)->cache;\
648 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
650 # define CLOSE_READER(name, gb)\
651 (gb)->bit_count= name##_bit_count;\
652 (gb)->cache= name##_cache;\
653 (gb)->buffer_ptr= name##_buffer_ptr;\
655 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
657 # define UPDATE_CACHE(name, gb)\
658 if(name##_bit_count >= 0){\
659 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
660 ((uint16_t*)name##_buffer_ptr)++;\
661 name##_bit_count-= 16;\
666 # define UPDATE_CACHE(name, gb)\
667 if(name##_bit_count >= 0){\
668 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
669 name##_buffer_ptr+=2;\
670 name##_bit_count-= 16;\
675 # define SKIP_CACHE(name, gb, num)\
676 name##_cache <<= (num);\
678 # define SKIP_COUNTER(name, gb, num)\
679 name##_bit_count += (num);\
681 # define SKIP_BITS(name, gb, num)\
683 SKIP_CACHE(name, gb, num)\
684 SKIP_COUNTER(name, gb, num)\
687 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
688 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
690 # define SHOW_UBITS(name, gb, num)\
691 NEG_USR32(name##_cache, num)
693 # define SHOW_SBITS(name, gb, num)\
694 NEG_SSR32(name##_cache, num)
696 # define GET_CACHE(name, gb)\
697 ((uint32_t)name##_cache)
699 static inline int get_bits_count(GetBitContext *s){
700 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
703 #elif defined A32_BITSTREAM_READER
705 # define MIN_CACHE_BITS 32
707 # define OPEN_READER(name, gb)\
708 int name##_bit_count=(gb)->bit_count;\
709 uint32_t name##_cache0= (gb)->cache0;\
710 uint32_t name##_cache1= (gb)->cache1;\
711 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
713 # define CLOSE_READER(name, gb)\
714 (gb)->bit_count= name##_bit_count;\
715 (gb)->cache0= name##_cache0;\
716 (gb)->cache1= name##_cache1;\
717 (gb)->buffer_ptr= name##_buffer_ptr;\
719 # define UPDATE_CACHE(name, gb)\
720 if(name##_bit_count > 0){\
721 const uint32_t next= be2me_32( *name##_buffer_ptr );\
722 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
723 name##_cache1 |= next<<name##_bit_count;\
724 name##_buffer_ptr++;\
725 name##_bit_count-= 32;\
729 # define SKIP_CACHE(name, gb, num)\
731 "shldl %2, %1, %0 \n\t"\
733 : "+r" (name##_cache0), "+r" (name##_cache1)\
734 : "Ic" ((uint8_t)num)\
737 # define SKIP_CACHE(name, gb, num)\
738 name##_cache0 <<= (num);\
739 name##_cache0 |= NEG_USR32(name##_cache1,num);\
740 name##_cache1 <<= (num);
743 # define SKIP_COUNTER(name, gb, num)\
744 name##_bit_count += (num);\
746 # define SKIP_BITS(name, gb, num)\
748 SKIP_CACHE(name, gb, num)\
749 SKIP_COUNTER(name, gb, num)\
752 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
753 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
755 # define SHOW_UBITS(name, gb, num)\
756 NEG_USR32(name##_cache0, num)
758 # define SHOW_SBITS(name, gb, num)\
759 NEG_SSR32(name##_cache0, num)
761 # define GET_CACHE(name, gb)\
764 static inline int get_bits_count(GetBitContext *s){
765 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
771 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
772 * if MSB not set it is negative
773 * @param n length in bits
776 static inline int get_xbits(GetBitContext *s, int n){
778 register int32_t cache;
781 cache = GET_CACHE(re,s);
782 if ((int32_t)cache<0) { //MSB=1
783 tmp = NEG_USR32(cache,n);
785 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
786 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
787 tmp = - NEG_USR32(~cache,n);
789 LAST_SKIP_BITS(re, s, n)
794 static inline int get_sbits(GetBitContext *s, int n){
798 tmp= SHOW_SBITS(re, s, n);
799 LAST_SKIP_BITS(re, s, n)
806 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
808 static inline unsigned int get_bits(GetBitContext *s, int n){
812 tmp= SHOW_UBITS(re, s, n);
813 LAST_SKIP_BITS(re, s, n)
818 unsigned int get_bits_long(GetBitContext *s, int n);
822 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
824 static inline unsigned int show_bits(GetBitContext *s, int n){
828 tmp= SHOW_UBITS(re, s, n);
829 // CLOSE_READER(re, s)
833 unsigned int show_bits_long(GetBitContext *s, int n);
835 static inline void skip_bits(GetBitContext *s, int n){
836 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
839 LAST_SKIP_BITS(re, s, n)
843 static inline unsigned int get_bits1(GetBitContext *s){
844 #ifdef ALT_BITSTREAM_READER
846 uint8_t result= s->buffer[ index>>3 ];
847 result<<= (index&0x07);
854 return get_bits(s, 1);
858 static inline unsigned int show_bits1(GetBitContext *s){
859 return show_bits(s, 1);
862 static inline void skip_bits1(GetBitContext *s){
867 * init GetBitContext.
868 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
869 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
870 * @param bit_size the size of the buffer in bits
872 static inline void init_get_bits(GetBitContext *s,
873 const uint8_t *buffer, int bit_size)
875 const int buffer_size= (bit_size+7)>>3;
878 s->size_in_bits= bit_size;
879 s->buffer_end= buffer + buffer_size;
880 #ifdef ALT_BITSTREAM_READER
882 #elif defined LIBMPEG2_BITSTREAM_READER
883 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
886 s->cache = (*buffer++)<<24;
887 s->buffer_ptr = buffer;
892 s->buffer_ptr = buffer;
896 #elif defined A32_BITSTREAM_READER
897 s->buffer_ptr = (uint32_t*)buffer;
908 #ifdef A32_BITSTREAM_READER
913 int check_marker(GetBitContext *s, const char *msg);
914 void align_get_bits(GetBitContext *s);
915 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
916 const void *bits, int bits_wrap, int bits_size,
917 const void *codes, int codes_wrap, int codes_size);
918 void free_vlc(VLC *vlc);
922 * if the vlc code is invalid and max_depth=1 than no bits will be removed
923 * if the vlc code is invalid and max_depth>1 than the number of bits removed
926 #define GET_VLC(code, name, gb, table, bits, max_depth)\
928 int n, index, nb_bits;\
930 index= SHOW_UBITS(name, gb, bits);\
931 code = table[index][0];\
932 n = table[index][1];\
934 if(max_depth > 1 && n < 0){\
935 LAST_SKIP_BITS(name, gb, bits)\
936 UPDATE_CACHE(name, gb)\
940 index= SHOW_UBITS(name, gb, nb_bits) + code;\
941 code = table[index][0];\
942 n = table[index][1];\
943 if(max_depth > 2 && n < 0){\
944 LAST_SKIP_BITS(name, gb, nb_bits)\
945 UPDATE_CACHE(name, gb)\
949 index= SHOW_UBITS(name, gb, nb_bits) + code;\
950 code = table[index][0];\
951 n = table[index][1];\
954 SKIP_BITS(name, gb, n)\
957 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
959 int n, index, nb_bits;\
961 index= SHOW_UBITS(name, gb, bits);\
962 level = table[index].level;\
963 n = table[index].len;\
965 if(max_depth > 1 && n < 0){\
966 LAST_SKIP_BITS(name, gb, bits)\
967 UPDATE_CACHE(name, gb)\
971 index= SHOW_UBITS(name, gb, nb_bits) + level;\
972 level = table[index].level;\
973 n = table[index].len;\
975 run= table[index].run;\
976 SKIP_BITS(name, gb, n)\
979 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
980 static inline int get_vlc(GetBitContext *s, VLC *vlc)
983 VLC_TYPE (*table)[2]= vlc->table;
988 GET_VLC(code, re, s, table, vlc->bits, 3)
995 * parses a vlc code, faster then get_vlc()
996 * @param bits is the number of bits which will be read at once, must be
997 * identical to nb_bits in init_vlc()
998 * @param max_depth is the number of times bits bits must be readed to completly
999 * read the longest vlc code
1000 * = (max_vlc_length + bits - 1) / bits
1002 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1003 int bits, int max_depth)
1010 GET_VLC(code, re, s, table, bits, max_depth)
1020 static inline void print_bin(int bits, int n){
1023 for(i=n-1; i>=0; i--){
1024 printf("%d", (bits>>i)&1);
1030 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1031 int r= get_bits(s, n);
1034 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1037 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1038 int show= show_bits(s, 24);
1039 int pos= get_bits_count(s);
1040 int r= get_vlc2(s, table, bits, max_depth);
1041 int len= get_bits_count(s) - pos;
1042 int bits2= show>>(24-len);
1044 print_bin(bits2, len);
1046 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1049 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1050 int show= show_bits(s, n);
1051 int r= get_xbits(s, n);
1054 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1058 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1059 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1060 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1061 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1062 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1064 #define tprintf printf
1067 #define tprintf(...) {}
1070 /* define it to include statistics code (useful only for optimizing
1087 extern int st_current_index;
1088 extern unsigned int st_bit_counts[ST_NB];
1089 extern unsigned int st_out_bit_counts[ST_NB];
1091 void print_stats(void);
1094 /* misc math functions */
1095 extern const uint8_t ff_log2_tab[256];
1097 static inline int av_log2(unsigned int v)
1102 if (v & 0xffff0000) {
1110 n += ff_log2_tab[v];
1115 static inline int av_log2_16bit(unsigned int v)
1124 n += ff_log2_tab[v];
1130 static inline int mid_pred(int a, int b, int c)
1133 int t= (a-b)&((a-b)>>31);
1136 b-= (b-c)&((b-c)>>31);
1137 b+= (a-b)&((a-b)>>31);
1156 static inline int clip(int a, int amin, int amax)
1167 extern const uint8_t ff_sqrt_tab[128];
1169 int64_t ff_gcd(int64_t a, int64_t b);
1171 static inline int ff_sqrt(int a)
1177 if(a<128) return ff_sqrt_tab[a];
1179 for(s=15; s>=0; s--){
1180 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1190 * converts fourcc string to int
1192 static inline int ff_get_fourcc(const char *s){
1193 assert( strlen(s)==4 );
1195 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1198 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1199 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1203 #define MASK_ABS(mask, level)\
1208 : "+a" (level), "=&d" (mask)\
1211 #define MASK_ABS(mask, level)\
1213 level= (level^mask)-mask;
1217 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1218 #define COPY3_IF_LT(x,y,a,b,c,d)\
1221 "cmovl %3, %0 \n\t"\
1222 "cmovl %4, %1 \n\t"\
1223 "cmovl %5, %2 \n\t"\
1224 : "+r" (x), "+r" (a), "+r" (c)\
1225 : "r" (y), "r" (b), "r" (d)\
1228 #define COPY3_IF_LT(x,y,a,b,c,d)\
1237 static inline long long rdtsc()
1240 asm volatile( "rdtsc\n\t"
1246 #define START_TIMER \
1248 uint64_t tstart= rdtsc();\
1250 #define STOP_TIMER(id) \
1253 static uint64_t tsum=0;\
1254 static int tcount=0;\
1255 static int tskip_count=0;\
1256 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1257 tsum+= tend - tstart;\
1261 if(256*256*256*64%(tcount+tskip_count)==0){\
1262 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1267 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1269 /* avoid usage of various functions */
1270 #define malloc please_use_av_malloc
1271 #define free please_use_av_free
1272 #define realloc please_use_av_realloc
1273 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1274 #define printf please_use_av_log
1275 #define fprintf please_use_av_log
1278 #define CHECKED_ALLOCZ(p, size)\
1280 p= av_mallocz(size);\
1281 if(p==NULL && (size)!=0){\
1287 #endif /* HAVE_AV_CONFIG_H */
1289 #endif /* COMMON_H */