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
13 //#define ALT_BITSTREAM_READER
14 //#define ALIGNED_BITSTREAM
15 #define FAST_GET_FIRST_VLC
16 //#define DUMP_STREAM // only works with the ALT_BITSTREAM_READER
18 #ifdef HAVE_AV_CONFIG_H
19 /* only include the following when compiling package */
32 #endif /* HAVE_AV_CONFIG_H */
38 typedef unsigned short UINT16;
39 typedef signed short INT16;
40 typedef unsigned char UINT8;
41 typedef unsigned int UINT32;
42 typedef unsigned __int64 UINT64;
43 typedef signed char INT8;
44 typedef signed int INT32;
45 typedef signed __int64 INT64;
47 typedef UINT8 uint8_t;
49 typedef UINT16 uint16_t;
50 typedef INT16 int16_t;
51 typedef UINT32 uint32_t;
52 typedef INT32 int32_t;
53 typedef UINT64 uint64_t;
54 typedef INT64 int64_t;
57 #define INT64_C(c) (c ## i64)
58 #define UINT64_C(c) (c ## i64)
60 #define inline __inline
63 #define INT64_C(c) (c ## LL)
64 #define UINT64_C(c) (c ## ULL)
65 #endif /* __MINGW32__ */
67 #define M_PI 3.14159265358979323846
68 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
74 #define snprintf _snprintf
76 #else /* CONFIG_WIN32 */
82 #ifndef __WINE_WINDEF16_H
83 /* workaround for typedef conflict in MPlayer (wine typedefs) */
84 typedef unsigned short UINT16;
85 typedef signed short INT16;
88 typedef unsigned char UINT8;
89 typedef unsigned int UINT32;
90 typedef unsigned long long UINT64;
91 typedef signed char INT8;
92 typedef signed int INT32;
93 typedef signed long long INT64;
95 #ifdef HAVE_AV_CONFIG_H
98 #include <sys/param.h>
102 #define INT64_C(c) (c ## LL)
103 #define UINT64_C(c) (c ## ULL)
106 #ifdef USE_FASTMEMCPY
107 #include "fastmemcpy.h"
110 #endif /* HAVE_AV_CONFIG_H */
112 #endif /* !CONFIG_WIN32 */
114 #ifdef HAVE_AV_CONFIG_H
118 #if defined(__MINGW32__) || defined(__CYGWIN__) || \
119 defined(__OS2__) || defined (__OpenBSD__)
120 #define MANGLE(a) "_" #a
133 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
135 inline void dprintf(const char* fmt,...) {}
140 #define dprintf(fmt,args...) printf(fmt, ## args)
142 #define dprintf(fmt,args...)
145 #endif /* !CONFIG_WIN32 */
147 #define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
150 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
151 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
152 #define MAX(a,b) ((a) > (b) ? (a) : (b))
153 #define MIN(a,b) ((a) > (b) ? (b) : (a))
157 struct PutBitContext;
159 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
161 typedef struct PutBitContext {
162 #ifdef ALT_BITSTREAM_WRITER
163 UINT8 *buf, *buf_end;
168 UINT8 *buf, *buf_ptr, *buf_end;
170 INT64 data_out_size; /* in bytes */
173 void init_put_bits(PutBitContext *s,
174 UINT8 *buffer, int buffer_size,
176 void (*write_data)(void *, UINT8 *, int));
178 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
179 void align_put_bits(PutBitContext *s);
180 void flush_put_bits(PutBitContext *s);
181 void put_string(PutBitContext * pbc, char *s);
183 /* jpeg specific put_bits */
184 void jflush_put_bits(PutBitContext *s);
188 typedef struct GetBitContext {
189 #ifdef ALT_BITSTREAM_READER
195 UINT8 *buf, *buf_ptr, *buf_end;
200 static inline int get_bits_count(GetBitContext *s);
206 int table_size, table_allocated;
209 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
211 #define unaligned32(a) (*(UINT32*)(a))
214 static inline uint32_t unaligned32(const void *v) {
217 } __attribute__((packed));
219 return ((const struct Unaligned *) v)->i;
221 #elif defined(__DECC)
222 static inline uint32_t unaligned32(const void *v) {
223 return *(const __unaligned uint32_t *) v;
226 static inline uint32_t unaligned32(const void *v) {
227 return *(const uint32_t *) v;
232 #ifndef ALT_BITSTREAM_WRITER
233 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
235 unsigned int bit_buf;
239 st_out_bit_counts[st_current_index] += n;
241 // printf("put_bits=%d %x\n", n, value);
242 assert(n == 32 || value < (1U << n));
244 bit_buf = s->bit_buf;
245 bit_left = s->bit_left;
247 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
250 bit_buf = (bit_buf<<n) | value;
254 bit_buf |= value >> (n - bit_left);
255 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
256 //printf("bitbuf = %08x\n", bit_buf);
262 s->bit_buf = bit_buf;
263 s->bit_left = bit_left;
268 #ifdef ALT_BITSTREAM_WRITER
269 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
271 #ifdef ALIGNED_BITSTREAM_WRITER
274 "movl %0, %%ecx \n\t"
275 "xorl %%eax, %%eax \n\t"
276 "shrdl %%cl, %1, %%eax \n\t"
278 "movl %0, %%ecx \n\t"
279 "shrl $3, %%ecx \n\t"
280 "andl $0xFFFFFFFC, %%ecx \n\t"
282 "orl %1, (%2, %%ecx) \n\t"
285 "movl %%eax, 4(%2, %%ecx) \n\t"
286 : "=&r" (s->index), "=&r" (value)
287 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
292 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
296 ptr[0] |= be2me_32(value>>(index&31));
297 ptr[1] = be2me_32(value<<(32-(index&31)));
298 //if(n>24) printf("%d %d\n", n, value);
302 #else //ALIGNED_BITSTREAM_WRITER
305 "movl $7, %%ecx \n\t"
306 "andl %0, %%ecx \n\t"
307 "addl %3, %%ecx \n\t"
311 "movl %0, %%ecx \n\t"
312 "shrl $3, %%ecx \n\t"
313 "orl %1, (%%ecx, %2) \n\t"
315 "movl $0, 4(%%ecx, %2) \n\t"
316 : "=&r" (s->index), "=&r" (value)
317 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
322 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
324 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
326 //if(n>24) printf("%d %d\n", n, value);
330 #endif //!ALIGNED_BITSTREAM_WRITER
334 #ifndef ALT_BITSTREAM_WRITER
335 /* for jpeg : escape 0xff with 0x00 after it */
336 static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
338 unsigned int bit_buf, b;
341 assert(n == 32 || value < (1U << n));
343 bit_buf = s->bit_buf;
344 bit_left = s->bit_left;
346 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
349 bit_buf = (bit_buf<<n) | value;
353 bit_buf |= value >> (n - bit_left);
367 s->bit_buf = bit_buf;
368 s->bit_left = bit_left;
373 #ifdef ALT_BITSTREAM_WRITER
374 static inline void jput_bits(PutBitContext *s, int n, int value)
377 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
379 //if(n>24) printf("%d %d\n", n, value);
381 v |= be2me_32(value<<(32-n-(index&7) ));
382 if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
384 /* handle idiotic (m)jpeg escapes */
385 uint8_t *bPtr= (uint8_t*)ptr;
386 int numChecked= ((index+n)>>3) - (index>>3);
391 if((v&0xFF000000)==0xFF000000 && numChecked>0){
395 *(bPtr++)= (v>>16)&0xFF;
396 if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
400 *(bPtr++)= (v>>8)&0xFF;
401 if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
406 if((v&0x000000FF)==0x000000FF && numChecked>3){
410 *((uint32_t*)bPtr)= 0;
423 static inline uint8_t* pbBufPtr(PutBitContext *s)
425 #ifdef ALT_BITSTREAM_WRITER
426 return s->buf + (s->index>>3);
432 void init_get_bits(GetBitContext *s,
433 UINT8 *buffer, int buffer_size);
435 #ifndef ALT_BITSTREAM_READER
436 unsigned int get_bits_long(GetBitContext *s, int n);
437 unsigned int show_bits_long(GetBitContext *s, int n);
440 static inline unsigned int get_bits(GetBitContext *s, int n){
441 #ifdef ALT_BITSTREAM_READER
442 #ifdef ALIGNED_BITSTREAM
444 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
445 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
447 asm ("shldl %%cl, %2, %0\n\t"
449 : "0" (result1), "r" (result2), "c" (index));
451 result1<<= (index&0x1F);
452 result2= (result2>>1) >> (31-(index&0x1F));
460 #else //ALIGNED_BITSTREAM
462 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
464 result<<= (index&0x07);
470 printf("%d", (result>>(n-1))&1);
476 #endif //!ALIGNED_BITSTREAM
477 #else //ALT_BITSTREAM_READER
479 /* most common case here */
480 unsigned int val = s->bit_buf >> (32 - n);
484 st_bit_counts[st_current_index] += n;
488 return get_bits_long(s,n);
489 #endif //!ALT_BITSTREAM_READER
492 static inline unsigned int get_bits1(GetBitContext *s){
493 #ifdef ALT_BITSTREAM_READER
495 uint8_t result= s->buffer[ index>>3 ];
496 result<<= (index&0x07);
502 printf("%d ", result);
507 /* most common case here */
508 unsigned int val = s->bit_buf >> 31;
512 st_bit_counts[st_current_index]++;
516 return get_bits_long(s,1);
520 /* This function is identical to get_bits(), the only */
521 /* diference is that it doesn't touch the buffer */
522 /* it is usefull to see the buffer. */
523 static inline unsigned int show_bits(GetBitContext *s, int n)
525 #ifdef ALT_BITSTREAM_READER
526 #ifdef ALIGNED_BITSTREAM
528 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
529 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
531 asm ("shldl %%cl, %2, %0\n\t"
533 : "0" (result1), "r" (result2), "c" (index));
535 result1<<= (index&0x1F);
536 result2= (result2>>1) >> (31-(index&0x1F));
542 #else //ALIGNED_BITSTREAM
544 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
546 result<<= (index&0x07);
550 #endif //!ALIGNED_BITSTREAM
551 #else //ALT_BITSTREAM_READER
553 /* most common case here */
554 unsigned int val = s->bit_buf >> (32 - n);
557 return show_bits_long(s,n);
558 #endif //!ALT_BITSTREAM_READER
561 static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
563 #ifdef ALT_BITSTREAM_READER
564 #ifdef ALIGNED_BITSTREAM
565 int index= (s->index + offset + 7)&(~7);
566 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
567 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
569 asm ("shldl %%cl, %2, %0\n\t"
571 : "0" (result1), "r" (result2), "c" (index));
573 result1<<= (index&0x1F);
574 result2= (result2>>1) >> (31-(index&0x1F));
580 #else //ALIGNED_BITSTREAM
581 int index= (s->index + offset + 7)>>3;
582 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
587 #endif //!ALIGNED_BITSTREAM
588 #else //ALT_BITSTREAM_READER
589 int index= (get_bits_count(s) + offset + 7)>>3;
590 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
593 //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
595 #endif //!ALT_BITSTREAM_READER
598 static inline void skip_bits(GetBitContext *s, int n){
599 #ifdef ALT_BITSTREAM_READER
605 result= get_bits(s, n);
611 /* most common case here */
615 st_bit_counts[st_current_index] += n;
623 static inline void skip_bits1(GetBitContext *s){
624 #ifdef ALT_BITSTREAM_READER
628 printf("%d ", get_bits1(s));
632 /* most common case here */
636 st_bit_counts[st_current_index]++;
644 static inline int get_bits_count(GetBitContext *s)
646 #ifdef ALT_BITSTREAM_READER
649 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
653 int check_marker(GetBitContext *s, char *msg);
654 void align_get_bits(GetBitContext *s);
655 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
656 const void *bits, int bits_wrap, int bits_size,
657 const void *codes, int codes_wrap, int codes_size);
658 void free_vlc(VLC *vlc);
660 #ifdef ALT_BITSTREAM_READER
661 #ifdef ALIGNED_BITSTREAM
663 #define SHOW_BITS(s, val, n) \
664 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
665 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
666 asm ("shldl %%cl, %2, %0\n\t"\
668 : "0" (val), "r" (result2), "c" (bit_cnt));\
669 ((uint32_t)val)>>= 32 - n;}
671 #define SHOW_BITS(s, val, n) \
672 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
673 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
674 val<<= (bit_cnt&0x1F);\
675 result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
677 ((uint32_t)val)>>= 32 - n;}
679 #else //ALIGNED_BITSTREAM
680 #define SHOW_BITS(s, val, n) \
681 val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
682 val<<= (bit_cnt&0x07);\
683 ((uint32_t)val)>>= 32 - n;
684 #endif // !ALIGNED_BITSTREAM
685 #define FLUSH_BITS(n) bit_cnt+=n;
686 #define SAVE_BITS(s) bit_cnt= (s)->index;
687 #define RESTORE_BITS(s) (s)->index= bit_cnt;
690 /* macro to go faster */
691 /* n must be <= 24 */
692 /* XXX: optimize buffer end test */
693 #define SHOW_BITS(s, val, n)\
695 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
696 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
698 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
699 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
701 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
702 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
707 val = bit_buf >> (32 - n);\
710 /* SHOW_BITS with n1 >= n must be been done before */
711 #define FLUSH_BITS(n)\
717 #define SAVE_BITS(s) \
719 bit_cnt = (s)->bit_cnt;\
720 bit_buf = (s)->bit_buf;\
721 buf_ptr = (s)->buf_ptr;\
724 #define RESTORE_BITS(s) \
726 (s)->buf_ptr = buf_ptr;\
727 (s)->bit_buf = bit_buf;\
728 (s)->bit_cnt = bit_cnt;\
730 #endif // !ALT_BITSTREAM_READER
732 static inline int get_vlc(GetBitContext *s, VLC *vlc)
734 int code, n, nb_bits, index;
738 #ifndef ALT_BITSTREAM_READER
745 table_codes = vlc->table_codes;
746 table_bits = vlc->table_bits;
748 #ifdef FAST_GET_FIRST_VLC
749 SHOW_BITS(s, index, nb_bits);
750 code = table_codes[index];
751 n = table_bits[index];
753 /* most common case (90%)*/
757 int n= bit_cnt - s->index;
769 table_codes = vlc->table_codes + code;
770 table_bits = vlc->table_bits + code;
774 SHOW_BITS(s, index, nb_bits);
775 code = table_codes[index];
776 n = table_bits[index];
778 /* most common case */
781 st_bit_counts[st_current_index] += n;
789 st_bit_counts[st_current_index] += nb_bits;
792 table_codes = vlc->table_codes + code;
793 table_bits = vlc->table_bits + code;
798 int n= bit_cnt - s->index;
808 /* define it to include statistics code (useful only for optimizing
825 extern int st_current_index;
826 extern unsigned int st_bit_counts[ST_NB];
827 extern unsigned int st_out_bit_counts[ST_NB];
829 void print_stats(void);
832 /* misc math functions */
834 static inline int av_log2(unsigned int v)
839 if (v & 0xffff0000) {
862 static inline int mid_pred(int a, int b, int c)
876 return a + b + c - vmin - vmax;
879 static inline int clip(int a, int amin, int amax)
890 int ff_gcd(int a, int b);
892 static inline int ff_sqrt(int a)
898 for(s=15; s>=0; s--){
899 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
907 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
908 #define COPY3_IF_LT(x,y,a,b,c,d)\
914 : "+r" (x), "+r" (a), "+r" (c)\
915 : "r" (y), "r" (b), "r" (d)\
918 #define COPY3_IF_LT(x,y,a,b,c,d)\
926 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
928 #endif /* HAVE_AV_CONFIG_H */
930 #endif /* COMMON_H */