Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 5917d17c

History | View | Annotate | Download (22.1 KB)

1
#ifndef COMMON_H
2
#define COMMON_H
3

    
4
#define FFMPEG_VERSION_INT 0x000406
5
#define FFMPEG_VERSION     "0.4.6"
6

    
7
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
8
#    define CONFIG_WIN32
9
#endif
10

    
11
//#define ALT_BITSTREAM_WRITER
12
//#define ALIGNED_BITSTREAM_WRITER
13

    
14
#define ALT_BITSTREAM_READER
15
//#define LIBMPEG2_BITSTREAM_READER
16
//#define A32_BITSTREAM_READER
17

    
18
#ifdef HAVE_AV_CONFIG_H
19
/* only include the following when compiling package */
20
#    include "config.h"
21

    
22
#    include <stdlib.h>
23
#    include <stdio.h>
24
#    include <string.h>
25
#    ifndef __BEOS__
26
#        include <errno.h>
27
#    else
28
#        include "berrno.h"
29
#    endif
30
#    include <math.h>
31

    
32
#    ifndef ENODATA
33
#        define ENODATA  61
34
#    endif
35

    
36
#endif /* HAVE_AV_CONFIG_H */
37

    
38
/* Suppress restrict if it was not defined in config.h.  */
39
#ifndef restrict
40
#    define restrict
41
#endif
42

    
43
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
44
#    define always_inline __attribute__((always_inline)) inline
45
#else
46
#    define always_inline inline
47
#endif
48

    
49
#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
typedef UINT64 uint64_t;
69
typedef INT64 int64_t;
70

    
71
#    ifndef __MINGW32__
72
#        define INT64_C(c)     (c ## i64)
73
#        define UINT64_C(c)    (c ## i64)
74

    
75
#        define inline __inline
76

    
77
#    else
78
#        define INT64_C(c)     (c ## LL)
79
#        define UINT64_C(c)    (c ## ULL)
80
#    endif /* __MINGW32__ */
81

    
82
#    define M_PI    3.14159265358979323846
83
#    define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
84

    
85
#    ifdef _DEBUG
86
#        define DEBUG
87
#    endif
88

    
89
#    define snprintf _snprintf
90

    
91
#else /* CONFIG_WIN32 */
92

    
93
/* unix */
94

    
95
#    include <inttypes.h>
96

    
97
#    ifndef __WINE_WINDEF16_H
98
/* workaround for typedef conflict in MPlayer (wine typedefs) */
99
typedef unsigned short UINT16;
100
typedef signed short INT16;
101
#    endif
102

    
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
#    ifdef HAVE_AV_CONFIG_H
111

    
112
#        ifndef INT64_C
113
#            define INT64_C(c)     (c ## LL)
114
#            define UINT64_C(c)    (c ## ULL)
115
#        endif
116

    
117
#        ifdef USE_FASTMEMCPY
118
#            include "fastmemcpy.h"
119
#        endif
120

    
121
#    endif /* HAVE_AV_CONFIG_H */
122

    
123
#endif /* !CONFIG_WIN32 */
124

    
125
#ifdef HAVE_AV_CONFIG_H
126

    
127
#    include "bswap.h"
128

    
129
#    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

    
136
/* debug stuff */
137

    
138
#    ifndef DEBUG
139
#        define NDEBUG
140
#    endif
141
#    include <assert.h>
142

    
143
/* dprintf macros */
144
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
145

    
146
inline void dprintf(const char* fmt,...) {}
147

    
148
#    else
149

    
150
#        ifdef DEBUG
151
#            define dprintf(fmt,args...) printf(fmt, ## args)
152
#        else
153
#            define dprintf(fmt,args...)
154
#        endif
155

    
156
#    endif /* !CONFIG_WIN32 */
157

    
158
#    define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
159

    
160
/* assume b>0 */
161
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
162
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
163
#define MAX(a,b) ((a) > (b) ? (a) : (b))
164
#define MIN(a,b) ((a) > (b) ? (b) : (a))
165

    
166
#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
#else
183
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
184
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
185
#endif
186

    
187
/* bit output */
188

    
189
struct PutBitContext;
190

    
191
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
192

    
193
typedef struct PutBitContext {
194
#ifdef ALT_BITSTREAM_WRITER
195
    UINT8 *buf, *buf_end;
196
    int index;
197
#else
198
    UINT32 bit_buf;
199
    int bit_left;
200
    UINT8 *buf, *buf_ptr, *buf_end;
201
#endif
202
    INT64 data_out_size; /* in bytes */
203
} 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

    
210
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
211
void align_put_bits(PutBitContext *s);
212
void flush_put_bits(PutBitContext *s);
213
void put_string(PutBitContext * pbc, char *s);
214

    
215
/* jpeg specific put_bits */
216
void jflush_put_bits(PutBitContext *s);
217

    
218
/* bit input */
219

    
220
typedef struct GetBitContext {
221
    UINT8 *buffer, *buffer_end;
222
#ifdef ALT_BITSTREAM_READER
223
    int index;
224
#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
#endif
234
    int size;
235
} GetBitContext;
236

    
237
static inline int get_bits_count(GetBitContext *s);
238

    
239
#define VLC_TYPE INT16
240

    
241
typedef struct VLC {
242
    int bits;
243
    VLC_TYPE (*table)[2]; // code, bits
244
    int table_size, table_allocated;
245
} VLC;
246

    
247
typedef struct RL_VLC_ELEM {
248
    int16_t level;
249
    int8_t len;
250
    uint8_t run;
251
} RL_VLC_ELEM;
252

    
253
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
254
#ifdef ARCH_X86
255
#    define unaligned32(a) (*(UINT32*)(a))
256
#else
257
#    ifdef __GNUC__
258
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
#    elif defined(__DECC)
266
static inline uint32_t unaligned32(const void *v) {
267
    return *(const __unaligned uint32_t *) v;
268
}
269
#    else
270
static inline uint32_t unaligned32(const void *v) {
271
    return *(const uint32_t *) v;
272
}
273
#    endif
274
#endif //!ARCH_X86
275

    
276
#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
    
