comparison src/ffmpeg/libffwma/common.h @ 806:74abcb9cafae trunk

[svn] - fork wma plugin
author nenolod
date Mon, 12 Mar 2007 10:59:21 -0700
parents src/wma/libffwma/common.h@3da1b8942b8b
children
comparison
equal deleted inserted replaced
805:1ba5f86aeac9 806:74abcb9cafae
1 /**
2 * @file common.h
3 * common internal api header.
4 */
5
6 #ifndef COMMON_H
7 #define COMMON_H
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #define TUNECPU generic
14 #undef HAVE_MMX
15 #define __CPU__ 586
16 #define HAVE_BUILTIN_VECTOR 1
17 #define CONFIG_DECODERS 1
18 /*#define CONFIG_HAVE_DLOPEN 1
19 #define CONFIG_HAVE_DLFCN 1*/
20 /*#undef CONFIG_AUDIO_OSS*/
21 #define SIMPLE_IDCT 1
22 /*#undef CONFIG_FFSERVER*/
23 #define CONFIG_RISKY 1
24
25 #define ALT_BITSTREAM_READER
26 //#define LIBMPEG2_BITSTREAM_READER
27 //#define A32_BITSTREAM_READER
28 //#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
29
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846
32 #endif
33
34 #if 1
35 /* only include the following when compiling package */
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <math.h>
43
44 # ifndef ENODATA
45 # define ENODATA 61
46 # endif
47
48 #include <stddef.h>
49 #ifndef offsetof
50 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
51 #endif
52
53 #define AVOPTION_CODEC_BOOL(name, help, field) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
55 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
57 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
59 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
60 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
61 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
62 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
63 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
64 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
65 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
66 #define AVOPTION_END() AVOPTION_SUB(NULL)
67
68 /**
69 * AVOption.
70 */
71 typedef struct AVOption {
72 /** options' name */
73 const char *name; /* if name is NULL, it indicates a link to next */
74 /** short English text help or const struct AVOption* subpointer */
75 const char *help; // const struct AVOption* sub;
76 /** offset to context structure where the parsed value should be stored */
77 int offset;
78 /** options' type */
79 int type;
80 #define FF_OPT_TYPE_BOOL 1 ///< boolean - true,1,on (or simply presence)
81 #define FF_OPT_TYPE_DOUBLE 2 ///< double
82 #define FF_OPT_TYPE_INT 3 ///< integer
83 #define FF_OPT_TYPE_STRING 4 ///< string (finished with \0)
84 #define FF_OPT_TYPE_MASK 0x1f ///< mask for types - upper bits are various flags
85 //#define FF_OPT_TYPE_EXPERT 0x20 // flag for expert option
86 #define FF_OPT_TYPE_FLAG (FF_OPT_TYPE_BOOL | 0x40)
87 #define FF_OPT_TYPE_RCOVERRIDE (FF_OPT_TYPE_STRING | 0x80)
88 /** min value (min == max -> no limits) */
89 double min;
90 /** maximum value for double/int */
91 double max;
92 /** default boo [0,1]l/double/int value */
93 double defval;
94 /**
95 * default string value (with optional semicolon delimited extra option-list
96 * i.e. option1;option2;option3
97 * defval might select other then first argument as default
98 */
99 const char *defstr;
100 #define FF_OPT_MAX_DEPTH 10
101 } AVOption;
102
103 #ifdef HAVE_MMX
104 extern const struct AVOption avoptions_common[3 + 5];
105 #else
106 extern const struct AVOption avoptions_common[3];
107 #endif
108 extern const struct AVOption avoptions_workaround_bug[11];
109
110 #endif /* HAVE_AV_CONFIG_H */
111
112 /* Suppress restrict if it was not defined in config.h. */
113 #ifndef restrict
114 # define restrict
115 #endif
116
117 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
118 # define always_inline __attribute__((always_inline)) inline
119 #else
120 # define always_inline inline
121 #endif
122
123 #ifndef EMULATE_INTTYPES
124 # include <inttypes.h>
125 #else
126 typedef signed char int8_t;
127 typedef signed short int16_t;
128 typedef signed int int32_t;
129 typedef unsigned char uint8_t;
130 typedef unsigned short uint16_t;
131 typedef unsigned int uint32_t;
132 typedef signed long long int64_t;
133 typedef unsigned long long uint64_t;
134 #endif /* HAVE_INTTYPES_H */
135
136 #ifndef INT64_MAX
137 #define INT64_MAX 9223372036854775807LL
138 #endif
139
140 #ifdef EMULATE_FAST_INT
141 /* note that we don't emulate 64bit ints */
142 typedef signed char int_fast8_t;
143 typedef signed int int_fast16_t;
144 typedef signed int int_fast32_t;
145 typedef unsigned char uint_fast8_t;
146 typedef unsigned int uint_fast16_t;
147 typedef unsigned int uint_fast32_t;
148 #endif
149
150 #if 1
151
152 #include <float.h>
153
154 #endif /* HAVE_AV_CONFIG_H */
155
156 /* CONFIG_OS2 end */
157
158 /* unix */
159
160 #ifndef int64_t_C
161 #define int64_t_C(c) (c ## LL)
162 #define uint64_t_C(c) (c ## ULL)
163
164 #if 1
165
166 # ifdef USE_FASTMEMCPY
167 # include "fastmemcpy.h"
168 # endif
169 # endif /* HAVE_AV_CONFIG_H */
170
171 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
172
173 #if 1
174
175 # include "bswap.h"
176
177 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
178 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
179 # define MANGLE(a) "_" #a
180 # else
181 # define MANGLE(a) #a
182 # endif
183
184 /* debug stuff */
185
186 # ifndef DEBUG
187 # define NDEBUG
188 # endif
189 # include <assert.h>
190
191 /* dprintf macros */
192 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
193
194 inline void dprintf(const char* fmt,...) {}
195
196 # else
197
198 # ifdef DEBUG
199 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
200 # else
201 # define dprintf(fmt,...)
202 # endif
203
204 # endif /* !CONFIG_WIN32 */
205
206 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
207
208 //rounded divison & shift
209 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
210 /* assume b>0 */
211 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
212
213 #ifndef ABS
214 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
215 #endif
216
217 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
218 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
219
220 extern const uint32_t inverse[256];
221
222 #ifdef ARCH_X86
223 # define FASTDIV(a,b) \
224 ({\
225 int ret,dmy;\
226 asm volatile(\
227 "mull %3"\
228 :"=d"(ret),"=a"(dmy)\
229 :"1"(a),"g"(inverse[b])\
230 );\
231 ret;\
232 })
233 #elif defined(CONFIG_FASTDIV)
234 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
235 #else
236 # define FASTDIV(a,b) ((a)/(b))
237 #endif
238
239 #ifdef ARCH_X86
240 // avoid +32 for shift optimization (gcc should do that ...)
241 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
242 asm ("sarl %1, %0\n\t"
243 : "+r" (a)
244 : "ic" ((uint8_t)(-s))
245 );
246 return a;
247 }
248 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
249 asm ("shrl %1, %0\n\t"
250 : "+r" (a)
251 : "ic" ((uint8_t)(-s))
252 );
253 return a;
254 }
255 #else
256 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
257 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
258 #endif
259
260 /* bit output */
261
262 struct PutBitContext;
263
264 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
265
266 typedef struct PutBitContext {
267 uint32_t bit_buf;
268 int bit_left;
269 uint8_t *buf, *buf_ptr, *buf_end;
270 } PutBitContext;
271
272 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
273
274 // XXX XXX XXX
275 #if 0
276 int get_bit_count(PutBitContext *s); /* XXX: change function name */
277 void align_put_bits(PutBitContext *s);
278 void flush_put_bits(PutBitContext *s);
279 #endif
280
281 /* bit input */
282
283 typedef struct GetBitContext {
284 const uint8_t *buffer, *buffer_end;
285 int index;
286 int size_in_bits;
287 } GetBitContext;
288
289 static inline int get_bits_count(GetBitContext *s);
290
291 #define VLC_TYPE int16_t
292
293 typedef struct VLC {
294 int bits;
295 VLC_TYPE (*table)[2]; ///< code, bits
296 int table_size, table_allocated;
297 } VLC;
298
299 typedef struct RL_VLC_ELEM {
300 int16_t level;
301 int8_t len;
302 uint8_t run;
303 } RL_VLC_ELEM;
304
305 #ifdef ARCH_SPARC64
306 #define UNALIGNED_STORES_ARE_BAD
307 #endif
308
309 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
310 #ifdef ARCH_X86
311 # define unaligned32(a) (*(uint32_t*)(a))
312 #else
313 # ifdef __GNUC__
314 static inline uint32_t unaligned32(const void *v) {
315 struct Unaligned {
316 uint32_t i;
317 } __attribute__((packed));
318
319 return ((const struct Unaligned *) v)->i;
320 }
321 # elif defined(__DECC)
322 static inline uint32_t unaligned32(const void *v) {
323 return *(const __unaligned uint32_t *) v;
324 }
325 # else
326 static inline uint32_t unaligned32(const void *v) {
327 return *(const uint32_t *) v;
328 }
329 # endif
330 #endif //!ARCH_X86
331
332 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
333 {
334 unsigned int bit_buf;
335 int bit_left;
336
337 #ifdef STATS
338 st_out_bit_counts[st_current_index] += n;
339 #endif
340 // printf("put_bits=%d %x\n", n, value);
341 assert(n == 32 || value < (1U << n));
342
343 bit_buf = s->bit_buf;
344 bit_left = s->bit_left;
345
346 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
347 /* XXX: optimize */
348 if (n < bit_left) {
349 bit_buf = (bit_buf<<n) | value;
350 bit_left-=n;
351 } else {
352 bit_buf<<=bit_left;
353 bit_buf |= value >> (n - bit_left);
354 #ifdef UNALIGNED_STORES_ARE_BAD
355 if (3 & (int) s->buf_ptr) {
356 s->buf_ptr[0] = bit_buf >> 24;
357 s->buf_ptr[1] = bit_buf >> 16;
358 s->buf_ptr[2] = bit_buf >> 8;
359 s->buf_ptr[3] = bit_buf ;
360 } else
361 #endif
362 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
363 //printf("bitbuf = %08x\n", bit_buf);
364 s->buf_ptr+=4;
365 bit_left+=32 - n;
366 bit_buf = value;
367 }
368
369 s->bit_buf = bit_buf;
370 s->bit_left = bit_left;
371 }
372
373 static inline uint8_t* pbBufPtr(PutBitContext *s)
374 {
375 return s->buf_ptr;
376 }
377
378 /* Bitstream reader API docs:
379 name
380 abritary name which is used as prefix for the internal variables
381
382 gb
383 getbitcontext
384
385 OPEN_READER(name, gb)
386 loads gb into local variables
387
388 CLOSE_READER(name, gb)
389 stores local vars in gb
390
391 UPDATE_CACHE(name, gb)
392 refills the internal cache from the bitstream
393 after this call at least MIN_CACHE_BITS will be available,
394
395 GET_CACHE(name, gb)
396 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
397
398 SHOW_UBITS(name, gb, num)
399 will return the nest num bits
400
401 SHOW_SBITS(name, gb, num)
402 will return the nest num bits and do sign extension
403
404 SKIP_BITS(name, gb, num)
405 will skip over the next num bits
406 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
407
408 SKIP_CACHE(name, gb, num)
409 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
410
411 SKIP_COUNTER(name, gb, num)
412 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
413
414 LAST_SKIP_CACHE(name, gb, num)
415 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
416
417 LAST_SKIP_BITS(name, gb, num)
418 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
419
420 for examples see get_bits, show_bits, skip_bits, get_vlc
421 */
422
423 static inline int unaligned32_be(const void *v)
424 {
425 #ifdef CONFIG_ALIGN
426 const uint8_t *p=v;
427 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
428 #else
429 return be2me_32( unaligned32(v)); //original
430 #endif
431 }
432
433 # define MIN_CACHE_BITS 25
434
435 # define OPEN_READER(name, gb)\
436 int name##_index= (gb)->index;\
437 int name##_cache= 0;\
438
439 # define CLOSE_READER(name, gb)\
440 (gb)->index= name##_index;\
441
442 # define UPDATE_CACHE(name, gb)\
443 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
444
445 # define SKIP_CACHE(name, gb, num)\
446 name##_cache <<= (num);\
447
448 // FIXME name?
449 # define SKIP_COUNTER(name, gb, num)\
450 name##_index += (num);\
451
452 # define SKIP_BITS(name, gb, num)\
453 {\
454 SKIP_CACHE(name, gb, num)\
455 SKIP_COUNTER(name, gb, num)\
456 }\
457
458 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
459 # define LAST_SKIP_CACHE(name, gb, num) ;
460
461 # define SHOW_UBITS(name, gb, num)\
462 NEG_USR32(name##_cache, num)
463
464 # define SHOW_SBITS(name, gb, num)\
465 NEG_SSR32(name##_cache, num)
466
467 # define GET_CACHE(name, gb)\
468 ((uint32_t)name##_cache)
469
470 static inline int get_bits_count(GetBitContext *s){
471 return s->index;
472 }
473
474 /**
475 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
476 * if MSB not set it is negative
477 * @param n length in bits
478 * @author BERO
479 */
480 static inline int get_xbits(GetBitContext *s, int n){
481 register int tmp;
482 register int32_t cache;
483 OPEN_READER(re, s)
484 UPDATE_CACHE(re, s)
485 cache = GET_CACHE(re,s);
486 if ((int32_t)cache<0) { //MSB=1
487 tmp = NEG_USR32(cache,n);
488 } else {
489 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
490 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
491 tmp = - NEG_USR32(~cache,n);
492 }
493 LAST_SKIP_BITS(re, s, n)
494 CLOSE_READER(re, s)
495 return tmp;
496 }
497
498 static inline int get_sbits(GetBitContext *s, int n){
499 register int tmp;
500 OPEN_READER(re, s)
501 UPDATE_CACHE(re, s)
502 tmp= SHOW_SBITS(re, s, n);
503 LAST_SKIP_BITS(re, s, n)
504 CLOSE_READER(re, s)
505 return tmp;
506 }
507
508 /**
509 * reads 0-17 bits.
510 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
511 */
512 static inline unsigned int get_bits(GetBitContext *s, int n){
513 register int tmp;
514 OPEN_READER(re, s)
515 UPDATE_CACHE(re, s)
516 tmp= SHOW_UBITS(re, s, n);
517 LAST_SKIP_BITS(re, s, n)
518 CLOSE_READER(re, s)
519 return tmp;
520 }
521
522 unsigned int get_bits_long(GetBitContext *s, int n);
523
524 /**
525 * shows 0-17 bits.
526 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
527 */
528 static inline unsigned int show_bits(GetBitContext *s, int n){
529 register int tmp;
530 OPEN_READER(re, s)
531 UPDATE_CACHE(re, s)
532 tmp= SHOW_UBITS(re, s, n);
533 // CLOSE_READER(re, s)
534 return tmp;
535 }
536
537 unsigned int show_bits_long(GetBitContext *s, int n);
538
539 static inline void skip_bits(GetBitContext *s, int n){
540 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
541 OPEN_READER(re, s)
542 UPDATE_CACHE(re, s)
543 LAST_SKIP_BITS(re, s, n)
544 CLOSE_READER(re, s)
545 }
546
547 static inline unsigned int get_bits1(GetBitContext *s){
548 #ifdef ALT_BITSTREAM_READER
549 int index= s->index;
550 uint8_t result= s->buffer[ index>>3 ];
551 result<<= (index&0x07);
552 result>>= 8 - 1;
553 index++;
554 s->index= index;
555
556 return result;
557 #else
558 return get_bits(s, 1);
559 #endif
560 }
561
562 static inline unsigned int show_bits1(GetBitContext *s){
563 return show_bits(s, 1);
564 }
565
566 static inline void skip_bits1(GetBitContext *s){
567 skip_bits(s, 1);
568 }
569
570 void init_get_bits(GetBitContext *s,
571 const uint8_t *buffer, int buffer_size);
572
573 int check_marker(GetBitContext *s, const char *msg);
574 void align_get_bits(GetBitContext *s);
575 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
576 const void *bits, int bits_wrap, int bits_size,
577 const void *codes, int codes_wrap, int codes_size);
578 void free_vlc(VLC *vlc);
579
580 /**
581 *
582 * if the vlc code is invalid and max_depth=1 than no bits will be removed
583 * if the vlc code is invalid and max_depth>1 than the number of bits removed
584 * is undefined
585 */
586 #define GET_VLC(code, name, gb, table, bits, max_depth)\
587 {\
588 int n, index, nb_bits;\
589 \
590 index= SHOW_UBITS(name, gb, bits);\
591 code = table[index][0];\
592 n = table[index][1];\
593 \
594 if(max_depth > 1 && n < 0){\
595 LAST_SKIP_BITS(name, gb, bits)\
596 UPDATE_CACHE(name, gb)\
597 \
598 nb_bits = -n;\
599 \
600 index= SHOW_UBITS(name, gb, nb_bits) + code;\
601 code = table[index][0];\
602 n = table[index][1];\
603 if(max_depth > 2 && n < 0){\
604 LAST_SKIP_BITS(name, gb, nb_bits)\
605 UPDATE_CACHE(name, gb)\
606 \
607 nb_bits = -n;\
608 \
609 index= SHOW_UBITS(name, gb, nb_bits) + code;\
610 code = table[index][0];\
611 n = table[index][1];\
612 }\
613 }\
614 SKIP_BITS(name, gb, n)\
615 }
616
617 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
618 {\
619 int n, index, nb_bits;\
620 \
621 index= SHOW_UBITS(name, gb, bits);\
622 level = table[index].level;\
623 n = table[index].len;\
624 \
625 if(max_depth > 1 && n < 0){\
626 LAST_SKIP_BITS(name, gb, bits)\
627 UPDATE_CACHE(name, gb)\
628 \
629 nb_bits = -n;\
630 \
631 index= SHOW_UBITS(name, gb, nb_bits) + level;\
632 level = table[index].level;\
633 n = table[index].len;\
634 }\
635 run= table[index].run;\
636 SKIP_BITS(name, gb, n)\
637 }
638
639 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
640 static inline int get_vlc(GetBitContext *s, VLC *vlc)
641 {
642 int code;
643 VLC_TYPE (*table)[2]= vlc->table;
644
645 OPEN_READER(re, s)
646 UPDATE_CACHE(re, s)
647
648 GET_VLC(code, re, s, table, vlc->bits, 3)
649
650 CLOSE_READER(re, s)
651 return code;
652 }
653
654 /**
655 * parses a vlc code, faster then get_vlc()
656 * @param bits is the number of bits which will be read at once, must be
657 * identical to nb_bits in init_vlc()
658 * @param max_depth is the number of times bits bits must be readed to completly
659 * read the longest vlc code
660 * = (max_vlc_length + bits - 1) / bits
661 */
662 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
663 int bits, int max_depth)
664 {
665 int code;
666
667 OPEN_READER(re, s)
668 UPDATE_CACHE(re, s)
669
670 GET_VLC(code, re, s, table, bits, max_depth)
671
672 CLOSE_READER(re, s)
673 return code;
674 }
675
676 //#define TRACE
677
678 #ifdef TRACE
679
680 static inline void print_bin(int bits, int n){
681 int i;
682
683 for(i=n-1; i>=0; i--){
684 printf("%d", (bits>>i)&1);
685 }
686 for(i=n; i<24; i++)
687 printf(" ");
688 }
689
690 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
691 int r= get_bits(s, n);
692
693 print_bin(r, n);
694 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
695 return r;
696 }
697 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
698 int show= show_bits(s, 24);
699 int pos= get_bits_count(s);
700 int r= get_vlc2(s, table, bits, max_depth);
701 int len= get_bits_count(s) - pos;
702 int bits2= show>>(24-len);
703
704 print_bin(bits2, len);
705
706 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
707 return r;
708 }
709 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
710 int show= show_bits(s, n);
711 int r= get_xbits(s, n);
712
713 print_bin(show, n);
714 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
715 return r;
716 }
717
718 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
719 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
720 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
721 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
722 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
723
724 #define tprintf printf
725
726 #else //TRACE
727 #define tprintf(_arg...) {}
728 #endif
729
730 /* define it to include statistics code (useful only for optimizing
731 codec efficiency */
732 //#define STATS
733
734 #ifdef STATS
735
736 enum {
737 ST_UNKNOWN,
738 ST_DC,
739 ST_INTRA_AC,
740 ST_INTER_AC,
741 ST_INTRA_MB,
742 ST_INTER_MB,
743 ST_MV,
744 ST_NB,
745 };
746
747 extern int st_current_index;
748 extern unsigned int st_bit_counts[ST_NB];
749 extern unsigned int st_out_bit_counts[ST_NB];
750
751 void print_stats(void);
752 #endif
753
754 /* misc math functions */
755 extern const uint8_t ff_log2_tab[256];
756
757 static inline int av_log2(unsigned int v)
758 {
759 int n;
760
761 n = 0;
762 if (v & 0xffff0000) {
763 v >>= 16;
764 n += 16;
765 }
766 if (v & 0xff00) {
767 v >>= 8;
768 n += 8;
769 }
770 n += ff_log2_tab[v];
771
772 return n;
773 }
774
775 static inline int av_log2_16bit(unsigned int v)
776 {
777 int n;
778
779 n = 0;
780 if (v & 0xff00) {
781 v >>= 8;
782 n += 8;
783 }
784 n += ff_log2_tab[v];
785
786 return n;
787 }
788
789 /* median of 3 */
790 static inline int mid_pred(int a, int b, int c)
791 {
792 #if 0
793 int t= (a-b)&((a-b)>>31);
794 a-=t;
795 b+=t;
796 b-= (b-c)&((b-c)>>31);
797 b+= (a-b)&((a-b)>>31);
798
799 return b;
800 #else
801 if(a>b){
802 if(c>b){
803 if(c>a) b=a;
804 else b=c;
805 }
806 }else{
807 if(b>c){
808 if(c>a) b=c;
809 else b=a;
810 }
811 }
812 return b;
813 #endif
814 }
815
816 static inline int clip(int a, int amin, int amax)
817 {
818 if (a < amin)
819 return amin;
820 else if (a > amax)
821 return amax;
822 else
823 return a;
824 }
825
826 /* math */
827 extern const uint8_t ff_sqrt_tab[128];
828
829 int64_t ff_gcd(int64_t a, int64_t b);
830
831 static inline int ff_sqrt(int a)
832 {
833 int ret=0;
834 int s;
835 int ret_sq=0;
836
837 if(a<128) return ff_sqrt_tab[a];
838
839 for(s=15; s>=0; s--){
840 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
841 if(b<=a){
842 ret_sq=b;
843 ret+= 1<<s;
844 }
845 }
846 return ret;
847 }
848
849 /**
850 * converts fourcc string to int
851 */
852 static inline int ff_get_fourcc(const char *s){
853 assert( strlen(s)==4 );
854
855 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
856 }
857
858 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
859 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
860
861
862 #ifdef ARCH_X86
863 #define MASK_ABS(mask, level)\
864 asm volatile(\
865 "cdq \n\t"\
866 "xorl %1, %0 \n\t"\
867 "subl %1, %0 \n\t"\
868 : "+a" (level), "=&d" (mask)\
869 );
870 #else
871 #define MASK_ABS(mask, level)\
872 mask= level>>31;\
873 level= (level^mask)-mask;
874 #endif
875
876
877 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
878 #define COPY3_IF_LT(x,y,a,b,c,d)\
879 asm volatile (\
880 "cmpl %0, %3 \n\t"\
881 "cmovl %3, %0 \n\t"\
882 "cmovl %4, %1 \n\t"\
883 "cmovl %5, %2 \n\t"\
884 : "+r" (x), "+r" (a), "+r" (c)\
885 : "r" (y), "r" (b), "r" (d)\
886 );
887 #else
888 #define COPY3_IF_LT(x,y,a,b,c,d)\
889 if((y)<(x)){\
890 (x)=(y);\
891 (a)=(b);\
892 (c)=(d);\
893 }
894 #endif
895
896 #ifdef ARCH_X86
897 static inline long long rdtsc()
898 {
899 long long l;
900 asm volatile( "rdtsc\n\t"
901 : "=A" (l)
902 );
903 return l;
904 }
905
906 #define START_TIMER \
907 static uint64_t tsum=0;\
908 static int tcount=0;\
909 static int tskip_count=0;\
910 uint64_t tend;\
911 uint64_t tstart= rdtsc();\
912
913 #define STOP_TIMER(id) \
914 tend= rdtsc();\
915 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
916 tsum+= tend - tstart;\
917 tcount++;\
918 }else\
919 tskip_count++;\
920 if(256*256*256*64%(tcount+tskip_count)==0){\
921 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
922 }
923 #endif
924
925 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
926
927 #define CHECKED_ALLOCZ(p, size)\
928 {\
929 p= av_mallocz(size);\
930 if(p==NULL && (size)!=0){\
931 perror("malloc");\
932 goto fail;\
933 }\
934 }
935
936
937
938
939 #endif /* HAVE_AV_CONFIG_H */
940
941 #endif /* COMMON_H */