1f9f8a2d5723fd423eee715edfec970382a79e9d
[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__) && !defined(__CYGWIN__)
8 # define CONFIG_WIN32
9 #endif
10
11 //#define ALT_BITSTREAM_WRITER
12 //#define ALIGNED_BITSTREAM_WRITER
13
14 #define ALT_BITSTREAM_READER
15 //#define LIBMPEG2_BITSTREAM_READER
16 //#define A32_BITSTREAM_READER
17
18 #ifdef HAVE_AV_CONFIG_H
19 /* only include the following when compiling package */
20 # include "config.h"
21
22 # include <stdlib.h>
23 # include <stdio.h>
24 # include <string.h>
25 # ifndef __BEOS__
26 # include <errno.h>
27 # else
28 # include "berrno.h"
29 # endif
30 # include <math.h>
31
32 # ifndef ENODATA
33 # define ENODATA 61
34 # endif
35
36 #endif /* HAVE_AV_CONFIG_H */
37
38 /* Suppress restrict if it was not defined in config.h. */
39 #ifndef restrict
40 # define restrict
41 #endif
42
43 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
44 # define always_inline __attribute__((always_inline)) inline
45 #else
46 # define always_inline inline
47 #endif
48
49 #ifdef CONFIG_WIN32
50
51 /* windows */
52
53 typedef unsigned short UINT16;
54 typedef signed short INT16;
55 typedef unsigned char UINT8;
56 typedef unsigned int UINT32;
57 typedef unsigned __int64 UINT64;
58 typedef signed char INT8;
59 typedef signed int INT32;
60 typedef signed __int64 INT64;
61
62 typedef UINT8 uint8_t;
63 typedef INT8 int8_t;
64 typedef UINT16 uint16_t;
65 typedef INT16 int16_t;
66 typedef UINT32 uint32_t;
67 typedef INT32 int32_t;
68 typedef UINT64 uint64_t;
69 typedef INT64 int64_t;
70
71 # ifndef __MINGW32__
72 # define INT64_C(c) (c ## i64)
73 # define UINT64_C(c) (c ## i64)
74
75 # define inline __inline
76
77 # else
78 # define INT64_C(c) (c ## LL)
79 # define UINT64_C(c) (c ## ULL)
80 # endif /* __MINGW32__ */
81
82 # define M_PI 3.14159265358979323846
83 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
84
85 # ifdef _DEBUG
86 # define DEBUG
87 # endif
88
89 # define snprintf _snprintf
90
91 #else /* CONFIG_WIN32 */
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 # ifndef INT64_C
113 # define INT64_C(c) (c ## LL)
114 # define UINT64_C(c) (c ## ULL)
115 # endif
116
117 # ifdef USE_FASTMEMCPY
118 # include "fastmemcpy.h"
119 # endif
120
121 # endif /* HAVE_AV_CONFIG_H */
122
123 #endif /* !CONFIG_WIN32 */
124
125 #ifdef HAVE_AV_CONFIG_H
126
127 # include "bswap.h"
128
129 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
130 defined(__OS2__) || defined (__OpenBSD__)
131 # define MANGLE(a) "_" #a
132 # else
133 # define MANGLE(a) #a
134 # endif
135
136 /* debug stuff */
137
138 # ifndef DEBUG
139 # define NDEBUG
140 # endif
141 # include <assert.h>
142
143 /* dprintf macros */
144 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
145
146 inline void dprintf(const char* fmt,...) {}
147
148 # else
149
150 # ifdef DEBUG
151 # define dprintf(fmt,args...) printf(fmt, ## args)
152 # else
153 # define dprintf(fmt,args...)
154 # endif
155
156 # endif /* !CONFIG_WIN32 */
157
158 # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
159
160 //rounded divison & shift
161 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
162 /* assume b>0 */
163 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
164 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
165
166 #ifndef MAX
167 # define MAX(a,b) ((a) > (b) ? (a) : (b))
168 #endif
169
170 #ifndef MIN
171 # define MIN(a,b) ((a) > (b) ? (b) : (a))
172 #endif
173
174 #ifdef ARCH_X86
175 // avoid +32 for shift optimization (gcc should do that ...)
176 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
177 asm ("sarl %1, %0\n\t"
178 : "+r" (a)
179 : "ic" ((uint8_t)(-s))
180 );
181 return a;
182 }
183 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
184 asm ("shrl %1, %0\n\t"
185 : "+r" (a)
186 : "ic" ((uint8_t)(-s))
187 );
188 return a;
189 }
190 #else
191 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
192 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
193 #endif
194
195 /* bit output */
196
197 struct PutBitContext;
198
199 typedef void (*WriteDataFunc)(void *, UINT8 *, int);
200
201 typedef struct PutBitContext {
202 #ifdef ALT_BITSTREAM_WRITER
203 UINT8 *buf, *buf_end;
204 int index;
205 #else
206 UINT32 bit_buf;
207 int bit_left;
208 UINT8 *buf, *buf_ptr, *buf_end;
209 #endif
210 INT64 data_out_size; /* in bytes */
211 } PutBitContext;
212
213 void init_put_bits(PutBitContext *s,
214 UINT8 *buffer, int buffer_size,
215 void *opaque,
216 void (*write_data)(void *, UINT8 *, int));
217
218 INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
219 void align_put_bits(PutBitContext *s);
220 void flush_put_bits(PutBitContext *s);
221 void put_string(PutBitContext * pbc, char *s);
222
223 /* jpeg specific put_bits */
224 void jflush_put_bits(PutBitContext *s);
225
226 /* bit input */
227
228 typedef struct GetBitContext {
229 UINT8 *buffer, *buffer_end;
230 #ifdef ALT_BITSTREAM_READER
231 int index;
232 #elif defined LIBMPEG2_BITSTREAM_READER
233 UINT8 *buffer_ptr;
234 UINT32 cache;
235 int bit_count;
236 #elif defined A32_BITSTREAM_READER
237 UINT32 *buffer_ptr;
238 UINT32 cache0;
239 UINT32 cache1;
240 int bit_count;
241 #endif
242 int size;
243 } GetBitContext;
244
245 static inline int get_bits_count(GetBitContext *s);
246
247 #define VLC_TYPE INT16
248
249 typedef struct VLC {
250 int bits;
251 VLC_TYPE (*table)[2]; // code, bits
252 int table_size, table_allocated;
253 } VLC;
254
255 typedef struct RL_VLC_ELEM {
256 int16_t level;
257 int8_t len;
258 uint8_t run;
259 } RL_VLC_ELEM;
260
261 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
262 #ifdef ARCH_X86
263 # define unaligned32(a) (*(UINT32*)(a))
264 #else
265 # ifdef __GNUC__
266 static inline uint32_t unaligned32(const void *v) {
267 struct Unaligned {
268 uint32_t i;
269 } __attribute__((packed));
270
271 return ((const struct Unaligned *) v)->i;
272 }
273 # elif defined(__DECC)
274 static inline uint32_t unaligned32(const void *v) {
275 return *(const __unaligned uint32_t *) v;
276 }
277 # else
278 static inline uint32_t unaligned32(const void *v) {
279 return *(const uint32_t *) v;
280 }
281 # endif
282 #endif //!ARCH_X86
283
284 #ifndef ALT_BITSTREAM_WRITER
285 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
286 {
287 unsigned int bit_buf;
288 int bit_left;
289
290 #ifdef STATS
291 st_out_bit_counts[st_current_index] += n;
292 #endif
293 // printf("put_bits=%d %x\n", n, value);
294 assert(n == 32 || value < (1U << n));
295
296 bit_buf = s->bit_buf;
297 bit_left = s->bit_left;
298
299 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
300 /* XXX: optimize */
301 if (n < bit_left) {
302 bit_buf = (bit_buf<<n) | value;
303 bit_left-=n;
304 } else {
305 bit_buf<<=bit_left;
306 bit_buf |= value >> (n - bit_left);
307 *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
308 //printf("bitbuf = %08x\n", bit_buf);
309 s->buf_ptr+=4;
310 bit_left+=32 - n;
311 bit_buf = value;
312 }
313
314 s->bit_buf = bit_buf;
315 s->bit_left = bit_left;
316 }
317 #endif
318
319
320 #ifdef ALT_BITSTREAM_WRITER
321 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
322 {
323 # ifdef ALIGNED_BITSTREAM_WRITER
324 # ifdef ARCH_X86
325 asm volatile(
326 "movl %0, %%ecx \n\t"
327 "xorl %%eax, %%eax \n\t"
328 "shrdl %%cl, %1, %%eax \n\t"
329 "shrl %%cl, %1 \n\t"
330 "movl %0, %%ecx \n\t"
331 "shrl $3, %%ecx \n\t"
332 "andl $0xFFFFFFFC, %%ecx \n\t"
333 "bswapl %1 \n\t"
334 "orl %1, (%2, %%ecx) \n\t"
335 "bswapl %%eax \n\t"
336 "addl %3, %0 \n\t"
337 "movl %%eax, 4(%2, %%ecx) \n\t"
338 : "=&r" (s->index), "=&r" (value)
339 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
340 : "%eax", "%ecx"
341 );
342 # else
343 int index= s->index;
344 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
345
346 value<<= 32-n;
347
348 ptr[0] |= be2me_32(value>>(index&31));
349 ptr[1] = be2me_32(value<<(32-(index&31)));
350 //if(n>24) printf("%d %d\n", n, value);
351 index+= n;
352 s->index= index;
353 # endif
354 # else //ALIGNED_BITSTREAM_WRITER
355 # ifdef ARCH_X86
356 asm volatile(
357 "movl $7, %%ecx \n\t"
358 "andl %0, %%ecx \n\t"
359 "addl %3, %%ecx \n\t"
360 "negl %%ecx \n\t"
361 "shll %%cl, %1 \n\t"
362 "bswapl %1 \n\t"
363 "movl %0, %%ecx \n\t"
364 "shrl $3, %%ecx \n\t"
365 "orl %1, (%%ecx, %2) \n\t"
366 "addl %3, %0 \n\t"
367 "movl $0, 4(%%ecx, %2) \n\t"
368 : "=&r" (s->index), "=&r" (value)
369 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
370 : "%ecx"
371 );
372 # else
373 int index= s->index;
374 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
375
376 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
377 ptr[1] = 0;
378 //if(n>24) printf("%d %d\n", n, value);
379 index+= n;
380 s->index= index;
381 # endif
382 # endif //!ALIGNED_BITSTREAM_WRITER
383 }
384 #endif
385
386 #ifndef ALT_BITSTREAM_WRITER
387 /* for jpeg : escape 0xff with 0x00 after it */
388 static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
389 {
390 unsigned int bit_buf, b;
391 int bit_left, i;
392
393 assert(n == 32 || value < (1U << n));
394
395 bit_buf = s->bit_buf;
396 bit_left = s->bit_left;
397
398 //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
399 /* XXX: optimize */
400 if (n < bit_left) {
401 bit_buf = (bit_buf<<n) | value;
402 bit_left-=n;
403 } else {
404 bit_buf<<=bit_left;
405 bit_buf |= value >> (n - bit_left);
406 /* handle escape */
407 for(i=0;i<4;i++) {
408 b = (bit_buf >> 24);
409 *(s->buf_ptr++) = b;
410 if (b == 0xff)
411 *(s->buf_ptr++) = 0;
412 bit_buf <<= 8;
413 }
414
415 bit_left+= 32 - n;
416 bit_buf = value;
417 }
418
419 s->bit_buf = bit_buf;
420 s->bit_left = bit_left;
421 }
422 #endif
423
424
425 #ifdef ALT_BITSTREAM_WRITER
426 static inline void jput_bits(PutBitContext *s, int n, int value)
427 {
428 int index= s->index;
429 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
430 int v= ptr[0];
431 //if(n>24) printf("%d %d\n", n, value);
432
433 v |= be2me_32(value<<(32-n-(index&7) ));
434 if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
435 {
436 /* handle idiotic (m)jpeg escapes */
437 uint8_t *bPtr= (uint8_t*)ptr;
438 int numChecked= ((index+n)>>3) - (index>>3);
439
440 v= be2me_32(v);
441
442 *(bPtr++)= v>>24;
443 if((v&0xFF000000)==0xFF000000 && numChecked>0){
444 *(bPtr++)= 0x00;
445 index+=8;
446 }
447 *(bPtr++)= (v>>16)&0xFF;
448 if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
449 *(bPtr++)= 0x00;
450 index+=8;
451 }
452 *(bPtr++)= (v>>8)&0xFF;
453 if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
454 *(bPtr++)= 0x00;
455 index+=8;
456 }
457 *(bPtr++)= v&0xFF;
458 if((v&0x000000FF)==0x000000FF && numChecked>3){
459 *(bPtr++)= 0x00;
460 index+=8;
461 }
462 *((uint32_t*)bPtr)= 0;
463 }
464 else
465 {
466 ptr[0] = v;
467 ptr[1] = 0;
468 }
469
470 index+= n;
471 s->index= index;
472 }
473 #endif
474
475 static inline uint8_t* pbBufPtr(PutBitContext *s)
476 {
477 #ifdef ALT_BITSTREAM_WRITER
478 return s->buf + (s->index>>3);
479 #else
480 return s->buf_ptr;
481 #endif
482 }
483
484 /* Bitstream reader API docs:
485 name
486 abritary name which is used as prefix for the internal variables
487
488 gb
489 getbitcontext
490
491 OPEN_READER(name, gb)
492 loads gb into local variables
493
494 CLOSE_READER(name, gb)
495 stores local vars in gb
496
497 UPDATE_CACHE(name, gb)
498 refills the internal cache from the bitstream
499 after this call at least MIN_CACHE_BITS will be available,
500
501 GET_CACHE(name, gb)
502 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
503
504 SHOW_UBITS(name, gb, num)
505 will return the nest num bits
506
507 SHOW_SBITS(name, gb, num)
508 will return the nest num bits and do sign extension
509
510 SKIP_BITS(name, gb, num)
511 will skip over the next num bits
512 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
513
514 SKIP_CACHE(name, gb, num)
515 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
516
517 SKIP_COUNTER(name, gb, num)
518 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
519
520 LAST_SKIP_CACHE(name, gb, num)
521 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
522
523 LAST_SKIP_BITS(name, gb, num)
524 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
525
526 for examples see get_bits, show_bits, skip_bits, get_vlc
527 */
528
529 #ifdef ALT_BITSTREAM_READER
530 # define MIN_CACHE_BITS 25
531
532 # define OPEN_READER(name, gb)\
533 int name##_index= (gb)->index;\
534 int name##_cache= 0;\
535
536 # define CLOSE_READER(name, gb)\
537 (gb)->index= name##_index;\
538
539 # define UPDATE_CACHE(name, gb)\
540 name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
541
542 # define SKIP_CACHE(name, gb, num)\
543 name##_cache <<= (num);\
544
545 // FIXME name?
546 # define SKIP_COUNTER(name, gb, num)\
547 name##_index += (num);\
548
549 # define SKIP_BITS(name, gb, num)\
550 {\
551 SKIP_CACHE(name, gb, num)\
552 SKIP_COUNTER(name, gb, num)\
553 }\
554
555 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
556 # define LAST_SKIP_CACHE(name, gb, num) ;
557
558 # define SHOW_UBITS(name, gb, num)\
559 NEG_USR32(name##_cache, num)
560
561 # define SHOW_SBITS(name, gb, num)\
562 NEG_SSR32(name##_cache, num)
563
564 # define GET_CACHE(name, gb)\
565 ((uint32_t)name##_cache)
566
567 static inline int get_bits_count(GetBitContext *s){
568 return s->index;
569 }
570 #elif defined LIBMPEG2_BITSTREAM_READER
571 //libmpeg2 like reader
572
573 # define MIN_CACHE_BITS 16
574
575 # define OPEN_READER(name, gb)\
576 int name##_bit_count=(gb)->bit_count;\
577 int name##_cache= (gb)->cache;\
578 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
579
580 # define CLOSE_READER(name, gb)\
581 (gb)->bit_count= name##_bit_count;\
582 (gb)->cache= name##_cache;\
583 (gb)->buffer_ptr= name##_buffer_ptr;\
584
585 # define UPDATE_CACHE(name, gb)\
586 if(name##_bit_count > 0){\
587 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
588 name##_buffer_ptr+=2;\
589 name##_bit_count-= 16;\
590 }\
591
592 # define SKIP_CACHE(name, gb, num)\
593 name##_cache <<= (num);\
594
595 # define SKIP_COUNTER(name, gb, num)\
596 name##_bit_count += (num);\
597
598 # define SKIP_BITS(name, gb, num)\
599 {\
600 SKIP_CACHE(name, gb, num)\
601 SKIP_COUNTER(name, gb, num)\
602 }\
603
604 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
605 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
606
607 # define SHOW_UBITS(name, gb, num)\
608 NEG_USR32(name##_cache, num)
609
610 # define SHOW_SBITS(name, gb, num)\
611 NEG_SSR32(name##_cache, num)
612
613 # define GET_CACHE(name, gb)\
614 ((uint32_t)name##_cache)
615
616 static inline int get_bits_count(GetBitContext *s){
617 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
618 }
619
620 #elif defined A32_BITSTREAM_READER
621
622 # define MIN_CACHE_BITS 32
623
624 # define OPEN_READER(name, gb)\
625 int name##_bit_count=(gb)->bit_count;\
626 uint32_t name##_cache0= (gb)->cache0;\
627 uint32_t name##_cache1= (gb)->cache1;\
628 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
629
630 # define CLOSE_READER(name, gb)\
631 (gb)->bit_count= name##_bit_count;\
632 (gb)->cache0= name##_cache0;\
633 (gb)->cache1= name##_cache1;\
634 (gb)->buffer_ptr= name##_buffer_ptr;\
635
636 # define UPDATE_CACHE(name, gb)\
637 if(name##_bit_count > 0){\
638 const uint32_t next= be2me_32( *name##_buffer_ptr );\
639 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
640 name##_cache1 |= next<<name##_bit_count;\
641 name##_buffer_ptr++;\
642 name##_bit_count-= 32;\
643 }\
644
645 #ifdef ARCH_X86
646 # define SKIP_CACHE(name, gb, num)\
647 asm(\
648 "shldl %2, %1, %0 \n\t"\
649 "shll %2, %1 \n\t"\
650 : "+r" (name##_cache0), "+r" (name##_cache1)\
651 : "Ic" ((uint8_t)num)\
652 );
653 #else
654 # define SKIP_CACHE(name, gb, num)\
655 name##_cache0 <<= (num);\
656 name##_cache0 |= NEG_USR32(name##_cache1,num);\
657 name##_cache1 <<= (num);
658 #endif
659
660 # define SKIP_COUNTER(name, gb, num)\
661 name##_bit_count += (num);\
662
663 # define SKIP_BITS(name, gb, num)\
664 {\
665 SKIP_CACHE(name, gb, num)\
666 SKIP_COUNTER(name, gb, num)\
667 }\
668
669 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
670 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
671
672 # define SHOW_UBITS(name, gb, num)\
673 NEG_USR32(name##_cache0, num)
674
675 # define SHOW_SBITS(name, gb, num)\
676 NEG_SSR32(name##_cache0, num)
677
678 # define GET_CACHE(name, gb)\
679 (name##_cache0)
680
681 static inline int get_bits_count(GetBitContext *s){
682 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
683 }
684
685 #endif
686
687 static inline unsigned int get_bits(GetBitContext *s, int n){
688 register int tmp;
689 OPEN_READER(re, s)
690 UPDATE_CACHE(re, s)
691 tmp= SHOW_UBITS(re, s, n);
692 LAST_SKIP_BITS(re, s, n)
693 CLOSE_READER(re, s)
694 return tmp;
695 }
696
697 static inline unsigned int show_bits(GetBitContext *s, int n){
698 register int tmp;
699 OPEN_READER(re, s)
700 UPDATE_CACHE(re, s)
701 tmp= SHOW_UBITS(re, s, n);
702 // CLOSE_READER(re, s)
703 return tmp;
704 }
705
706 static inline void skip_bits(GetBitContext *s, int n){
707 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
708 OPEN_READER(re, s)
709 UPDATE_CACHE(re, s)
710 LAST_SKIP_BITS(re, s, n)
711 CLOSE_READER(re, s)
712 }
713
714 static inline unsigned int get_bits1(GetBitContext *s){
715 #ifdef ALT_BITSTREAM_READER
716 int index= s->index;
717 uint8_t result= s->buffer[ index>>3 ];
718 result<<= (index&0x07);
719 result>>= 8 - 1;
720 index++;
721 s->index= index;
722
723 return result;
724 #else
725 return get_bits(s, 1);
726 #endif
727 }
728
729 static inline unsigned int show_bits1(GetBitContext *s){
730 return show_bits(s, 1);
731 }
732
733 static inline void skip_bits1(GetBitContext *s){
734 skip_bits(s, 1);
735 }
736
737 void init_get_bits(GetBitContext *s,
738 UINT8 *buffer, int buffer_size);
739
740 int check_marker(GetBitContext *s, char *msg);
741 void align_get_bits(GetBitContext *s);
742 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
743 const void *bits, int bits_wrap, int bits_size,
744 const void *codes, int codes_wrap, int codes_size);
745 void free_vlc(VLC *vlc);
746
747 #define GET_VLC(code, name, gb, table, bits, max_depth)\
748 {\
749 int n, index, nb_bits;\
750 \
751 index= SHOW_UBITS(name, gb, bits);\
752 code = table[index][0];\
753 n = table[index][1];\
754 \
755 if(max_depth > 1 && n < 0){\
756 LAST_SKIP_BITS(name, gb, bits)\
757 UPDATE_CACHE(name, gb)\
758 \
759 nb_bits = -n;\
760 \
761 index= SHOW_UBITS(name, gb, nb_bits) + code;\
762 code = table[index][0];\
763 n = table[index][1];\
764 if(max_depth > 2 && n < 0){\
765 LAST_SKIP_BITS(name, gb, nb_bits)\
766 UPDATE_CACHE(name, gb)\
767 \
768 nb_bits = -n;\
769 \
770 index= SHOW_UBITS(name, gb, nb_bits) + code;\
771 code = table[index][0];\
772 n = table[index][1];\
773 }\
774 }\
775 SKIP_BITS(name, gb, n)\
776 }
777
778 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
779 {\
780 int n, index, nb_bits;\
781 \
782 index= SHOW_UBITS(name, gb, bits);\
783 level = table[index].level;\
784 n = table[index].len;\
785 \
786 if(max_depth > 1 && n < 0){\
787 LAST_SKIP_BITS(name, gb, bits)\
788 UPDATE_CACHE(name, gb)\
789 \
790 nb_bits = -n;\
791 \
792 index= SHOW_UBITS(name, gb, nb_bits) + level;\
793 level = table[index].level;\
794 n = table[index].len;\
795 }\
796 run= table[index].run;\
797 SKIP_BITS(name, gb, n)\
798 }
799
800 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
801 static inline int get_vlc(GetBitContext *s, VLC *vlc)
802 {
803 int code;
804 VLC_TYPE (*table)[2]= vlc->table;
805
806 OPEN_READER(re, s)
807 UPDATE_CACHE(re, s)
808
809 GET_VLC(code, re, s, table, vlc->bits, 3)
810
811 CLOSE_READER(re, s)
812 return code;
813 }
814
815 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
816 int bits, int max_depth)
817 {
818 int code;
819
820 OPEN_READER(re, s)
821 UPDATE_CACHE(re, s)
822
823 GET_VLC(code, re, s, table, bits, max_depth)
824
825 CLOSE_READER(re, s)
826 return code;
827 }
828
829
830 /* define it to include statistics code (useful only for optimizing
831 codec efficiency */
832 //#define STATS
833
834 #ifdef STATS
835
836 enum {
837 ST_UNKNOWN,
838 ST_DC,
839 ST_INTRA_AC,
840 ST_INTER_AC,
841 ST_INTRA_MB,
842 ST_INTER_MB,
843 ST_MV,
844 ST_NB,
845 };
846
847 extern int st_current_index;
848 extern unsigned int st_bit_counts[ST_NB];
849 extern unsigned int st_out_bit_counts[ST_NB];
850
851 void print_stats(void);
852 #endif
853
854 /* misc math functions */
855
856 static inline int av_log2(unsigned int v)
857 {
858 int n;
859
860 n = 0;
861 if (v & 0xffff0000) {
862 v >>= 16;
863 n += 16;
864 }
865 if (v & 0xff00) {
866 v >>= 8;
867 n += 8;
868 }
869 if (v & 0xf0) {
870 v >>= 4;
871 n += 4;
872 }
873 if (v & 0xc) {
874 v >>= 2;
875 n += 2;
876 }
877 if (v & 0x2) {
878 n++;
879 }
880 return n;
881 }
882
883 /* median of 3 */
884 static inline int mid_pred(int a, int b, int c)
885 {
886 int vmin, vmax;
887 vmax = vmin = a;
888 if (b < vmin)
889 vmin = b;
890 else
891 vmax = b;
892
893 if (c < vmin)
894 vmin = c;
895 else if (c > vmax)
896 vmax = c;
897
898 return a + b + c - vmin - vmax;
899 }
900
901 static inline int clip(int a, int amin, int amax)
902 {
903 if (a < amin)
904 return amin;
905 else if (a > amax)
906 return amax;
907 else
908 return a;
909 }
910
911 /* math */
912 extern const UINT8 ff_sqrt_tab[128];
913
914 int ff_gcd(int a, int b);
915
916 static inline int ff_sqrt(int a)
917 {
918 int ret=0;
919 int s;
920 int ret_sq=0;
921
922 if(a<128) return ff_sqrt_tab[a];
923
924 for(s=15; s>=0; s--){
925 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
926 if(b<=a){
927 ret_sq=b;
928 ret+= 1<<s;
929 }
930 }
931 return ret;
932 }
933
934 /**
935 * converts fourcc string to int
936 */
937 static inline int ff_get_fourcc(char *s){
938 assert( strlen(s)==4 );
939
940 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
941 }
942
943
944 #ifdef ARCH_X86
945 #define MASK_ABS(mask, level)\
946 asm volatile(\
947 "cdq \n\t"\
948 "xorl %1, %0 \n\t"\
949 "subl %1, %0 \n\t"\
950 : "+a" (level), "=&d" (mask)\
951 );
952 #else
953 #define MASK_ABS(mask, level)\
954 mask= level>>31;\
955 level= (level^mask)-mask;
956 #endif
957
958
959 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
960 #define COPY3_IF_LT(x,y,a,b,c,d)\
961 asm volatile (\
962 "cmpl %0, %3 \n\t"\
963 "cmovl %3, %0 \n\t"\
964 "cmovl %4, %1 \n\t"\
965 "cmovl %5, %2 \n\t"\
966 : "+r" (x), "+r" (a), "+r" (c)\
967 : "r" (y), "r" (b), "r" (d)\
968 );
969 #else
970 #define COPY3_IF_LT(x,y,a,b,c,d)\
971 if((y)<(x)){\
972 (x)=(y);\
973 (a)=(b);\
974 (c)=(d);\
975 }
976 #endif
977
978 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
979
980 #endif /* HAVE_AV_CONFIG_H */
981
982 #endif /* COMMON_H */