a0586a3d78d7731fdca9b4f85e6806061ca2902c
[libav.git] / libavcodec / common.h
1 #ifndef COMMON_H
2 #define COMMON_H
3
4 #define FFMPEG_VERSION_INT 0x000406
5 #define FFMPEG_VERSION "0.4.6"
6
7 #if defined(WIN32) && !defined(__MINGW32__)
8 #define CONFIG_WIN32
9 #endif
10
11 //#define ALT_BITSTREAM_READER
12 //#define ALIGNED_BITSTREAM
13
14 #ifdef HAVE_AV_CONFIG_H
15 /* only include the following when compiling package */
16 #include "../config.h"
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
33 typedef unsigned short UINT16;
34 typedef signed short INT16;
35 typedef unsigned char UINT8;
36 typedef unsigned int UINT32;
37 typedef unsigned __int64 UINT64;
38 typedef signed char INT8;
39 typedef signed int INT32;
40 typedef signed __int64 INT64;
41
42 typedef UINT8 uint8_t;
43 typedef INT8 int8_t;
44 typedef UINT16 uint16_t;
45 typedef INT16 int16_t;
46 typedef UINT32 uint32_t;
47 typedef INT32 int32_t;
48
49 #ifndef __MINGW32__
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
63 #else
64 #define INT64_C(c) (c ## LL)
65 #define UINT64_C(c) (c ## ULL)
66 #endif /* __MINGW32__ */
67
68 #define M_PI 3.14159265358979323846
69 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
70
71 #ifdef _DEBUG
72 #define DEBUG
73 #endif
74
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
83 #ifndef __MINGW32__
84 /* no config.h with VC */
85 #define CONFIG_ENCODERS 1
86 #define CONFIG_DECODERS 1
87 #define CONFIG_AC3 1
88 #endif
89
90 #else
91
92 /* unix */
93
94 #include <inttypes.h>
95
96 #ifndef __WINE_WINDEF16_H
97 /* workaround for typedef conflict in MPlayer (wine typedefs) */
98 typedef unsigned short UINT16;
99 typedef signed short INT16;
100 #endif
101
102 typedef unsigned char UINT8;
103 typedef unsigned int UINT32;
104 typedef unsigned long long UINT64;
105 typedef signed char INT8;
106 typedef signed int INT32;
107 typedef signed long long INT64;
108
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
126 #endif /* HAVE_AV_CONFIG_H */
127
128 #endif /* !CONFIG_WIN32 */
129
130
131 /* debug stuff */
132 #ifdef HAVE_AV_CONFIG_H
133
134 #ifndef DEBUG
135 #define NDEBUG
136 #endif
137 #include <assert.h>
138
139 /* dprintf macros */
140 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
141
142 inline 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
151
152 #endif /* !CONFIG_WIN32 */
153
154 #endif /* HAVE_AV_CONFIG_H */
155
156 /* bit output */
157
158 struct PutBitContext;
159
160 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
161
162 typedef struct PutBitContext {
163 UINT32 bit_buf;
164 int bit_cnt;
165 UINT8 *buf, *buf_ptr, *buf_end;
166 INT64 data_out_size; /* in bytes */
167 void *opaque;
168 WriteDataFunc write_data;
169 } PutBitContext;
170
171 void init_put_bits(PutBitContext *s,
172 UINT8 *buffer, int buffer_size,
173 void *opaque,
174 void (*write_data)(void *, UINT8 *, int));
175 void put_bits(PutBitContext *s, int n, unsigned int value);
176 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
177 void align_put_bits(PutBitContext *s);
178 void flush_put_bits(PutBitContext *s);
179
180 /* jpeg specific put_bits */
181 void jput_bits(PutBitContext *s, int n, unsigned int value);
182 void jflush_put_bits(PutBitContext *s);
183
184 /* bit input */
185
186 typedef struct GetBitContext {
187 #ifdef ALT_BITSTREAM_READER
188 int index;
189 UINT8 *buffer;
190 #else
191 UINT32 bit_buf;
192 int bit_cnt;
193 UINT8 *buf, *buf_ptr, *buf_end;
194 #endif
195 } GetBitContext;
196
197 typedef struct VLC {
198 int bits;
199 INT16 *table_codes;
200 INT8 *table_bits;
201 int table_size, table_allocated;
202 } VLC;
203
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__
209 static 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)
217 static inline uint32_t unaligned32(const void *v) {
218 return *(const __unaligned uint32_t *) v;
219 }
220 #else
221 static inline uint32_t unaligned32(const void *v) {
222 return *(const uint32_t *) v;
223 }
224 #endif
225 #endif //!ARCH_X86
226
227 void init_get_bits(GetBitContext *s,
228 UINT8 *buffer, int buffer_size);
229
230 #ifndef ALT_BITSTREAM_READER
231 unsigned int get_bits_long(GetBitContext *s, int n);
232 unsigned int show_bits_long(GetBitContext *s, int n);
233 #endif
234
235 static inline unsigned int get_bits(GetBitContext *s, int n){
236 #ifdef ALT_BITSTREAM_READER
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
256 int index= s->index;
257 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
258
259 result<<= (index&0x07);
260 result>>= 32 - n;
261 index+= n;
262 s->index= index;
263
264 return result;
265 #endif //!ALIGNED_BITSTREAM
266 #else //ALT_BITSTREAM_READER
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);
278 #endif //!ALT_BITSTREAM_READER
279 }
280
281 static inline unsigned int get_bits1(GetBitContext *s){
282 #ifdef ALT_BITSTREAM_READER
283 int index= s->index;
284 uint8_t result= s->buffer[ index>>3 ];
285 result>>= 7-(index&0x07);
286 result&=1;
287 index++;
288 s->index= index;
289
290 return result;
291 #else
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);
303 #endif
304 }
305
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. */
309 static inline unsigned int show_bits(GetBitContext *s, int n)
310 {
311 #ifdef ALT_BITSTREAM_READER
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
329 int index= s->index;
330 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
331
332 result<<= (index&0x07);
333 result>>= 32 - n;
334
335 return result;
336 #endif //!ALIGNED_BITSTREAM
337 #else //ALT_BITSTREAM_READER
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);
344 #endif //!ALT_BITSTREAM_READER
345 }
346
347 static inline void skip_bits(GetBitContext *s, int n){
348 #ifdef ALT_BITSTREAM_READER
349 s->index+= n;
350 #else
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 }
361 #endif
362 }
363
364 static inline void skip_bits1(GetBitContext *s){
365 #ifdef ALT_BITSTREAM_READER
366 s->index++;
367 #else
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 }
378 #endif
379 }
380
381 static inline int get_bits_count(GetBitContext *s)
382 {
383 #ifdef ALT_BITSTREAM_READER
384 return s->index;
385 #else
386 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
387 #endif
388 }
389
390 void align_get_bits(GetBitContext *s);
391 int 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);
394 void free_vlc(VLC *vlc);
395
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
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 }
443 #endif // !ALT_BITSTREAM_READER
444
445 static 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
505 /* define it to include statistics code (useful only for optimizing
506 codec efficiency */
507 //#define STATS
508
509 #ifdef STATS
510
511 enum {
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
522 extern int st_current_index;
523 extern unsigned int st_bit_counts[ST_NB];
524 extern unsigned int st_out_bit_counts[ST_NB];
525
526 void print_stats(void);
527 #endif
528
529 /* misc math functions */
530
531 static inline int av_log2(unsigned int v)
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 */
559 void *av_mallocz(int size);
560
561 #endif