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 #ifndef EMULATE_INTTYPES
86 # include <inttypes.h>
88 typedef signed char int8_t;
89 typedef signed short int16_t;
90 typedef signed int int32_t;
91 typedef unsigned char uint8_t;
92 typedef unsigned short uint16_t;
93 typedef unsigned int uint32_t;
96 typedef signed __int64
int64_t;
97 typedef unsigned __int64
uint64_t;
99 typedef signed long long int64_t;
100 typedef unsigned long long uint64_t;
101 # endif /* other OS */
102 #endif /* HAVE_INTTYPES_H */
104 #ifdef EMULATE_FAST_INT
105 /* note that we don't emulate 64bit ints */
106 typedef signed char int_fast8_t;
107 typedef signed int int_fast16_t;
108 typedef signed int int_fast32_t;
109 typedef unsigned char uint_fast8_t;
110 typedef unsigned int uint_fast16_t;
111 typedef unsigned int uint_fast32_t;
114 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
115 static inline float floorf(float f
) {
125 # define int64_t_C(c) (c ## i64)
126 # define uint64_t_C(c) (c ## i64)
128 # ifdef HAVE_AV_CONFIG_H
129 # define inline __inline
133 # define int64_t_C(c) (c ## LL)
134 # define uint64_t_C(c) (c ## ULL)
135 # endif /* __MINGW32__ */
137 # ifdef HAVE_AV_CONFIG_H
142 # define snprintf _snprintf
143 # define vsnprintf _vsnprintf
146 /* CONFIG_WIN32 end */
147 #elif defined (CONFIG_OS2)
151 #define int64_t_C(c) (c ## LL)
152 #define uint64_t_C(c) (c ## ULL)
155 #ifdef HAVE_AV_CONFIG_H
157 #ifdef USE_FASTMEMCPY
158 #include "fastmemcpy.h"
163 #endif /* HAVE_AV_CONFIG_H */
171 #define int64_t_C(c) (c ## LL)
172 #define uint64_t_C(c) (c ## ULL)
175 #ifdef HAVE_AV_CONFIG_H
177 # ifdef USE_FASTMEMCPY
178 # include "fastmemcpy.h"
180 # endif /* HAVE_AV_CONFIG_H */
182 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
184 #ifdef HAVE_AV_CONFIG_H
188 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
189 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
190 # define MANGLE(a) "_" #a
192 # define MANGLE(a) #a
203 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
205 inline void dprintf(const char* fmt
,...) {}
210 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
212 # define dprintf(fmt,...)
215 # endif /* !CONFIG_WIN32 */
217 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
219 //rounded divison & shift
220 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
222 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
223 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
225 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
226 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
228 extern const uint32_t inverse
[256];
231 # define FASTDIV(a,b) \
236 :"=d"(ret),"=a"(dmy)\
237 :"1"(a),"g"(inverse[b])\
241 #elif defined(CONFIG_FASTDIV)
242 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
244 # define FASTDIV(a,b) ((a)/(b))
248 // avoid +32 for shift optimization (gcc should do that ...)
249 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
250 asm ("sarl %1, %0\n\t"
252 : "ic" ((uint8_t)(-s
))
256 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
257 asm ("shrl %1, %0\n\t"
259 : "ic" ((uint8_t)(-s
))
264 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
265 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
270 struct PutBitContext
;
272 typedef void (*WriteDataFunc
)(void *, uint8_t *, int);
274 typedef struct PutBitContext
{
275 #ifdef ALT_BITSTREAM_WRITER
276 uint8_t *buf
, *buf_end
;
281 uint8_t *buf
, *buf_ptr
, *buf_end
;
285 void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
);
287 int get_bit_count(PutBitContext
*s
); /* XXX: change function name */
288 void align_put_bits(PutBitContext
*s
);
289 void flush_put_bits(PutBitContext
*s
);
290 void put_string(PutBitContext
* pbc
, char *s
);
294 typedef struct GetBitContext
{
295 const uint8_t *buffer
, *buffer_end
;
296 #ifdef ALT_BITSTREAM_READER
298 #elif defined LIBMPEG2_BITSTREAM_READER
302 #elif defined A32_BITSTREAM_READER
303 uint32_t *buffer_ptr
;
311 static inline int get_bits_count(GetBitContext
*s
);
313 #define VLC_TYPE int16_t
317 VLC_TYPE (*table
)[2]; ///< code, bits
318 int table_size
, table_allocated
;
321 typedef struct RL_VLC_ELEM
{
328 #define UNALIGNED_STORES_ARE_BAD
331 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
333 # define unaligned32(a) (*(uint32_t*)(a))
336 static inline uint32_t unaligned32(const void *v
) {
339 } __attribute__((packed
));
341 return ((const struct Unaligned
*) v
)->i
;
343 # elif defined(__DECC)
344 static inline uint32_t unaligned32(const void *v
) {
345 return *(const __unaligned
uint32_t *) v
;
348 static inline uint32_t unaligned32(const void *v
) {
349 return *(const uint32_t *) v
;
354 #ifndef ALT_BITSTREAM_WRITER
355 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
357 unsigned int bit_buf
;
361 st_out_bit_counts
[st_current_index
] += n
;
363 // printf("put_bits=%d %x\n", n, value);
364 assert(n
== 32 || value
< (1U << n
));
366 bit_buf
= s
->bit_buf
;
367 bit_left
= s
->bit_left
;
369 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
372 bit_buf
= (bit_buf
<<n
) | value
;
376 bit_buf
|= value
>> (n
- bit_left
);
377 #ifdef UNALIGNED_STORES_ARE_BAD
378 if (3 & (int) s
->buf_ptr
) {
379 s
->buf_ptr
[0] = bit_buf
>> 24;
380 s
->buf_ptr
[1] = bit_buf
>> 16;
381 s
->buf_ptr
[2] = bit_buf
>> 8;
382 s
->buf_ptr
[3] = bit_buf
;
385 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
386 //printf("bitbuf = %08x\n", bit_buf);
392 s
->bit_buf
= bit_buf
;
393 s
->bit_left
= bit_left
;
398 #ifdef ALT_BITSTREAM_WRITER
399 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
401 # ifdef ALIGNED_BITSTREAM_WRITER
404 "movl %0, %%ecx \n\t"
405 "xorl %%eax, %%eax \n\t"
406 "shrdl %%cl, %1, %%eax \n\t"
408 "movl %0, %%ecx \n\t"
409 "shrl $3, %%ecx \n\t"
410 "andl $0xFFFFFFFC, %%ecx \n\t"
412 "orl %1, (%2, %%ecx) \n\t"
415 "movl %%eax, 4(%2, %%ecx) \n\t"
416 : "=&r" (s
->index
), "=&r" (value
)
417 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
422 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
426 ptr
[0] |= be2me_32(value
>>(index
&31));
427 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
428 //if(n>24) printf("%d %d\n", n, value);
432 # else //ALIGNED_BITSTREAM_WRITER
435 "movl $7, %%ecx \n\t"
436 "andl %0, %%ecx \n\t"
437 "addl %3, %%ecx \n\t"
441 "movl %0, %%ecx \n\t"
442 "shrl $3, %%ecx \n\t"
443 "orl %1, (%%ecx, %2) \n\t"
445 "movl $0, 4(%%ecx, %2) \n\t"
446 : "=&r" (s
->index
), "=&r" (value
)
447 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
452 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
454 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
456 //if(n>24) printf("%d %d\n", n, value);
460 # endif //!ALIGNED_BITSTREAM_WRITER
465 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
467 #ifdef ALT_BITSTREAM_WRITER
468 return s
->buf
+ (s
->index
>>3);
474 /* Bitstream reader API docs:
476 abritary name which is used as prefix for the internal variables
481 OPEN_READER(name, gb)
482 loads gb into local variables
484 CLOSE_READER(name, gb)
485 stores local vars in gb
487 UPDATE_CACHE(name, gb)
488 refills the internal cache from the bitstream
489 after this call at least MIN_CACHE_BITS will be available,
492 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
494 SHOW_UBITS(name, gb, num)
495 will return the nest num bits
497 SHOW_SBITS(name, gb, num)
498 will return the nest num bits and do sign extension
500 SKIP_BITS(name, gb, num)
501 will skip over the next num bits
502 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
504 SKIP_CACHE(name, gb, num)
505 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
507 SKIP_COUNTER(name, gb, num)
508 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
510 LAST_SKIP_CACHE(name, gb, num)
511 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
513 LAST_SKIP_BITS(name, gb, num)
514 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
516 for examples see get_bits, show_bits, skip_bits, get_vlc
519 static inline int unaligned32_be(const void *v
)
523 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
525 return be2me_32( unaligned32(v
)); //original
529 #ifdef ALT_BITSTREAM_READER
530 # define MIN_CACHE_BITS 25
532 # define OPEN_READER(name, gb)\
533 int name##_index= (gb)->index;\
534 int name##_cache= 0;\
536 # define CLOSE_READER(name, gb)\
537 (gb)->index= name##_index;\
539 # define UPDATE_CACHE(name, gb)\
540 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
542 # define SKIP_CACHE(name, gb, num)\
543 name##_cache <<= (num);\
546 # define SKIP_COUNTER(name, gb, num)\
547 name##_index += (num);\
549 # define SKIP_BITS(name, gb, num)\
551 SKIP_CACHE(name, gb, num)\
552 SKIP_COUNTER(name, gb, num)\
555 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
556 # define LAST_SKIP_CACHE(name, gb, num) ;
558 # define SHOW_UBITS(name, gb, num)\
559 NEG_USR32(name##_cache, num)
561 # define SHOW_SBITS(name, gb, num)\
562 NEG_SSR32(name##_cache, num)
564 # define GET_CACHE(name, gb)\
565 ((uint32_t)name##_cache)
567 static inline int get_bits_count(GetBitContext
*s
){
570 #elif defined LIBMPEG2_BITSTREAM_READER
571 //libmpeg2 like reader
573 # define MIN_CACHE_BITS 17
575 # define OPEN_READER(name, gb)\
576 int name##_bit_count=(gb)->bit_count;\
577 int name##_cache= (gb)->cache;\
578 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
580 # define CLOSE_READER(name, gb)\
581 (gb)->bit_count= name##_bit_count;\
582 (gb)->cache= name##_cache;\
583 (gb)->buffer_ptr= name##_buffer_ptr;\
585 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
587 # define UPDATE_CACHE(name, gb)\
588 if(name##_bit_count >= 0){\
589 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
590 ((uint16_t*)name##_buffer_ptr)++;\
591 name##_bit_count-= 16;\
596 # define UPDATE_CACHE(name, gb)\
597 if(name##_bit_count >= 0){\
598 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
599 name##_buffer_ptr+=2;\
600 name##_bit_count-= 16;\
605 # define SKIP_CACHE(name, gb, num)\
606 name##_cache <<= (num);\
608 # define SKIP_COUNTER(name, gb, num)\
609 name##_bit_count += (num);\
611 # define SKIP_BITS(name, gb, num)\
613 SKIP_CACHE(name, gb, num)\
614 SKIP_COUNTER(name, gb, num)\
617 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
618 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
620 # define SHOW_UBITS(name, gb, num)\
621 NEG_USR32(name##_cache, num)
623 # define SHOW_SBITS(name, gb, num)\
624 NEG_SSR32(name##_cache, num)
626 # define GET_CACHE(name, gb)\
627 ((uint32_t)name##_cache)
629 static inline int get_bits_count(GetBitContext
*s
){
630 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
633 #elif defined A32_BITSTREAM_READER
635 # define MIN_CACHE_BITS 32
637 # define OPEN_READER(name, gb)\
638 int name##_bit_count=(gb)->bit_count;\
639 uint32_t name##_cache0= (gb)->cache0;\
640 uint32_t name##_cache1= (gb)->cache1;\
641 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
643 # define CLOSE_READER(name, gb)\
644 (gb)->bit_count= name##_bit_count;\
645 (gb)->cache0= name##_cache0;\
646 (gb)->cache1= name##_cache1;\
647 (gb)->buffer_ptr= name##_buffer_ptr;\
649 # define UPDATE_CACHE(name, gb)\
650 if(name##_bit_count > 0){\
651 const uint32_t next= be2me_32( *name##_buffer_ptr );\
652 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
653 name##_cache1 |= next<<name##_bit_count;\
654 name##_buffer_ptr++;\
655 name##_bit_count-= 32;\
659 # define SKIP_CACHE(name, gb, num)\
661 "shldl %2, %1, %0 \n\t"\
663 : "+r" (name##_cache0), "+r" (name##_cache1)\
664 : "Ic" ((uint8_t)num)\
667 # define SKIP_CACHE(name, gb, num)\
668 name##_cache0 <<= (num);\
669 name##_cache0 |= NEG_USR32(name##_cache1,num);\
670 name##_cache1 <<= (num);
673 # define SKIP_COUNTER(name, gb, num)\
674 name##_bit_count += (num);\
676 # define SKIP_BITS(name, gb, num)\
678 SKIP_CACHE(name, gb, num)\
679 SKIP_COUNTER(name, gb, num)\
682 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
683 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
685 # define SHOW_UBITS(name, gb, num)\
686 NEG_USR32(name##_cache0, num)
688 # define SHOW_SBITS(name, gb, num)\
689 NEG_SSR32(name##_cache0, num)
691 # define GET_CACHE(name, gb)\
694 static inline int get_bits_count(GetBitContext
*s
){
695 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
701 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
702 * if MSB not set it is negative
703 * @param n length in bits
706 static inline int get_xbits(GetBitContext
*s
, int n
){
708 register int32_t cache
;
711 cache
= GET_CACHE(re
,s
);
712 if ((int32_t)cache
<0) { //MSB=1
713 tmp
= NEG_USR32(cache
,n
);
715 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
716 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
717 tmp
= - NEG_USR32(~cache
,n
);
719 LAST_SKIP_BITS(re
, s
, n
)
724 static inline int get_sbits(GetBitContext
*s
, int n
){
728 tmp
= SHOW_SBITS(re
, s
, n
);
729 LAST_SKIP_BITS(re
, s
, n
)
736 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
738 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
742 tmp
= SHOW_UBITS(re
, s
, n
);
743 LAST_SKIP_BITS(re
, s
, n
)
748 unsigned int get_bits_long(GetBitContext
*s
, int n
);
752 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
754 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
758 tmp
= SHOW_UBITS(re
, s
, n
);
759 // CLOSE_READER(re, s)
763 unsigned int show_bits_long(GetBitContext
*s
, int n
);
765 static inline void skip_bits(GetBitContext
*s
, int n
){
766 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
769 LAST_SKIP_BITS(re
, s
, n
)
773 static inline unsigned int get_bits1(GetBitContext
*s
){
774 #ifdef ALT_BITSTREAM_READER
776 uint8_t result
= s
->buffer
[ index
>>3 ];
777 result
<<= (index
&0x07);
784 return get_bits(s
, 1);
788 static inline unsigned int show_bits1(GetBitContext
*s
){
789 return show_bits(s
, 1);
792 static inline void skip_bits1(GetBitContext
*s
){
796 void init_get_bits(GetBitContext
*s
,
797 const uint8_t *buffer
, int buffer_size
);
799 int check_marker(GetBitContext
*s
, const char *msg
);
800 void align_get_bits(GetBitContext
*s
);
801 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
802 const void *bits
, int bits_wrap
, int bits_size
,
803 const void *codes
, int codes_wrap
, int codes_size
);
804 void free_vlc(VLC
*vlc
);
808 * if the vlc code is invalid and max_depth=1 than no bits will be removed
809 * if the vlc code is invalid and max_depth>1 than the number of bits removed
812 #define GET_VLC(code, name, gb, table, bits, max_depth)\
814 int n, index, nb_bits;\
816 index= SHOW_UBITS(name, gb, bits);\
817 code = table[index][0];\
818 n = table[index][1];\
820 if(max_depth > 1 && n < 0){\
821 LAST_SKIP_BITS(name, gb, bits)\
822 UPDATE_CACHE(name, gb)\
826 index= SHOW_UBITS(name, gb, nb_bits) + code;\
827 code = table[index][0];\
828 n = table[index][1];\
829 if(max_depth > 2 && n < 0){\
830 LAST_SKIP_BITS(name, gb, nb_bits)\
831 UPDATE_CACHE(name, gb)\
835 index= SHOW_UBITS(name, gb, nb_bits) + code;\
836 code = table[index][0];\
837 n = table[index][1];\
840 SKIP_BITS(name, gb, n)\
843 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
845 int n, index, nb_bits;\
847 index= SHOW_UBITS(name, gb, bits);\
848 level = table[index].level;\
849 n = table[index].len;\
851 if(max_depth > 1 && n < 0){\
852 LAST_SKIP_BITS(name, gb, bits)\
853 UPDATE_CACHE(name, gb)\
857 index= SHOW_UBITS(name, gb, nb_bits) + level;\
858 level = table[index].level;\
859 n = table[index].len;\
861 run= table[index].run;\
862 SKIP_BITS(name, gb, n)\
865 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
866 static inline int get_vlc(GetBitContext
*s
, VLC
*vlc
)
869 VLC_TYPE (*table
)[2]= vlc
->table
;
874 GET_VLC(code
, re
, s
, table
, vlc
->bits
, 3)
881 * parses a vlc code, faster then get_vlc()
882 * @param bits is the number of bits which will be read at once, must be
883 * identical to nb_bits in init_vlc()
884 * @param max_depth is the number of times bits bits must be readed to completly
885 * read the longest vlc code
886 * = (max_vlc_length + bits - 1) / bits
888 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
889 int bits
, int max_depth
)
896 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
906 static inline void print_bin(int bits
, int n
){
909 for(i
=n
-1; i
>=0; i
--){
910 printf("%d", (bits
>>i
)&1);
916 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
917 int r
= get_bits(s
, n
);
920 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
923 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, char *func
, int line
){
924 int show
= show_bits(s
, 24);
925 int pos
= get_bits_count(s
);
926 int r
= get_vlc2(s
, table
, bits
, max_depth
);
927 int len
= get_bits_count(s
) - pos
;
928 int bits2
= show
>>(24-len
);
930 print_bin(bits2
, len
);
932 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
935 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
936 int show
= show_bits(s
, n
);
937 int r
= get_xbits(s
, n
);
940 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
944 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
945 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
946 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
947 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
948 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
950 #define tprintf printf
953 #define tprintf(_arg...) {}
956 /* define it to include statistics code (useful only for optimizing
973 extern int st_current_index
;
974 extern unsigned int st_bit_counts
[ST_NB
];
975 extern unsigned int st_out_bit_counts
[ST_NB
];
977 void print_stats(void);
980 /* misc math functions */
981 extern const uint8_t ff_log2_tab
[256];
983 static inline int av_log2(unsigned int v
)
988 if (v
& 0xffff0000) {
1001 static inline int av_log2_16bit(unsigned int v
)
1010 n
+= ff_log2_tab
[v
];
1017 static inline int mid_pred(int a
, int b
, int c
)
1031 return a
+ b
+ c
- vmin
- vmax
;
1034 static inline int clip(int a
, int amin
, int amax
)
1045 extern const uint8_t ff_sqrt_tab
[128];
1047 int64_t ff_gcd(int64_t a
, int64_t b
);
1049 static inline int ff_sqrt(int a
)
1055 if(a
<128) return ff_sqrt_tab
[a
];
1057 for(s
=15; s
>=0; s
--){
1058 int b
= ret_sq
+ (1<<(s
*2)) + (ret
<<s
)*2;
1068 * converts fourcc string to int
1070 static inline int ff_get_fourcc(const char *s
){
1071 assert( strlen(s
)==4 );
1073 return (s
[0]) + (s
[1]<<8) + (s
[2]<<16) + (s
[3]<<24);
1076 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1077 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1081 #define MASK_ABS(mask, level)\
1086 : "+a" (level), "=&d" (mask)\
1089 #define MASK_ABS(mask, level)\
1091 level= (level^mask)-mask;
1095 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1096 #define COPY3_IF_LT(x,y,a,b,c,d)\
1099 "cmovl %3, %0 \n\t"\
1100 "cmovl %4, %1 \n\t"\
1101 "cmovl %5, %2 \n\t"\
1102 : "+r" (x), "+r" (a), "+r" (c)\
1103 : "r" (y), "r" (b), "r" (d)\
1106 #define COPY3_IF_LT(x,y,a,b,c,d)\
1115 static inline long long rdtsc()
1118 asm volatile( "rdtsc\n\t"
1124 #define START_TIMER \
1125 static uint64_t tsum=0;\
1126 static int tcount=0;\
1127 static int tskip_count=0;\
1129 uint64_t tstart= rdtsc();\
1131 #define STOP_TIMER(id) \
1133 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1134 tsum+= tend - tstart;\
1138 if(256*256*256*64%(tcount+tskip_count)==0){\
1139 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1143 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1145 /* avoid usage of various functions */
1146 #define malloc please_use_av_malloc
1147 #define free please_use_av_free
1148 #define realloc please_use_av_realloc
1150 #define CHECKED_ALLOCZ(p, size)\
1152 p= av_mallocz(size);\
1153 if(p==NULL && (size)!=0){\
1159 #endif /* HAVE_AV_CONFIG_H */
1161 #endif /* COMMON_H */