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