Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 9f9c3229

History | View | Annotate | Download (22.3 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 073b013d Michael Niedermayer
//rounded divison & shift
161
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
162 d7e9533a Michael Niedermayer
/* assume b>0 */
163
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
164 32cd20de Michael Niedermayer
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
165 75460b0c Michael Niedermayer
166
#ifndef MAX
167
#    define MAX(a,b) ((a) > (b) ? (a) : (b))
168
#endif
169
170
#ifndef MIN
171
#    define MIN(a,b) ((a) > (b) ? (b) : (a))
172
#endif
173 d7e9533a Michael Niedermayer
174 ef5e74bb Michael Niedermayer
#ifdef ARCH_X86
175
// avoid +32 for shift optimization (gcc should do that ...)
176
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
177
    asm ("sarl %1, %0\n\t"
178
         : "+r" (a)
179
         : "ic" ((uint8_t)(-s))
180
    );
181
    return a;
182
}
183
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
184
    asm ("shrl %1, %0\n\t"
185
         : "+r" (a)
186
         : "ic" ((uint8_t)(-s))
187
    );
188
    return a;
189
}
190 8db1a1dd Michael Niedermayer
#else
191 9b59c92f Michael Niedermayer
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
192
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
193 8db1a1dd Michael Niedermayer
#endif
194
195 de6d9b64 Fabrice Bellard
/* bit output */
196
197
struct PutBitContext;
198
199
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
200
201
typedef struct PutBitContext {
202 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
203
    UINT8 *buf, *buf_end;
204
    int index;
205
#else
206 de6d9b64 Fabrice Bellard
    UINT32 bit_buf;
207 d8cf5aea Michael Niedermayer
    int bit_left;
208 2931ecb9 Arpi
    UINT8 *buf, *buf_ptr, *buf_end;
209 17592475 Michael Niedermayer
#endif
210
    INT64 data_out_size; /* in bytes */
211 de6d9b64 Fabrice Bellard
} PutBitContext;
212
213
void init_put_bits(PutBitContext *s, 
214
                   UINT8 *buffer, int buffer_size,
215
                   void *opaque,
216
                   void (*write_data)(void *, UINT8 *, int));
217 17592475 Michael Niedermayer
218 9dbf9389 Fabrice Bellard
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
219 de6d9b64 Fabrice Bellard
void align_put_bits(PutBitContext *s);
220
void flush_put_bits(PutBitContext *s);
221 9717dad8 Michael Niedermayer
void put_string(PutBitContext * pbc, char *s);
222 de6d9b64 Fabrice Bellard
223
/* jpeg specific put_bits */
224
void jflush_put_bits(PutBitContext *s);
225
226
/* bit input */
227
228
typedef struct GetBitContext {
229 8db1a1dd Michael Niedermayer
    UINT8 *buffer, *buffer_end;
230 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
231
    int index;
232 8db1a1dd Michael Niedermayer
#elif defined LIBMPEG2_BITSTREAM_READER
233
    UINT8 *buffer_ptr;
234
    UINT32 cache;
235
    int bit_count;
236
#elif defined A32_BITSTREAM_READER
237
    UINT32 *buffer_ptr;
238
    UINT32 cache0;
239
    UINT32 cache1;
240
    int bit_count;
241 27a3e2c5 Michael Niedermayer
#endif
242 45870f57 Michael Niedermayer
    int size;
243 de6d9b64 Fabrice Bellard
} GetBitContext;
244
245 49092244 Michael Niedermayer
static inline int get_bits_count(GetBitContext *s);
246
247 8db1a1dd Michael Niedermayer
#define VLC_TYPE INT16
248
249 de6d9b64 Fabrice Bellard
typedef struct VLC {
250
    int bits;
251 8db1a1dd Michael Niedermayer
    VLC_TYPE (*table)[2]; // code, bits
252 de6d9b64 Fabrice Bellard
    int table_size, table_allocated;
253
} VLC;
254
255 45a82edb Michael Niedermayer
typedef struct RL_VLC_ELEM {
256
    int16_t level;
257
    int8_t len;
258
    uint8_t run;
259
} RL_VLC_ELEM;
260
261 3587d501 Michael Niedermayer
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
262
#ifdef ARCH_X86
263 9b59c92f Michael Niedermayer
#    define unaligned32(a) (*(UINT32*)(a))
264 3587d501 Michael Niedermayer
#else
265 9b59c92f Michael Niedermayer
#    ifdef __GNUC__
266 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
267
    struct Unaligned {
268
        uint32_t i;
269
    } __attribute__((packed));
