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 */
37 #define M_PI 3.14159265358979323846
40 #endif /* HAVE_AV_CONFIG_H */
42 /* Suppress restrict if it was not defined in config.h. */
47 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
48 # define always_inline __attribute__((always_inline)) inline
50 # define always_inline inline
57 typedef unsigned short UINT16
;
58 typedef signed short INT16
;
59 typedef unsigned char UINT8
;
60 typedef unsigned int UINT32
;
61 typedef unsigned __int64 UINT64
;
62 typedef signed char INT8
;
63 typedef signed int INT32
;
64 typedef signed __int64 INT64
;
66 typedef UINT8
uint8_t;
68 typedef UINT16
uint16_t;
69 typedef INT16
int16_t;
70 typedef UINT32
uint32_t;
71 typedef INT32
int32_t;
72 typedef UINT64
uint64_t;
73 typedef INT64
int64_t;
76 # define INT64_C(c) (c ## i64)
77 # define UINT64_C(c) (c ## i64)
79 # define inline __inline
82 # define INT64_C(c) (c ## LL)
83 # define UINT64_C(c) (c ## ULL)
84 # endif /* __MINGW32__ */
90 # define snprintf _snprintf
92 #else /* CONFIG_WIN32 */
96 # include <inttypes.h>
98 # ifndef __WINE_WINDEF16_H
99 /* workaround for typedef conflict in MPlayer (wine typedefs) */
100 typedef unsigned short UINT16
;
101 typedef signed short INT16
;
104 typedef unsigned char UINT8
;
105 typedef unsigned int UINT32
;
106 typedef unsigned long long UINT64
;
107 typedef signed char INT8
;
108 typedef signed int INT32
;
109 typedef signed long long INT64
;
111 # ifdef HAVE_AV_CONFIG_H
113 # define INT64_C(c) (c ## LL)
114 # define UINT64_C(c) (c ## ULL)
117 # ifdef USE_FASTMEMCPY
118 # include "fastmemcpy.h"
120 # endif /* HAVE_AV_CONFIG_H */
122 #endif /* !CONFIG_WIN32 */
124 #ifdef HAVE_AV_CONFIG_H
128 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
129 defined(__OS2__) || defined (__OpenBSD__)
130 # define MANGLE(a) "_" #a
132 # define MANGLE(a) #a
143 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
145 inline void dprintf(const char* fmt
,...) {}
150 # define dprintf(fmt,args...) printf(fmt, ## args)
152 # define dprintf(fmt,args...)
155 # endif /* !CONFIG_WIN32 */
157 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
159 //rounded divison & shift
160 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
162 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
163 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
165 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
166 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
169 // avoid +32 for shift optimization (gcc should do that ...)
170 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
171 asm ("sarl %1, %0\n\t"
173 : "ic" ((uint8_t)(-s
))
177 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
178 asm ("shrl %1, %0\n\t"
180 : "ic" ((uint8_t)(-s
))
185 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
186 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
191 struct PutBitContext
;
193 typedef void (*WriteDataFunc
)(void *, UINT8
*, int);
195 typedef struct PutBitContext
{
196 #ifdef ALT_BITSTREAM_WRITER
197 UINT8
*buf
, *buf_end
;
202 UINT8
*buf
, *buf_ptr
, *buf_end
;
204 INT64 data_out_size
; /* in bytes */
207 void init_put_bits(PutBitContext
*s
,
208 UINT8
*buffer
, int buffer_size
,
210 void (*write_data
)(void *, UINT8
*, int));
212 INT64
get_bit_count(PutBitContext
*s
); /* XXX: change function name */
213 void align_put_bits(PutBitContext
*s
);
214 void flush_put_bits(PutBitContext
*s
);
215 void put_string(PutBitContext
* pbc
, char *s
);
219 typedef struct GetBitContext
{
220 UINT8
*buffer
, *buffer_end
;
221 #ifdef ALT_BITSTREAM_READER
223 #elif defined LIBMPEG2_BITSTREAM_READER
227 #elif defined A32_BITSTREAM_READER
236 static inline int get_bits_count(GetBitContext
*s
);
238 #define VLC_TYPE INT16
242 VLC_TYPE (*table
)[2]; // code, bits
243 int table_size
, table_allocated
;
246 typedef struct RL_VLC_ELEM
{
253 #define UNALIGNED_STORES_ARE_BAD
256 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
258 # define unaligned32(a) (*(UINT32*)(a))
261 static inline uint32_t unaligned32(const void *v
) {
264 } __attribute__((packed
));
266 return ((const struct Unaligned
*) v
)->i
;
268 # elif defined(__DECC)
269 static inline uint32_t unaligned32(const void *v
) {
270 return *(const __unaligned
uint32_t *) v
;
273 static inline uint32_t unaligned32(const void *v
) {
274 return *(const uint32_t *) v
;
279 #ifndef ALT_BITSTREAM_WRITER
280 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
282 unsigned int bit_buf
;
286 st_out_bit_counts
[st_current_index
] += n
;
288 // printf("put_bits=%d %x\n", n, value);
289 assert(n
== 32 || value
< (1U << n
));
291 bit_buf
= s
->bit_buf
;
292 bit_left
= s
->bit_left
;
294 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
297 bit_buf
= (bit_buf
<<n
) | value
;
301 bit_buf
|= value
>> (n
- bit_left
);
302 #ifdef UNALIGNED_STORES_ARE_BAD
303 if (3 & (int) s
->buf_ptr
) {
304 s
->buf_ptr
[0] = bit_buf
>> 24;
305 s
->buf_ptr
[1] = bit_buf
>> 16;
306 s
->buf_ptr
[2] = bit_buf
>> 8;
307 s
->buf_ptr
[3] = bit_buf
;
310 *(UINT32
*)s
->buf_ptr
= be2me_32(bit_buf
);
311 //printf("bitbuf = %08x\n", bit_buf);
317 s
->bit_buf
= bit_buf
;
318 s
->bit_left
= bit_left
;
323 #ifdef ALT_BITSTREAM_WRITER
324 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
326 # ifdef ALIGNED_BITSTREAM_WRITER
329 "movl %0, %%ecx \n\t"
330 "xorl %%eax, %%eax \n\t"
331 "shrdl %%cl, %1, %%eax \n\t"
333 "movl %0, %%ecx \n\t"
334 "shrl $3, %%ecx \n\t"
335 "andl $0xFFFFFFFC, %%ecx \n\t"
337 "orl %1, (%2, %%ecx) \n\t"
340 "movl %%eax, 4(%2, %%ecx) \n\t"
341 : "=&r" (s
->index
), "=&r" (value
)
342 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
347 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
351 ptr
[0] |= be2me_32(value
>>(index
&31));
352 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
353 //if(n>24) printf("%d %d\n", n, value);
357 # else //ALIGNED_BITSTREAM_WRITER
360 "movl $7, %%ecx \n\t"
361 "andl %0, %%ecx \n\t"
362 "addl %3, %%ecx \n\t"
366 "movl %0, %%ecx \n\t"
367 "shrl $3, %%ecx \n\t"
368 "orl %1, (%%ecx, %2) \n\t"
370 "movl $0, 4(%%ecx, %2) \n\t"
371 : "=&r" (s
->index
), "=&r" (value
)
372 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
377 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
379 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
381 //if(n>24) printf("%d %d\n", n, value);
385 # endif //!ALIGNED_BITSTREAM_WRITER
390 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
392 #ifdef ALT_BITSTREAM_WRITER
393 return s
->buf
+ (s
->index
>>3);
399 /* Bitstream reader API docs:
401 abritary name which is used as prefix for the internal variables
406 OPEN_READER(name, gb)
407 loads gb into local variables
409 CLOSE_READER(name, gb)
410 stores local vars in gb
412 UPDATE_CACHE(name, gb)
413 refills the internal cache from the bitstream
414 after this call at least MIN_CACHE_BITS will be available,
417 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
419 SHOW_UBITS(name, gb, num)
420 will return the nest num bits
422 SHOW_SBITS(name, gb, num)
423 will return the nest num bits and do sign extension
425 SKIP_BITS(name, gb, num)
426 will skip over the next num bits
427 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
429 SKIP_CACHE(name, gb, num)
430 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
432 SKIP_COUNTER(name, gb, num)
433 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
435 LAST_SKIP_CACHE(name, gb, num)
436 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
438 LAST_SKIP_BITS(name, gb, num)
439 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
441 for examples see get_bits, show_bits, skip_bits, get_vlc
444 #ifdef ALT_BITSTREAM_READER
445 # define MIN_CACHE_BITS 25
447 # define OPEN_READER(name, gb)\
448 int name##_index= (gb)->index;\
449 int name##_cache= 0;\
451 # define CLOSE_READER(name, gb)\
452 (gb)->index= name##_index;\
454 # define UPDATE_CACHE(name, gb)\
455 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
457 # define SKIP_CACHE(name, gb, num)\
458 name##_cache <<= (num);\
461 # define SKIP_COUNTER(name, gb, num)\
462 name##_index += (num);\
464 # define SKIP_BITS(name, gb, num)\
466 SKIP_CACHE(name, gb, num)\
467 SKIP_COUNTER(name, gb, num)\
470 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
471 # define LAST_SKIP_CACHE(name, gb, num) ;
473 # define SHOW_UBITS(name, gb, num)\
474 NEG_USR32(name##_cache, num)
476 # define SHOW_SBITS(name, gb, num)\
477 NEG_SSR32(name##_cache, num)
479 # define GET_CACHE(name, gb)\
480 ((uint32_t)name##_cache)
482 static inline int get_bits_count(GetBitContext
*s
){
485 #elif defined LIBMPEG2_BITSTREAM_READER
486 //libmpeg2 like reader
488 # define MIN_CACHE_BITS 16
490 # define OPEN_READER(name, gb)\
491 int name##_bit_count=(gb)->bit_count;\
492 int name##_cache= (gb)->cache;\
493 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
495 # define CLOSE_READER(name, gb)\
496 (gb)->bit_count= name##_bit_count;\
497 (gb)->cache= name##_cache;\
498 (gb)->buffer_ptr= name##_buffer_ptr;\
500 # define UPDATE_CACHE(name, gb)\
501 if(name##_bit_count > 0){\
502 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
503 name##_buffer_ptr+=2;\
504 name##_bit_count-= 16;\
507 # define SKIP_CACHE(name, gb, num)\
508 name##_cache <<= (num);\
510 # define SKIP_COUNTER(name, gb, num)\
511 name##_bit_count += (num);\
513 # define SKIP_BITS(name, gb, num)\
515 SKIP_CACHE(name, gb, num)\
516 SKIP_COUNTER(name, gb, num)\
519 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
520 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
522 # define SHOW_UBITS(name, gb, num)\
523 NEG_USR32(name##_cache, num)
525 # define SHOW_SBITS(name, gb, num)\
526 NEG_SSR32(name##_cache, num)
528 # define GET_CACHE(name, gb)\
529 ((uint32_t)name##_cache)
531 static inline int get_bits_count(GetBitContext
*s
){
532 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
535 #elif defined A32_BITSTREAM_READER
537 # define MIN_CACHE_BITS 32
539 # define OPEN_READER(name, gb)\
540 int name##_bit_count=(gb)->bit_count;\
541 uint32_t name##_cache0= (gb)->cache0;\
542 uint32_t name##_cache1= (gb)->cache1;\
543 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
545 # define CLOSE_READER(name, gb)\
546 (gb)->bit_count= name##_bit_count;\
547 (gb)->cache0= name##_cache0;\
548 (gb)->cache1= name##_cache1;\
549 (gb)->buffer_ptr= name##_buffer_ptr;\
551 # define UPDATE_CACHE(name, gb)\
552 if(name##_bit_count > 0){\
553 const uint32_t next= be2me_32( *name##_buffer_ptr );\
554 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
555 name##_cache1 |= next<<name##_bit_count;\
556 name##_buffer_ptr++;\
557 name##_bit_count-= 32;\
561 # define SKIP_CACHE(name, gb, num)\
563 "shldl %2, %1, %0 \n\t"\
565 : "+r" (name##_cache0), "+r" (name##_cache1)\
566 : "Ic" ((uint8_t)num)\
569 # define SKIP_CACHE(name, gb, num)\
570 name##_cache0 <<= (num);\
571 name##_cache0 |= NEG_USR32(name##_cache1,num);\
572 name##_cache1 <<= (num);
575 # define SKIP_COUNTER(name, gb, num)\
576 name##_bit_count += (num);\
578 # define SKIP_BITS(name, gb, num)\
580 SKIP_CACHE(name, gb, num)\
581 SKIP_COUNTER(name, gb, num)\
584 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
585 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
587 # define SHOW_UBITS(name, gb, num)\
588 NEG_USR32(name##_cache0, num)
590 # define SHOW_SBITS(name, gb, num)\
591 NEG_SSR32(name##_cache0, num)
593 # define GET_CACHE(name, gb)\
596 static inline int get_bits_count(GetBitContext
*s
){
597 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
602 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
606 tmp
= SHOW_UBITS(re
, s
, n
);
607 LAST_SKIP_BITS(re
, s
, n
)
612 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
616 tmp
= SHOW_UBITS(re
, s
, n
);
617 // CLOSE_READER(re, s)
621 static inline void skip_bits(GetBitContext
*s
, int n
){
622 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
625 LAST_SKIP_BITS(re
, s
, n
)
629 static inline unsigned int get_bits1(GetBitContext
*s
){
630 #ifdef ALT_BITSTREAM_READER
632 uint8_t result
= s
->buffer
[ index
>>3 ];
633 result
<<= (index
&0x07);
640 return get_bits(s
, 1);
644 static inline unsigned int show_bits1(GetBitContext
*s
){
645 return show_bits(s
, 1);
648 static inline void skip_bits1(GetBitContext
*s
){
652 void init_get_bits(GetBitContext
*s
,
653 UINT8
*buffer
, int buffer_size
);
655 int check_marker(GetBitContext
*s
, const char *msg
);
656 void align_get_bits(GetBitContext
*s
);
657 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
658 const void *bits
, int bits_wrap
, int bits_size
,
659 const void *codes
, int codes_wrap
, int codes_size
);
660 void free_vlc(VLC
*vlc
);
662 #define GET_VLC(code, name, gb, table, bits, max_depth)\
664 int n, index, nb_bits;\
666 index= SHOW_UBITS(name, gb, bits);\
667 code = table[index][0];\
668 n = table[index][1];\
670 if(max_depth > 1 && n < 0){\
671 LAST_SKIP_BITS(name, gb, bits)\
672 UPDATE_CACHE(name, gb)\
676 index= SHOW_UBITS(name, gb, nb_bits) + code;\
677 code = table[index][0];\
678 n = table[index][1];\
679 if(max_depth > 2 && n < 0){\
680 LAST_SKIP_BITS(name, gb, nb_bits)\
681 UPDATE_CACHE(name, gb)\
685 index= SHOW_UBITS(name, gb, nb_bits) + code;\
686 code = table[index][0];\
687 n = table[index][1];\
690 SKIP_BITS(name, gb, n)\
693 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
695 int n, index, nb_bits;\
697 index= SHOW_UBITS(name, gb, bits);\
698 level = table[index].level;\
699 n = table[index].len;\
701 if(max_depth > 1 && n < 0){\
702 LAST_SKIP_BITS(name, gb, bits)\
703 UPDATE_CACHE(name, gb)\
707 index= SHOW_UBITS(name, gb, nb_bits) + level;\
708 level = table[index].level;\
709 n = table[index].len;\
711 run= table[index].run;\
712 SKIP_BITS(name, gb, n)\
715 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
716 static inline int get_vlc(GetBitContext
*s
, VLC
*vlc
)
719 VLC_TYPE (*table
)[2]= vlc
->table
;
724 GET_VLC(code
, re
, s
, table
, vlc
->bits
, 3)
730 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
731 int bits
, int max_depth
)
738 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
745 /* define it to include statistics code (useful only for optimizing
762 extern int st_current_index
;
763 extern unsigned int st_bit_counts
[ST_NB
];
764 extern unsigned int st_out_bit_counts
[ST_NB
];
766 void print_stats(void);
769 /* misc math functions */
771 static inline int av_log2(unsigned int v
)
776 if (v
& 0xffff0000) {
799 static inline int mid_pred(int a
, int b
, int c
)
813 return a
+ b
+ c
- vmin
- vmax
;
816 static inline int clip(int a
, int amin
, int amax
)
827 extern const UINT8 ff_sqrt_tab
[128];
829 int ff_gcd(int a
, int b
);
831 static inline int ff_sqrt(int a
)
837 if(a
<128) return ff_sqrt_tab
[a
];
839 for(s
=15; s
>=0; s
--){
840 int b
= ret_sq
+ (1<<(s
*2)) + (ret
<<s
)*2;
850 * converts fourcc string to int
852 static inline int ff_get_fourcc(const char *s
){
853 assert( strlen(s
)==4 );
855 return (s
[0]) + (s
[1]<<8) + (s
[2]<<16) + (s
[3]<<24);
858 void ff_float2fraction(int *nom_arg
, int *denom_arg
, double f
, int max
);
862 #define MASK_ABS(mask, level)\
867 : "+a" (level), "=&d" (mask)\
870 #define MASK_ABS(mask, level)\
872 level= (level^mask)-mask;
876 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
877 #define COPY3_IF_LT(x,y,a,b,c,d)\
883 : "+r" (x), "+r" (a), "+r" (c)\
884 : "r" (y), "r" (b), "r" (d)\
887 #define COPY3_IF_LT(x,y,a,b,c,d)\
895 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
897 #endif /* HAVE_AV_CONFIG_H */
899 #endif /* COMMON_H */