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