rv10 sig11 fix by Michael Niedermayer <michaelni@gmx.at>
[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
a74127c0 7#if defined(WIN32) && !defined(__MINGW32__)
1a565432
FB
8#define CONFIG_WIN32
9#endif
10
27a3e2c5 11//#define ALT_BITSTREAM_READER
3587d501 12//#define ALIGNED_BITSTREAM
27a3e2c5 13
420b073b 14#ifdef HAVE_AV_CONFIG_H
1a565432 15/* only include the following when compiling package */
de6d9b64 16#include "../config.h"
1a565432
FB
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22
23#ifndef ENODATA
24#define ENODATA 61
25#endif
26
27#endif
28
29#ifdef CONFIG_WIN32
30
31/* windows */
32
33typedef unsigned short UINT16;
34typedef signed short INT16;
35typedef unsigned char UINT8;
36typedef unsigned int UINT32;
37typedef unsigned __int64 UINT64;
38typedef signed char INT8;
39typedef signed int INT32;
40typedef signed __int64 INT64;
41
42typedef UINT8 uint8_t;
43typedef INT8 int8_t;
44typedef UINT16 uint16_t;
45typedef INT16 int16_t;
46typedef UINT32 uint32_t;
47typedef INT32 int32_t;
48
a74127c0 49#ifndef __MINGW32__
1a565432
FB
50#define INT64_C(c) (c ## i64)
51#define UINT64_C(c) (c ## i64)
52
53#define inline __inline
54
55/*
56 Disable warning messages:
57 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
58 warning C4305: 'argument' : truncation from 'const double' to 'float'
59*/
60#pragma warning( disable : 4244 )
61#pragma warning( disable : 4305 )
62
a74127c0
FB
63#else
64#define INT64_C(c) (c ## LL)
65#define UINT64_C(c) (c ## ULL)
66#endif /* __MINGW32__ */
67
1a565432
FB
68#define M_PI 3.14159265358979323846
69#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
70
71#ifdef _DEBUG
72#define DEBUG
420b073b 73#endif
de6d9b64 74
1a565432
FB
75// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
76#define bswap_32(x) \
77 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
78 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
79#define be2me_32(x) bswap_32(x)
80
81#define snprintf _snprintf
82
a74127c0
FB
83#ifndef __MINGW32__
84/* no config.h with VC */
1a565432
FB
85#define CONFIG_ENCODERS 1
86#define CONFIG_DECODERS 1
87#define CONFIG_AC3 1
a74127c0 88#endif
1a565432
FB
89
90#else
91
92/* unix */
93
94#include <inttypes.h>
95
980fc7b8
FB
96#ifndef __WINE_WINDEF16_H
97/* workaround for typedef conflict in MPlayer (wine typedefs) */
de6d9b64
FB
98typedef unsigned short UINT16;
99typedef signed short INT16;
100#endif
101
102typedef unsigned char UINT8;
103typedef unsigned int UINT32;
104typedef unsigned long long UINT64;
105typedef signed char INT8;
106typedef signed int INT32;
107typedef signed long long INT64;
108
1a565432
FB
109#ifdef HAVE_AV_CONFIG_H
110
111#ifdef __FreeBSD__
112#include <sys/param.h>
113#endif
114
115#ifndef INT64_C
116#define INT64_C(c) (c ## LL)
117#define UINT64_C(c) (c ## ULL)
118#endif
119
120#include "../bswap.h"
121
122#ifdef USE_FASTMEMCPY
123#include "fastmemcpy.h"
124#endif
125
a74127c0
FB
126#endif /* HAVE_AV_CONFIG_H */
127
128#endif /* !CONFIG_WIN32 */
129
27a3e2c5 130
a74127c0
FB
131/* debug stuff */
132#ifdef HAVE_AV_CONFIG_H
133
1a565432
FB
134#ifndef DEBUG
135#define NDEBUG
136#endif
137#include <assert.h>
138
a74127c0
FB
139/* dprintf macros */
140#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
141
142inline void dprintf(const char* fmt,...) {}
143
144#else
145
146#ifdef DEBUG
147#define dprintf(fmt,args...) printf(fmt, ## args)
148#else
149#define dprintf(fmt,args...)
150#endif
1a565432
FB
151
152#endif /* !CONFIG_WIN32 */
153
a74127c0
FB
154#endif /* HAVE_AV_CONFIG_H */
155
de6d9b64
FB
156/* bit output */
157
158struct PutBitContext;
159
160typedef void (*WriteDataFunc)(void *, UINT8 *, int);
161
162typedef struct PutBitContext {
de6d9b64 163 UINT32 bit_buf;
2931ecb9
A
164 int bit_cnt;
165 UINT8 *buf, *buf_ptr, *buf_end;
1a565432 166 INT64 data_out_size; /* in bytes */
de6d9b64
FB
167 void *opaque;
168 WriteDataFunc write_data;
169} PutBitContext;
170
171void init_put_bits(PutBitContext *s,
172 UINT8 *buffer, int buffer_size,
173 void *opaque,
174 void (*write_data)(void *, UINT8 *, int));
175void put_bits(PutBitContext *s, int n, unsigned int value);
9dbf9389 176INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
de6d9b64
FB
177void align_put_bits(PutBitContext *s);
178void flush_put_bits(PutBitContext *s);
179
180/* jpeg specific put_bits */
181void jput_bits(PutBitContext *s, int n, unsigned int value);
182void jflush_put_bits(PutBitContext *s);
183
184/* bit input */
185
186typedef struct GetBitContext {
27a3e2c5
MN
187#ifdef ALT_BITSTREAM_READER
188 int index;
189 UINT8 *buffer;
190#else
de6d9b64 191 UINT32 bit_buf;
2931ecb9
A
192 int bit_cnt;
193 UINT8 *buf, *buf_ptr, *buf_end;
27a3e2c5 194#endif
de6d9b64
FB
195} GetBitContext;
196
197typedef struct VLC {
198 int bits;
199 INT16 *table_codes;
200 INT8 *table_bits;
201 int table_size, table_allocated;
202} VLC;
203
3587d501
MN
204/* used to avoid missaligned exceptions on some archs (alpha, ...) */
205#ifdef ARCH_X86
206#define unaligned32(a) (*(UINT32*)(a))
207#else
208#ifdef __GNUC__
209static inline uint32_t unaligned32(const void *v) {
210 struct Unaligned {
211 uint32_t i;
212 } __attribute__((packed));
213
214 return ((const struct Unaligned *) v)->i;
215}
216#elif defined(__DECC)
217static inline uint32_t unaligned32(const void *v) {
218 return *(const __unaligned uint32_t *) v;
219}
220#else
221static inline uint32_t unaligned32(const void *v) {
222 return *(const uint32_t *) v;
223}
224#endif
225#endif //!ARCH_X86
226
de6d9b64
FB
227void init_get_bits(GetBitContext *s,
228 UINT8 *buffer, int buffer_size);
229
27a3e2c5 230#ifndef ALT_BITSTREAM_READER
2931ecb9 231unsigned int get_bits_long(GetBitContext *s, int n);
4949028f 232unsigned int show_bits_long(GetBitContext *s, int n);
27a3e2c5 233#endif
2931ecb9
A
234
235static inline unsigned int get_bits(GetBitContext *s, int n){
27a3e2c5 236#ifdef ALT_BITSTREAM_READER
3587d501
MN
237#ifdef ALIGNED_BITSTREAM
238 int index= s->index;
239 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
240 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
241#ifdef ARCH_X86
242 asm ("shldl %%cl, %2, %0\n\t"
243 : "=r" (result1)
244 : "0" (result1), "r" (result2), "c" (index));
245#else
246 result1<<= (index&0x1F);
247 result2>>= 32-(index&0x1F);
248 if((index&0x1F)!=0) result1|= result2;
249#endif
250 result1>>= 32 - n;
251 index+= n;
252 s->index= index;
253
254 return result1;
255#else //ALIGNED_BITSTREAM
27a3e2c5 256 int index= s->index;
3587d501 257 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
27a3e2c5
MN
258
259 result<<= (index&0x07);
260 result>>= 32 - n;
261 index+= n;
262 s->index= index;
263
264 return result;
3587d501
MN
265#endif //!ALIGNED_BITSTREAM
266#else //ALT_BITSTREAM_READER
2931ecb9
A
267 if(s->bit_cnt>=n){
268 /* most common case here */
269 unsigned int val = s->bit_buf >> (32 - n);
270 s->bit_buf <<= n;
271 s->bit_cnt -= n;
272#ifdef STATS
273 st_bit_counts[st_current_index] += n;
274#endif
275 return val;
276 }
277 return get_bits_long(s,n);
3587d501 278#endif //!ALT_BITSTREAM_READER
2931ecb9
A
279}
280
612476ef 281static inline unsigned int get_bits1(GetBitContext *s){
27a3e2c5
MN
282#ifdef ALT_BITSTREAM_READER
283 int index= s->index;
3587d501
MN
284 uint8_t result= s->buffer[ index>>3 ];
285 result>>= 7-(index&0x07);
286 result&=1;
27a3e2c5
MN
287 index++;
288 s->index= index;
289
290 return result;
291#else
612476ef
A
292 if(s->bit_cnt>0){
293 /* most common case here */
294 unsigned int val = s->bit_buf >> 31;
295 s->bit_buf <<= 1;
296 s->bit_cnt--;
297#ifdef STATS
298 st_bit_counts[st_current_index]++;
299#endif
300 return val;
301 }
302 return get_bits_long(s,1);
27a3e2c5 303#endif
612476ef
A
304}
305
4949028f
J
306/* This function is identical to get_bits(), the only */
307/* diference is that it doesn't touch the buffer */
308/* it is usefull to see the buffer. */
309static inline unsigned int show_bits(GetBitContext *s, int n)
310{
27a3e2c5 311#ifdef ALT_BITSTREAM_READER
3587d501
MN
312#ifdef ALIGNED_BITSTREAM
313 int index= s->index;
314 uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
315 uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
316#ifdef ARCH_X86
317 asm ("shldl %%cl, %2, %0\n\t"
318 : "=r" (result1)
319 : "0" (result1), "r" (result2), "c" (index));
320#else
321 result1<<= (index&0x1F);
322 result2>>= 32-(index&0x1F);
323 if((index&0x1F)!=0) result1|= result2;
324#endif
325 result1>>= 32 - n;
326
327 return result1;
328#else //ALIGNED_BITSTREAM
27a3e2c5 329 int index= s->index;
3587d501 330 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
27a3e2c5
MN
331
332 result<<= (index&0x07);
333 result>>= 32 - n;
334
335 return result;
3587d501
MN
336#endif //!ALIGNED_BITSTREAM
337#else //ALT_BITSTREAM_READER
4949028f
J
338 if(s->bit_cnt>=n) {
339 /* most common case here */
340 unsigned int val = s->bit_buf >> (32 - n);
341 return val;
342 }
343 return show_bits_long(s,n);
3587d501 344#endif //!ALT_BITSTREAM_READER
4949028f
J
345}
346
612476ef 347static inline void skip_bits(GetBitContext *s, int n){
27a3e2c5
MN
348#ifdef ALT_BITSTREAM_READER
349 s->index+= n;
350#else
612476ef
A
351 if(s->bit_cnt>=n){
352 /* most common case here */
353 s->bit_buf <<= n;
354 s->bit_cnt -= n;
355#ifdef STATS
356 st_bit_counts[st_current_index] += n;
357#endif
358 } else {
359 get_bits_long(s,n);
360 }
27a3e2c5 361#endif
612476ef
A
362}
363
364static inline void skip_bits1(GetBitContext *s){
27a3e2c5
MN
365#ifdef ALT_BITSTREAM_READER
366 s->index++;
367#else
612476ef
A
368 if(s->bit_cnt>0){
369 /* most common case here */
370 s->bit_buf <<= 1;
371 s->bit_cnt--;
372#ifdef STATS
373 st_bit_counts[st_current_index]++;
374#endif
375 } else {
376 get_bits_long(s,1);
377 }
27a3e2c5 378#endif
612476ef
A
379}
380
9dbf9389
FB
381static inline int get_bits_count(GetBitContext *s)
382{
27a3e2c5
MN
383#ifdef ALT_BITSTREAM_READER
384 return s->index;
385#else
9dbf9389 386 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
27a3e2c5 387#endif
9dbf9389 388}
612476ef 389
de6d9b64
FB
390void align_get_bits(GetBitContext *s);
391int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
392 const void *bits, int bits_wrap, int bits_size,
393 const void *codes, int codes_wrap, int codes_size);
394void free_vlc(VLC *vlc);
de6d9b64 395
27a3e2c5
MN
396#ifdef ALT_BITSTREAM_READER
397#define SHOW_BITS(s, val, n) val= show_bits(s, n);
398#define FLUSH_BITS(n) skip_bits(s, n);
399#define SAVE_BITS(s) ;
400#define RESTORE_BITS(s) ;
401#else
402
de6d9b64
FB
403/* macro to go faster */
404/* n must be <= 24 */
405/* XXX: optimize buffer end test */
406#define SHOW_BITS(s, val, n)\
407{\
408 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
409 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
410 bit_cnt += 8;\
411 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
412 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
413 bit_cnt += 8;\
414 if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
415 bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
416 bit_cnt += 8;\
417 }\
418 }\
419 }\
420 val = bit_buf >> (32 - n);\
421}
422
423/* SHOW_BITS with n1 >= n must be been done before */
424#define FLUSH_BITS(n)\
425{\
426 bit_buf <<= n;\
427 bit_cnt -= n;\
428}
429
430#define SAVE_BITS(s) \
431{\
432 bit_cnt = (s)->bit_cnt;\
433 bit_buf = (s)->bit_buf;\
434 buf_ptr = (s)->buf_ptr;\
435}
436
437#define RESTORE_BITS(s) \
438{\
439 (s)->buf_ptr = buf_ptr;\
440 (s)->bit_buf = bit_buf;\
441 (s)->bit_cnt = bit_cnt;\
442}
27a3e2c5 443#endif // !ALT_BITSTREAM_READER
3587d501
MN
444
445static inline int get_vlc(GetBitContext *s, VLC *vlc)
446{
447 int code, n, nb_bits, index;
448 INT16 *table_codes;
449 INT8 *table_bits;
450#ifndef ALT_BITSTREAM_READER
451 int bit_cnt;
452 UINT32 bit_buf;
453 UINT8 *buf_ptr;
454#endif
455
456 SAVE_BITS(s);
457 nb_bits = vlc->bits;
458 table_codes = vlc->table_codes;
459 table_bits = vlc->table_bits;
460
461 SHOW_BITS(s, index, nb_bits);
462 code = table_codes[index];
463 n = table_bits[index];
464 if (n > 0) {
465 /* most common case (90%)*/
466 FLUSH_BITS(n);
467 RESTORE_BITS(s);
468 return code;
469 } else if (n == 0) {
470 return -1;
471 } else {
472 FLUSH_BITS(nb_bits);
473 nb_bits = -n;
474 table_codes = vlc->table_codes + code;
475 table_bits = vlc->table_bits + code;
476 }
477 for(;;) {
478 SHOW_BITS(s, index, nb_bits);
479 code = table_codes[index];
480 n = table_bits[index];
481 if (n > 0) {
482 /* most common case */
483 FLUSH_BITS(n);
484#ifdef STATS
485 st_bit_counts[st_current_index] += n;
486#endif
487 break;
488 } else if (n == 0) {
489 return -1;
490 } else {
491 FLUSH_BITS(nb_bits);
492#ifdef STATS
493 st_bit_counts[st_current_index] += nb_bits;
494#endif
495 nb_bits = -n;
496 table_codes = vlc->table_codes + code;
497 table_bits = vlc->table_bits + code;
498 }
499 }
500 RESTORE_BITS(s);
501 return code;
502}
503
504
de6d9b64
FB
505/* define it to include statistics code (useful only for optimizing
506 codec efficiency */
507//#define STATS
508
509#ifdef STATS
510
511enum {
512 ST_UNKNOWN,
513 ST_DC,
514 ST_INTRA_AC,
515 ST_INTER_AC,
516 ST_INTRA_MB,
517 ST_INTER_MB,
518 ST_MV,
519 ST_NB,
520};
521
522extern int st_current_index;
523extern unsigned int st_bit_counts[ST_NB];
524extern unsigned int st_out_bit_counts[ST_NB];
525
526void print_stats(void);
527#endif
528
529/* misc math functions */
530
a822a479 531static inline int av_log2(unsigned int v)
de6d9b64
FB
532{
533 int n;
534
535 n = 0;
536 if (v & 0xffff0000) {
537 v >>= 16;
538 n += 16;
539 }
540 if (v & 0xff00) {
541 v >>= 8;
542 n += 8;
543 }
544 if (v & 0xf0) {
545 v >>= 4;
546 n += 4;
547 }
548 if (v & 0xc) {
549 v >>= 2;
550 n += 2;
551 }
552 if (v & 0x2) {
553 n++;
554 }
555 return n;
556}
557
558/* memory */
559void *av_mallocz(int size);
560
561#endif