Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ f3ec2d46

History | View | Annotate | Download (21.2 KB)

1 de6d9b64 Fabrice Bellard
#ifndef COMMON_H
2
#define COMMON_H
3
4 73120e55 Fabrice Bellard
#define FFMPEG_VERSION_INT 0x000406
5
#define FFMPEG_VERSION     "0.4.6"
6 1a565432 Fabrice Bellard
7 4bdd9157 Nick Kurshev
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
8 9b59c92f Michael Niedermayer
#    define CONFIG_WIN32
9 1a565432 Fabrice Bellard
#endif
10
11 17592475 Michael Niedermayer
//#define ALT_BITSTREAM_WRITER
12 4f0f8bfc Michael Niedermayer
//#define ALIGNED_BITSTREAM_WRITER
13 3530320d Falk Hüffner
14 8db1a1dd Michael Niedermayer
#define ALT_BITSTREAM_READER
15
//#define LIBMPEG2_BITSTREAM_READER
16
//#define A32_BITSTREAM_READER
17
18 420b073b Fabrice Bellard
#ifdef HAVE_AV_CONFIG_H
19 1a565432 Fabrice Bellard
/* only include the following when compiling package */
20 9b59c92f Michael Niedermayer
#    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
35 1a565432 Fabrice Bellard
36 c4d82fea Fabrice Bellard
#ifndef M_PI
37
#define M_PI    3.14159265358979323846
38
#endif
39
40 44f27b3a Fabrice Bellard
#endif /* HAVE_AV_CONFIG_H */
41 1a565432 Fabrice Bellard
42 02da51ec Falk Hüffner
/* Suppress restrict if it was not defined in config.h.  */
43
#ifndef restrict
44 9b59c92f Michael Niedermayer
#    define restrict
45 02da51ec Falk Hüffner
#endif
46
47 d200cab6 Falk Hüffner
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
48 9b59c92f Michael Niedermayer
#    define always_inline __attribute__((always_inline)) inline
49 d200cab6 Falk Hüffner
#else
50 9b59c92f Michael Niedermayer
#    define always_inline inline
51 d200cab6 Falk Hüffner
#endif
52
53 1a565432 Fabrice Bellard
#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;
72 d503f970 Fabrice Bellard
typedef UINT64 uint64_t;
73
typedef INT64 int64_t;
74 1a565432 Fabrice Bellard
75 9b59c92f Michael Niedermayer
#    ifndef __MINGW32__
76
#        define INT64_C(c)     (c ## i64)
77
#        define UINT64_C(c)    (c ## i64)
78 1a565432 Fabrice Bellard
79 9b59c92f Michael Niedermayer
#        define inline __inline
80 1a565432 Fabrice Bellard
81 9b59c92f Michael Niedermayer
#    else
82
#        define INT64_C(c)     (c ## LL)
83
#        define UINT64_C(c)    (c ## ULL)
84
#    endif /* __MINGW32__ */
85 a74127c0 Fabrice Bellard
86 9b59c92f Michael Niedermayer
#    ifdef _DEBUG
87
#        define DEBUG
88
#    endif
89 de6d9b64 Fabrice Bellard
90 9b59c92f Michael Niedermayer
#    define snprintf _snprintf
91 1a565432 Fabrice Bellard
92 f3ec2d46 Slavik Gnatenko
/* CONFIG_WIN32 end */
93
#elif defined (CONFIG_OS2)
94
/* OS/2 EMX */
95
96
#include <inttypes.h>
97
98
typedef unsigned char UINT8;
99
typedef unsigned short UINT16;
100
typedef unsigned int UINT32;
101
typedef unsigned long long UINT64;
102
typedef signed char INT8;
103
typedef signed short INT16;
104
typedef signed int INT32;
105
typedef signed long long INT64;
106
107
#ifdef HAVE_AV_CONFIG_H
108
109
#ifndef INT64_C
110
#define INT64_C(c)     (c ## LL)
111
#define UINT64_C(c)    (c ## ULL)
112
#endif
113
114
#ifdef USE_FASTMEMCPY
115
#include "fastmemcpy.h"
116
#endif
117
118
#include <float.h>
119
120
#endif /* HAVE_AV_CONFIG_H */
121
122
/* CONFIG_OS2 end */
123
#else
124 1a565432 Fabrice Bellard
125
/* unix */
126
127 9b59c92f Michael Niedermayer
#    include <inttypes.h>
128 1a565432 Fabrice Bellard
129 9b59c92f Michael Niedermayer
#    ifndef __WINE_WINDEF16_H
130 980fc7b8 Fabrice Bellard
/* workaround for typedef conflict in MPlayer (wine typedefs) */
131 de6d9b64 Fabrice Bellard
typedef unsigned short UINT16;
132
typedef signed short INT16;
133 9b59c92f Michael Niedermayer
#    endif
134 de6d9b64 Fabrice Bellard
135
typedef unsigned char UINT8;
136
typedef unsigned int UINT32;
137
typedef unsigned long long UINT64;
138
typedef signed char INT8;
139
typedef signed int INT32;
140
typedef signed long long INT64;
141
142 9b59c92f Michael Niedermayer
#    ifdef HAVE_AV_CONFIG_H
143
#        ifndef INT64_C
144
#            define INT64_C(c)     (c ## LL)
145
#            define UINT64_C(c)    (c ## ULL)
146
#        endif
147 1a565432 Fabrice Bellard
148 9b59c92f Michael Niedermayer
#        ifdef USE_FASTMEMCPY
149
#            include "fastmemcpy.h"
150
#        endif
151
#    endif /* HAVE_AV_CONFIG_H */
152 a74127c0 Fabrice Bellard
153 f3ec2d46 Slavik Gnatenko
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
154 a74127c0 Fabrice Bellard
155
#ifdef HAVE_AV_CONFIG_H
156
157 9b59c92f Michael Niedermayer
#    include "bswap.h"
158 96707bb7 Fabrice Bellard
159 9b59c92f Michael Niedermayer
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
160
        defined(__OS2__) || defined (__OpenBSD__)
