Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 5917d17c

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

480 8db1a1dd Michael Niedermayer
gb
481
    getbitcontext
482

483
OPEN_READER(name, gb)
484
    loads gb into local variables
485

486
CLOSE_READER(name, gb)
487
    stores local vars in gb
488

489
UPDATE_CACHE(name, gb)
490
    refills the internal cache from the bitstream
491
    after this call at least MIN_CACHE_BITS will be available,
492

493
GET_CACHE(name, gb)
494
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
495

496
SHOW_UBITS(name, gb, num)
497
    will return the nest num bits
498

499
SHOW_SBITS(name, gb, num)
500
    will return the nest num bits and do sign extension
501

502
SKIP_BITS(name, gb, num)
503
    will skip over the next num bits
504
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
505

506
SKIP_CACHE(name, gb, num)
507
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
508

509
SKIP_COUNTER(name, gb, num)
510
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
511

512
LAST_SKIP_CACHE(name, gb, num)
513
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
514

515
LAST_SKIP_BITS(name, gb, num)
516
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
517

518
for examples see get_bits, show_bits, skip_bits, get_vlc
519
*/
520 2931ecb9 Arpi
521 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
522 8db1a1dd Michael Niedermayer
#   define MIN_CACHE_BITS 25
523 27a3e2c5 Michael Niedermayer
524 8db1a1dd Michael Niedermayer
#   define OPEN_READER(name, gb)\
525
        int name##_index= (gb)->index;\
526
        int name##_cache= 0;\
527
528
#   define CLOSE_READER(name, gb)\
529
        (gb)->index= name##_index;\
530
531
#   define UPDATE_CACHE(name, gb)\
532
        name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
533
534
#   define SKIP_CACHE(name, gb, num)\
535
        name##_cache <<= (num);\
536
537
// FIXME name?
538
#   define SKIP_COUNTER(name, gb, num)\
539
        name##_index += (num);\
540
541
#   define SKIP_BITS(name, gb, num)\
542
        {\
543
            SKIP_CACHE(name, gb, num)\
544
            SKIP_COUNTER(name, gb, num)\
545
        }\
546
547
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
548
#   define LAST_SKIP_CACHE(name, gb, num) ;
549
550
#   define SHOW_UBITS(name, gb, num)\
551 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
552 8db1a1dd Michael Niedermayer
553
#   define SHOW_SBITS(name, gb, num)\
554 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
555 8db1a1dd Michael Niedermayer
556
#   define GET_CACHE(name, gb)\
557
        ((uint32_t)name##_cache)
558
559
static inline int get_bits_count(GetBitContext *s){
560
    return s->index;
561 2931ecb9 Arpi
}
562 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
563
//libmpeg2 like reader
564
565
#   define MIN_CACHE_BITS 16
566
567
#   define OPEN_READER(name, gb)\
568
        int name##_bit_count=(gb)->bit_count;\
569
        int name##_cache= (gb)->cache;\
570
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
571
572
#   define CLOSE_READER(name, gb)\
573
        (gb)->bit_count= name##_bit_count;\
574
        (gb)->cache= name##_cache;\
575
        (gb)->buffer_ptr= name##_buffer_ptr;\
576
577
#   define UPDATE_CACHE(name, gb)\
578
    if(name##_bit_count > 0){\
579
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
580
        name##_buffer_ptr+=2;\
581
        name##_bit_count-= 16;\
582
    }\
583 2931ecb9 Arpi
584 8db1a1dd Michael Niedermayer
#   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)\
600 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache, num)
601 8db1a1dd Michael Niedermayer
602
#   define SHOW_SBITS(name, gb, num)\
603 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache, num)
604 8db1a1dd Michael Niedermayer
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;
610 612476ef Arpi
}
611
612 8db1a1dd Michael Niedermayer
#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 );\
631 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
632 8db1a1dd Michael Niedermayer
        name##_cache1 |= next<<name##_bit_count;\
