3 * common internal api header.
9 #define FFMPEG_VERSION_INT 0x000406
10 #define FFMPEG_VERSION "0.4.6"
12 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
16 //#define ALT_BITSTREAM_WRITER
17 //#define ALIGNED_BITSTREAM_WRITER
19 #define ALT_BITSTREAM_READER
20 //#define LIBMPEG2_BITSTREAM_READER
21 //#define A32_BITSTREAM_READER
23 #ifdef HAVE_AV_CONFIG_H
24 /* only include the following when compiling package */
42 #define M_PI 3.14159265358979323846
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_FLAG(name, help, field, flag, defval) \
53 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
54 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
55 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
56 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
57 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
58 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
59 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
60 #define AVOPTION_SUB(ptr) { .name = NULL, .sub = ptr }
61 #define AVOPTION_END() AVOPTION_SUB(NULL)
63 #endif /* HAVE_AV_CONFIG_H */
65 /* Suppress restrict if it was not defined in config.h. */
70 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
71 # define always_inline __attribute__((always_inline)) inline
73 # define always_inline inline
80 typedef unsigned short uint16_t;
81 typedef signed short int16_t;
82 typedef unsigned char uint8_t;
83 typedef unsigned int uint32_t;
84 typedef unsigned __int64 uint64_t;
85 typedef signed char int8_t;
86 typedef signed int int32_t;
87 typedef signed __int64 int64_t;
90 # define int64_t_C(c) (c ## i64)
91 # define uint64_t_C(c) (c ## i64)
93 # define inline __inline
96 # define int64_t_C(c) (c ## LL)
97 # define uint64_t_C(c) (c ## ULL)
98 # endif /* __MINGW32__ */
104 # define snprintf _snprintf
105 # define vsnprintf _vsnprintf
107 /* CONFIG_WIN32 end */
108 #elif defined (CONFIG_OS2)
111 #include <inttypes.h>
113 #ifdef HAVE_AV_CONFIG_H
116 #define int64_t_C(c) (c ## LL)
117 #define uint64_t_C(c) (c ## ULL)
120 #ifdef USE_FASTMEMCPY
121 #include "fastmemcpy.h"
126 #endif /* HAVE_AV_CONFIG_H */
133 # include <inttypes.h>
135 # ifdef HAVE_AV_CONFIG_H
137 # define int64_t_C(c) (c ## LL)
138 # define uint64_t_C(c) (c ## ULL)
141 # ifdef USE_FASTMEMCPY
142 # include "fastmemcpy.h"
144 # endif /* HAVE_AV_CONFIG_H */
146 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
148 #ifdef HAVE_AV_CONFIG_H
152 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
153 defined(__OS2__) || defined (__OpenBSD__)
154 # define MANGLE(a) "_" #a
156 # define MANGLE(a) #a
167 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
169 inline void dprintf(const char* fmt,...) {}
174 # define dprintf(fmt,args...) printf(fmt, ## args)
176 # define dprintf(fmt,args...)
179 # endif /* !CONFIG_WIN32 */
181 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
183 //rounded divison & shift
184 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
186 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
187 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
189 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
190 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
193 // avoid +32 for shift optimization (gcc should do that ...)
194 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
195 asm ("sarl %1, %0\n\t"
197 : "ic" ((uint8_t)(-s))
201 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
202 asm ("shrl %1, %0\n\t"
204 : "ic" ((uint8_t)(-s))
209 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
210 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
215 struct PutBitContext;
217 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
219 typedef struct PutBitContext {
220 #ifdef ALT_BITSTREAM_WRITER
221 uint8_t *buf, *buf_end;
226 uint8_t *buf, *buf_ptr, *buf_end;
228 int64_t data_out_size; /* in bytes */
231 void init_put_bits(PutBitContext *s,
232 uint8_t *buffer, int buffer_size,
234 void (*write_data)(void *, uint8_t *, int));
236 int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */
237 void align_put_bits(PutBitContext *s);
238 void flush_put_bits(PutBitContext *s);
239 void put_string(PutBitContext * pbc, char *s);
243 typedef struct GetBitContext {
244 const uint8_t *buffer, *buffer_end;
245 #ifdef ALT_BITSTREAM_READER
247 #elif defined LIBMPEG2_BITSTREAM_READER
251 #elif defined A32_BITSTREAM_READER
252 uint32_t *buffer_ptr;
260 static inline int get_bits_count(GetBitContext *s);
262 #define VLC_TYPE int16_t
266 VLC_TYPE (*table)[2]; // code, bits
267 int table_size, table_allocated;
270 typedef struct RL_VLC_ELEM {
277 #define UNALIGNED_STORES_ARE_BAD
280 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
282 # define unaligned32(a) (*(uint32_t*)(a))
285 static inline uint32_t unaligned32(const void *v) {
288 } __attribute__((packed));
290 return ((const struct Unaligned *) v)->i;
292 # elif defined(__DECC)
293 static inline uint32_t unaligned32(const void *v) {
294 return *(const __unaligned uint32_t *) v;
297 static inline uint32_t unaligned32(const void *v) {
298 return *(const uint32_t *) v;
303 #ifndef ALT_BITSTREAM_WRITER
304 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
306 unsigned int bit_buf;
310 st_out_bit_counts[st_current_index] += n;
312 // printf("put_bits=%d %x\n", n, value);
313 assert(n == 32 || value < (1U << n));
315 bit_buf = s->bit_buf;
316 bit_left = s->bit_left;
318 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
321 bit_buf = (bit_buf<<n) | value;
325 bit_buf |= value >> (n - bit_left);
326 #ifdef UNALIGNED_STORES_ARE_BAD
327 if (3 & (int) s->buf_ptr) {
328 s->buf_ptr[0] = bit_buf >> 24;
329 s->buf_ptr[1] = bit_buf >> 16;
330 s->buf_ptr[2] = bit_buf >> 8;
331 s->buf_ptr[3] = bit_buf ;
334 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
335 //printf("bitbuf = %08x\n", bit_buf);
341 s->bit_buf = bit_buf;
342 s->bit_left = bit_left;
347 #ifdef ALT_BITSTREAM_WRITER
348 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
350 # ifdef ALIGNED_BITSTREAM_WRITER
353 "movl %0, %%ecx \n\t"
354 "xorl %%eax, %%eax \n\t"
355 "shrdl %%cl, %1, %%eax \n\t"
357 "movl %0, %%ecx \n\t"
358 "shrl $3, %%ecx \n\t"
359 "andl $0xFFFFFFFC, %%ecx \n\t"
361 "orl %1, (%2, %%ecx) \n\t"
364 "movl %%eax, 4(%2, %%ecx) \n\t"
365 : "=&r" (s->index), "=&r" (value)
366 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
371 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
375 ptr[0] |= be2me_32(value>>(index&31));
376 ptr[1] = be2me_32(value<<(32-(index&31)));
377 //if(n>24) printf("%d %d\n", n, value);
381 # else //ALIGNED_BITSTREAM_WRITER
384 "movl $7, %%ecx \n\t"
385 "andl %0, %%ecx \n\t"
386 "addl %3, %%ecx \n\t"
390 "movl %0, %%ecx \n\t"
391 "shrl $3, %%ecx \n\t"
392 "orl %1, (%%ecx, %2) \n\t"
394 "movl $0, 4(%%ecx, %2) \n\t"
395 : "=&r" (s->index), "=&r" (value)
396 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
401 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
403 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
405 //if(n>24) printf("%d %d\n", n, value);
409 # endif //!ALIGNED_BITSTREAM_WRITER
414 static inline uint8_t* pbBufPtr(PutBitContext *s)
416 #ifdef ALT_BITSTREAM_WRITER
417 return s->buf + (s->index>>3);
423 /* Bitstream reader API docs:
425 abritary name which is used as prefix for the internal variables
430 OPEN_READER(name, gb)
431 loads gb into local variables
433 CLOSE_READER(name, gb)
434 stores local vars in gb
436 UPDATE_CACHE(name, gb)
437 refills the internal cache from the bitstream
438 after this call at least MIN_CACHE_BITS will be available,
441 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
443 SHOW_UBITS(name, gb, num)
444 will return the nest num bits
446 SHOW_SBITS(name, gb, num)
447 will return the nest num bits and do sign extension
449 SKIP_BITS(name, gb, num)
450 will skip over the next num bits
451 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
453 SKIP_CACHE(name, gb, num)
454 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
456 SKIP_COUNTER(name, gb, num)
457 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
459 LAST_SKIP_CACHE(name, gb, num)
460 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
462 LAST_SKIP_BITS(name, gb, num)
463 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
465 for examples see get_bits, show_bits, skip_bits, get_vlc
468 #ifdef ALT_BITSTREAM_READER
469 # define MIN_CACHE_BITS 25
471 # define OPEN_READER(name, gb)\
472 int name##_index= (gb)->index;\
473 int name##_cache= 0;\
475 # define CLOSE_READER(name, gb)\
476 (gb)->index= name##_index;\
478 # define UPDATE_CACHE(name, gb)\
479 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
481 # define SKIP_CACHE(name, gb, num)\
482 name##_cache <<= (num);\
485 # define SKIP_COUNTER(name, gb, num)\
486 name##_index += (num);\
488 # define SKIP_BITS(name, gb, num)\
490 SKIP_CACHE(name, gb, num)\
491 SKIP_COUNTER(name, gb, num)\
494 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
495 # define LAST_SKIP_CACHE(name, gb, num) ;
497 # define SHOW_UBITS(name, gb, num)\
498 NEG_USR32(name##_cache, num)
500 # define SHOW_SBITS(name, gb, num)\
501 NEG_SSR32(name##_cache, num)
503 # define GET_CACHE(name, gb)\
504 ((uint32_t)name##_cache)
506 static inline int get_bits_count(GetBitContext *s){
509 #elif defined LIBMPEG2_BITSTREAM_READER
510 //libmpeg2 like reader
512 # define MIN_CACHE_BITS 16
514 # define OPEN_READER(name, gb)\
515 int name##_bit_count=(gb)->bit_count;\
516 int name##_cache= (gb)->cache;\
517 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
519 # define CLOSE_READER(name, gb)\
520 (gb)->bit_count= name##_bit_count;\
521 (gb)->cache= name##_cache;\
522 (gb)->buffer_ptr= name##_buffer_ptr;\
524 # define UPDATE_CACHE(name, gb)\
525 if(name##_bit_count > 0){\
526 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
527 name##_buffer_ptr+=2;\
528 name##_bit_count-= 16;\
531 # define SKIP_CACHE(name, gb, num)\
532 name##_cache <<= (num);\
534 # define SKIP_COUNTER(name, gb, num)\
535 name##_bit_count += (num);\
537 # define SKIP_BITS(name, gb, num)\
539 SKIP_CACHE(name, gb, num)\
540 SKIP_COUNTER(name, gb, num)\
543 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
544 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
546 # define SHOW_UBITS(name, gb, num)\
547 NEG_USR32(name##_cache, num)
549 # define SHOW_SBITS(name, gb, num)\
550 NEG_SSR32(name##_cache, num)
552 # define GET_CACHE(name, gb)\
553 ((uint32_t)name##_cache)
555 static inline int get_bits_count(GetBitContext *s){
556 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
559 #elif defined A32_BITSTREAM_READER
561 # define MIN_CACHE_BITS 32
563 # define OPEN_READER(name, gb)\
564 int name##_bit_count=(gb)->bit_count;\
565 uint32_t name##_cache0= (gb)->cache0;\
566 uint32_t name##_cache1= (gb)->cache1;\
567 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
569 # define CLOSE_READER(name, gb)\
570 (gb)->bit_count= name##_bit_count;\
571 (gb)->cache0= name##_cache0;\
572 (gb)->cache1= name##_cache1;\
573 (gb)->buffer_ptr= name##_buffer_ptr;\
575 # define UPDATE_CACHE(name, gb)\
576 if(name##_bit_count > 0){\
577 const uint32_t next= be2me_32( *name##_buffer_ptr );\
578 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
579 name##_cache1 |= next<<name##_bit_count;\
580 name##_buffer_ptr++;\
581 name##_bit_count-= 32;\
585 # define SKIP_CACHE(name, gb, num)\
587 "shldl %2, %1, %0 \n\t"\
589 : "+r" (name##_cache0), "+r" (name##_cache1)\
590 : "Ic" ((uint8_t)num)\
593 # define SKIP_CACHE(name, gb, num)\
594 name##_cache0 <<= (num);\
595 name##_cache0 |= NEG_USR32(name##_cache1,num);\
596 name##_cache1 <<= (num);
599 # define SKIP_COUNTER(name, gb, num)\
600 name##_bit_count += (num);\
602 # define SKIP_BITS(name, gb, num)\
604 SKIP_CACHE(name, gb, num)\
605 SKIP_COUNTER(name, gb, num)\
608 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
609 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
611 # define SHOW_UBITS(name, gb, num)\
612 NEG_USR32(name##_cache0, num)
614 # define SHOW_SBITS(name, gb, num)\
615 NEG_SSR32(name##_cache0, num)
617 # define GET_CACHE(name, gb)\
620 static inline int get_bits_count(GetBitContext *s){
621 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
626 static inline unsigned int get_bits(GetBitContext *s, int n){
630 tmp= SHOW_UBITS(re, s, n);
631 LAST_SKIP_BITS(re, s, n)
636 static inline unsigned int show_bits(GetBitContext *s, int n){
640 tmp= SHOW_UBITS(re, s, n);
641 // CLOSE_READER(re, s)
645 static inline void skip_bits(GetBitContext *s, int n){
646 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
649 LAST_SKIP_BITS(re, s, n)
653 static inline unsigned int get_bits1(GetBitContext *s){
654 #ifdef ALT_BITSTREAM_READER
656 uint8_t result= s->buffer[ index>>3 ];
657 result<<= (index&0x07);
664 return get_bits(s, 1);
668 static inline unsigned int show_bits1(GetBitContext *s){
669 return show_bits(s, 1);
672 static inline void skip_bits1(GetBitContext *s){
676 void init_get_bits(GetBitContext *s,
677 const uint8_t *buffer, int buffer_size);
679 int check_marker(GetBitContext *s, const char *msg);
680 void align_get_bits(GetBitContext *s);
681 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
682 const void *bits, int bits_wrap, int bits_size,
683 const void *codes, int codes_wrap, int codes_size);
684 void free_vlc(VLC *vlc);
688 * if the vlc code is invalid and max_depth=1 than no bits will be removed
689 * if the vlc code is invalid and max_depth>1 than the number of bits removed
692 #define GET_VLC(code, name, gb, table, bits, max_depth)\
694 int n, index, nb_bits;\
696 index= SHOW_UBITS(name, gb, bits);\
697 code = table[index][0];\
698 n = table[index][1];\
700 if(max_depth > 1 && n < 0){\
701 LAST_SKIP_BITS(name, gb, bits)\
702 UPDATE_CACHE(name, gb)\
706 index= SHOW_UBITS(name, gb, nb_bits) + code;\
707 code = table[index][0];\
708 n = table[index][1];\
709 if(max_depth > 2 && n < 0){\
710 LAST_SKIP_BITS(name, gb, nb_bits)\
711 UPDATE_CACHE(name, gb)\
715 index= SHOW_UBITS(name, gb, nb_bits) + code;\
716 code = table[index][0];\
717 n = table[index][1];\
720 SKIP_BITS(name, gb, n)\
723 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
725 int n, index, nb_bits;\
727 index= SHOW_UBITS(name, gb, bits);\
728 level = table[index].level;\
729 n = table[index].len;\
731 if(max_depth > 1 && n < 0){\
732 LAST_SKIP_BITS(name, gb, bits)\
733 UPDATE_CACHE(name, gb)\
737 index= SHOW_UBITS(name, gb, nb_bits) + level;\
738 level = table[index].level;\
739 n = table[index].len;\
741 run= table[index].run;\
742 SKIP_BITS(name, gb, n)\
745 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
746 static inline int get_vlc(GetBitContext *s, VLC *vlc)
749 VLC_TYPE (*table)[2]= vlc->table;
754 GET_VLC(code, re, s, table, vlc->bits, 3)
761 * parses a vlc code, faster then get_vlc()
762 * @param bits is the number of bits which will be read at once, must be
763 * identical to nb_bits in init_vlc()
764 * @param max_depth is the number of times bits bits must be readed to completly
765 * read the longest vlc code
766 * = (max_vlc_length + bits - 1) / bits
768 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
769 int bits, int max_depth)
776 GET_VLC(code, re, s, table, bits, max_depth)
783 /* define it to include statistics code (useful only for optimizing
800 extern int st_current_index;
801 extern unsigned int st_bit_counts[ST_NB];
802 extern unsigned int st_out_bit_counts[ST_NB];
804 void print_stats(void);
807 /* misc math functions */
808 extern const uint8_t ff_log2_tab[256];
810 static inline int av_log2(unsigned int v)
815 if (v & 0xffff0000) {
828 static inline int av_log2_16bit(unsigned int v)
844 static inline int mid_pred(int a, int b, int c)
858 return a + b + c - vmin - vmax;
861 static inline int clip(int a, int amin, int amax)
872 extern const uint8_t ff_sqrt_tab[128];
874 int ff_gcd(int a, int b);
876 static inline int ff_sqrt(int a)
882 if(a<128) return ff_sqrt_tab[a];
884 for(s=15; s>=0; s--){
885 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
895 * converts fourcc string to int
897 static inline int ff_get_fourcc(const char *s){
898 assert( strlen(s)==4 );
900 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
903 void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
907 #define MASK_ABS(mask, level)\
912 : "+a" (level), "=&d" (mask)\
915 #define MASK_ABS(mask, level)\
917 level= (level^mask)-mask;
921 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
922 #define COPY3_IF_LT(x,y,a,b,c,d)\
928 : "+r" (x), "+r" (a), "+r" (c)\
929 : "r" (y), "r" (b), "r" (d)\
932 #define COPY3_IF_LT(x,y,a,b,c,d)\
940 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
942 /* avoid usage of various functions */
943 #define malloc please_use_av_malloc
944 #define free please_use_av_free
945 #define realloc please_use_av_realloc
947 #endif /* HAVE_AV_CONFIG_H */
949 #endif /* COMMON_H */