]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/common.h
(f)printf() is disallowed in libavcodec, compilation will fail now if its used, excep...
[frescor/ffmpeg.git] / 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 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10 #    define CONFIG_WIN32
11 #endif
12
13 //#define ALT_BITSTREAM_WRITER
14 //#define ALIGNED_BITSTREAM_WRITER
15
16 #define ALT_BITSTREAM_READER
17 //#define LIBMPEG2_BITSTREAM_READER
18 //#define A32_BITSTREAM_READER
19 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
20
21 #ifndef M_PI
22 #define M_PI    3.14159265358979323846
23 #endif
24
25 #ifdef HAVE_AV_CONFIG_H
26 /* only include the following when compiling package */
27 #    include "config.h"
28
29 #    include <stdlib.h>
30 #    include <stdio.h>
31 #    include <string.h>
32 #    include <ctype.h>
33 #    ifndef __BEOS__
34 #        include <errno.h>
35 #    else
36 #        include "berrno.h"
37 #    endif
38 #    include <math.h>
39
40 #    ifndef ENODATA
41 #        define ENODATA  61
42 #    endif
43
44 #include <stddef.h>
45 #ifndef offsetof
46 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
47 #endif
48
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)
63
64 struct AVOption;
65 #ifdef HAVE_MMX
66 extern const struct AVOption avoptions_common[3 + 5];
67 #else
68 extern const struct AVOption avoptions_common[3];
69 #endif
70 extern const struct AVOption avoptions_workaround_bug[11];
71
72 #endif /* HAVE_AV_CONFIG_H */
73
74 /* Suppress restrict if it was not defined in config.h.  */
75 #ifndef restrict
76 #    define restrict
77 #endif
78
79 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80 #    define always_inline __attribute__((always_inline)) inline
81 #else
82 #    define always_inline inline
83 #endif
84
85 #ifndef EMULATE_INTTYPES
86 #   include <inttypes.h>
87 #else
88     typedef signed char  int8_t;
89     typedef signed short int16_t;
90     typedef signed int   int32_t;
91     typedef unsigned char  uint8_t;
92     typedef unsigned short uint16_t;
93     typedef unsigned int   uint32_t;
94
95 #   ifdef CONFIG_WIN32
96         typedef signed __int64   int64_t;
97         typedef unsigned __int64 uint64_t;
98 #   else /* other OS */
99         typedef signed long long   int64_t;
100         typedef unsigned long long uint64_t;
101 #   endif /* other OS */
102 #endif /* HAVE_INTTYPES_H */
103
104 #ifndef INT64_MAX
105 #define INT64_MAX 9223372036854775807LL
106 #endif
107
108 #ifdef EMULATE_FAST_INT
109 /* note that we don't emulate 64bit ints */
110 typedef signed char int_fast8_t;
111 typedef signed int  int_fast16_t;
112 typedef signed int  int_fast32_t;
113 typedef unsigned char uint_fast8_t;
114 typedef unsigned int  uint_fast16_t;
115 typedef unsigned int  uint_fast32_t;
116 #endif
117
118 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
119 static inline float floorf(float f) { 
120     return floor(f); 
121 }
122 #endif
123
124 #ifdef CONFIG_WIN32
125
126 /* windows */
127
128 #    ifndef __MINGW32__
129 #        define int64_t_C(c)     (c ## i64)
130 #        define uint64_t_C(c)    (c ## i64)
131
132 #    ifdef HAVE_AV_CONFIG_H
133 #            define inline __inline
134 #    endif
135
136 #    else
137 #        define int64_t_C(c)     (c ## LL)
138 #        define uint64_t_C(c)    (c ## ULL)
139 #    endif /* __MINGW32__ */
140
141 #    ifdef HAVE_AV_CONFIG_H
142 #        ifdef _DEBUG
143 #            define DEBUG
144 #        endif
145
146 #        define snprintf _snprintf
147 #        define vsnprintf _vsnprintf
148 #    endif
149
150 /* CONFIG_WIN32 end */
151 #elif defined (CONFIG_OS2)
152 /* OS/2 EMX */
153
154 #ifndef int64_t_C
155 #define int64_t_C(c)     (c ## LL)
156 #define uint64_t_C(c)    (c ## ULL)
157 #endif
158
159 #ifdef HAVE_AV_CONFIG_H
160
161 #ifdef USE_FASTMEMCPY
162 #include "fastmemcpy.h"
163 #endif
164
165 #include <float.h>
166
167 #endif /* HAVE_AV_CONFIG_H */
168
169 /* CONFIG_OS2 end */
170 #else
171
172 /* unix */
173
174 #ifndef int64_t_C
175 #define int64_t_C(c)     (c ## LL)
176 #define uint64_t_C(c)    (c ## ULL)
177 #endif
178
179 #ifdef HAVE_AV_CONFIG_H
180
181 #        ifdef USE_FASTMEMCPY
182 #            include "fastmemcpy.h"
183 #        endif
184 #    endif /* HAVE_AV_CONFIG_H */
185
186 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
187
188 #ifdef HAVE_AV_CONFIG_H
189
190 #    include "bswap.h"
191
192 #    if defined(__MINGW32__) || defined(__CYGWIN__) || \
193         defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
194 #        define MANGLE(a) "_" #a
195 #    else
196 #        define MANGLE(a) #a
197 #    endif
198
199 /* debug stuff */
200
201 #    ifndef DEBUG
202 #        define NDEBUG
203 #    endif
204 #    include <assert.h>
205
206 /* dprintf macros */
207 #    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
208
209 inline void dprintf(const char* fmt,...) {}
210
211 #    else
212
213 #        ifdef DEBUG
214 #            define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
215 #        else
216 #            define dprintf(fmt,...)
217 #        endif
218
219 #    endif /* !CONFIG_WIN32 */
220
221 #    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
222
223 //rounded divison & shift
224 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
225 /* assume b>0 */
226 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
227 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
228
229 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
230 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
231
232 extern const uint32_t inverse[256];
233
234 #ifdef ARCH_X86
235 #    define FASTDIV(a,b) \
236     ({\
237         int ret,dmy;\
238         asm volatile(\
239             "mull %3"\
240             :"=d"(ret),"=a"(dmy)\
241             :"1"(a),"g"(inverse[b])\
242             );\
243         ret;\
244     })
245 #elif defined(CONFIG_FASTDIV)
246 #    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
247 #else
248 #    define FASTDIV(a,b)   ((a)/(b))
249 #endif
250  
251 #ifdef ARCH_X86
252 // avoid +32 for shift optimization (gcc should do that ...)
253 static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
254     asm ("sarl %1, %0\n\t"
255          : "+r" (a)
256          : "ic" ((uint8_t)(-s))
257     );
258     return a;
259 }
260 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
261     asm ("shrl %1, %0\n\t"
262          : "+r" (a)
263          : "ic" ((uint8_t)(-s))
264     );
265     return a;
266 }
267 #else
268 #    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
269 #    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
270 #endif
271
272 /* bit output */
273
274 struct PutBitContext;
275
276 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
277
278 typedef struct PutBitContext {
279 #ifdef ALT_BITSTREAM_WRITER
280     uint8_t *buf, *buf_end;
281     int index;
282 #else
283     uint32_t bit_buf;
284     int bit_left;
285     uint8_t *buf, *buf_ptr, *buf_end;
286 #endif
287 } PutBitContext;
288
289 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
290
291 int put_bits_count(PutBitContext *s);
292 void align_put_bits(PutBitContext *s);
293 void flush_put_bits(PutBitContext *s);
294 void put_string(PutBitContext * pbc, char *s, int put_zero);
295
296 /* bit input */
297
298 typedef struct GetBitContext {
299     const uint8_t *buffer, *buffer_end;
300 #ifdef ALT_BITSTREAM_READER
301     int index;
302 #elif defined LIBMPEG2_BITSTREAM_READER
303     uint8_t *buffer_ptr;
304     uint32_t cache;
305     int bit_count;
306 #elif defined A32_BITSTREAM_READER
307     uint32_t *buffer_ptr;
308     uint32_t cache0;
309     uint32_t cache1;
310     int bit_count;
311 #endif
312     int size_in_bits;
313 } GetBitContext;
314
315 static inline int get_bits_count(GetBitContext *s);
316
317 #define VLC_TYPE int16_t
318
319 typedef struct VLC {
320     int bits;
321     VLC_TYPE (*table)[2]; ///< code, bits
322     int table_size, table_allocated;
323 } VLC;
324
325 typedef struct RL_VLC_ELEM {
326     int16_t level;
327     int8_t len;
328     uint8_t run;
329 } RL_VLC_ELEM;
330
331 #ifdef ARCH_SPARC64
332 #define UNALIGNED_STORES_ARE_BAD
333 #endif
334
335 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
336 #ifdef ARCH_X86
337 #    define unaligned32(a) (*(uint32_t*)(a))
338 #else
339 #    ifdef __GNUC__
340 static inline uint32_t unaligned32(const void *v) {
341     struct Unaligned {
342         uint32_t i;
343     } __attribute__((packed));
344
345     return ((const struct Unaligned *) v)->i;
346 }
347 #    elif defined(__DECC)
348 static inline uint32_t unaligned32(const void *v) {
349     return *(const __unaligned uint32_t *) v;
350 }
351 #    else
352 static inline uint32_t unaligned32(const void *v) {
353     return *(const uint32_t *) v;
354 }
355 #    endif
356 #endif //!ARCH_X86
357
358 #ifndef ALT_BITSTREAM_WRITER
359 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
360 {
361     unsigned int bit_buf;
362     int bit_left;
363
364 #ifdef STATS
365     st_out_bit_counts[st_current_index] += n;
366 #endif
367     //    printf("put_bits=%d %x\n", n, value);
368     assert(n == 32 || value < (1U << n));
369     
370     bit_buf = s->bit_buf;
371     bit_left = s->bit_left;
372
373     //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
374     /* XXX: optimize */
375     if (n < bit_left) {
376         bit_buf = (bit_buf<<n) | value;
377         bit_left-=n;
378     } else {
379         bit_buf<<=bit_left;
380         bit_buf |= value >> (n - bit_left);
381 #ifdef UNALIGNED_STORES_ARE_BAD
382         if (3 & (int) s->buf_ptr) {
383             s->buf_ptr[0] = bit_buf >> 24;
384             s->buf_ptr[1] = bit_buf >> 16;
385             s->buf_ptr[2] = bit_buf >>  8;
386             s->buf_ptr[3] = bit_buf      ;
387         } else
388 #endif
389         *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
390         //printf("bitbuf = %08x\n", bit_buf);
391         s->buf_ptr+=4;
392         bit_left+=32 - n;
393         bit_buf = value;
394     }
395
396     s->bit_buf = bit_buf;
397     s->bit_left = bit_left;
398 }
399 #endif
400
401
402 #ifdef ALT_BITSTREAM_WRITER
403 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
404 {
405 #    ifdef ALIGNED_BITSTREAM_WRITER
406 #        ifdef ARCH_X86
407     asm volatile(
408         "movl %0, %%ecx                 \n\t"
409         "xorl %%eax, %%eax              \n\t"
410         "shrdl %%cl, %1, %%eax          \n\t"
411         "shrl %%cl, %1                  \n\t"
412         "movl %0, %%ecx                 \n\t"
413         "shrl $3, %%ecx                 \n\t"
414         "andl $0xFFFFFFFC, %%ecx        \n\t"
415         "bswapl %1                      \n\t"
416         "orl %1, (%2, %%ecx)            \n\t"
417         "bswapl %%eax                   \n\t"
418         "addl %3, %0                    \n\t"
419         "movl %%eax, 4(%2, %%ecx)       \n\t"
420         : "=&r" (s->index), "=&r" (value)
421         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
422         : "%eax", "%ecx"
423     );
424 #        else
425     int index= s->index;
426     uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
427     
428     value<<= 32-n; 
429     
430     ptr[0] |= be2me_32(value>>(index&31));
431     ptr[1]  = be2me_32(value<<(32-(index&31)));
432 //if(n>24) printf("%d %d\n", n, value);
433     index+= n;
434     s->index= index;
435 #        endif
436 #    else //ALIGNED_BITSTREAM_WRITER
437 #        ifdef ARCH_X86
438     asm volatile(
439         "movl $7, %%ecx                 \n\t"
440         "andl %0, %%ecx                 \n\t"
441         "addl %3, %%ecx                 \n\t"
442         "negl %%ecx                     \n\t"
443         "shll %%cl, %1                  \n\t"
444         "bswapl %1                      \n\t"
445         "movl %0, %%ecx                 \n\t"
446         "shrl $3, %%ecx                 \n\t"
447         "orl %1, (%%ecx, %2)            \n\t"
448         "addl %3, %0                    \n\t"
449         "movl $0, 4(%%ecx, %2)          \n\t"
450         : "=&r" (s->index), "=&r" (value)
451         : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
452         : "%ecx"
453     );
454 #        else
455     int index= s->index;
456     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
457     
458     ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
459     ptr[1] = 0;
460 //if(n>24) printf("%d %d\n", n, value);
461     index+= n;
462     s->index= index;
463 #        endif
464 #    endif //!ALIGNED_BITSTREAM_WRITER
465 }
466 #endif
467
468
469 static inline uint8_t* pbBufPtr(PutBitContext *s)
470 {
471 #ifdef ALT_BITSTREAM_WRITER
472         return s->buf + (s->index>>3);
473 #else
474         return s->buf_ptr;
475 #endif
476 }
477
478 /**
479  *
480  * PutBitContext must be flushed & aligned to a byte boundary before calling this.
481  */
482 static inline void skip_put_bytes(PutBitContext *s, int n){
483         assert((put_bits_count(s)&7)==0);
484 #ifdef ALT_BITSTREAM_WRITER
485         FIXME may need some cleaning of the buffer
486         s->index += n<<3;
487 #else
488         assert(s->bit_left==32);
489         s->buf_ptr += n;
490 #endif    
491 }
492
493 /**
494  * Changes the end of the buffer.
495  */
496 static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
497     s->buf_end= s->buf + size;
498 }
499
500 /* Bitstream reader API docs:
501 name
502     abritary name which is used as prefix for the internal variables
503
504 gb
505     getbitcontext
506
507 OPEN_READER(name, gb)
508     loads gb into local variables
509
510 CLOSE_READER(name, gb)
511     stores local vars in gb
512
513 UPDATE_CACHE(name, gb)
514     refills the internal cache from the bitstream
515     after this call at least MIN_CACHE_BITS will be available,
516
517 GET_CACHE(name, gb)
518     will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
519
520 SHOW_UBITS(name, gb, num)
521     will return the nest num bits
522
523 SHOW_SBITS(name, gb, num)
524     will return the nest num bits and do sign extension
525
526 SKIP_BITS(name, gb, num)
527     will skip over the next num bits
528     note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
529
530 SKIP_CACHE(name, gb, num)
531     will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
532
533 SKIP_COUNTER(name, gb, num)
534     will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
535
536 LAST_SKIP_CACHE(name, gb, num)
537     will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
538
539 LAST_SKIP_BITS(name, gb, num)
540     is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
541
542 for examples see get_bits, show_bits, skip_bits, get_vlc
543 */
544
545 static inline int unaligned32_be(const void *v)
546 {
547 #ifdef CONFIG_ALIGN
548         const uint8_t *p=v;
549         return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
550 #else
551         return be2me_32( unaligned32(v)); //original
552 #endif
553 }
554
555 #ifdef ALT_BITSTREAM_READER
556 #   define MIN_CACHE_BITS 25
557
558 #   define OPEN_READER(name, gb)\
559         int name##_index= (gb)->index;\
560         int name##_cache= 0;\
561
562 #   define CLOSE_READER(name, gb)\
563         (gb)->index= name##_index;\
564
565 #   define UPDATE_CACHE(name, gb)\
566         name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
567
568 #   define SKIP_CACHE(name, gb, num)\
569         name##_cache <<= (num);\
570
571 // FIXME name?
572 #   define SKIP_COUNTER(name, gb, num)\
573         name##_index += (num);\
574
575 #   define SKIP_BITS(name, gb, num)\
576         {\
577             SKIP_CACHE(name, gb, num)\
578             SKIP_COUNTER(name, gb, num)\
579         }\
580
581 #   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
582 #   define LAST_SKIP_CACHE(name, gb, num) ;
583
584 #   define SHOW_UBITS(name, gb, num)\
585         NEG_USR32(name##_cache, num)
586
587 #   define SHOW_SBITS(name, gb, num)\
588         NEG_SSR32(name##_cache, num)
589
590 #   define GET_CACHE(name, gb)\
591         ((uint32_t)name##_cache)
592
593 static inline int get_bits_count(GetBitContext *s){
594     return s->index;
595 }
596 #elif defined LIBMPEG2_BITSTREAM_READER
597 //libmpeg2 like reader
598
599 #   define MIN_CACHE_BITS 17
600
601 #   define OPEN_READER(name, gb)\
602         int name##_bit_count=(gb)->bit_count;\
603         int name##_cache= (gb)->cache;\
604         uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
605
606 #   define CLOSE_READER(name, gb)\
607         (gb)->bit_count= name##_bit_count;\
608         (gb)->cache= name##_cache;\
609         (gb)->buffer_ptr= name##_buffer_ptr;\
610
611 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
612
613 #   define UPDATE_CACHE(name, gb)\
614     if(name##_bit_count >= 0){\
615         name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
616         ((uint16_t*)name##_buffer_ptr)++;\
617         name##_bit_count-= 16;\
618     }\
619
620 #else
621
622 #   define UPDATE_CACHE(name, gb)\
623     if(name##_bit_count >= 0){\
624         name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
625         name##_buffer_ptr+=2;\
626         name##_bit_count-= 16;\
627     }\
628
629 #endif
630
631 #   define SKIP_CACHE(name, gb, num)\
632         name##_cache <<= (num);\
633
634 #   define SKIP_COUNTER(name, gb, num)\
635         name##_bit_count += (num);\
636
637 #   define SKIP_BITS(name, gb, num)\
638         {\
639             SKIP_CACHE(name, gb, num)\
640             SKIP_COUNTER(name, gb, num)\
641         }\
642
643 #   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
644 #   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
645
646 #   define SHOW_UBITS(name, gb, num)\
647         NEG_USR32(name##_cache, num)
648
649 #   define SHOW_SBITS(name, gb, num)\
650         NEG_SSR32(name##_cache, num)
651
652 #   define GET_CACHE(name, gb)\
653         ((uint32_t)name##_cache)
654
655 static inline int get_bits_count(GetBitContext *s){
656     return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
657 }
658
659 #elif defined A32_BITSTREAM_READER
660
661 #   define MIN_CACHE_BITS 32
662
663 #   define OPEN_READER(name, gb)\
664         int name##_bit_count=(gb)->bit_count;\
665         uint32_t name##_cache0= (gb)->cache0;\
666         uint32_t name##_cache1= (gb)->cache1;\
667         uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
668
669 #   define CLOSE_READER(name, gb)\
670         (gb)->bit_count= name##_bit_count;\
671         (gb)->cache0= name##_cache0;\
672         (gb)->cache1= name##_cache1;\
673         (gb)->buffer_ptr= name##_buffer_ptr;\
674
675 #   define UPDATE_CACHE(name, gb)\
676     if(name##_bit_count > 0){\
677         const uint32_t next= be2me_32( *name##_buffer_ptr );\
678         name##_cache0 |= NEG_USR32(next,name##_bit_count);\
679         name##_cache1 |= next<<name##_bit_count;\
680         name##_buffer_ptr++;\
681         name##_bit_count-= 32;\
682     }\
683
684 #ifdef ARCH_X86
685 #   define SKIP_CACHE(name, gb, num)\
686         asm(\
687             "shldl %2, %1, %0           \n\t"\
688             "shll %2, %1                \n\t"\
689             : "+r" (name##_cache0), "+r" (name##_cache1)\
690             : "Ic" ((uint8_t)num)\
691            );
692 #else
693 #   define SKIP_CACHE(name, gb, num)\
694         name##_cache0 <<= (num);\
695         name##_cache0 |= NEG_USR32(name##_cache1,num);\
696         name##_cache1 <<= (num);
697 #endif
698
699 #   define SKIP_COUNTER(name, gb, num)\
700         name##_bit_count += (num);\
701
702 #   define SKIP_BITS(name, gb, num)\
703         {\
704             SKIP_CACHE(name, gb, num)\
705             SKIP_COUNTER(name, gb, num)\
706         }\
707
708 #   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
709 #   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
710
711 #   define SHOW_UBITS(name, gb, num)\
712         NEG_USR32(name##_cache0, num)
713
714 #   define SHOW_SBITS(name, gb, num)\
715         NEG_SSR32(name##_cache0, num)
716
717 #   define GET_CACHE(name, gb)\
718         (name##_cache0)
719
720 static inline int get_bits_count(GetBitContext *s){
721     return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
722 }
723
724 #endif
725
726 /**
727  * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
728  * if MSB not set it is negative 
729  * @param n length in bits
730  * @author BERO  
731  */
732 static inline int get_xbits(GetBitContext *s, int n){
733     register int tmp;
734     register int32_t cache;
735     OPEN_READER(re, s)
736     UPDATE_CACHE(re, s)
737     cache = GET_CACHE(re,s);
738     if ((int32_t)cache<0) { //MSB=1
739         tmp = NEG_USR32(cache,n);
740     } else {
741     //   tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
742     //   tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
743         tmp = - NEG_USR32(~cache,n);
744     }
745     LAST_SKIP_BITS(re, s, n)
746     CLOSE_READER(re, s)
747     return tmp;
748 }
749
750 static inline int get_sbits(GetBitContext *s, int n){
751     register int tmp;
752     OPEN_READER(re, s)
753     UPDATE_CACHE(re, s)
754     tmp= SHOW_SBITS(re, s, n);
755     LAST_SKIP_BITS(re, s, n)
756     CLOSE_READER(re, s)
757     return tmp;
758 }
759
760 /**
761  * reads 0-17 bits.
762  * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
763  */
764 static inline unsigned int get_bits(GetBitContext *s, int n){
765     register int tmp;
766     OPEN_READER(re, s)
767     UPDATE_CACHE(re, s)
768     tmp= SHOW_UBITS(re, s, n);
769     LAST_SKIP_BITS(re, s, n)
770     CLOSE_READER(re, s)
771     return tmp;
772 }
773
774 unsigned int get_bits_long(GetBitContext *s, int n);
775
776 /**
777  * shows 0-17 bits.
778  * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
779  */
780 static inline unsigned int show_bits(GetBitContext *s, int n){
781     register int tmp;
782     OPEN_READER(re, s)
783     UPDATE_CACHE(re, s)
784     tmp= SHOW_UBITS(re, s, n);
785 //    CLOSE_READER(re, s)
786     return tmp;
787 }
788
789 unsigned int show_bits_long(GetBitContext *s, int n);
790
791 static inline void skip_bits(GetBitContext *s, int n){
792  //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
793     OPEN_READER(re, s)
794     UPDATE_CACHE(re, s)
795     LAST_SKIP_BITS(re, s, n)
796     CLOSE_READER(re, s)
797 }
798
799 static inline unsigned int get_bits1(GetBitContext *s){
800 #ifdef ALT_BITSTREAM_READER
801     int index= s->index;
802     uint8_t result= s->buffer[ index>>3 ];
803     result<<= (index&0x07);
804     result>>= 8 - 1;
805     index++;
806     s->index= index;
807
808     return result;
809 #else
810     return get_bits(s, 1);
811 #endif
812 }
813
814 static inline unsigned int show_bits1(GetBitContext *s){
815     return show_bits(s, 1);
816 }
817
818 static inline void skip_bits1(GetBitContext *s){
819     skip_bits(s, 1);
820 }
821
822 void init_get_bits(GetBitContext *s,
823                    const uint8_t *buffer, int buffer_size);
824
825 int check_marker(GetBitContext *s, const char *msg);
826 void align_get_bits(GetBitContext *s);
827 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
828              const void *bits, int bits_wrap, int bits_size,
829              const void *codes, int codes_wrap, int codes_size);
830 void free_vlc(VLC *vlc);
831
832 /**
833  *
834  * if the vlc code is invalid and max_depth=1 than no bits will be removed
835  * if the vlc code is invalid and max_depth>1 than the number of bits removed
836  * is undefined
837  */
838 #define GET_VLC(code, name, gb, table, bits, max_depth)\
839 {\
840     int n, index, nb_bits;\
841 \
842     index= SHOW_UBITS(name, gb, bits);\
843     code = table[index][0];\
844     n    = table[index][1];\
845 \
846     if(max_depth > 1 && n < 0){\
847         LAST_SKIP_BITS(name, gb, bits)\
848         UPDATE_CACHE(name, gb)\
849 \
850         nb_bits = -n;\
851 \
852         index= SHOW_UBITS(name, gb, nb_bits) + code;\
853         code = table[index][0];\
854         n    = table[index][1];\
855         if(max_depth > 2 && n < 0){\
856             LAST_SKIP_BITS(name, gb, nb_bits)\
857             UPDATE_CACHE(name, gb)\
858 \
859             nb_bits = -n;\
860 \
861             index= SHOW_UBITS(name, gb, nb_bits) + code;\
862             code = table[index][0];\
863             n    = table[index][1];\
864         }\
865     }\
866     SKIP_BITS(name, gb, n)\
867 }
868
869 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
870 {\
871     int n, index, nb_bits;\
872 \
873     index= SHOW_UBITS(name, gb, bits);\
874     level = table[index].level;\
875     n     = table[index].len;\
876 \
877     if(max_depth > 1 && n < 0){\
878         LAST_SKIP_BITS(name, gb, bits)\
879         UPDATE_CACHE(name, gb)\
880 \
881         nb_bits = -n;\
882 \
883         index= SHOW_UBITS(name, gb, nb_bits) + level;\
884         level = table[index].level;\
885         n     = table[index].len;\
886     }\
887     run= table[index].run;\
888     SKIP_BITS(name, gb, n)\
889 }
890
891 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
892 static inline int get_vlc(GetBitContext *s, VLC *vlc)
893 {
894     int code;
895     VLC_TYPE (*table)[2]= vlc->table;
896     
897     OPEN_READER(re, s)
898     UPDATE_CACHE(re, s)
899
900     GET_VLC(code, re, s, table, vlc->bits, 3)    
901
902     CLOSE_READER(re, s)
903     return code;
904 }
905
906 /**
907  * parses a vlc code, faster then get_vlc()
908  * @param bits is the number of bits which will be read at once, must be 
909  *             identical to nb_bits in init_vlc()
910  * @param max_depth is the number of times bits bits must be readed to completly
911  *                  read the longest vlc code 
912  *                  = (max_vlc_length + bits - 1) / bits
913  */
914 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
915                                   int bits, int max_depth)
916 {
917     int code;
918     
919     OPEN_READER(re, s)
920     UPDATE_CACHE(re, s)
921
922     GET_VLC(code, re, s, table, bits, max_depth)
923
924     CLOSE_READER(re, s)
925     return code;
926 }
927
928 //#define TRACE
929
930 #ifdef TRACE
931
932 static inline void print_bin(int bits, int n){
933     int i;
934     
935     for(i=n-1; i>=0; i--){
936         printf("%d", (bits>>i)&1);
937     }
938     for(i=n; i<24; i++)
939         printf(" ");
940 }
941
942 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
943     int r= get_bits(s, n);
944     
945     print_bin(r, n);
946     printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
947     return r;
948 }
949 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
950     int show= show_bits(s, 24);
951     int pos= get_bits_count(s);
952     int r= get_vlc2(s, table, bits, max_depth);
953     int len= get_bits_count(s) - pos;
954     int bits2= show>>(24-len);
955     
956     print_bin(bits2, len);
957     
958     printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
959     return r;
960 }
961 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
962     int show= show_bits(s, n);
963     int r= get_xbits(s, n);
964     
965     print_bin(show, n);
966     printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
967     return r;
968 }
969
970 #define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
971 #define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
972 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
973 #define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
974 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
975
976 #define tprintf printf
977
978 #else //TRACE
979 #define tprintf(_arg...) {}
980 #endif
981
982 /* define it to include statistics code (useful only for optimizing
983    codec efficiency */
984 //#define STATS
985
986 #ifdef STATS
987
988 enum {
989     ST_UNKNOWN,
990     ST_DC,
991     ST_INTRA_AC,
992     ST_INTER_AC,
993     ST_INTRA_MB,
994     ST_INTER_MB,
995     ST_MV,
996     ST_NB,
997 };
998
999 extern int st_current_index;
1000 extern unsigned int st_bit_counts[ST_NB];
1001 extern unsigned int st_out_bit_counts[ST_NB];
1002
1003 void print_stats(void);
1004 #endif
1005
1006 /* misc math functions */
1007 extern const uint8_t ff_log2_tab[256];
1008
1009 static inline int av_log2(unsigned int v)
1010 {
1011     int n;
1012
1013     n = 0;
1014     if (v & 0xffff0000) {
1015         v >>= 16;
1016         n += 16;
1017     }
1018     if (v & 0xff00) {
1019         v >>= 8;
1020         n += 8;
1021     }
1022     n += ff_log2_tab[v];
1023
1024     return n;
1025 }
1026
1027 static inline int av_log2_16bit(unsigned int v)
1028 {
1029     int n;
1030
1031     n = 0;
1032     if (v & 0xff00) {
1033         v >>= 8;
1034         n += 8;
1035     }
1036     n += ff_log2_tab[v];
1037
1038     return n;
1039 }
1040
1041 /* median of 3 */
1042 static inline int mid_pred(int a, int b, int c)
1043 {
1044 #if 0
1045     int t= (a-b)&((a-b)>>31);
1046     a-=t;
1047     b+=t;
1048     b-= (b-c)&((b-c)>>31);
1049     b+= (a-b)&((a-b)>>31);
1050
1051     return b;
1052 #else
1053     if(a>b){
1054         if(c>b){
1055             if(c>a) b=a;
1056             else    b=c;
1057         }
1058     }else{
1059         if(b>c){
1060             if(c>a) b=c;
1061             else    b=a;
1062         }
1063     }
1064     return b;
1065 #endif
1066 }
1067
1068 static inline int clip(int a, int amin, int amax)
1069 {
1070     if (a < amin)
1071         return amin;
1072     else if (a > amax)
1073         return amax;
1074     else
1075         return a;
1076 }
1077
1078 /* math */
1079 extern const uint8_t ff_sqrt_tab[128];
1080
1081 int64_t ff_gcd(int64_t a, int64_t b);
1082
1083 static inline int ff_sqrt(int a)
1084 {
1085     int ret=0;
1086     int s;
1087     int ret_sq=0;
1088     
1089     if(a<128) return ff_sqrt_tab[a];
1090     
1091     for(s=15; s>=0; s--){
1092         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1093         if(b<=a){
1094             ret_sq=b;
1095             ret+= 1<<s;
1096         }
1097     }
1098     return ret;
1099 }
1100
1101 /**
1102  * converts fourcc string to int
1103  */
1104 static inline int ff_get_fourcc(const char *s){
1105     assert( strlen(s)==4 );
1106
1107     return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1108 }
1109
1110 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1111 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1112
1113
1114 #ifdef ARCH_X86
1115 #define MASK_ABS(mask, level)\
1116             asm volatile(\
1117                 "cdq                    \n\t"\
1118                 "xorl %1, %0            \n\t"\
1119                 "subl %1, %0            \n\t"\
1120                 : "+a" (level), "=&d" (mask)\
1121             );
1122 #else
1123 #define MASK_ABS(mask, level)\
1124             mask= level>>31;\
1125             level= (level^mask)-mask;
1126 #endif
1127
1128
1129 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1130 #define COPY3_IF_LT(x,y,a,b,c,d)\
1131 asm volatile (\
1132     "cmpl %0, %3        \n\t"\
1133     "cmovl %3, %0       \n\t"\
1134     "cmovl %4, %1       \n\t"\
1135     "cmovl %5, %2       \n\t"\
1136     : "+r" (x), "+r" (a), "+r" (c)\
1137     : "r" (y), "r" (b), "r" (d)\
1138 );
1139 #else
1140 #define COPY3_IF_LT(x,y,a,b,c,d)\
1141 if((y)<(x)){\
1142      (x)=(y);\
1143      (a)=(b);\
1144      (c)=(d);\
1145 }
1146 #endif
1147
1148 #ifdef ARCH_X86
1149 static inline long long rdtsc()
1150 {
1151         long long l;
1152         asm volatile(   "rdtsc\n\t"
1153                 : "=A" (l)
1154         );
1155         return l;
1156 }
1157
1158 #define START_TIMER \
1159 uint64_t tend;\
1160 uint64_t tstart= rdtsc();\
1161
1162 #define STOP_TIMER(id) \
1163 tend= rdtsc();\
1164 {\
1165   static uint64_t tsum=0;\
1166   static int tcount=0;\
1167   static int tskip_count=0;\
1168   if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1169       tsum+= tend - tstart;\
1170       tcount++;\
1171   }else\
1172       tskip_count++;\
1173   if(256*256*256*64%(tcount+tskip_count)==0){\
1174       av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1175   }\
1176 }
1177 #endif
1178
1179 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1180
1181 /* avoid usage of various functions */
1182 #define malloc please_use_av_malloc
1183 #define free please_use_av_free
1184 #define realloc please_use_av_realloc
1185 #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1186 #define printf please_use_av_log
1187 #define fprintf please_use_av_log
1188 #endif
1189
1190 #define CHECKED_ALLOCZ(p, size)\
1191 {\
1192     p= av_mallocz(size);\
1193     if(p==NULL && (size)!=0){\
1194         perror("malloc");\
1195         goto fail;\
1196     }\
1197 }
1198
1199 #endif /* HAVE_AV_CONFIG_H */
1200
1201 #endif /* COMMON_H */