288
    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
#ifdef ALT_BITSTREAM_WRITER
313
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
314
{
315
#    ifdef ALIGNED_BITSTREAM_WRITER
316
#        ifdef ARCH_X86
317
    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
#        else
335
    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
#        endif
346
#    else //ALIGNED_BITSTREAM_WRITER
347
#        ifdef ARCH_X86
348
    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
#        else
365
    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
#        endif
374
#    endif //!ALIGNED_BITSTREAM_WRITER
375
}
376
#endif
377

    
378
#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
#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
/* Bitstream reader API docs:
477
name
478
    abritary name which is used as prefix for the internal variables
479

480
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

    
521
#ifdef ALT_BITSTREAM_READER
522
#   define MIN_CACHE_BITS 25
523

    
524
#   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
        NEG_USR32(name##_cache, num)
552

    
553
#   define SHOW_SBITS(name, gb, num)\
554
        NEG_SSR32(name##_cache, num)
555

    
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
}
562
#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

    
584
#   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
        NEG_USR32(name##_cache, num)
601

    
602
#   define SHOW_SBITS(name, gb, num)\
603
        NEG_SSR32(name##_cache, num)
604

    
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
}
611

    
612
#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
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
632
        name##_cache1 |= next<<name##_bit_count;\
633
        name##_buffer_ptr++;\
634
        name##_bit_count-= 32;\
635
    }\
636

    
637
#ifdef ARCH_X86
638
#   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
#else
646
#   define SKIP_CACHE(name, gb, num)\
647
        name##_cache0 <<= (num);\
648
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
649
        name##_cache1 <<= (num);
650
#endif
651

    
652
#   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
        NEG_USR32(name##_cache0, num)
666

    
667
#   define SHOW_SBITS(name, gb, num)\
668
        NEG_SSR32(name##_cache0, num)
669

    
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
}
676

    
677
#endif
678

    
679
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

    
689
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
}
697

    
698
static inline void skip_bits(GetBitContext *s, int n){
699
 //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
#ifdef ALT_BITSTREAM_READER
708
    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

    
715
    return result;
716
#else
717
    return get_bits(s, 1);
718
#endif
719
}
720

    
721
static inline unsigned int show_bits1(GetBitContext *s){
722
    return show_bits(s, 1);
723
}
724

    
725
static inline void skip_bits1(GetBitContext *s){
726
    skip_bits(s, 1);
727
}
728

    
729
void init_get_bits(GetBitContext *s,
730
                   UINT8 *buffer, int buffer_size);
731

    
732
int check_marker(GetBitContext *s, char *msg);
733
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
#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
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
754
        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
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
763
            code = table[index][0];\
764
            n    = table[index][1];\
765
        }\
766
    }\
767
    SKIP_BITS(name, gb, n)\
768
}
769

    
770
#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
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
793
static inline int get_vlc(GetBitContext *s, VLC *vlc)
794
{
795
    int code;
796
    VLC_TYPE (*table)[2]= vlc->table;
797
    
798
    OPEN_READER(re, s)
799
    UPDATE_CACHE(re, s)
800

    
801
    GET_VLC(code, re, s, table, vlc->bits, 3)    
802

    
803
    CLOSE_READER(re, s)
804
    return code;
805
}
806

    
807
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
808
                                  int bits, int max_depth)
809
{
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
}
820

    
821

    
822
/* 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
static inline int av_log2(unsigned int v)
849
{
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
/* 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
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
/* math */
904
extern const UINT8 ff_sqrt_tab[128];
905

    
906
int ff_gcd(int a, int b);
907

    
908
static inline int ff_sqrt(int a)
909
{
910
    int ret=0;
911
    int s;
912
    int ret_sq=0;
913
    
914
    if(a<128) return ff_sqrt_tab[a];
915
    
916
    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

    
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

    
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
#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
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
971

    
972
#endif /* HAVE_AV_CONFIG_H */
973

    
974
#endif /* COMMON_H */