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