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