Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ cceabc86

History | View | Annotate | Download (20.2 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 ARCH_ALPHA
19
#define ALT_BITSTREAM_READER
20
#endif
21

    
22
//#define ALIGNED_BITSTREAM
23
#define FAST_GET_FIRST_VLC
24
//#define DUMP_STREAM
25

    
26
#ifdef HAVE_AV_CONFIG_H
27
/* only include the following when compiling package */
28
#include "config.h"
29

    
30
#include <stdlib.h>
31
#include <stdio.h>
32
#include <string.h>
33
#include <errno.h>
34
#include <math.h>
35

    
36
#ifndef ENODATA
37
#define ENODATA  61
38
#endif
39

    
40
#endif /* HAVE_AV_CONFIG_H */
41

    
42
/* Suppress restrict if it was not defined in config.h.  */
43
#ifndef restrict
44
#define restrict
45
#endif
46

    
47
#ifdef CONFIG_WIN32
48

    
49
/* windows */
50

    
51
typedef unsigned short UINT16;
52
typedef signed short INT16;
53
typedef unsigned char UINT8;
54
typedef unsigned int UINT32;
55
typedef unsigned __int64 UINT64;
56
typedef signed char INT8;
57
typedef signed int INT32;
58
typedef signed __int64 INT64;
59

    
60
typedef UINT8 uint8_t;
61
typedef INT8 int8_t;
62
typedef UINT16 uint16_t;
63
typedef INT16 int16_t;
64
typedef UINT32 uint32_t;
65
typedef INT32 int32_t;
66
typedef UINT64 uint64_t;
67
typedef INT64 int64_t;
68

    
69
#ifndef __MINGW32__
70
#define INT64_C(c)     (c ## i64)
71
#define UINT64_C(c)    (c ## i64)
72

    
73
#define inline __inline
74

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

    
80
#define M_PI    3.14159265358979323846
81
#define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
82

    
83
#ifdef _DEBUG
84
#define DEBUG
85
#endif
86

    
87
#define snprintf _snprintf
88

    
89
#else /* CONFIG_WIN32 */
90

    
91
/* unix */
92

    
93
#include <inttypes.h>
94

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

    
101
typedef unsigned char UINT8;
102
typedef unsigned int UINT32;
103
typedef unsigned long long UINT64;
104
typedef signed char INT8;
105
typedef signed int INT32;
106
typedef signed long long INT64;
107

    
108
#ifdef HAVE_AV_CONFIG_H
109

    
110
#ifdef __FreeBSD__
111
#include <sys/param.h>
112
#endif
113

    
114
#ifndef INT64_C
115
#define INT64_C(c)     (c ## LL)
116
#define UINT64_C(c)    (c ## ULL)
117
#endif
118

    
119
#ifdef USE_FASTMEMCPY
120
#include "fastmemcpy.h"
121
#endif
122

    
123
#endif /* HAVE_AV_CONFIG_H */
124

    
125
#endif /* !CONFIG_WIN32 */
126

    
127
#ifdef HAVE_AV_CONFIG_H
128

    
129
#include "bswap.h"
130

    
131
#if defined(__MINGW32__) || defined(__CYGWIN__) || \
132
    defined(__OS2__) || defined (__OpenBSD__)
133
#define MANGLE(a) "_" #a
134
#else
135
#define MANGLE(a) #a
136
#endif
137

    
138
/* debug stuff */
139

    
140
#ifndef DEBUG
141
#define NDEBUG
142
#endif
143
#include <assert.h>
144

    
145
/* dprintf macros */
146
#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
147

    
148
inline void dprintf(const char* fmt,...) {}
149

    
150
#else
151

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

    
158
#endif /* !CONFIG_WIN32 */
159

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

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

    
168
#ifdef ARCH_X86
169
// avoid +32 for shift optimization (gcc should do that ...)
170
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
171
    asm ("sarl %1, %0\n\t"
172
         : "+r" (a)
173
         : "ic" ((uint8_t)(-s))
174
    );
175
    return a;
176
}
177
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
178
    asm ("shrl %1, %0\n\t"
179
         : "+r" (a)
180
         : "ic" ((uint8_t)(-s))
181
    );