161
#        define MANGLE(a) "_" #a
162
#    else
163
#        define MANGLE(a) #a
164
#    endif
165 6a3d7e36 Fabrice Bellard
166 d503f970 Fabrice Bellard
/* debug stuff */
167
168 9b59c92f Michael Niedermayer
#    ifndef DEBUG
169
#        define NDEBUG
170
#    endif
171
#    include <assert.h>
172 1a565432 Fabrice Bellard
173 a74127c0 Fabrice Bellard
/* dprintf macros */
174 9b59c92f Michael Niedermayer
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
175 a74127c0 Fabrice Bellard
176
inline void dprintf(const char* fmt,...) {}
177
178 9b59c92f Michael Niedermayer
#    else
179 a74127c0 Fabrice Bellard
180 9b59c92f Michael Niedermayer
#        ifdef DEBUG
181
#            define dprintf(fmt,args...) printf(fmt, ## args)
182
#        else
183
#            define dprintf(fmt,args...)
184
#        endif
185 1a565432 Fabrice Bellard
186 9b59c92f Michael Niedermayer
#    endif /* !CONFIG_WIN32 */
187 1a565432 Fabrice Bellard
188 9b59c92f Michael Niedermayer
#    define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
189 935cdf09 Philip Gladstone
190 073b013d Michael Niedermayer
//rounded divison & shift
191
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
192 d7e9533a Michael Niedermayer
/* assume b>0 */
193
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
194 32cd20de Michael Niedermayer
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
195 75460b0c Michael Niedermayer
196 b8a78f41 Michael Niedermayer
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
197
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
198 d7e9533a Michael Niedermayer
199 ef5e74bb Michael Niedermayer
#ifdef ARCH_X86
200
// avoid +32 for shift optimization (gcc should do that ...)
201
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
202
    asm ("sarl %1, %0\n\t"
203
         : "+r" (a)
204
         : "ic" ((uint8_t)(-s))
205
    );
206
    return a;
207
}
208
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
209
    asm ("shrl %1, %0\n\t"
210
         : "+r" (a)
211
         : "ic" ((uint8_t)(-s))
212
    );
213
    return a;
