Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ f3ec2d46

History | View | Annotate | Download (21.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 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
#ifndef M_PI
37
#define M_PI    3.14159265358979323846
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
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
48
#    define always_inline __attribute__((always_inline)) inline
49
#else
50
#    define always_inline inline
51
#endif
52

    
53
#ifdef CONFIG_WIN32
54

    
55
/* windows */
56

    
57
typedef unsigned short UINT16;
58
typedef signed short INT16;
59
typedef unsigned char UINT8;
60
typedef unsigned int UINT32;
61
typedef unsigned __int64 UINT64;
62
typedef signed char INT8;
63
typedef signed int INT32;
64
typedef signed __int64 INT64;
65

    
66
typedef UINT8 uint8_t;
67
typedef INT8 int8_t;
68
typedef UINT16 uint16_t;
69
typedef INT16 int16_t;
70
typedef UINT32 uint32_t;
71
typedef INT32 int32_t;
72
typedef UINT64 uint64_t;
73
typedef INT64 int64_t;
74

    
75
#    ifndef __MINGW32__
76
#        define INT64_C(c)     (c ## i64)
77
#        define UINT64_C(c)    (c ## i64)
78

    
79
#        define inline __inline
80

    
81
#    else
82
#        define INT64_C(c)     (c ## LL)
83
#        define UINT64_C(c)    (c ## ULL)
84
#    endif /* __MINGW32__ */
85

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

    
90
#    define snprintf _snprintf
91

    
92
/* CONFIG_WIN32 end */
93
#elif defined (CONFIG_OS2)
94
/* OS/2 EMX */
95

    
96
#include <inttypes.h>
97

    
98
typedef unsigned char UINT8;
99
typedef unsigned short UINT16;
100
typedef unsigned int UINT32;
101
typedef unsigned long long UINT64;
102
typedef signed char INT8;
103
typedef signed short INT16;
104
typedef signed int INT32;
105
typedef signed long long INT64;
106

    
107
#ifdef HAVE_AV_CONFIG_H
108

    
109
#ifndef INT64_C
110
#define INT64_C(c)     (c ## LL)
111
#define UINT64_C(c)    (c ## ULL)
112
#endif
113

    
114
#ifdef USE_FASTMEMCPY
115
#include "fastmemcpy.h"
116
#endif
117

    
118
#include <float.h>
119

    
120
#endif /* HAVE_AV_CONFIG_H */
121

    
122
/* CONFIG_OS2 end */
123
#else
124

    
125
/* unix */
126

    
127
#    include <inttypes.h>
128

    
129
#    ifndef __WINE_WINDEF16_H
130
/* workaround for typedef conflict in MPlayer (wine typedefs) */
131
typedef unsigned short UINT16;
132
typedef signed short INT16;
133
#    endif
134

    
135
typedef unsigned char UINT8;
136
typedef unsigned int UINT32;
137
typedef unsigned long long UINT64;
138
typedef signed char INT8;
139
typedef signed int INT32;
140
typedef signed long long INT64;
141

    
142
#    ifdef HAVE_AV_CONFIG_H
143
#        ifndef INT64_C
144
#            define INT64_C(c)     (c ## LL)
145
#            define UINT64_C(c)    (c ## ULL)
146
#        endif
147

    
148
#        ifdef USE_FASTMEMCPY
149
#            include "fastmemcpy.h"
150
#        endif
151
#    endif /* HAVE_AV_CONFIG_H */
152

    
153
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
154

    
155
#ifdef HAVE_AV_CONFIG_H
156

    
157
#    include "bswap.h"
158

    
159
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
160
        defined(__OS2__) || defined (__OpenBSD__)
161
#        define MANGLE(a) "_" #a
162
#    else
163
#        define MANGLE(a) #a
164
#    endif
165

    
166
/* debug stuff */
167

    
168
#    ifndef DEBUG
169
#        define NDEBUG
170
#    endif
171
#    include <assert.h>
172

    
173
/* dprintf macros */
174
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
175

    
176
inline void dprintf(const char* fmt,...) {}
177

    
178
#    else
179

    
180
#        ifdef DEBUG
181
#            define dprintf(fmt,args...) printf(fmt, ## args)
182
#        else
183
#            define dprintf(fmt,args...)
184
#        endif
185

    
186
#    endif /* !CONFIG_WIN32 */
187

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

    
190
//rounded divison & shift
191
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
192
/* assume b>0 */
193
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
194
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
195

    
196
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
197
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
198

    
199
#ifdef ARCH_X86
200
// avoid +32 for shift optimization (gcc should do that ...)
201
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
202
    asm ("sarl %1, %0\n\t"
203
         : "+r" (a)
204
         : "ic" ((uint8_t)(-s))
205
    );
206
    return a;
207
}
208
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
209
    asm ("shrl %1, %0\n\t"
210
         : "+r" (a)
211
         : "ic" ((uint8_t)(-s))
212
    );
