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 */
36 #endif /* HAVE_AV_CONFIG_H */
38 /* Suppress restrict if it was not defined in config.h. */
43 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
44 # define always_inline __attribute__((always_inline)) inline
46 # define always_inline inline
53 typedef unsigned short UINT16
;
54 typedef signed short INT16
;
55 typedef unsigned char UINT8
;
56 typedef unsigned int UINT32
;
57 typedef unsigned __int64 UINT64
;
58 typedef signed char INT8
;
59 typedef signed int INT32
;
60 typedef signed __int64 INT64
;
62 typedef UINT8
uint8_t;
64 typedef UINT16
uint16_t;
65 typedef INT16
int16_t;
66 typedef UINT32
uint32_t;
67 typedef INT32
int32_t;
68 typedef UINT64
uint64_t;
69 typedef INT64
int64_t;
72 # define INT64_C(c) (c ## i64)
73 # define UINT64_C(c) (c ## i64)
75 # define inline __inline
78 # define INT64_C(c) (c ## LL)
79 # define UINT64_C(c) (c ## ULL)
80 # endif /* __MINGW32__ */
82 # define M_PI 3.14159265358979323846
83 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
89 # define snprintf _snprintf
91 #else /* CONFIG_WIN32 */
95 # include <inttypes.h>
97 # ifndef __WINE_WINDEF16_H
98 /* workaround for typedef conflict in MPlayer (wine typedefs) */
99 typedef unsigned short UINT16
;
100 typedef signed short INT16
;
103 typedef unsigned char UINT8
;
104 typedef unsigned int UINT32
;
105 typedef unsigned long long UINT64
;
106 typedef signed char INT8
;
107 typedef signed int INT32
;
108 typedef signed long long INT64
;
110 # ifdef HAVE_AV_CONFIG_H
112 # define INT64_C(c) (c ## LL)
113 # define UINT64_C(c) (c ## ULL)
116 # ifdef USE_FASTMEMCPY
117 # 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
131 # 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)
158 //rounded divison & shift
159 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
161 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
162 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
164 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
165 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
168 // avoid +32 for shift optimization (gcc should do that ...)
169 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
170 asm ("sarl %1, %0\n\t"
172 : "ic" ((uint8_t)(-s
))
176 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
177 asm ("shrl %1, %0\n\t"
179 : "ic" ((uint8_t)(-s
))
184 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
185 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
190 struct PutBitContext
;
192 typedef void (*WriteDataFunc
)(void *, UINT8
*, int);
194 typedef struct PutBitContext
{
195 #ifdef ALT_BITSTREAM_WRITER
196 UINT8
*buf
, *buf_end
;
201 UINT8
*buf
, *buf_ptr
, *buf_end
;
203 INT64 data_out_size
; /* in bytes */
206 void init_put_bits(PutBitContext
*s
,
207 UINT8
*buffer
, int buffer_size
,
209 void (*write_data
)(void *, UINT8
*, int));
211 INT64
get_bit_count(PutBitContext
*s
); /* XXX: change function name */
212 void align_put_bits(PutBitContext
*s
);
213 void flush_put_bits(PutBitContext
*s
);
214 void put_string(PutBitContext
* pbc
, char *s
);
218 typedef struct GetBitContext
{
219 UINT8
*buffer
, *buffer_end
;
220 #ifdef ALT_BITSTREAM_READER
222 #elif defined LIBMPEG2_BITSTREAM_READER
226 #elif defined A32_BITSTREAM_READER
235 static inline int get_bits_count(GetBitContext
*s
);
237 #define VLC_TYPE INT16
241 VLC_TYPE (*table
)[2]; // code, bits
242 int table_size
, table_allocated
;
245 typedef struct RL_VLC_ELEM
{
252 #define UNALIGNED_STORES_ARE_BAD
255 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
257 # define unaligned32(a) (*(UINT32*)(a))
260 static inline uint32_t unaligned32(const void *v
) {
263 } __attribute__((packed
));
265 return ((const struct Unaligned
*) v
)->i
;
267 # elif defined(__DECC)
268 static inline uint32_t unaligned32(const void *v
) {
269 return *(const __unaligned
uint32_t *) v
;
272 static inline uint32_t unaligned32(const void *v
) {
273 return *(const uint32_t *) v
;
278 #ifndef ALT_BITSTREAM_WRITER
279 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
281 unsigned int bit_buf
;
285 st_out_bit_counts
[st_current_index
] += n
;
287 // printf("put_bits=%d %x\n", n, value);
288 assert(n
== 32 || value
< (1U << n
));
290 bit_buf
= s
->bit_buf
;
291 bit_left
= s
->bit_left
;
293 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
296 bit_buf
= (bit_buf
<<n
) | value
;
300 bit_buf
|= value
>> (n
- bit_left
);
301 #ifdef UNALIGNED_STORES_ARE_BAD
302 if (3 & (int) s
->buf_ptr
) {
303 s
->buf_ptr
[0] = bit_buf
>> 24;
304 s
->buf_ptr
[1] = bit_buf
>> 16;
305 s
->buf_ptr
[2] = bit_buf
>> 8;
306 s
->buf_ptr
[3] = bit_buf
;
309 *(UINT32
*)s
->buf_ptr
= be2me_32(bit_buf
);
310 //printf("bitbuf = %08x\n", bit_buf);
316 s
->bit_buf
= bit_buf
;
317 s
->bit_left
= bit_left
;
322 #ifdef ALT_BITSTREAM_WRITER
323 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
325 # ifdef ALIGNED_BITSTREAM_WRITER
328 "movl %0, %%ecx \n\t"
329 "xorl %%eax, %%eax \n\t"
330 "shrdl %%cl, %1, %%eax \n\t"
332 "movl %0, %%ecx \n\t"
333 "shrl $3, %%ecx \n\t"
334 "andl $0xFFFFFFFC, %%ecx \n\t"
336 "orl %1, (%2, %%ecx) \n\t"
339 "movl %%eax, 4(%2, %%ecx) \n\t"
340 : "=&r" (s
->index
), "=&r" (value
)
341 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
346 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
350 ptr
[0] |= be2me_32(value
>>(index
&31));
351 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
352 //if(n>24) printf("%d %d\n", n, value);
356 # else //ALIGNED_BITSTREAM_WRITER
359 "movl $7, %%ecx \n\t"
360 "andl %0, %%ecx \n\t"
361 "addl %3, %%ecx \n\t"
365 "movl %0, %%ecx \n\t"
366 "shrl $3, %%ecx \n\t"
367 "orl %1, (%%ecx, %2) \n\t"
369 "movl $0, 4(%%ecx, %2) \n\t"
370 : "=&r" (s
->index
), "=&r" (value
)
371 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
376 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
378 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
380 //if(n>24) printf("%d %d\n", n, value);
384 # endif //!ALIGNED_BITSTREAM_WRITER
389 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
391 #ifdef ALT_BITSTREAM_WRITER
392 return s
->buf
+ (s
->index
>>3);
398 /* Bitstream reader API docs:
400 abritary name which is used as prefix for the internal variables
405 OPEN_READER(name, gb)
406 loads gb into local variables
408 CLOSE_READER(name, gb)
409 stores local vars in gb
411 UPDATE_CACHE(name, gb)
412 refills the internal cache from the bitstream
413 after this call at least MIN_CACHE_BITS will be available,
416 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
418 SHOW_UBITS(name, gb, num)
419 will return the nest num bits
421 SHOW_SBITS(name, gb, num)
422 will return the nest num bits and do sign extension
424 SKIP_BITS(name, gb, num)
425 will skip over the next num bits
426 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
428 SKIP_CACHE(name, gb, num)
429 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
431 SKIP_COUNTER(name, gb, num)
432 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
434 LAST_SKIP_CACHE(name, gb, num)
435 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
437 LAST_SKIP_BITS(name, gb, num)
438 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
440 for examples see get_bits, show_bits, skip_bits, get_vlc
443 #ifdef ALT_BITSTREAM_READER
444 # define MIN_CACHE_BITS 25
446 # define OPEN_READER(name, gb)\
447 int name##_index= (gb)->index;\
448 int name##_cache= 0;\
450 # define CLOSE_READER(name, gb)\
451 (gb)->index= name##_index;\
453 # define UPDATE_CACHE(name, gb)\
454 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
456 # define SKIP_CACHE(name, gb, num)\
457 name##_cache <<= (num);\
460 # define SKIP_COUNTER(name, gb, num)\
461 name##_index += (num);\
463 # define SKIP_BITS(name, gb, num)\
465 SKIP_CACHE(name, gb, num)\
466 SKIP_COUNTER(name, gb, num)\
469 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
470 # define LAST_SKIP_CACHE(name, gb, num) ;
472 # define SHOW_UBITS(name, gb, num)\
473 NEG_USR32(name##_cache, num)
475 # define SHOW_SBITS(name, gb, num)\
476 NEG_SSR32(name##_cache, num)
478 # define GET_CACHE(name, gb)\
479 ((uint32_t)name##_cache)
481 static inline int get_bits_count(GetBitContext
*s
){
484 #elif defined LIBMPEG2_BITSTREAM_READER
485 //libmpeg2 like reader
487 # define MIN_CACHE_BITS 16
489 # define OPEN_READER(name, gb)\
490 int name##_bit_count=(gb)->bit_count;\
491 int name##_cache= (gb)->cache;\
492 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
494 # define CLOSE_READER(name, gb)\
495 (gb)->bit_count= name##_bit_count;\
496 (gb)->cache= name##_cache;\
497 (gb)->buffer_ptr= name##_buffer_ptr;\
499 # define UPDATE_CACHE(name, gb)\
500 if(name##_bit_count > 0){\
501 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
502 name##_buffer_ptr+=2;\
503 name##_bit_count-= 16;\
506 # define SKIP_CACHE(name, gb, num)\
507 name##_cache <<= (num);\
509 # define SKIP_COUNTER(name, gb, num)\
510 name##_bit_count += (num);\
512 # define SKIP_BITS(name, gb, num)\
514 SKIP_CACHE(name, gb, num)\
515 SKIP_COUNTER(name, gb, num)\
518 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
519 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
521 # define SHOW_UBITS(name, gb, num)\
522 NEG_USR32(name##_cache, num)
524 # define SHOW_SBITS(name, gb, num)\
525 NEG_SSR32(name##_cache, num)
527 # define GET_CACHE(name, gb)\
528 ((uint32_t)name##_cache)
530 static inline int get_bits_count(GetBitContext
*s
){
531 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
534 #elif defined A32_BITSTREAM_READER
536 # define MIN_CACHE_BITS 32
538 # define OPEN_READER(name, gb)\
539 int name##_bit_count=(gb)->bit_count;\
540 uint32_t name##_cache0= (gb)->cache0;\
541 uint32_t name##_cache1= (gb)->cache1;\
542 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
544 # define CLOSE_READER(name, gb)\
545 (gb)->bit_count= name##_bit_count;\
546 (gb)->cache0= name##_cache0;\
547 (gb)->cache1= name##_cache1;\
548 (gb)->buffer_ptr= name##_buffer_ptr;\
550 # define UPDATE_CACHE(name, gb)\
551 if(name##_bit_count > 0){\
552 const uint32_t next= be2me_32( *name##_buffer_ptr );\
553 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
554 name##_cache1 |= next<<name##_bit_count;\
555 name##_buffer_ptr++;\
556 name##_bit_count-= 32;\
560 # define SKIP_CACHE(name, gb, num)\
562 "shldl %2, %1, %0 \n\t"\
564 : "+r" (name##_cache0), "+r" (name##_cache1)\
565 : "Ic" ((uint8_t)num)\
568 # define SKIP_CACHE(name, gb, num)\
569 name##_cache0 <<= (num);\
570 name##_cache0 |= NEG_USR32(name##_cache1,num);\
571 name##_cache1 <<= (num);
574 # define SKIP_COUNTER(name, gb, num)\
575 name##_bit_count += (num);\
577 # define SKIP_BITS(name, gb, num)\
579 SKIP_CACHE(name, gb, num)\
580 SKIP_COUNTER(name, gb, num)\
583 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
584 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
586 # define SHOW_UBITS(name, gb, num)\
587 NEG_USR32(name##_cache0, num)
589 # define SHOW_SBITS(name, gb, num)\
590 NEG_SSR32(name##_cache0, num)
592 # define GET_CACHE(name, gb)\
595 static inline int get_bits_count(GetBitContext
*s
){
596 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
601 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
605 tmp
= SHOW_UBITS(re
, s
, n
);
606 LAST_SKIP_BITS(re
, s
, n
)
611 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
615 tmp
= SHOW_UBITS(re
, s
, n
);
616 // CLOSE_READER(re, s)
620 static inline void skip_bits(GetBitContext
*s
, int n
){
621 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
624 LAST_SKIP_BITS(re
, s
, n
)
628 static inline unsigned int get_bits1(GetBitContext
*s
){
629 #ifdef ALT_BITSTREAM_READER
631 uint8_t result
= s
->buffer
[ index
>>3 ];
632 result
<<= (index
&0x07);
639 return get_bits(s
, 1);
643 static inline unsigned int show_bits1(GetBitContext
*s
){
644 return show_bits(s
, 1);
647 static inline void skip_bits1(GetBitContext
*s
){
651 void init_get_bits(GetBitContext
*s
,
652 UINT8
*buffer
, int buffer_size
);
654 int check_marker(GetBitContext
*s
, const 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 #define GET_VLC(code, name, gb, table, bits, max_depth)\
663 int n, index, nb_bits;\
665 index= SHOW_UBITS(name, gb, bits);\
666 code = table[index][0];\
667 n = table[index][1];\
669 if(max_depth > 1 && n < 0){\
670 LAST_SKIP_BITS(name, gb, bits)\
671 UPDATE_CACHE(name, gb)\
675 index= SHOW_UBITS(name, gb, nb_bits) + code;\
676 code = table[index][0];\
677 n = table[index][1];\
678 if(max_depth > 2 && n < 0){\
679 LAST_SKIP_BITS(name, gb, nb_bits)\
680 UPDATE_CACHE(name, gb)\
684 index= SHOW_UBITS(name, gb, nb_bits) + code;\
685 code = table[index][0];\
686 n = table[index][1];\
689 SKIP_BITS(name, gb, n)\
692 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
694 int n, index, nb_bits;\
696 index= SHOW_UBITS(name, gb, bits);\
697 level = table[index].level;\
698 n = table[index].len;\
700 if(max_depth > 1 && n < 0){\
701 LAST_SKIP_BITS(name, gb, bits)\
702 UPDATE_CACHE(name, gb)\
706 index= SHOW_UBITS(name, gb, nb_bits) + level;\
707 level = table[index].level;\
708 n = table[index].len;\
710 run= table[index].run;\
711 SKIP_BITS(name, gb, n)\
714 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
715 static inline int get_vlc(GetBitContext
*s
, VLC
*vlc
)
718 VLC_TYPE (*table
)[2]= vlc
->table
;
723 GET_VLC(code
, re
, s
, table
, vlc
->bits
, 3)
729 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
730 int bits
, int max_depth
)
737 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
744 /* define it to include statistics code (useful only for optimizing
761 extern int st_current_index
;
762 extern unsigned int st_bit_counts
[ST_NB
];
763 extern unsigned int st_out_bit_counts
[ST_NB
];
765 void print_stats(void);
768 /* misc math functions */
770 static inline int av_log2(unsigned int v
)
775 if (v
& 0xffff0000) {
798 static inline int mid_pred(int a
, int b
, int c
)
812 return a
+ b
+ c
- vmin
- vmax
;
815 static inline int clip(int a
, int amin
, int amax
)
826 extern const UINT8 ff_sqrt_tab
[128];
828 int ff_gcd(int a
, int b
);
830 static inline int ff_sqrt(int a
)
836 if(a
<128) return ff_sqrt_tab
[a
];
838 for(s
=15; s
>=0; s
--){
839 int b
= ret_sq
+ (1<<(s
*2)) + (ret
<<s
)*2;
849 * converts fourcc string to int
851 static inline int ff_get_fourcc(const char *s
){
852 assert( strlen(s
)==4 );
854 return (s
[0]) + (s
[1]<<8) + (s
[2]<<16) + (s
[3]<<24);
857 void ff_float2fraction(int *nom_arg
, int *denom_arg
, double f
, int max
);
861 #define MASK_ABS(mask, level)\
866 : "+a" (level), "=&d" (mask)\
869 #define MASK_ABS(mask, level)\
871 level= (level^mask)-mask;
875 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
876 #define COPY3_IF_LT(x,y,a,b,c,d)\
882 : "+r" (x), "+r" (a), "+r" (c)\
883 : "r" (y), "r" (b), "r" (d)\
886 #define COPY3_IF_LT(x,y,a,b,c,d)\
894 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
896 #endif /* HAVE_AV_CONFIG_H */
898 #endif /* COMMON_H */