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
89 typedef unsigned short uint16_t;
90 typedef signed short int16_t;
91 typedef unsigned char uint8_t;
92 typedef unsigned int uint32_t;
93 typedef unsigned __int64
uint64_t;
94 typedef signed char int8_t;
95 typedef signed int int32_t;
96 typedef signed __int64
int64_t;
99 # define int64_t_C(c) (c ## i64)
100 # define uint64_t_C(c) (c ## i64)
102 # ifdef HAVE_AV_CONFIG_H
103 # define inline __inline
107 # define int64_t_C(c) (c ## LL)
108 # define uint64_t_C(c) (c ## ULL)
109 # endif /* __MINGW32__ */
111 # ifdef HAVE_AV_CONFIG_H
116 # define snprintf _snprintf
117 # define vsnprintf _vsnprintf
120 /* CONFIG_WIN32 end */
121 #elif defined (CONFIG_OS2)
124 #include <inttypes.h>
127 #define int64_t_C(c) (c ## LL)
128 #define uint64_t_C(c) (c ## ULL)
131 #ifdef HAVE_AV_CONFIG_H
133 #ifdef USE_FASTMEMCPY
134 #include "fastmemcpy.h"
139 #endif /* HAVE_AV_CONFIG_H */
146 #include <inttypes.h>
149 #define int64_t_C(c) (c ## LL)
150 #define uint64_t_C(c) (c ## ULL)
153 #ifdef HAVE_AV_CONFIG_H
155 # ifdef USE_FASTMEMCPY
156 # include "fastmemcpy.h"
158 # endif /* HAVE_AV_CONFIG_H */
160 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
162 #ifdef HAVE_AV_CONFIG_H
166 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
167 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
168 # define MANGLE(a) "_" #a
170 # define MANGLE(a) #a
181 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
183 inline void dprintf(const char* fmt
,...) {}
188 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
190 # define dprintf(fmt,...)
193 # endif /* !CONFIG_WIN32 */
195 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
197 //rounded divison & shift
198 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
200 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
201 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
203 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
204 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
206 extern const uint32_t inverse
[256];
209 # define FASTDIV(a,b) \
214 :"=d"(ret),"=a"(dmy)\
215 :"1"(a),"g"(inverse[b])\
219 #elif defined(CONFIG_FASTDIV)
220 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
222 # define FASTDIV(a,b) ((a)/(b))
226 // avoid +32 for shift optimization (gcc should do that ...)
227 static inline int32_t NEG_SSR32( int32_t a
, int8_t s
){
228 asm ("sarl %1, %0\n\t"
230 : "ic" ((uint8_t)(-s
))
234 static inline uint32_t NEG_USR32(uint32_t a
, int8_t s
){
235 asm ("shrl %1, %0\n\t"
237 : "ic" ((uint8_t)(-s
))
242 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
243 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
248 struct PutBitContext
;
250 typedef void (*WriteDataFunc
)(void *, uint8_t *, int);
252 typedef struct PutBitContext
{
253 #ifdef ALT_BITSTREAM_WRITER
254 uint8_t *buf
, *buf_end
;
259 uint8_t *buf
, *buf_ptr
, *buf_end
;
261 int64_t data_out_size
; /* in bytes */
264 void init_put_bits(PutBitContext
*s
, uint8_t *buffer
, int buffer_size
);
266 int64_t get_bit_count(PutBitContext
*s
); /* XXX: change function name */
267 void align_put_bits(PutBitContext
*s
);
268 void flush_put_bits(PutBitContext
*s
);
269 void put_string(PutBitContext
* pbc
, char *s
);
273 typedef struct GetBitContext
{
274 const uint8_t *buffer
, *buffer_end
;
275 #ifdef ALT_BITSTREAM_READER
277 #elif defined LIBMPEG2_BITSTREAM_READER
281 #elif defined A32_BITSTREAM_READER
282 uint32_t *buffer_ptr
;
290 static inline int get_bits_count(GetBitContext
*s
);
292 #define VLC_TYPE int16_t
296 VLC_TYPE (*table
)[2]; ///< code, bits
297 int table_size
, table_allocated
;
300 typedef struct RL_VLC_ELEM
{
307 #define UNALIGNED_STORES_ARE_BAD
310 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
312 # define unaligned32(a) (*(uint32_t*)(a))
315 static inline uint32_t unaligned32(const void *v
) {
318 } __attribute__((packed
));
320 return ((const struct Unaligned
*) v
)->i
;
322 # elif defined(__DECC)
323 static inline uint32_t unaligned32(const void *v
) {
324 return *(const __unaligned
uint32_t *) v
;
327 static inline uint32_t unaligned32(const void *v
) {
328 return *(const uint32_t *) v
;
333 #ifndef ALT_BITSTREAM_WRITER
334 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
336 unsigned int bit_buf
;
340 st_out_bit_counts
[st_current_index
] += n
;
342 // printf("put_bits=%d %x\n", n, value);
343 assert(n
== 32 || value
< (1U << n
));
345 bit_buf
= s
->bit_buf
;
346 bit_left
= s
->bit_left
;
348 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
351 bit_buf
= (bit_buf
<<n
) | value
;
355 bit_buf
|= value
>> (n
- bit_left
);
356 #ifdef UNALIGNED_STORES_ARE_BAD
357 if (3 & (int) s
->buf_ptr
) {
358 s
->buf_ptr
[0] = bit_buf
>> 24;
359 s
->buf_ptr
[1] = bit_buf
>> 16;
360 s
->buf_ptr
[2] = bit_buf
>> 8;
361 s
->buf_ptr
[3] = bit_buf
;
364 *(uint32_t *)s
->buf_ptr
= be2me_32(bit_buf
);
365 //printf("bitbuf = %08x\n", bit_buf);
371 s
->bit_buf
= bit_buf
;
372 s
->bit_left
= bit_left
;
377 #ifdef ALT_BITSTREAM_WRITER
378 static inline void put_bits(PutBitContext
*s
, int n
, unsigned int value
)
380 # ifdef ALIGNED_BITSTREAM_WRITER
383 "movl %0, %%ecx \n\t"
384 "xorl %%eax, %%eax \n\t"
385 "shrdl %%cl, %1, %%eax \n\t"
387 "movl %0, %%ecx \n\t"
388 "shrl $3, %%ecx \n\t"
389 "andl $0xFFFFFFFC, %%ecx \n\t"
391 "orl %1, (%2, %%ecx) \n\t"
394 "movl %%eax, 4(%2, %%ecx) \n\t"
395 : "=&r" (s
->index
), "=&r" (value
)
396 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
<<(-n
))
401 uint32_t *ptr
= ((uint32_t *)s
->buf
)+(index
>>5);
405 ptr
[0] |= be2me_32(value
>>(index
&31));
406 ptr
[1] = be2me_32(value
<<(32-(index
&31)));
407 //if(n>24) printf("%d %d\n", n, value);
411 # else //ALIGNED_BITSTREAM_WRITER
414 "movl $7, %%ecx \n\t"
415 "andl %0, %%ecx \n\t"
416 "addl %3, %%ecx \n\t"
420 "movl %0, %%ecx \n\t"
421 "shrl $3, %%ecx \n\t"
422 "orl %1, (%%ecx, %2) \n\t"
424 "movl $0, 4(%%ecx, %2) \n\t"
425 : "=&r" (s
->index
), "=&r" (value
)
426 : "r" (s
->buf
), "r" (n
), "0" (s
->index
), "1" (value
)
431 uint32_t *ptr
= (uint32_t*)(((uint8_t *)s
->buf
)+(index
>>3));
433 ptr
[0] |= be2me_32(value
<<(32-n
-(index
&7) ));
435 //if(n>24) printf("%d %d\n", n, value);
439 # endif //!ALIGNED_BITSTREAM_WRITER
444 static inline uint8_t* pbBufPtr(PutBitContext
*s
)
446 #ifdef ALT_BITSTREAM_WRITER
447 return s
->buf
+ (s
->index
>>3);
453 /* Bitstream reader API docs:
455 abritary name which is used as prefix for the internal variables
460 OPEN_READER(name, gb)
461 loads gb into local variables
463 CLOSE_READER(name, gb)
464 stores local vars in gb
466 UPDATE_CACHE(name, gb)
467 refills the internal cache from the bitstream
468 after this call at least MIN_CACHE_BITS will be available,
471 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
473 SHOW_UBITS(name, gb, num)
474 will return the nest num bits
476 SHOW_SBITS(name, gb, num)
477 will return the nest num bits and do sign extension
479 SKIP_BITS(name, gb, num)
480 will skip over the next num bits
481 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
483 SKIP_CACHE(name, gb, num)
484 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
486 SKIP_COUNTER(name, gb, num)
487 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
489 LAST_SKIP_CACHE(name, gb, num)
490 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
492 LAST_SKIP_BITS(name, gb, num)
493 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
495 for examples see get_bits, show_bits, skip_bits, get_vlc
498 static inline int unaligned32_be(const void *v
)
502 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
504 return be2me_32( unaligned32(v
)); //original
508 #ifdef ALT_BITSTREAM_READER
509 # define MIN_CACHE_BITS 25
511 # define OPEN_READER(name, gb)\
512 int name##_index= (gb)->index;\
513 int name##_cache= 0;\
515 # define CLOSE_READER(name, gb)\
516 (gb)->index= name##_index;\
518 # define UPDATE_CACHE(name, gb)\
519 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
521 # define SKIP_CACHE(name, gb, num)\
522 name##_cache <<= (num);\
525 # define SKIP_COUNTER(name, gb, num)\
526 name##_index += (num);\
528 # define SKIP_BITS(name, gb, num)\
530 SKIP_CACHE(name, gb, num)\
531 SKIP_COUNTER(name, gb, num)\
534 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
535 # define LAST_SKIP_CACHE(name, gb, num) ;
537 # define SHOW_UBITS(name, gb, num)\
538 NEG_USR32(name##_cache, num)
540 # define SHOW_SBITS(name, gb, num)\
541 NEG_SSR32(name##_cache, num)
543 # define GET_CACHE(name, gb)\
544 ((uint32_t)name##_cache)
546 static inline int get_bits_count(GetBitContext
*s
){
549 #elif defined LIBMPEG2_BITSTREAM_READER
550 //libmpeg2 like reader
552 # define MIN_CACHE_BITS 17
554 # define OPEN_READER(name, gb)\
555 int name##_bit_count=(gb)->bit_count;\
556 int name##_cache= (gb)->cache;\
557 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
559 # define CLOSE_READER(name, gb)\
560 (gb)->bit_count= name##_bit_count;\
561 (gb)->cache= name##_cache;\
562 (gb)->buffer_ptr= name##_buffer_ptr;\
564 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
566 # define UPDATE_CACHE(name, gb)\
567 if(name##_bit_count >= 0){\
568 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
569 ((uint16_t*)name##_buffer_ptr)++;\
570 name##_bit_count-= 16;\
575 # define UPDATE_CACHE(name, gb)\
576 if(name##_bit_count >= 0){\
577 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
578 name##_buffer_ptr+=2;\
579 name##_bit_count-= 16;\
584 # define SKIP_CACHE(name, gb, num)\
585 name##_cache <<= (num);\
587 # define SKIP_COUNTER(name, gb, num)\
588 name##_bit_count += (num);\
590 # define SKIP_BITS(name, gb, num)\
592 SKIP_CACHE(name, gb, num)\
593 SKIP_COUNTER(name, gb, num)\
596 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
597 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
599 # define SHOW_UBITS(name, gb, num)\
600 NEG_USR32(name##_cache, num)
602 # define SHOW_SBITS(name, gb, num)\
603 NEG_SSR32(name##_cache, num)
605 # define GET_CACHE(name, gb)\
606 ((uint32_t)name##_cache)
608 static inline int get_bits_count(GetBitContext
*s
){
609 return (s
->buffer_ptr
- s
->buffer
)*8 - 16 + s
->bit_count
;
612 #elif defined A32_BITSTREAM_READER
614 # define MIN_CACHE_BITS 32
616 # define OPEN_READER(name, gb)\
617 int name##_bit_count=(gb)->bit_count;\
618 uint32_t name##_cache0= (gb)->cache0;\
619 uint32_t name##_cache1= (gb)->cache1;\
620 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
622 # define CLOSE_READER(name, gb)\
623 (gb)->bit_count= name##_bit_count;\
624 (gb)->cache0= name##_cache0;\
625 (gb)->cache1= name##_cache1;\
626 (gb)->buffer_ptr= name##_buffer_ptr;\
628 # define UPDATE_CACHE(name, gb)\
629 if(name##_bit_count > 0){\
630 const uint32_t next= be2me_32( *name##_buffer_ptr );\
631 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
632 name##_cache1 |= next<<name##_bit_count;\
633 name##_buffer_ptr++;\
634 name##_bit_count-= 32;\
638 # define SKIP_CACHE(name, gb, num)\
640 "shldl %2, %1, %0 \n\t"\
642 : "+r" (name##_cache0), "+r" (name##_cache1)\
643 : "Ic" ((uint8_t)num)\
646 # define SKIP_CACHE(name, gb, num)\
647 name##_cache0 <<= (num);\
648 name##_cache0 |= NEG_USR32(name##_cache1,num);\
649 name##_cache1 <<= (num);
652 # define SKIP_COUNTER(name, gb, num)\
653 name##_bit_count += (num);\
655 # define SKIP_BITS(name, gb, num)\
657 SKIP_CACHE(name, gb, num)\
658 SKIP_COUNTER(name, gb, num)\
661 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
662 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
664 # define SHOW_UBITS(name, gb, num)\
665 NEG_USR32(name##_cache0, num)
667 # define SHOW_SBITS(name, gb, num)\
668 NEG_SSR32(name##_cache0, num)
670 # define GET_CACHE(name, gb)\
673 static inline int get_bits_count(GetBitContext
*s
){
674 return ((uint8_t*)s
->buffer_ptr
- s
->buffer
)*8 - 32 + s
->bit_count
;
680 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
681 * if MSB not set it is negative
682 * @param n length in bits
685 static inline int get_xbits(GetBitContext
*s
, int n
){
687 register int32_t cache
;
690 cache
= GET_CACHE(re
,s
);
691 if ((int32_t)cache
<0) { //MSB=1
692 tmp
= NEG_USR32(cache
,n
);
694 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
695 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
696 tmp
= - NEG_USR32(~cache
,n
);
698 LAST_SKIP_BITS(re
, s
, n
)
703 static inline int get_sbits(GetBitContext
*s
, int n
){
707 tmp
= SHOW_SBITS(re
, s
, n
);
708 LAST_SKIP_BITS(re
, s
, n
)
715 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
717 static inline unsigned int get_bits(GetBitContext
*s
, int n
){
721 tmp
= SHOW_UBITS(re
, s
, n
);
722 LAST_SKIP_BITS(re
, s
, n
)
727 unsigned int get_bits_long(GetBitContext
*s
, int n
);
731 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
733 static inline unsigned int show_bits(GetBitContext
*s
, int n
){
737 tmp
= SHOW_UBITS(re
, s
, n
);
738 // CLOSE_READER(re, s)
742 unsigned int show_bits_long(GetBitContext
*s
, int n
);
744 static inline void skip_bits(GetBitContext
*s
, int n
){
745 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
748 LAST_SKIP_BITS(re
, s
, n
)
752 static inline unsigned int get_bits1(GetBitContext
*s
){
753 #ifdef ALT_BITSTREAM_READER
755 uint8_t result
= s
->buffer
[ index
>>3 ];
756 result
<<= (index
&0x07);
763 return get_bits(s
, 1);
767 static inline unsigned int show_bits1(GetBitContext
*s
){
768 return show_bits(s
, 1);
771 static inline void skip_bits1(GetBitContext
*s
){
775 void init_get_bits(GetBitContext
*s
,
776 const uint8_t *buffer
, int buffer_size
);
778 int check_marker(GetBitContext
*s
, const char *msg
);
779 void align_get_bits(GetBitContext
*s
);
780 int init_vlc(VLC
*vlc
, int nb_bits
, int nb_codes
,
781 const void *bits
, int bits_wrap
, int bits_size
,
782 const void *codes
, int codes_wrap
, int codes_size
);
783 void free_vlc(VLC
*vlc
);
787 * if the vlc code is invalid and max_depth=1 than no bits will be removed
788 * if the vlc code is invalid and max_depth>1 than the number of bits removed
791 #define GET_VLC(code, name, gb, table, bits, max_depth)\
793 int n, index, nb_bits;\
795 index= SHOW_UBITS(name, gb, bits);\
796 code = table[index][0];\
797 n = table[index][1];\
799 if(max_depth > 1 && n < 0){\
800 LAST_SKIP_BITS(name, gb, bits)\
801 UPDATE_CACHE(name, gb)\
805 index= SHOW_UBITS(name, gb, nb_bits) + code;\
806 code = table[index][0];\
807 n = table[index][1];\
808 if(max_depth > 2 && n < 0){\
809 LAST_SKIP_BITS(name, gb, nb_bits)\
810 UPDATE_CACHE(name, gb)\
814 index= SHOW_UBITS(name, gb, nb_bits) + code;\
815 code = table[index][0];\
816 n = table[index][1];\
819 SKIP_BITS(name, gb, n)\
822 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
824 int n, index, nb_bits;\
826 index= SHOW_UBITS(name, gb, bits);\
827 level = table[index].level;\
828 n = table[index].len;\
830 if(max_depth > 1 && n < 0){\
831 LAST_SKIP_BITS(name, gb, bits)\
832 UPDATE_CACHE(name, gb)\
836 index= SHOW_UBITS(name, gb, nb_bits) + level;\
837 level = table[index].level;\
838 n = table[index].len;\
840 run= table[index].run;\
841 SKIP_BITS(name, gb, n)\
844 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
845 static inline int get_vlc(GetBitContext
*s
, VLC
*vlc
)
848 VLC_TYPE (*table
)[2]= vlc
->table
;
853 GET_VLC(code
, re
, s
, table
, vlc
->bits
, 3)
860 * parses a vlc code, faster then get_vlc()
861 * @param bits is the number of bits which will be read at once, must be
862 * identical to nb_bits in init_vlc()
863 * @param max_depth is the number of times bits bits must be readed to completly
864 * read the longest vlc code
865 * = (max_vlc_length + bits - 1) / bits
867 static always_inline
int get_vlc2(GetBitContext
*s
, VLC_TYPE (*table
)[2],
868 int bits
, int max_depth
)
875 GET_VLC(code
, re
, s
, table
, bits
, max_depth
)
885 static inline void print_bin(int bits
, int n
){
888 for(i
=n
-1; i
>=0; i
--){
889 printf("%d", (bits
>>i
)&1);
895 static inline int get_bits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
896 int r
= get_bits(s
, n
);
899 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
902 static inline int get_vlc_trace(GetBitContext
*s
, VLC_TYPE (*table
)[2], int bits
, int max_depth
, char *file
, char *func
, int line
){
903 int show
= show_bits(s
, 24);
904 int pos
= get_bits_count(s
);
905 int r
= get_vlc2(s
, table
, bits
, max_depth
);
906 int len
= get_bits_count(s
) - pos
;
907 int bits2
= show
>>(24-len
);
909 print_bin(bits2
, len
);
911 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2
, len
, r
, pos
, file
, func
, line
);
914 static inline int get_xbits_trace(GetBitContext
*s
, int n
, char *file
, char *func
, int line
){
915 int show
= show_bits(s
, n
);
916 int r
= get_xbits(s
, n
);
919 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show
, n
, r
, get_bits_count(s
)-n
, file
, func
, line
);
923 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
924 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
925 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
926 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
927 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
929 #define tprintf printf
932 #define tprintf(_arg...) {}
935 /* define it to include statistics code (useful only for optimizing
952 extern int st_current_index
;
953 extern unsigned int st_bit_counts
[ST_NB
];
954 extern unsigned int st_out_bit_counts
[ST_NB
];
956 void print_stats(void);
959 /* misc math functions */
960 extern const uint8_t ff_log2_tab
[256];
962 static inline int av_log2(unsigned int v
)
967 if (v
& 0xffff0000) {
980 static inline int av_log2_16bit(unsigned int v
)
996 static inline int mid_pred(int a
, int b
, int c
)
1010 return a
+ b
+ c
- vmin
- vmax
;
1013 static inline int clip(int a
, int amin
, int amax
)
1024 extern const uint8_t ff_sqrt_tab
[128];
1026 int64_t ff_gcd(int64_t a
, int64_t b
);
1028 static inline int ff_sqrt(int a
)
1034 if(a
<128) return ff_sqrt_tab
[a
];
1036 for(s
=15; s
>=0; s
--){
1037 int b
= ret_sq
+ (1<<(s
*2)) + (ret
<<s
)*2;
1047 * converts fourcc string to int
1049 static inline int ff_get_fourcc(const char *s
){
1050 assert( strlen(s
)==4 );
1052 return (s
[0]) + (s
[1]<<8) + (s
[2]<<16) + (s
[3]<<24);
1055 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1056 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1059 void ff_float2fraction(int *nom_arg
, int *denom_arg
, double f
, int max
);
1063 #define MASK_ABS(mask, level)\
1068 : "+a" (level), "=&d" (mask)\
1071 #define MASK_ABS(mask, level)\
1073 level= (level^mask)-mask;
1077 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1078 #define COPY3_IF_LT(x,y,a,b,c,d)\
1081 "cmovl %3, %0 \n\t"\
1082 "cmovl %4, %1 \n\t"\
1083 "cmovl %5, %2 \n\t"\
1084 : "+r" (x), "+r" (a), "+r" (c)\
1085 : "r" (y), "r" (b), "r" (d)\
1088 #define COPY3_IF_LT(x,y,a,b,c,d)\
1097 static inline long long rdtsc()
1100 asm volatile( "rdtsc\n\t"
1106 #define START_TIMER \
1107 static uint64_t tsum=0;\
1108 static int tcount=0;\
1109 static int tskip_count=0;\
1111 uint64_t tstart= rdtsc();\
1113 #define STOP_TIMER(id) \
1115 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1116 tsum+= tend - tstart;\
1120 if(256*256*256*64%(tcount+tskip_count)==0){\
1121 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1125 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1127 /* avoid usage of various functions */
1128 #define malloc please_use_av_malloc
1129 #define free please_use_av_free
1130 #define realloc please_use_av_realloc
1132 #define CHECKED_ALLOCZ(p, size)\
1134 p= av_mallocz(size);\
1135 if(p==NULL && (size)!=0){\
1141 #endif /* HAVE_AV_CONFIG_H */
1143 #endif /* COMMON_H */