213
    return a;
214
}
215
#else
216
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
217
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
218
#endif
219

    
220
/* bit output */
221

    
222
struct PutBitContext;
223

    
224
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
225

    
226
typedef struct PutBitContext {
227
#ifdef ALT_BITSTREAM_WRITER
228
    UINT8 *buf, *buf_end;
229
    int index;
230
#else
231
    UINT32 bit_buf;
232
    int bit_left;
233
    UINT8 *buf, *buf_ptr, *buf_end;
234
#endif
235
    INT64 data_out_size; /* in bytes */
236
} PutBitContext;
237

    
238
void init_put_bits(PutBitContext *s, 
239
                   UINT8 *buffer, int buffer_size,
240
                   void *opaque,
241
                   void (*write_data)(void *, UINT8 *, int));
242

    
243
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
244
void align_put_bits(PutBitContext *s);
245
void flush_put_bits(PutBitContext *s);
246
void put_string(PutBitContext * pbc, char *s);
247

    
248
/* bit input */
249

    
250
typedef struct GetBitContext {
251
    UINT8 *buffer, *buffer_end;
252
#ifdef ALT_BITSTREAM_READER
253
    int index;
254
#elif defined LIBMPEG2_BITSTREAM_READER
255
    UINT8 *buffer_ptr;
256
    UINT32 cache;
257
    int bit_count;
258
#elif defined A32_BITSTREAM_READER
259
    UINT32 *buffer_ptr;
260
    UINT32 cache0;
261
    UINT32 cache1;
262
    int bit_count;
263
#endif
264
    int size;
265
} GetBitContext;
266

    
267
static inline int get_bits_count(GetBitContext *s);
268

    
269
#define VLC_TYPE INT16
270

    
271
typedef struct VLC {
272
    int bits;
273
    VLC_TYPE (*table)[2]; // code, bits
274
    int table_size, table_allocated;
275
} VLC;
276

    
277
typedef struct RL_VLC_ELEM {
278
    int16_t level;
279
    int8_t len;
280
    uint8_t run;
281
} RL_VLC_ELEM;
282

    
283
#ifdef ARCH_SPARC64
284
#define UNALIGNED_STORES_ARE_BAD
285
#endif
286

    
287
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
288
#ifdef ARCH_X86
289
#    define unaligned32(a) (*(UINT32*)(a))
290
#else
291
#    ifdef __GNUC__
292
static inline uint32_t unaligned32(const void *v) {
293
    struct Unaligned {
294
        uint32_t i;
295
    } __attribute__((packed));
296

    
297
    return ((const struct Unaligned *) v)->i;
298
}
299
#    elif defined(__DECC)
300
static inline uint32_t unaligned32(const void *v) {
301
    return *(const __unaligned uint32_t *) v;
302
}
303
#    else
304
static inline uint32_t unaligned32(const void *v) {
305
    return *(const uint32_t *) v;
306
}
307
#    endif
308
#endif //!ARCH_X86
309

    
310
#ifndef ALT_BITSTREAM_WRITER
311
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
312
{
313
    unsigned int bit_buf;
314
    int bit_left;
315

    
316
#ifdef STATS
317
    st_out_bit_counts[st_current_index] += n;
318
#endif
319
    //    printf("put_bits=%d %x\n", n, value);
320
    assert(n == 32 || value < (1U << n));
321
    
322
    bit_buf = s->bit_buf;
323
    bit_left = s->bit_left;
324

    
325
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
326
    /* XXX: optimize */
327
    if (n < bit_left) {
328
        bit_buf = (bit_buf<<n) | value;
329
        bit_left-=n;
330
    } else {
331
        bit_buf<<=bit_left;
332
        bit_buf |= value >> (n - bit_left);
333
#ifdef UNALIGNED_STORES_ARE_BAD
334
        if (3 & (int) s->buf_ptr) {
335
            s->buf_ptr[0] = bit_buf >> 24;
336
            s->buf_ptr[1] = bit_buf >> 16;
337
            s->buf_ptr[2] = bit_buf >>  8;
338
            s->buf_ptr[3] = bit_buf      ;
339
        } else
340
#endif
341
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
342
        //printf("bitbuf = %08x\n", bit_buf);
343
        s->buf_ptr+=4;
344
        bit_left+=32 - n;
345
        bit_buf = value;
346
    }
347

    
348
    s->bit_buf = bit_buf;
349
    s->bit_left = bit_left;
350
}
351
#endif
352

    
353

    
354
#ifdef ALT_BITSTREAM_WRITER
355
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
356
{
357
#    ifdef ALIGNED_BITSTREAM_WRITER
358
#        ifdef ARCH_X86
359
    asm volatile(
360
        "movl %0, %%ecx                        \n\t"
361
        "xorl %%eax, %%eax                \n\t"
362
        "shrdl %%cl, %1, %%eax                \n\t"
363
        "shrl %%cl, %1                        \n\t"
364
        "movl %0, %%ecx                        \n\t"
365
        "shrl $3, %%ecx                        \n\t"
366
        "andl $0xFFFFFFFC, %%ecx        \n\t"
367
        "bswapl %1                        \n\t"
368
        "orl %1, (%2, %%ecx)                \n\t"
369
        "bswapl %%eax                        \n\t"
370
        "addl %3, %0                        \n\t"
371
        "movl %%eax, 4(%2, %%ecx)        \n\t"
372
        : "=&r" (s->index), "=&r" (value)
373
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
374
        : "%eax", "%ecx"
375
    );
376
#        else
377
    int index= s->index;
378
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
379
    
380
    value<<= 32-n; 
381
    
382
    ptr[0] |= be2me_32(value>>(index&31));
383
    ptr[1]  = be2me_32(value<<(32-(index&31)));
384
//if(n>24) printf("%d %d\n", n, value);
385
    index+= n;
386
    s->index= index;
387
#        endif
388
#    else //ALIGNED_BITSTREAM_WRITER
389
#        ifdef ARCH_X86
390
    asm volatile(
391
        "movl $7, %%ecx                        \n\t"
392
        "andl %0, %%ecx                        \n\t"
393
        "addl %3, %%ecx                        \n\t"
394
        "negl %%ecx                        \n\t"
395
        "shll %%cl, %1                        \n\t"
396
        "bswapl %1                        \n\t"
397
        "movl %0, %%ecx                        \n\t"
398
        "shrl $3, %%ecx                        \n\t"
399
        "orl %1, (%%ecx, %2)                \n\t"
400
        "addl %3, %0                        \n\t"
401
        "movl $0, 4(%%ecx, %2)                \n\t"
402
        : "=&r" (s->index), "=&r" (value)
403
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
404
        : "%ecx"
405
    );
406
#        else
407
    int index= s->index;
408
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
409
    
410
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
411
    ptr[1] = 0;
412
//if(n>24) printf("%d %d\n", n, value);
413
    index+= n;
414
    s->index= index;
415
#        endif
416
#    endif //!ALIGNED_BITSTREAM_WRITER
417
}
418
#endif
419

    
420

    
421
static inline uint8_t* pbBufPtr(PutBitContext *s)
422
{
423
#ifdef ALT_BITSTREAM_WRITER
424
        return s->buf + (s->index>>3);
425
#else
426
        return s->buf_ptr;
427
#endif
428
}
429

    
430
/* Bitstream reader API docs:
431
name
432
    abritary name which is used as prefix for the internal variables
433

434
gb
435
    getbitcontext
436

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

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

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

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

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

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

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

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

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

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

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

472
for examples see get_bits, show_bits, skip_bits, get_vlc
473
*/
474

    
475
#ifdef ALT_BITSTREAM_READER
476
#   define MIN_CACHE_BITS 25
477

    
478
#   define OPEN_READER(name, gb)\
479
        int name##_index= (gb)->index;\
