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