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
20 #ifdef HAVE_AV_CONFIG_H
21 /* only include the following when compiling package */
39 #define M_PI 3.14159265358979323846
44 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
47 #define AVOPTION_CODEC_BOOL(name, help, field) \
48 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
49 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
50 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
51 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
53 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
55 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
57 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
59 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
60 #define AVOPTION_END() AVOPTION_SUB(NULL)
64 extern const struct AVOption avoptions_common[3 + 5];
66 extern const struct AVOption avoptions_common[3];
68 extern const struct AVOption avoptions_workaround_bug[11];
70 #endif /* HAVE_AV_CONFIG_H */
72 /* Suppress restrict if it was not defined in config.h. */
77 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
78 # define always_inline __attribute__((always_inline)) inline
80 # define always_inline inline
87 typedef unsigned short uint16_t;
88 typedef signed short int16_t;
89 typedef unsigned char uint8_t;
90 typedef unsigned int uint32_t;
91 typedef unsigned __int64 uint64_t;
92 typedef signed char int8_t;
93 typedef signed int int32_t;
94 typedef signed __int64 int64_t;
97 # define int64_t_C(c) (c ## i64)
98 # define uint64_t_C(c) (c ## i64)
100 # define inline __inline
103 # define int64_t_C(c) (c ## LL)
104 # define uint64_t_C(c) (c ## ULL)
105 # endif /* __MINGW32__ */
111 # define snprintf _snprintf
112 # define vsnprintf _vsnprintf
114 /* CONFIG_WIN32 end */
115 #elif defined (CONFIG_OS2)
118 #include <inttypes.h>
120 #ifdef HAVE_AV_CONFIG_H
123 #define int64_t_C(c) (c ## LL)
124 #define uint64_t_C(c) (c ## ULL)
127 #ifdef USE_FASTMEMCPY
128 #include "fastmemcpy.h"
133 #endif /* HAVE_AV_CONFIG_H */
140 # include <inttypes.h>
142 # ifdef HAVE_AV_CONFIG_H
144 # define int64_t_C(c) (c ## LL)
145 # define uint64_t_C(c) (c ## ULL)
148 # ifdef USE_FASTMEMCPY
149 # include "fastmemcpy.h"
151 # endif /* HAVE_AV_CONFIG_H */
153 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
155 #ifdef HAVE_AV_CONFIG_H
159 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
160 defined(__OS2__) || defined (__OpenBSD__)
161 # define MANGLE(a) "_" #a
163 # define MANGLE(a) #a
174 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
176 inline void dprintf(const char* fmt,...) {}
181 # define dprintf(fmt,args...) printf(fmt, ## args)
183 # define dprintf(fmt,args...)
186 # endif /* !CONFIG_WIN32 */
188 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
190 //rounded divison & shift
191 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
193 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
194 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
196 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
197 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
200 // avoid +32 for shift optimization (gcc should do that ...)
201 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
202 asm ("sarl %1, %0\n\t"
204 : "ic" ((uint8_t)(-s))
208 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
209 asm ("shrl %1, %0\n\t"
211 : "ic" ((uint8_t)(-s))
216 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
217 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
222 struct PutBitContext;
224 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
226 typedef struct PutBitContext {
227 #ifdef ALT_BITSTREAM_WRITER
228 uint8_t *buf, *buf_end;
233 uint8_t *buf, *buf_ptr, *buf_end;
235 int64_t data_out_size; /* in bytes */
238 void init_put_bits(PutBitContext *s,
239 uint8_t *buffer, int buffer_size,
241 void (*write_data)(void *, uint8_t *, int));
243 int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */
244 void align_put_bits(PutBitContext *s);
245 void flush_put_bits(PutBitContext *s);
246 void put_string(PutBitContext * pbc, char *s);
250 typedef struct GetBitContext {
251 const uint8_t *buffer, *buffer_end;
252 #ifdef ALT_BITSTREAM_READER
254 #elif defined LIBMPEG2_BITSTREAM_READER
258 #elif defined A32_BITSTREAM_READER
259 uint32_t *buffer_ptr;
267 static inline int get_bits_count(GetBitContext *s);
269 #define VLC_TYPE int16_t
273 VLC_TYPE (*table)[2]; ///< code, bits
274 int table_size, table_allocated;
277 typedef struct RL_VLC_ELEM {
284 #define UNALIGNED_STORES_ARE_BAD
287 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
289 # define unaligned32(a) (*(uint32_t*)(a))
292 static inline uint32_t unaligned32(const void *v) {
295 } __attribute__((packed));
297 return ((const struct Unaligned *) v)->i;
299 # elif defined(__DECC)
300 static inline uint32_t unaligned32(const void *v) {
301 return *(const __unaligned uint32_t *) v;
304 static inline uint32_t unaligned32(const void *v) {
305 return *(const uint32_t *) v;
310 #ifndef ALT_BITSTREAM_WRITER
311 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
313 unsigned int bit_buf;
317 st_out_bit_counts[st_current_index] += n;
319 // printf("put_bits=%d %x\n", n, value);
320 assert(n == 32 || value < (1U << n));
322 bit_buf = s->bit_buf;
323 bit_left = s->bit_left;
325 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
328 bit_buf = (bit_buf<<n) | value;
332 bit_buf |= value >> (n - bit_left);
333 #ifdef UNALIGNED_STORES_ARE_BAD
334 if (3 & (int) s->buf_ptr) {
335 s->buf_ptr[0] = bit_buf >> 24;
336 s->buf_ptr[1] = bit_buf >> 16;
337 s->buf_ptr[2] = bit_buf >> 8;
338 s->buf_ptr[3] = bit_buf ;
341 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
342 //printf("bitbuf = %08x\n", bit_buf);
348 s->bit_buf = bit_buf;
349 s->bit_left = bit_left;
354 #ifdef ALT_BITSTREAM_WRITER
355 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
357 # ifdef ALIGNED_BITSTREAM_WRITER
360 "movl %0, %%ecx \n\t"
361 "xorl %%eax, %%eax \n\t"
362 "shrdl %%cl, %1, %%eax \n\t"
364 "movl %0, %%ecx \n\t"
365 "shrl $3, %%ecx \n\t"
366 "andl $0xFFFFFFFC, %%ecx \n\t"
368 "orl %1, (%2, %%ecx) \n\t"
371 "movl %%eax, 4(%2, %%ecx) \n\t"
372 : "=&r" (s->index), "=&r" (value)
373 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
378 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
382 ptr[0] |= be2me_32(value>>(index&31));
383 ptr[1] = be2me_32(value<<(32-(index&31)));
384 //if(n>24) printf("%d %d\n", n, value);
388 # else //ALIGNED_BITSTREAM_WRITER
391 "movl $7, %%ecx \n\t"
392 "andl %0, %%ecx \n\t"
393 "addl %3, %%ecx \n\t"
397 "movl %0, %%ecx \n\t"
398 "shrl $3, %%ecx \n\t"
399 "orl %1, (%%ecx, %2) \n\t"
401 "movl $0, 4(%%ecx, %2) \n\t"
402 : "=&r" (s->index), "=&r" (value)
403 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
408 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
410 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
412 //if(n>24) printf("%d %d\n", n, value);
416 # endif //!ALIGNED_BITSTREAM_WRITER
421 static inline uint8_t* pbBufPtr(PutBitContext *s)
423 #ifdef ALT_BITSTREAM_WRITER
424 return s->buf + (s->index>>3);
430 /* Bitstream reader API docs:
432 abritary name which is used as prefix for the internal variables
437 OPEN_READER(name, gb)
438 loads gb into local variables
440 CLOSE_READER(name, gb)
441 stores local vars in gb
443 UPDATE_CACHE(name, gb)
444 refills the internal cache from the bitstream
445 after this call at least MIN_CACHE_BITS will be available,
448 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
450 SHOW_UBITS(name, gb, num)
451 will return the nest num bits
453 SHOW_SBITS(name, gb, num)
454 will return the nest num bits and do sign extension
456 SKIP_BITS(name, gb, num)
457 will skip over the next num bits
458 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
460 SKIP_CACHE(name, gb, num)
461 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
463 SKIP_COUNTER(name, gb, num)
464 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
466 LAST_SKIP_CACHE(name, gb, num)
467 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
469 LAST_SKIP_BITS(name, gb, num)
470 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
472 for examples see get_bits, show_bits, skip_bits, get_vlc
475 #ifdef ALT_BITSTREAM_READER
476 # define MIN_CACHE_BITS 25
478 # define OPEN_READER(name, gb)\
479 int name##_index= (gb)->index;\
480 int name##_cache= 0;\
482 # define CLOSE_READER(name, gb)\
483 (gb)->index= name##_index;\
485 # define UPDATE_CACHE(name, gb)\
486 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
488 # define SKIP_CACHE(name, gb, num)\
489 name##_cache <<= (num);\
492 # define SKIP_COUNTER(name, gb, num)\
493 name##_index += (num);\
495 # define SKIP_BITS(name, gb, num)\
497 SKIP_CACHE(name, gb, num)\
498 SKIP_COUNTER(name, gb, num)\
501 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
502 # define LAST_SKIP_CACHE(name, gb, num) ;
504 # define SHOW_UBITS(name, gb, num)\
505 NEG_USR32(name##_cache, num)
507 # define SHOW_SBITS(name, gb, num)\
508 NEG_SSR32(name##_cache, num)
510 # define GET_CACHE(name, gb)\
511 ((uint32_t)name##_cache)
513 static inline int get_bits_count(GetBitContext *s){
516 #elif defined LIBMPEG2_BITSTREAM_READER
517 //libmpeg2 like reader
519 # define MIN_CACHE_BITS 16
521 # define OPEN_READER(name, gb)\
522 int name##_bit_count=(gb)->bit_count;\
523 int name##_cache= (gb)->cache;\
524 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
526 # define CLOSE_READER(name, gb)\
527 (gb)->bit_count= name##_bit_count;\
528 (gb)->cache= name##_cache;\
529 (gb)->buffer_ptr= name##_buffer_ptr;\
531 # define UPDATE_CACHE(name, gb)\
532 if(name##_bit_count > 0){\
533 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
534 name##_buffer_ptr+=2;\
535 name##_bit_count-= 16;\
538 # define SKIP_CACHE(name, gb, num)\
539 name##_cache <<= (num);\
541 # define SKIP_COUNTER(name, gb, num)\
542 name##_bit_count += (num);\
544 # define SKIP_BITS(name, gb, num)\
546 SKIP_CACHE(name, gb, num)\
547 SKIP_COUNTER(name, gb, num)\
550 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
551 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
553 # define SHOW_UBITS(name, gb, num)\
554 NEG_USR32(name##_cache, num)
556 # define SHOW_SBITS(name, gb, num)\
557 NEG_SSR32(name##_cache, num)
559 # define GET_CACHE(name, gb)\
560 ((uint32_t)name##_cache)
562 static inline int get_bits_count(GetBitContext *s){
563 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
566 #elif defined A32_BITSTREAM_READER
568 # define MIN_CACHE_BITS 32
570 # define OPEN_READER(name, gb)\
571 int name##_bit_count=(gb)->bit_count;\
572 uint32_t name##_cache0= (gb)->cache0;\
573 uint32_t name##_cache1= (gb)->cache1;\
574 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
576 # define CLOSE_READER(name, gb)\
577 (gb)->bit_count= name##_bit_count;\
578 (gb)->cache0= name##_cache0;\
579 (gb)->cache1= name##_cache1;\
580 (gb)->buffer_ptr= name##_buffer_ptr;\
582 # define UPDATE_CACHE(name, gb)\
583 if(name##_bit_count > 0){\
584 const uint32_t next= be2me_32( *name##_buffer_ptr );\
585 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
586 name##_cache1 |= next<<name##_bit_count;\
587 name##_buffer_ptr++;\
588 name##_bit_count-= 32;\
592 # define SKIP_CACHE(name, gb, num)\
594 "shldl %2, %1, %0 \n\t"\
596 : "+r" (name##_cache0), "+r" (name##_cache1)\
597 : "Ic" ((uint8_t)num)\
600 # define SKIP_CACHE(name, gb, num)\
601 name##_cache0 <<= (num);\
602 name##_cache0 |= NEG_USR32(name##_cache1,num);\
603 name##_cache1 <<= (num);
606 # define SKIP_COUNTER(name, gb, num)\
607 name##_bit_count += (num);\
609 # define SKIP_BITS(name, gb, num)\
611 SKIP_CACHE(name, gb, num)\
612 SKIP_COUNTER(name, gb, num)\
615 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
616 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
618 # define SHOW_UBITS(name, gb, num)\
619 NEG_USR32(name##_cache0, num)
621 # define SHOW_SBITS(name, gb, num)\
622 NEG_SSR32(name##_cache0, num)
624 # define GET_CACHE(name, gb)\
627 static inline int get_bits_count(GetBitContext *s){
628 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
633 static inline unsigned int get_bits(GetBitContext *s, int n){
637 tmp= SHOW_UBITS(re, s, n);
638 LAST_SKIP_BITS(re, s, n)
643 static inline unsigned int show_bits(GetBitContext *s, int n){
647 tmp= SHOW_UBITS(re, s, n);
648 // CLOSE_READER(re, s)
652 static inline void skip_bits(GetBitContext *s, int n){
653 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
656 LAST_SKIP_BITS(re, s, n)
660 static inline unsigned int get_bits1(GetBitContext *s){
661 #ifdef ALT_BITSTREAM_READER
663 uint8_t result= s->buffer[ index>>3 ];
664 result<<= (index&0x07);
671 return get_bits(s, 1);
675 static inline unsigned int show_bits1(GetBitContext *s){
676 return show_bits(s, 1);
679 static inline void skip_bits1(GetBitContext *s){
683 void init_get_bits(GetBitContext *s,
684 const uint8_t *buffer, int buffer_size);
686 int check_marker(GetBitContext *s, const char *msg);
687 void align_get_bits(GetBitContext *s);
688 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
689 const void *bits, int bits_wrap, int bits_size,
690 const void *codes, int codes_wrap, int codes_size);
691 void free_vlc(VLC *vlc);
695 * if the vlc code is invalid and max_depth=1 than no bits will be removed
696 * if the vlc code is invalid and max_depth>1 than the number of bits removed
699 #define GET_VLC(code, name, gb, table, bits, max_depth)\
701 int n, index, nb_bits;\
703 index= SHOW_UBITS(name, gb, bits);\
704 code = table[index][0];\
705 n = table[index][1];\
707 if(max_depth > 1 && n < 0){\
708 LAST_SKIP_BITS(name, gb, bits)\
709 UPDATE_CACHE(name, gb)\
713 index= SHOW_UBITS(name, gb, nb_bits) + code;\
714 code = table[index][0];\
715 n = table[index][1];\
716 if(max_depth > 2 && n < 0){\
717 LAST_SKIP_BITS(name, gb, nb_bits)\
718 UPDATE_CACHE(name, gb)\
722 index= SHOW_UBITS(name, gb, nb_bits) + code;\
723 code = table[index][0];\
724 n = table[index][1];\
727 SKIP_BITS(name, gb, n)\
730 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
732 int n, index, nb_bits;\
734 index= SHOW_UBITS(name, gb, bits);\
735 level = table[index].level;\
736 n = table[index].len;\
738 if(max_depth > 1 && n < 0){\
739 LAST_SKIP_BITS(name, gb, bits)\
740 UPDATE_CACHE(name, gb)\
744 index= SHOW_UBITS(name, gb, nb_bits) + level;\
745 level = table[index].level;\
746 n = table[index].len;\
748 run= table[index].run;\
749 SKIP_BITS(name, gb, n)\
752 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
753 static inline int get_vlc(GetBitContext *s, VLC *vlc)
756 VLC_TYPE (*table)[2]= vlc->table;
761 GET_VLC(code, re, s, table, vlc->bits, 3)
768 * parses a vlc code, faster then get_vlc()
769 * @param bits is the number of bits which will be read at once, must be
770 * identical to nb_bits in init_vlc()
771 * @param max_depth is the number of times bits bits must be readed to completly
772 * read the longest vlc code
773 * = (max_vlc_length + bits - 1) / bits
775 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
776 int bits, int max_depth)
783 GET_VLC(code, re, s, table, bits, max_depth)
790 /* define it to include statistics code (useful only for optimizing
807 extern int st_current_index;
808 extern unsigned int st_bit_counts[ST_NB];
809 extern unsigned int st_out_bit_counts[ST_NB];
811 void print_stats(void);
814 /* misc math functions */
815 extern const uint8_t ff_log2_tab[256];
817 static inline int av_log2(unsigned int v)
822 if (v & 0xffff0000) {
835 static inline int av_log2_16bit(unsigned int v)
851 static inline int mid_pred(int a, int b, int c)
865 return a + b + c - vmin - vmax;
868 static inline int clip(int a, int amin, int amax)
879 extern const uint8_t ff_sqrt_tab[128];
881 int64_t ff_gcd(int64_t a, int64_t b);
883 static inline int ff_sqrt(int a)
889 if(a<128) return ff_sqrt_tab[a];
891 for(s=15; s>=0; s--){
892 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
902 * converts fourcc string to int
904 static inline int ff_get_fourcc(const char *s){
905 assert( strlen(s)==4 );
907 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
910 void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
914 #define MASK_ABS(mask, level)\
919 : "+a" (level), "=&d" (mask)\
922 #define MASK_ABS(mask, level)\
924 level= (level^mask)-mask;
928 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
929 #define COPY3_IF_LT(x,y,a,b,c,d)\
935 : "+r" (x), "+r" (a), "+r" (c)\
936 : "r" (y), "r" (b), "r" (d)\
939 #define COPY3_IF_LT(x,y,a,b,c,d)\
947 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
949 /* avoid usage of various functions */
950 #define malloc please_use_av_malloc
951 #define free please_use_av_free
952 #define realloc please_use_av_realloc
954 #endif /* HAVE_AV_CONFIG_H */
956 #endif /* COMMON_H */