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