214
}
215 8db1a1dd Michael Niedermayer
#else
216 9b59c92f Michael Niedermayer
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
217
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
218 8db1a1dd Michael Niedermayer
#endif
219
220 de6d9b64 Fabrice Bellard
/* bit output */
221
222
struct PutBitContext;
223
224
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
225
226
typedef struct PutBitContext {
227 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
228
    UINT8 *buf, *buf_end;
229
    int index;
230
#else
231 de6d9b64 Fabrice Bellard
    UINT32 bit_buf;
232 d8cf5aea Michael Niedermayer
    int bit_left;
233 2931ecb9 Arpi
    UINT8 *buf, *buf_ptr, *buf_end;
234 17592475 Michael Niedermayer
#endif
235
    INT64 data_out_size; /* in bytes */
236 de6d9b64 Fabrice Bellard
} PutBitContext;
237
238
void init_put_bits(PutBitContext *s, 
239
                   UINT8 *buffer, int buffer_size,
240
                   void *opaque,
241
                   void (*write_data)(void *, UINT8 *, int));
242 17592475 Michael Niedermayer
243 9dbf9389 Fabrice Bellard
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
244 de6d9b64 Fabrice Bellard
void align_put_bits(PutBitContext *s);
245
void flush_put_bits(PutBitContext *s);
246 9717dad8 Michael Niedermayer
void put_string(PutBitContext * pbc, char *s);
247 de6d9b64 Fabrice Bellard
248
/* bit input */
249
250
typedef struct GetBitContext {
251 8db1a1dd Michael Niedermayer
    UINT8 *buffer, *buffer_end;
252 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
253
    int index;
254 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
255
    UINT8 *buffer_ptr;
256
    UINT32 cache;
257
    int bit_count;
258
#elif defined A32_BITSTREAM_READER
259
    UINT32 *buffer_ptr;
260
    UINT32 cache0;
261
    UINT32 cache1;
262
    int bit_count;
263 27a3e2c5 Michael Niedermayer
#endif
264 45870f57 Michael Niedermayer
    int size;
265 de6d9b64 Fabrice Bellard
} GetBitContext;
266
267 49092244 Michael Niedermayer
static inline int get_bits_count(GetBitContext *s);
268
269 8db1a1dd Michael Niedermayer
#define VLC_TYPE INT16
270
271 de6d9b64 Fabrice Bellard
typedef struct VLC {
272
    int bits;
273 8db1a1dd Michael Niedermayer
    VLC_TYPE (*table)[2]; // code, bits
274 de6d9b64 Fabrice Bellard
    int table_size, table_allocated;
275
} VLC;
276
277 45a82edb Michael Niedermayer
typedef struct RL_VLC_ELEM {
278
    int16_t level;
279
    int8_t len;
280
    uint8_t run;
281
} RL_VLC_ELEM;
282
283 35fedfc3 Philip Gladstone
#ifdef ARCH_SPARC64
284
#define UNALIGNED_STORES_ARE_BAD
285
#endif
286
287 3587d501 Michael Niedermayer
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
288
#ifdef ARCH_X86
289 9b59c92f Michael Niedermayer
#    define unaligned32(a) (*(UINT32*)(a))
290 3587d501 Michael Niedermayer
#else
291 9b59c92f Michael Niedermayer
#    ifdef __GNUC__
292 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
293
    struct Unaligned {
294
        uint32_t i;
295
    } __attribute__((packed));
296
297
    return ((const struct Unaligned *) v)->i;
298
}
299 9b59c92f Michael Niedermayer
#    elif defined(__DECC)
300 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
301
    return *(const __unaligned uint32_t *) v;
302
}
303 9b59c92f Michael Niedermayer
#    else
304 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
305
    return *(const uint32_t *) v;
