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