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