182
    return a;
183
}
184
#else
185
#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
186
#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
187
#endif
188

    
189
/* bit output */
190

    
191
struct PutBitContext;
192

    
193
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
194

    
195
typedef struct PutBitContext {
196
#ifdef ALT_BITSTREAM_WRITER
197
    UINT8 *buf, *buf_end;
198
    int index;
199
#else
200
    UINT32 bit_buf;
201
    int bit_left;
202
    UINT8 *buf, *buf_ptr, *buf_end;
203
#endif
204
    INT64 data_out_size; /* in bytes */
205
} PutBitContext;
206

    
207
void init_put_bits(PutBitContext *s, 
208
                   UINT8 *buffer, int buffer_size,
209
                   void *opaque,
210
                   void (*write_data)(void *, UINT8 *, int));
211

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

    
217
/* jpeg specific put_bits */
218
void jflush_put_bits(PutBitContext *s);
219

    
220
/* bit input */
221

    
222
typedef struct GetBitContext {
223
    UINT8 *buffer, *buffer_end;
224
#ifdef ALT_BITSTREAM_READER
225
    int index;
226
#elif defined LIBMPEG2_BITSTREAM_READER
227
    UINT8 *buffer_ptr;
228
    UINT32 cache;
229
    int bit_count;
230
#elif defined A32_BITSTREAM_READER
231
    UINT32 *buffer_ptr;
232
    UINT32 cache0;
233
    UINT32 cache1;
234
    int bit_count;
235
#endif
236
    int size;
237
} GetBitContext;
238

    
239
static inline int get_bits_count(GetBitContext *s);
240

    
241
#define VLC_TYPE INT16
242

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

    
249
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
250
#ifdef ARCH_X86
251
#define unaligned32(a) (*(UINT32*)(a))
252
#else
253
#ifdef __GNUC__
254
static inline uint32_t unaligned32(const void *v) {
255
    struct Unaligned {
256
        uint32_t i;
257
    } __attribute__((packed));
258

    
259
    return ((const struct Unaligned *) v)->i;
260
}
261
#elif defined(__DECC)
262
static inline uint32_t unaligned32(const void *v) {
263
    return *(const __unaligned uint32_t *) v;
264
}
265
#else
266
static inline uint32_t unaligned32(const void *v) {
267
    return *(const uint32_t *) v;
268
}
269
#endif
270
#endif //!ARCH_X86
271

    
272
#ifndef ALT_BITSTREAM_WRITER
273
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
274
{
275
    unsigned int bit_buf;
276
    int bit_left;
277

    
278
#ifdef STATS
279
    st_out_bit_counts[st_current_index] += n;
280
#endif
281
    //    printf("put_bits=%d %x\n", n, value);
282
    assert(n == 32 || value < (1U << n));
283
    
284
    bit_buf = s->bit_buf;
285
    bit_left = s->bit_left;
286

    
287
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
288
    /* XXX: optimize */
289
    if (n < bit_left) {
290
        bit_buf = (bit_buf<<n) | value;
291
        bit_left-=n;
292
    } else {
293
        bit_buf<<=bit_left;
294
        bit_buf |= value >> (n - bit_left);
295
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
296
        //printf("bitbuf = %08x\n", bit_buf);
297
        s->buf_ptr+=4;
298
        bit_left+=32 - n;
299
        bit_buf = value;
300
    }
301

    
302
    s->bit_buf = bit_buf;
303
    s->bit_left = bit_left;
304
}
305
#endif
306

    
307

    
308
#ifdef ALT_BITSTREAM_WRITER
309
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
310
{
311
#ifdef ALIGNED_BITSTREAM_WRITER
312
#ifdef ARCH_X86
313
    asm volatile(
314
        "movl %0, %%ecx                        \n\t"
315
        "xorl %%eax, %%eax                \n\t"
316
        "shrdl %%cl, %1, %%eax                \n\t"
317
        "shrl %%cl, %1                        \n\t"
318
        "movl %0, %%ecx                        \n\t"
319
        "shrl $3, %%ecx                        \n\t"
320
        "andl $0xFFFFFFFC, %%ecx        \n\t"
321
        "bswapl %1                        \n\t"
322
        "orl %1, (%2, %%ecx)                \n\t"
323
        "bswapl %%eax                        \n\t"
324
        "addl %3, %0                        \n\t"
325
        "movl %%eax, 4(%2, %%ecx)        \n\t"
326
        : "=&r" (s->index), "=&r" (value)
327
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
328
        : "%eax", "%ecx"
329
    );
330
#else
331
    int index= s->index;
332
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
333
    
334
    value<<= 32-n; 
335
    
336
    ptr[0] |= be2me_32(value>>(index&31));
337
    ptr[1]  = be2me_32(value<<(32-(index&31)));
338
//if(n>24) printf("%d %d\n", n, value);
339
    index+= n;
340
    s->index= index;
341
#endif
342
#else //ALIGNED_BITSTREAM_WRITER
343
#ifdef ARCH_X86
344
    asm volatile(
345
        "movl $7, %%ecx                        \n\t"
346
        "andl %0, %%ecx                        \n\t"
347
        "addl %3, %%ecx                        \n\t"
348
        "negl %%ecx                        \n\t"
349
        "shll %%cl, %1                        \n\t"
350
        "bswapl %1                        \n\t"
351
        "movl %0, %%ecx                        \n\t"
352
        "shrl $3, %%ecx                        \n\t"
353
        "orl %1, (%%ecx, %2)                \n\t"
354
        "addl %3, %0                        \n\t"
355
        "movl $0, 4(%%ecx, %2)                \n\t"
356
        : "=&r" (s->index), "=&r" (value)
357
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
358
        : "%ecx"
359
    );
360
#else
361
    int index= s->index;
362
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
363
    
364
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
365
    ptr[1] = 0;
366
//if(n>24) printf("%d %d\n", n, value);
367
    index+= n;
368
    s->index= index;
369
#endif
370
#endif //!ALIGNED_BITSTREAM_WRITER
371
}
372
#endif
373

    
374
#ifndef ALT_BITSTREAM_WRITER
375
/* for jpeg : escape 0xff with 0x00 after it */
376
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
377
{
378
    unsigned int bit_buf, b;
379
    int bit_left, i;
380
    
381
    assert(n == 32 || value < (1U << n));
382

    
383
    bit_buf = s->bit_buf;
384
    bit_left = s->bit_left;
385

    
386
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
387
    /* XXX: optimize */
388
    if (n < bit_left) {
389
        bit_buf = (bit_buf<<n) | value;
390
        bit_left-=n;
391
    } else {
392
        bit_buf<<=bit_left;
393
        bit_buf |= value >> (n - bit_left);
394
        /* handle escape */
395
        for(i=0;i<4;i++) {
396
            b = (bit_buf >> 24);
397
            *(s->buf_ptr++) = b;
398
            if (b == 0xff)
399
                *(s->buf_ptr++) = 0;
400
            bit_buf <<= 8;
401
        }
402

    
403
        bit_left+= 32 - n;
404
        bit_buf = value;
405
    }
406
    
407
    s->bit_buf = bit_buf;
408
    s->bit_left = bit_left;
409
}
410
#endif
411

    
412

    
413
#ifdef ALT_BITSTREAM_WRITER
414
static inline void jput_bits(PutBitContext *s, int n, int value)
415
{
416
    int index= s->index;
417
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
418
    int v= ptr[0];
419
//if(n>24) printf("%d %d\n", n, value);
420
    
421
    v |= be2me_32(value<<(32-n-(index&7) ));
422
    if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
423
    {
424
        /* handle idiotic (m)jpeg escapes */
425
        uint8_t *bPtr= (uint8_t*)ptr;
426
        int numChecked= ((index+n)>>3) - (index>>3);
427
        
428
        v= be2me_32(v);
429

    
430
        *(bPtr++)= v>>24;
431
        if((v&0xFF000000)==0xFF000000 && numChecked>0){
432
                *(bPtr++)= 0x00;
433
                index+=8;
434
        }
435
        *(bPtr++)= (v>>16)&0xFF;
436
        if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
437
                *(bPtr++)= 0x00;
438
                index+=8;
439
        }
440
        *(bPtr++)= (v>>8)&0xFF;
441
        if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
442
                *(bPtr++)= 0x00;
443
                index+=8;
444
        }