480
        int name##_cache= 0;\
481

    
482
#   define CLOSE_READER(name, gb)\
483
        (gb)->index= name##_index;\
484

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

    
488
#   define SKIP_CACHE(name, gb, num)\
489
        name##_cache <<= (num);\
490

    
491
// FIXME name?
492
#   define SKIP_COUNTER(name, gb, num)\
493
        name##_index += (num);\
494

    
495
#   define SKIP_BITS(name, gb, num)\
496
        {\
497
            SKIP_CACHE(name, gb, num)\
498
            SKIP_COUNTER(name, gb, num)\
499
        }\
500

    
501
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
502
#   define LAST_SKIP_CACHE(name, gb, num) ;
503

    
504
#   define SHOW_UBITS(name, gb, num)\
505
        NEG_USR32(name##_cache, num)
506

    
507
#   define SHOW_SBITS(name, gb, num)\
508
        NEG_SSR32(name##_cache, num)
509

    
510
#   define GET_CACHE(name, gb)\
511
        ((uint32_t)name##_cache)
512

    
513
static inline int get_bits_count(GetBitContext *s){
514
    return s->index;
515
}
516
#elif defined LIBMPEG2_BITSTREAM_READER
517
//libmpeg2 like reader
518

    
519
#   define MIN_CACHE_BITS 16
520

    
521
#   define OPEN_READER(name, gb)\
522
        int name##_bit_count=(gb)->bit_count;\
523
        int name##_cache= (gb)->cache;\
524
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
525

    
526
#   define CLOSE_READER(name, gb)\
527
        (gb)->bit_count= name##_bit_count;\
528
        (gb)->cache= name##_cache;\
529
        (gb)->buffer_ptr= name##_buffer_ptr;\
530

    
531
#   define UPDATE_CACHE(name, gb)\
532
    if(name##_bit_count > 0){\
533
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
534
        name##_buffer_ptr+=2;\
535
        name##_bit_count-= 16;\
536
    }\
537

    
538
#   define SKIP_CACHE(name, gb, num)\
539
        name##_cache <<= (num);\
540

    
541
#   define SKIP_COUNTER(name, gb, num)\
542
        name##_bit_count += (num);\
543

    
544
#   define SKIP_BITS(name, gb, num)\
545
        {\
546
            SKIP_CACHE(name, gb, num)\
547
            SKIP_COUNTER(name, gb, num)\
548
        }\
549

    
550
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
551
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
552

    
553
#   define SHOW_UBITS(name, gb, num)\
554
        NEG_USR32(name##_cache, num)
555

    
556
#   define SHOW_SBITS(name, gb, num)\
557
        NEG_SSR32(name##_cache, num)
558

    
559
#   define GET_CACHE(name, gb)\
560
        ((uint32_t)name##_cache)
561

    
562
static inline int get_bits_count(GetBitContext *s){
563
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
564
}
565

    
566
#elif defined A32_BITSTREAM_READER
567

    
568
#   define MIN_CACHE_BITS 32
569

    
570
#   define OPEN_READER(name, gb)\
571
        int name##_bit_count=(gb)->bit_count;\
572
        uint32_t name##_cache0= (gb)->cache0;\
573
        uint32_t name##_cache1= (gb)->cache1;\
574
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
575

    
576
#   define CLOSE_READER(name, gb)\
577
        (gb)->bit_count= name##_bit_count;\
578
        (gb)->cache0= name##_cache0;\
579
        (gb)->cache1= name##_cache1;\
580
        (gb)->buffer_ptr= name##_buffer_ptr;\
581

    
582
#   define UPDATE_CACHE(name, gb)\
583
    if(name##_bit_count > 0){\
584
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
585
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
586
        name##_cache1 |= next<<name##_bit_count;\
587
        name##_buffer_ptr++;\
588
        name##_bit_count-= 32;\
589
    }\
590

    
591
#ifdef ARCH_X86
592
#   define SKIP_CACHE(name, gb, num)\
593
        asm(\
594
            "shldl %2, %1, %0                \n\t"\
595
            "shll %2, %1                \n\t"\
596
            : "+r" (name##_cache0), "+r" (name##_cache1)\
597
            : "Ic" ((uint8_t)num)\
598
           );
599
#else
600
#   define SKIP_CACHE(name, gb, num)\
601
        name##_cache0 <<= (num);\
602
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
603
        name##_cache1 <<= (num);
604
#endif
605

    
606
#   define SKIP_COUNTER(name, gb, num)\
607
        name##_bit_count += (num);\
608

    
609
#   define SKIP_BITS(name, gb, num)\
610
        {\
611
            SKIP_CACHE(name, gb, num)\
612
            SKIP_COUNTER(name, gb, num)\
613
        }\
614

    
615
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
616
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
617

    
618
#   define SHOW_UBITS(name, gb, num)\
619
        NEG_USR32(name##_cache0, num)
620

    
621
#   define SHOW_SBITS(name, gb, num)\
622
        NEG_SSR32(name##_cache0, num)
623

    
624
#   define GET_CACHE(name, gb)\
625
        (name##_cache0)
626

    
627
static inline int get_bits_count(GetBitContext *s){
628
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
629
}
630

    
631
#endif
632

    
633
static inline unsigned int get_bits(GetBitContext *s, int n){
634
    register int tmp;
635
    OPEN_READER(re, s)
636
    UPDATE_CACHE(re, s)
637
    tmp= SHOW_UBITS(re, s, n);
638
    LAST_SKIP_BITS(re, s, n)
639
    CLOSE_READER(re, s)
640
    return tmp;
641
}
642

    
643
static inline unsigned int show_bits(GetBitContext *s, int n){
644
    register int tmp;
645
    OPEN_READER(re, s)
646
    UPDATE_CACHE(re, s)
647
    tmp= SHOW_UBITS(re, s, n);
648
//    CLOSE_READER(re, s)
649
    return tmp;
650
}
651

    
652
static inline void skip_bits(GetBitContext *s, int n){
653
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
654
    OPEN_READER(re, s)
655
    UPDATE_CACHE(re, s)
656
    LAST_SKIP_BITS(re, s, n)
657
    CLOSE_READER(re, s)
658
}
659

    
660
static inline unsigned int get_bits1(GetBitContext *s){
661
#ifdef ALT_BITSTREAM_READER
662
    int index= s->index;
663
    uint8_t result= s->buffer[ index>>3 ];
664
    result<<= (index&0x07);
665
    result>>= 8 - 1;
666
    index++;
667
    s->index= index;
668

    
669
    return result;
670
#else
671
    return get_bits(s, 1);
672
#endif
673
}
674

    
675
static inline unsigned int show_bits1(GetBitContext *s){
676
    return show_bits(s, 1);
677
}
678

    
679
static inline void skip_bits1(GetBitContext *s){
680
    skip_bits(s, 1);
681
}
682

    
683
void init_get_bits(GetBitContext *s,
684
                   UINT8 *buffer, int buffer_size);
685

    
686
int check_marker(GetBitContext *s, const char *msg);
687
void align_get_bits(GetBitContext *s);
688
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
689
             const void *bits, int bits_wrap, int bits_size,
690
             const void *codes, int codes_wrap, int codes_size);
691
void free_vlc(VLC *vlc);
692

    
693
#define GET_VLC(code, name, gb, table, bits, max_depth)\
694
{\
695
    int n, index, nb_bits;\
696
\
697
    index= SHOW_UBITS(name, gb, bits);\
698
    code = table[index][0];\
699
    n    = table[index][1];\
700
\
701
    if(max_depth > 1 && n < 0){\
702
        LAST_SKIP_BITS(name, gb, bits)\
703
        UPDATE_CACHE(name, gb)\
704
\
705
        nb_bits = -n;\
706
\
707
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
708
        code = table[index][0];\
709
        n    = table[index][1];\
710
        if(max_depth > 2 && n < 0){\
711
            LAST_SKIP_BITS(name, gb, nb_bits)\
712
            UPDATE_CACHE(name, gb)\
713
\
714
            nb_bits = -n;\
715
\
716
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
717
            code = table[index][0];\
718
            n    = table[index][1];\
719
        }\
720
    }\
721
    SKIP_BITS(name, gb, n)\
722
}
723

    
724
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
725
{\
726
    int n, index, nb_bits;\
727
\
728
    index= SHOW_UBITS(name, gb, bits);\
729
    level = table[index].level;\
730
    n     = table[index].len;\
731
\
732
    if(max_depth > 1 && n < 0){\
733
        LAST_SKIP_BITS(name, gb, bits)\
734
        UPDATE_CACHE(name, gb)\
735
\
736
        nb_bits = -n;\
737
\
738
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
739
        level = table[index].level;\
740
        n     = table[index].len;\
741
    }\
742
    run= table[index].run;\
743
    SKIP_BITS(name, gb, n)\
744
}
745

    
746
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
747
static inline int get_vlc(GetBitContext *s, VLC *vlc)
748
{
749
    int code;
750
    VLC_TYPE (*table)[2]= vlc->table;
751
    
752
    OPEN_READER(re, s)
753
    UPDATE_CACHE(re, s)
754

    
755
    GET_VLC(code, re, s, table, vlc->bits, 3)    
756

    
757
    CLOSE_READER(re, s)
758
    return code;
759
}
760

    
761
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
762
                                  int bits, int max_depth)
763
{
764
    int code;
765
    
766
    OPEN_READER(re, s)
767
    UPDATE_CACHE(re, s)
768

    
769
    GET_VLC(code, re, s, table, bits, max_depth)
770

    
771
    CLOSE_READER(re, s)
772
    return code;
773
}
774

    
775

    
776
/* define it to include statistics code (useful only for optimizing
777
   codec efficiency */
778
//#define STATS
779

    
780
#ifdef STATS
781

    
782
enum {
783
    ST_UNKNOWN,
784
    ST_DC,
785
    ST_INTRA_AC,
786
    ST_INTER_AC,
787
    ST_INTRA_MB,
788
    ST_INTER_MB,
789
    ST_MV,
790
    ST_NB,
791
};
792

    
793
extern int st_current_index;
794
extern unsigned int st_bit_counts[ST_NB];
795
extern unsigned int st_out_bit_counts[ST_NB];
796

    
797
void print_stats(void);
798
#endif
799

    
800
/* misc math functions */
801

    
802
static inline int av_log2(unsigned int v)
803
{
804
    int n;
805

    
806
    n = 0;
807
    if (v & 0xffff0000) {
808
        v >>= 16;
809
        n += 16;
810
    }
811
    if (v & 0xff00) {
812
        v >>= 8;
813
        n += 8;
814
    }
815
    if (v & 0xf0) {
816
        v >>= 4;
817
        n += 4;
818
    }
819
    if (v & 0xc) {
820
        v >>= 2;
821
        n += 2;
822
    }
823
    if (v & 0x2) {
824
        n++;
825
    }
826
    return n;
827
}
828

    
829
/* median of 3 */
830
static inline int mid_pred(int a, int b, int c)
831
{
832
    int vmin, vmax;
833
    vmax = vmin = a;
834
    if (b < vmin)
835
        vmin = b;
836
    else
837
        vmax = b;
838

    
839
    if (c < vmin)
840
        vmin = c;
841
    else if (c > vmax)
842
        vmax = c;
843

    
844
    return a + b + c - vmin - vmax;
845
}
846

    
847
static inline int clip(int a, int amin, int amax)
848
{
849
    if (a < amin)
850
        return amin;
851
    else if (a > amax)
852
        return amax;
853
    else
854
        return a;
855
}
856

    
857
/* math */
858
extern const UINT8 ff_sqrt_tab[128];
859

    
860
int ff_gcd(int a, int b);
861

    
862
static inline int ff_sqrt(int a)
863
{
864
    int ret=0;
865
    int s;
866
    int ret_sq=0;
867
    
868
    if(a<128) return ff_sqrt_tab[a];
869
    
870
    for(s=15; s>=0; s--){
871
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
872
        if(b<=a){
873
            ret_sq=b;
874
            ret+= 1<<s;
875
        }
876
    }
877
    return ret;
878
}
879

    
880
/**
881
 * converts fourcc string to int
882
 */
883
static inline int ff_get_fourcc(const char *s){
884
    assert( strlen(s)==4 );
885
    
886
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
887
}
888

    
889
void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
890

    
891

    
892
#ifdef ARCH_X86
893
#define MASK_ABS(mask, level)\
894
            asm volatile(\
895
                "cdq                        \n\t"\
896
                "xorl %1, %0                \n\t"\
897
                "subl %1, %0                \n\t"\
898
                : "+a" (level), "=&d" (mask)\
899
            );
900
#else
901
#define MASK_ABS(mask, level)\
902
            mask= level>>31;\
903
            level= (level^mask)-mask;
904
#endif
905

    
906

    
907
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
908
#define COPY3_IF_LT(x,y,a,b,c,d)\
909
asm volatile (\
910
    "cmpl %0, %3        \n\t"\
911
    "cmovl %3, %0        \n\t"\
912
    "cmovl %4, %1        \n\t"\
913
    "cmovl %5, %2        \n\t"\
914
    : "+r" (x), "+r" (a), "+r" (c)\
915
    : "r" (y), "r" (b), "r" (d)\
916
);
917
#else
918
#define COPY3_IF_LT(x,y,a,b,c,d)\
919
if((y)<(x)){\
920
     (x)=(y);\
921
     (a)=(b);\
922
     (c)=(d);\
923
}
924
#endif
925

    
926
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
927

    
928
#endif /* HAVE_AV_CONFIG_H */
929

    
930
#endif /* COMMON_H */