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