306
}
307 9b59c92f Michael Niedermayer
#    endif
308 3587d501 Michael Niedermayer
#endif //!ARCH_X86
309
310 d8cf5aea Michael Niedermayer
#ifndef ALT_BITSTREAM_WRITER
311
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
312
{
313
    unsigned int bit_buf;
314
    int bit_left;
315
316
#ifdef STATS
317
    st_out_bit_counts[st_current_index] += n;
318
#endif
319
    //    printf("put_bits=%d %x\n", n, value);
320
    assert(n == 32 || value < (1U << n));
321 fcb609f4 Michael Niedermayer
    
322 d8cf5aea Michael Niedermayer
    bit_buf = s->bit_buf;
323
    bit_left = s->bit_left;
324
325
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
326
    /* XXX: optimize */
327
    if (n < bit_left) {
328
        bit_buf = (bit_buf<<n) | value;
329
        bit_left-=n;
330
    } else {
331
        bit_buf<<=bit_left;
332
        bit_buf |= value >> (n - bit_left);
333 35fedfc3 Philip Gladstone
#ifdef UNALIGNED_STORES_ARE_BAD
334
        if (3 & (int) s->buf_ptr) {
335
            s->buf_ptr[0] = bit_buf >> 24;
336
            s->buf_ptr[1] = bit_buf >> 16;
337
            s->buf_ptr[2] = bit_buf >>  8;
338
            s->buf_ptr[3] = bit_buf      ;
339
        } else
340
#endif
341 d8cf5aea Michael Niedermayer
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
342
        //printf("bitbuf = %08x\n", bit_buf);
343
        s->buf_ptr+=4;
344
        bit_left+=32 - n;
345
        bit_buf = value;
346
    }
347
348
    s->bit_buf = bit_buf;
349
    s->bit_left = bit_left;
350
}
351
#endif
352
353
354 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
355 4f0f8bfc Michael Niedermayer
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
356 17592475 Michael Niedermayer
{
357 9b59c92f Michael Niedermayer
#    ifdef ALIGNED_BITSTREAM_WRITER
358
#        ifdef ARCH_X86
359 4f0f8bfc Michael Niedermayer
    asm volatile(
360
        "movl %0, %%ecx                        \n\t"
361
        "xorl %%eax, %%eax                \n\t"
362
        "shrdl %%cl, %1, %%eax                \n\t"
363
        "shrl %%cl, %1                        \n\t"
364
        "movl %0, %%ecx                        \n\t"
365
        "shrl $3, %%ecx                        \n\t"
366
        "andl $0xFFFFFFFC, %%ecx        \n\t"
367
        "bswapl %1                        \n\t"
368
        "orl %1, (%2, %%ecx)                \n\t"
369
        "bswapl %%eax                        \n\t"
370
        "addl %3, %0                        \n\t"
371
        "movl %%eax, 4(%2, %%ecx)        \n\t"
372
        : "=&r" (s->index), "=&r" (value)
373
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
374
        : "%eax", "%ecx"
375
    );
376 9b59c92f Michael Niedermayer
#        else
377 4f0f8bfc Michael Niedermayer
    int index= s->index;
378
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
379
    
380
    value<<= 32-n; 
381
    
382
    ptr[0] |= be2me_32(value>>(index&31));
383
    ptr[1]  = be2me_32(value<<(32-(index&31)));
384
//if(n>24) printf("%d %d\n", n, value);
385
    index+= n;
386
    s->index= index;
387 9b59c92f Michael Niedermayer
#        endif
388
#    else //ALIGNED_BITSTREAM_WRITER
389
#        ifdef ARCH_X86
390 17592475 Michael Niedermayer
    asm volatile(
391
        "movl $7, %%ecx                        \n\t"
392
        "andl %0, %%ecx                        \n\t"
393
        "addl %3, %%ecx                        \n\t"
394
        "negl %%ecx                        \n\t"
395
        "shll %%cl, %1                        \n\t"
396
        "bswapl %1                        \n\t"
397
        "movl %0, %%ecx                        \n\t"
398
        "shrl $3, %%ecx                        \n\t"
399
        "orl %1, (%%ecx, %2)                \n\t"
400
        "addl %3, %0                        \n\t"
401
        "movl $0, 4(%%ecx, %2)                \n\t"
402
        : "=&r" (s->index), "=&r" (value)
403
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
404
        : "%ecx"
405
    );
406 9b59c92f Michael Niedermayer
#        else
407 17592475 Michael Niedermayer
    int index= s->index;
408
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
409
    
410
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
411
    ptr[1] = 0;
412
//if(n>24) printf("%d %d\n", n, value);
413
    index+= n;
414
    s->index= index;
415 9b59c92f Michael Niedermayer
#        endif
416
#    endif //!ALIGNED_BITSTREAM_WRITER
417 17592475 Michael Niedermayer
}
418
#endif
419
420
421
static inline uint8_t* pbBufPtr(PutBitContext *s)
422
{
423
#ifdef ALT_BITSTREAM_WRITER
424
        return s->buf + (s->index>>3);
425
#else
426
        return s->buf_ptr;
427
#endif
428
}
429
430 8db1a1dd Michael Niedermayer
/* Bitstream reader API docs:
431
name
432
    abritary name which is used as prefix for the internal variables
433 de6d9b64 Fabrice Bellard

434 8db1a1dd Michael Niedermayer
gb
435
    getbitcontext
436

437
OPEN_READER(name, gb)
438
    loads gb into local variables
439

440
CLOSE_READER(name, gb)
441
    stores local vars in gb
442

443
UPDATE_CACHE(name, gb)
444
    refills the internal cache from the bitstream
445
    after this call at least MIN_CACHE_BITS will be available,
446

447
GET_CACHE(name, gb)
448
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
449

450
SHOW_UBITS(name, gb, num)
451
    will return the nest num bits
452

453
SHOW_SBITS(name, gb, num)
454
    will return the nest num bits and do sign extension
455

456
SKIP_BITS(name, gb, num)
457
    will skip over the next num bits
458
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
459

460
SKIP_CACHE(name, gb, num)
461
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
462

463
SKIP_COUNTER(name, gb, num)
464
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
465

466
LAST_SKIP_CACHE(name, gb, num)
467
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
468

469
LAST_SKIP_BITS(name, gb, num)
470
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
471

472
for examples see get_bits, show_bits, skip_bits, get_vlc
473
*/
474 2931ecb9 Arpi
475 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
476 8db1a1dd Michael Niedermayer
#   define MIN_CACHE_BITS 25
477 27a3e2c5 Michael Niedermayer
478 8db1a1dd Michael Niedermayer
#   define OPEN_READER(name, gb)\
479
        int name##_index= (gb)->index;\