270
271
    return ((const struct Unaligned *) v)->i;
272
}
273 9b59c92f Michael Niedermayer
#    elif defined(__DECC)
274 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
275
    return *(const __unaligned uint32_t *) v;
276
}
277 9b59c92f Michael Niedermayer
#    else
278 3587d501 Michael Niedermayer
static inline uint32_t unaligned32(const void *v) {
279
    return *(const uint32_t *) v;
280
}
281 9b59c92f Michael Niedermayer
#    endif
282 3587d501 Michael Niedermayer
#endif //!ARCH_X86
283
284 d8cf5aea Michael Niedermayer
#ifndef ALT_BITSTREAM_WRITER
285
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
286
{
287
    unsigned int bit_buf;
288
    int bit_left;
289
290
#ifdef STATS
291
    st_out_bit_counts[st_current_index] += n;
292
#endif
293
    //    printf("put_bits=%d %x\n", n, value);
294
    assert(n == 32 || value < (1U << n));
295 fcb609f4 Michael Niedermayer
    
296 d8cf5aea Michael Niedermayer
    bit_buf = s->bit_buf;
297
    bit_left = s->bit_left;
298
299
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
300
    /* XXX: optimize */
301
    if (n < bit_left) {
302
        bit_buf = (bit_buf<<n) | value;
303
        bit_left-=n;
304
    } else {
305
        bit_buf<<=bit_left;
306
        bit_buf |= value >> (n - bit_left);
307
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
308
        //printf("bitbuf = %08x\n", bit_buf);
309
        s->buf_ptr+=4;
310
        bit_left+=32 - n;
311
        bit_buf = value;
312
    }
313
314
    s->bit_buf = bit_buf;
315
    s->bit_left = bit_left;
316
}
317
#endif
318
319
320 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
321 4f0f8bfc Michael Niedermayer
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
322 17592475 Michael Niedermayer
{
323 9b59c92f Michael Niedermayer
#    ifdef ALIGNED_BITSTREAM_WRITER
324
#        ifdef ARCH_X86
325 4f0f8bfc Michael Niedermayer
    asm volatile(
326
        "movl %0, %%ecx                        \n\t"
327
        "xorl %%eax, %%eax                \n\t"
328
        "shrdl %%cl, %1, %%eax                \n\t"
329
        "shrl %%cl, %1                        \n\t"
330
        "movl %0, %%ecx                        \n\t"
331
        "shrl $3, %%ecx                        \n\t"
332
        "andl $0xFFFFFFFC, %%ecx        \n\t"
333
        "bswapl %1                        \n\t"
334
        "orl %1, (%2, %%ecx)                \n\t"
335
        "bswapl %%eax                        \n\t"
336
        "addl %3, %0                        \n\t"
337
        "movl %%eax, 4(%2, %%ecx)        \n\t"
338
        : "=&r" (s->index), "=&r" (value)
339
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
340
        : "%eax", "%ecx"
341
    );
342 9b59c92f Michael Niedermayer
#        else
343 4f0f8bfc Michael Niedermayer
    int index= s->index;
344
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
345
    
346
    value<<= 32-n; 
347
    
348
    ptr[0] |= be2me_32(value>>(index&31));
349
    ptr[1]  = be2me_32(value<<(32-(index&31)));
350
//if(n>24) printf("%d %d\n", n, value);
351
    index+= n;
352
    s->index= index;
353 9b59c92f Michael Niedermayer
#        endif
354
#    else //ALIGNED_BITSTREAM_WRITER
355
#        ifdef ARCH_X86
356 17592475 Michael Niedermayer
    asm volatile(
357
        "movl $7, %%ecx                        \n\t"
358
        "andl %0, %%ecx                        \n\t"
359
        "addl %3, %%ecx                        \n\t"
360
        "negl %%ecx                        \n\t"
361
        "shll %%cl, %1                        \n\t"
362
        "bswapl %1                        \n\t"
363
        "movl %0, %%ecx                        \n\t"
364
        "shrl $3, %%ecx                        \n\t"
365
        "orl %1, (%%ecx, %2)                \n\t"
366
        "addl %3, %0                        \n\t"
367
        "movl $0, 4(%%ecx, %2)                \n\t"
368
        : "=&r" (s->index), "=&r" (value)
369
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
370
        : "%ecx"
371
    );
372 9b59c92f Michael Niedermayer
#        else
373 17592475 Michael Niedermayer
    int index= s->index;
374
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
375
    
376
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
377
    ptr[1] = 0;
378
//if(n>24) printf("%d %d\n", n, value);
379
    index+= n;
380
    s->index= index;
381 9b59c92f Michael Niedermayer
#        endif
382
#    endif //!ALIGNED_BITSTREAM_WRITER
383 17592475 Michael Niedermayer
}
384
#endif
385
386 d8cf5aea Michael Niedermayer
#ifndef ALT_BITSTREAM_WRITER
387
/* for jpeg : escape 0xff with 0x00 after it */
388
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
389
{
390
    unsigned int bit_buf, b;
391
    int bit_left, i;
392
    
393
    assert(n == 32 || value < (1U << n));
394
395
    bit_buf = s->bit_buf;
396
    bit_left = s->bit_left;
397
398
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
399
    /* XXX: optimize */
400
    if (n < bit_left) {
401
        bit_buf = (bit_buf<<n) | value;
402
        bit_left-=n;
403
    } else {
404
        bit_buf<<=bit_left;
405
        bit_buf |= value >> (n - bit_left);
406
        /* handle escape */
407
        for(i=0;i<4;i++) {
408
            b = (bit_buf >> 24);
409
            *(s->buf_ptr++) = b;
410
            if (b == 0xff)
411
                *(s->buf_ptr++) = 0;
412
            bit_buf <<= 8;
413
        }
414
415
        bit_left+= 32 - n;
416
        bit_buf = value;
417
    }
418
    
419
    s->bit_buf = bit_buf;
420
    s->bit_left = bit_left;
421
}
422
#endif
423
424
425 17592475 Michael Niedermayer
#ifdef ALT_BITSTREAM_WRITER
426
static inline void jput_bits(PutBitContext *s, int n, int value)
427
{
428
    int index= s->index;
429
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
430
    int v= ptr[0];
431
//if(n>24) printf("%d %d\n", n, value);
432
    
433
    v |= be2me_32(value<<(32-n-(index&7) ));
434
    if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
435
    {
436
        /* handle idiotic (m)jpeg escapes */
437
        uint8_t *bPtr= (uint8_t*)ptr;
438
        int numChecked= ((index+n)>>3) - (index>>3);
439
        
440
        v= be2me_32(v);
441
442
        *(bPtr++)= v>>24;
443
        if((v&0xFF000000)==0xFF000000 && numChecked>0){
444
                *(bPtr++)= 0x00;
445
                index+=8;
446
        }
447
        *(bPtr++)= (v>>16)&0xFF;
448
        if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
449
                *(bPtr++)= 0x00;
450
                index+=8;
451
        }
452
        *(bPtr++)= (v>>8)&0xFF;
453
        if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
454
                *(bPtr++)= 0x00;
455
                index+=8;
456
        }
