* fixes for broken builds on Solaris, OS2 and all bingendian
[libav.git] / libavcodec / common.h
1 /**
2 * @file common.h
3 * common internal api header.
4 */
5
6 #ifndef COMMON_H
7 #define COMMON_H
8
9 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10 # define CONFIG_WIN32
11 #endif
12
13 //#define ALT_BITSTREAM_WRITER
14 //#define ALIGNED_BITSTREAM_WRITER
15
16 #define ALT_BITSTREAM_READER
17 //#define LIBMPEG2_BITSTREAM_READER
18 //#define A32_BITSTREAM_READER
19 #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
20
21 #ifdef HAVE_AV_CONFIG_H
22 /* only include the following when compiling package */
23 # include "config.h"
24
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <ctype.h>
29 # ifndef __BEOS__
30 # include <errno.h>
31 # else
32 # include "berrno.h"
33 # endif
34 # include <math.h>
35
36 # ifndef ENODATA
37 # define ENODATA 61
38 # endif
39
40 #ifndef M_PI
41 #define M_PI 3.14159265358979323846
42 #endif
43
44 #include <stddef.h>
45 #ifndef offsetof
46 # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
47 #endif
48
49 #define AVOPTION_CODEC_BOOL(name, help, field) \
50 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
51 #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
53 #define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
54 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
55 #define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
56 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
57 #define AVOPTION_CODEC_STRING(name, help, field, str, val) \
58 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
59 #define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
60 { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
61 #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
62 #define AVOPTION_END() AVOPTION_SUB(NULL)
63
64 struct AVOption;
65 #ifdef HAVE_MMX
66 extern const struct AVOption avoptions_common[3 + 5];
67 #else
68 extern const struct AVOption avoptions_common[3];
69 #endif
70 extern const struct AVOption avoptions_workaround_bug[11];
71
72 #endif /* HAVE_AV_CONFIG_H */
73
74 /* Suppress restrict if it was not defined in config.h. */
75 #ifndef restrict
76 # define restrict
77 #endif
78
79 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80 # define always_inline __attribute__((always_inline)) inline
81 #else
82 # define always_inline inline
83 #endif
84
85 #ifdef EMULATE_FAST_INT
86 /* note that we don't emulate 64bit ints */
87 typedef signed char int_fast8_t;
88 typedef signed int int_fast16_t;
89 typedef signed int int_fast32_t;
90 typedef unsigned char uint_fast8_t;
91 typedef unsigned int uint_fast16_t;
92 typedef unsigned int uint_fast32_t;
93 #endif
94
95 #if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
96 static inline float floorf(float f) {
97 return floor(f);
98 }
99 #endif
100
101 #ifdef CONFIG_WIN32
102
103 /* windows */
104
105 typedef unsigned short uint16_t;
106 typedef signed short int16_t;
107 typedef unsigned char uint8_t;
108 typedef unsigned int uint32_t;
109 typedef unsigned __int64 uint64_t;
110 typedef signed char int8_t;
111 typedef signed int int32_t;
112 typedef signed __int64 int64_t;
113
114 # ifndef __MINGW32__
115 # define int64_t_C(c) (c ## i64)
116 # define uint64_t_C(c) (c ## i64)
117
118 # ifdef HAVE_AV_CONFIG_H
119 # define inline __inline
120 # endif
121
122 # else
123 # define int64_t_C(c) (c ## LL)
124 # define uint64_t_C(c) (c ## ULL)
125 # endif /* __MINGW32__ */
126
127 # ifdef HAVE_AV_CONFIG_H
128 # ifdef _DEBUG
129 # define DEBUG
130 # endif
131
132 # define snprintf _snprintf
133 # define vsnprintf _vsnprintf
134 # endif
135
136 /* CONFIG_WIN32 end */
137 #elif defined (CONFIG_OS2)
138 /* OS/2 EMX */
139
140 #include <inttypes.h>
141
142 #ifndef int64_t_C
143 #define int64_t_C(c) (c ## LL)
144 #define uint64_t_C(c) (c ## ULL)
145 #endif
146
147 #ifdef HAVE_AV_CONFIG_H
148
149 #ifdef USE_FASTMEMCPY
150 #include "fastmemcpy.h"
151 #endif
152
153 #include <float.h>
154
155 #endif /* HAVE_AV_CONFIG_H */
156
157 /* CONFIG_OS2 end */
158 #else
159
160 /* unix */
161
162 #include <inttypes.h>
163
164 #ifndef int64_t_C
165 #define int64_t_C(c) (c ## LL)
166 #define uint64_t_C(c) (c ## ULL)
167 #endif
168
169 #ifdef HAVE_AV_CONFIG_H
170
171 # ifdef USE_FASTMEMCPY
172 # include "fastmemcpy.h"
173 # endif
174 # endif /* HAVE_AV_CONFIG_H */
175
176 #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
177
178 #ifdef HAVE_AV_CONFIG_H
179
180 # include "bswap.h"
181
182 # if defined(__MINGW32__) || defined(__CYGWIN__) || \
183 defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
184 # define MANGLE(a) "_" #a
185 # else
186 # define MANGLE(a) #a
187 # endif
188
189 /* debug stuff */
190
191 # ifndef DEBUG
192 # define NDEBUG
193 # endif
194 # include <assert.h>
195
196 /* dprintf macros */
197 # if defined(CONFIG_WIN32) && !defined(__MINGW32__)
198
199 inline void dprintf(const char* fmt,...) {}
200
201 # else
202
203 # ifdef DEBUG
204 # define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
205 # else
206 # define dprintf(fmt,...)
207 # endif
208
209 # endif /* !CONFIG_WIN32 */
210
211 # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
212
213 //rounded divison & shift
214 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
215 /* assume b>0 */
216 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
217 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
218
219 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
220 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
221
222 extern const uint32_t inverse[256];
223
224 #ifdef ARCH_X86
225 # define FASTDIV(a,b) \
226 ({\
227 int ret,dmy;\
228 asm volatile(\
229 "mull %3"\
230 :"=d"(ret),"=a"(dmy)\
231 :"1"(a),"g"(inverse[b])\
232 );\
233 ret;\
234 })
235 #elif defined(CONFIG_FASTDIV)
236 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
237 #else
238 # define FASTDIV(a,b) ((a)/(b))
239 #endif
240
241 #ifdef ARCH_X86
242 // avoid +32 for shift optimization (gcc should do that ...)
243 static inline int32_t NEG_SSR32( int32_t a, int8_t s){
244 asm ("sarl %1, %0\n\t"
245 : "+r" (a)
246 : "ic" ((uint8_t)(-s))
247 );
248 return a;
249 }
250 static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
251 asm ("shrl %1, %0\n\t"
252 : "+r" (a)
253 : "ic" ((uint8_t)(-s))
254 );
255 return a;
256 }
257 #else
258 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
259 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
260 #endif
261
262 /* bit output */
263
264 struct PutBitContext;
265
266 typedef void (*WriteDataFunc)(void *, uint8_t *, int);
267
268 typedef struct PutBitContext {
269 #ifdef ALT_BITSTREAM_WRITER
270 uint8_t *buf, *buf_end;
271 int index;
272 #else
273 uint32_t bit_buf;
274 int bit_left;
275 uint8_t *buf, *buf_ptr, *buf_end;
276 #endif
277 int64_t data_out_size; /* in bytes */
278 } PutBitContext;
279
280 void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
281
282 int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */
283 void align_put_bits(PutBitContext *s);
284 void flush_put_bits(PutBitContext *s);
285 void put_string(PutBitContext * pbc, char *s);
286
287 /* bit input */
288
289 typedef struct GetBitContext {
290 const uint8_t *buffer, *buffer_end;
291 #ifdef ALT_BITSTREAM_READER
292 int index;
293 #elif defined LIBMPEG2_BITSTREAM_READER
294 uint8_t *buffer_ptr;
295 uint32_t cache;
296 int bit_count;
297 #elif defined A32_BITSTREAM_READER
298 uint32_t *buffer_ptr;
299 uint32_t cache0;
300 uint32_t cache1;
301 int bit_count;
302 #endif
303 int size_in_bits;
304 } GetBitContext;
305
306 static inline int get_bits_count(GetBitContext *s);
307
308 #define VLC_TYPE int16_t
309
310 typedef struct VLC {
311 int bits;
312 VLC_TYPE (*table)[2]; ///< code, bits
313 int table_size, table_allocated;
314 } VLC;
315
316 typedef struct RL_VLC_ELEM {
317 int16_t level;
318 int8_t len;
319 uint8_t run;
320 } RL_VLC_ELEM;
321
322 #ifdef ARCH_SPARC64
323 #define UNALIGNED_STORES_ARE_BAD
324 #endif
325
326 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
327 #ifdef ARCH_X86
328 # define unaligned32(a) (*(uint32_t*)(a))
329 #else
330 # ifdef __GNUC__
331 static inline uint32_t unaligned32(const void *v) {
332 struct Unaligned {
333 uint32_t i;
334 } __attribute__((packed));
335
336 return ((const struct Unaligned *) v)->i;
337 }
338 # elif defined(__DECC)
339 static inline uint32_t unaligned32(const void *v) {
340 return *(const __unaligned uint32_t *) v;
341 }
342 # else
343 static inline uint32_t unaligned32(const void *v) {
344 return *(const uint32_t *) v;
345 }
346 # endif
347 #endif //!ARCH_X86
348
349 #ifndef ALT_BITSTREAM_WRITER
350 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
351 {
352 unsigned int bit_buf;
353 int bit_left;
354
355 #ifdef STATS
356 st_out_bit_counts[st_current_index] += n;
357 #endif
358 // printf("put_bits=%d %x\n", n, value);
359 assert(n == 32 || value < (1U << n));
360
361 bit_buf = s->bit_buf;
362 bit_left = s->bit_left;
363
364 // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
365 /* XXX: optimize */
366 if (n < bit_left) {
367 bit_buf = (bit_buf<<n) | value;
368 bit_left-=n;
369 } else {
370 bit_buf<<=bit_left;
371 bit_buf |= value >> (n - bit_left);
372 #ifdef UNALIGNED_STORES_ARE_BAD
373 if (3 & (int) s->buf_ptr) {
374 s->buf_ptr[0] = bit_buf >> 24;
375 s->buf_ptr[1] = bit_buf >> 16;
376 s->buf_ptr[2] = bit_buf >> 8;
377 s->buf_ptr[3] = bit_buf ;
378 } else
379 #endif
380 *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
381 //printf("bitbuf = %08x\n", bit_buf);
382 s->buf_ptr+=4;
383 bit_left+=32 - n;
384 bit_buf = value;
385 }
386
387 s->bit_buf = bit_buf;
388 s->bit_left = bit_left;
389 }
390 #endif
391
392
393 #ifdef ALT_BITSTREAM_WRITER
394 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
395 {
396 # ifdef ALIGNED_BITSTREAM_WRITER
397 # ifdef ARCH_X86
398 asm volatile(
399 "movl %0, %%ecx \n\t"
400 "xorl %%eax, %%eax \n\t"
401 "shrdl %%cl, %1, %%eax \n\t"
402 "shrl %%cl, %1 \n\t"
403 "movl %0, %%ecx \n\t"
404 "shrl $3, %%ecx \n\t"
405 "andl $0xFFFFFFFC, %%ecx \n\t"
406 "bswapl %1 \n\t"
407 "orl %1, (%2, %%ecx) \n\t"
408 "bswapl %%eax \n\t"
409 "addl %3, %0 \n\t"
410 "movl %%eax, 4(%2, %%ecx) \n\t"
411 : "=&r" (s->index), "=&r" (value)
412 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
413 : "%eax", "%ecx"
414 );
415 # else
416 int index= s->index;
417 uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
418
419 value<<= 32-n;
420
421 ptr[0] |= be2me_32(value>>(index&31));
422 ptr[1] = be2me_32(value<<(32-(index&31)));
423 //if(n>24) printf("%d %d\n", n, value);
424 index+= n;
425 s->index= index;
426 # endif
427 # else //ALIGNED_BITSTREAM_WRITER
428 # ifdef ARCH_X86
429 asm volatile(
430 "movl $7, %%ecx \n\t"
431 "andl %0, %%ecx \n\t"
432 "addl %3, %%ecx \n\t"
433 "negl %%ecx \n\t"
434 "shll %%cl, %1 \n\t"
435 "bswapl %1 \n\t"
436 "movl %0, %%ecx \n\t"
437 "shrl $3, %%ecx \n\t"
438 "orl %1, (%%ecx, %2) \n\t"
439 "addl %3, %0 \n\t"
440 "movl $0, 4(%%ecx, %2) \n\t"
441 : "=&r" (s->index), "=&r" (value)
442 : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
443 : "%ecx"
444 );
445 # else
446 int index= s->index;
447 uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
448
449 ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
450 ptr[1] = 0;
451 //if(n>24) printf("%d %d\n", n, value);
452 index+= n;
453 s->index= index;
454 # endif
455 # endif //!ALIGNED_BITSTREAM_WRITER
456 }
457 #endif
458
459
460 static inline uint8_t* pbBufPtr(PutBitContext *s)
461 {
462 #ifdef ALT_BITSTREAM_WRITER
463 return s->buf + (s->index>>3);
464 #else
465 return s->buf_ptr;
466 #endif
467 }
468
469 /* Bitstream reader API docs:
470 name
471 abritary name which is used as prefix for the internal variables
472
473 gb
474 getbitcontext
475
476 OPEN_READER(name, gb)
477 loads gb into local variables
478
479 CLOSE_READER(name, gb)
480 stores local vars in gb
481
482 UPDATE_CACHE(name, gb)
483 refills the internal cache from the bitstream
484 after this call at least MIN_CACHE_BITS will be available,
485
486 GET_CACHE(name, gb)
487 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
488
489 SHOW_UBITS(name, gb, num)
490 will return the nest num bits
491
492 SHOW_SBITS(name, gb, num)
493 will return the nest num bits and do sign extension
494
495 SKIP_BITS(name, gb, num)
496 will skip over the next num bits
497 note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
498
499 SKIP_CACHE(name, gb, num)
500 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
501
502 SKIP_COUNTER(name, gb, num)
503 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
504
505 LAST_SKIP_CACHE(name, gb, num)
506 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
507
508 LAST_SKIP_BITS(name, gb, num)
509 is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
510
511 for examples see get_bits, show_bits, skip_bits, get_vlc
512 */
513
514 static inline int unaligned32_be(const void *v)
515 {
516 #ifdef CONFIG_ALIGN
517 const uint8_t *p=v;
518 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
519 #else
520 return be2me_32( unaligned32(v)); //original
521 #endif
522 }
523
524 #ifdef ALT_BITSTREAM_READER
525 # define MIN_CACHE_BITS 25
526
527 # define OPEN_READER(name, gb)\
528 int name##_index= (gb)->index;\
529 int name##_cache= 0;\
530
531 # define CLOSE_READER(name, gb)\
532 (gb)->index= name##_index;\
533
534 # define UPDATE_CACHE(name, gb)\
535 name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
536
537 # define SKIP_CACHE(name, gb, num)\
538 name##_cache <<= (num);\
539
540 // FIXME name?
541 # define SKIP_COUNTER(name, gb, num)\
542 name##_index += (num);\
543
544 # define SKIP_BITS(name, gb, num)\
545 {\
546 SKIP_CACHE(name, gb, num)\
547 SKIP_COUNTER(name, gb, num)\
548 }\
549
550 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
551 # define LAST_SKIP_CACHE(name, gb, num) ;
552
553 # define SHOW_UBITS(name, gb, num)\
554 NEG_USR32(name##_cache, num)
555
556 # define SHOW_SBITS(name, gb, num)\
557 NEG_SSR32(name##_cache, num)
558
559 # define GET_CACHE(name, gb)\
560 ((uint32_t)name##_cache)
561
562 static inline int get_bits_count(GetBitContext *s){
563 return s->index;
564 }
565 #elif defined LIBMPEG2_BITSTREAM_READER
566 //libmpeg2 like reader
567
568 # define MIN_CACHE_BITS 17
569
570 # define OPEN_READER(name, gb)\
571 int name##_bit_count=(gb)->bit_count;\
572 int name##_cache= (gb)->cache;\
573 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
574
575 # define CLOSE_READER(name, gb)\
576 (gb)->bit_count= name##_bit_count;\
577 (gb)->cache= name##_cache;\
578 (gb)->buffer_ptr= name##_buffer_ptr;\
579
580 #ifdef LIBMPEG2_BITSTREAM_READER_HACK
581
582 # define UPDATE_CACHE(name, gb)\
583 if(name##_bit_count >= 0){\
584 name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
585 ((uint16_t*)name##_buffer_ptr)++;\
586 name##_bit_count-= 16;\
587 }\
588
589 #else
590
591 # define UPDATE_CACHE(name, gb)\
592 if(name##_bit_count >= 0){\
593 name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
594 name##_buffer_ptr+=2;\
595 name##_bit_count-= 16;\
596 }\
597
598 #endif
599
600 # define SKIP_CACHE(name, gb, num)\
601 name##_cache <<= (num);\
602
603 # define SKIP_COUNTER(name, gb, num)\
604 name##_bit_count += (num);\
605
606 # define SKIP_BITS(name, gb, num)\
607 {\
608 SKIP_CACHE(name, gb, num)\
609 SKIP_COUNTER(name, gb, num)\
610 }\
611
612 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
613 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
614
615 # define SHOW_UBITS(name, gb, num)\
616 NEG_USR32(name##_cache, num)
617
618 # define SHOW_SBITS(name, gb, num)\
619 NEG_SSR32(name##_cache, num)
620
621 # define GET_CACHE(name, gb)\
622 ((uint32_t)name##_cache)
623
624 static inline int get_bits_count(GetBitContext *s){
625 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
626 }
627
628 #elif defined A32_BITSTREAM_READER
629
630 # define MIN_CACHE_BITS 32
631
632 # define OPEN_READER(name, gb)\
633 int name##_bit_count=(gb)->bit_count;\
634 uint32_t name##_cache0= (gb)->cache0;\
635 uint32_t name##_cache1= (gb)->cache1;\
636 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
637
638 # define CLOSE_READER(name, gb)\
639 (gb)->bit_count= name##_bit_count;\
640 (gb)->cache0= name##_cache0;\
641 (gb)->cache1= name##_cache1;\
642 (gb)->buffer_ptr= name##_buffer_ptr;\
643
644 # define UPDATE_CACHE(name, gb)\
645 if(name##_bit_count > 0){\
646 const uint32_t next= be2me_32( *name##_buffer_ptr );\
647 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
648 name##_cache1 |= next<<name##_bit_count;\
649 name##_buffer_ptr++;\
650 name##_bit_count-= 32;\
651 }\
652
653 #ifdef ARCH_X86
654 # define SKIP_CACHE(name, gb, num)\
655 asm(\
656 "shldl %2, %1, %0 \n\t"\
657 "shll %2, %1 \n\t"\
658 : "+r" (name##_cache0), "+r" (name##_cache1)\
659 : "Ic" ((uint8_t)num)\
660 );
661 #else
662 # define SKIP_CACHE(name, gb, num)\
663 name##_cache0 <<= (num);\
664 name##_cache0 |= NEG_USR32(name##_cache1,num);\
665 name##_cache1 <<= (num);
666 #endif
667
668 # define SKIP_COUNTER(name, gb, num)\
669 name##_bit_count += (num);\
670
671 # define SKIP_BITS(name, gb, num)\
672 {\
673 SKIP_CACHE(name, gb, num)\
674 SKIP_COUNTER(name, gb, num)\
675 }\
676
677 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
678 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
679
680 # define SHOW_UBITS(name, gb, num)\
681 NEG_USR32(name##_cache0, num)
682
683 # define SHOW_SBITS(name, gb, num)\
684 NEG_SSR32(name##_cache0, num)
685
686 # define GET_CACHE(name, gb)\
687 (name##_cache0)
688
689 static inline int get_bits_count(GetBitContext *s){
690 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
691 }
692
693 #endif
694
695 /**
696 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
697 * if MSB not set it is negative
698 * @param n length in bits
699 * @author BERO
700 */
701 static inline int get_xbits(GetBitContext *s, int n){
702 register int tmp;
703 register int32_t cache;
704 OPEN_READER(re, s)
705 UPDATE_CACHE(re, s)
706 cache = GET_CACHE(re,s);
707 if ((int32_t)cache<0) { //MSB=1
708 tmp = NEG_USR32(cache,n);
709 } else {
710 // tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
711 // tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
712 tmp = - NEG_USR32(~cache,n);
713 }
714 LAST_SKIP_BITS(re, s, n)
715 CLOSE_READER(re, s)
716 return tmp;
717 }
718
719 static inline int get_sbits(GetBitContext *s, int n){
720 register int tmp;
721 OPEN_READER(re, s)
722 UPDATE_CACHE(re, s)
723 tmp= SHOW_SBITS(re, s, n);
724 LAST_SKIP_BITS(re, s, n)
725 CLOSE_READER(re, s)
726 return tmp;
727 }
728
729 /**
730 * reads 0-17 bits.
731 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
732 */
733 static inline unsigned int get_bits(GetBitContext *s, int n){
734 register int tmp;
735 OPEN_READER(re, s)
736 UPDATE_CACHE(re, s)
737 tmp= SHOW_UBITS(re, s, n);
738 LAST_SKIP_BITS(re, s, n)
739 CLOSE_READER(re, s)
740 return tmp;
741 }
742
743 unsigned int get_bits_long(GetBitContext *s, int n);
744
745 /**
746 * shows 0-17 bits.
747 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
748 */
749 static inline unsigned int show_bits(GetBitContext *s, int n){
750 register int tmp;
751 OPEN_READER(re, s)
752 UPDATE_CACHE(re, s)
753 tmp= SHOW_UBITS(re, s, n);
754 // CLOSE_READER(re, s)
755 return tmp;
756 }
757
758 unsigned int show_bits_long(GetBitContext *s, int n);
759
760 static inline void skip_bits(GetBitContext *s, int n){
761 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
762 OPEN_READER(re, s)
763 UPDATE_CACHE(re, s)
764 LAST_SKIP_BITS(re, s, n)
765 CLOSE_READER(re, s)
766 }
767
768 static inline unsigned int get_bits1(GetBitContext *s){
769 #ifdef ALT_BITSTREAM_READER
770 int index= s->index;
771 uint8_t result= s->buffer[ index>>3 ];
772 result<<= (index&0x07);
773 result>>= 8 - 1;
774 index++;
775 s->index= index;
776
777 return result;
778 #else
779 return get_bits(s, 1);
780 #endif
781 }
782
783 static inline unsigned int show_bits1(GetBitContext *s){
784 return show_bits(s, 1);
785 }
786
787 static inline void skip_bits1(GetBitContext *s){
788 skip_bits(s, 1);
789 }
790
791 void init_get_bits(GetBitContext *s,
792 const uint8_t *buffer, int buffer_size);
793
794 int check_marker(GetBitContext *s, const char *msg);
795 void align_get_bits(GetBitContext *s);
796 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
797 const void *bits, int bits_wrap, int bits_size,
798 const void *codes, int codes_wrap, int codes_size);
799 void free_vlc(VLC *vlc);
800
801 /**
802 *
803 * if the vlc code is invalid and max_depth=1 than no bits will be removed
804 * if the vlc code is invalid and max_depth>1 than the number of bits removed
805 * is undefined
806 */
807 #define GET_VLC(code, name, gb, table, bits, max_depth)\
808 {\
809 int n, index, nb_bits;\
810 \
811 index= SHOW_UBITS(name, gb, bits);\
812 code = table[index][0];\
813 n = table[index][1];\
814 \
815 if(max_depth > 1 && n < 0){\
816 LAST_SKIP_BITS(name, gb, bits)\
817 UPDATE_CACHE(name, gb)\
818 \
819 nb_bits = -n;\
820 \
821 index= SHOW_UBITS(name, gb, nb_bits) + code;\
822 code = table[index][0];\
823 n = table[index][1];\
824 if(max_depth > 2 && n < 0){\
825 LAST_SKIP_BITS(name, gb, nb_bits)\
826 UPDATE_CACHE(name, gb)\
827 \
828 nb_bits = -n;\
829 \
830 index= SHOW_UBITS(name, gb, nb_bits) + code;\
831 code = table[index][0];\
832 n = table[index][1];\
833 }\
834 }\
835 SKIP_BITS(name, gb, n)\
836 }
837
838 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
839 {\
840 int n, index, nb_bits;\
841 \
842 index= SHOW_UBITS(name, gb, bits);\
843 level = table[index].level;\
844 n = table[index].len;\
845 \
846 if(max_depth > 1 && n < 0){\
847 LAST_SKIP_BITS(name, gb, bits)\
848 UPDATE_CACHE(name, gb)\
849 \
850 nb_bits = -n;\
851 \
852 index= SHOW_UBITS(name, gb, nb_bits) + level;\
853 level = table[index].level;\
854 n = table[index].len;\
855 }\
856 run= table[index].run;\
857 SKIP_BITS(name, gb, n)\
858 }
859
860 // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
861 static inline int get_vlc(GetBitContext *s, VLC *vlc)
862 {
863 int code;
864 VLC_TYPE (*table)[2]= vlc->table;
865
866 OPEN_READER(re, s)
867 UPDATE_CACHE(re, s)
868
869 GET_VLC(code, re, s, table, vlc->bits, 3)
870
871 CLOSE_READER(re, s)
872 return code;
873 }
874
875 /**
876 * parses a vlc code, faster then get_vlc()
877 * @param bits is the number of bits which will be read at once, must be
878 * identical to nb_bits in init_vlc()
879 * @param max_depth is the number of times bits bits must be readed to completly
880 * read the longest vlc code
881 * = (max_vlc_length + bits - 1) / bits
882 */
883 static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
884 int bits, int max_depth)
885 {
886 int code;
887
888 OPEN_READER(re, s)
889 UPDATE_CACHE(re, s)
890
891 GET_VLC(code, re, s, table, bits, max_depth)
892
893 CLOSE_READER(re, s)
894 return code;
895 }
896
897 //#define TRACE
898
899 #ifdef TRACE
900
901 static inline void print_bin(int bits, int n){
902 int i;
903
904 for(i=n-1; i>=0; i--){
905 printf("%d", (bits>>i)&1);
906 }
907 for(i=n; i<24; i++)
908 printf(" ");
909 }
910
911 static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
912 int r= get_bits(s, n);
913
914 print_bin(r, n);
915 printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
916 return r;
917 }
918 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
919 int show= show_bits(s, 24);
920 int pos= get_bits_count(s);
921 int r= get_vlc2(s, table, bits, max_depth);
922 int len= get_bits_count(s) - pos;
923 int bits2= show>>(24-len);
924
925 print_bin(bits2, len);
926
927 printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
928 return r;
929 }
930 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
931 int show= show_bits(s, n);
932 int r= get_xbits(s, n);
933
934 print_bin(show, n);
935 printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
936 return r;
937 }
938
939 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
940 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
941 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
942 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
943 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
944
945 #define tprintf printf
946
947 #else //TRACE
948 #define tprintf(_arg...) {}
949 #endif
950
951 /* define it to include statistics code (useful only for optimizing
952 codec efficiency */
953 //#define STATS
954
955 #ifdef STATS
956
957 enum {
958 ST_UNKNOWN,
959 ST_DC,
960 ST_INTRA_AC,
961 ST_INTER_AC,
962 ST_INTRA_MB,
963 ST_INTER_MB,
964 ST_MV,
965 ST_NB,
966 };
967
968 extern int st_current_index;
969 extern unsigned int st_bit_counts[ST_NB];
970 extern unsigned int st_out_bit_counts[ST_NB];
971
972 void print_stats(void);
973 #endif
974
975 /* misc math functions */
976 extern const uint8_t ff_log2_tab[256];
977
978 static inline int av_log2(unsigned int v)
979 {
980 int n;
981
982 n = 0;
983 if (v & 0xffff0000) {
984 v >>= 16;
985 n += 16;
986 }
987 if (v & 0xff00) {
988 v >>= 8;
989 n += 8;
990 }
991 n += ff_log2_tab[v];
992
993 return n;
994 }
995
996 static inline int av_log2_16bit(unsigned int v)
997 {
998 int n;
999
1000 n = 0;
1001 if (v & 0xff00) {
1002 v >>= 8;
1003 n += 8;
1004 }
1005 n += ff_log2_tab[v];
1006
1007 return n;
1008 }
1009
1010
1011 /* median of 3 */
1012 static inline int mid_pred(int a, int b, int c)
1013 {
1014 int vmin, vmax;
1015 vmax = vmin = a;
1016 if (b < vmin)
1017 vmin = b;
1018 else
1019 vmax = b;
1020
1021 if (c < vmin)
1022 vmin = c;
1023 else if (c > vmax)
1024 vmax = c;
1025
1026 return a + b + c - vmin - vmax;
1027 }
1028
1029 static inline int clip(int a, int amin, int amax)
1030 {
1031 if (a < amin)
1032 return amin;
1033 else if (a > amax)
1034 return amax;
1035 else
1036 return a;
1037 }
1038
1039 /* math */
1040 extern const uint8_t ff_sqrt_tab[128];
1041
1042 int64_t ff_gcd(int64_t a, int64_t b);
1043
1044 static inline int ff_sqrt(int a)
1045 {
1046 int ret=0;
1047 int s;
1048 int ret_sq=0;
1049
1050 if(a<128) return ff_sqrt_tab[a];
1051
1052 for(s=15; s>=0; s--){
1053 int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1054 if(b<=a){
1055 ret_sq=b;
1056 ret+= 1<<s;
1057 }
1058 }
1059 return ret;
1060 }
1061
1062 /**
1063 * converts fourcc string to int
1064 */
1065 static inline int ff_get_fourcc(const char *s){
1066 assert( strlen(s)==4 );
1067
1068 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1069 }
1070
1071 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1072 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1073
1074
1075 #ifdef ARCH_X86
1076 #define MASK_ABS(mask, level)\
1077 asm volatile(\
1078 "cdq \n\t"\
1079 "xorl %1, %0 \n\t"\
1080 "subl %1, %0 \n\t"\
1081 : "+a" (level), "=&d" (mask)\
1082 );
1083 #else
1084 #define MASK_ABS(mask, level)\
1085 mask= level>>31;\
1086 level= (level^mask)-mask;
1087 #endif
1088
1089
1090 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1091 #define COPY3_IF_LT(x,y,a,b,c,d)\
1092 asm volatile (\
1093 "cmpl %0, %3 \n\t"\
1094 "cmovl %3, %0 \n\t"\
1095 "cmovl %4, %1 \n\t"\
1096 "cmovl %5, %2 \n\t"\
1097 : "+r" (x), "+r" (a), "+r" (c)\
1098 : "r" (y), "r" (b), "r" (d)\
1099 );
1100 #else
1101 #define COPY3_IF_LT(x,y,a,b,c,d)\
1102 if((y)<(x)){\
1103 (x)=(y);\
1104 (a)=(b);\
1105 (c)=(d);\
1106 }
1107 #endif
1108
1109 #ifdef ARCH_X86
1110 static inline long long rdtsc()
1111 {
1112 long long l;
1113 asm volatile( "rdtsc\n\t"
1114 : "=A" (l)
1115 );
1116 return l;
1117 }
1118
1119 #define START_TIMER \
1120 static uint64_t tsum=0;\
1121 static int tcount=0;\
1122 static int tskip_count=0;\
1123 uint64_t tend;\
1124 uint64_t tstart= rdtsc();\
1125
1126 #define STOP_TIMER(id) \
1127 tend= rdtsc();\
1128 if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1129 tsum+= tend - tstart;\
1130 tcount++;\
1131 }else\
1132 tskip_count++;\
1133 if(256*256*256*64%(tcount+tskip_count)==0){\
1134 fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1135 }
1136 #endif
1137
1138 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1139
1140 /* avoid usage of various functions */
1141 #define malloc please_use_av_malloc
1142 #define free please_use_av_free
1143 #define realloc please_use_av_realloc
1144
1145 #define CHECKED_ALLOCZ(p, size)\
1146 {\
1147 p= av_mallocz(size);\
1148 if(p==NULL && (size)!=0){\
1149 perror("malloc");\
1150 goto fail;\
1151 }\
1152 }
1153
1154 #endif /* HAVE_AV_CONFIG_H */
1155
1156 #endif /* COMMON_H */