3 * common internal api header.
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
13 //#define ALT_BITSTREAM_WRITER
14 //#define ALIGNED_BITSTREAM_WRITER
16 #define ALT_BITSTREAM_READER
17 //#define LIBMPEG2_BITSTREAM_READER
18 //#define A32_BITSTREAM_READER
19 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
21 #ifdef HAVE_AV_CONFIG_H
22 /* only include the following when compiling package */
41 #define M_PI 3.14159265358979323846
46 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
49 #define AVOPTION_CODEC_BOOL(name, help, field) \
50 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
51 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
53 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
55 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
57 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
59 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
60 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
61 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
62 #define AVOPTION_END() AVOPTION_SUB(NULL)
66 extern const struct AVOption avoptions_common
[3 + 5];
68 extern const struct AVOption avoptions_common
[3];
70 extern const struct AVOption avoptions_workaround_bug
[11];
72 #endif /* HAVE_AV_CONFIG_H */
74 /* Suppress restrict if it was not defined in config.h. */
79 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80 # define always_inline __attribute__((always_inline)) inline
82 # define always_inline inline
85 #ifdef EMULATE_FAST_INT
86 /* note that we don't emulate 64bit ints */
87 typedef signed char int_fast8_t;
88 typedef signed int int_fast16_t;
89 typedef signed int int_fast32_t;
90 typedef unsigned char uint_fast8_t;
91 typedef unsigned int uint_fast16_t;
92 typedef unsigned int uint_fast32_t;
95 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
96 static inline float floorf(float f
) {
105 typedef unsigned short uint16_t;
106 typedef signed short int16_t;
107 typedef unsigned char uint8_t;
108 typedef unsigned int uint32_t;
109 typedef unsigned __int64
uint64_t;
110 typedef signed char int8_t;
111 typedef signed int int32_t;
112 typedef signed __int64
int64_t;
115 # define int64_t_C(c) (c ## i64)
116 # define uint64_t_C(c) (c ## i64)
118 # ifdef HAVE_AV_CONFIG_H
119 # define inline __inline
123 # define int64_t_C(c) (c ## LL)
124 # define uint64_t_C(c) (c ## ULL)
125 # endif /* __MINGW32__ */
127 # ifdef HAVE_AV_CONFIG_H
132 # define snprintf _snprintf
133 # define vsnprintf _vsnprintf
136 /* CONFIG_WIN32 end */
137 #elif defined (CONFIG_OS2)
140 #include <inttypes.h>
143 #define int64_t_C(c) (c ## LL)
144 #define uint64_t_C(c) (c ## ULL)
147 #ifdef HAVE_AV_CONFIG_H
149 #ifdef USE_FASTMEMCPY
150 #include "fastmemcpy.h"
155 #endif /* HAVE_AV_CONFIG_H */
162 #include <inttypes.h>
165 #define int64_t_C(c) (c ## LL)
166 #define uint64_t_C(c) (c ## ULL)
169 #ifdef HAVE_AV_CONFIG_H
171 # ifdef USE_FASTMEMCPY
172 # include "fastmemcpy.h"
174 # endif /* HAVE_AV_CONFIG_H */
176 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
178 #ifdef HAVE_AV_CONFIG_H
182 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
183 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
184 # define MANGLE(a) "_" #a
186 # define MANGLE(a) #a
197 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
199 inline void dprintf(const char* fmt
,...) {}
204 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
206 # define dprintf(fmt,...)
209 # endif /* !CONFIG_WIN32 */
211 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
213 //rounded divison & shift
214 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
216 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
217 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
219 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
220 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
222 extern const uint32_t inverse
[256];
225 # define FASTDIV(a,b) \
230 :"=d"(ret),"=a"(dmy)\
231 :"1"(a),"g"(inverse[b])\
235 #elif defined(CONFIG_FASTDIV)
236 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
238 # define FASTDIV(a,b) ((a)/(b))
242 // avoid +32 for shift optimization (gcc should do that ...)
243 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
244 asm ("sarl %1, %0\n\t"
246 : "ic" ((uint8_t)(-s
))
250 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
251 asm ("shrl %1, %0\n\t"
253 : "ic" ((uint8_t)(-s
))
258 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
259 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
264 struct PutBitContext
;
266 typedef void (*WriteDataFunc
)(void *, uint8_t *, int);
268 typedef struct PutBitContext
{
269 #ifdef ALT_BITSTREAM_WRITER
270 uint8_t *buf
, *buf_end
;
275 uint8_t *buf
, *buf_ptr
, *buf_end
;
277 int64_t data_out_size
; /* in bytes */
280 void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
);
282 int64_t get_bit_count(PutBitContext
*s
); /* XXX: change function name */
283 void align_put_bits(PutBitContext
*s
);
284 void flush_put_bits(PutBitContext
*s
);
285 void put_string(PutBitContext
* pbc
, char *s
);
289 typedef struct GetBitContext
{
290 const uint8_t *buffer
, *buffer_end
;
291 #ifdef ALT_BITSTREAM_READER
293 #elif defined LIBMPEG2_BITSTREAM_READER
297 #elif defined A32_BITSTREAM_READER
298 uint32_t *buffer_ptr
;
306 static inline int get_bits_count(GetBitContext
*s
);
308 #define VLC_TYPE int16_t
312 VLC_TYPE (*table
)[2]; ///< code, bits
313 int table_size
, table_allocated
;
316 typedef struct RL_VLC_ELEM
{
323 #define UNALIGNED_STORES_ARE_BAD
326 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
328 # define unaligned32(a) (*(uint32_t*)(a))
331 static inline uint32_t unaligned32(const void *v
) {
334 } __attribute__((packed
));
336 return ((const struct Unaligned
*) v
)->i
;
338 # elif defined(__DECC)
339 static inline uint32_t unaligned32(const void *v
) {
340 return *(const __unaligned
uint32_t *) v
;
343 static inline uint32_t unaligned32(const void *v
) {
344 return *(const uint32_t *) v
;
349 #ifndef ALT_BITSTREAM_WRITER
350 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
352 unsigned int bit_buf
;
356 st_out_bit_counts
[st_current_index
] += n
;
358 // printf("put_bits=%d %x\n", n, value);
359 assert(n
== 32 || value
< (1U << n
));
361 bit_buf
= s
->bit_buf
;
362 bit_left
= s
->bit_left
;
364 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
367 bit_buf
= (bit_buf
<<n
) | value
;
371 bit_buf
|= value
>> (n
- bit_left
);
372 #ifdef UNALIGNED_STORES_ARE_BAD
373 if (3 & (int) s
->buf_ptr
) {
374 s
->buf_ptr
[0] = bit_buf
>> 24;
375 s
->buf_ptr
[1] = bit_buf
>> 16;
376 s
->buf_ptr
[2] = bit_buf
>> 8;
377 s
->buf_ptr
[3] = bit_buf
;
380 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
381 //printf("bitbuf = %08x\n", bit_buf);
387 s
->bit_buf
= bit_buf
;
388 s
->bit_left
= bit_left
;
393 #ifdef ALT_BITSTREAM_WRITER
394 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
396 # ifdef ALIGNED_BITSTREAM_WRITER
399 "movl %0, %%ecx \n\t"
400 "xorl %%eax, %%eax \n\t"
401 "shrdl %%cl, %1, %%eax \n\t"
403 "movl %0, %%ecx \n\t"
404 "shrl $3, %%ecx \n\t"
405 "andl $0xFFFFFFFC, %%ecx \n\t"
407 "orl %1, (%2, %%ecx) \n\t"
410 "movl %%eax, 4(%2, %%ecx) \n\t"
411 : "=&r" (s
->index
), "=&r" (value
)
412 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
417 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
421 ptr
[0] |= be2me_32(value
>>(index
&31));
422 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
423 //if(n>24) printf("%d %d\n", n, value);
427 # else //ALIGNED_BITSTREAM_WRITER
430 "movl $7, %%ecx \n\t"
431 "andl %0, %%ecx \n\t"
432 "addl %3, %%ecx \n\t"
436 "movl %0, %%ecx \n\t"
437 "shrl $3, %%ecx \n\t"
438 "orl %1, (%%ecx, %2) \n\t"
440 "movl $0, 4(%%ecx, %2) \n\t"
441 : "=&r" (s
->index
), "=&r" (value
)
442 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
447 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
449 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
451 //if(n>24) printf("%d %d\n", n, value);
455 # endif //!ALIGNED_BITSTREAM_WRITER
460 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
462 #ifdef ALT_BITSTREAM_WRITER
463 return s
->buf
+ (s
->index
>>3);
469 /* Bitstream reader API docs:
471 abritary name which is used as prefix for the internal variables
476 OPEN_READER(name, gb)
477 loads gb into local variables
479 CLOSE_READER(name, gb)
480 stores local vars in gb
482 UPDATE_CACHE(name, gb)
483 refills the internal cache from the bitstream
484 after this call at least MIN_CACHE_BITS will be available,
487 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
489 SHOW_UBITS(name, gb, num)
490 will return the nest num bits
492 SHOW_SBITS(name, gb, num)
493 will return the nest num bits and do sign extension
495 SKIP_BITS(name, gb, num)
496 will skip over the next num bits
497 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
499 SKIP_CACHE(name, gb, num)
500 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
502 SKIP_COUNTER(name, gb, num)
503 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
505 LAST_SKIP_CACHE(name, gb, num)
506 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
508 LAST_SKIP_BITS(name, gb, num)
509 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
511 for examples see get_bits, show_bits, skip_bits, get_vlc
514 static inline int unaligned32_be(const void *v
)
518 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
520 return be2me_32( unaligned32(v
)); //original
524 #ifdef ALT_BITSTREAM_READER
525 # define MIN_CACHE_BITS 25
527 # define OPEN_READER(name, gb)\
528 int name##_index= (gb)->index;\
529 int name##_cache= 0;\
531 # define CLOSE_READER(name, gb)\
532 (gb)->index= name##_index;\
534 # define UPDATE_CACHE(name, gb)\
535 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
537 # define SKIP_CACHE(name, gb, num)\
538 name##_cache <<= (num);\
541 # define SKIP_COUNTER(name, gb, num)\
542 name##_index += (num);\
544 # define SKIP_BITS(name, gb, num)\
546 SKIP_CACHE(name, gb, num)\
547 SKIP_COUNTER(name, gb, num)\
550 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
551 # define LAST_SKIP_CACHE(name, gb, num) ;
553 # define SHOW_UBITS(name, gb, num)\
554 NEG_USR32(name##_cache, num)
556 # define SHOW_SBITS(name, gb, num)\
557 NEG_SSR32(name##_cache, num)
559 # define GET_CACHE(name, gb)\
560 ((uint32_t)name##_cache)
562 static inline int get_bits_count(GetBitContext
*s
){
565 #elif defined LIBMPEG2_BITSTREAM_READER
566 //libmpeg2 like reader
568 # define MIN_CACHE_BITS 17
570 # define OPEN_READER(name, gb)\
571 int name##_bit_count=(gb)->bit_count;\
572 int name##_cache= (gb)->cache;\
573 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
575 # define CLOSE_READER(name, gb)\
576 (gb)->bit_count= name##_bit_count;\
577 (gb)->cache= name##_cache;\
578 (gb)->buffer_ptr= name##_buffer_ptr;\
580 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
582 # define UPDATE_CACHE(name, gb)\
583 if(name##_bit_count >= 0){\
584 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
585 ((uint16_t*)name##_buffer_ptr)++;\
586 name##_bit_count-= 16;\
591 # define UPDATE_CACHE(name, gb)\
592 if(name##_bit_count >= 0){\
593 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
594 name##_buffer_ptr+=2;\
595 name##_bit_count-= 16;\
600 # define SKIP_CACHE(name, gb, num)\
601 name##_cache <<= (num);\
603 # define SKIP_COUNTER(name, gb, num)\
604 name##_bit_count += (num);\
606 # define SKIP_BITS(name, gb, num)\
608 SKIP_CACHE(name, gb, num)\
609 SKIP_COUNTER(name, gb, num)\
612 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
613 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
615 # define SHOW_UBITS(name, gb, num)\
616 NEG_USR32(name##_cache, num)
618 # define SHOW_SBITS(name, gb, num)\
619 NEG_SSR32(name##_cache, num)
621 # define GET_CACHE(name, gb)\
622 ((uint32_t)name##_cache)
624 static inline int get_bits_count(GetBitContext
*s
){
625 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
628 #elif defined A32_BITSTREAM_READER
630 # define MIN_CACHE_BITS 32
632 # define OPEN_READER(name, gb)\
633 int name##_bit_count=(gb)->bit_count;\
634 uint32_t name##_cache0= (gb)->cache0;\
635 uint32_t name##_cache1= (gb)->cache1;\
636 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
638 # define CLOSE_READER(name, gb)\
639 (gb)->bit_count= name##_bit_count;\
640 (gb)->cache0= name##_cache0;\
641 (gb)->cache1= name##_cache1;\
642 (gb)->buffer_ptr= name##_buffer_ptr;\
644 # define UPDATE_CACHE(name, gb)\
645 if(name##_bit_count > 0){\
646 const uint32_t next= be2me_32( *name##_buffer_ptr );\
647 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
648 name##_cache1 |= next<<name##_bit_count;\
649 name##_buffer_ptr++;\
650 name##_bit_count-= 32;\
654 # define SKIP_CACHE(name, gb, num)\
656 "shldl %2, %1, %0 \n\t"\
658 : "+r" (name##_cache0), "+r" (name##_cache1)\
659 : "Ic" ((uint8_t)num)\
662 # define SKIP_CACHE(name, gb, num)\
663 name##_cache0 <<= (num);\
664 name##_cache0 |= NEG_USR32(name##_cache1,num);\
665 name##_cache1 <<= (num);
668 # define SKIP_COUNTER(name, gb, num)\
669 name##_bit_count += (num);\
671 # define SKIP_BITS(name, gb, num)\
673 SKIP_CACHE(name, gb, num)\
674 SKIP_COUNTER(name, gb, num)\
677 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
678 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
680 # define SHOW_UBITS(name, gb, num)\
681 NEG_USR32(name##_cache0, num)
683 # define SHOW_SBITS(name, gb, num)\
684 NEG_SSR32(name##_cache0, num)
686 # define GET_CACHE(name, gb)\
689 static inline int get_bits_count(GetBitContext
*s
){
690 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
696 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
697 * if MSB not set it is negative
698 * @param n length in bits
701 static inline int get_xbits(GetBitContext
*s
, int n
){
703 register int32_t cache
;
706 cache
= GET_CACHE(re
,s
);
707 if ((int32_t)cache
<0) { //MSB=1
708 tmp
= NEG_USR32(cache
,n
);
710 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
711 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
712 tmp
= - NEG_USR32(~cache
,n
);
714 LAST_SKIP_BITS(re
, s
, n
)
719 static inline int get_sbits(GetBitContext
*s
, int n
){
723 tmp
= SHOW_SBITS(re
, s
, n
);
724 LAST_SKIP_BITS(re
, s
, n
)
731 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
733 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
737 tmp
= SHOW_UBITS(re
, s
, n
);
738 LAST_SKIP_BITS(re
, s
, n
)
743 unsigned int get_bits_long(GetBitContext
*s
, int n
);
747 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
749 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
753 tmp
= SHOW_UBITS(re
, s
, n
);
754 // CLOSE_READER(re, s)
758 unsigned int show_bits_long(GetBitContext
*s
, int n
);
760 static inline void skip_bits(GetBitContext
*s
, int n
){
761 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
764 LAST_SKIP_BITS(re
, s
, n
)
768 static inline unsigned int get_bits1(GetBitContext
*s
){
769 #ifdef ALT_BITSTREAM_READER
771 uint8_t result
= s
->buffer
[ index
>>3 ];
772 result
<<= (index
&0x07);
779 return get_bits(s
, 1);
783 static inline unsigned int show_bits1(GetBitContext
*s
){
784 return show_bits(s
, 1);
787 static inline void skip_bits1(GetBitContext
*s
){
791 void init_get_bits(GetBitContext
*s
,
792 const uint8_t *buffer
, int buffer_size
);
794 int check_marker(GetBitContext
*s
, const char *msg
);
795 void align_get_bits(GetBitContext
*s
);
796 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
797 const void *bits
, int bits_wrap
, int bits_size
,
798 const void *codes
, int codes_wrap
, int codes_size
);
799 void free_vlc(VLC
*vlc
);
803 * if the vlc code is invalid and max_depth=1 than no bits will be removed
804 * if the vlc code is invalid and max_depth>1 than the number of bits removed
807 #define GET_VLC(code, name, gb, table, bits, max_depth)\
809 int n, index, nb_bits;\
811 index= SHOW_UBITS(name, gb, bits);\
812 code = table[index][0];\
813 n = table[index][1];\
815 if(max_depth > 1 && n < 0){\
816 LAST_SKIP_BITS(name, gb, bits)\
817 UPDATE_CACHE(name, gb)\
821 index= SHOW_UBITS(name, gb, nb_bits) + code;\
822 code = table[index][0];\
823 n = table[index][1];\
824 if(max_depth > 2 && n < 0){\
825 LAST_SKIP_BITS(name, gb, nb_bits)\
826 UPDATE_CACHE(name, gb)\
830 index= SHOW_UBITS(name, gb, nb_bits) + code;\
831 code = table[index][0];\
832 n = table[index][1];\
835 SKIP_BITS(name, gb, n)\
838 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
840 int n, index, nb_bits;\
842 index= SHOW_UBITS(name, gb, bits);\
843 level = table[index].level;\
844 n = table[index].len;\
846 if(max_depth > 1 && n < 0){\
847 LAST_SKIP_BITS(name, gb, bits)\
848 UPDATE_CACHE(name, gb)\
852 index= SHOW_UBITS(name, gb, nb_bits) + level;\
853 level = table[index].level;\
854 n = table[index].len;\
856 run= table[index].run;\
857 SKIP_BITS(name, gb, n)\
860 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
861 static inline int get_vlc(GetBitContext
*s
, VLC
*vlc
)
864 VLC_TYPE (*table
)[2]= vlc
->table
;
869 GET_VLC(code
, re
, s
, table
, vlc
->bits
, 3)
876 * parses a vlc code, faster then get_vlc()
877 * @param bits is the number of bits which will be read at once, must be
878 * identical to nb_bits in init_vlc()
879 * @param max_depth is the number of times bits bits must be readed to completly
880 * read the longest vlc code
881 * = (max_vlc_length + bits - 1) / bits
883 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
884 int bits
, int max_depth
)
891 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
901 static inline void print_bin(int bits
, int n
){
904 for(i
=n
-1; i
>=0; i
--){
905 printf("%d", (bits
>>i
)&1);
911 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
912 int r
= get_bits(s
, n
);
915 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
918 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, char *func
, int line
){
919 int show
= show_bits(s
, 24);
920 int pos
= get_bits_count(s
);
921 int r
= get_vlc2(s
, table
, bits
, max_depth
);
922 int len
= get_bits_count(s
) - pos
;
923 int bits2
= show
>>(24-len
);
925 print_bin(bits2
, len
);
927 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
930 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
931 int show
= show_bits(s
, n
);
932 int r
= get_xbits(s
, n
);
935 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
939 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
940 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
941 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
942 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
943 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
945 #define tprintf printf
948 #define tprintf(_arg...) {}
951 /* define it to include statistics code (useful only for optimizing
968 extern int st_current_index
;
969 extern unsigned int st_bit_counts
[ST_NB
];
970 extern unsigned int st_out_bit_counts
[ST_NB
];
972 void print_stats(void);
975 /* misc math functions */
976 extern const uint8_t ff_log2_tab
[256];
978 static inline int av_log2(unsigned int v
)
983 if (v
& 0xffff0000) {
996 static inline int av_log2_16bit(unsigned int v
)
1005 n
+= ff_log2_tab
[v
];
1012 static inline int mid_pred(int a
, int b
, int c
)
1026 return a
+ b
+ c
- vmin
- vmax
;
1029 static inline int clip(int a
, int amin
, int amax
)
1040 extern const uint8_t ff_sqrt_tab
[128];
1042 int64_t ff_gcd(int64_t a
, int64_t b
);
1044 static inline int ff_sqrt(int a
)
1050 if(a
<128) return ff_sqrt_tab
[a
];
1052 for(s
=15; s
>=0; s
--){
1053 int b
= ret_sq
+ (1<<(s
*2)) + (ret
<<s
)*2;
1063 * converts fourcc string to int
1065 static inline int ff_get_fourcc(const char *s
){
1066 assert( strlen(s
)==4 );
1068 return (s
[0]) + (s
[1]<<8) + (s
[2]<<16) + (s
[3]<<24);
1071 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1072 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1076 #define MASK_ABS(mask, level)\
1081 : "+a" (level), "=&d" (mask)\
1084 #define MASK_ABS(mask, level)\
1086 level= (level^mask)-mask;
1090 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1091 #define COPY3_IF_LT(x,y,a,b,c,d)\
1094 "cmovl %3, %0 \n\t"\
1095 "cmovl %4, %1 \n\t"\
1096 "cmovl %5, %2 \n\t"\
1097 : "+r" (x), "+r" (a), "+r" (c)\
1098 : "r" (y), "r" (b), "r" (d)\
1101 #define COPY3_IF_LT(x,y,a,b,c,d)\
1110 static inline long long rdtsc()
1113 asm volatile( "rdtsc\n\t"
1119 #define START_TIMER \
1120 static uint64_t tsum=0;\
1121 static int tcount=0;\
1122 static int tskip_count=0;\
1124 uint64_t tstart= rdtsc();\
1126 #define STOP_TIMER(id) \
1128 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1129 tsum+= tend - tstart;\
1133 if(256*256*256*64%(tcount+tskip_count)==0){\
1134 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1138 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1140 /* avoid usage of various functions */
1141 #define malloc please_use_av_malloc
1142 #define free please_use_av_free
1143 #define realloc please_use_av_realloc
1145 #define CHECKED_ALLOCZ(p, size)\
1147 p= av_mallocz(size);\
1148 if(p==NULL && (size)!=0){\
1154 #endif /* HAVE_AV_CONFIG_H */
1156 #endif /* COMMON_H */