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