445
        *(bPtr++)= v&0xFF;
446
        if((v&0x000000FF)==0x000000FF && numChecked>3){
447
                *(bPtr++)= 0x00;
448
                index+=8;
449
        }
450
        *((uint32_t*)bPtr)= 0;
451
    }
452
    else
453
    {
454
        ptr[0] = v;
455
        ptr[1] = 0;
456
    }
457

    
458
    index+= n;
459
    s->index= index;
460
 }
461
#endif
462

    
463
static inline uint8_t* pbBufPtr(PutBitContext *s)
464
{
465
#ifdef ALT_BITSTREAM_WRITER
466
        return s->buf + (s->index>>3);
467
#else
468
        return s->buf_ptr;
469
#endif
470
}
471

    
472
/* Bitstream reader API docs:
473
name
474
    abritary name which is used as prefix for the internal variables
475

476
gb
477
    getbitcontext
478

479
OPEN_READER(name, gb)
480
    loads gb into local variables
481

482
CLOSE_READER(name, gb)
483
    stores local vars in gb
484

485
UPDATE_CACHE(name, gb)
486
    refills the internal cache from the bitstream
487
    after this call at least MIN_CACHE_BITS will be available,
488

489
GET_CACHE(name, gb)
490
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
491

492
SHOW_UBITS(name, gb, num)
493
    will return the nest num bits
494

495
SHOW_SBITS(name, gb, num)
496
    will return the nest num bits and do sign extension
497

498
SKIP_BITS(name, gb, num)
499
    will skip over the next num bits
500
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
501

502
SKIP_CACHE(name, gb, num)
503
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
504

505
SKIP_COUNTER(name, gb, num)
506
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
507

508
LAST_SKIP_CACHE(name, gb, num)
509
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
510

511
LAST_SKIP_BITS(name, gb, num)
512
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
513

514
for examples see get_bits, show_bits, skip_bits, get_vlc
515
*/
516

    
517
#ifdef ALT_BITSTREAM_READER
518
#   define MIN_CACHE_BITS 25
519

    
520
#   define OPEN_READER(name, gb)\
521
        int name##_index= (gb)->index;\