633
        name##_buffer_ptr++;\
634
        name##_bit_count-= 32;\
635
    }\
636
637 3587d501 Michael Niedermayer
#ifdef ARCH_X86
638 8db1a1dd Michael Niedermayer
#   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
           );
645 3587d501 Michael Niedermayer
#else
646 8db1a1dd Michael Niedermayer
#   define SKIP_CACHE(name, gb, num)\
647
        name##_cache0 <<= (num);\
648 ef5e74bb Michael Niedermayer
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
649 8db1a1dd Michael Niedermayer
        name##_cache1 <<= (num);
650 3587d501 Michael Niedermayer
#endif
651 27a3e2c5 Michael Niedermayer
652 8db1a1dd Michael Niedermayer
#   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)\
665 ef5e74bb Michael Niedermayer
        NEG_USR32(name##_cache0, num)
666 8db1a1dd Michael Niedermayer
667
#   define SHOW_SBITS(name, gb, num)\
668 ef5e74bb Michael Niedermayer
        NEG_SSR32(name##_cache0, num)
669 8db1a1dd Michael Niedermayer
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;
675 4949028f Juanjo
}
676
677 49092244 Michael Niedermayer
#endif
678
679 8db1a1dd Michael Niedermayer
static inline unsigned int get_bits(GetBitContext *s, int n){
680
    register int tmp;
681
    OPEN_READER(re, s)
682
    UPDATE_CACHE(re, s)
683
    tmp= SHOW_UBITS(re, s, n);
684
    LAST_SKIP_BITS(re, s, n)
685
    CLOSE_READER(re, s)
686
    return tmp;
687
}
688 49092244 Michael Niedermayer
689 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits(GetBitContext *s, int n){
690
    register int tmp;
691
    OPEN_READER(re, s)
692
    UPDATE_CACHE(re, s)
693
    tmp= SHOW_UBITS(re, s, n);
694
//    CLOSE_READER(re, s)
695
    return tmp;
696 49092244 Michael Niedermayer
}
697
698 612476ef Arpi
static inline void skip_bits(GetBitContext *s, int n){
699 8db1a1dd Michael Niedermayer
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
700
    OPEN_READER(re, s)
701
    UPDATE_CACHE(re, s)
702
    LAST_SKIP_BITS(re, s, n)
703
    CLOSE_READER(re, s)
704
}
705
706
static inline unsigned int get_bits1(GetBitContext *s){
707 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
708 8db1a1dd Michael Niedermayer
    int index= s->index;
709
    uint8_t result= s->buffer[ index>>3 ];
710
    result<<= (index&0x07);
711
    result>>= 8 - 1;
712
    index++;
713
    s->index= index;
714 fcb609f4 Michael Niedermayer
715 8db1a1dd Michael Niedermayer
    return result;
716 27a3e2c5 Michael Niedermayer
#else
717 8db1a1dd Michael Niedermayer
    return get_bits(s, 1);
718 27a3e2c5 Michael Niedermayer
#endif
719 612476ef Arpi
}
720
721 8db1a1dd Michael Niedermayer
static inline unsigned int show_bits1(GetBitContext *s){
722
    return show_bits(s, 1);
723 612476ef Arpi
}
724
725 8db1a1dd Michael Niedermayer
static inline void skip_bits1(GetBitContext *s){
726
    skip_bits(s, 1);
727 9dbf9389 Fabrice Bellard
}
728 612476ef Arpi
729 8db1a1dd Michael Niedermayer
void init_get_bits(GetBitContext *s,
730
                   UINT8 *buffer, int buffer_size);
731
732 49c9325f Michael Niedermayer
int check_marker(GetBitContext *s, char *msg);
733 de6d9b64 Fabrice Bellard
void align_get_bits(GetBitContext *s);
734
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
735
             const void *bits, int bits_wrap, int bits_size,
736
             const void *codes, int codes_wrap, int codes_size);
