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