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