(commited by michael / arpi was crazy enough to give me his password)
[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 #define FAST_GET_FIRST_VLC
14
15 #ifdef HAVE_AV_CONFIG_H
16 /* only include the following when compiling package */
17 #include "../config.h"
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
34 typedef unsigned short UINT16;
35 typedef signed short INT16;
36 typedef unsigned char UINT8;
37 typedef unsigned int UINT32;
38 typedef unsigned __int64 UINT64;
39 typedef signed char INT8;
40 typedef signed int INT32;
41 typedef signed __int64 INT64;
42
43 typedef UINT8 uint8_t;
44 typedef INT8 int8_t;
45 typedef UINT16 uint16_t;
46 typedef INT16 int16_t;
47 typedef UINT32 uint32_t;
48 typedef INT32 int32_t;
49
50 #ifndef __MINGW32__
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
64 #else
65 #define INT64_C(c) (c ## LL)
66 #define UINT64_C(c) (c ## ULL)
67 #endif /* __MINGW32__ */
68
69 #define M_PI 3.14159265358979323846
70 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
71
72 #ifdef _DEBUG
73 #define DEBUG
74 #endif
75
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
84 #ifndef __MINGW32__
85 /* no config.h with VC */
86 #define CONFIG_ENCODERS 1
87 #define CONFIG_DECODERS 1
88 #define CONFIG_AC3 1
89 #endif
90
91 #else
92
93 /* unix */
94
95 #include <inttypes.h>
96
97 #ifndef __WINE_WINDEF16_H
98 /* workaround for typedef conflict in MPlayer (wine typedefs) */
99 typedef unsigned short UINT16;
100 typedef signed short INT16;
101 #endif
102
103 typedef unsigned char UINT8;
104 typedef unsigned int UINT32;
105 typedef unsigned long long UINT64;
106 typedef signed char INT8;
107 typedef signed int INT32;
108 typedef signed long long INT64;
109
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
127 #endif /* HAVE_AV_CONFIG_H */
128
129 #endif /* !CONFIG_WIN32 */
130
131
132 /* debug stuff */
133 #ifdef HAVE_AV_CONFIG_H
134
135 #ifndef DEBUG
136 #define NDEBUG
137 #endif
138 #include <assert.h>
139
140 /* dprintf macros */
141 #if defined(CONFIG_WIN32) && !defined(__MINGW32__)
142
143 inline 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
152
153 #endif /* !CONFIG_WIN32 */
154
155 #endif /* HAVE_AV_CONFIG_H */
156
157 /* bit output */
158
159 struct PutBitContext;
160
161 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
162
163 typedef struct PutBitContext {
164 UINT32 bit_buf;
165 int bit_cnt;
166 UINT8 *buf, *buf_ptr, *buf_end;
167 INT64 data_out_size; /* in bytes */
168 void *opaque;
169 WriteDataFunc write_data;
170 } PutBitContext;
171
172 void init_put_bits(PutBitContext *s,
173 UINT8 *buffer, int buffer_size,
174 void *opaque,
175 void (*write_data)(void *, UINT8 *, int));
176 void put_bits(PutBitContext *s, int n, unsigned int value);
177 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
178 void align_put_bits(PutBitContext *s);
179 void flush_put_bits(PutBitContext *s);
180
181 /* jpeg specific put_bits */
182 void jput_bits(PutBitContext *s, int n, unsigned int value);
183 void jflush_put_bits(PutBitContext *s);
184
185 /* bit input */
186
187 typedef struct GetBitContext {
188 #ifdef ALT_BITSTREAM_READER
189 int index;
190 UINT8 *buffer;
191 #else
192 UINT32 bit_buf;
193 int bit_cnt;
194 UINT8 *buf, *buf_ptr, *buf_end;
195 #endif
196 } GetBitContext;
197
198 typedef struct VLC {
199 int bits;
200 INT16 *table_codes;
201 INT8 *table_bits;
202 int table_size, table_allocated;
203 } VLC;
204
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__
210 static 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)
218 static inline uint32_t unaligned32(const void *v) {
219 return *(const __unaligned uint32_t *) v;
220 }
221 #else
222 static inline uint32_t unaligned32(const void *v) {
223 return *(const uint32_t *) v;
224 }
225 #endif
226 #endif //!ARCH_X86
227
228 void init_get_bits(GetBitContext *s,
229 UINT8 *buffer, int buffer_size);
230
231 #ifndef ALT_BITSTREAM_READER
232 unsigned int get_bits_long(GetBitContext *s, int n);
233 unsigned int show_bits_long(GetBitContext *s, int n);
234 #endif
235
236 static inline unsigned int get_bits(GetBitContext *s, int n){
237 #ifdef ALT_BITSTREAM_READER
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);
248 result2= (result2>>1) >> (31-(index&0x1F));
249 result1|= result2;
250 #endif
251 result1>>= 32 - n;
252 index+= n;
253 s->index= index;
254
255 return result1;
256 #else //ALIGNED_BITSTREAM
257 int index= s->index;
258 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
259
260 result<<= (index&0x07);
261 result>>= 32 - n;
262 index+= n;
263 s->index= index;
264
265 return result;
266 #endif //!ALIGNED_BITSTREAM
267 #else //ALT_BITSTREAM_READER
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);
279 #endif //!ALT_BITSTREAM_READER
280 }
281
282 static inline unsigned int get_bits1(GetBitContext *s){
283 #ifdef ALT_BITSTREAM_READER
284 int index= s->index;
285 uint8_t result= s->buffer[ index>>3 ];
286 result<<= (index&0x07);
287 result>>= 8 - 1;
288 index++;
289 s->index= index;
290
291 return result;
292 #else
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);
304 #endif
305 }
306
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. */
310 static inline unsigned int show_bits(GetBitContext *s, int n)
311 {
312 #ifdef ALT_BITSTREAM_READER
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);
323 result2= (result2>>1) >> (31-(index&0x1F));
324 result1|= result2;
325 #endif
326 result1>>= 32 - n;
327
328 return result1;
329 #else //ALIGNED_BITSTREAM
330 int index= s->index;
331 uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
332
333 result<<= (index&0x07);
334 result>>= 32 - n;
335
336 return result;
337 #endif //!ALIGNED_BITSTREAM
338 #else //ALT_BITSTREAM_READER
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);
345 #endif //!ALT_BITSTREAM_READER
346 }
347
348 static inline void skip_bits(GetBitContext *s, int n){
349 #ifdef ALT_BITSTREAM_READER
350 s->index+= n;
351 #else
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 }
362 #endif
363 }
364
365 static inline void skip_bits1(GetBitContext *s){
366 #ifdef ALT_BITSTREAM_READER
367 s->index++;
368 #else
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 }
379 #endif
380 }
381
382 static inline int get_bits_count(GetBitContext *s)
383 {
384 #ifdef ALT_BITSTREAM_READER
385 return s->index;
386 #else
387 return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
388 #endif
389 }
390
391 void align_get_bits(GetBitContext *s);
392 int 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);
395 void free_vlc(VLC *vlc);
396
397 #ifdef ALT_BITSTREAM_READER
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;
425 #else
426
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 }
467 #endif // !ALT_BITSTREAM_READER
468
469 static inline int get_vlc(GetBitContext *s, VLC *vlc)
470 {
471 int code, n, nb_bits, index;
472 INT16 *table_codes;
473 INT8 *table_bits;
474 int bit_cnt;
475 #ifndef ALT_BITSTREAM_READER
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;
484
485 #ifdef FAST_GET_FIRST_VLC
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 }
502 #endif
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
531 /* define it to include statistics code (useful only for optimizing
532 codec efficiency */
533 //#define STATS
534
535 #ifdef STATS
536
537 enum {
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
548 extern int st_current_index;
549 extern unsigned int st_bit_counts[ST_NB];
550 extern unsigned int st_out_bit_counts[ST_NB];
551
552 void print_stats(void);
553 #endif
554
555 /* misc math functions */
556
557 static inline int av_log2(unsigned int v)
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 */
585 void *av_mallocz(int size);
586
587 #endif