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