4 #define FFMPEG_VERSION_INT 0x000406
5 #define FFMPEG_VERSION "0.4.6"
7 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
11 //#define ALT_BITSTREAM_WRITER
12 //#define ALIGNED_BITSTREAM_WRITER
14 #define ALT_BITSTREAM_READER
15 //#define LIBMPEG2_BITSTREAM_READER
16 //#define A32_BITSTREAM_READER
18 #ifdef HAVE_AV_CONFIG_H
19 /* only include the following when compiling package */
36 #endif /* HAVE_AV_CONFIG_H */
38 /* Suppress restrict if it was not defined in config.h. */
43 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
44 #define always_inline __attribute__((always_inline)) inline
46 #define always_inline inline
53 typedef unsigned short UINT16;
54 typedef signed short INT16;
55 typedef unsigned char UINT8;
56 typedef unsigned int UINT32;
57 typedef unsigned __int64 UINT64;
58 typedef signed char INT8;
59 typedef signed int INT32;
60 typedef signed __int64 INT64;
62 typedef UINT8 uint8_t;
64 typedef UINT16 uint16_t;
65 typedef INT16 int16_t;
66 typedef UINT32 uint32_t;
67 typedef INT32 int32_t;
68 typedef UINT64 uint64_t;
69 typedef INT64 int64_t;
72 #define INT64_C(c) (c ## i64)
73 #define UINT64_C(c) (c ## i64)
75 #define inline __inline
78 #define INT64_C(c) (c ## LL)
79 #define UINT64_C(c) (c ## ULL)
80 #endif /* __MINGW32__ */
82 #define M_PI 3.14159265358979323846
83 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
89 #define snprintf _snprintf
91 #else /* CONFIG_WIN32 */
97 #ifndef __WINE_WINDEF16_H
98 /* workaround for typedef conflict in MPlayer (wine typedefs) */
99 typedef unsigned short UINT16;
100 typedef signed short INT16;
103 typedef unsigned char UINT8;
104 typedef unsigned int UINT32;
105 typedef unsigned long long UINT64;
106 typedef signed char INT8;
107 typedef signed int INT32;
108 typedef signed long long INT64;
110 #ifdef HAVE_AV_CONFIG_H
113 #include <sys/param.h>
117 #define INT64_C(c) (c ## LL)
118 #define UINT64_C(c) (c ## ULL)
121 #ifdef USE_FASTMEMCPY
122 #include "fastmemcpy.h"
125 #endif /* HAVE_AV_CONFIG_H */
127 #endif /* !CONFIG_WIN32 */
129 #ifdef HAVE_AV_CONFIG_H
133 #if defined(__MINGW32__) || defined(__CYGWIN__) || \
134 defined(__OS2__) || defined (__OpenBSD__)
135 #define MANGLE(a) "_" #a
148 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
150 inline void dprintf(const char* fmt,...) {}
155 #define dprintf(fmt,args...) printf(fmt, ## args)
157 #define dprintf(fmt,args...)
160 #endif /* !CONFIG_WIN32 */
162 #define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
165 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
166 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
167 #define MAX(a,b) ((a) > (b) ? (a) : (b))
168 #define MIN(a,b) ((a) > (b) ? (b) : (a))
171 // avoid +32 for shift optimization (gcc should do that ...)
172 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
173 asm ("sarl %1, %0\n\t"
175 : "ic" ((uint8_t)(-s))
179 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
180 asm ("shrl %1, %0\n\t"
182 : "ic" ((uint8_t)(-s))
187 #define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
188 #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
193 struct PutBitContext;
195 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
197 typedef struct PutBitContext {
198 #ifdef ALT_BITSTREAM_WRITER
199 UINT8 *buf, *buf_end;
204 UINT8 *buf, *buf_ptr, *buf_end;
206 INT64 data_out_size; /* in bytes */
209 void init_put_bits(PutBitContext *s,
210 UINT8 *buffer, int buffer_size,
212 void (*write_data)(void *, UINT8 *, int));
214 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
215 void align_put_bits(PutBitContext *s);
216 void flush_put_bits(PutBitContext *s);
217 void put_string(PutBitContext * pbc, char *s);
219 /* jpeg specific put_bits */
220 void jflush_put_bits(PutBitContext *s);
224 typedef struct GetBitContext {
225 UINT8 *buffer, *buffer_end;
226 #ifdef ALT_BITSTREAM_READER
228 #elif defined LIBMPEG2_BITSTREAM_READER
232 #elif defined A32_BITSTREAM_READER
241 static inline int get_bits_count(GetBitContext *s);
243 #define VLC_TYPE INT16
247 VLC_TYPE (*table)[2]; // code, bits
248 int table_size, table_allocated;
251 typedef struct RL_VLC_ELEM {
257 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
259 #define unaligned32(a) (*(UINT32*)(a))
262 static inline uint32_t unaligned32(const void *v) {
265 } __attribute__((packed));
267 return ((const struct Unaligned *) v)->i;
269 #elif defined(__DECC)
270 static inline uint32_t unaligned32(const void *v) {
271 return *(const __unaligned uint32_t *) v;
274 static inline uint32_t unaligned32(const void *v) {
275 return *(const uint32_t *) v;
280 #ifndef ALT_BITSTREAM_WRITER
281 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
283 unsigned int bit_buf;
287 st_out_bit_counts[st_current_index] += n;
289 // printf("put_bits=%d %x\n", n, value);
290 assert(n == 32 || value < (1U << n));
292 bit_buf = s->bit_buf;
293 bit_left = s->bit_left;
295 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
298 bit_buf = (bit_buf<<n) | value;
302 bit_buf |= value >> (n - bit_left);
303 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
304 //printf("bitbuf = %08x\n", bit_buf);
310 s->bit_buf = bit_buf;
311 s->bit_left = bit_left;
316 #ifdef ALT_BITSTREAM_WRITER
317 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
319 #ifdef ALIGNED_BITSTREAM_WRITER
322 "movl %0, %%ecx \n\t"
323 "xorl %%eax, %%eax \n\t"
324 "shrdl %%cl, %1, %%eax \n\t"
326 "movl %0, %%ecx \n\t"
327 "shrl $3, %%ecx \n\t"
328 "andl $0xFFFFFFFC, %%ecx \n\t"
330 "orl %1, (%2, %%ecx) \n\t"
333 "movl %%eax, 4(%2, %%ecx) \n\t"
334 : "=&r" (s->index), "=&r" (value)
335 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
340 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
344 ptr[0] |= be2me_32(value>>(index&31));
345 ptr[1] = be2me_32(value<<(32-(index&31)));
346 //if(n>24) printf("%d %d\n", n, value);
350 #else //ALIGNED_BITSTREAM_WRITER
353 "movl $7, %%ecx \n\t"
354 "andl %0, %%ecx \n\t"
355 "addl %3, %%ecx \n\t"
359 "movl %0, %%ecx \n\t"
360 "shrl $3, %%ecx \n\t"
361 "orl %1, (%%ecx, %2) \n\t"
363 "movl $0, 4(%%ecx, %2) \n\t"
364 : "=&r" (s->index), "=&r" (value)
365 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
370 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
372 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
374 //if(n>24) printf("%d %d\n", n, value);
378 #endif //!ALIGNED_BITSTREAM_WRITER
382 #ifndef ALT_BITSTREAM_WRITER
383 /* for jpeg : escape 0xff with 0x00 after it */
384 static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
386 unsigned int bit_buf, b;
389 assert(n == 32 || value < (1U << n));
391 bit_buf = s->bit_buf;
392 bit_left = s->bit_left;
394 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
397 bit_buf = (bit_buf<<n) | value;
401 bit_buf |= value >> (n - bit_left);
415 s->bit_buf = bit_buf;
416 s->bit_left = bit_left;
421 #ifdef ALT_BITSTREAM_WRITER
422 static inline void jput_bits(PutBitContext *s, int n, int value)
425 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
427 //if(n>24) printf("%d %d\n", n, value);
429 v |= be2me_32(value<<(32-n-(index&7) ));
430 if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
432 /* handle idiotic (m)jpeg escapes */
433 uint8_t *bPtr= (uint8_t*)ptr;
434 int numChecked= ((index+n)>>3) - (index>>3);
439 if((v&0xFF000000)==0xFF000000 && numChecked>0){
443 *(bPtr++)= (v>>16)&0xFF;
444 if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
448 *(bPtr++)= (v>>8)&0xFF;
449 if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
454 if((v&0x000000FF)==0x000000FF && numChecked>3){
458 *((uint32_t*)bPtr)= 0;
471 static inline uint8_t* pbBufPtr(PutBitContext *s)
473 #ifdef ALT_BITSTREAM_WRITER
474 return s->buf + (s->index>>3);
480 /* Bitstream reader API docs:
482 abritary name which is used as prefix for the internal variables
487 OPEN_READER(name, gb)
488 loads gb into local variables
490 CLOSE_READER(name, gb)
491 stores local vars in gb
493 UPDATE_CACHE(name, gb)
494 refills the internal cache from the bitstream
495 after this call at least MIN_CACHE_BITS will be available,
498 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
500 SHOW_UBITS(name, gb, num)
501 will return the nest num bits
503 SHOW_SBITS(name, gb, num)
504 will return the nest num bits and do sign extension
506 SKIP_BITS(name, gb, num)
507 will skip over the next num bits
508 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
510 SKIP_CACHE(name, gb, num)
511 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
513 SKIP_COUNTER(name, gb, num)
514 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
516 LAST_SKIP_CACHE(name, gb, num)
517 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
519 LAST_SKIP_BITS(name, gb, num)
520 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
522 for examples see get_bits, show_bits, skip_bits, get_vlc
525 #ifdef ALT_BITSTREAM_READER
526 # define MIN_CACHE_BITS 25
528 # define OPEN_READER(name, gb)\
529 int name##_index= (gb)->index;\
530 int name##_cache= 0;\
532 # define CLOSE_READER(name, gb)\
533 (gb)->index= name##_index;\
535 # define UPDATE_CACHE(name, gb)\
536 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
538 # define SKIP_CACHE(name, gb, num)\
539 name##_cache <<= (num);\
542 # define SKIP_COUNTER(name, gb, num)\
543 name##_index += (num);\
545 # define SKIP_BITS(name, gb, num)\
547 SKIP_CACHE(name, gb, num)\
548 SKIP_COUNTER(name, gb, num)\
551 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
552 # define LAST_SKIP_CACHE(name, gb, num) ;
554 # define SHOW_UBITS(name, gb, num)\
555 NEG_USR32(name##_cache, num)
557 # define SHOW_SBITS(name, gb, num)\
558 NEG_SSR32(name##_cache, num)
560 # define GET_CACHE(name, gb)\
561 ((uint32_t)name##_cache)
563 static inline int get_bits_count(GetBitContext *s){
566 #elif defined LIBMPEG2_BITSTREAM_READER
567 //libmpeg2 like reader
569 # define MIN_CACHE_BITS 16
571 # define OPEN_READER(name, gb)\
572 int name##_bit_count=(gb)->bit_count;\
573 int name##_cache= (gb)->cache;\
574 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
576 # define CLOSE_READER(name, gb)\
577 (gb)->bit_count= name##_bit_count;\
578 (gb)->cache= name##_cache;\
579 (gb)->buffer_ptr= name##_buffer_ptr;\
581 # define UPDATE_CACHE(name, gb)\
582 if(name##_bit_count > 0){\
583 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
584 name##_buffer_ptr+=2;\
585 name##_bit_count-= 16;\
588 # define SKIP_CACHE(name, gb, num)\
589 name##_cache <<= (num);\
591 # define SKIP_COUNTER(name, gb, num)\
592 name##_bit_count += (num);\
594 # define SKIP_BITS(name, gb, num)\
596 SKIP_CACHE(name, gb, num)\
597 SKIP_COUNTER(name, gb, num)\
600 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
601 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
603 # define SHOW_UBITS(name, gb, num)\
604 NEG_USR32(name##_cache, num)
606 # define SHOW_SBITS(name, gb, num)\
607 NEG_SSR32(name##_cache, num)
609 # define GET_CACHE(name, gb)\
610 ((uint32_t)name##_cache)
612 static inline int get_bits_count(GetBitContext *s){
613 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
616 #elif defined A32_BITSTREAM_READER
618 # define MIN_CACHE_BITS 32
620 # define OPEN_READER(name, gb)\
621 int name##_bit_count=(gb)->bit_count;\
622 uint32_t name##_cache0= (gb)->cache0;\
623 uint32_t name##_cache1= (gb)->cache1;\
624 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
626 # define CLOSE_READER(name, gb)\
627 (gb)->bit_count= name##_bit_count;\
628 (gb)->cache0= name##_cache0;\
629 (gb)->cache1= name##_cache1;\
630 (gb)->buffer_ptr= name##_buffer_ptr;\
632 # define UPDATE_CACHE(name, gb)\
633 if(name##_bit_count > 0){\
634 const uint32_t next= be2me_32( *name##_buffer_ptr );\
635 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
636 name##_cache1 |= next<<name##_bit_count;\
637 name##_buffer_ptr++;\
638 name##_bit_count-= 32;\
642 # define SKIP_CACHE(name, gb, num)\
644 "shldl %2, %1, %0 \n\t"\
646 : "+r" (name##_cache0), "+r" (name##_cache1)\
647 : "Ic" ((uint8_t)num)\
650 # define SKIP_CACHE(name, gb, num)\
651 name##_cache0 <<= (num);\
652 name##_cache0 |= NEG_USR32(name##_cache1,num);\
653 name##_cache1 <<= (num);
656 # define SKIP_COUNTER(name, gb, num)\
657 name##_bit_count += (num);\
659 # define SKIP_BITS(name, gb, num)\
661 SKIP_CACHE(name, gb, num)\
662 SKIP_COUNTER(name, gb, num)\
665 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
666 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
668 # define SHOW_UBITS(name, gb, num)\
669 NEG_USR32(name##_cache0, num)
671 # define SHOW_SBITS(name, gb, num)\
672 NEG_SSR32(name##_cache0, num)
674 # define GET_CACHE(name, gb)\
677 static inline int get_bits_count(GetBitContext *s){
678 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
683 static inline unsigned int get_bits(GetBitContext *s, int n){
687 tmp= SHOW_UBITS(re, s, n);
688 LAST_SKIP_BITS(re, s, n)
693 static inline unsigned int show_bits(GetBitContext *s, int n){
697 tmp= SHOW_UBITS(re, s, n);
698 // CLOSE_READER(re, s)
702 static inline void skip_bits(GetBitContext *s, int n){
703 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
706 LAST_SKIP_BITS(re, s, n)
710 static inline unsigned int get_bits1(GetBitContext *s){
711 #ifdef ALT_BITSTREAM_READER
713 uint8_t result= s->buffer[ index>>3 ];
714 result<<= (index&0x07);
721 return get_bits(s, 1);
725 static inline unsigned int show_bits1(GetBitContext *s){
726 return show_bits(s, 1);
729 static inline void skip_bits1(GetBitContext *s){
733 void init_get_bits(GetBitContext *s,
734 UINT8 *buffer, int buffer_size);
736 int check_marker(GetBitContext *s, char *msg);
737 void align_get_bits(GetBitContext *s);
738 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
739 const void *bits, int bits_wrap, int bits_size,
740 const void *codes, int codes_wrap, int codes_size);
741 void free_vlc(VLC *vlc);
743 #define GET_VLC(code, name, gb, table, bits, max_depth)\
745 int n, index, nb_bits;\
747 index= SHOW_UBITS(name, gb, bits);\
748 code = table[index][0];\
749 n = table[index][1];\
751 if(max_depth > 1 && n < 0){\
752 LAST_SKIP_BITS(name, gb, bits)\
753 UPDATE_CACHE(name, gb)\
757 index= SHOW_UBITS(name, gb, nb_bits) + code;\
758 code = table[index][0];\
759 n = table[index][1];\
760 if(max_depth > 2 && n < 0){\
761 LAST_SKIP_BITS(name, gb, nb_bits)\
762 UPDATE_CACHE(name, gb)\
766 index= SHOW_UBITS(name, gb, nb_bits) + code;\
767 code = table[index][0];\
768 n = table[index][1];\
771 SKIP_BITS(name, gb, n)\
774 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
776 int n, index, nb_bits;\
778 index= SHOW_UBITS(name, gb, bits);\
779 level = table[index].level;\
780 n = table[index].len;\
782 if(max_depth > 1 && n < 0){\
783 LAST_SKIP_BITS(name, gb, bits)\
784 UPDATE_CACHE(name, gb)\
788 index= SHOW_UBITS(name, gb, nb_bits) + level;\
789 level = table[index].level;\
790 n = table[index].len;\
792 run= table[index].run;\
793 SKIP_BITS(name, gb, n)\
796 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
797 static inline int get_vlc(GetBitContext *s, VLC *vlc)
800 VLC_TYPE (*table)[2]= vlc->table;
805 GET_VLC(code, re, s, table, vlc->bits, 3)
811 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
812 int bits, int max_depth)
819 GET_VLC(code, re, s, table, bits, max_depth)
826 /* define it to include statistics code (useful only for optimizing
843 extern int st_current_index;
844 extern unsigned int st_bit_counts[ST_NB];
845 extern unsigned int st_out_bit_counts[ST_NB];
847 void print_stats(void);
850 /* misc math functions */
852 static inline int av_log2(unsigned int v)
857 if (v & 0xffff0000) {
880 static inline int mid_pred(int a, int b, int c)
894 return a + b + c - vmin - vmax;
897 static inline int clip(int a, int amin, int amax)
908 int ff_gcd(int a, int b);
910 static inline int ff_sqrt(int a)
916 for(s=15; s>=0; s--){
917 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
925 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
926 #define COPY3_IF_LT(x,y,a,b,c,d)\
932 : "+r" (x), "+r" (a), "+r" (c)\
933 : "r" (y), "r" (b), "r" (d)\
936 #define COPY3_IF_LT(x,y,a,b,c,d)\
944 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
946 #endif /* HAVE_AV_CONFIG_H */
948 #endif /* COMMON_H */