os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
[libav.git] / libavcodec / common.h
CommitLineData
de6d9b64
FB
1#ifndef COMMON_H
2#define COMMON_H
3
73120e55
FB
4#define FFMPEG_VERSION_INT 0x000406
5#define FFMPEG_VERSION "0.4.6"
1a565432 6
4bdd9157 7#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
9b59c92f 8# define CONFIG_WIN32
1a565432
FB
9#endif
10
17592475 11//#define ALT_BITSTREAM_WRITER
4f0f8bfc 12//#define ALIGNED_BITSTREAM_WRITER
3530320d 13
8db1a1dd
MN
14#define ALT_BITSTREAM_READER
15//#define LIBMPEG2_BITSTREAM_READER
16//#define A32_BITSTREAM_READER
17
420b073b 18#ifdef HAVE_AV_CONFIG_H
1a565432 19/* only include the following when compiling package */
9b59c92f
MN
20# include "config.h"
21
22# include <stdlib.h>
23# include <stdio.h>
24# include <string.h>
25# ifndef __BEOS__
26# include <errno.h>
27# else
28# include "berrno.h"
29# endif
30# include <math.h>
31
32# ifndef ENODATA
33# define ENODATA 61
34# endif
1a565432 35
c4d82fea
FB
36#ifndef M_PI
37#define M_PI 3.14159265358979323846
38#endif
39
44f27b3a 40#endif /* HAVE_AV_CONFIG_H */
1a565432 41
02da51ec
FH
42/* Suppress restrict if it was not defined in config.h. */
43#ifndef restrict
9b59c92f 44# define restrict
02da51ec
FH
45#endif
46
d200cab6 47#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
9b59c92f 48# define always_inline __attribute__((always_inline)) inline
d200cab6 49#else
9b59c92f 50# define always_inline inline
d200cab6
FH
51#endif
52
1a565432
FB
53#ifdef CONFIG_WIN32
54
55/* windows */
56
57typedef unsigned short UINT16;
58typedef signed short INT16;
59typedef unsigned char UINT8;
60typedef unsigned int UINT32;
61typedef unsigned __int64 UINT64;
62typedef signed char INT8;
63typedef signed int INT32;
64typedef signed __int64 INT64;
65
66typedef UINT8 uint8_t;
67typedef INT8 int8_t;
68typedef UINT16 uint16_t;
69typedef INT16 int16_t;
70typedef UINT32 uint32_t;
71typedef INT32 int32_t;
d503f970
FB
72typedef UINT64 uint64_t;
73typedef INT64 int64_t;
1a565432 74
9b59c92f
MN
75# ifndef __MINGW32__
76# define INT64_C(c) (c ## i64)
77# define UINT64_C(c) (c ## i64)
1a565432 78
9b59c92f 79# define inline __inline
1a565432 80
9b59c92f
MN
81# else
82# define INT64_C(c) (c ## LL)
83# define UINT64_C(c) (c ## ULL)
84# endif /* __MINGW32__ */
a74127c0 85
9b59c92f
MN
86# ifdef _DEBUG
87# define DEBUG
88# endif
de6d9b64 89
9b59c92f 90# define snprintf _snprintf
1a565432 91
f3ec2d46
SG
92/* CONFIG_WIN32 end */
93#elif defined (CONFIG_OS2)
94/* OS/2 EMX */
95
96#include <inttypes.h>
97
98typedef unsigned char UINT8;
99typedef unsigned short UINT16;
100typedef unsigned int UINT32;
101typedef unsigned long long UINT64;
102typedef signed char INT8;
103typedef signed short INT16;
104typedef signed int INT32;
105typedef signed long long INT64;
106
107#ifdef HAVE_AV_CONFIG_H
108
109#ifndef INT64_C
110#define INT64_C(c) (c ## LL)
111#define UINT64_C(c) (c ## ULL)
112#endif
113
114#ifdef USE_FASTMEMCPY
115#include "fastmemcpy.h"
116#endif
117
118#include <float.h>
119
120#endif /* HAVE_AV_CONFIG_H */
121
122/* CONFIG_OS2 end */
123#else
1a565432
FB
124
125/* unix */
126
9b59c92f 127# include <inttypes.h>
1a565432 128
9b59c92f 129# ifndef __WINE_WINDEF16_H
980fc7b8 130/* workaround for typedef conflict in MPlayer (wine typedefs) */
de6d9b64
FB
131typedef unsigned short UINT16;
132typedef signed short INT16;
9b59c92f 133# endif
de6d9b64
FB
134
135typedef unsigned char UINT8;
136typedef unsigned int UINT32;
137typedef unsigned long long UINT64;
138typedef signed char INT8;
139typedef signed int INT32;
140typedef signed long long INT64;
141
9b59c92f 142# ifdef HAVE_AV_CONFIG_H
9b59c92f
MN
143# ifndef INT64_C
144# define INT64_C(c) (c ## LL)
145# define UINT64_C(c) (c ## ULL)
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
MN
159# if defined(__MINGW32__) || defined(__CYGWIN__) || \
160 defined(__OS2__) || defined (__OpenBSD__)
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
224typedef void (*WriteDataFunc)(void *, UINT8 *, int);
225
226typedef struct PutBitContext {
17592475
MN
227#ifdef ALT_BITSTREAM_WRITER
228 UINT8 *buf, *buf_end;
229 int index;
230#else
de6d9b64 231 UINT32 bit_buf;
d8cf5aea 232 int bit_left;
2931ecb9 233 UINT8 *buf, *buf_ptr, *buf_end;
17592475
MN
234#endif
235 INT64 data_out_size; /* in bytes */
de6d9b64
FB
236} PutBitContext;
237
238void init_put_bits(PutBitContext *s,
239 UINT8 *buffer, int buffer_size,
240 void *opaque,
241 void (*write_data)(void *, UINT8 *, int));
17592475 242
9dbf9389 243INT64 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 {
8db1a1dd 251 UINT8 *buffer, *buffer_end;
27a3e2c5
MN
252#ifdef ALT_BITSTREAM_READER
253 int index;
8db1a1dd
MN
254#elif defined LIBMPEG2_BITSTREAM_READER
255 UINT8 *buffer_ptr;
256 UINT32 cache;
257 int bit_count;
258#elif defined A32_BITSTREAM_READER
259 UINT32 *buffer_ptr;
260 UINT32 cache0;
261 UINT32 cache1;
262 int bit_count;
27a3e2c5 263#endif
45870f57 264 int size;
de6d9b64
FB
265} GetBitContext;
266
49092244
MN
267static inline int get_bits_count(GetBitContext *s);
268
8db1a1dd
MN
269#define VLC_TYPE INT16
270
de6d9b64
FB
271typedef struct VLC {
272 int bits;
8db1a1dd 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
9b59c92f 289# define unaligned32(a) (*(UINT32*)(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
d8cf5aea
MN
341 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
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
MN
683void init_get_bits(GetBitContext *s,
684 UINT8 *buffer, int buffer_size);
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
cceabc86
MN
693#define GET_VLC(code, name, gb, table, bits, max_depth)\
694{\
695 int n, index, nb_bits;\
696\
697 index= SHOW_UBITS(name, gb, bits);\
698 code = table[index][0];\
699 n = table[index][1];\
700\
701 if(max_depth > 1 && n < 0){\
702 LAST_SKIP_BITS(name, gb, bits)\
703 UPDATE_CACHE(name, gb)\
704\
705 nb_bits = -n;\
cceabc86 706\
6300c80a 707 index= SHOW_UBITS(name, gb, nb_bits) + code;\
cceabc86
MN
708 code = table[index][0];\
709 n = table[index][1];\
710 if(max_depth > 2 && n < 0){\
711 LAST_SKIP_BITS(name, gb, nb_bits)\
712 UPDATE_CACHE(name, gb)\
713\
714 nb_bits = -n;\
cceabc86 715\
6300c80a 716 index= SHOW_UBITS(name, gb, nb_bits) + code;\
cceabc86
MN
717 code = table[index][0];\
718 n = table[index][1];\
719 }\
720 }\
721 SKIP_BITS(name, gb, n)\
722}
723
45a82edb
MN
724#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
725{\
726 int n, index, nb_bits;\
727\
728 index= SHOW_UBITS(name, gb, bits);\
729 level = table[index].level;\
730 n = table[index].len;\
731\
732 if(max_depth > 1 && n < 0){\
733 LAST_SKIP_BITS(name, gb, bits)\
734 UPDATE_CACHE(name, gb)\
735\
736 nb_bits = -n;\
737\
738 index= SHOW_UBITS(name, gb, nb_bits) + level;\
739 level = table[index].level;\
740 n = table[index].len;\
741 }\
742 run= table[index].run;\
743 SKIP_BITS(name, gb, n)\
744}
745
08dce7b0 746// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
3587d501
MN
747static inline int get_vlc(GetBitContext *s, VLC *vlc)
748{
cceabc86
MN
749 int code;
750 VLC_TYPE (*table)[2]= vlc->table;
751
8db1a1dd 752 OPEN_READER(re, s)
8db1a1dd 753 UPDATE_CACHE(re, s)
3587d501 754
cceabc86 755 GET_VLC(code, re, s, table, vlc->bits, 3)
8db1a1dd 756
8db1a1dd 757 CLOSE_READER(re, s)
3587d501 758 return code;
08dce7b0
MN
759}
760
d200cab6
FH
761static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
762 int bits, int max_depth)
08dce7b0
MN
763{
764 int code;
765
766 OPEN_READER(re, s)
767 UPDATE_CACHE(re, s)
768
769 GET_VLC(code, re, s, table, bits, max_depth)
770
771 CLOSE_READER(re, s)
772 return code;
3587d501
MN
773}
774
45a82edb 775
de6d9b64
FB
776/* define it to include statistics code (useful only for optimizing
777 codec efficiency */
778//#define STATS
779
780#ifdef STATS
781
782enum {
783 ST_UNKNOWN,
784 ST_DC,
785 ST_INTRA_AC,
786 ST_INTER_AC,
787 ST_INTRA_MB,
788 ST_INTER_MB,
789 ST_MV,
790 ST_NB,
791};
792
793extern int st_current_index;
794extern unsigned int st_bit_counts[ST_NB];
795extern unsigned int st_out_bit_counts[ST_NB];
796
797void print_stats(void);
798#endif
799
800/* misc math functions */
801
a822a479 802static inline int av_log2(unsigned int v)
de6d9b64
FB
803{
804 int n;
805
806 n = 0;
807 if (v & 0xffff0000) {
808 v >>= 16;
809 n += 16;
810 }
811 if (v & 0xff00) {
812 v >>= 8;
813 n += 8;
814 }
815 if (v & 0xf0) {
816 v >>= 4;
817 n += 4;
818 }
819 if (v & 0xc) {
820 v >>= 2;
821 n += 2;
822 }
823 if (v & 0x2) {
824 n++;
825 }
826 return n;
827}
828
45870f57
MN
829/* median of 3 */
830static inline int mid_pred(int a, int b, int c)
831{
832 int vmin, vmax;
833 vmax = vmin = a;
834 if (b < vmin)
835 vmin = b;
836 else
837 vmax = b;
838
839 if (c < vmin)
840 vmin = c;
841 else if (c > vmax)
842 vmax = c;
843
844 return a + b + c - vmin - vmax;
845}
846
91029be7
MN
847static inline int clip(int a, int amin, int amax)
848{
849 if (a < amin)
850 return amin;
851 else if (a > amax)
852 return amax;
853 else
854 return a;
855}
856
9dbcbd92 857/* math */
f36db5df
MN
858extern const UINT8 ff_sqrt_tab[128];
859
9dbcbd92
MN
860int ff_gcd(int a, int b);
861
7fd08ac1
MN
862static inline int ff_sqrt(int a)
863{
864 int ret=0;
865 int s;
866 int ret_sq=0;
f36db5df
MN
867
868 if(a<128) return ff_sqrt_tab[a];
869
7fd08ac1
MN
870 for(s=15; s>=0; s--){
871 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
872 if(b<=a){
873 ret_sq=b;
874 ret+= 1<<s;
875 }
876 }
877 return ret;
878}
202ef8b8
MN
879
880/**
881 * converts fourcc string to int
882 */
6a85ec8d 883static inline int ff_get_fourcc(const char *s){
202ef8b8
MN
884 assert( strlen(s)==4 );
885
886 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
887}
888
5d3cea3a
MN
889void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
890
2ad1516a
MN
891
892#ifdef ARCH_X86
893#define MASK_ABS(mask, level)\
894 asm volatile(\
895 "cdq \n\t"\
896 "xorl %1, %0 \n\t"\
897 "subl %1, %0 \n\t"\
898 : "+a" (level), "=&d" (mask)\
899 );
900#else
901#define MASK_ABS(mask, level)\
902 mask= level>>31;\
903 level= (level^mask)-mask;
904#endif
905
906
7fd08ac1
MN
907#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
908#define COPY3_IF_LT(x,y,a,b,c,d)\
909asm volatile (\
910 "cmpl %0, %3 \n\t"\
911 "cmovl %3, %0 \n\t"\
912 "cmovl %4, %1 \n\t"\
913 "cmovl %5, %2 \n\t"\
914 : "+r" (x), "+r" (a), "+r" (c)\
915 : "r" (y), "r" (b), "r" (d)\
916);
917#else
918#define COPY3_IF_LT(x,y,a,b,c,d)\
919if((y)<(x)){\
920 (x)=(y);\
921 (a)=(b);\
922 (c)=(d);\
923}
924#endif
925
a69b930c
AB
926#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
927
96707bb7
FB
928#endif /* HAVE_AV_CONFIG_H */
929
930#endif /* COMMON_H */