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
15 #define ALT_BITSTREAM_READER
18 //#define ALIGNED_BITSTREAM
19 #define FAST_GET_FIRST_VLC
20 //#define DUMP_STREAM // only works with the ALT_BITSTREAM_READER
22 #ifdef HAVE_AV_CONFIG_H
23 /* only include the following when compiling package */
36 #endif /* HAVE_AV_CONFIG_H */
38 /* Suppress restrict if it was not defined in config.h. */
47 typedef unsigned short UINT16;
48 typedef signed short INT16;
49 typedef unsigned char UINT8;
50 typedef unsigned int UINT32;
51 typedef unsigned __int64 UINT64;
52 typedef signed char INT8;
53 typedef signed int INT32;
54 typedef signed __int64 INT64;
56 typedef UINT8 uint8_t;
58 typedef UINT16 uint16_t;
59 typedef INT16 int16_t;
60 typedef UINT32 uint32_t;
61 typedef INT32 int32_t;
62 typedef UINT64 uint64_t;
63 typedef INT64 int64_t;
66 #define INT64_C(c) (c ## i64)
67 #define UINT64_C(c) (c ## i64)
69 #define inline __inline
72 #define INT64_C(c) (c ## LL)
73 #define UINT64_C(c) (c ## ULL)
74 #endif /* __MINGW32__ */
76 #define M_PI 3.14159265358979323846
77 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
83 #define snprintf _snprintf
85 #else /* CONFIG_WIN32 */
91 #ifndef __WINE_WINDEF16_H
92 /* workaround for typedef conflict in MPlayer (wine typedefs) */
93 typedef unsigned short UINT16;
94 typedef signed short INT16;
97 typedef unsigned char UINT8;
98 typedef unsigned int UINT32;
99 typedef unsigned long long UINT64;
100 typedef signed char INT8;
101 typedef signed int INT32;
102 typedef signed long long INT64;
104 #ifdef HAVE_AV_CONFIG_H
107 #include <sys/param.h>
111 #define INT64_C(c) (c ## LL)
112 #define UINT64_C(c) (c ## ULL)
115 #ifdef USE_FASTMEMCPY
116 #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
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)
159 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
160 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
161 #define MAX(a,b) ((a) > (b) ? (a) : (b))
162 #define MIN(a,b) ((a) > (b) ? (b) : (a))
166 struct PutBitContext;
168 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
170 typedef struct PutBitContext {
171 #ifdef ALT_BITSTREAM_WRITER
172 UINT8 *buf, *buf_end;
177 UINT8 *buf, *buf_ptr, *buf_end;
179 INT64 data_out_size; /* in bytes */
182 void init_put_bits(PutBitContext *s,
183 UINT8 *buffer, int buffer_size,
185 void (*write_data)(void *, UINT8 *, int));
187 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
188 void align_put_bits(PutBitContext *s);
189 void flush_put_bits(PutBitContext *s);
190 void put_string(PutBitContext * pbc, char *s);
192 /* jpeg specific put_bits */
193 void jflush_put_bits(PutBitContext *s);
197 typedef struct GetBitContext {
198 #ifdef ALT_BITSTREAM_READER
204 UINT8 *buf, *buf_ptr, *buf_end;
209 static inline int get_bits_count(GetBitContext *s);
215 int table_size, table_allocated;
218 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
220 #define unaligned32(a) (*(UINT32*)(a))
223 static inline uint32_t unaligned32(const void *v) {
226 } __attribute__((packed));
228 return ((const struct Unaligned *) v)->i;
230 #elif defined(__DECC)
231 static inline uint32_t unaligned32(const void *v) {
232 return *(const __unaligned uint32_t *) v;
235 static inline uint32_t unaligned32(const void *v) {
236 return *(const uint32_t *) v;
241 #ifndef ALT_BITSTREAM_WRITER
242 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
244 unsigned int bit_buf;
248 st_out_bit_counts[st_current_index] += n;
250 // printf("put_bits=%d %x\n", n, value);
251 assert(n == 32 || value < (1U << n));
253 bit_buf = s->bit_buf;
254 bit_left = s->bit_left;
256 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
259 bit_buf = (bit_buf<<n) | value;
263 bit_buf |= value >> (n - bit_left);
264 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
265 //printf("bitbuf = %08x\n", bit_buf);
271 s->bit_buf = bit_buf;
272 s->bit_left = bit_left;
277 #ifdef ALT_BITSTREAM_WRITER
278 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
280 #ifdef ALIGNED_BITSTREAM_WRITER
283 "movl %0, %%ecx \n\t"
284 "xorl %%eax, %%eax \n\t"
285 "shrdl %%cl, %1, %%eax \n\t"
287 "movl %0, %%ecx \n\t"
288 "shrl $3, %%ecx \n\t"
289 "andl $0xFFFFFFFC, %%ecx \n\t"
291 "orl %1, (%2, %%ecx) \n\t"
294 "movl %%eax, 4(%2, %%ecx) \n\t"
295 : "=&r" (s->index), "=&r" (value)
296 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
301 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
305 ptr[0] |= be2me_32(value>>(index&31));
306 ptr[1] = be2me_32(value<<(32-(index&31)));
307 //if(n>24) printf("%d %d\n", n, value);
311 #else //ALIGNED_BITSTREAM_WRITER
314 "movl $7, %%ecx \n\t"
315 "andl %0, %%ecx \n\t"
316 "addl %3, %%ecx \n\t"
320 "movl %0, %%ecx \n\t"
321 "shrl $3, %%ecx \n\t"
322 "orl %1, (%%ecx, %2) \n\t"
324 "movl $0, 4(%%ecx, %2) \n\t"
325 : "=&r" (s->index), "=&r" (value)
326 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
331 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
333 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
335 //if(n>24) printf("%d %d\n", n, value);
339 #endif //!ALIGNED_BITSTREAM_WRITER
343 #ifndef ALT_BITSTREAM_WRITER
344 /* for jpeg : escape 0xff with 0x00 after it */
345 static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
347 unsigned int bit_buf, b;
350 assert(n == 32 || value < (1U << n));
352 bit_buf = s->bit_buf;
353 bit_left = s->bit_left;
355 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
358 bit_buf = (bit_buf<<n) | value;
362 bit_buf |= value >> (n - bit_left);
376 s->bit_buf = bit_buf;
377 s->bit_left = bit_left;
382 #ifdef ALT_BITSTREAM_WRITER
383 static inline void jput_bits(PutBitContext *s, int n, int value)
386 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
388 //if(n>24) printf("%d %d\n", n, value);
390 v |= be2me_32(value<<(32-n-(index&7) ));
391 if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
393 /* handle idiotic (m)jpeg escapes */
394 uint8_t *bPtr= (uint8_t*)ptr;
395 int numChecked= ((index+n)>>3) - (index>>3);
400 if((v&0xFF000000)==0xFF000000 && numChecked>0){
404 *(bPtr++)= (v>>16)&0xFF;
405 if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
409 *(bPtr++)= (v>>8)&0xFF;
410 if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
415 if((v&0x000000FF)==0x000000FF && numChecked>3){
419 *((uint32_t*)bPtr)= 0;
432 static inline uint8_t* pbBufPtr(PutBitContext *s)
434 #ifdef ALT_BITSTREAM_WRITER
435 return s->buf + (s->index>>3);
441 void init_get_bits(GetBitContext *s,
442 UINT8 *buffer, int buffer_size);
444 #ifndef ALT_BITSTREAM_READER
445 unsigned int get_bits_long(GetBitContext *s, int n);
446 unsigned int show_bits_long(GetBitContext *s, int n);
449 static inline unsigned int get_bits(GetBitContext *s, int n){
450 #ifdef ALT_BITSTREAM_READER
451 #ifdef ALIGNED_BITSTREAM
453 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
454 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
456 asm ("shldl %%cl, %2, %0\n\t"
458 : "0" (result1), "r" (result2), "c" (index));
460 result1<<= (index&0x1F);
461 result2= (result2>>1) >> (31-(index&0x1F));
469 #else //ALIGNED_BITSTREAM
471 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
473 result<<= (index&0x07);
479 printf("%d", (result>>(n-1))&1);
485 #endif //!ALIGNED_BITSTREAM
486 #else //ALT_BITSTREAM_READER
488 /* most common case here */
489 unsigned int val = s->bit_buf >> (32 - n);
493 st_bit_counts[st_current_index] += n;
497 return get_bits_long(s,n);
498 #endif //!ALT_BITSTREAM_READER
501 static inline unsigned int get_bits1(GetBitContext *s){
502 #ifdef ALT_BITSTREAM_READER
504 uint8_t result= s->buffer[ index>>3 ];
505 result<<= (index&0x07);
511 printf("%d ", result);
516 /* most common case here */
517 unsigned int val = s->bit_buf >> 31;
521 st_bit_counts[st_current_index]++;
525 return get_bits_long(s,1);
529 /* This function is identical to get_bits(), the only */
530 /* diference is that it doesn't touch the buffer */
531 /* it is usefull to see the buffer. */
532 static inline unsigned int show_bits(GetBitContext *s, int n)
534 #ifdef ALT_BITSTREAM_READER
535 #ifdef ALIGNED_BITSTREAM
537 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
538 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
540 asm ("shldl %%cl, %2, %0\n\t"
542 : "0" (result1), "r" (result2), "c" (index));
544 result1<<= (index&0x1F);
545 result2= (result2>>1) >> (31-(index&0x1F));
551 #else //ALIGNED_BITSTREAM
553 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
555 result<<= (index&0x07);
559 #endif //!ALIGNED_BITSTREAM
560 #else //ALT_BITSTREAM_READER
562 /* most common case here */
563 unsigned int val = s->bit_buf >> (32 - n);
566 return show_bits_long(s,n);
567 #endif //!ALT_BITSTREAM_READER
570 static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
572 #ifdef ALT_BITSTREAM_READER
573 #ifdef ALIGNED_BITSTREAM
574 int index= (s->index + offset + 7)&(~7);
575 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
576 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
578 asm ("shldl %%cl, %2, %0\n\t"
580 : "0" (result1), "r" (result2), "c" (index));
582 result1<<= (index&0x1F);
583 result2= (result2>>1) >> (31-(index&0x1F));
589 #else //ALIGNED_BITSTREAM
590 int index= (s->index + offset + 7)>>3;
591 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
596 #endif //!ALIGNED_BITSTREAM
597 #else //ALT_BITSTREAM_READER
598 int index= (get_bits_count(s) + offset + 7)>>3;
599 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
602 //printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);
604 #endif //!ALT_BITSTREAM_READER
607 static inline void skip_bits(GetBitContext *s, int n){
608 #ifdef ALT_BITSTREAM_READER
614 result= get_bits(s, n);
620 /* most common case here */
624 st_bit_counts[st_current_index] += n;
632 static inline void skip_bits1(GetBitContext *s){
633 #ifdef ALT_BITSTREAM_READER
637 printf("%d ", get_bits1(s));
641 /* most common case here */
645 st_bit_counts[st_current_index]++;
653 static inline int get_bits_count(GetBitContext *s)
655 #ifdef ALT_BITSTREAM_READER
658 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
662 int check_marker(GetBitContext *s, char *msg);
663 void align_get_bits(GetBitContext *s);
664 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
665 const void *bits, int bits_wrap, int bits_size,
666 const void *codes, int codes_wrap, int codes_size);
667 void free_vlc(VLC *vlc);
669 #ifdef ALT_BITSTREAM_READER
670 #ifdef ALIGNED_BITSTREAM
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 asm ("shldl %%cl, %2, %0\n\t"\
677 : "0" (val), "r" (result2), "c" (bit_cnt));\
678 ((uint32_t)val)>>= 32 - n;}
680 #define SHOW_BITS(s, val, n) \
681 val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
682 {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
683 val<<= (bit_cnt&0x1F);\
684 result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
686 ((uint32_t)val)>>= 32 - n;}
688 #else //ALIGNED_BITSTREAM
689 #define SHOW_BITS(s, val, n) \
690 val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
691 val<<= (bit_cnt&0x07);\
692 ((uint32_t)val)>>= 32 - n;
693 #endif // !ALIGNED_BITSTREAM
694 #define FLUSH_BITS(n) bit_cnt+=n;
695 #define SAVE_BITS(s) bit_cnt= (s)->index;
696 #define RESTORE_BITS(s) (s)->index= bit_cnt;
699 /* macro to go faster */
700 /* n must be <= 24 */
701 /* XXX: optimize buffer end test */
702 #define SHOW_BITS(s, val, n)\
704 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
705 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
707 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
708 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
710 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
711 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
716 val = bit_buf >> (32 - n);\
719 /* SHOW_BITS with n1 >= n must be been done before */
720 #define FLUSH_BITS(n)\
726 #define SAVE_BITS(s) \
728 bit_cnt = (s)->bit_cnt;\
729 bit_buf = (s)->bit_buf;\
730 buf_ptr = (s)->buf_ptr;\
733 #define RESTORE_BITS(s) \
735 (s)->buf_ptr = buf_ptr;\
736 (s)->bit_buf = bit_buf;\
737 (s)->bit_cnt = bit_cnt;\
739 #endif // !ALT_BITSTREAM_READER
741 static inline int get_vlc(GetBitContext *s, VLC *vlc)
743 int code, n, nb_bits, index;
747 #ifndef ALT_BITSTREAM_READER
754 table_codes = vlc->table_codes;
755 table_bits = vlc->table_bits;
757 #ifdef FAST_GET_FIRST_VLC
758 SHOW_BITS(s, index, nb_bits);
759 code = table_codes[index];
760 n = table_bits[index];
762 /* most common case (90%)*/
766 int n= bit_cnt - s->index;
778 table_codes = vlc->table_codes + code;
779 table_bits = vlc->table_bits + code;
783 SHOW_BITS(s, index, nb_bits);
784 code = table_codes[index];
785 n = table_bits[index];
787 /* most common case */
790 st_bit_counts[st_current_index] += n;
798 st_bit_counts[st_current_index] += nb_bits;
801 table_codes = vlc->table_codes + code;
802 table_bits = vlc->table_bits + code;
807 int n= bit_cnt - s->index;
817 /* define it to include statistics code (useful only for optimizing
834 extern int st_current_index;
835 extern unsigned int st_bit_counts[ST_NB];
836 extern unsigned int st_out_bit_counts[ST_NB];
838 void print_stats(void);
841 /* misc math functions */
843 static inline int av_log2(unsigned int v)
848 if (v & 0xffff0000) {
871 static inline int mid_pred(int a, int b, int c)
885 return a + b + c - vmin - vmax;
888 static inline int clip(int a, int amin, int amax)
899 int ff_gcd(int a, int b);
901 static inline int ff_sqrt(int a)
907 for(s=15; s>=0; s--){
908 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
916 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
917 #define COPY3_IF_LT(x,y,a,b,c,d)\
923 : "+r" (x), "+r" (a), "+r" (c)\
924 : "r" (y), "r" (b), "r" (d)\
927 #define COPY3_IF_LT(x,y,a,b,c,d)\
935 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
937 #endif /* HAVE_AV_CONFIG_H */
939 #endif /* COMMON_H */