480
        int name##_cache= 0;\
481
482
#   define CLOSE_READER(name, gb)\
483
        (gb)->index= name##_index;\
484
485
#   define UPDATE_CACHE(name, gb)\
486
        name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
487
488
#   define SKIP_CACHE(name, gb, num)\
489
        name##_cache <<= (num);\
490
491
// FIXME name?
492
#   define SKIP_COUNTER(name, gb, num)\
493
        name##_index += (num);\
494
495
#   define SKIP_BITS(name, gb, num)\
496
        {\
497
            SKIP_CACHE(name, gb, num)\
498
            SKIP_COUNTER(name, gb, num)\
499
        }\
500
501
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
502
#   define LAST_SKIP_CACHE(name, gb, num) ;
503
504
#   define SHOW_UBITS(name, gb, num)\
505 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
506 8db1a1dd Michael Niedermayer
507
#   define SHOW_SBITS(name, gb, num)\
508 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
509 8db1a1dd Michael Niedermayer
510
#   define GET_CACHE(name, gb)\
511
        ((uint32_t)name##_cache)
512
513
static inline int get_bits_count(GetBitContext *s){
514
    return s->index;
515 2931ecb9 Arpi
}
516 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
517
//libmpeg2 like reader
518
519
#   define MIN_CACHE_BITS 16
520
521
#   define OPEN_READER(name, gb)\
522
        int name##_bit_count=(gb)->bit_count;\
523
        int name##_cache= (gb)->cache;\
524
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
525
526
#   define CLOSE_READER(name, gb)\
527
        (gb)->bit_count= name##_bit_count;\
528
        (gb)->cache= name##_cache;\
529
        (gb)->buffer_ptr= name##_buffer_ptr;\
530
531
#   define UPDATE_CACHE(name, gb)\
532
    if(name##_bit_count > 0){\
533
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
534
        name##_buffer_ptr+=2;\
535
        name##_bit_count-= 16;\
536
    }\
537 2931ecb9 Arpi
538 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
539
        name##_cache <<= (num);\
540
541
#   define SKIP_COUNTER(name, gb, num)\
542
        name##_bit_count += (num);\
543
544
#   define SKIP_BITS(name, gb, num)\
545
        {\
546
            SKIP_CACHE(name, gb, num)\
547
            SKIP_COUNTER(name, gb, num)\
548
        }\
549
550
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
551
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
552
553
#   define SHOW_UBITS(name, gb, num)\
554 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
555 8db1a1dd Michael Niedermayer
556
#   define SHOW_SBITS(name, gb, num)\
557 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
558 8db1a1dd Michael Niedermayer
559
#   define GET_CACHE(name, gb)\
560
        ((uint32_t)name##_cache)
561
562
static inline int get_bits_count(GetBitContext *s){
563
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
564 612476ef Arpi
}
565
566 8db1a1dd Michael Niedermayer
#elif defined A32_BITSTREAM_READER
567
568
#   define MIN_CACHE_BITS 32
569
570
#   define OPEN_READER(name, gb)\
571
        int name##_bit_count=(gb)->bit_count;\
572
        uint32_t name##_cache0= (gb)->cache0;\
573
        uint32_t name##_cache1= (gb)->cache1;\
574
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
575
576
#   define CLOSE_READER(name, gb)\
577
        (gb)->bit_count= name##_bit_count;\
578
        (gb)->cache0= name##_cache0;\
579
        (gb)->cache1= name##_cache1;\
580
        (gb)->buffer_ptr= name##_buffer_ptr;\
581
582
#   define UPDATE_CACHE(name, gb)\
583
    if(name##_bit_count > 0){\
584
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
585 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
586 8db1a1dd Michael Niedermayer
        name##_cache1 |= next<<name##_bit_count;\
587
        name##_buffer_ptr++;\
588
        name##_bit_count-= 32;\
589
    }\
590
591 3587d501 Michael Niedermayer
#ifdef ARCH_X86
592 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
593
        asm(\
594
            "shldl %2, %1, %0                \n\t"\
595
            "shll %2, %1                \n\t"\
596
            : "+r" (name##_cache0), "+r" (name##_cache1)\
597
            : "Ic" ((uint8_t)num)\
598
           );
599 3587d501 Michael Niedermayer
#else
600 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
601
        name##_cache0 <<= (num);\
602 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
603 8db1a1dd Michael Niedermayer
        name##_cache1 <<= (num);
604 3587d501 Michael Niedermayer
#endif
605 27a3e2c5 Michael Niedermayer
606 8db1a1dd Michael Niedermayer
#   define SKIP_COUNTER(name, gb, num)\
607
        name##_bit_count += (num);\
608
609
#   define SKIP_BITS(name, gb, num)\
610
        {\
611
            SKIP_CACHE(name, gb, num)\
612
            SKIP_COUNTER(name, gb, num)\
613
        }\
614
615
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
616
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
617
618
#   define SHOW_UBITS(name, gb, num)\
619 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache0, num)
620 8db1a1dd Michael Niedermayer
621
#   define SHOW_SBITS(name, gb, num)\
622 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache0, num)
623 8db1a1dd Michael Niedermayer
624
#   define GET_CACHE(name, gb)\
625
        (name##_cache0)
626
627
static inline int get_bits_count(GetBitContext *s){
628
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
629 4949028f Juanjo
}
630
631 49092244 Michael Niedermayer
#endif
632
633 8db1a1dd Michael Niedermayer
static inline unsigned int get_bits(GetBitContext *s, int n){
634
    register int tmp;
635
    OPEN_READER(re, s)
636
    UPDATE_CACHE(re, s)
637
    tmp= SHOW_UBITS(re, s, n);
638
    LAST_SKIP_BITS(re, s, n)
639
    CLOSE_READER(re, s)
640
    return tmp;
641
}
642 49092244 Michael Niedermayer
643 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits(GetBitContext *s, int n){
644
    register int tmp;
645
    OPEN_READER(re, s)
646
    UPDATE_CACHE(re, s)
647
    tmp= SHOW_UBITS(re, s, n);
648
//    CLOSE_READER(re, s)
649
    return tmp;
650 49092244 Michael Niedermayer
}
651
652 612476ef Arpi
static inline void skip_bits(GetBitContext *s, int n){
653 8db1a1dd Michael Niedermayer
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
654
    OPEN_READER(re, s)
655
    UPDATE_CACHE(re, s)
656
    LAST_SKIP_BITS(re, s, n)
657
    CLOSE_READER(re, s)
658
}
659
660
static inline unsigned int get_bits1(GetBitContext *s){
661 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
662 8db1a1dd Michael Niedermayer
    int index= s->index;
663
    uint8_t result= s->buffer[ index>>3 ];
664
    result<<= (index&0x07);
665
    result>>= 8 - 1;
666
    index++;
667
    s->index= index;
668 fcb609f4 Michael Niedermayer
669 8db1a1dd Michael Niedermayer
    return result;
670 27a3e2c5 Michael Niedermayer
#else
671 8db1a1dd Michael Niedermayer
    return get_bits(s, 1);
672 27a3e2c5 Michael Niedermayer
#endif
673 612476ef Arpi
}
674
675 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits1(GetBitContext *s){
676
    return show_bits(s, 1);
677 612476ef Arpi
}
678
679 8db1a1dd Michael Niedermayer
static inline void skip_bits1(GetBitContext *s){
680
    skip_bits(s, 1);
681 9dbf9389 Fabrice Bellard
}
682 612476ef Arpi
683 8db1a1dd Michael Niedermayer
void init_get_bits(GetBitContext *s,
684
                   UINT8 *buffer, int buffer_size);
685
686 18f77016 Zdenek Kabelac
int check_marker(GetBitContext *s, const char *msg);
687 de6d9b64 Fabrice Bellard
void align_get_bits(GetBitContext *s);
688
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
689
             const void *bits, int bits_wrap, int bits_size,
690
             const void *codes, int codes_wrap, int codes_size);
691
void free_vlc(VLC *vlc);
692
693 cceabc86 Michael Niedermayer
#define GET_VLC(code, name, gb, table, bits, max_depth)\
694
{\
695
    int n, index, nb_bits;\
696
\
697
    index= SHOW_UBITS(name, gb, bits);\
698
    code = table[index][0];\
699
    n    = table[index][1];\
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 6300c80a Michael Niedermayer
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
708 cceabc86 Michael Niedermayer
        code = table[index][0];\
709
        n    = table[index][1];\
710
        if(max_depth > 2 && n < 0){\
711
            LAST_SKIP_BITS(name, gb, nb_bits)\
712
            UPDATE_CACHE(name, gb)\
713
\
714
            nb_bits = -n;\
715
\
716 6300c80a Michael Niedermayer
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
717 cceabc86 Michael Niedermayer
            code = table[index][0];\
718
            n    = table[index][1];\
719
        }\
720
    }\
721
    SKIP_BITS(name, gb, n)\
722
}
723
724 45a82edb Michael Niedermayer
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
725
{\
726
    int n, index, nb_bits;\
727
\
728
    index= SHOW_UBITS(name, gb, bits);\
729
    level = table[index].level;\
730
    n     = table[index].len;\
731
\
732
    if(max_depth > 1 && n < 0){\
733
        LAST_SKIP_BITS(name, gb, bits)\
734
        UPDATE_CACHE(name, gb)\
735
\
736
        nb_bits = -n;\
737
\
738
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
739
        level = table[index].level;\
740
        n     = table[index].len;\
741
    }\
742
    run= table[index].run;\
743
    SKIP_BITS(name, gb, n)\
744
}
745
746 08dce7b0 Michael Niedermayer
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
747 3587d501 Michael Niedermayer
static inline int get_vlc(GetBitContext *s, VLC *vlc)
748
{
749 cceabc86 Michael Niedermayer
    int code;
750
    VLC_TYPE (*table)[2]= vlc->table;
751
    
752 8db1a1dd Michael Niedermayer
    OPEN_READER(re, s)
753
    UPDATE_CACHE(re, s)
754 3587d501 Michael Niedermayer
755 cceabc86 Michael Niedermayer
    GET_VLC(code, re, s, table, vlc->bits, 3)    
756 8db1a1dd Michael Niedermayer
757
    CLOSE_READER(re, s)
758 3587d501 Michael Niedermayer
    return code;
759 08dce7b0 Michael Niedermayer
}
760
761 d200cab6 Falk Hüffner
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
762
                                  int bits, int max_depth)
