Commit | Line | Data |
---|---|---|
983e3246 MN |
1 | /** |
2 | * @file common.h | |
3 | * common internal api header. | |
4 | */ | |
5 | ||
de6d9b64 FB |
6 | #ifndef COMMON_H |
7 | #define COMMON_H | |
8 | ||
4bdd9157 | 9 | #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) |
9b59c92f | 10 | # define CONFIG_WIN32 |
1a565432 FB |
11 | #endif |
12 | ||
17592475 | 13 | //#define ALT_BITSTREAM_WRITER |
4f0f8bfc | 14 | //#define ALIGNED_BITSTREAM_WRITER |
3530320d | 15 | |
8db1a1dd MN |
16 | #define ALT_BITSTREAM_READER |
17 | //#define LIBMPEG2_BITSTREAM_READER | |
18 | //#define A32_BITSTREAM_READER | |
d8e00c09 | 19 | #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO |
8db1a1dd | 20 | |
420b073b | 21 | #ifdef HAVE_AV_CONFIG_H |
1a565432 | 22 | /* only include the following when compiling package */ |
9b59c92f MN |
23 | # include "config.h" |
24 | ||
25 | # include <stdlib.h> | |
26 | # include <stdio.h> | |
27 | # include <string.h> | |
56c4a184 | 28 | # include <ctype.h> |
9b59c92f MN |
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 | |
1a565432 | 39 | |
c4d82fea FB |
40 | #ifndef M_PI |
41 | #define M_PI 3.14159265358979323846 | |
42 | #endif | |
43 | ||
fe1b62fb ZK |
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 } | |
5d4ce457 ZK |
51 | #define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \ |
52 | { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval } | |
fe1b62fb ZK |
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 } | |
bec89a84 | 61 | #define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr } |
fe1b62fb ZK |
62 | #define AVOPTION_END() AVOPTION_SUB(NULL) |
63 | ||
a77146ab ZK |
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 | ||
44f27b3a | 72 | #endif /* HAVE_AV_CONFIG_H */ |
1a565432 | 73 | |
02da51ec FH |
74 | /* Suppress restrict if it was not defined in config.h. */ |
75 | #ifndef restrict | |
9b59c92f | 76 | # define restrict |
02da51ec FH |
77 | #endif |
78 | ||
d200cab6 | 79 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) |
9b59c92f | 80 | # define always_inline __attribute__((always_inline)) inline |
d200cab6 | 81 | #else |
9b59c92f | 82 | # define always_inline inline |
d200cab6 FH |
83 | #endif |
84 | ||
1a565432 FB |
85 | #ifdef CONFIG_WIN32 |
86 | ||
87 | /* windows */ | |
88 | ||
0c1a9eda ZK |
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; | |
1a565432 | 97 | |
9b59c92f | 98 | # ifndef __MINGW32__ |
0c1a9eda ZK |
99 | # define int64_t_C(c) (c ## i64) |
100 | # define uint64_t_C(c) (c ## i64) | |
1a565432 | 101 | |
49e17f84 MN |
102 | # ifdef HAVE_AV_CONFIG_H |
103 | # define inline __inline | |
104 | # endif | |
1a565432 | 105 | |
9b59c92f | 106 | # else |
0c1a9eda ZK |
107 | # define int64_t_C(c) (c ## LL) |
108 | # define uint64_t_C(c) (c ## ULL) | |
9b59c92f | 109 | # endif /* __MINGW32__ */ |
a74127c0 | 110 | |
49e17f84 MN |
111 | # ifdef HAVE_AV_CONFIG_H |
112 | # ifdef _DEBUG | |
113 | # define DEBUG | |
114 | # endif | |
de6d9b64 | 115 | |
49e17f84 MN |
116 | # define snprintf _snprintf |
117 | # define vsnprintf _vsnprintf | |
118 | # endif | |
1a565432 | 119 | |
f3ec2d46 SG |
120 | /* CONFIG_WIN32 end */ |
121 | #elif defined (CONFIG_OS2) | |
122 | /* OS/2 EMX */ | |
123 | ||
124 | #include <inttypes.h> | |
125 | ||
0c1a9eda ZK |
126 | #ifndef int64_t_C |
127 | #define int64_t_C(c) (c ## LL) | |
128 | #define uint64_t_C(c) (c ## ULL) | |
f3ec2d46 SG |
129 | #endif |
130 | ||
f5fe9d5f FB |
131 | #ifdef HAVE_AV_CONFIG_H |
132 | ||
f3ec2d46 SG |
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 | |
1a565432 FB |
143 | |
144 | /* unix */ | |
145 | ||
f5fe9d5f | 146 | #include <inttypes.h> |
1a565432 | 147 | |
f5fe9d5f FB |
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 | |
1a565432 | 154 | |
9b59c92f MN |
155 | # ifdef USE_FASTMEMCPY |
156 | # include "fastmemcpy.h" | |
157 | # endif | |
9b59c92f | 158 | # endif /* HAVE_AV_CONFIG_H */ |
a74127c0 | 159 | |
f3ec2d46 | 160 | #endif /* !CONFIG_WIN32 && !CONFIG_OS2 */ |
a74127c0 | 161 | |
a74127c0 FB |
162 | #ifdef HAVE_AV_CONFIG_H |
163 | ||
9b59c92f | 164 | # include "bswap.h" |
96707bb7 | 165 | |
9b59c92f | 166 | # if defined(__MINGW32__) || defined(__CYGWIN__) || \ |
814b648c | 167 | defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__)) |
9b59c92f MN |
168 | # define MANGLE(a) "_" #a |
169 | # else | |
170 | # define MANGLE(a) #a | |
171 | # endif | |
6a3d7e36 | 172 | |
d503f970 FB |
173 | /* debug stuff */ |
174 | ||
9b59c92f MN |
175 | # ifndef DEBUG |
176 | # define NDEBUG | |
177 | # endif | |
178 | # include <assert.h> | |
1a565432 | 179 | |
a74127c0 | 180 | /* dprintf macros */ |
9b59c92f | 181 | # if defined(CONFIG_WIN32) && !defined(__MINGW32__) |
a74127c0 FB |
182 | |
183 | inline void dprintf(const char* fmt,...) {} | |
184 | ||
9b59c92f | 185 | # else |
a74127c0 | 186 | |
9b59c92f | 187 | # ifdef DEBUG |
dc7cb06a | 188 | # define dprintf(fmt,...) printf(fmt, __VA_ARGS__) |
9b59c92f | 189 | # else |
dc7cb06a | 190 | # define dprintf(fmt,...) |
9b59c92f | 191 | # endif |
1a565432 | 192 | |
9b59c92f | 193 | # endif /* !CONFIG_WIN32 */ |
1a565432 | 194 | |
9b59c92f | 195 | # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0) |
935cdf09 | 196 | |
073b013d MN |
197 | //rounded divison & shift |
198 | #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) | |
d7e9533a MN |
199 | /* assume b>0 */ |
200 | #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) | |
32cd20de | 201 | #define ABS(a) ((a) >= 0 ? (a) : (-(a))) |
75460b0c | 202 | |
b8a78f41 MN |
203 | #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) |
204 | #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) | |
d7e9533a | 205 | |
d4961b35 B |
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 | ||
ef5e74bb MN |
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 | } | |
8db1a1dd | 241 | #else |
9b59c92f MN |
242 | # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) |
243 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) | |
8db1a1dd MN |
244 | #endif |
245 | ||
de6d9b64 FB |
246 | /* bit output */ |
247 | ||
248 | struct PutBitContext; | |
249 | ||
0c1a9eda | 250 | typedef void (*WriteDataFunc)(void *, uint8_t *, int); |
de6d9b64 FB |
251 | |
252 | typedef struct PutBitContext { | |
17592475 | 253 | #ifdef ALT_BITSTREAM_WRITER |
0c1a9eda | 254 | uint8_t *buf, *buf_end; |
17592475 MN |
255 | int index; |
256 | #else | |
0c1a9eda | 257 | uint32_t bit_buf; |
d8cf5aea | 258 | int bit_left; |
0c1a9eda | 259 | uint8_t *buf, *buf_ptr, *buf_end; |
17592475 | 260 | #endif |
0c1a9eda | 261 | int64_t data_out_size; /* in bytes */ |
de6d9b64 FB |
262 | } PutBitContext; |
263 | ||
ed7debda | 264 | void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size); |
17592475 | 265 | |
0c1a9eda | 266 | int64_t get_bit_count(PutBitContext *s); /* XXX: change function name */ |
de6d9b64 FB |
267 | void align_put_bits(PutBitContext *s); |
268 | void flush_put_bits(PutBitContext *s); | |
9717dad8 | 269 | void put_string(PutBitContext * pbc, char *s); |
de6d9b64 | 270 | |
de6d9b64 FB |
271 | /* bit input */ |
272 | ||
273 | typedef struct GetBitContext { | |
ae39132d | 274 | const uint8_t *buffer, *buffer_end; |
27a3e2c5 MN |
275 | #ifdef ALT_BITSTREAM_READER |
276 | int index; | |
8db1a1dd | 277 | #elif defined LIBMPEG2_BITSTREAM_READER |
0c1a9eda ZK |
278 | uint8_t *buffer_ptr; |
279 | uint32_t cache; | |
8db1a1dd MN |
280 | int bit_count; |
281 | #elif defined A32_BITSTREAM_READER | |
0c1a9eda ZK |
282 | uint32_t *buffer_ptr; |
283 | uint32_t cache0; | |
284 | uint32_t cache1; | |
8db1a1dd | 285 | int bit_count; |
27a3e2c5 | 286 | #endif |
68f593b4 | 287 | int size_in_bits; |
de6d9b64 FB |
288 | } GetBitContext; |
289 | ||
49092244 MN |
290 | static inline int get_bits_count(GetBitContext *s); |
291 | ||
0c1a9eda | 292 | #define VLC_TYPE int16_t |
8db1a1dd | 293 | |
de6d9b64 FB |
294 | typedef struct VLC { |
295 | int bits; | |
64863965 | 296 | VLC_TYPE (*table)[2]; ///< code, bits |
de6d9b64 FB |
297 | int table_size, table_allocated; |
298 | } VLC; | |
299 | ||
45a82edb MN |
300 | typedef struct RL_VLC_ELEM { |
301 | int16_t level; | |
302 | int8_t len; | |
303 | uint8_t run; | |
304 | } RL_VLC_ELEM; | |
305 | ||
35fedfc3 PG |
306 | #ifdef ARCH_SPARC64 |
307 | #define UNALIGNED_STORES_ARE_BAD | |
308 | #endif | |
309 | ||
3587d501 MN |
310 | /* used to avoid missaligned exceptions on some archs (alpha, ...) */ |
311 | #ifdef ARCH_X86 | |
0c1a9eda | 312 | # define unaligned32(a) (*(uint32_t*)(a)) |
3587d501 | 313 | #else |
9b59c92f | 314 | # ifdef __GNUC__ |
3587d501 MN |
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 | } | |
9b59c92f | 322 | # elif defined(__DECC) |
3587d501 MN |
323 | static inline uint32_t unaligned32(const void *v) { |
324 | return *(const __unaligned uint32_t *) v; | |
325 | } | |
9b59c92f | 326 | # else |
3587d501 MN |
327 | static inline uint32_t unaligned32(const void *v) { |
328 | return *(const uint32_t *) v; | |
329 | } | |
9b59c92f | 330 | # endif |
3587d501 MN |
331 | #endif //!ARCH_X86 |
332 | ||
d8cf5aea MN |
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)); | |
fcb609f4 | 344 | |
d8cf5aea MN |
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); | |
35fedfc3 PG |
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 | |
0c1a9eda | 364 | *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); |
d8cf5aea MN |
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 | ||
17592475 | 377 | #ifdef ALT_BITSTREAM_WRITER |
4f0f8bfc | 378 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) |
17592475 | 379 | { |
9b59c92f MN |
380 | # ifdef ALIGNED_BITSTREAM_WRITER |
381 | # ifdef ARCH_X86 | |
4f0f8bfc MN |
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 | ); | |
9b59c92f | 399 | # else |
4f0f8bfc MN |
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; | |
9b59c92f MN |
410 | # endif |
411 | # else //ALIGNED_BITSTREAM_WRITER | |
412 | # ifdef ARCH_X86 | |
17592475 MN |
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 | ); | |
9b59c92f | 429 | # else |
17592475 MN |
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; | |
9b59c92f MN |
438 | # endif |
439 | # endif //!ALIGNED_BITSTREAM_WRITER | |
17592475 MN |
440 | } |
441 | #endif | |
442 | ||
17592475 | 443 | |
17592475 MN |
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 | ||
8db1a1dd MN |
453 | /* Bitstream reader API docs: |
454 | name | |
455 | abritary name which is used as prefix for the internal variables | |
de6d9b64 | 456 | |
8db1a1dd MN |
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 | */ | |
2931ecb9 | 497 | |
d8e00c09 B |
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 | ||
27a3e2c5 | 508 | #ifdef ALT_BITSTREAM_READER |
8db1a1dd | 509 | # define MIN_CACHE_BITS 25 |
27a3e2c5 | 510 | |
8db1a1dd MN |
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)\ | |
d8e00c09 | 519 | name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ |
8db1a1dd MN |
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)\ | |
ef5e74bb | 538 | NEG_USR32(name##_cache, num) |
8db1a1dd MN |
539 | |
540 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 541 | NEG_SSR32(name##_cache, num) |
8db1a1dd MN |
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; | |
2931ecb9 | 548 | } |
8db1a1dd MN |
549 | #elif defined LIBMPEG2_BITSTREAM_READER |
550 | //libmpeg2 like reader | |
551 | ||
3bb10888 | 552 | # define MIN_CACHE_BITS 17 |
8db1a1dd MN |
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 | ||
d8e00c09 B |
564 | #ifdef LIBMPEG2_BITSTREAM_READER_HACK |
565 | ||
566 | # define UPDATE_CACHE(name, gb)\ | |
567 | if(name##_bit_count >= 0){\ | |
17fb5fd3 MN |
568 | name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\ |
569 | ((uint16_t*)name##_buffer_ptr)++;\ | |
d8e00c09 B |
570 | name##_bit_count-= 16;\ |
571 | }\ | |
572 | ||
573 | #else | |
574 | ||
8db1a1dd | 575 | # define UPDATE_CACHE(name, gb)\ |
3bb10888 | 576 | if(name##_bit_count >= 0){\ |
8db1a1dd MN |
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 | }\ | |
2931ecb9 | 581 | |
d8e00c09 B |
582 | #endif |
583 | ||
8db1a1dd MN |
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)\ | |
ef5e74bb | 600 | NEG_USR32(name##_cache, num) |
8db1a1dd MN |
601 | |
602 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 603 | NEG_SSR32(name##_cache, num) |
8db1a1dd MN |
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; | |
612476ef A |
610 | } |
611 | ||
8db1a1dd MN |
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 );\ | |
ef5e74bb | 631 | name##_cache0 |= NEG_USR32(next,name##_bit_count);\ |
8db1a1dd MN |
632 | name##_cache1 |= next<<name##_bit_count;\ |
633 | name##_buffer_ptr++;\ | |
634 | name##_bit_count-= 32;\ | |
635 | }\ | |
636 | ||
3587d501 | 637 | #ifdef ARCH_X86 |
8db1a1dd MN |
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 | ); | |
3587d501 | 645 | #else |
8db1a1dd MN |
646 | # define SKIP_CACHE(name, gb, num)\ |
647 | name##_cache0 <<= (num);\ | |
ef5e74bb | 648 | name##_cache0 |= NEG_USR32(name##_cache1,num);\ |
8db1a1dd | 649 | name##_cache1 <<= (num); |
3587d501 | 650 | #endif |
27a3e2c5 | 651 | |
8db1a1dd MN |
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)\ | |
ef5e74bb | 665 | NEG_USR32(name##_cache0, num) |
8db1a1dd MN |
666 | |
667 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 668 | NEG_SSR32(name##_cache0, num) |
8db1a1dd MN |
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; | |
4949028f J |
675 | } |
676 | ||
49092244 | 677 | #endif |
49092244 | 678 | |
17fb5fd3 MN |
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 | */ | |
d8e00c09 B |
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 | ||
17fb5fd3 MN |
713 | /** |
714 | * reads 0-17 bits. | |
715 | * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant | |
716 | */ | |
8db1a1dd MN |
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 | } | |
49092244 | 726 | |
17fb5fd3 MN |
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 | */ | |
8db1a1dd MN |
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; | |
49092244 MN |
740 | } |
741 | ||
17fb5fd3 MN |
742 | unsigned int show_bits_long(GetBitContext *s, int n); |
743 | ||
612476ef | 744 | static inline void skip_bits(GetBitContext *s, int n){ |
8db1a1dd MN |
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){ | |
27a3e2c5 | 753 | #ifdef ALT_BITSTREAM_READER |
8db1a1dd MN |
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; | |
fcb609f4 | 760 | |
8db1a1dd | 761 | return result; |
27a3e2c5 | 762 | #else |
8db1a1dd | 763 | return get_bits(s, 1); |
27a3e2c5 | 764 | #endif |
612476ef A |
765 | } |
766 | ||
8db1a1dd MN |
767 | static inline unsigned int show_bits1(GetBitContext *s){ |
768 | return show_bits(s, 1); | |
612476ef A |
769 | } |
770 | ||
8db1a1dd MN |
771 | static inline void skip_bits1(GetBitContext *s){ |
772 | skip_bits(s, 1); | |
9dbf9389 | 773 | } |
612476ef | 774 | |
8db1a1dd | 775 | void init_get_bits(GetBitContext *s, |
ae39132d | 776 | const uint8_t *buffer, int buffer_size); |
8db1a1dd | 777 | |
18f77016 | 778 | int check_marker(GetBitContext *s, const char *msg); |
de6d9b64 FB |
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); | |
de6d9b64 | 784 | |
68f593b4 MN |
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 | */ | |
cceabc86 MN |
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;\ | |
cceabc86 | 804 | \ |
6300c80a | 805 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ |
cceabc86 MN |
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;\ | |
cceabc86 | 813 | \ |
6300c80a | 814 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ |
cceabc86 MN |
815 | code = table[index][0];\ |
816 | n = table[index][1];\ | |
817 | }\ | |
818 | }\ | |
819 | SKIP_BITS(name, gb, n)\ | |
820 | } | |
821 | ||
45a82edb MN |
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 | ||
08dce7b0 | 844 | // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly |
3587d501 MN |
845 | static inline int get_vlc(GetBitContext *s, VLC *vlc) |
846 | { | |
cceabc86 MN |
847 | int code; |
848 | VLC_TYPE (*table)[2]= vlc->table; | |
849 | ||
8db1a1dd | 850 | OPEN_READER(re, s) |
8db1a1dd | 851 | UPDATE_CACHE(re, s) |
3587d501 | 852 | |
cceabc86 | 853 | GET_VLC(code, re, s, table, vlc->bits, 3) |
8db1a1dd | 854 | |
8db1a1dd | 855 | CLOSE_READER(re, s) |
3587d501 | 856 | return code; |
08dce7b0 MN |
857 | } |
858 | ||
d4c48e48 MN |
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 | */ | |
d200cab6 FH |
867 | static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], |
868 | int bits, int max_depth) | |
08dce7b0 MN |
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; | |
3587d501 MN |
879 | } |
880 | ||
64338956 MN |
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 | } | |
3d2e8cce MN |
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 | } | |
64338956 MN |
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__) | |
3d2e8cce | 925 | #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
3b0da3f9 | 926 | #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
64338956 MN |
927 | #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) |
928 | ||
95c26348 MN |
929 | #define tprintf printf |
930 | ||
931 | #else //TRACE | |
c8816e01 | 932 | #define tprintf(_arg...) {} |
95c26348 | 933 | #endif |
45a82edb | 934 | |
de6d9b64 FB |
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 */ | |
c81f0349 | 960 | extern const uint8_t ff_log2_tab[256]; |
de6d9b64 | 961 | |
a822a479 | 962 | static inline int av_log2(unsigned int v) |
de6d9b64 FB |
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 | } | |
c81f0349 MN |
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; | |
de6d9b64 | 988 | } |
c81f0349 MN |
989 | n += ff_log2_tab[v]; |
990 | ||
de6d9b64 FB |
991 | return n; |
992 | } | |
993 | ||
c81f0349 | 994 | |
45870f57 MN |
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 | ||
91029be7 MN |
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 | ||
9dbcbd92 | 1023 | /* math */ |
c81f0349 | 1024 | extern const uint8_t ff_sqrt_tab[128]; |
f36db5df | 1025 | |
14bea432 | 1026 | int64_t ff_gcd(int64_t a, int64_t b); |
9dbcbd92 | 1027 | |
7fd08ac1 MN |
1028 | static inline int ff_sqrt(int a) |
1029 | { | |
1030 | int ret=0; | |
1031 | int s; | |
1032 | int ret_sq=0; | |
f36db5df MN |
1033 | |
1034 | if(a<128) return ff_sqrt_tab[a]; | |
1035 | ||
7fd08ac1 MN |
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 | } | |
202ef8b8 MN |
1045 | |
1046 | /** | |
1047 | * converts fourcc string to int | |
1048 | */ | |
6a85ec8d | 1049 | static inline int ff_get_fourcc(const char *s){ |
202ef8b8 | 1050 | assert( strlen(s)==4 ); |
966df5b6 | 1051 | |
202ef8b8 MN |
1052 | return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24); |
1053 | } | |
1054 | ||
e8750b00 FR |
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 | ||
2ad1516a MN |
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 | ||
7fd08ac1 MN |
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 | ||
b534c7f9 MN |
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 | ||
a69b930c AB |
1122 | #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d) |
1123 | ||
8e1e6f31 FB |
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 | ||
7bc9090a MN |
1129 | #define CHECKED_ALLOCZ(p, size)\ |
1130 | {\ | |
1131 | p= av_mallocz(size);\ | |
dc939fd5 | 1132 | if(p==NULL && (size)!=0){\ |
7bc9090a MN |
1133 | perror("malloc");\ |
1134 | goto fail;\ | |
1135 | }\ | |
1136 | } | |
1137 | ||
96707bb7 FB |
1138 | #endif /* HAVE_AV_CONFIG_H */ |
1139 | ||
1140 | #endif /* COMMON_H */ |