737
void free_vlc(VLC *vlc);
738
739 cceabc86 Michael Niedermayer
#define GET_VLC(code, name, gb, table, bits, max_depth)\
740
{\
741
    int n, index, nb_bits;\
742
\
743
    index= SHOW_UBITS(name, gb, bits);\
744
    code = table[index][0];\
745
    n    = table[index][1];\
746
\
747
    if(max_depth > 1 && n < 0){\
748
        LAST_SKIP_BITS(name, gb, bits)\
749
        UPDATE_CACHE(name, gb)\
750
\
751
        nb_bits = -n;\
752
\
753 6300c80a Michael Niedermayer
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
754 cceabc86 Michael Niedermayer
        code = table[index][0];\
755
        n    = table[index][1];\
756
        if(max_depth > 2 && n < 0){\
757
            LAST_SKIP_BITS(name, gb, nb_bits)\
758
            UPDATE_CACHE(name, gb)\
759
\
760
            nb_bits = -n;\
761
\
762 6300c80a Michael Niedermayer
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
763 cceabc86 Michael Niedermayer
            code = table[index][0];\
764
            n    = table[index][1];\
765
        }\
766
    }\
767
    SKIP_BITS(name, gb, n)\
768
}
769
770 45a82edb Michael Niedermayer
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
771
{\
772
    int n, index, nb_bits;\
773
\
774
    index= SHOW_UBITS(name, gb, bits);\
775
    level = table[index].level;\
776
    n     = table[index].len;\
777
\
778
    if(max_depth > 1 && n < 0){\
779
        LAST_SKIP_BITS(name, gb, bits)\
780
        UPDATE_CACHE(name, gb)\
781
\
782
        nb_bits = -n;\
783
\
784
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
785
        level = table[index].level;\
786
        n     = table[index].len;\
787
    }\
788
    run= table[index].run;\
789
    SKIP_BITS(name, gb, n)\
790
}
791
792 08dce7b0 Michael Niedermayer
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
793 3587d501 Michael Niedermayer
static inline int get_vlc(GetBitContext *s, VLC *vlc)
794
{
795 cceabc86 Michael Niedermayer
    int code;
796
    VLC_TYPE (*table)[2]= vlc->table;
797
    
798 8db1a1dd Michael Niedermayer
    OPEN_READER(re, s)
799
    UPDATE_CACHE(re, s)
800 3587d501 Michael Niedermayer
801 cceabc86 Michael Niedermayer
    GET_VLC(code, re, s, table, vlc->bits, 3)    
802 8db1a1dd Michael Niedermayer
803
    CLOSE_READER(re, s)
804 3587d501 Michael Niedermayer
    return code;
805 08dce7b0 Michael Niedermayer
}
806
807 d200cab6 Falk Hüffner
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
808
                                  int bits, int max_depth)
809 08dce7b0 Michael Niedermayer
{
810
    int code;
811
    
812
    OPEN_READER(re, s)
813
    UPDATE_CACHE(re, s)
814
815
    GET_VLC(code, re, s, table, bits, max_depth)
816
817
    CLOSE_READER(re, s)
818
    return code;
819 3587d501 Michael Niedermayer
}
820
821 45a82edb Michael Niedermayer
822 de6d9b64 Fabrice Bellard
/* define it to include statistics code (useful only for optimizing
823
   codec efficiency */
