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;
55 #define INT64_C(c) (c ## i64)
56 #define UINT64_C(c) (c ## i64)
58 #define inline __inline
61 #define INT64_C(c) (c ## LL)
62 #define UINT64_C(c) (c ## ULL)
63 #endif /* __MINGW32__ */
65 #define M_PI 3.14159265358979323846
66 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
72 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
74 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
75 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
76 #define be2me_32(x) bswap_32(x)
78 #define snprintf _snprintf
81 /* no config.h with VC */
82 #define CONFIG_ENCODERS 1
83 #define CONFIG_DECODERS 1
93 #ifndef __WINE_WINDEF16_H
94 /* workaround for typedef conflict in MPlayer (wine typedefs) */
95 typedef unsigned short UINT16;
96 typedef signed short INT16;
99 typedef unsigned char UINT8;
100 typedef unsigned int UINT32;
101 typedef unsigned long long UINT64;
102 typedef signed char INT8;
103 typedef signed int INT32;
104 typedef signed long long INT64;
106 #ifdef HAVE_AV_CONFIG_H
109 #include <sys/param.h>
113 #define INT64_C(c) (c ## LL)
114 #define UINT64_C(c) (c ## ULL)
119 #ifdef USE_FASTMEMCPY
120 #include "fastmemcpy.h"
123 #endif /* HAVE_AV_CONFIG_H */
125 #endif /* !CONFIG_WIN32 */
129 #ifdef HAVE_AV_CONFIG_H
137 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
139 inline void dprintf(const char* fmt,...) {}
144 #define dprintf(fmt,args...) printf(fmt, ## args)
146 #define dprintf(fmt,args...)
149 #endif /* !CONFIG_WIN32 */
151 #endif /* HAVE_AV_CONFIG_H */
154 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
155 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
159 struct PutBitContext;
161 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
163 typedef struct PutBitContext {
164 #ifdef ALT_BITSTREAM_WRITER
165 UINT8 *buf, *buf_end;
170 UINT8 *buf, *buf_ptr, *buf_end;
172 INT64 data_out_size; /* in bytes */
175 void init_put_bits(PutBitContext *s,
176 UINT8 *buffer, int buffer_size,
178 void (*write_data)(void *, UINT8 *, int));
180 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
181 void align_put_bits(PutBitContext *s);
182 void flush_put_bits(PutBitContext *s);
183 void put_string(PutBitContext * pbc, char *s);
185 /* jpeg specific put_bits */
186 void jflush_put_bits(PutBitContext *s);
190 typedef struct GetBitContext {
191 #ifdef ALT_BITSTREAM_READER
197 UINT8 *buf, *buf_ptr, *buf_end;
202 static inline int get_bits_count(GetBitContext *s);
208 int table_size, table_allocated;
211 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
213 #define unaligned32(a) (*(UINT32*)(a))
216 static inline uint32_t unaligned32(const void *v) {
219 } __attribute__((packed));
221 return ((const struct Unaligned *) v)->i;
223 #elif defined(__DECC)
224 static inline uint32_t unaligned32(const void *v) {
225 return *(const __unaligned uint32_t *) v;
228 static inline uint32_t unaligned32(const void *v) {
229 return *(const uint32_t *) v;
234 #ifndef ALT_BITSTREAM_WRITER
235 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
237 unsigned int bit_buf;
241 st_out_bit_counts[st_current_index] += n;
243 // printf("put_bits=%d %x\n", n, value);
244 assert(n == 32 || value < (1U << n));
246 bit_buf = s->bit_buf;
247 bit_left = s->bit_left;
249 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
252 bit_buf = (bit_buf<<n) | value;
256 bit_buf |= value >> (n - bit_left);
257 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
258 //printf("bitbuf = %08x\n", bit_buf);
264 s->bit_buf = bit_buf;
265 s->bit_left = bit_left;
270 #ifdef ALT_BITSTREAM_WRITER
271 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
273 #ifdef ALIGNED_BITSTREAM_WRITER
276 "movl %0, %%ecx \n\t"
277 "xorl %%eax, %%eax \n\t"
278 "shrdl %%cl, %1, %%eax \n\t"
280 "movl %0, %%ecx \n\t"
281 "shrl $3, %%ecx \n\t"
282 "andl $0xFFFFFFFC, %%ecx \n\t"
284 "orl %1, (%2, %%ecx) \n\t"
287 "movl %%eax, 4(%2, %%ecx) \n\t"
288 : "=&r" (s->index), "=&r" (value)
289 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
294 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
298 ptr[0] |= be2me_32(value>>(index&31));
299 ptr[1] = be2me_32(value<<(32-(index&31)));
300 //if(n>24) printf("%d %d\n", n, value);
304 #else //ALIGNED_BITSTREAM_WRITER
307 "movl $7, %%ecx \n\t"
308 "andl %0, %%ecx \n\t"
309 "addl %3, %%ecx \n\t"
313 "movl %0, %%ecx \n\t"
314 "shrl $3, %%ecx \n\t"
315 "orl %1, (%%ecx, %2) \n\t"
317 "movl $0, 4(%%ecx, %2) \n\t"
318 : "=&r" (s->index), "=&r" (value)
319 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
324 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
326 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
328 //if(n>24) printf("%d %d\n", n, value);
332 #endif //!ALIGNED_BITSTREAM_WRITER
336 #ifndef ALT_BITSTREAM_WRITER
337 /* for jpeg : escape 0xff with 0x00 after it */
338 static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
340 unsigned int bit_buf, b;
343 assert(n == 32 || value < (1U << n));
345 bit_buf = s->bit_buf;
346 bit_left = s->bit_left;
348 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
351 bit_buf = (bit_buf<<n) | value;
355 bit_buf |= value >> (n - bit_left);
369 s->bit_buf = bit_buf;
370 s->bit_left = bit_left;
375 #ifdef ALT_BITSTREAM_WRITER
376 static inline void jput_bits(PutBitContext *s, int n, int value)
379 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
381 //if(n>24) printf("%d %d\n", n, value);
383 v |= be2me_32(value<<(32-n-(index&7) ));
384 if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
386 /* handle idiotic (m)jpeg escapes */
387 uint8_t *bPtr= (uint8_t*)ptr;
388 int numChecked= ((index+n)>>3) - (index>>3);
393 if((v&0xFF000000)==0xFF000000 && numChecked>0){
397 *(bPtr++)= (v>>16)&0xFF;
398 if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
402 *(bPtr++)= (v>>8)&0xFF;
403 if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
408 if((v&0x000000FF)==0x000000FF && numChecked>3){
412 *((uint32_t*)bPtr)= 0;
426 static inline uint8_t* pbBufPtr(PutBitContext *s)
428 #ifdef ALT_BITSTREAM_WRITER
429 return s->buf + (s->index>>3);
435 void init_get_bits(GetBitContext *s,
436 UINT8 *buffer, int buffer_size);
438 #ifndef ALT_BITSTREAM_READER
439 unsigned int get_bits_long(GetBitContext *s, int n);
440 unsigned int show_bits_long(GetBitContext *s, int n);
443 static inline unsigned int get_bits(GetBitContext *s, int n){
444 #ifdef ALT_BITSTREAM_READER
445 #ifdef ALIGNED_BITSTREAM
447 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
448 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
450 asm ("shldl %%cl, %2, %0\n\t"
452 : "0" (result1), "r" (result2), "c" (index));
454 result1<<= (index&0x1F);
455 result2= (result2>>1) >> (31-(index&0x1F));
463 #else //ALIGNED_BITSTREAM
465 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
467 result<<= (index&0x07);
473 printf("%d", (result>>(n-1))&1);
479 #endif //!ALIGNED_BITSTREAM
480 #else //ALT_BITSTREAM_READER
482 /* most common case here */
483 unsigned int val = s->bit_buf >> (32 - n);
487 st_bit_counts[st_current_index] += n;
491 return get_bits_long(s,n);
492 #endif //!ALT_BITSTREAM_READER
495 static inline unsigned int get_bits1(GetBitContext *s){
496 #ifdef ALT_BITSTREAM_READER
498 uint8_t result= s->buffer[ index>>3 ];
499 result<<= (index&0x07);
505 printf("%d ", result);
510 /* most common case here */
511 unsigned int val = s->bit_buf >> 31;
515 st_bit_counts[st_current_index]++;
519 return get_bits_long(s,1);
523 /* This function is identical to get_bits(), the only */
524 /* diference is that it doesn't touch the buffer */
525 /* it is usefull to see the buffer. */
526 static inline unsigned int show_bits(GetBitContext *s, int n)
528 #ifdef ALT_BITSTREAM_READER
529 #ifdef ALIGNED_BITSTREAM
531 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
532 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
534 asm ("shldl %%cl, %2, %0\n\t"
536 : "0" (result1), "r" (result2), "c" (index));
538 result1<<= (index&0x1F);
539 result2= (result2>>1) >> (31-(index&0x1F));
545 #else //ALIGNED_BITSTREAM
547 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
549 result<<= (index&0x07);
553 #endif //!ALIGNED_BITSTREAM
554 #else //ALT_BITSTREAM_READER
556 /* most common case here */
557 unsigned int val = s->bit_buf >> (32 - n);
560 return show_bits_long(s,n);
561 #endif //!ALT_BITSTREAM_READER
564 static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
566 #ifdef ALT_BITSTREAM_READER
567 #ifdef ALIGNED_BITSTREAM
568 int index= (s->index + offset + 7)&(~7);
569 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
570 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
572 asm ("shldl %%cl, %2, %0\n\t"
574 : "0" (result1), "r" (result2), "c" (index));
576 result1<<= (index&0x1F);
577 result2= (result2>>1) >> (31-(index&0x1F));
583 #else //ALIGNED_BITSTREAM
584 int index= (s->index + offset + 7)>>3;
585 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
590 #endif //!ALIGNED_BITSTREAM
591 #else //ALT_BITSTREAM_READER
592 int index= (get_bits_count(s) + offset + 7)>>3;
593 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
596 //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
598 #endif //!ALT_BITSTREAM_READER
601 static inline void skip_bits(GetBitContext *s, int n){
602 #ifdef ALT_BITSTREAM_READER
608 result= get_bits(s, n);
614 /* most common case here */
618 st_bit_counts[st_current_index] += n;
626 static inline void skip_bits1(GetBitContext *s){
627 #ifdef ALT_BITSTREAM_READER
631 printf("%d ", get_bits1(s));
635 /* most common case here */
639 st_bit_counts[st_current_index]++;
647 static inline int get_bits_count(GetBitContext *s)
649 #ifdef ALT_BITSTREAM_READER
652 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
656 int check_marker(GetBitContext *s, char *msg);
657 void align_get_bits(GetBitContext *s);
658 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
659 const void *bits, int bits_wrap, int bits_size,
660 const void *codes, int codes_wrap, int codes_size);
661 void free_vlc(VLC *vlc);
663 #ifdef ALT_BITSTREAM_READER
664 #ifdef ALIGNED_BITSTREAM
666 #define SHOW_BITS(s, val, n) \
667 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
668 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
669 asm ("shldl %%cl, %2, %0\n\t"\
671 : "0" (val), "r" (result2), "c" (bit_cnt));\
672 ((uint32_t)val)>>= 32 - n;}
674 #define SHOW_BITS(s, val, n) \
675 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
676 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
677 val<<= (bit_cnt&0x1F);\
678 result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
680 ((uint32_t)val)>>= 32 - n;}
682 #else //ALIGNED_BITSTREAM
683 #define SHOW_BITS(s, val, n) \
684 val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
685 val<<= (bit_cnt&0x07);\
686 ((uint32_t)val)>>= 32 - n;
687 #endif // !ALIGNED_BITSTREAM
688 #define FLUSH_BITS(n) bit_cnt+=n;
689 #define SAVE_BITS(s) bit_cnt= (s)->index;
690 #define RESTORE_BITS(s) (s)->index= bit_cnt;
693 /* macro to go faster */
694 /* n must be <= 24 */
695 /* XXX: optimize buffer end test */
696 #define SHOW_BITS(s, val, n)\
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);\
704 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
705 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
710 val = bit_buf >> (32 - n);\
713 /* SHOW_BITS with n1 >= n must be been done before */
714 #define FLUSH_BITS(n)\
720 #define SAVE_BITS(s) \
722 bit_cnt = (s)->bit_cnt;\
723 bit_buf = (s)->bit_buf;\
724 buf_ptr = (s)->buf_ptr;\
727 #define RESTORE_BITS(s) \
729 (s)->buf_ptr = buf_ptr;\
730 (s)->bit_buf = bit_buf;\
731 (s)->bit_cnt = bit_cnt;\
733 #endif // !ALT_BITSTREAM_READER
735 static inline int get_vlc(GetBitContext *s, VLC *vlc)
737 int code, n, nb_bits, index;
741 #ifndef ALT_BITSTREAM_READER
748 table_codes = vlc->table_codes;
749 table_bits = vlc->table_bits;
751 #ifdef FAST_GET_FIRST_VLC
752 SHOW_BITS(s, index, nb_bits);
753 code = table_codes[index];
754 n = table_bits[index];
756 /* most common case (90%)*/
760 int n= bit_cnt - s->index;
772 table_codes = vlc->table_codes + code;
773 table_bits = vlc->table_bits + code;
777 SHOW_BITS(s, index, nb_bits);
778 code = table_codes[index];
779 n = table_bits[index];
781 /* most common case */
784 st_bit_counts[st_current_index] += n;
792 st_bit_counts[st_current_index] += nb_bits;
795 table_codes = vlc->table_codes + code;
796 table_bits = vlc->table_bits + code;
801 int n= bit_cnt - s->index;
811 /* define it to include statistics code (useful only for optimizing
828 extern int st_current_index;
829 extern unsigned int st_bit_counts[ST_NB];
830 extern unsigned int st_out_bit_counts[ST_NB];
832 void print_stats(void);
835 /* misc math functions */
837 static inline int av_log2(unsigned int v)
842 if (v & 0xffff0000) {
865 static inline int mid_pred(int a, int b, int c)
879 return a + b + c - vmin - vmax;
882 static inline int clip(int a, int amin, int amax)
893 void *av_malloc(int size);
894 void *av_mallocz(int size);
895 void av_free(void *ptr);
896 void __av_freep(void **ptr);
897 #define av_freep(p) __av_freep((void **)(p))
900 int ff_gcd(int a, int b);
902 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)