763 08dce7b0 Michael Niedermayer
{
764
    int code;
765
    
766
    OPEN_READER(re, s)
767
    UPDATE_CACHE(re, s)
768
769
    GET_VLC(code, re, s, table, bits, max_depth)
770
771
    CLOSE_READER(re, s)
772
    return code;
773 3587d501 Michael Niedermayer
}
774
775 45a82edb Michael Niedermayer
776 de6d9b64 Fabrice Bellard
/* define it to include statistics code (useful only for optimizing
777
   codec efficiency */
778
//#define STATS
779
780
#ifdef STATS
781
782
enum {
783
    ST_UNKNOWN,
784
    ST_DC,
785
    ST_INTRA_AC,
786
    ST_INTER_AC,
787
    ST_INTRA_MB,
788
    ST_INTER_MB,
789
    ST_MV,
790
    ST_NB,
791
};
792
793
extern int st_current_index;
794
extern unsigned int st_bit_counts[ST_NB];
795
extern unsigned int st_out_bit_counts[ST_NB];
796
797
void print_stats(void);
798
#endif
799
800
/* misc math functions */
801
802 a822a479 Nick Kurshev
static inline int av_log2(unsigned int v)
803 de6d9b64 Fabrice Bellard
{
804
    int n;
805
806
    n = 0;
807
    if (v & 0xffff0000) {
808
        v >>= 16;
809
        n += 16;
810
    }
811
    if (v & 0xff00) {
812
        v >>= 8;
813
        n += 8;
814
    }
815
    if (v & 0xf0) {
816
        v >>= 4;
817
        n += 4;
818
    }
819
    if (v & 0xc) {
820
        v >>= 2;
821
        n += 2;
822
    }
823
    if (v & 0x2) {
824
        n++;
825
    }
826
    return n;
827
}
828
829 45870f57 Michael Niedermayer
/* median of 3 */
830
static inline int mid_pred(int a, int b, int c)
831
{
832
    int vmin, vmax;
833
    vmax = vmin = a;
834
    if (b < vmin)
835
        vmin = b;
836
    else
837
        vmax = b;
838
839
    if (c < vmin)
840
        vmin = c;
841
    else if (c > vmax)
842
        vmax = c;
843
844
    return a + b + c - vmin - vmax;
845
}
846
847 91029be7 Michael Niedermayer
static inline int clip(int a, int amin, int amax)
848
{
849
    if (a < amin)
850
        return amin;
851
    else if (a > amax)
852
        return amax;
853
    else
854
        return a;
855
}
856
857 9dbcbd92 Michael Niedermayer
/* math */
858 f36db5df Michael Niedermayer
extern const UINT8 ff_sqrt_tab[128];
859
860 9dbcbd92 Michael Niedermayer
int ff_gcd(int a, int b);
861
862 7fd08ac1 Michael Niedermayer
static inline int ff_sqrt(int a)
863
{
864
    int ret=0;
865
    int s;
866
    int ret_sq=0;
867 f36db5df Michael Niedermayer
    
868
    if(a<128) return ff_sqrt_tab[a];
869
    
870 7fd08ac1 Michael Niedermayer
    for(s=15; s>=0; s--){
871
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
872
        if(b<=a){
873
            ret_sq=b;
874
            ret+= 1<<s;
875
        }
876
    }
877
    return ret;
878
}
879 202ef8b8 Michael Niedermayer
880
/**
881
 * converts fourcc string to int
882
 */
