r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / libavcodec / common.h
blob966f1cd6af7d83f6d073b595dd90adf5c514b20d
1 #ifndef COMMON_H
2 #define COMMON_H
4 #define FFMPEG_VERSION_INT 0x000406
5 #define FFMPEG_VERSION "0.4.6"
7 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
8 # define CONFIG_WIN32
9 #endif
11 //#define ALT_BITSTREAM_WRITER
12 //#define ALIGNED_BITSTREAM_WRITER
14 #define ALT_BITSTREAM_READER
15 //#define LIBMPEG2_BITSTREAM_READER
16 //#define A32_BITSTREAM_READER
18 #ifdef HAVE_AV_CONFIG_H
19 /* only include the following when compiling package */
20 # include "config.h"
22 # include <stdlib.h>
23 # include <stdio.h>
24 # include <string.h>
25 # ifndef __BEOS__
26 # include <errno.h>
27 # else
28 # include "berrno.h"
29 # endif
30 # include <math.h>
32 # ifndef ENODATA
33 # define ENODATA 61
34 # endif
36 #endif /* HAVE_AV_CONFIG_H */
38 /* Suppress restrict if it was not defined in config.h. */
39 #ifndef restrict
40 # define restrict
41 #endif
43 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
44 # define always_inline __attribute__((always_inline)) inline
45 #else
46 # define always_inline inline
47 #endif
49 #ifdef CONFIG_WIN32
51 /* windows */
53 typedef unsigned short UINT16;
54 typedef signed short INT16;
55 typedef unsigned char UINT8;
56 typedef unsigned int UINT32;
57 typedef unsigned __int64 UINT64;
58 typedef signed char INT8;
59 typedef signed int INT32;
60 typedef signed __int64 INT64;
62 typedef UINT8 uint8_t;
63 typedef INT8 int8_t;
64 typedef UINT16 uint16_t;
65 typedef INT16 int16_t;
66 typedef UINT32 uint32_t;
67 typedef INT32 int32_t;
68 typedef UINT64 uint64_t;
69 typedef INT64 int64_t;
71 # ifndef __MINGW32__
72 # define INT64_C(c) (c ## i64)
73 # define UINT64_C(c) (c ## i64)
75 # define inline __inline
77 # else
78 # define INT64_C(c) (c ## LL)
79 # define UINT64_C(c) (c ## ULL)
80 # endif /* __MINGW32__ */
82 # define M_PI 3.14159265358979323846
83 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
85 # ifdef _DEBUG
86 # define DEBUG
87 # endif
89 # define snprintf _snprintf
91 #else /* CONFIG_WIN32 */
93 /* unix */
95 # include <inttypes.h>
97 # ifndef __WINE_WINDEF16_H
98 /* workaround for typedef conflict in MPlayer (wine typedefs) */
99 typedef unsigned short UINT16;
100 typedef signed short INT16;
101 # endif
103 typedef unsigned char UINT8;
104 typedef unsigned int UINT32;
105 typedef unsigned long long UINT64;
106 typedef signed char INT8;
107 typedef signed int INT32;
108 typedef signed long long INT64;
110 # ifdef HAVE_AV_CONFIG_H
111 # ifndef INT64_C
112 # define INT64_C(c) (c ## LL)
113 # define UINT64_C(c) (c ## ULL)
114 # endif
116 # ifdef USE_FASTMEMCPY
117 # include "fastmemcpy.h"
118 # endif
119 # endif /* HAVE_AV_CONFIG_H */
121 #endif /* !CONFIG_WIN32 */
123 #ifdef HAVE_AV_CONFIG_H
125 # include "bswap.h"
127 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
128 defined(__OS2__) || defined (__OpenBSD__)
129 # define MANGLE(a) "_" #a
130 # else
131 # define MANGLE(a) #a
132 # endif
134 /* debug stuff */
136 # ifndef DEBUG
137 # define NDEBUG
138 # endif
139 # include <assert.h>
141 /* dprintf macros */
142 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
144 inline void dprintf(const char* fmt,...) {}
146 # else
148 # ifdef DEBUG
149 # define dprintf(fmt,args...) printf(fmt, ## args)
150 # else
151 # define dprintf(fmt,args...)
152 # endif
154 # endif /* !CONFIG_WIN32 */
156 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
158 //rounded divison & shift
159 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
160 /* assume b>0 */
161 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
162 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
164 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
165 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
167 #ifdef ARCH_X86
168 // avoid +32 for shift optimization (gcc should do that ...)
169 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
170 asm ("sarl %1, %0\n\t"
171 : "+r" (a)
172 : "ic" ((uint8_t)(-s))
174 return a;
176 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
177 asm ("shrl %1, %0\n\t"
178 : "+r" (a)
179 : "ic" ((uint8_t)(-s))
181 return a;
183 #else
184 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
185 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
186 #endif
188 /* bit output */
190 struct PutBitContext;
192 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
194 typedef struct PutBitContext {
195 #ifdef ALT_BITSTREAM_WRITER
196 UINT8 *buf, *buf_end;
197 int index;
198 #else
199 UINT32 bit_buf;
200 int bit_left;
201 UINT8 *buf, *buf_ptr, *buf_end;
202 #endif
203 INT64 data_out_size; /* in bytes */
204 } PutBitContext;
206 void init_put_bits(PutBitContext *s,
207 UINT8 *buffer, int buffer_size,
208 void *opaque,
209 void (*write_data)(void *, UINT8 *, int));
211 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
212 void align_put_bits(PutBitContext *s);
213 void flush_put_bits(PutBitContext *s);
214 void put_string(PutBitContext * pbc, char *s);
216 /* bit input */
218 typedef struct GetBitContext {
219 UINT8 *buffer, *buffer_end;
220 #ifdef ALT_BITSTREAM_READER
221 int index;
222 #elif defined LIBMPEG2_BITSTREAM_READER
223 UINT8 *buffer_ptr;
224 UINT32 cache;
225 int bit_count;
226 #elif defined A32_BITSTREAM_READER
227 UINT32 *buffer_ptr;
228 UINT32 cache0;
229 UINT32 cache1;
230 int bit_count;
231 #endif
232 int size;
233 } GetBitContext;
235 static inline int get_bits_count(GetBitContext *s);
237 #define VLC_TYPE INT16
239 typedef struct VLC {
240 int bits;
241 VLC_TYPE (*table)[2]; // code, bits
242 int table_size, table_allocated;
243 } VLC;
245 typedef struct RL_VLC_ELEM {
246 int16_t level;
247 int8_t len;
248 uint8_t run;
249 } RL_VLC_ELEM;
251 #ifdef ARCH_SPARC64
252 #define UNALIGNED_STORES_ARE_BAD
253 #endif
255 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
256 #ifdef ARCH_X86
257 # define unaligned32(a) (*(UINT32*)(a))
258 #else
259 # ifdef __GNUC__
260 static inline uint32_t unaligned32(const void *v) {
261 struct Unaligned {
262 uint32_t i;
263 } __attribute__((packed));
265 return ((const struct Unaligned *) v)->i;
267 # elif defined(__DECC)
268 static inline uint32_t unaligned32(const void *v) {
269 return *(const __unaligned uint32_t *) v;
271 # else
272 static inline uint32_t unaligned32(const void *v) {
273 return *(const uint32_t *) v;
275 # endif
276 #endif //!ARCH_X86
278 #ifndef ALT_BITSTREAM_WRITER
279 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
281 unsigned int bit_buf;
282 int bit_left;
284 #ifdef STATS
285 st_out_bit_counts[st_current_index] += n;
286 #endif
287 // printf("put_bits=%d %x\n", n, value);
288 assert(n == 32 || value < (1U << n));
290 bit_buf = s->bit_buf;
291 bit_left = s->bit_left;
293 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
294 /* XXX: optimize */
295 if (n < bit_left) {
296 bit_buf = (bit_buf<<n) | value;
297 bit_left-=n;
298 } else {
299 bit_buf<<=bit_left;
300 bit_buf |= value >> (n - bit_left);
301 #ifdef UNALIGNED_STORES_ARE_BAD
302 if (3 & (int) s->buf_ptr) {
303 s->buf_ptr[0] = bit_buf >> 24;
304 s->buf_ptr[1] = bit_buf >> 16;
305 s->buf_ptr[2] = bit_buf >> 8;
306 s->buf_ptr[3] = bit_buf ;
307 } else
308 #endif
309 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
310 //printf("bitbuf = %08x\n", bit_buf);
311 s->buf_ptr+=4;
312 bit_left+=32 - n;
313 bit_buf = value;
316 s->bit_buf = bit_buf;
317 s->bit_left = bit_left;
319 #endif
322 #ifdef ALT_BITSTREAM_WRITER
323 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
325 # ifdef ALIGNED_BITSTREAM_WRITER
326 # ifdef ARCH_X86
327 asm volatile(
328 "movl %0, %%ecx \n\t"
329 "xorl %%eax, %%eax \n\t"
330 "shrdl %%cl, %1, %%eax \n\t"
331 "shrl %%cl, %1 \n\t"
332 "movl %0, %%ecx \n\t"
333 "shrl $3, %%ecx \n\t"
334 "andl $0xFFFFFFFC, %%ecx \n\t"
335 "bswapl %1 \n\t"
336 "orl %1, (%2, %%ecx) \n\t"
337 "bswapl %%eax \n\t"
338 "addl %3, %0 \n\t"
339 "movl %%eax, 4(%2, %%ecx) \n\t"
340 : "=&r" (s->index), "=&r" (value)
341 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
342 : "%eax", "%ecx"
344 # else
345 int index= s->index;
346 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
348 value<<= 32-n;
350 ptr[0] |= be2me_32(value>>(index&31));
351 ptr[1] = be2me_32(value<<(32-(index&31)));
352 //if(n>24) printf("%d %d\n", n, value);
353 index+= n;
354 s->index= index;
355 # endif
356 # else //ALIGNED_BITSTREAM_WRITER
357 # ifdef ARCH_X86
358 asm volatile(
359 "movl $7, %%ecx \n\t"
360 "andl %0, %%ecx \n\t"
361 "addl %3, %%ecx \n\t"
362 "negl %%ecx \n\t"
363 "shll %%cl, %1 \n\t"
364 "bswapl %1 \n\t"
365 "movl %0, %%ecx \n\t"
366 "shrl $3, %%ecx \n\t"
367 "orl %1, (%%ecx, %2) \n\t"
368 "addl %3, %0 \n\t"
369 "movl $0, 4(%%ecx, %2) \n\t"
370 : "=&r" (s->index), "=&r" (value)
371 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
372 : "%ecx"
374 # else
375 int index= s->index;
376 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
378 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
379 ptr[1] = 0;
380 //if(n>24) printf("%d %d\n", n, value);
381 index+= n;
382 s->index= index;
383 # endif
384 # endif //!ALIGNED_BITSTREAM_WRITER
386 #endif
389 static inline uint8_t* pbBufPtr(PutBitContext *s)
391 #ifdef ALT_BITSTREAM_WRITER
392 return s->buf + (s->index>>3);
393 #else
394 return s->buf_ptr;
395 #endif
398 /* Bitstream reader API docs:
399 name
400 abritary name which is used as prefix for the internal variables
403 getbitcontext
405 OPEN_READER(name, gb)
406 loads gb into local variables
408 CLOSE_READER(name, gb)
409 stores local vars in gb
411 UPDATE_CACHE(name, gb)
412 refills the internal cache from the bitstream
413 after this call at least MIN_CACHE_BITS will be available,
415 GET_CACHE(name, gb)
416 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
418 SHOW_UBITS(name, gb, num)
419 will return the nest num bits
421 SHOW_SBITS(name, gb, num)
422 will return the nest num bits and do sign extension
424 SKIP_BITS(name, gb, num)
425 will skip over the next num bits
426 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
428 SKIP_CACHE(name, gb, num)
429 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
431 SKIP_COUNTER(name, gb, num)
432 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
434 LAST_SKIP_CACHE(name, gb, num)
435 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
437 LAST_SKIP_BITS(name, gb, num)
438 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
440 for examples see get_bits, show_bits, skip_bits, get_vlc
443 #ifdef ALT_BITSTREAM_READER
444 # define MIN_CACHE_BITS 25
446 # define OPEN_READER(name, gb)\
447 int name##_index= (gb)->index;\
448 int name##_cache= 0;\
450 # define CLOSE_READER(name, gb)\
451 (gb)->index= name##_index;\
453 # define UPDATE_CACHE(name, gb)\
454 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
456 # define SKIP_CACHE(name, gb, num)\
457 name##_cache <<= (num);\
459 // FIXME name?
460 # define SKIP_COUNTER(name, gb, num)\
461 name##_index += (num);\
463 # define SKIP_BITS(name, gb, num)\
465 SKIP_CACHE(name, gb, num)\
466 SKIP_COUNTER(name, gb, num)\
469 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
470 # define LAST_SKIP_CACHE(name, gb, num) ;
472 # define SHOW_UBITS(name, gb, num)\
473 NEG_USR32(name##_cache, num)
475 # define SHOW_SBITS(name, gb, num)\
476 NEG_SSR32(name##_cache, num)
478 # define GET_CACHE(name, gb)\
479 ((uint32_t)name##_cache)
481 static inline int get_bits_count(GetBitContext *s){
482 return s->index;
484 #elif defined LIBMPEG2_BITSTREAM_READER
485 //libmpeg2 like reader
487 # define MIN_CACHE_BITS 16
489 # define OPEN_READER(name, gb)\
490 int name##_bit_count=(gb)->bit_count;\
491 int name##_cache= (gb)->cache;\
492 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
494 # define CLOSE_READER(name, gb)\
495 (gb)->bit_count= name##_bit_count;\
496 (gb)->cache= name##_cache;\
497 (gb)->buffer_ptr= name##_buffer_ptr;\
499 # define UPDATE_CACHE(name, gb)\
500 if(name##_bit_count > 0){\
501 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
502 name##_buffer_ptr+=2;\
503 name##_bit_count-= 16;\
506 # define SKIP_CACHE(name, gb, num)\
507 name##_cache <<= (num);\
509 # define SKIP_COUNTER(name, gb, num)\
510 name##_bit_count += (num);\
512 # define SKIP_BITS(name, gb, num)\
514 SKIP_CACHE(name, gb, num)\
515 SKIP_COUNTER(name, gb, num)\
518 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
519 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
521 # define SHOW_UBITS(name, gb, num)\
522 NEG_USR32(name##_cache, num)
524 # define SHOW_SBITS(name, gb, num)\
525 NEG_SSR32(name##_cache, num)
527 # define GET_CACHE(name, gb)\
528 ((uint32_t)name##_cache)
530 static inline int get_bits_count(GetBitContext *s){
531 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
534 #elif defined A32_BITSTREAM_READER
536 # define MIN_CACHE_BITS 32
538 # define OPEN_READER(name, gb)\
539 int name##_bit_count=(gb)->bit_count;\
540 uint32_t name##_cache0= (gb)->cache0;\
541 uint32_t name##_cache1= (gb)->cache1;\
542 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
544 # define CLOSE_READER(name, gb)\
545 (gb)->bit_count= name##_bit_count;\
546 (gb)->cache0= name##_cache0;\
547 (gb)->cache1= name##_cache1;\
548 (gb)->buffer_ptr= name##_buffer_ptr;\
550 # define UPDATE_CACHE(name, gb)\
551 if(name##_bit_count > 0){\
552 const uint32_t next= be2me_32( *name##_buffer_ptr );\
553 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
554 name##_cache1 |= next<<name##_bit_count;\
555 name##_buffer_ptr++;\
556 name##_bit_count-= 32;\
559 #ifdef ARCH_X86
560 # define SKIP_CACHE(name, gb, num)\
561 asm(\
562 "shldl %2, %1, %0 \n\t"\
563 "shll %2, %1 \n\t"\
564 : "+r" (name##_cache0), "+r" (name##_cache1)\
565 : "Ic" ((uint8_t)num)\
567 #else
568 # define SKIP_CACHE(name, gb, num)\
569 name##_cache0 <<= (num);\
570 name##_cache0 |= NEG_USR32(name##_cache1,num);\
571 name##_cache1 <<= (num);
572 #endif
574 # define SKIP_COUNTER(name, gb, num)\
575 name##_bit_count += (num);\
577 # define SKIP_BITS(name, gb, num)\
579 SKIP_CACHE(name, gb, num)\
580 SKIP_COUNTER(name, gb, num)\
583 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
584 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
586 # define SHOW_UBITS(name, gb, num)\
587 NEG_USR32(name##_cache0, num)
589 # define SHOW_SBITS(name, gb, num)\
590 NEG_SSR32(name##_cache0, num)
592 # define GET_CACHE(name, gb)\
593 (name##_cache0)
595 static inline int get_bits_count(GetBitContext *s){
596 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
599 #endif
601 static inline unsigned int get_bits(GetBitContext *s, int n){
602 register int tmp;
603 OPEN_READER(re, s)
604 UPDATE_CACHE(re, s)
605 tmp= SHOW_UBITS(re, s, n);
606 LAST_SKIP_BITS(re, s, n)
607 CLOSE_READER(re, s)
608 return tmp;
611 static inline unsigned int show_bits(GetBitContext *s, int n){
612 register int tmp;
613 OPEN_READER(re, s)
614 UPDATE_CACHE(re, s)
615 tmp= SHOW_UBITS(re, s, n);
616 // CLOSE_READER(re, s)
617 return tmp;
620 static inline void skip_bits(GetBitContext *s, int n){
621 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
622 OPEN_READER(re, s)
623 UPDATE_CACHE(re, s)
624 LAST_SKIP_BITS(re, s, n)
625 CLOSE_READER(re, s)
628 static inline unsigned int get_bits1(GetBitContext *s){
629 #ifdef ALT_BITSTREAM_READER
630 int index= s->index;
631 uint8_t result= s->buffer[ index>>3 ];
632 result<<= (index&0x07);
633 result>>= 8 - 1;
634 index++;
635 s->index= index;
637 return result;
638 #else
639 return get_bits(s, 1);
640 #endif
643 static inline unsigned int show_bits1(GetBitContext *s){
644 return show_bits(s, 1);
647 static inline void skip_bits1(GetBitContext *s){
648 skip_bits(s, 1);
651 void init_get_bits(GetBitContext *s,
652 UINT8 *buffer, int buffer_size);
654 int check_marker(GetBitContext *s, const char *msg);
655 void align_get_bits(GetBitContext *s);
656 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
657 const void *bits, int bits_wrap, int bits_size,
658 const void *codes, int codes_wrap, int codes_size);
659 void free_vlc(VLC *vlc);
661 #define GET_VLC(code, name, gb, table, bits, max_depth)\
663 int n, index, nb_bits;\
665 index= SHOW_UBITS(name, gb, bits);\
666 code = table[index][0];\
667 n = table[index][1];\
669 if(max_depth > 1 && n < 0){\
670 LAST_SKIP_BITS(name, gb, bits)\
671 UPDATE_CACHE(name, gb)\
673 nb_bits = -n;\
675 index= SHOW_UBITS(name, gb, nb_bits) + code;\
676 code = table[index][0];\
677 n = table[index][1];\
678 if(max_depth > 2 && n < 0){\
679 LAST_SKIP_BITS(name, gb, nb_bits)\
680 UPDATE_CACHE(name, gb)\
682 nb_bits = -n;\
684 index= SHOW_UBITS(name, gb, nb_bits) + code;\
685 code = table[index][0];\
686 n = table[index][1];\
689 SKIP_BITS(name, gb, n)\
692 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
694 int n, index, nb_bits;\
696 index= SHOW_UBITS(name, gb, bits);\
697 level = table[index].level;\
698 n = table[index].len;\
700 if(max_depth > 1 && n < 0){\
701 LAST_SKIP_BITS(name, gb, bits)\
702 UPDATE_CACHE(name, gb)\
704 nb_bits = -n;\
706 index= SHOW_UBITS(name, gb, nb_bits) + level;\
707 level = table[index].level;\
708 n = table[index].len;\
710 run= table[index].run;\
711 SKIP_BITS(name, gb, n)\
714 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
715 static inline int get_vlc(GetBitContext *s, VLC *vlc)
717 int code;
718 VLC_TYPE (*table)[2]= vlc->table;
720 OPEN_READER(re, s)
721 UPDATE_CACHE(re, s)
723 GET_VLC(code, re, s, table, vlc->bits, 3)
725 CLOSE_READER(re, s)
726 return code;
729 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
730 int bits, int max_depth)
732 int code;
734 OPEN_READER(re, s)
735 UPDATE_CACHE(re, s)
737 GET_VLC(code, re, s, table, bits, max_depth)
739 CLOSE_READER(re, s)
740 return code;
744 /* define it to include statistics code (useful only for optimizing
745 codec efficiency */
746 //#define STATS
748 #ifdef STATS
750 enum {
751 ST_UNKNOWN,
752 ST_DC,
753 ST_INTRA_AC,
754 ST_INTER_AC,
755 ST_INTRA_MB,
756 ST_INTER_MB,
757 ST_MV,
758 ST_NB,
761 extern int st_current_index;
762 extern unsigned int st_bit_counts[ST_NB];
763 extern unsigned int st_out_bit_counts[ST_NB];
765 void print_stats(void);
766 #endif
768 /* misc math functions */
770 static inline int av_log2(unsigned int v)
772 int n;
774 n = 0;
775 if (v & 0xffff0000) {
776 v >>= 16;
777 n += 16;
779 if (v & 0xff00) {
780 v >>= 8;
781 n += 8;
783 if (v & 0xf0) {
784 v >>= 4;
785 n += 4;
787 if (v & 0xc) {
788 v >>= 2;
789 n += 2;
791 if (v & 0x2) {
792 n++;
794 return n;
797 /* median of 3 */
798 static inline int mid_pred(int a, int b, int c)
800 int vmin, vmax;
801 vmax = vmin = a;
802 if (b < vmin)
803 vmin = b;
804 else
805 vmax = b;
807 if (c < vmin)
808 vmin = c;
809 else if (c > vmax)
810 vmax = c;
812 return a + b + c - vmin - vmax;
815 static inline int clip(int a, int amin, int amax)
817 if (a < amin)
818 return amin;
819 else if (a > amax)
820 return amax;
821 else
822 return a;
825 /* math */
826 extern const UINT8 ff_sqrt_tab[128];
828 int ff_gcd(int a, int b);
830 static inline int ff_sqrt(int a)
832 int ret=0;
833 int s;
834 int ret_sq=0;
836 if(a<128) return ff_sqrt_tab[a];
838 for(s=15; s>=0; s--){
839 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
840 if(b<=a){
841 ret_sq=b;
842 ret+= 1<<s;
845 return ret;
849 * converts fourcc string to int
851 static inline int ff_get_fourcc(const char *s){
852 assert( strlen(s)==4 );
854 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
857 void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
860 #ifdef ARCH_X86
861 #define MASK_ABS(mask, level)\
862 asm volatile(\
863 "cdq \n\t"\
864 "xorl %1, %0 \n\t"\
865 "subl %1, %0 \n\t"\
866 : "+a" (level), "=&d" (mask)\
868 #else
869 #define MASK_ABS(mask, level)\
870 mask= level>>31;\
871 level= (level^mask)-mask;
872 #endif
875 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
876 #define COPY3_IF_LT(x,y,a,b,c,d)\
877 asm volatile (\
878 "cmpl %0, %3 \n\t"\
879 "cmovl %3, %0 \n\t"\
880 "cmovl %4, %1 \n\t"\
881 "cmovl %5, %2 \n\t"\
882 : "+r" (x), "+r" (a), "+r" (c)\
883 : "r" (y), "r" (b), "r" (d)\
885 #else
886 #define COPY3_IF_LT(x,y,a,b,c,d)\
887 if((y)<(x)){\
888 (x)=(y);\
889 (a)=(b);\
890 (c)=(d);\
892 #endif
894 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
896 #endif /* HAVE_AV_CONFIG_H */
898 #endif /* COMMON_H */