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