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