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 */
116 #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 #endif /* HAVE_AV_CONFIG_H */
149 #define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
152 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
153 #define ABS(a) ((a) >= 0 ? (a) : (-(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;
424 static inline uint8_t* pbBufPtr(PutBitContext *s)
426 #ifdef ALT_BITSTREAM_WRITER
427 return s->buf + (s->index>>3);
433 void init_get_bits(GetBitContext *s,
434 UINT8 *buffer, int buffer_size);
436 #ifndef ALT_BITSTREAM_READER
437 unsigned int get_bits_long(GetBitContext *s, int n);
438 unsigned int show_bits_long(GetBitContext *s, int n);
441 static inline unsigned int get_bits(GetBitContext *s, int n){
442 #ifdef ALT_BITSTREAM_READER
443 #ifdef ALIGNED_BITSTREAM
445 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
446 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
448 asm ("shldl %%cl, %2, %0\n\t"
450 : "0" (result1), "r" (result2), "c" (index));
452 result1<<= (index&0x1F);
453 result2= (result2>>1) >> (31-(index&0x1F));
461 #else //ALIGNED_BITSTREAM
463 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
465 result<<= (index&0x07);
471 printf("%d", (result>>(n-1))&1);
477 #endif //!ALIGNED_BITSTREAM
478 #else //ALT_BITSTREAM_READER
480 /* most common case here */
481 unsigned int val = s->bit_buf >> (32 - n);
485 st_bit_counts[st_current_index] += n;
489 return get_bits_long(s,n);
490 #endif //!ALT_BITSTREAM_READER
493 static inline unsigned int get_bits1(GetBitContext *s){
494 #ifdef ALT_BITSTREAM_READER
496 uint8_t result= s->buffer[ index>>3 ];
497 result<<= (index&0x07);
503 printf("%d ", result);
508 /* most common case here */
509 unsigned int val = s->bit_buf >> 31;
513 st_bit_counts[st_current_index]++;
517 return get_bits_long(s,1);
521 /* This function is identical to get_bits(), the only */
522 /* diference is that it doesn't touch the buffer */
523 /* it is usefull to see the buffer. */
524 static inline unsigned int show_bits(GetBitContext *s, int n)
526 #ifdef ALT_BITSTREAM_READER
527 #ifdef ALIGNED_BITSTREAM
529 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
530 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
532 asm ("shldl %%cl, %2, %0\n\t"
534 : "0" (result1), "r" (result2), "c" (index));
536 result1<<= (index&0x1F);
537 result2= (result2>>1) >> (31-(index&0x1F));
543 #else //ALIGNED_BITSTREAM
545 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
547 result<<= (index&0x07);
551 #endif //!ALIGNED_BITSTREAM
552 #else //ALT_BITSTREAM_READER
554 /* most common case here */
555 unsigned int val = s->bit_buf >> (32 - n);
558 return show_bits_long(s,n);
559 #endif //!ALT_BITSTREAM_READER
562 static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
564 #ifdef ALT_BITSTREAM_READER
565 #ifdef ALIGNED_BITSTREAM
566 int index= (s->index + offset + 7)&(~7);
567 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
568 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
570 asm ("shldl %%cl, %2, %0\n\t"
572 : "0" (result1), "r" (result2), "c" (index));
574 result1<<= (index&0x1F);
575 result2= (result2>>1) >> (31-(index&0x1F));
581 #else //ALIGNED_BITSTREAM
582 int index= (s->index + offset + 7)>>3;
583 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
588 #endif //!ALIGNED_BITSTREAM
589 #else //ALT_BITSTREAM_READER
590 int index= (get_bits_count(s) + offset + 7)>>3;
591 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
594 //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
596 #endif //!ALT_BITSTREAM_READER
599 static inline void skip_bits(GetBitContext *s, int n){
600 #ifdef ALT_BITSTREAM_READER
606 result= get_bits(s, n);
612 /* most common case here */
616 st_bit_counts[st_current_index] += n;
624 static inline void skip_bits1(GetBitContext *s){
625 #ifdef ALT_BITSTREAM_READER
629 printf("%d ", get_bits1(s));
633 /* most common case here */
637 st_bit_counts[st_current_index]++;
645 static inline int get_bits_count(GetBitContext *s)
647 #ifdef ALT_BITSTREAM_READER
650 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
654 int check_marker(GetBitContext *s, char *msg);
655 void align_get_bits(GetBitContext *s);
656 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
657 const void *bits, int bits_wrap, int bits_size,
658 const void *codes, int codes_wrap, int codes_size);
659 void free_vlc(VLC *vlc);
661 #ifdef ALT_BITSTREAM_READER
662 #ifdef ALIGNED_BITSTREAM
664 #define SHOW_BITS(s, val, n) \
665 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
666 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
667 asm ("shldl %%cl, %2, %0\n\t"\
669 : "0" (val), "r" (result2), "c" (bit_cnt));\
670 ((uint32_t)val)>>= 32 - n;}
672 #define SHOW_BITS(s, val, n) \
673 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
674 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
675 val<<= (bit_cnt&0x1F);\
676 result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
678 ((uint32_t)val)>>= 32 - n;}
680 #else //ALIGNED_BITSTREAM
681 #define SHOW_BITS(s, val, n) \
682 val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
683 val<<= (bit_cnt&0x07);\
684 ((uint32_t)val)>>= 32 - n;
685 #endif // !ALIGNED_BITSTREAM
686 #define FLUSH_BITS(n) bit_cnt+=n;
687 #define SAVE_BITS(s) bit_cnt= (s)->index;
688 #define RESTORE_BITS(s) (s)->index= bit_cnt;
691 /* macro to go faster */
692 /* n must be <= 24 */
693 /* XXX: optimize buffer end test */
694 #define SHOW_BITS(s, val, n)\
696 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
697 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
699 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
700 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
702 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
703 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
708 val = bit_buf >> (32 - n);\
711 /* SHOW_BITS with n1 >= n must be been done before */
712 #define FLUSH_BITS(n)\
718 #define SAVE_BITS(s) \
720 bit_cnt = (s)->bit_cnt;\
721 bit_buf = (s)->bit_buf;\
722 buf_ptr = (s)->buf_ptr;\
725 #define RESTORE_BITS(s) \
727 (s)->buf_ptr = buf_ptr;\
728 (s)->bit_buf = bit_buf;\
729 (s)->bit_cnt = bit_cnt;\
731 #endif // !ALT_BITSTREAM_READER
733 static inline int get_vlc(GetBitContext *s, VLC *vlc)
735 int code, n, nb_bits, index;
739 #ifndef ALT_BITSTREAM_READER
746 table_codes = vlc->table_codes;
747 table_bits = vlc->table_bits;
749 #ifdef FAST_GET_FIRST_VLC
750 SHOW_BITS(s, index, nb_bits);
751 code = table_codes[index];
752 n = table_bits[index];
754 /* most common case (90%)*/
758 int n= bit_cnt - s->index;
770 table_codes = vlc->table_codes + code;
771 table_bits = vlc->table_bits + code;
775 SHOW_BITS(s, index, nb_bits);
776 code = table_codes[index];
777 n = table_bits[index];
779 /* most common case */
782 st_bit_counts[st_current_index] += n;
790 st_bit_counts[st_current_index] += nb_bits;
793 table_codes = vlc->table_codes + code;
794 table_bits = vlc->table_bits + code;
799 int n= bit_cnt - s->index;
809 /* define it to include statistics code (useful only for optimizing
826 extern int st_current_index;
827 extern unsigned int st_bit_counts[ST_NB];
828 extern unsigned int st_out_bit_counts[ST_NB];
830 void print_stats(void);
833 /* misc math functions */
835 static inline int av_log2(unsigned int v)
840 if (v & 0xffff0000) {
863 static inline int mid_pred(int a, int b, int c)
877 return a + b + c - vmin - vmax;
880 static inline int clip(int a, int amin, int amax)
891 void *av_malloc(int size);
892 void *av_mallocz(int size);
893 void av_free(void *ptr);
894 void __av_freep(void **ptr);
895 #define av_freep(p) __av_freep((void **)(p))
898 int ff_gcd(int a, int b);
900 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)