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 */
95 # include <inttypes.h>
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
112 # define INT64_C(c) (c ## LL)
113 # define UINT64_C(c) (c ## ULL)
116 # ifdef USE_FASTMEMCPY
117 # include "fastmemcpy.h"
119 # endif /* HAVE_AV_CONFIG_H */
121 #endif /* !CONFIG_WIN32 */
123 #ifdef HAVE_AV_CONFIG_H
127 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
128 defined(__OS2__) || defined (__OpenBSD__)
129 # define MANGLE(a) "_" #a
131 # define MANGLE(a) #a
142 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
144 inline void dprintf(const char* fmt,...) {}
149 # define dprintf(fmt,args...) printf(fmt, ## args)
151 # define dprintf(fmt,args...)
154 # endif /* !CONFIG_WIN32 */
156 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
158 //rounded divison & shift
159 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
161 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
162 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
164 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
165 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
168 // avoid +32 for shift optimization (gcc should do that ...)
169 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
170 asm ("sarl %1, %0\n\t"
172 : "ic" ((uint8_t)(-s))
176 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
177 asm ("shrl %1, %0\n\t"
179 : "ic" ((uint8_t)(-s))
184 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
185 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
190 struct PutBitContext;
192 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
194 typedef struct PutBitContext {
195 #ifdef ALT_BITSTREAM_WRITER
196 UINT8 *buf, *buf_end;
201 UINT8 *buf, *buf_ptr, *buf_end;
203 INT64 data_out_size; /* in bytes */
206 void init_put_bits(PutBitContext *s,
207 UINT8 *buffer, int buffer_size,
209 void (*write_data)(void *, UINT8 *, int));
211 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
212 void align_put_bits(PutBitContext *s);
213 void flush_put_bits(PutBitContext *s);
214 void put_string(PutBitContext * pbc, char *s);
218 typedef struct GetBitContext {
219 UINT8 *buffer, *buffer_end;
220 #ifdef ALT_BITSTREAM_READER
222 #elif defined LIBMPEG2_BITSTREAM_READER
226 #elif defined A32_BITSTREAM_READER
235 static inline int get_bits_count(GetBitContext *s);
237 #define VLC_TYPE INT16
241 VLC_TYPE (*table)[2]; // code, bits
242 int table_size, table_allocated;
245 typedef struct RL_VLC_ELEM {
251 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
253 # define unaligned32(a) (*(UINT32*)(a))
256 static inline uint32_t unaligned32(const void *v) {
259 } __attribute__((packed));
261 return ((const struct Unaligned *) v)->i;
263 # elif defined(__DECC)
264 static inline uint32_t unaligned32(const void *v) {
265 return *(const __unaligned uint32_t *) v;
268 static inline uint32_t unaligned32(const void *v) {
269 return *(const uint32_t *) v;
274 #ifndef ALT_BITSTREAM_WRITER
275 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
277 unsigned int bit_buf;
281 st_out_bit_counts[st_current_index] += n;
283 // printf("put_bits=%d %x\n", n, value);
284 assert(n == 32 || value < (1U << n));
286 bit_buf = s->bit_buf;
287 bit_left = s->bit_left;
289 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
292 bit_buf = (bit_buf<<n) | value;
296 bit_buf |= value >> (n - bit_left);
297 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
298 //printf("bitbuf = %08x\n", bit_buf);
304 s->bit_buf = bit_buf;
305 s->bit_left = bit_left;
310 #ifdef ALT_BITSTREAM_WRITER
311 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
313 # ifdef ALIGNED_BITSTREAM_WRITER
316 "movl %0, %%ecx \n\t"
317 "xorl %%eax, %%eax \n\t"
318 "shrdl %%cl, %1, %%eax \n\t"
320 "movl %0, %%ecx \n\t"
321 "shrl $3, %%ecx \n\t"
322 "andl $0xFFFFFFFC, %%ecx \n\t"
324 "orl %1, (%2, %%ecx) \n\t"
327 "movl %%eax, 4(%2, %%ecx) \n\t"
328 : "=&r" (s->index), "=&r" (value)
329 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
334 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
338 ptr[0] |= be2me_32(value>>(index&31));
339 ptr[1] = be2me_32(value<<(32-(index&31)));
340 //if(n>24) printf("%d %d\n", n, value);
344 # else //ALIGNED_BITSTREAM_WRITER
347 "movl $7, %%ecx \n\t"
348 "andl %0, %%ecx \n\t"
349 "addl %3, %%ecx \n\t"
353 "movl %0, %%ecx \n\t"
354 "shrl $3, %%ecx \n\t"
355 "orl %1, (%%ecx, %2) \n\t"
357 "movl $0, 4(%%ecx, %2) \n\t"
358 : "=&r" (s->index), "=&r" (value)
359 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
364 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
366 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
368 //if(n>24) printf("%d %d\n", n, value);
372 # endif //!ALIGNED_BITSTREAM_WRITER
377 static inline uint8_t* pbBufPtr(PutBitContext *s)
379 #ifdef ALT_BITSTREAM_WRITER
380 return s->buf + (s->index>>3);
386 /* Bitstream reader API docs:
388 abritary name which is used as prefix for the internal variables
393 OPEN_READER(name, gb)
394 loads gb into local variables
396 CLOSE_READER(name, gb)
397 stores local vars in gb
399 UPDATE_CACHE(name, gb)
400 refills the internal cache from the bitstream
401 after this call at least MIN_CACHE_BITS will be available,
404 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
406 SHOW_UBITS(name, gb, num)
407 will return the nest num bits
409 SHOW_SBITS(name, gb, num)
410 will return the nest num bits and do sign extension
412 SKIP_BITS(name, gb, num)
413 will skip over the next num bits
414 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
416 SKIP_CACHE(name, gb, num)
417 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
419 SKIP_COUNTER(name, gb, num)
420 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
422 LAST_SKIP_CACHE(name, gb, num)
423 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
425 LAST_SKIP_BITS(name, gb, num)
426 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
428 for examples see get_bits, show_bits, skip_bits, get_vlc
431 #ifdef ALT_BITSTREAM_READER
432 # define MIN_CACHE_BITS 25
434 # define OPEN_READER(name, gb)\
435 int name##_index= (gb)->index;\
436 int name##_cache= 0;\
438 # define CLOSE_READER(name, gb)\
439 (gb)->index= name##_index;\
441 # define UPDATE_CACHE(name, gb)\
442 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
444 # define SKIP_CACHE(name, gb, num)\
445 name##_cache <<= (num);\
448 # define SKIP_COUNTER(name, gb, num)\
449 name##_index += (num);\
451 # define SKIP_BITS(name, gb, num)\
453 SKIP_CACHE(name, gb, num)\
454 SKIP_COUNTER(name, gb, num)\
457 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
458 # define LAST_SKIP_CACHE(name, gb, num) ;
460 # define SHOW_UBITS(name, gb, num)\
461 NEG_USR32(name##_cache, num)
463 # define SHOW_SBITS(name, gb, num)\
464 NEG_SSR32(name##_cache, num)
466 # define GET_CACHE(name, gb)\
467 ((uint32_t)name##_cache)
469 static inline int get_bits_count(GetBitContext *s){
472 #elif defined LIBMPEG2_BITSTREAM_READER
473 //libmpeg2 like reader
475 # define MIN_CACHE_BITS 16
477 # define OPEN_READER(name, gb)\
478 int name##_bit_count=(gb)->bit_count;\
479 int name##_cache= (gb)->cache;\
480 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
482 # define CLOSE_READER(name, gb)\
483 (gb)->bit_count= name##_bit_count;\
484 (gb)->cache= name##_cache;\
485 (gb)->buffer_ptr= name##_buffer_ptr;\
487 # define UPDATE_CACHE(name, gb)\
488 if(name##_bit_count > 0){\
489 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
490 name##_buffer_ptr+=2;\
491 name##_bit_count-= 16;\
494 # define SKIP_CACHE(name, gb, num)\
495 name##_cache <<= (num);\
497 # define SKIP_COUNTER(name, gb, num)\
498 name##_bit_count += (num);\
500 # define SKIP_BITS(name, gb, num)\
502 SKIP_CACHE(name, gb, num)\
503 SKIP_COUNTER(name, gb, num)\
506 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
507 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
509 # define SHOW_UBITS(name, gb, num)\
510 NEG_USR32(name##_cache, num)
512 # define SHOW_SBITS(name, gb, num)\
513 NEG_SSR32(name##_cache, num)
515 # define GET_CACHE(name, gb)\
516 ((uint32_t)name##_cache)
518 static inline int get_bits_count(GetBitContext *s){
519 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
522 #elif defined A32_BITSTREAM_READER
524 # define MIN_CACHE_BITS 32
526 # define OPEN_READER(name, gb)\
527 int name##_bit_count=(gb)->bit_count;\
528 uint32_t name##_cache0= (gb)->cache0;\
529 uint32_t name##_cache1= (gb)->cache1;\
530 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
532 # define CLOSE_READER(name, gb)\
533 (gb)->bit_count= name##_bit_count;\
534 (gb)->cache0= name##_cache0;\
535 (gb)->cache1= name##_cache1;\
536 (gb)->buffer_ptr= name##_buffer_ptr;\
538 # define UPDATE_CACHE(name, gb)\
539 if(name##_bit_count > 0){\
540 const uint32_t next= be2me_32( *name##_buffer_ptr );\
541 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
542 name##_cache1 |= next<<name##_bit_count;\
543 name##_buffer_ptr++;\
544 name##_bit_count-= 32;\
548 # define SKIP_CACHE(name, gb, num)\
550 "shldl %2, %1, %0 \n\t"\
552 : "+r" (name##_cache0), "+r" (name##_cache1)\
553 : "Ic" ((uint8_t)num)\
556 # define SKIP_CACHE(name, gb, num)\
557 name##_cache0 <<= (num);\
558 name##_cache0 |= NEG_USR32(name##_cache1,num);\
559 name##_cache1 <<= (num);
562 # define SKIP_COUNTER(name, gb, num)\
563 name##_bit_count += (num);\
565 # define SKIP_BITS(name, gb, num)\
567 SKIP_CACHE(name, gb, num)\
568 SKIP_COUNTER(name, gb, num)\
571 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
572 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
574 # define SHOW_UBITS(name, gb, num)\
575 NEG_USR32(name##_cache0, num)
577 # define SHOW_SBITS(name, gb, num)\
578 NEG_SSR32(name##_cache0, num)
580 # define GET_CACHE(name, gb)\
583 static inline int get_bits_count(GetBitContext *s){
584 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
589 static inline unsigned int get_bits(GetBitContext *s, int n){
593 tmp= SHOW_UBITS(re, s, n);
594 LAST_SKIP_BITS(re, s, n)
599 static inline unsigned int show_bits(GetBitContext *s, int n){
603 tmp= SHOW_UBITS(re, s, n);
604 // CLOSE_READER(re, s)
608 static inline void skip_bits(GetBitContext *s, int n){
609 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
612 LAST_SKIP_BITS(re, s, n)
616 static inline unsigned int get_bits1(GetBitContext *s){
617 #ifdef ALT_BITSTREAM_READER
619 uint8_t result= s->buffer[ index>>3 ];
620 result<<= (index&0x07);
627 return get_bits(s, 1);
631 static inline unsigned int show_bits1(GetBitContext *s){
632 return show_bits(s, 1);
635 static inline void skip_bits1(GetBitContext *s){
639 void init_get_bits(GetBitContext *s,
640 UINT8 *buffer, int buffer_size);
642 int check_marker(GetBitContext *s, const char *msg);
643 void align_get_bits(GetBitContext *s);
644 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
645 const void *bits, int bits_wrap, int bits_size,
646 const void *codes, int codes_wrap, int codes_size);
647 void free_vlc(VLC *vlc);
649 #define GET_VLC(code, name, gb, table, bits, max_depth)\
651 int n, index, nb_bits;\
653 index= SHOW_UBITS(name, gb, bits);\
654 code = table[index][0];\
655 n = table[index][1];\
657 if(max_depth > 1 && n < 0){\
658 LAST_SKIP_BITS(name, gb, bits)\
659 UPDATE_CACHE(name, gb)\
663 index= SHOW_UBITS(name, gb, nb_bits) + code;\
664 code = table[index][0];\
665 n = table[index][1];\
666 if(max_depth > 2 && n < 0){\
667 LAST_SKIP_BITS(name, gb, nb_bits)\
668 UPDATE_CACHE(name, gb)\
672 index= SHOW_UBITS(name, gb, nb_bits) + code;\
673 code = table[index][0];\
674 n = table[index][1];\
677 SKIP_BITS(name, gb, n)\
680 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
682 int n, index, nb_bits;\
684 index= SHOW_UBITS(name, gb, bits);\
685 level = table[index].level;\
686 n = table[index].len;\
688 if(max_depth > 1 && n < 0){\
689 LAST_SKIP_BITS(name, gb, bits)\
690 UPDATE_CACHE(name, gb)\
694 index= SHOW_UBITS(name, gb, nb_bits) + level;\
695 level = table[index].level;\
696 n = table[index].len;\
698 run= table[index].run;\
699 SKIP_BITS(name, gb, n)\
702 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
703 static inline int get_vlc(GetBitContext *s, VLC *vlc)
706 VLC_TYPE (*table)[2]= vlc->table;
711 GET_VLC(code, re, s, table, vlc->bits, 3)
717 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
718 int bits, int max_depth)
725 GET_VLC(code, re, s, table, bits, max_depth)
732 /* define it to include statistics code (useful only for optimizing
749 extern int st_current_index;
750 extern unsigned int st_bit_counts[ST_NB];
751 extern unsigned int st_out_bit_counts[ST_NB];
753 void print_stats(void);
756 /* misc math functions */
758 static inline int av_log2(unsigned int v)
763 if (v & 0xffff0000) {
786 static inline int mid_pred(int a, int b, int c)
800 return a + b + c - vmin - vmax;
803 static inline int clip(int a, int amin, int amax)
814 extern const UINT8 ff_sqrt_tab[128];
816 int ff_gcd(int a, int b);
818 static inline int ff_sqrt(int a)
824 if(a<128) return ff_sqrt_tab[a];
826 for(s=15; s>=0; s--){
827 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
837 * converts fourcc string to int
839 static inline int ff_get_fourcc(const char *s){
840 assert( strlen(s)==4 );
842 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
845 void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
849 #define MASK_ABS(mask, level)\
854 : "+a" (level), "=&d" (mask)\
857 #define MASK_ABS(mask, level)\
859 level= (level^mask)-mask;
863 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
864 #define COPY3_IF_LT(x,y,a,b,c,d)\
870 : "+r" (x), "+r" (a), "+r" (c)\
871 : "r" (y), "r" (b), "r" (d)\
874 #define COPY3_IF_LT(x,y,a,b,c,d)\
882 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
884 #endif /* HAVE_AV_CONFIG_H */
886 #endif /* COMMON_H */