457
        *(bPtr++)= v&0xFF;
458
        if((v&0x000000FF)==0x000000FF && numChecked>3){
459
                *(bPtr++)= 0x00;
460
                index+=8;
461
        }
462
        *((uint32_t*)bPtr)= 0;
463
    }
464
    else
465
    {
466
        ptr[0] = v;
467
        ptr[1] = 0;
468
    }
469
470
    index+= n;
471
    s->index= index;
472
 }
473
#endif
474
475
static inline uint8_t* pbBufPtr(PutBitContext *s)
476
{
477
#ifdef ALT_BITSTREAM_WRITER
478
        return s->buf + (s->index>>3);
479
#else
480
        return s->buf_ptr;
481
#endif
482
}
483
484 8db1a1dd Michael Niedermayer
/* Bitstream reader API docs:
485
name
486
    abritary name which is used as prefix for the internal variables
487 de6d9b64 Fabrice Bellard

488 8db1a1dd Michael Niedermayer
gb
489
    getbitcontext
490

491
OPEN_READER(name, gb)
492
    loads gb into local variables
493

494
CLOSE_READER(name, gb)
495
    stores local vars in gb
496

497
UPDATE_CACHE(name, gb)
498
    refills the internal cache from the bitstream
499
    after this call at least MIN_CACHE_BITS will be available,
500

501
GET_CACHE(name, gb)
502
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
503

504
SHOW_UBITS(name, gb, num)
505
    will return the nest num bits
506

507
SHOW_SBITS(name, gb, num)
508
    will return the nest num bits and do sign extension
509

510
SKIP_BITS(name, gb, num)
511
    will skip over the next num bits
512
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
513

514
SKIP_CACHE(name, gb, num)
515
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
516

517
SKIP_COUNTER(name, gb, num)
518
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
519

520
LAST_SKIP_CACHE(name, gb, num)
521
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
522

523
LAST_SKIP_BITS(name, gb, num)
524
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
525

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