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