824
//#define STATS
825
826
#ifdef STATS
827
828
enum {
829
    ST_UNKNOWN,
830
    ST_DC,
831
    ST_INTRA_AC,
832
    ST_INTER_AC,
833
    ST_INTRA_MB,
834
    ST_INTER_MB,
835
    ST_MV,
836
    ST_NB,
837
};
838
839
extern int st_current_index;
840
extern unsigned int st_bit_counts[ST_NB];
841
extern unsigned int st_out_bit_counts[ST_NB];
842
843
void print_stats(void);
844
#endif
845
846
/* misc math functions */
847
848 a822a479 Nick Kurshev
static inline int av_log2(unsigned int v)
849 de6d9b64 Fabrice Bellard
{
850
    int n;
851
852
    n = 0;
853
    if (v & 0xffff0000) {
854
        v >>= 16;
855
        n += 16;
856
    }
857
    if (v & 0xff00) {
858
        v >>= 8;
859
        n += 8;
860
    }
861
    if (v & 0xf0) {
862
        v >>= 4;
863
        n += 4;
864
    }
865
    if (v & 0xc) {
866
        v >>= 2;
867
        n += 2;
868
    }
869
    if (v & 0x2) {
870
        n++;
871
    }
872
    return n;
873
}
874
875 45870f57 Michael Niedermayer
/* median of 3 */
876
static inline int mid_pred(int a, int b, int c)
877
{
878
    int vmin, vmax;
879
    vmax = vmin = a;
880
    if (b < vmin)
881
        vmin = b;
882
    else
883
        vmax = b;
884
885
    if (c < vmin)
886
        vmin = c;
887
    else if (c > vmax)
888
        vmax = c;
889
890
    return a + b + c - vmin - vmax;
891
}
892
893 91029be7 Michael Niedermayer
static inline int clip(int a, int amin, int amax)
894
{
895
    if (a < amin)
896
        return amin;
897
    else if (a > amax)
898
        return amax;
899
    else
900
        return a;
901
}
902
903 9dbcbd92 Michael Niedermayer
/* math */
904 f36db5df Michael Niedermayer
extern const UINT8 ff_sqrt_tab[128];
905
906 9dbcbd92 Michael Niedermayer
int ff_gcd(int a, int b);
907
908 7fd08ac1 Michael Niedermayer
static inline int ff_sqrt(int a)
909
{
910
    int ret=0;
911
    int s;
912
    int ret_sq=0;
913 f36db5df Michael Niedermayer
    
914
    if(a<128) return ff_sqrt_tab[a];
915
    
916 7fd08ac1 Michael Niedermayer
    for(s=15; s>=0; s--){
917
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
918
        if(b<=a){
919
            ret_sq=b;
920
            ret+= 1<<s;
921
        }
922
    }
923
    return ret;
924
}
925 202ef8b8 Michael Niedermayer
926
/**
927
 * converts fourcc string to int
928
 */
929
static inline int ff_get_fourcc(char *s){
930
    assert( strlen(s)==4 );
931
    
932
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
933
}
934
935 2ad1516a Michael Niedermayer
936
#ifdef ARCH_X86
937
#define MASK_ABS(mask, level)\
938
            asm volatile(\
939
                "cdq                        \n\t"\
940
                "xorl %1, %0                \n\t"\
941
                "subl %1, %0                \n\t"\
942
                : "+a" (level), "=&d" (mask)\
943
            );
944
#else
945
#define MASK_ABS(mask, level)\
946
            mask= level>>31;\
947
            level= (level^mask)-mask;
948
#endif
949
950
951 7fd08ac1 Michael Niedermayer
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
952
#define COPY3_IF_LT(x,y,a,b,c,d)\
953
asm volatile (\
954
    "cmpl %0, %3        \n\t"\
955
    "cmovl %3, %0        \n\t"\
956
    "cmovl %4, %1        \n\t"\
957
    "cmovl %5, %2        \n\t"\
958
    : "+r" (x), "+r" (a), "+r" (c)\
959
    : "r" (y), "r" (b), "r" (d)\
960
);
961
#else
962
#define COPY3_IF_LT(x,y,a,b,c,d)\
963
if((y)<(x)){\
964
     (x)=(y);\
965
     (a)=(b);\
966
     (c)=(d);\
967
}
968
#endif
969
970 a69b930c Alex Beregszaszi
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
971
972 96707bb7 Fabrice Bellard
#endif /* HAVE_AV_CONFIG_H */
973
974
#endif /* COMMON_H */