883 6a85ec8d Zdenek Kabelac
static inline int ff_get_fourcc(const char *s){
884 202ef8b8 Michael Niedermayer
    assert( strlen(s)==4 );
885
    
886
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
887
}
888
889 5d3cea3a Michael Niedermayer
void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
890
891 2ad1516a Michael Niedermayer
892
#ifdef ARCH_X86
893
#define MASK_ABS(mask, level)\
894
            asm volatile(\
895
                "cdq                        \n\t"\
896
                "xorl %1, %0                \n\t"\
897
                "subl %1, %0                \n\t"\
898
                : "+a" (level), "=&d" (mask)\
899
            );
900
#else
901
#define MASK_ABS(mask, level)\
902
            mask= level>>31;\
903
            level= (level^mask)-mask;
904
#endif
905
906
907 7fd08ac1 Michael Niedermayer
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
908
#define COPY3_IF_LT(x,y,a,b,c,d)\
909
asm volatile (\
910
    "cmpl %0, %3        \n\t"\
911
    "cmovl %3, %0        \n\t"\
912
    "cmovl %4, %1        \n\t"\
913
    "cmovl %5, %2        \n\t"\
914
    : "+r" (x), "+r" (a), "+r" (c)\
915
    : "r" (y), "r" (b), "r" (d)\
916
);
917
#else
918
#define COPY3_IF_LT(x,y,a,b,c,d)\
919
if((y)<(x)){\
920
     (x)=(y);\
921
     (a)=(b);\
922
     (c)=(d);\
923
}
924
#endif
925
926 a69b930c Alex Beregszaszi
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
927
928 96707bb7 Fabrice Bellard
#endif /* HAVE_AV_CONFIG_H */
929
930
#endif /* COMMON_H */