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 INT64_MAX int64_t_C(9223372036854775807)
120 #define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
123 #ifdef EMULATE_FAST_INT
124 /* note that we don't emulate 64bit ints */
125 typedef signed char int_fast8_t;
126 typedef signed int int_fast16_t;
127 typedef signed int int_fast32_t;
128 typedef unsigned char uint_fast8_t;
129 typedef unsigned int uint_fast16_t;
130 typedef unsigned int uint_fast32_t;
134 # if INT_MAX != 2147483647
141 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
142 static inline float floorf(float f) {
151 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
152 # define int64_t_C(c) (c ## i64)
153 # define uint64_t_C(c) (c ## i64)
155 # ifdef HAVE_AV_CONFIG_H
156 # define inline __inline
160 # define int64_t_C(c) (c ## LL)
161 # define uint64_t_C(c) (c ## ULL)
162 # endif /* __MINGW32__ */
164 # ifdef HAVE_AV_CONFIG_H
169 # define snprintf _snprintf
170 # define vsnprintf _vsnprintf
173 /* CONFIG_WIN32 end */
174 #elif defined (CONFIG_OS2)
178 #define int64_t_C(c) (c ## LL)
179 #define uint64_t_C(c) (c ## ULL)
182 #ifdef HAVE_AV_CONFIG_H
184 #ifdef USE_FASTMEMCPY
185 #include "fastmemcpy.h"
190 #endif /* HAVE_AV_CONFIG_H */
198 #define int64_t_C(c) (c ## LL)
199 #define uint64_t_C(c) (c ## ULL)
202 #ifdef HAVE_AV_CONFIG_H
204 # ifdef USE_FASTMEMCPY
205 # include "fastmemcpy.h"
207 # endif /* HAVE_AV_CONFIG_H */
209 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
211 #ifdef HAVE_AV_CONFIG_H
215 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
216 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
217 # define MANGLE(a) "_" #a
219 # define MANGLE(a) #a
230 # if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
232 inline void dprintf(const char* fmt,...) {}
237 # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
239 # define dprintf(fmt,...)
242 # endif /* !CONFIG_WIN32 */
244 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
246 //rounded divison & shift
247 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
249 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
250 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
252 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
253 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
255 extern const uint32_t inverse[256];
258 # define FASTDIV(a,b) \
263 :"=d"(ret),"=a"(dmy)\
264 :"1"(a),"g"(inverse[b])\
268 #elif defined(CONFIG_FASTDIV)
269 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
271 # define FASTDIV(a,b) ((a)/(b))
275 // avoid +32 for shift optimization (gcc should do that ...)
276 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
277 asm ("sarl %1, %0\n\t"
279 : "ic" ((uint8_t)(-s))
283 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
284 asm ("shrl %1, %0\n\t"
286 : "ic" ((uint8_t)(-s))
291 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
292 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
297 /* buf and buf_end must be present and used by every alternative writer. */
298 typedef struct PutBitContext {
299 #ifdef ALT_BITSTREAM_WRITER
300 uint8_t *buf, *buf_end;
305 uint8_t *buf, *buf_ptr, *buf_end;
309 static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
312 s->buf_end = s->buf + buffer_size;
313 #ifdef ALT_BITSTREAM_WRITER
315 ((uint32_t*)(s->buf))[0]=0;
316 // memset(buffer, 0, buffer_size);
324 /* return the number of bits output */
325 static inline int put_bits_count(PutBitContext *s)
327 #ifdef ALT_BITSTREAM_WRITER
330 return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
334 /* pad the end of the output stream with zeros */
335 static inline void flush_put_bits(PutBitContext *s)
337 #ifdef ALT_BITSTREAM_WRITER
340 s->bit_buf<<= s->bit_left;
341 while (s->bit_left < 32) {
342 /* XXX: should test end of buffer */
343 *s->buf_ptr++=s->bit_buf >> 24;
352 void align_put_bits(PutBitContext *s);
353 void put_string(PutBitContext * pbc, char *s, int put_zero);
356 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
357 typedef struct GetBitContext {
358 const uint8_t *buffer, *buffer_end;
359 #ifdef ALT_BITSTREAM_READER
361 #elif defined LIBMPEG2_BITSTREAM_READER
365 #elif defined A32_BITSTREAM_READER
366 uint32_t *buffer_ptr;
374 #define VLC_TYPE int16_t
378 VLC_TYPE (*table)[2]; ///< code, bits
379 int table_size, table_allocated;
382 typedef struct RL_VLC_ELEM {
389 #define UNALIGNED_STORES_ARE_BAD
392 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
394 # define unaligned32(a) (*(uint32_t*)(a))
397 static inline uint32_t unaligned32(const void *v) {
400 } __attribute__((packed));
402 return ((const struct Unaligned *) v)->i;
404 # elif defined(__DECC)
405 static inline uint32_t unaligned32(const void *v) {
406 return *(const __unaligned uint32_t *) v;
409 static inline uint32_t unaligned32(const void *v) {
410 return *(const uint32_t *) v;
415 #ifndef ALT_BITSTREAM_WRITER
416 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
418 unsigned int bit_buf;
422 st_out_bit_counts[st_current_index] += n;
424 // printf("put_bits=%d %x\n", n, value);
425 assert(n == 32 || value < (1U << n));
427 bit_buf = s->bit_buf;
428 bit_left = s->bit_left;
430 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
433 bit_buf = (bit_buf<<n) | value;
437 bit_buf |= value >> (n - bit_left);
438 #ifdef UNALIGNED_STORES_ARE_BAD
439 if (3 & (intptr_t) s->buf_ptr) {
440 s->buf_ptr[0] = bit_buf >> 24;
441 s->buf_ptr[1] = bit_buf >> 16;
442 s->buf_ptr[2] = bit_buf >> 8;
443 s->buf_ptr[3] = bit_buf ;
446 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
447 //printf("bitbuf = %08x\n", bit_buf);
453 s->bit_buf = bit_buf;
454 s->bit_left = bit_left;
459 #ifdef ALT_BITSTREAM_WRITER
460 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
462 # ifdef ALIGNED_BITSTREAM_WRITER
465 "movl %0, %%ecx \n\t"
466 "xorl %%eax, %%eax \n\t"
467 "shrdl %%cl, %1, %%eax \n\t"
469 "movl %0, %%ecx \n\t"
470 "shrl $3, %%ecx \n\t"
471 "andl $0xFFFFFFFC, %%ecx \n\t"
473 "orl %1, (%2, %%ecx) \n\t"
476 "movl %%eax, 4(%2, %%ecx) \n\t"
477 : "=&r" (s->index), "=&r" (value)
478 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
483 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
487 ptr[0] |= be2me_32(value>>(index&31));
488 ptr[1] = be2me_32(value<<(32-(index&31)));
489 //if(n>24) printf("%d %d\n", n, value);
493 # else //ALIGNED_BITSTREAM_WRITER
496 "movl $7, %%ecx \n\t"
497 "andl %0, %%ecx \n\t"
498 "addl %3, %%ecx \n\t"
502 "movl %0, %%ecx \n\t"
503 "shrl $3, %%ecx \n\t"
504 "orl %1, (%%ecx, %2) \n\t"
506 "movl $0, 4(%%ecx, %2) \n\t"
507 : "=&r" (s->index), "=&r" (value)
508 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
513 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
515 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
517 //if(n>24) printf("%d %d\n", n, value);
521 # endif //!ALIGNED_BITSTREAM_WRITER
526 static inline uint8_t* pbBufPtr(PutBitContext *s)
528 #ifdef ALT_BITSTREAM_WRITER
529 return s->buf + (s->index>>3);
537 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
539 static inline void skip_put_bytes(PutBitContext *s, int n){
540 assert((put_bits_count(s)&7)==0);
541 #ifdef ALT_BITSTREAM_WRITER
542 FIXME may need some cleaning of the buffer
545 assert(s->bit_left==32);
551 * Changes the end of the buffer.
553 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
554 s->buf_end= s->buf + size;
557 /* Bitstream reader API docs:
559 abritary name which is used as prefix for the internal variables
564 OPEN_READER(name, gb)
565 loads gb into local variables
567 CLOSE_READER(name, gb)
568 stores local vars in gb
570 UPDATE_CACHE(name, gb)
571 refills the internal cache from the bitstream
572 after this call at least MIN_CACHE_BITS will be available,
575 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
577 SHOW_UBITS(name, gb, num)
578 will return the nest num bits
580 SHOW_SBITS(name, gb, num)
581 will return the nest num bits and do sign extension
583 SKIP_BITS(name, gb, num)
584 will skip over the next num bits
585 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
587 SKIP_CACHE(name, gb, num)
588 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
590 SKIP_COUNTER(name, gb, num)
591 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
593 LAST_SKIP_CACHE(name, gb, num)
594 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
596 LAST_SKIP_BITS(name, gb, num)
597 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
599 for examples see get_bits, show_bits, skip_bits, get_vlc
602 static inline int unaligned32_be(const void *v)
606 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
608 return be2me_32( unaligned32(v)); //original
612 #ifdef ALT_BITSTREAM_READER
613 # define MIN_CACHE_BITS 25
615 # define OPEN_READER(name, gb)\
616 int name##_index= (gb)->index;\
617 int name##_cache= 0;\
619 # define CLOSE_READER(name, gb)\
620 (gb)->index= name##_index;\
622 # define UPDATE_CACHE(name, gb)\
623 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
625 # define SKIP_CACHE(name, gb, num)\
626 name##_cache <<= (num);\
629 # define SKIP_COUNTER(name, gb, num)\
630 name##_index += (num);\
632 # define SKIP_BITS(name, gb, num)\
634 SKIP_CACHE(name, gb, num)\
635 SKIP_COUNTER(name, gb, num)\
638 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
639 # define LAST_SKIP_CACHE(name, gb, num) ;
641 # define SHOW_UBITS(name, gb, num)\
642 NEG_USR32(name##_cache, num)
644 # define SHOW_SBITS(name, gb, num)\
645 NEG_SSR32(name##_cache, num)
647 # define GET_CACHE(name, gb)\
648 ((uint32_t)name##_cache)
650 static inline int get_bits_count(GetBitContext *s){
653 #elif defined LIBMPEG2_BITSTREAM_READER
654 //libmpeg2 like reader
656 # define MIN_CACHE_BITS 17
658 # define OPEN_READER(name, gb)\
659 int name##_bit_count=(gb)->bit_count;\
660 int name##_cache= (gb)->cache;\
661 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
663 # define CLOSE_READER(name, gb)\
664 (gb)->bit_count= name##_bit_count;\
665 (gb)->cache= name##_cache;\
666 (gb)->buffer_ptr= name##_buffer_ptr;\
668 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
670 # define UPDATE_CACHE(name, gb)\
671 if(name##_bit_count >= 0){\
672 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
673 ((uint16_t*)name##_buffer_ptr)++;\
674 name##_bit_count-= 16;\
679 # define UPDATE_CACHE(name, gb)\
680 if(name##_bit_count >= 0){\
681 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
682 name##_buffer_ptr+=2;\
683 name##_bit_count-= 16;\
688 # define SKIP_CACHE(name, gb, num)\
689 name##_cache <<= (num);\
691 # define SKIP_COUNTER(name, gb, num)\
692 name##_bit_count += (num);\
694 # define SKIP_BITS(name, gb, num)\
696 SKIP_CACHE(name, gb, num)\
697 SKIP_COUNTER(name, gb, num)\
700 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
701 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
703 # define SHOW_UBITS(name, gb, num)\
704 NEG_USR32(name##_cache, num)
706 # define SHOW_SBITS(name, gb, num)\
707 NEG_SSR32(name##_cache, num)
709 # define GET_CACHE(name, gb)\
710 ((uint32_t)name##_cache)
712 static inline int get_bits_count(GetBitContext *s){
713 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
716 #elif defined A32_BITSTREAM_READER
718 # define MIN_CACHE_BITS 32
720 # define OPEN_READER(name, gb)\
721 int name##_bit_count=(gb)->bit_count;\
722 uint32_t name##_cache0= (gb)->cache0;\
723 uint32_t name##_cache1= (gb)->cache1;\
724 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
726 # define CLOSE_READER(name, gb)\
727 (gb)->bit_count= name##_bit_count;\
728 (gb)->cache0= name##_cache0;\
729 (gb)->cache1= name##_cache1;\
730 (gb)->buffer_ptr= name##_buffer_ptr;\
732 # define UPDATE_CACHE(name, gb)\
733 if(name##_bit_count > 0){\
734 const uint32_t next= be2me_32( *name##_buffer_ptr );\
735 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
736 name##_cache1 |= next<<name##_bit_count;\
737 name##_buffer_ptr++;\
738 name##_bit_count-= 32;\
742 # define SKIP_CACHE(name, gb, num)\
744 "shldl %2, %1, %0 \n\t"\
746 : "+r" (name##_cache0), "+r" (name##_cache1)\
747 : "Ic" ((uint8_t)num)\
750 # define SKIP_CACHE(name, gb, num)\
751 name##_cache0 <<= (num);\
752 name##_cache0 |= NEG_USR32(name##_cache1,num);\
753 name##_cache1 <<= (num);
756 # define SKIP_COUNTER(name, gb, num)\
757 name##_bit_count += (num);\
759 # define SKIP_BITS(name, gb, num)\
761 SKIP_CACHE(name, gb, num)\
762 SKIP_COUNTER(name, gb, num)\
765 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
766 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
768 # define SHOW_UBITS(name, gb, num)\
769 NEG_USR32(name##_cache0, num)
771 # define SHOW_SBITS(name, gb, num)\
772 NEG_SSR32(name##_cache0, num)
774 # define GET_CACHE(name, gb)\
777 static inline int get_bits_count(GetBitContext *s){
778 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
784 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
785 * if MSB not set it is negative
786 * @param n length in bits
789 static inline int get_xbits(GetBitContext *s, int n){
791 register int32_t cache;
794 cache = GET_CACHE(re,s);
795 if ((int32_t)cache<0) { //MSB=1
796 tmp = NEG_USR32(cache,n);
798 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
799 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
800 tmp = - NEG_USR32(~cache,n);
802 LAST_SKIP_BITS(re, s, n)
807 static inline int get_sbits(GetBitContext *s, int n){
811 tmp= SHOW_SBITS(re, s, n);
812 LAST_SKIP_BITS(re, s, n)
819 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
821 static inline unsigned int get_bits(GetBitContext *s, int n){
825 tmp= SHOW_UBITS(re, s, n);
826 LAST_SKIP_BITS(re, s, n)
831 unsigned int get_bits_long(GetBitContext *s, int n);
835 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
837 static inline unsigned int show_bits(GetBitContext *s, int n){
841 tmp= SHOW_UBITS(re, s, n);
842 // CLOSE_READER(re, s)
846 unsigned int show_bits_long(GetBitContext *s, int n);
848 static inline void skip_bits(GetBitContext *s, int n){
849 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
852 LAST_SKIP_BITS(re, s, n)
856 static inline unsigned int get_bits1(GetBitContext *s){
857 #ifdef ALT_BITSTREAM_READER
859 uint8_t result= s->buffer[ index>>3 ];
860 result<<= (index&0x07);
867 return get_bits(s, 1);
871 static inline unsigned int show_bits1(GetBitContext *s){
872 return show_bits(s, 1);
875 static inline void skip_bits1(GetBitContext *s){
880 * init GetBitContext.
881 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
882 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
883 * @param bit_size the size of the buffer in bits
885 static inline void init_get_bits(GetBitContext *s,
886 const uint8_t *buffer, int bit_size)
888 const int buffer_size= (bit_size+7)>>3;
891 s->size_in_bits= bit_size;
892 s->buffer_end= buffer + buffer_size;
893 #ifdef ALT_BITSTREAM_READER
895 #elif defined LIBMPEG2_BITSTREAM_READER
896 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
899 s->cache = (*buffer++)<<24;
900 s->buffer_ptr = buffer;
905 s->buffer_ptr = buffer;
909 #elif defined A32_BITSTREAM_READER
910 s->buffer_ptr = (uint32_t*)buffer;
921 #ifdef A32_BITSTREAM_READER
926 int check_marker(GetBitContext *s, const char *msg);
927 void align_get_bits(GetBitContext *s);
928 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
929 const void *bits, int bits_wrap, int bits_size,
930 const void *codes, int codes_wrap, int codes_size);
931 void free_vlc(VLC *vlc);
935 * if the vlc code is invalid and max_depth=1 than no bits will be removed
936 * if the vlc code is invalid and max_depth>1 than the number of bits removed
939 #define GET_VLC(code, name, gb, table, bits, max_depth)\
941 int n, index, nb_bits;\
943 index= SHOW_UBITS(name, gb, bits);\
944 code = table[index][0];\
945 n = table[index][1];\
947 if(max_depth > 1 && n < 0){\
948 LAST_SKIP_BITS(name, gb, bits)\
949 UPDATE_CACHE(name, gb)\
953 index= SHOW_UBITS(name, gb, nb_bits) + code;\
954 code = table[index][0];\
955 n = table[index][1];\
956 if(max_depth > 2 && n < 0){\
957 LAST_SKIP_BITS(name, gb, nb_bits)\
958 UPDATE_CACHE(name, gb)\
962 index= SHOW_UBITS(name, gb, nb_bits) + code;\
963 code = table[index][0];\
964 n = table[index][1];\
967 SKIP_BITS(name, gb, n)\
970 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
972 int n, index, nb_bits;\
974 index= SHOW_UBITS(name, gb, bits);\
975 level = table[index].level;\
976 n = table[index].len;\
978 if(max_depth > 1 && n < 0){\
979 LAST_SKIP_BITS(name, gb, bits)\
980 UPDATE_CACHE(name, gb)\
984 index= SHOW_UBITS(name, gb, nb_bits) + level;\
985 level = table[index].level;\
986 n = table[index].len;\
988 run= table[index].run;\
989 SKIP_BITS(name, gb, n)\
992 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
993 static inline int get_vlc(GetBitContext *s, VLC *vlc)
996 VLC_TYPE (*table)[2]= vlc->table;
1001 GET_VLC(code, re, s, table, vlc->bits, 3)
1008 * parses a vlc code, faster then get_vlc()
1009 * @param bits is the number of bits which will be read at once, must be
1010 * identical to nb_bits in init_vlc()
1011 * @param max_depth is the number of times bits bits must be readed to completly
1012 * read the longest vlc code
1013 * = (max_vlc_length + bits - 1) / bits
1015 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1016 int bits, int max_depth)
1023 GET_VLC(code, re, s, table, bits, max_depth)
1033 static inline void print_bin(int bits, int n){
1036 for(i=n-1; i>=0; i--){
1037 printf("%d", (bits>>i)&1);
1043 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1044 int r= get_bits(s, n);
1047 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1050 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1051 int show= show_bits(s, 24);
1052 int pos= get_bits_count(s);
1053 int r= get_vlc2(s, table, bits, max_depth);
1054 int len= get_bits_count(s) - pos;
1055 int bits2= show>>(24-len);
1057 print_bin(bits2, len);
1059 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1062 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1063 int show= show_bits(s, n);
1064 int r= get_xbits(s, n);
1067 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1071 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1072 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1073 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1074 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1075 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1077 #define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1080 #define tprintf(...) {}
1083 /* define it to include statistics code (useful only for optimizing
1100 extern int st_current_index;
1101 extern unsigned int st_bit_counts[ST_NB];
1102 extern unsigned int st_out_bit_counts[ST_NB];
1104 void print_stats(void);
1107 /* misc math functions */
1108 extern const uint8_t ff_log2_tab[256];
1110 static inline int av_log2(unsigned int v)
1115 if (v & 0xffff0000) {
1123 n += ff_log2_tab[v];
1128 static inline int av_log2_16bit(unsigned int v)
1137 n += ff_log2_tab[v];
1143 static inline int mid_pred(int a, int b, int c)
1146 int t= (a-b)&((a-b)>>31);
1149 b-= (b-c)&((b-c)>>31);
1150 b+= (a-b)&((a-b)>>31);
1169 static inline int clip(int a, int amin, int amax)
1179 static inline int clip_uint8(int a)
1181 if (a&(~255)) return (-a)>>31;
1186 extern const uint8_t ff_sqrt_tab[128];
1188 int64_t ff_gcd(int64_t a, int64_t b);
1190 static inline int ff_sqrt(int a)
1196 if(a<128) return ff_sqrt_tab[a];
1198 for(s=15; s>=0; s--){
1199 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1209 * converts fourcc string to int
1211 static inline int ff_get_fourcc(const char *s){
1212 assert( strlen(s)==4 );
1214 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1217 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1218 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1222 #define MASK_ABS(mask, level)\
1227 : "+a" (level), "=&d" (mask)\
1230 #define MASK_ABS(mask, level)\
1232 level= (level^mask)-mask;
1236 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1237 #define COPY3_IF_LT(x,y,a,b,c,d)\
1240 "cmovl %3, %0 \n\t"\
1241 "cmovl %4, %1 \n\t"\
1242 "cmovl %5, %2 \n\t"\
1243 : "+r" (x), "+r" (a), "+r" (c)\
1244 : "r" (y), "r" (b), "r" (d)\
1247 #define COPY3_IF_LT(x,y,a,b,c,d)\
1256 static inline long long rdtsc()
1259 asm volatile( "rdtsc\n\t"
1265 #define START_TIMER \
1267 uint64_t tstart= rdtsc();\
1269 #define STOP_TIMER(id) \
1272 static uint64_t tsum=0;\
1273 static int tcount=0;\
1274 static int tskip_count=0;\
1275 if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1276 tsum+= tend - tstart;\
1280 if(256*256*256*64%(tcount+tskip_count)==0){\
1281 av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1286 #define STOP_TIMER(id) {}
1289 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1291 /* avoid usage of various functions */
1292 #define malloc please_use_av_malloc
1293 #define free please_use_av_free
1294 #define realloc please_use_av_realloc
1295 #define time time_is_forbidden_due_to_security_issues
1296 #define rand rand_is_forbidden_due_to_state_trashing
1297 #define srand srand_is_forbidden_due_to_state_trashing
1298 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1299 #define printf please_use_av_log
1300 #define fprintf please_use_av_log
1303 #define CHECKED_ALLOCZ(p, size)\
1305 p= av_mallocz(size);\
1306 if(p==NULL && (size)!=0){\
1312 #endif /* HAVE_AV_CONFIG_H */
1314 #endif /* COMMON_H */