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