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