more sane inttypes emulation behavior if libavcodec is used outside ffmpeg
[libav.git] / libavcodec / common.h
CommitLineData
983e3246
MN
1/**
2 * @file common.h
3 * common internal api header.
4 */
5
de6d9b64
FB
6#ifndef COMMON_H
7#define COMMON_H
8
4bdd9157 9#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
9b59c92f 10# define CONFIG_WIN32
1a565432
FB
11#endif
12
17592475 13//#define ALT_BITSTREAM_WRITER
4f0f8bfc 14//#define ALIGNED_BITSTREAM_WRITER
3530320d 15
8db1a1dd
MN
16#define ALT_BITSTREAM_READER
17//#define LIBMPEG2_BITSTREAM_READER
18//#define A32_BITSTREAM_READER
d8e00c09 19#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
8db1a1dd 20
420b073b 21#ifdef HAVE_AV_CONFIG_H
1a565432 22/* only include the following when compiling package */
9b59c92f
MN
23# include "config.h"
24
25# include <stdlib.h>
26# include <stdio.h>
27# include <string.h>
56c4a184 28# include <ctype.h>
9b59c92f
MN
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
1a565432 39
c4d82fea
FB
40#ifndef M_PI
41#define M_PI 3.14159265358979323846
42#endif
43
fe1b62fb
ZK
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 }
5d4ce457
ZK
51#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
fe1b62fb
ZK
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 }
bec89a84 61#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
fe1b62fb
ZK
62#define AVOPTION_END() AVOPTION_SUB(NULL)
63
a77146ab
ZK
64struct AVOption;
65#ifdef HAVE_MMX
66extern const struct AVOption avoptions_common[3 + 5];
67#else
68extern const struct AVOption avoptions_common[3];
69#endif
70extern const struct AVOption avoptions_workaround_bug[11];
71
44f27b3a 72#endif /* HAVE_AV_CONFIG_H */
1a565432 73
02da51ec
FH
74/* Suppress restrict if it was not defined in config.h. */
75#ifndef restrict
9b59c92f 76# define restrict
02da51ec
FH
77#endif
78
d200cab6 79#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
9b59c92f 80# define always_inline __attribute__((always_inline)) inline
d200cab6 81#else
9b59c92f 82# define always_inline inline
d200cab6
FH
83#endif
84
d2a9bddd 85#ifndef EMULATE_INTTYPES
7f965c1c
CF
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
19d053c5
RS
104#ifdef EMULATE_FAST_INT
105/* note that we don't emulate 64bit ints */
106typedef signed char int_fast8_t;
107typedef signed int int_fast16_t;
108typedef signed int int_fast32_t;
109typedef unsigned char uint_fast8_t;
110typedef unsigned int uint_fast16_t;
111typedef unsigned int uint_fast32_t;
112#endif
113
114#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
115static inline float floorf(float f) {
116 return floor(f);
117}
118#endif
119
1a565432
FB
120#ifdef CONFIG_WIN32
121
122/* windows */
123
9b59c92f 124# ifndef __MINGW32__
0c1a9eda
ZK
125# define int64_t_C(c) (c ## i64)
126# define uint64_t_C(c) (c ## i64)
1a565432 127
49e17f84
MN
128# ifdef HAVE_AV_CONFIG_H
129# define inline __inline
130# endif
1a565432 131
9b59c92f 132# else
0c1a9eda
ZK
133# define int64_t_C(c) (c ## LL)
134# define uint64_t_C(c) (c ## ULL)
9b59c92f 135# endif /* __MINGW32__ */
a74127c0 136
49e17f84
MN
137# ifdef HAVE_AV_CONFIG_H
138# ifdef _DEBUG
139# define DEBUG
140# endif
de6d9b64 141
49e17f84
MN
142# define snprintf _snprintf
143# define vsnprintf _vsnprintf
144# endif
1a565432 145
f3ec2d46
SG
146/* CONFIG_WIN32 end */
147#elif defined (CONFIG_OS2)
148/* OS/2 EMX */
149
0c1a9eda
ZK
150#ifndef int64_t_C
151#define int64_t_C(c) (c ## LL)
152#define uint64_t_C(c) (c ## ULL)
f3ec2d46
SG
153#endif
154
f5fe9d5f
FB
155#ifdef HAVE_AV_CONFIG_H
156
f3ec2d46
SG
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
1a565432
FB
167
168/* unix */
169
f5fe9d5f
FB
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
1a565432 176
9b59c92f
MN
177# ifdef USE_FASTMEMCPY
178# include "fastmemcpy.h"
179# endif
9b59c92f 180# endif /* HAVE_AV_CONFIG_H */
a74127c0 181
f3ec2d46 182#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
a74127c0 183
a74127c0
FB
184#ifdef HAVE_AV_CONFIG_H
185
9b59c92f 186# include "bswap.h"
96707bb7 187
9b59c92f 188# if defined(__MINGW32__) || defined(__CYGWIN__) || \
814b648c 189 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
9b59c92f
MN
190# define MANGLE(a) "_" #a
191# else
192# define MANGLE(a) #a
193# endif
6a3d7e36 194
d503f970
FB
195/* debug stuff */
196
9b59c92f
MN
197# ifndef DEBUG
198# define NDEBUG
199# endif
200# include <assert.h>
1a565432 201
a74127c0 202/* dprintf macros */
9b59c92f 203# if defined(CONFIG_WIN32) && !defined(__MINGW32__)
a74127c0
FB
204
205inline void dprintf(const char* fmt,...) {}
206
9b59c92f 207# else
a74127c0 208
9b59c92f 209# ifdef DEBUG
dc7cb06a 210# define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
9b59c92f 211# else
dc7cb06a 212# define dprintf(fmt,...)
9b59c92f 213# endif
1a565432 214
9b59c92f 215# endif /* !CONFIG_WIN32 */
1a565432 216
9b879566 217# define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
935cdf09 218
073b013d
MN
219//rounded divison & shift
220#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
d7e9533a
MN
221/* assume b>0 */
222#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
32cd20de 223#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
75460b0c 224
b8a78f41
MN
225#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
226#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
d7e9533a 227
d4961b35
B
228extern 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
ef5e74bb
MN
247#ifdef ARCH_X86
248// avoid +32 for shift optimization (gcc should do that ...)
249static 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}
256static 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}
8db1a1dd 263#else
9b59c92f
MN
264# define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
265# define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
8db1a1dd
MN
266#endif
267
de6d9b64
FB
268/* bit output */
269
270struct PutBitContext;
271
0c1a9eda 272typedef void (*WriteDataFunc)(void *, uint8_t *, int);
de6d9b64
FB
273
274typedef struct PutBitContext {
17592475 275#ifdef ALT_BITSTREAM_WRITER
0c1a9eda 276 uint8_t *buf, *buf_end;
17592475
MN
277 int index;
278#else
0c1a9eda 279 uint32_t bit_buf;
d8cf5aea 280 int bit_left;
0c1a9eda 281 uint8_t *buf, *buf_ptr, *buf_end;
17592475 282#endif
de6d9b64
FB
283} PutBitContext;
284
ed7debda 285void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
17592475 286
a8380f44 287int get_bit_count(PutBitContext *s); /* XXX: change function name */
de6d9b64
FB
288void align_put_bits(PutBitContext *s);
289void flush_put_bits(PutBitContext *s);
9717dad8 290void put_string(PutBitContext * pbc, char *s);
de6d9b64 291
de6d9b64
FB
292/* bit input */
293
294typedef struct GetBitContext {
ae39132d 295 const uint8_t *buffer, *buffer_end;
27a3e2c5
MN
296#ifdef ALT_BITSTREAM_READER
297 int index;
8db1a1dd 298#elif defined LIBMPEG2_BITSTREAM_READER
0c1a9eda
ZK
299 uint8_t *buffer_ptr;
300 uint32_t cache;
8db1a1dd
MN
301 int bit_count;
302#elif defined A32_BITSTREAM_READER
0c1a9eda
ZK
303 uint32_t *buffer_ptr;
304 uint32_t cache0;
305 uint32_t cache1;
8db1a1dd 306 int bit_count;
27a3e2c5 307#endif
68f593b4 308 int size_in_bits;
de6d9b64
FB
309} GetBitContext;
310
49092244
MN
311static inline int get_bits_count(GetBitContext *s);
312
0c1a9eda 313#define VLC_TYPE int16_t
8db1a1dd 314
de6d9b64
FB
315typedef struct VLC {
316 int bits;
64863965 317 VLC_TYPE (*table)[2]; ///< code, bits
de6d9b64
FB
318 int table_size, table_allocated;
319} VLC;
320
45a82edb
MN
321typedef struct RL_VLC_ELEM {
322 int16_t level;
323 int8_t len;
324 uint8_t run;
325} RL_VLC_ELEM;
326
35fedfc3
PG
327#ifdef ARCH_SPARC64
328#define UNALIGNED_STORES_ARE_BAD
329#endif
330
3587d501
MN
331/* used to avoid missaligned exceptions on some archs (alpha, ...) */
332#ifdef ARCH_X86
0c1a9eda 333# define unaligned32(a) (*(uint32_t*)(a))
3587d501 334#else
9b59c92f 335# ifdef __GNUC__
3587d501
MN
336static 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}
9b59c92f 343# elif defined(__DECC)
3587d501
MN
344static inline uint32_t unaligned32(const void *v) {
345 return *(const __unaligned uint32_t *) v;
346}
9b59c92f 347# else
3587d501
MN
348static inline uint32_t unaligned32(const void *v) {
349 return *(const uint32_t *) v;
350}
9b59c92f 351# endif
3587d501
MN
352#endif //!ARCH_X86
353
d8cf5aea
MN
354#ifndef ALT_BITSTREAM_WRITER
355static 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));
fcb609f4 365
d8cf5aea
MN
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);
35fedfc3
PG
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
0c1a9eda 385 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
d8cf5aea
MN
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
17592475 398#ifdef ALT_BITSTREAM_WRITER
4f0f8bfc 399static inline void put_bits(PutBitContext *s, int n, unsigned int value)
17592475 400{
9b59c92f
MN
401# ifdef ALIGNED_BITSTREAM_WRITER
402# ifdef ARCH_X86
4f0f8bfc
MN
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 );
9b59c92f 420# else
4f0f8bfc
MN
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;
9b59c92f
MN
431# endif
432# else //ALIGNED_BITSTREAM_WRITER
433# ifdef ARCH_X86
17592475
MN
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 );
9b59c92f 450# else
17592475
MN
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;
9b59c92f
MN
459# endif
460# endif //!ALIGNED_BITSTREAM_WRITER
17592475
MN
461}
462#endif
463
17592475 464
17592475
MN
465static 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
8db1a1dd
MN
474/* Bitstream reader API docs:
475name
476 abritary name which is used as prefix for the internal variables
de6d9b64 477
8db1a1dd
MN
478gb
479 getbitcontext
480
481OPEN_READER(name, gb)
482 loads gb into local variables
483
484CLOSE_READER(name, gb)
485 stores local vars in gb
486
487UPDATE_CACHE(name, gb)
488 refills the internal cache from the bitstream
489 after this call at least MIN_CACHE_BITS will be available,
490
491GET_CACHE(name, gb)
492 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
493
494SHOW_UBITS(name, gb, num)
495 will return the nest num bits
496
497SHOW_SBITS(name, gb, num)
498 will return the nest num bits and do sign extension
499
500SKIP_BITS(name, gb, num)
501 will skip over the next num bits
502 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
503
504SKIP_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
507SKIP_COUNTER(name, gb, num)
508 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
509
510LAST_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
513LAST_SKIP_BITS(name, gb, num)
514 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
515
516for examples see get_bits, show_bits, skip_bits, get_vlc
517*/
2931ecb9 518
d8e00c09
B
519static 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
27a3e2c5 529#ifdef ALT_BITSTREAM_READER
8db1a1dd 530# define MIN_CACHE_BITS 25
27a3e2c5 531
8db1a1dd
MN
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)\
d8e00c09 540 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
8db1a1dd
MN
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)\
ef5e74bb 559 NEG_USR32(name##_cache, num)
8db1a1dd
MN
560
561# define SHOW_SBITS(name, gb, num)\
ef5e74bb 562 NEG_SSR32(name##_cache, num)
8db1a1dd
MN
563
564# define GET_CACHE(name, gb)\
565 ((uint32_t)name##_cache)
566
567static inline int get_bits_count(GetBitContext *s){
568 return s->index;
2931ecb9 569}
8db1a1dd
MN
570#elif defined LIBMPEG2_BITSTREAM_READER
571//libmpeg2 like reader
572
3bb10888 573# define MIN_CACHE_BITS 17
8db1a1dd
MN
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
d8e00c09
B
585#ifdef LIBMPEG2_BITSTREAM_READER_HACK
586
587# define UPDATE_CACHE(name, gb)\
588 if(name##_bit_count >= 0){\
17fb5fd3
MN
589 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
590 ((uint16_t*)name##_buffer_ptr)++;\
d8e00c09
B
591 name##_bit_count-= 16;\
592 }\
593
594#else
595
8db1a1dd 596# define UPDATE_CACHE(name, gb)\
3bb10888 597 if(name##_bit_count >= 0){\
8db1a1dd
MN
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 }\
2931ecb9 602
d8e00c09
B
603#endif
604
8db1a1dd
MN
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)\
ef5e74bb 621 NEG_USR32(name##_cache, num)
8db1a1dd
MN
622
623# define SHOW_SBITS(name, gb, num)\
ef5e74bb 624 NEG_SSR32(name##_cache, num)
8db1a1dd
MN
625
626# define GET_CACHE(name, gb)\
627 ((uint32_t)name##_cache)
628
629static inline int get_bits_count(GetBitContext *s){
630 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
612476ef
A
631}
632
8db1a1dd
MN
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 );\
ef5e74bb 652 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
8db1a1dd
MN
653 name##_cache1 |= next<<name##_bit_count;\
654 name##_buffer_ptr++;\
655 name##_bit_count-= 32;\
656 }\
657
3587d501 658#ifdef ARCH_X86
8db1a1dd
MN
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 );
3587d501 666#else
8db1a1dd
MN
667# define SKIP_CACHE(name, gb, num)\
668 name##_cache0 <<= (num);\
ef5e74bb 669 name##_cache0 |= NEG_USR32(name##_cache1,num);\
8db1a1dd 670 name##_cache1 <<= (num);
3587d501 671#endif
27a3e2c5 672
8db1a1dd
MN
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)\
ef5e74bb 686 NEG_USR32(name##_cache0, num)
8db1a1dd
MN
687
688# define SHOW_SBITS(name, gb, num)\
ef5e74bb 689 NEG_SSR32(name##_cache0, num)
8db1a1dd
MN
690
691# define GET_CACHE(name, gb)\
692 (name##_cache0)
693
694static inline int get_bits_count(GetBitContext *s){
695 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
4949028f
J
696}
697
49092244 698#endif
49092244 699
17fb5fd3
MN
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 */
d8e00c09
B
706static 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
724static 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
17fb5fd3
MN
734/**
735 * reads 0-17 bits.
736 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
737 */
8db1a1dd
MN
738static 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}
49092244 747
17fb5fd3
MN
748unsigned 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 */
8db1a1dd
MN
754static 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;
49092244
MN
761}
762
17fb5fd3
MN
763unsigned int show_bits_long(GetBitContext *s, int n);
764
612476ef 765static inline void skip_bits(GetBitContext *s, int n){
8db1a1dd
MN
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
773static inline unsigned int get_bits1(GetBitContext *s){
27a3e2c5 774#ifdef ALT_BITSTREAM_READER
8db1a1dd
MN
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;
fcb609f4 781
8db1a1dd 782 return result;
27a3e2c5 783#else
8db1a1dd 784 return get_bits(s, 1);
27a3e2c5 785#endif
612476ef
A
786}
787
8db1a1dd
MN
788static inline unsigned int show_bits1(GetBitContext *s){
789 return show_bits(s, 1);
612476ef
A
790}
791
8db1a1dd
MN
792static inline void skip_bits1(GetBitContext *s){
793 skip_bits(s, 1);
9dbf9389 794}
612476ef 795
8db1a1dd 796void init_get_bits(GetBitContext *s,
ae39132d 797 const uint8_t *buffer, int buffer_size);
8db1a1dd 798
18f77016 799int check_marker(GetBitContext *s, const char *msg);
de6d9b64
FB
800void align_get_bits(GetBitContext *s);
801int 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);
804void free_vlc(VLC *vlc);
de6d9b64 805
68f593b4
MN
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 */
cceabc86
MN
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;\
cceabc86 825\
6300c80a 826 index= SHOW_UBITS(name, gb, nb_bits) + code;\
cceabc86
MN
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;\
cceabc86 834\
6300c80a 835 index= SHOW_UBITS(name, gb, nb_bits) + code;\
cceabc86
MN
836 code = table[index][0];\
837 n = table[index][1];\
838 }\
839 }\
840 SKIP_BITS(name, gb, n)\
841}
842
45a82edb
MN
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
08dce7b0 865// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
3587d501
MN
866static inline int get_vlc(GetBitContext *s, VLC *vlc)
867{
cceabc86
MN
868 int code;
869 VLC_TYPE (*table)[2]= vlc->table;
870
8db1a1dd 871 OPEN_READER(re, s)
8db1a1dd 872 UPDATE_CACHE(re, s)
3587d501 873
cceabc86 874 GET_VLC(code, re, s, table, vlc->bits, 3)
8db1a1dd 875
8db1a1dd 876 CLOSE_READER(re, s)
3587d501 877 return code;
08dce7b0
MN
878}
879
d4c48e48
MN
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 */
d200cab6
FH
888static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
889 int bits, int max_depth)
08dce7b0
MN
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;
3587d501
MN
900}
901
64338956
MN
902//#define TRACE
903
904#ifdef TRACE
905
906static 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
916static 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}
923static 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}
3d2e8cce
MN
935static 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}
64338956
MN
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__)
3d2e8cce 946#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
3b0da3f9 947#define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
64338956
MN
948#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
949
95c26348
MN
950#define tprintf printf
951
952#else //TRACE
c8816e01 953#define tprintf(_arg...) {}
95c26348 954#endif
45a82edb 955
de6d9b64
FB
956/* define it to include statistics code (useful only for optimizing
957 codec efficiency */
958//#define STATS
959
960#ifdef STATS
961
962enum {
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
973extern int st_current_index;
974extern unsigned int st_bit_counts[ST_NB];
975extern unsigned int st_out_bit_counts[ST_NB];
976
977void print_stats(void);
978#endif
979
980/* misc math functions */
c81f0349 981extern const uint8_t ff_log2_tab[256];
de6d9b64 982
a822a479 983static inline int av_log2(unsigned int v)
de6d9b64
FB
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 }
c81f0349
MN
996 n += ff_log2_tab[v];
997
998 return n;
999}
1000
1001static 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;
de6d9b64 1009 }
c81f0349
MN
1010 n += ff_log2_tab[v];
1011
de6d9b64
FB
1012 return n;
1013}
1014
c81f0349 1015
45870f57
MN
1016/* median of 3 */
1017static 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
91029be7
MN
1034static 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
9dbcbd92 1044/* math */
c81f0349 1045extern const uint8_t ff_sqrt_tab[128];
f36db5df 1046
14bea432 1047int64_t ff_gcd(int64_t a, int64_t b);
9dbcbd92 1048
7fd08ac1
MN
1049static inline int ff_sqrt(int a)
1050{
1051 int ret=0;
1052 int s;
1053 int ret_sq=0;
f36db5df
MN
1054
1055 if(a<128) return ff_sqrt_tab[a];
1056
7fd08ac1
MN
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}
202ef8b8
MN
1066
1067/**
1068 * converts fourcc string to int
1069 */
6a85ec8d 1070static inline int ff_get_fourcc(const char *s){
202ef8b8 1071 assert( strlen(s)==4 );
966df5b6 1072
202ef8b8
MN
1073 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1074}
1075
e8750b00
FR
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
2ad1516a
MN
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
7fd08ac1
MN
1095#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1096#define COPY3_IF_LT(x,y,a,b,c,d)\
1097asm 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)\
1107if((y)<(x)){\
1108 (x)=(y);\
1109 (a)=(b);\
1110 (c)=(d);\
1111}
1112#endif
1113
b534c7f9
MN
1114#ifdef ARCH_X86
1115static 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 \
1125static uint64_t tsum=0;\
1126static int tcount=0;\
1127static int tskip_count=0;\
1128uint64_t tend;\
1129uint64_t tstart= rdtsc();\
1130
1131#define STOP_TIMER(id) \
1132tend= rdtsc();\
1133if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1134 tsum+= tend - tstart;\
1135 tcount++;\
1136}else\
1137 tskip_count++;\
1138if(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
a69b930c
AB
1143#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1144
8e1e6f31
FB
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
7bc9090a
MN
1150#define CHECKED_ALLOCZ(p, size)\
1151{\
1152 p= av_mallocz(size);\
dc939fd5 1153 if(p==NULL && (size)!=0){\
7bc9090a
MN
1154 perror("malloc");\
1155 goto fail;\
1156 }\
1157}
1158
96707bb7
FB
1159#endif /* HAVE_AV_CONFIG_H */
1160
1161#endif /* COMMON_H */