clib/mpeg2audio/common.h

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