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