Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | #ifndef COMMON_H |
2 | #define COMMON_H | |
3 | ||
73120e55 FB |
4 | #define FFMPEG_VERSION_INT 0x000406 |
5 | #define FFMPEG_VERSION "0.4.6" | |
1a565432 | 6 | |
4bdd9157 | 7 | #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) |
9b59c92f | 8 | # define CONFIG_WIN32 |
1a565432 FB |
9 | #endif |
10 | ||
17592475 | 11 | //#define ALT_BITSTREAM_WRITER |
4f0f8bfc | 12 | //#define ALIGNED_BITSTREAM_WRITER |
3530320d | 13 | |
8db1a1dd MN |
14 | #define ALT_BITSTREAM_READER |
15 | //#define LIBMPEG2_BITSTREAM_READER | |
16 | //#define A32_BITSTREAM_READER | |
17 | ||
420b073b | 18 | #ifdef HAVE_AV_CONFIG_H |
1a565432 | 19 | /* only include the following when compiling package */ |
9b59c92f MN |
20 | # include "config.h" |
21 | ||
22 | # include <stdlib.h> | |
23 | # include <stdio.h> | |
24 | # include <string.h> | |
25 | # ifndef __BEOS__ | |
26 | # include <errno.h> | |
27 | # else | |
28 | # include "berrno.h" | |
29 | # endif | |
30 | # include <math.h> | |
31 | ||
32 | # ifndef ENODATA | |
33 | # define ENODATA 61 | |
34 | # endif | |
1a565432 | 35 | |
c4d82fea FB |
36 | #ifndef M_PI |
37 | #define M_PI 3.14159265358979323846 | |
38 | #endif | |
39 | ||
44f27b3a | 40 | #endif /* HAVE_AV_CONFIG_H */ |
1a565432 | 41 | |
02da51ec FH |
42 | /* Suppress restrict if it was not defined in config.h. */ |
43 | #ifndef restrict | |
9b59c92f | 44 | # define restrict |
02da51ec FH |
45 | #endif |
46 | ||
d200cab6 | 47 | #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0) |
9b59c92f | 48 | # define always_inline __attribute__((always_inline)) inline |
d200cab6 | 49 | #else |
9b59c92f | 50 | # define always_inline inline |
d200cab6 FH |
51 | #endif |
52 | ||
1a565432 FB |
53 | #ifdef CONFIG_WIN32 |
54 | ||
55 | /* windows */ | |
56 | ||
57 | typedef unsigned short UINT16; | |
58 | typedef signed short INT16; | |
59 | typedef unsigned char UINT8; | |
60 | typedef unsigned int UINT32; | |
61 | typedef unsigned __int64 UINT64; | |
62 | typedef signed char INT8; | |
63 | typedef signed int INT32; | |
64 | typedef signed __int64 INT64; | |
65 | ||
66 | typedef UINT8 uint8_t; | |
67 | typedef INT8 int8_t; | |
68 | typedef UINT16 uint16_t; | |
69 | typedef INT16 int16_t; | |
70 | typedef UINT32 uint32_t; | |
71 | typedef INT32 int32_t; | |
d503f970 FB |
72 | typedef UINT64 uint64_t; |
73 | typedef INT64 int64_t; | |
1a565432 | 74 | |
9b59c92f MN |
75 | # ifndef __MINGW32__ |
76 | # define INT64_C(c) (c ## i64) | |
77 | # define UINT64_C(c) (c ## i64) | |
1a565432 | 78 | |
9b59c92f | 79 | # define inline __inline |
1a565432 | 80 | |
9b59c92f MN |
81 | # else |
82 | # define INT64_C(c) (c ## LL) | |
83 | # define UINT64_C(c) (c ## ULL) | |
84 | # endif /* __MINGW32__ */ | |
a74127c0 | 85 | |
9b59c92f MN |
86 | # ifdef _DEBUG |
87 | # define DEBUG | |
88 | # endif | |
de6d9b64 | 89 | |
9b59c92f | 90 | # define snprintf _snprintf |
1a565432 | 91 | |
d503f970 | 92 | #else /* CONFIG_WIN32 */ |
1a565432 FB |
93 | |
94 | /* unix */ | |
95 | ||
9b59c92f | 96 | # include <inttypes.h> |
1a565432 | 97 | |
9b59c92f | 98 | # ifndef __WINE_WINDEF16_H |
980fc7b8 | 99 | /* workaround for typedef conflict in MPlayer (wine typedefs) */ |
de6d9b64 FB |
100 | typedef unsigned short UINT16; |
101 | typedef signed short INT16; | |
9b59c92f | 102 | # endif |
de6d9b64 FB |
103 | |
104 | typedef unsigned char UINT8; | |
105 | typedef unsigned int UINT32; | |
106 | typedef unsigned long long UINT64; | |
107 | typedef signed char INT8; | |
108 | typedef signed int INT32; | |
109 | typedef signed long long INT64; | |
110 | ||
9b59c92f | 111 | # ifdef HAVE_AV_CONFIG_H |
9b59c92f MN |
112 | # ifndef INT64_C |
113 | # define INT64_C(c) (c ## LL) | |
114 | # define UINT64_C(c) (c ## ULL) | |
115 | # endif | |
1a565432 | 116 | |
9b59c92f MN |
117 | # ifdef USE_FASTMEMCPY |
118 | # include "fastmemcpy.h" | |
119 | # endif | |
9b59c92f | 120 | # endif /* HAVE_AV_CONFIG_H */ |
a74127c0 FB |
121 | |
122 | #endif /* !CONFIG_WIN32 */ | |
123 | ||
a74127c0 FB |
124 | #ifdef HAVE_AV_CONFIG_H |
125 | ||
9b59c92f | 126 | # include "bswap.h" |
96707bb7 | 127 | |
9b59c92f MN |
128 | # if defined(__MINGW32__) || defined(__CYGWIN__) || \ |
129 | defined(__OS2__) || defined (__OpenBSD__) | |
130 | # define MANGLE(a) "_" #a | |
131 | # else | |
132 | # define MANGLE(a) #a | |
133 | # endif | |
6a3d7e36 | 134 | |
d503f970 FB |
135 | /* debug stuff */ |
136 | ||
9b59c92f MN |
137 | # ifndef DEBUG |
138 | # define NDEBUG | |
139 | # endif | |
140 | # include <assert.h> | |
1a565432 | 141 | |
a74127c0 | 142 | /* dprintf macros */ |
9b59c92f | 143 | # if defined(CONFIG_WIN32) && !defined(__MINGW32__) |
a74127c0 FB |
144 | |
145 | inline void dprintf(const char* fmt,...) {} | |
146 | ||
9b59c92f | 147 | # else |
a74127c0 | 148 | |
9b59c92f MN |
149 | # ifdef DEBUG |
150 | # define dprintf(fmt,args...) printf(fmt, ## args) | |
151 | # else | |
152 | # define dprintf(fmt,args...) | |
153 | # endif | |
1a565432 | 154 | |
9b59c92f | 155 | # endif /* !CONFIG_WIN32 */ |
1a565432 | 156 | |
9b59c92f | 157 | # define av_abort() do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0) |
935cdf09 | 158 | |
073b013d MN |
159 | //rounded divison & shift |
160 | #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) | |
d7e9533a MN |
161 | /* assume b>0 */ |
162 | #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) | |
32cd20de | 163 | #define ABS(a) ((a) >= 0 ? (a) : (-(a))) |
75460b0c | 164 | |
b8a78f41 MN |
165 | #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) |
166 | #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) | |
d7e9533a | 167 | |
ef5e74bb MN |
168 | #ifdef ARCH_X86 |
169 | // avoid +32 for shift optimization (gcc should do that ...) | |
170 | static inline int32_t NEG_SSR32( int32_t a, int8_t s){ | |
171 | asm ("sarl %1, %0\n\t" | |
172 | : "+r" (a) | |
173 | : "ic" ((uint8_t)(-s)) | |
174 | ); | |
175 | return a; | |
176 | } | |
177 | static inline uint32_t NEG_USR32(uint32_t a, int8_t s){ | |
178 | asm ("shrl %1, %0\n\t" | |
179 | : "+r" (a) | |
180 | : "ic" ((uint8_t)(-s)) | |
181 | ); | |
182 | return a; | |
183 | } | |
8db1a1dd | 184 | #else |
9b59c92f MN |
185 | # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) |
186 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) | |
8db1a1dd MN |
187 | #endif |
188 | ||
de6d9b64 FB |
189 | /* bit output */ |
190 | ||
191 | struct PutBitContext; | |
192 | ||
193 | typedef void (*WriteDataFunc)(void *, UINT8 *, int); | |
194 | ||
195 | typedef struct PutBitContext { | |
17592475 MN |
196 | #ifdef ALT_BITSTREAM_WRITER |
197 | UINT8 *buf, *buf_end; | |
198 | int index; | |
199 | #else | |
de6d9b64 | 200 | UINT32 bit_buf; |
d8cf5aea | 201 | int bit_left; |
2931ecb9 | 202 | UINT8 *buf, *buf_ptr, *buf_end; |
17592475 MN |
203 | #endif |
204 | INT64 data_out_size; /* in bytes */ | |
de6d9b64 FB |
205 | } PutBitContext; |
206 | ||
207 | void init_put_bits(PutBitContext *s, | |
208 | UINT8 *buffer, int buffer_size, | |
209 | void *opaque, | |
210 | void (*write_data)(void *, UINT8 *, int)); | |
17592475 | 211 | |
9dbf9389 | 212 | INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */ |
de6d9b64 FB |
213 | void align_put_bits(PutBitContext *s); |
214 | void flush_put_bits(PutBitContext *s); | |
9717dad8 | 215 | void put_string(PutBitContext * pbc, char *s); |
de6d9b64 | 216 | |
de6d9b64 FB |
217 | /* bit input */ |
218 | ||
219 | typedef struct GetBitContext { | |
8db1a1dd | 220 | UINT8 *buffer, *buffer_end; |
27a3e2c5 MN |
221 | #ifdef ALT_BITSTREAM_READER |
222 | int index; | |
8db1a1dd MN |
223 | #elif defined LIBMPEG2_BITSTREAM_READER |
224 | UINT8 *buffer_ptr; | |
225 | UINT32 cache; | |
226 | int bit_count; | |
227 | #elif defined A32_BITSTREAM_READER | |
228 | UINT32 *buffer_ptr; | |
229 | UINT32 cache0; | |
230 | UINT32 cache1; | |
231 | int bit_count; | |
27a3e2c5 | 232 | #endif |
45870f57 | 233 | int size; |
de6d9b64 FB |
234 | } GetBitContext; |
235 | ||
49092244 MN |
236 | static inline int get_bits_count(GetBitContext *s); |
237 | ||
8db1a1dd MN |
238 | #define VLC_TYPE INT16 |
239 | ||
de6d9b64 FB |
240 | typedef struct VLC { |
241 | int bits; | |
8db1a1dd | 242 | VLC_TYPE (*table)[2]; // code, bits |
de6d9b64 FB |
243 | int table_size, table_allocated; |
244 | } VLC; | |
245 | ||
45a82edb MN |
246 | typedef struct RL_VLC_ELEM { |
247 | int16_t level; | |
248 | int8_t len; | |
249 | uint8_t run; | |
250 | } RL_VLC_ELEM; | |
251 | ||
35fedfc3 PG |
252 | #ifdef ARCH_SPARC64 |
253 | #define UNALIGNED_STORES_ARE_BAD | |
254 | #endif | |
255 | ||
3587d501 MN |
256 | /* used to avoid missaligned exceptions on some archs (alpha, ...) */ |
257 | #ifdef ARCH_X86 | |
9b59c92f | 258 | # define unaligned32(a) (*(UINT32*)(a)) |
3587d501 | 259 | #else |
9b59c92f | 260 | # ifdef __GNUC__ |
3587d501 MN |
261 | static inline uint32_t unaligned32(const void *v) { |
262 | struct Unaligned { | |
263 | uint32_t i; | |
264 | } __attribute__((packed)); | |
265 | ||
266 | return ((const struct Unaligned *) v)->i; | |
267 | } | |
9b59c92f | 268 | # elif defined(__DECC) |
3587d501 MN |
269 | static inline uint32_t unaligned32(const void *v) { |
270 | return *(const __unaligned uint32_t *) v; | |
271 | } | |
9b59c92f | 272 | # else |
3587d501 MN |
273 | static inline uint32_t unaligned32(const void *v) { |
274 | return *(const uint32_t *) v; | |
275 | } | |
9b59c92f | 276 | # endif |
3587d501 MN |
277 | #endif //!ARCH_X86 |
278 | ||
d8cf5aea MN |
279 | #ifndef ALT_BITSTREAM_WRITER |
280 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) | |
281 | { | |
282 | unsigned int bit_buf; | |
283 | int bit_left; | |
284 | ||
285 | #ifdef STATS | |
286 | st_out_bit_counts[st_current_index] += n; | |
287 | #endif | |
288 | // printf("put_bits=%d %x\n", n, value); | |
289 | assert(n == 32 || value < (1U << n)); | |
fcb609f4 | 290 | |
d8cf5aea MN |
291 | bit_buf = s->bit_buf; |
292 | bit_left = s->bit_left; | |
293 | ||
294 | // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | |
295 | /* XXX: optimize */ | |
296 | if (n < bit_left) { | |
297 | bit_buf = (bit_buf<<n) | value; | |
298 | bit_left-=n; | |
299 | } else { | |
300 | bit_buf<<=bit_left; | |
301 | bit_buf |= value >> (n - bit_left); | |
35fedfc3 PG |
302 | #ifdef UNALIGNED_STORES_ARE_BAD |
303 | if (3 & (int) s->buf_ptr) { | |
304 | s->buf_ptr[0] = bit_buf >> 24; | |
305 | s->buf_ptr[1] = bit_buf >> 16; | |
306 | s->buf_ptr[2] = bit_buf >> 8; | |
307 | s->buf_ptr[3] = bit_buf ; | |
308 | } else | |
309 | #endif | |
d8cf5aea MN |
310 | *(UINT32 *)s->buf_ptr = be2me_32(bit_buf); |
311 | //printf("bitbuf = %08x\n", bit_buf); | |
312 | s->buf_ptr+=4; | |
313 | bit_left+=32 - n; | |
314 | bit_buf = value; | |
315 | } | |
316 | ||
317 | s->bit_buf = bit_buf; | |
318 | s->bit_left = bit_left; | |
319 | } | |
320 | #endif | |
321 | ||
322 | ||
17592475 | 323 | #ifdef ALT_BITSTREAM_WRITER |
4f0f8bfc | 324 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) |
17592475 | 325 | { |
9b59c92f MN |
326 | # ifdef ALIGNED_BITSTREAM_WRITER |
327 | # ifdef ARCH_X86 | |
4f0f8bfc MN |
328 | asm volatile( |
329 | "movl %0, %%ecx \n\t" | |
330 | "xorl %%eax, %%eax \n\t" | |
331 | "shrdl %%cl, %1, %%eax \n\t" | |
332 | "shrl %%cl, %1 \n\t" | |
333 | "movl %0, %%ecx \n\t" | |
334 | "shrl $3, %%ecx \n\t" | |
335 | "andl $0xFFFFFFFC, %%ecx \n\t" | |
336 | "bswapl %1 \n\t" | |
337 | "orl %1, (%2, %%ecx) \n\t" | |
338 | "bswapl %%eax \n\t" | |
339 | "addl %3, %0 \n\t" | |
340 | "movl %%eax, 4(%2, %%ecx) \n\t" | |
341 | : "=&r" (s->index), "=&r" (value) | |
342 | : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n)) | |
343 | : "%eax", "%ecx" | |
344 | ); | |
9b59c92f | 345 | # else |
4f0f8bfc MN |
346 | int index= s->index; |
347 | uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5); | |
348 | ||
349 | value<<= 32-n; | |
350 | ||
351 | ptr[0] |= be2me_32(value>>(index&31)); | |
352 | ptr[1] = be2me_32(value<<(32-(index&31))); | |
353 | //if(n>24) printf("%d %d\n", n, value); | |
354 | index+= n; | |
355 | s->index= index; | |
9b59c92f MN |
356 | # endif |
357 | # else //ALIGNED_BITSTREAM_WRITER | |
358 | # ifdef ARCH_X86 | |
17592475 MN |
359 | asm volatile( |
360 | "movl $7, %%ecx \n\t" | |
361 | "andl %0, %%ecx \n\t" | |
362 | "addl %3, %%ecx \n\t" | |
363 | "negl %%ecx \n\t" | |
364 | "shll %%cl, %1 \n\t" | |
365 | "bswapl %1 \n\t" | |
366 | "movl %0, %%ecx \n\t" | |
367 | "shrl $3, %%ecx \n\t" | |
368 | "orl %1, (%%ecx, %2) \n\t" | |
369 | "addl %3, %0 \n\t" | |
370 | "movl $0, 4(%%ecx, %2) \n\t" | |
371 | : "=&r" (s->index), "=&r" (value) | |
372 | : "r" (s->buf), "r" (n), "0" (s->index), "1" (value) | |
373 | : "%ecx" | |
374 | ); | |
9b59c92f | 375 | # else |
17592475 MN |
376 | int index= s->index; |
377 | uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3)); | |
378 | ||
379 | ptr[0] |= be2me_32(value<<(32-n-(index&7) )); | |
380 | ptr[1] = 0; | |
381 | //if(n>24) printf("%d %d\n", n, value); | |
382 | index+= n; | |
383 | s->index= index; | |
9b59c92f MN |
384 | # endif |
385 | # endif //!ALIGNED_BITSTREAM_WRITER | |
17592475 MN |
386 | } |
387 | #endif | |
388 | ||
17592475 | 389 | |
17592475 MN |
390 | static inline uint8_t* pbBufPtr(PutBitContext *s) |
391 | { | |
392 | #ifdef ALT_BITSTREAM_WRITER | |
393 | return s->buf + (s->index>>3); | |
394 | #else | |
395 | return s->buf_ptr; | |
396 | #endif | |
397 | } | |
398 | ||
8db1a1dd MN |
399 | /* Bitstream reader API docs: |
400 | name | |
401 | abritary name which is used as prefix for the internal variables | |
de6d9b64 | 402 | |
8db1a1dd MN |
403 | gb |
404 | getbitcontext | |
405 | ||
406 | OPEN_READER(name, gb) | |
407 | loads gb into local variables | |
408 | ||
409 | CLOSE_READER(name, gb) | |
410 | stores local vars in gb | |
411 | ||
412 | UPDATE_CACHE(name, gb) | |
413 | refills the internal cache from the bitstream | |
414 | after this call at least MIN_CACHE_BITS will be available, | |
415 | ||
416 | GET_CACHE(name, gb) | |
417 | will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) | |
418 | ||
419 | SHOW_UBITS(name, gb, num) | |
420 | will return the nest num bits | |
421 | ||
422 | SHOW_SBITS(name, gb, num) | |
423 | will return the nest num bits and do sign extension | |
424 | ||
425 | SKIP_BITS(name, gb, num) | |
426 | will skip over the next num bits | |
427 | note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER | |
428 | ||
429 | SKIP_CACHE(name, gb, num) | |
430 | will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) | |
431 | ||
432 | SKIP_COUNTER(name, gb, num) | |
433 | will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) | |
434 | ||
435 | LAST_SKIP_CACHE(name, gb, num) | |
436 | will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing | |
437 | ||
438 | LAST_SKIP_BITS(name, gb, num) | |
439 | is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER | |
440 | ||
441 | for examples see get_bits, show_bits, skip_bits, get_vlc | |
442 | */ | |
2931ecb9 | 443 | |
27a3e2c5 | 444 | #ifdef ALT_BITSTREAM_READER |
8db1a1dd | 445 | # define MIN_CACHE_BITS 25 |
27a3e2c5 | 446 | |
8db1a1dd MN |
447 | # define OPEN_READER(name, gb)\ |
448 | int name##_index= (gb)->index;\ | |
449 | int name##_cache= 0;\ | |
450 | ||
451 | # define CLOSE_READER(name, gb)\ | |
452 | (gb)->index= name##_index;\ | |
453 | ||
454 | # define UPDATE_CACHE(name, gb)\ | |
455 | name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\ | |
456 | ||
457 | # define SKIP_CACHE(name, gb, num)\ | |
458 | name##_cache <<= (num);\ | |
459 | ||
460 | // FIXME name? | |
461 | # define SKIP_COUNTER(name, gb, num)\ | |
462 | name##_index += (num);\ | |
463 | ||
464 | # define SKIP_BITS(name, gb, num)\ | |
465 | {\ | |
466 | SKIP_CACHE(name, gb, num)\ | |
467 | SKIP_COUNTER(name, gb, num)\ | |
468 | }\ | |
469 | ||
470 | # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | |
471 | # define LAST_SKIP_CACHE(name, gb, num) ; | |
472 | ||
473 | # define SHOW_UBITS(name, gb, num)\ | |
ef5e74bb | 474 | NEG_USR32(name##_cache, num) |
8db1a1dd MN |
475 | |
476 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 477 | NEG_SSR32(name##_cache, num) |
8db1a1dd MN |
478 | |
479 | # define GET_CACHE(name, gb)\ | |
480 | ((uint32_t)name##_cache) | |
481 | ||
482 | static inline int get_bits_count(GetBitContext *s){ | |
483 | return s->index; | |
2931ecb9 | 484 | } |
8db1a1dd MN |
485 | #elif defined LIBMPEG2_BITSTREAM_READER |
486 | //libmpeg2 like reader | |
487 | ||
488 | # define MIN_CACHE_BITS 16 | |
489 | ||
490 | # define OPEN_READER(name, gb)\ | |
491 | int name##_bit_count=(gb)->bit_count;\ | |
492 | int name##_cache= (gb)->cache;\ | |
493 | uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | |
494 | ||
495 | # define CLOSE_READER(name, gb)\ | |
496 | (gb)->bit_count= name##_bit_count;\ | |
497 | (gb)->cache= name##_cache;\ | |
498 | (gb)->buffer_ptr= name##_buffer_ptr;\ | |
499 | ||
500 | # define UPDATE_CACHE(name, gb)\ | |
501 | if(name##_bit_count > 0){\ | |
502 | name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\ | |
503 | name##_buffer_ptr+=2;\ | |
504 | name##_bit_count-= 16;\ | |
505 | }\ | |
2931ecb9 | 506 | |
8db1a1dd MN |
507 | # define SKIP_CACHE(name, gb, num)\ |
508 | name##_cache <<= (num);\ | |
509 | ||
510 | # define SKIP_COUNTER(name, gb, num)\ | |
511 | name##_bit_count += (num);\ | |
512 | ||
513 | # define SKIP_BITS(name, gb, num)\ | |
514 | {\ | |
515 | SKIP_CACHE(name, gb, num)\ | |
516 | SKIP_COUNTER(name, gb, num)\ | |
517 | }\ | |
518 | ||
519 | # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | |
520 | # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | |
521 | ||
522 | # define SHOW_UBITS(name, gb, num)\ | |
ef5e74bb | 523 | NEG_USR32(name##_cache, num) |
8db1a1dd MN |
524 | |
525 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 526 | NEG_SSR32(name##_cache, num) |
8db1a1dd MN |
527 | |
528 | # define GET_CACHE(name, gb)\ | |
529 | ((uint32_t)name##_cache) | |
530 | ||
531 | static inline int get_bits_count(GetBitContext *s){ | |
532 | return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; | |
612476ef A |
533 | } |
534 | ||
8db1a1dd MN |
535 | #elif defined A32_BITSTREAM_READER |
536 | ||
537 | # define MIN_CACHE_BITS 32 | |
538 | ||
539 | # define OPEN_READER(name, gb)\ | |
540 | int name##_bit_count=(gb)->bit_count;\ | |
541 | uint32_t name##_cache0= (gb)->cache0;\ | |
542 | uint32_t name##_cache1= (gb)->cache1;\ | |
543 | uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | |
544 | ||
545 | # define CLOSE_READER(name, gb)\ | |
546 | (gb)->bit_count= name##_bit_count;\ | |
547 | (gb)->cache0= name##_cache0;\ | |
548 | (gb)->cache1= name##_cache1;\ | |
549 | (gb)->buffer_ptr= name##_buffer_ptr;\ | |
550 | ||
551 | # define UPDATE_CACHE(name, gb)\ | |
552 | if(name##_bit_count > 0){\ | |
553 | const uint32_t next= be2me_32( *name##_buffer_ptr );\ | |
ef5e74bb | 554 | name##_cache0 |= NEG_USR32(next,name##_bit_count);\ |
8db1a1dd MN |
555 | name##_cache1 |= next<<name##_bit_count;\ |
556 | name##_buffer_ptr++;\ | |
557 | name##_bit_count-= 32;\ | |
558 | }\ | |
559 | ||
3587d501 | 560 | #ifdef ARCH_X86 |
8db1a1dd MN |
561 | # define SKIP_CACHE(name, gb, num)\ |
562 | asm(\ | |
563 | "shldl %2, %1, %0 \n\t"\ | |
564 | "shll %2, %1 \n\t"\ | |
565 | : "+r" (name##_cache0), "+r" (name##_cache1)\ | |
566 | : "Ic" ((uint8_t)num)\ | |
567 | ); | |
3587d501 | 568 | #else |
8db1a1dd MN |
569 | # define SKIP_CACHE(name, gb, num)\ |
570 | name##_cache0 <<= (num);\ | |
ef5e74bb | 571 | name##_cache0 |= NEG_USR32(name##_cache1,num);\ |
8db1a1dd | 572 | name##_cache1 <<= (num); |
3587d501 | 573 | #endif |
27a3e2c5 | 574 | |
8db1a1dd MN |
575 | # define SKIP_COUNTER(name, gb, num)\ |
576 | name##_bit_count += (num);\ | |
577 | ||
578 | # define SKIP_BITS(name, gb, num)\ | |
579 | {\ | |
580 | SKIP_CACHE(name, gb, num)\ | |
581 | SKIP_COUNTER(name, gb, num)\ | |
582 | }\ | |
583 | ||
584 | # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | |
585 | # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | |
586 | ||
587 | # define SHOW_UBITS(name, gb, num)\ | |
ef5e74bb | 588 | NEG_USR32(name##_cache0, num) |
8db1a1dd MN |
589 | |
590 | # define SHOW_SBITS(name, gb, num)\ | |
ef5e74bb | 591 | NEG_SSR32(name##_cache0, num) |
8db1a1dd MN |
592 | |
593 | # define GET_CACHE(name, gb)\ | |
594 | (name##_cache0) | |
595 | ||
596 | static inline int get_bits_count(GetBitContext *s){ | |
597 | return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; | |
4949028f J |
598 | } |
599 | ||
49092244 | 600 | #endif |
49092244 | 601 | |
8db1a1dd MN |
602 | static inline unsigned int get_bits(GetBitContext *s, int n){ |
603 | register int tmp; | |
604 | OPEN_READER(re, s) | |
605 | UPDATE_CACHE(re, s) | |
606 | tmp= SHOW_UBITS(re, s, n); | |
607 | LAST_SKIP_BITS(re, s, n) | |
608 | CLOSE_READER(re, s) | |
609 | return tmp; | |
610 | } | |
49092244 | 611 | |
8db1a1dd MN |
612 | static inline unsigned int show_bits(GetBitContext *s, int n){ |
613 | register int tmp; | |
614 | OPEN_READER(re, s) | |
615 | UPDATE_CACHE(re, s) | |
616 | tmp= SHOW_UBITS(re, s, n); | |
617 | // CLOSE_READER(re, s) | |
618 | return tmp; | |
49092244 MN |
619 | } |
620 | ||
612476ef | 621 | static inline void skip_bits(GetBitContext *s, int n){ |
8db1a1dd MN |
622 | //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) |
623 | OPEN_READER(re, s) | |
624 | UPDATE_CACHE(re, s) | |
625 | LAST_SKIP_BITS(re, s, n) | |
626 | CLOSE_READER(re, s) | |
627 | } | |
628 | ||
629 | static inline unsigned int get_bits1(GetBitContext *s){ | |
27a3e2c5 | 630 | #ifdef ALT_BITSTREAM_READER |
8db1a1dd MN |
631 | int index= s->index; |
632 | uint8_t result= s->buffer[ index>>3 ]; | |
633 | result<<= (index&0x07); | |
634 | result>>= 8 - 1; | |
635 | index++; | |
636 | s->index= index; | |
fcb609f4 | 637 | |
8db1a1dd | 638 | return result; |
27a3e2c5 | 639 | #else |
8db1a1dd | 640 | return get_bits(s, 1); |
27a3e2c5 | 641 | #endif |
612476ef A |
642 | } |
643 | ||
8db1a1dd MN |
644 | static inline unsigned int show_bits1(GetBitContext *s){ |
645 | return show_bits(s, 1); | |
612476ef A |
646 | } |
647 | ||
8db1a1dd MN |
648 | static inline void skip_bits1(GetBitContext *s){ |
649 | skip_bits(s, 1); | |
9dbf9389 | 650 | } |
612476ef | 651 | |
8db1a1dd MN |
652 | void init_get_bits(GetBitContext *s, |
653 | UINT8 *buffer, int buffer_size); | |
654 | ||
18f77016 | 655 | int check_marker(GetBitContext *s, const char *msg); |
de6d9b64 FB |
656 | void align_get_bits(GetBitContext *s); |
657 | int init_vlc(VLC *vlc, int nb_bits, int nb_codes, | |
658 | const void *bits, int bits_wrap, int bits_size, | |
659 | const void *codes, int codes_wrap, int codes_size); | |
660 | void free_vlc(VLC *vlc); | |
de6d9b64 | 661 | |
cceabc86 MN |
662 | #define GET_VLC(code, name, gb, table, bits, max_depth)\ |
663 | {\ | |
664 | int n, index, nb_bits;\ | |
665 | \ | |
666 | index= SHOW_UBITS(name, gb, bits);\ | |
667 | code = table[index][0];\ | |
668 | n = table[index][1];\ | |
669 | \ | |
670 | if(max_depth > 1 && n < 0){\ | |
671 | LAST_SKIP_BITS(name, gb, bits)\ | |
672 | UPDATE_CACHE(name, gb)\ | |
673 | \ | |
674 | nb_bits = -n;\ | |
cceabc86 | 675 | \ |
6300c80a | 676 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ |
cceabc86 MN |
677 | code = table[index][0];\ |
678 | n = table[index][1];\ | |
679 | if(max_depth > 2 && n < 0){\ | |
680 | LAST_SKIP_BITS(name, gb, nb_bits)\ | |
681 | UPDATE_CACHE(name, gb)\ | |
682 | \ | |
683 | nb_bits = -n;\ | |
cceabc86 | 684 | \ |
6300c80a | 685 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ |
cceabc86 MN |
686 | code = table[index][0];\ |
687 | n = table[index][1];\ | |
688 | }\ | |
689 | }\ | |
690 | SKIP_BITS(name, gb, n)\ | |
691 | } | |
692 | ||
45a82edb MN |
693 | #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\ |
694 | {\ | |
695 | int n, index, nb_bits;\ | |
696 | \ | |
697 | index= SHOW_UBITS(name, gb, bits);\ | |
698 | level = table[index].level;\ | |
699 | n = table[index].len;\ | |
700 | \ | |
701 | if(max_depth > 1 && n < 0){\ | |
702 | LAST_SKIP_BITS(name, gb, bits)\ | |
703 | UPDATE_CACHE(name, gb)\ | |
704 | \ | |
705 | nb_bits = -n;\ | |
706 | \ | |
707 | index= SHOW_UBITS(name, gb, nb_bits) + level;\ | |
708 | level = table[index].level;\ | |
709 | n = table[index].len;\ | |
710 | }\ | |
711 | run= table[index].run;\ | |
712 | SKIP_BITS(name, gb, n)\ | |
713 | } | |
714 | ||
08dce7b0 | 715 | // deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly |
3587d501 MN |
716 | static inline int get_vlc(GetBitContext *s, VLC *vlc) |
717 | { | |
cceabc86 MN |
718 | int code; |
719 | VLC_TYPE (*table)[2]= vlc->table; | |
720 | ||
8db1a1dd | 721 | OPEN_READER(re, s) |
8db1a1dd | 722 | UPDATE_CACHE(re, s) |
3587d501 | 723 | |
cceabc86 | 724 | GET_VLC(code, re, s, table, vlc->bits, 3) |
8db1a1dd | 725 | |
8db1a1dd | 726 | CLOSE_READER(re, s) |
3587d501 | 727 | return code; |
08dce7b0 MN |
728 | } |
729 | ||
d200cab6 FH |
730 | static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], |
731 | int bits, int max_depth) | |
08dce7b0 MN |
732 | { |
733 | int code; | |
734 | ||
735 | OPEN_READER(re, s) | |
736 | UPDATE_CACHE(re, s) | |
737 | ||
738 | GET_VLC(code, re, s, table, bits, max_depth) | |
739 | ||
740 | CLOSE_READER(re, s) | |
741 | return code; | |
3587d501 MN |
742 | } |
743 | ||
45a82edb | 744 | |
de6d9b64 FB |
745 | /* define it to include statistics code (useful only for optimizing |
746 | codec efficiency */ | |
747 | //#define STATS | |
748 | ||
749 | #ifdef STATS | |
750 | ||
751 | enum { | |
752 | ST_UNKNOWN, | |
753 | ST_DC, | |
754 | ST_INTRA_AC, | |
755 | ST_INTER_AC, | |
756 | ST_INTRA_MB, | |
757 | ST_INTER_MB, | |
758 | ST_MV, | |
759 | ST_NB, | |
760 | }; | |
761 | ||
762 | extern int st_current_index; | |
763 | extern unsigned int st_bit_counts[ST_NB]; | |
764 | extern unsigned int st_out_bit_counts[ST_NB]; | |
765 | ||
766 | void print_stats(void); | |
767 | #endif | |
768 | ||
769 | /* misc math functions */ | |
770 | ||
a822a479 | 771 | static inline int av_log2(unsigned int v) |
de6d9b64 FB |
772 | { |
773 | int n; | |
774 | ||
775 | n = 0; | |
776 | if (v & 0xffff0000) { | |
777 | v >>= 16; | |
778 | n += 16; | |
779 | } | |
780 | if (v & 0xff00) { | |
781 | v >>= 8; | |
782 | n += 8; | |
783 | } | |
784 | if (v & 0xf0) { | |
785 | v >>= 4; | |
786 | n += 4; | |
787 | } | |
788 | if (v & 0xc) { | |
789 | v >>= 2; | |
790 | n += 2; | |
791 | } | |
792 | if (v & 0x2) { | |
793 | n++; | |
794 | } | |
795 | return n; | |
796 | } | |
797 | ||
45870f57 MN |
798 | /* median of 3 */ |
799 | static inline int mid_pred(int a, int b, int c) | |
800 | { | |
801 | int vmin, vmax; | |
802 | vmax = vmin = a; | |
803 | if (b < vmin) | |
804 | vmin = b; | |
805 | else | |
806 | vmax = b; | |
807 | ||
808 | if (c < vmin) | |
809 | vmin = c; | |
810 | else if (c > vmax) | |
811 | vmax = c; | |
812 | ||
813 | return a + b + c - vmin - vmax; | |
814 | } | |
815 | ||
91029be7 MN |
816 | static inline int clip(int a, int amin, int amax) |
817 | { | |
818 | if (a < amin) | |
819 | return amin; | |
820 | else if (a > amax) | |
821 | return amax; | |
822 | else | |
823 | return a; | |
824 | } | |
825 | ||
9dbcbd92 | 826 | /* math */ |
f36db5df MN |
827 | extern const UINT8 ff_sqrt_tab[128]; |
828 | ||
9dbcbd92 MN |
829 | int ff_gcd(int a, int b); |
830 | ||
7fd08ac1 MN |
831 | static inline int ff_sqrt(int a) |
832 | { | |
833 | int ret=0; | |
834 | int s; | |
835 | int ret_sq=0; | |
f36db5df MN |
836 | |
837 | if(a<128) return ff_sqrt_tab[a]; | |
838 | ||
7fd08ac1 MN |
839 | for(s=15; s>=0; s--){ |
840 | int b= ret_sq + (1<<(s*2)) + (ret<<s)*2; | |
841 | if(b<=a){ | |
842 | ret_sq=b; | |
843 | ret+= 1<<s; | |
844 | } | |
845 | } | |
846 | return ret; | |
847 | } | |
202ef8b8 MN |
848 | |
849 | /** | |
850 | * converts fourcc string to int | |
851 | */ | |
6a85ec8d | 852 | static inline int ff_get_fourcc(const char *s){ |
202ef8b8 MN |
853 | assert( strlen(s)==4 ); |
854 | ||
855 | return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24); | |
856 | } | |
857 | ||
5d3cea3a MN |
858 | void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max); |
859 | ||
2ad1516a MN |
860 | |
861 | #ifdef ARCH_X86 | |
862 | #define MASK_ABS(mask, level)\ | |
863 | asm volatile(\ | |
864 | "cdq \n\t"\ | |
865 | "xorl %1, %0 \n\t"\ | |
866 | "subl %1, %0 \n\t"\ | |
867 | : "+a" (level), "=&d" (mask)\ | |
868 | ); | |
869 | #else | |
870 | #define MASK_ABS(mask, level)\ | |
871 | mask= level>>31;\ | |
872 | level= (level^mask)-mask; | |
873 | #endif | |
874 | ||
875 | ||
7fd08ac1 MN |
876 | #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT) |
877 | #define COPY3_IF_LT(x,y,a,b,c,d)\ | |
878 | asm volatile (\ | |
879 | "cmpl %0, %3 \n\t"\ | |
880 | "cmovl %3, %0 \n\t"\ | |
881 | "cmovl %4, %1 \n\t"\ | |
882 | "cmovl %5, %2 \n\t"\ | |
883 | : "+r" (x), "+r" (a), "+r" (c)\ | |
884 | : "r" (y), "r" (b), "r" (d)\ | |
885 | ); | |
886 | #else | |
887 | #define COPY3_IF_LT(x,y,a,b,c,d)\ | |
888 | if((y)<(x)){\ | |
889 | (x)=(y);\ | |
890 | (a)=(b);\ | |
891 | (c)=(d);\ | |
892 | } | |
893 | #endif | |
894 | ||
a69b930c AB |
895 | #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d) |
896 | ||
96707bb7 FB |
897 | #endif /* HAVE_AV_CONFIG_H */ |
898 | ||
899 | #endif /* COMMON_H */ |