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