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