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