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