522
        int name##_cache= 0;\
523

    
524
#   define CLOSE_READER(name, gb)\
525
        (gb)->index= name##_index;\
526

    
527
#   define UPDATE_CACHE(name, gb)\
528
        name##_cache= be2me_32( unaligned32( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) ) << (name##_index&0x07);\
529

    
530
#   define SKIP_CACHE(name, gb, num)\
531
        name##_cache <<= (num);\
532

    
533
// FIXME name?
534
#   define SKIP_COUNTER(name, gb, num)\
535
        name##_index += (num);\
536

    
537
#   define SKIP_BITS(name, gb, num)\
538
        {\
539
            SKIP_CACHE(name, gb, num)\
540
            SKIP_COUNTER(name, gb, num)\
541
        }\
542

    
543
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
544
#   define LAST_SKIP_CACHE(name, gb, num) ;
545

    
546
#   define SHOW_UBITS(name, gb, num)\
547
        NEG_USR32(name##_cache, num)
548

    
549
#   define SHOW_SBITS(name, gb, num)\
550
        NEG_SSR32(name##_cache, num)
551

    
552
#   define GET_CACHE(name, gb)\
553
        ((uint32_t)name##_cache)
554

    
555
static inline int get_bits_count(GetBitContext *s){
556
    return s->index;
557
}
558
#elif defined LIBMPEG2_BITSTREAM_READER
559
//libmpeg2 like reader
560

    
561
#   define MIN_CACHE_BITS 16
562

    
563
#   define OPEN_READER(name, gb)\
564
        int name##_bit_count=(gb)->bit_count;\
565
        int name##_cache= (gb)->cache;\
566
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
567

    
568
#   define CLOSE_READER(name, gb)\
569
        (gb)->bit_count= name##_bit_count;\
570
        (gb)->cache= name##_cache;\
571
        (gb)->buffer_ptr= name##_buffer_ptr;\
572

    
573
#   define UPDATE_CACHE(name, gb)\
574
    if(name##_bit_count > 0){\
575
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
576
        name##_buffer_ptr+=2;\
577
        name##_bit_count-= 16;\
578
    }\
579

    
580
#   define SKIP_CACHE(name, gb, num)\
581
        name##_cache <<= (num);\
582

    
583
#   define SKIP_COUNTER(name, gb, num)\
584
        name##_bit_count += (num);\
585

    
586
#   define SKIP_BITS(name, gb, num)\
587
        {\
588
            SKIP_CACHE(name, gb, num)\
589
            SKIP_COUNTER(name, gb, num)\
590
        }\
591

    
592
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
593
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
594

    
595
#   define SHOW_UBITS(name, gb, num)\
596
        NEG_USR32(name##_cache, num)
597

    
598
#   define SHOW_SBITS(name, gb, num)\
599
        NEG_SSR32(name##_cache, num)
600

    
601
#   define GET_CACHE(name, gb)\
602
        ((uint32_t)name##_cache)
603

    
604
static inline int get_bits_count(GetBitContext *s){
605
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
606
}
607

    
608
#elif defined A32_BITSTREAM_READER
609

    
610
#   define MIN_CACHE_BITS 32
611

    
612
#   define OPEN_READER(name, gb)\
613
        int name##_bit_count=(gb)->bit_count;\
614
        uint32_t name##_cache0= (gb)->cache0;\
615
        uint32_t name##_cache1= (gb)->cache1;\
616
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
617

    
618
#   define CLOSE_READER(name, gb)\
619
        (gb)->bit_count= name##_bit_count;\
620
        (gb)->cache0= name##_cache0;\
621
        (gb)->cache1= name##_cache1;\
622
        (gb)->buffer_ptr= name##_buffer_ptr;\
623

    
624
#   define UPDATE_CACHE(name, gb)\
625
    if(name##_bit_count > 0){\
626
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
627
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
628
        name##_cache1 |= next<<name##_bit_count;\
629
        name##_buffer_ptr++;\
630
        name##_bit_count-= 32;\
631
    }\
632

    
633
#ifdef ARCH_X86
634
#   define SKIP_CACHE(name, gb, num)\
635
        asm(\
636
            "shldl %2, %1, %0                \n\t"\
637
            "shll %2, %1                \n\t"\
638
            : "+r" (name##_cache0), "+r" (name##_cache1)\
639
            : "Ic" ((uint8_t)num)\
640
           );
641
#else
642
#   define SKIP_CACHE(name, gb, num)\
643
        name##_cache0 <<= (num);\
644
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
645
        name##_cache1 <<= (num);
646
#endif
647

    
648
#   define SKIP_COUNTER(name, gb, num)\
649
        name##_bit_count += (num);\
650

    
651
#   define SKIP_BITS(name, gb, num)\
652
        {\
653
            SKIP_CACHE(name, gb, num)\
654
            SKIP_COUNTER(name, gb, num)\
655
        }\
656

    
657
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
658
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
659

    
660
#   define SHOW_UBITS(name, gb, num)\
661
        NEG_USR32(name##_cache0, num)
662

    
663
#   define SHOW_SBITS(name, gb, num)\
664
        NEG_SSR32(name##_cache0, num)
665

    
666
#   define GET_CACHE(name, gb)\
667
        (name##_cache0)
668

    
669
static inline int get_bits_count(GetBitContext *s){
670
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
671
}
672

    
673
#endif
674

    
675
static inline unsigned int get_bits(GetBitContext *s, int n){
676
    register int tmp;
677
    OPEN_READER(re, s)
678
    UPDATE_CACHE(re, s)
679
    tmp= SHOW_UBITS(re, s, n);
680
    LAST_SKIP_BITS(re, s, n)
681
    CLOSE_READER(re, s)
682
    return tmp;
683
}
684

    
685
static inline unsigned int show_bits(GetBitContext *s, int n){
686
    register int tmp;
687
    OPEN_READER(re, s)
688
    UPDATE_CACHE(re, s)
689
    tmp= SHOW_UBITS(re, s, n);
690
//    CLOSE_READER(re, s)
691
    return tmp;
692
}
693

    
694
static inline void skip_bits(GetBitContext *s, int n){
695
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
696
    OPEN_READER(re, s)
697
    UPDATE_CACHE(re, s)
698
    LAST_SKIP_BITS(re, s, n)
699
    CLOSE_READER(re, s)
700
}
701

    
702
static inline unsigned int get_bits1(GetBitContext *s){
703
#ifdef ALT_BITSTREAM_READER
704
    int index= s->index;
705
    uint8_t result= s->buffer[ index>>3 ];
706
    result<<= (index&0x07);
707
    result>>= 8 - 1;
708
    index++;
709
    s->index= index;
710

    
711
    return result;
712
#else
713
    return get_bits(s, 1);
714
#endif
715
}
716

    
717
static inline unsigned int show_bits1(GetBitContext *s){
718
    return show_bits(s, 1);
719
}
720

    
721
static inline void skip_bits1(GetBitContext *s){
722
    skip_bits(s, 1);
723
}
724

    
725
void init_get_bits(GetBitContext *s,
726
                   UINT8 *buffer, int buffer_size);
727

    
728
int check_marker(GetBitContext *s, char *msg);
729
void align_get_bits(GetBitContext *s);
730
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
731
             const void *bits, int bits_wrap, int bits_size,
732
             const void *codes, int codes_wrap, int codes_size);
733
void free_vlc(VLC *vlc);
734

    
735
//note table will be trashed (pointer increased)
736
#define GET_VLC(code, name, gb, table, bits, max_depth)\
737
{\
738
    int n, index, nb_bits;\
739
\
740
    index= SHOW_UBITS(name, gb, bits);\
741
    code = table[index][0];\
742
    n    = table[index][1];\
743
\
744
    if(max_depth > 1 && n < 0){\
745
        LAST_SKIP_BITS(name, gb, bits)\
746
        UPDATE_CACHE(name, gb)\
747
\
748
        nb_bits = -n;\
749
        table += code;\
750
\
751
        index= SHOW_UBITS(name, gb, nb_bits);\
752
        code = table[index][0];\
753
        n    = table[index][1];\
754
        if(max_depth > 2 && n < 0){\
755
            LAST_SKIP_BITS(name, gb, nb_bits)\
756
            UPDATE_CACHE(name, gb)\
757
\
758
            nb_bits = -n;\
759
            table += code;\
760
\
761
            index= SHOW_UBITS(name, gb, nb_bits);\
762
            code = table[index][0];\
763
            n    = table[index][1];\
764
        }\
765
    }\
766
    SKIP_BITS(name, gb, n)\
767
}
768

    
769
static inline int get_vlc(GetBitContext *s, VLC *vlc)
770
{
771
    int code;
772
    VLC_TYPE (*table)[2]= vlc->table;
773
    
774
    OPEN_READER(re, s)
775
    UPDATE_CACHE(re, s)
776

    
777
    GET_VLC(code, re, s, table, vlc->bits, 3)    
778

    
779
    CLOSE_READER(re, s)
780
    return code;
781
}
782

    
783
/* define it to include statistics code (useful only for optimizing
784
   codec efficiency */
785
//#define STATS
786

    
787
#ifdef STATS
788

    
789
enum {
790
    ST_UNKNOWN,
791
    ST_DC,
792
    ST_INTRA_AC,
793
    ST_INTER_AC,
794
    ST_INTRA_MB,
795
    ST_INTER_MB,
796
    ST_MV,
797
    ST_NB,
798
};
799

    
800
extern int st_current_index;
801
extern unsigned int st_bit_counts[ST_NB];
802
extern unsigned int st_out_bit_counts[ST_NB];
803

    
804
void print_stats(void);
805
#endif
806

    
807
/* misc math functions */
808

    
809
static inline int av_log2(unsigned int v)
810
{
811
    int n;
812

    
813
    n = 0;
814
    if (v & 0xffff0000) {
815
        v >>= 16;
816
        n += 16;
817
    }
818
    if (v & 0xff00) {
819
        v >>= 8;
820
        n += 8;
821
    }
822
    if (v & 0xf0) {
823
        v >>= 4;
824
        n += 4;
825
    }
826
    if (v & 0xc) {
827
        v >>= 2;
828
        n += 2;
829
    }
830
    if (v & 0x2) {
831
        n++;
832
    }
833
    return n;
834
}
835

    
836
/* median of 3 */
837
static inline int mid_pred(int a, int b, int c)
838
{
839
    int vmin, vmax;
840
    vmax = vmin = a;
841
    if (b < vmin)
842
        vmin = b;
843
    else
844
        vmax = b;
845

    
846
    if (c < vmin)
847
        vmin = c;
848
    else if (c > vmax)
849
        vmax = c;
850

    
851
    return a + b + c - vmin - vmax;
852
}
853

    
854
static inline int clip(int a, int amin, int amax)
855
{
856
    if (a < amin)
857
        return amin;
858
    else if (a > amax)
859
        return amax;
860
    else
861
        return a;
862
}
863

    
864
/* math */
865
int ff_gcd(int a, int b);
866

    
867
static inline int ff_sqrt(int a)
868
{
869
    int ret=0;
870
    int s;
871
    int ret_sq=0;
872

    
873
    for(s=15; s>=0; s--){
874
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
875
        if(b<=a){
876
            ret_sq=b;
877
            ret+= 1<<s;
878
        }
879
    }
880
    return ret;
881
}
882
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
883
#define COPY3_IF_LT(x,y,a,b,c,d)\
884
asm volatile (\
885
    "cmpl %0, %3        \n\t"\
886
    "cmovl %3, %0        \n\t"\
887
    "cmovl %4, %1        \n\t"\
888
    "cmovl %5, %2        \n\t"\
889
    : "+r" (x), "+r" (a), "+r" (c)\
890
    : "r" (y), "r" (b), "r" (d)\
891
);
892
#else
893
#define COPY3_IF_LT(x,y,a,b,c,d)\
894
if((y)<(x)){\
895
     (x)=(y);\
896
     (a)=(b);\
897
     (c)=(d);\
898
}
899
#endif
900

    
901
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
902

    
903
#endif /* HAVE_AV_CONFIG_H */
904

    
905
#endif /* COMMON_H */