3
* common internal api header.
11
#define TUNECPU generic
14
#define HAVE_BUILTIN_VECTOR 1
15
#define CONFIG_DECODERS 1
16
/*#define CONFIG_HAVE_DLOPEN 1
17
#define CONFIG_HAVE_DLFCN 1*/
18
/*#undef CONFIG_AUDIO_OSS*/
20
/*#undef CONFIG_FFSERVER*/
21
#define CONFIG_RISKY 1
23
#define ALT_BITSTREAM_READER
24
//#define LIBMPEG2_BITSTREAM_READER
25
//#define A32_BITSTREAM_READER
26
//#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
29
#define M_PI 3.14159265358979323846
33
/* only include the following when compiling package */
48
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
51
#define AVOPTION_CODEC_BOOL(name, help, field) \
52
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
53
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
54
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
55
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
56
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
57
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
58
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
59
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
60
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
61
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
62
{ name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
63
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
64
#define AVOPTION_END() AVOPTION_SUB(NULL)
69
typedef struct AVOption {
71
const char *name; /* if name is NULL, it indicates a link to next */
72
/** short English text help or const struct AVOption* subpointer */
73
const char *help; // const struct AVOption* sub;
74
/** offset to context structure where the parsed value should be stored */
78
#define FF_OPT_TYPE_BOOL 1 ///< boolean - true,1,on (or simply presence)
79
#define FF_OPT_TYPE_DOUBLE 2 ///< double
80
#define FF_OPT_TYPE_INT 3 ///< integer
81
#define FF_OPT_TYPE_STRING 4 ///< string (finished with \0)
82
#define FF_OPT_TYPE_MASK 0x1f ///< mask for types - upper bits are various flags
83
//#define FF_OPT_TYPE_EXPERT 0x20 // flag for expert option
84
#define FF_OPT_TYPE_FLAG (FF_OPT_TYPE_BOOL | 0x40)
85
#define FF_OPT_TYPE_RCOVERRIDE (FF_OPT_TYPE_STRING | 0x80)
86
/** min value (min == max -> no limits) */
88
/** maximum value for double/int */
90
/** default boo [0,1]l/double/int value */
93
* default string value (with optional semicolon delimited extra option-list
94
* i.e. option1;option2;option3
95
* defval might select other then first argument as default
98
#define FF_OPT_MAX_DEPTH 10
102
extern const struct AVOption avoptions_common[3 + 5];
104
extern const struct AVOption avoptions_common[3];
106
extern const struct AVOption avoptions_workaround_bug[11];
108
#endif /* HAVE_AV_CONFIG_H */
110
/* Suppress restrict if it was not defined in config.h. */
115
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
116
# define always_inline __attribute__((always_inline)) inline
118
# define always_inline inline
121
#ifndef EMULATE_INTTYPES
122
# include <inttypes.h>
124
typedef signed char int8_t;
125
typedef signed short int16_t;
126
typedef signed int int32_t;
127
typedef unsigned char uint8_t;
128
typedef unsigned short uint16_t;
129
typedef unsigned int uint32_t;
130
typedef signed long long int64_t;
131
typedef unsigned long long uint64_t;
132
#endif /* HAVE_INTTYPES_H */
135
#define INT64_MAX 9223372036854775807LL
138
#ifdef EMULATE_FAST_INT
139
/* note that we don't emulate 64bit ints */
140
typedef signed char int_fast8_t;
141
typedef signed int int_fast16_t;
142
typedef signed int int_fast32_t;
143
typedef unsigned char uint_fast8_t;
144
typedef unsigned int uint_fast16_t;
145
typedef unsigned int uint_fast32_t;
152
#endif /* HAVE_AV_CONFIG_H */
159
#define int64_t_C(c) (c ## LL)
160
#define uint64_t_C(c) (c ## ULL)
164
# ifdef USE_FASTMEMCPY
165
# include "fastmemcpy.h"
167
# endif /* HAVE_AV_CONFIG_H */
169
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
175
# if defined(__MINGW32__) || defined(__CYGWIN__) || \
176
defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
177
# define MANGLE(a) "_" #a
179
# define MANGLE(a) #a
190
# if defined(CONFIG_WIN32) && !defined(__MINGW32__)
192
inline void dprintf(const char* fmt,...) {}
197
# define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
199
# define dprintf(fmt,...)
202
# endif /* !CONFIG_WIN32 */
204
# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
206
//rounded divison & shift
207
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
209
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
212
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
215
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
216
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
218
extern const uint32_t inverse[256];
221
# define FASTDIV(a,b) \
226
:"=d"(ret),"=a"(dmy)\
227
:"1"(a),"g"(inverse[b])\
231
#elif defined(CONFIG_FASTDIV)
232
# define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
234
# define FASTDIV(a,b) ((a)/(b))
238
// avoid +32 for shift optimization (gcc should do that ...)
239
static inline int32_t NEG_SSR32( int32_t a, int8_t s){
240
asm ("sarl %1, %0\n\t"
242
: "ic" ((uint8_t)(-s))
246
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
247
asm ("shrl %1, %0\n\t"
249
: "ic" ((uint8_t)(-s))
254
# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
255
# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
260
struct PutBitContext;
262
typedef void (*WriteDataFunc)(void *, uint8_t *, int);
264
typedef struct PutBitContext {
267
uint8_t *buf, *buf_ptr, *buf_end;
270
void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
274
int get_bit_count(PutBitContext *s); /* XXX: change function name */
275
void align_put_bits(PutBitContext *s);
276
void flush_put_bits(PutBitContext *s);
281
typedef struct GetBitContext {
282
const uint8_t *buffer, *buffer_end;
287
static inline int get_bits_count(GetBitContext *s);
289
#define VLC_TYPE int16_t
293
VLC_TYPE (*table)[2]; ///< code, bits
294
int table_size, table_allocated;
297
typedef struct RL_VLC_ELEM {
304
#define UNALIGNED_STORES_ARE_BAD
307
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
309
# define unaligned32(a) (*(uint32_t*)(a))
312
static inline uint32_t unaligned32(const void *v) {
315
} __attribute__((packed));
317
return ((const struct Unaligned *) v)->i;
319
# elif defined(__DECC)
320
static inline uint32_t unaligned32(const void *v) {
321
return *(const __unaligned uint32_t *) v;
324
static inline uint32_t unaligned32(const void *v) {
325
return *(const uint32_t *) v;
330
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
332
unsigned int bit_buf;
336
st_out_bit_counts[st_current_index] += n;
338
// printf("put_bits=%d %x\n", n, value);
339
assert(n == 32 || value < (1U << n));
341
bit_buf = s->bit_buf;
342
bit_left = s->bit_left;
344
// printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
347
bit_buf = (bit_buf<<n) | value;
351
bit_buf |= value >> (n - bit_left);
352
#ifdef UNALIGNED_STORES_ARE_BAD
353
if (3 & (int) s->buf_ptr) {
354
s->buf_ptr[0] = bit_buf >> 24;
355
s->buf_ptr[1] = bit_buf >> 16;
356
s->buf_ptr[2] = bit_buf >> 8;
357
s->buf_ptr[3] = bit_buf ;
360
*(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
361
//printf("bitbuf = %08x\n", bit_buf);
367
s->bit_buf = bit_buf;
368
s->bit_left = bit_left;
371
static inline uint8_t* pbBufPtr(PutBitContext *s)
376
/* Bitstream reader API docs:
378
abritary name which is used as prefix for the internal variables
383
OPEN_READER(name, gb)
384
loads gb into local variables
386
CLOSE_READER(name, gb)
387
stores local vars in gb
389
UPDATE_CACHE(name, gb)
390
refills the internal cache from the bitstream
391
after this call at least MIN_CACHE_BITS will be available,
394
will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
396
SHOW_UBITS(name, gb, num)
397
will return the nest num bits
399
SHOW_SBITS(name, gb, num)
400
will return the nest num bits and do sign extension
402
SKIP_BITS(name, gb, num)
403
will skip over the next num bits
404
note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
406
SKIP_CACHE(name, gb, num)
407
will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
409
SKIP_COUNTER(name, gb, num)
410
will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
412
LAST_SKIP_CACHE(name, gb, num)
413
will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
415
LAST_SKIP_BITS(name, gb, num)
416
is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
418
for examples see get_bits, show_bits, skip_bits, get_vlc
421
static inline int unaligned32_be(const void *v)
425
return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
427
return be2me_32( unaligned32(v)); //original
431
# define MIN_CACHE_BITS 25
433
# define OPEN_READER(name, gb)\
434
int name##_index= (gb)->index;\
435
int name##_cache= 0;\
437
# define CLOSE_READER(name, gb)\
438
(gb)->index= name##_index;\
440
# define UPDATE_CACHE(name, gb)\
441
name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
443
# define SKIP_CACHE(name, gb, num)\
444
name##_cache <<= (num);\
447
# define SKIP_COUNTER(name, gb, num)\
448
name##_index += (num);\
450
# define SKIP_BITS(name, gb, num)\
452
SKIP_CACHE(name, gb, num)\
453
SKIP_COUNTER(name, gb, num)\
456
# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
457
# define LAST_SKIP_CACHE(name, gb, num) ;
459
# define SHOW_UBITS(name, gb, num)\
460
NEG_USR32(name##_cache, num)
462
# define SHOW_SBITS(name, gb, num)\
463
NEG_SSR32(name##_cache, num)
465
# define GET_CACHE(name, gb)\
466
((uint32_t)name##_cache)
468
static inline int get_bits_count(GetBitContext *s){
473
* read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
474
* if MSB not set it is negative
475
* @param n length in bits
478
static inline int get_xbits(GetBitContext *s, int n){
480
register int32_t cache;
483
cache = GET_CACHE(re,s);
484
if ((int32_t)cache<0) { //MSB=1
485
tmp = NEG_USR32(cache,n);
487
// tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
488
// tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
489
tmp = - NEG_USR32(~cache,n);
491
LAST_SKIP_BITS(re, s, n)
496
static inline int get_sbits(GetBitContext *s, int n){
500
tmp= SHOW_SBITS(re, s, n);
501
LAST_SKIP_BITS(re, s, n)
508
* Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
510
static inline unsigned int get_bits(GetBitContext *s, int n){
514
tmp= SHOW_UBITS(re, s, n);
515
LAST_SKIP_BITS(re, s, n)
520
unsigned int get_bits_long(GetBitContext *s, int n);
524
* Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
526
static inline unsigned int show_bits(GetBitContext *s, int n){
530
tmp= SHOW_UBITS(re, s, n);
531
// CLOSE_READER(re, s)
535
unsigned int show_bits_long(GetBitContext *s, int n);
537
static inline void skip_bits(GetBitContext *s, int n){
538
//Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
541
LAST_SKIP_BITS(re, s, n)
545
static inline unsigned int get_bits1(GetBitContext *s){
546
#ifdef ALT_BITSTREAM_READER
548
uint8_t result= s->buffer[ index>>3 ];
549
result<<= (index&0x07);
556
return get_bits(s, 1);
560
static inline unsigned int show_bits1(GetBitContext *s){
561
return show_bits(s, 1);
564
static inline void skip_bits1(GetBitContext *s){
568
void init_get_bits(GetBitContext *s,
569
const uint8_t *buffer, int buffer_size);
571
int check_marker(GetBitContext *s, const char *msg);
572
void align_get_bits(GetBitContext *s);
573
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
574
const void *bits, int bits_wrap, int bits_size,
575
const void *codes, int codes_wrap, int codes_size);
576
void free_vlc(VLC *vlc);
580
* if the vlc code is invalid and max_depth=1 than no bits will be removed
581
* if the vlc code is invalid and max_depth>1 than the number of bits removed
584
#define GET_VLC(code, name, gb, table, bits, max_depth)\
586
int n, index, nb_bits;\
588
index= SHOW_UBITS(name, gb, bits);\
589
code = table[index][0];\
590
n = table[index][1];\
592
if(max_depth > 1 && n < 0){\
593
LAST_SKIP_BITS(name, gb, bits)\
594
UPDATE_CACHE(name, gb)\
598
index= SHOW_UBITS(name, gb, nb_bits) + code;\
599
code = table[index][0];\
600
n = table[index][1];\
601
if(max_depth > 2 && n < 0){\
602
LAST_SKIP_BITS(name, gb, nb_bits)\
603
UPDATE_CACHE(name, gb)\
607
index= SHOW_UBITS(name, gb, nb_bits) + code;\
608
code = table[index][0];\
609
n = table[index][1];\
612
SKIP_BITS(name, gb, n)\
615
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
617
int n, index, nb_bits;\
619
index= SHOW_UBITS(name, gb, bits);\
620
level = table[index].level;\
621
n = table[index].len;\
623
if(max_depth > 1 && n < 0){\
624
LAST_SKIP_BITS(name, gb, bits)\
625
UPDATE_CACHE(name, gb)\
629
index= SHOW_UBITS(name, gb, nb_bits) + level;\
630
level = table[index].level;\
631
n = table[index].len;\
633
run= table[index].run;\
634
SKIP_BITS(name, gb, n)\
637
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
638
static inline int get_vlc(GetBitContext *s, VLC *vlc)
641
VLC_TYPE (*table)[2]= vlc->table;
646
GET_VLC(code, re, s, table, vlc->bits, 3)
653
* parses a vlc code, faster then get_vlc()
654
* @param bits is the number of bits which will be read at once, must be
655
* identical to nb_bits in init_vlc()
656
* @param max_depth is the number of times bits bits must be readed to completly
657
* read the longest vlc code
658
* = (max_vlc_length + bits - 1) / bits
660
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
661
int bits, int max_depth)
668
GET_VLC(code, re, s, table, bits, max_depth)
678
static inline void print_bin(int bits, int n){
681
for(i=n-1; i>=0; i--){
682
printf("%d", (bits>>i)&1);
688
static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
689
int r= get_bits(s, n);
692
printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
695
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
696
int show= show_bits(s, 24);
697
int pos= get_bits_count(s);
698
int r= get_vlc2(s, table, bits, max_depth);
699
int len= get_bits_count(s) - pos;
700
int bits2= show>>(24-len);
702
print_bin(bits2, len);
704
printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
707
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
708
int show= show_bits(s, n);
709
int r= get_xbits(s, n);
712
printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
716
#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
717
#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
718
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
719
#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
720
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
722
#define tprintf printf
725
#define tprintf(_arg...) {}
728
/* define it to include statistics code (useful only for optimizing
745
extern int st_current_index;
746
extern unsigned int st_bit_counts[ST_NB];
747
extern unsigned int st_out_bit_counts[ST_NB];
749
void print_stats(void);
752
/* misc math functions */
753
extern const uint8_t ff_log2_tab[256];
755
static inline int av_log2(unsigned int v)
760
if (v & 0xffff0000) {
773
static inline int av_log2_16bit(unsigned int v)
788
static inline int mid_pred(int a, int b, int c)
791
int t= (a-b)&((a-b)>>31);
794
b-= (b-c)&((b-c)>>31);
795
b+= (a-b)&((a-b)>>31);
814
static inline int clip(int a, int amin, int amax)
825
extern const uint8_t ff_sqrt_tab[128];
827
int64_t ff_gcd(int64_t a, int64_t b);
829
static inline int ff_sqrt(int a)
835
if(a<128) return ff_sqrt_tab[a];
837
for(s=15; s>=0; s--){
838
int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
848
* converts fourcc string to int
850
static inline int ff_get_fourcc(const char *s){
851
assert( strlen(s)==4 );
853
return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
856
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
857
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
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)\
895
static inline long long rdtsc()
898
asm volatile( "rdtsc\n\t"
904
#define START_TIMER \
905
static uint64_t tsum=0;\
906
static int tcount=0;\
907
static int tskip_count=0;\
909
uint64_t tstart= rdtsc();\
911
#define STOP_TIMER(id) \
913
if(tcount<2 || tend - tstart < 4*tsum/tcount){\
914
tsum+= tend - tstart;\
918
if(256*256*256*64%(tcount+tskip_count)==0){\
919
fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
923
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
925
#define CHECKED_ALLOCZ(p, size)\
927
p= av_mallocz(size);\
928
if(p==NULL && (size)!=0){\
937
#endif /* HAVE_AV_CONFIG_H */
939
#endif /* COMMON_H */