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