Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ fe1b62fb

History | View | Annotate | Download (22.6 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
#include <stddef.h>
41
#ifndef offsetof
42
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
43
#endif
44

    
45
#define AVOPTION_CODEC_BOOL(name, help, field) \
46
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
47
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
48
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
49
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
50
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
51
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
52
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
53
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
54
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
55
#define AVOPTION_SUB(ptr) { .name = NULL, .sub = ptr }
56
#define AVOPTION_END() AVOPTION_SUB(NULL)
57

    
58
#endif /* HAVE_AV_CONFIG_H */
59

    
60
/* Suppress restrict if it was not defined in config.h.  */
61
#ifndef restrict
62
#    define restrict
63
#endif
64

    
65
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
66
#    define always_inline __attribute__((always_inline)) inline
67
#else
68
#    define always_inline inline
69
#endif
70

    
71
#ifdef CONFIG_WIN32
72

    
73
/* windows */
74

    
75
typedef unsigned short UINT16;
76
typedef signed short INT16;
77
typedef unsigned char UINT8;
78
typedef unsigned int UINT32;
79
typedef unsigned __int64 UINT64;
80
typedef signed char INT8;
81
typedef signed int INT32;
82
typedef signed __int64 INT64;
83

    
84
typedef UINT8 uint8_t;
85
typedef INT8 int8_t;
86
typedef UINT16 uint16_t;
87
typedef INT16 int16_t;
88
typedef UINT32 uint32_t;
89
typedef INT32 int32_t;
90
typedef UINT64 uint64_t;
91
typedef INT64 int64_t;
92

    
93
#    ifndef __MINGW32__
94
#        define INT64_C(c)     (c ## i64)
95
#        define UINT64_C(c)    (c ## i64)
96

    
97
#        define inline __inline
98

    
99
#    else
100
#        define INT64_C(c)     (c ## LL)
101
#        define UINT64_C(c)    (c ## ULL)
102
#    endif /* __MINGW32__ */
103

    
104
#    ifdef _DEBUG
105
#        define DEBUG
106
#    endif
107

    
108
#    define snprintf _snprintf
109
#    define vsnprintf _vsnprintf
110

    
111
/* CONFIG_WIN32 end */
112
#elif defined (CONFIG_OS2)
113
/* OS/2 EMX */
114

    
115
#include <inttypes.h>
116

    
117
typedef unsigned char UINT8;
118
typedef unsigned short UINT16;
119
typedef unsigned int UINT32;
120
typedef unsigned long long UINT64;
121
typedef signed char INT8;
122
typedef signed short INT16;
123
typedef signed int INT32;
124
typedef signed long long INT64;
125

    
126
#ifdef HAVE_AV_CONFIG_H
127

    
128
#ifndef INT64_C
129
#define INT64_C(c)     (c ## LL)
130
#define UINT64_C(c)    (c ## ULL)
131
#endif
132

    
133
#ifdef USE_FASTMEMCPY
134
#include "fastmemcpy.h"
135
#endif
136

    
137
#include <float.h>
138

    
139
#endif /* HAVE_AV_CONFIG_H */
140

    
141
/* CONFIG_OS2 end */
142
#else
143

    
144
/* unix */
145

    
146
#    include <inttypes.h>
147

    
148
#    ifndef __WINE_WINDEF16_H
149
/* workaround for typedef conflict in MPlayer (wine typedefs) */
150
typedef unsigned short UINT16;
151
typedef signed short INT16;
152
#    endif
153

    
154
typedef unsigned char UINT8;
155
typedef unsigned int UINT32;
156
typedef unsigned long long UINT64;
157
typedef signed char INT8;
158
typedef signed int INT32;
159
typedef signed long long INT64;
160

    
161
#    ifdef HAVE_AV_CONFIG_H
162
#        ifndef INT64_C
163
#            define INT64_C(c)     (c ## LL)
164
#            define UINT64_C(c)    (c ## ULL)
165
#        endif
166

    
167
#        ifdef USE_FASTMEMCPY
168
#            include "fastmemcpy.h"
169
#        endif
170
#    endif /* HAVE_AV_CONFIG_H */
171

    
172
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
173

    
174
#ifdef HAVE_AV_CONFIG_H
175

    
176
#    include "bswap.h"
177

    
178
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
179
        defined(__OS2__) || defined (__OpenBSD__)
180
#        define MANGLE(a) "_" #a
181
#    else
182
#        define MANGLE(a) #a
183
#    endif
184

    
185
/* debug stuff */
186

    
187
#    ifndef DEBUG
188
#        define NDEBUG
189
#    endif
190
#    include <assert.h>
191

    
192
/* dprintf macros */
193
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
194

    
195
inline void dprintf(const char* fmt,...) {}
196

    
197
#    else
198

    
199
#        ifdef DEBUG
200
#            define dprintf(fmt,args...) printf(fmt, ## args)
201
#        else
202
#            define dprintf(fmt,args...)
203
#        endif
204

    
205
#    endif /* !CONFIG_WIN32 */
206

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

    
209
//rounded divison & shift
210
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
211
/* assume b>0 */
212
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
213
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
214

    
215
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
216
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
217

    
218
#ifdef ARCH_X86
219
// avoid +32 for shift optimization (gcc should do that ...)
220
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
221
    asm ("sarl %1, %0\n\t"
222
         : "+r" (a)
223
         : "ic" ((uint8_t)(-s))
224
    );
225
    return a;
226
}
227
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
228
    asm ("shrl %1, %0\n\t"
229
         : "+r" (a)
230
         : "ic" ((uint8_t)(-s))
231
    );
232
    return a;
233
}
234
#else
235
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
236
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
237
#endif
238

    
239
/* bit output */
240

    
241
struct PutBitContext;
242

    
243
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
244

    
245
typedef struct PutBitContext {
246
#ifdef ALT_BITSTREAM_WRITER
247
    UINT8 *buf, *buf_end;
248
    int index;
249
#else
250
    UINT32 bit_buf;
251
    int bit_left;
252
    UINT8 *buf, *buf_ptr, *buf_end;
253
#endif
254
    INT64 data_out_size; /* in bytes */
255
} PutBitContext;
256

    
257
void init_put_bits(PutBitContext *s, 
258
                   UINT8 *buffer, int buffer_size,
259
                   void *opaque,
260
                   void (*write_data)(void *, UINT8 *, int));
261

    
262
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
263
void align_put_bits(PutBitContext *s);
264
void flush_put_bits(PutBitContext *s);
265
void put_string(PutBitContext * pbc, char *s);
266

    
267
/* bit input */
268

    
269
typedef struct GetBitContext {
270
    UINT8 *buffer, *buffer_end;
271
#ifdef ALT_BITSTREAM_READER
272
    int index;
273
#elif defined LIBMPEG2_BITSTREAM_READER
274
    UINT8 *buffer_ptr;
275
    UINT32 cache;
276
    int bit_count;
277
#elif defined A32_BITSTREAM_READER
278
    UINT32 *buffer_ptr;
279
    UINT32 cache0;
280
    UINT32 cache1;
281
    int bit_count;
282
#endif
283
    int size_in_bits;
284
} GetBitContext;
285

    
286
static inline int get_bits_count(GetBitContext *s);
287

    
288
#define VLC_TYPE INT16
289

    
290
typedef struct VLC {
291
    int bits;
292
    VLC_TYPE (*table)[2]; // code, bits
293
    int table_size, table_allocated;
294
} VLC;
295

    
296
typedef struct RL_VLC_ELEM {
297
    int16_t level;
298
    int8_t len;
299
    uint8_t run;
300
} RL_VLC_ELEM;
301

    
302
#ifdef ARCH_SPARC64
303
#define UNALIGNED_STORES_ARE_BAD
304
#endif
305

    
306
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
307
#ifdef ARCH_X86
308
#    define unaligned32(a) (*(UINT32*)(a))
309
#else
310
#    ifdef __GNUC__
311
static inline uint32_t unaligned32(const void *v) {
312
    struct Unaligned {
313
        uint32_t i;
314
    } __attribute__((packed));
315

    
316
    return ((const struct Unaligned *) v)->i;
317
}
318
#    elif defined(__DECC)
319
static inline uint32_t unaligned32(const void *v) {
320
    return *(const __unaligned uint32_t *) v;
321
}
322
#    else
323
static inline uint32_t unaligned32(const void *v) {
324
    return *(const uint32_t *) v;
325
}
326
#    endif
327
#endif //!ARCH_X86
328

    
329
#ifndef ALT_BITSTREAM_WRITER
330
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
331
{
332
    unsigned int bit_buf;
333
    int bit_left;
334

    
335
#ifdef STATS
336
    st_out_bit_counts[st_current_index] += n;
337
#endif
338
    //    printf("put_bits=%d %x\n", n, value);
339
    assert(n == 32 || value < (1U << n));
340
    
341
    bit_buf = s->bit_buf;
342
    bit_left = s->bit_left;
343

    
344
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
345
    /* XXX: optimize */
346
    if (n < bit_left) {
347
        bit_buf = (bit_buf<<n) | value;
348
        bit_left-=n;
349
    } else {
350
        bit_buf<<=bit_left;
351
        bit_buf |= value >> (n - bit_left);
352
#ifdef UNALIGNED_STORES_ARE_BAD
353
        if (3 & (int) s->buf_ptr) {
354
            s->buf_ptr[0] = bit_buf >> 24;
355
            s->buf_ptr[1] = bit_buf >> 16;
356
            s->buf_ptr[2] = bit_buf >>  8;
357
            s->buf_ptr[3] = bit_buf      ;
358
        } else
359
#endif
360
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
361
        //printf("bitbuf = %08x\n", bit_buf);
362
        s->buf_ptr+=4;
363
        bit_left+=32 - n;
364
        bit_buf = value;
365
    }
366

    
367
    s->bit_buf = bit_buf;
368
    s->bit_left = bit_left;
369
}
370
#endif
371

    
372

    
373
#ifdef ALT_BITSTREAM_WRITER
374
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
375
{
376
#    ifdef ALIGNED_BITSTREAM_WRITER
377
#        ifdef ARCH_X86
378
    asm volatile(
379
        "movl %0, %%ecx                        \n\t"
380
        "xorl %%eax, %%eax                \n\t"
381
        "shrdl %%cl, %1, %%eax                \n\t"
382
        "shrl %%cl, %1                        \n\t"
383
        "movl %0, %%ecx                        \n\t"
384
        "shrl $3, %%ecx                        \n\t"
385
        "andl $0xFFFFFFFC, %%ecx        \n\t"
386
        "bswapl %1                        \n\t"
387
        "orl %1, (%2, %%ecx)                \n\t"
388
        "bswapl %%eax                        \n\t"
389
        "addl %3, %0                        \n\t"
390
        "movl %%eax, 4(%2, %%ecx)        \n\t"
391
        : "=&r" (s->index), "=&r" (value)
392
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
393
        : "%eax", "%ecx"
394
    );
395
#        else
396
    int index= s->index;
397
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
398
    
399
    value<<= 32-n; 
400
    
401
    ptr[0] |= be2me_32(value>>(index&31));
402
    ptr[1]  = be2me_32(value<<(32-(index&31)));
403
//if(n>24) printf("%d %d\n", n, value);
404
    index+= n;
405
    s->index= index;
406
#        endif
407
#    else //ALIGNED_BITSTREAM_WRITER
408
#        ifdef ARCH_X86
409
    asm volatile(
410
        "movl $7, %%ecx                        \n\t"
411
        "andl %0, %%ecx                        \n\t"
412
        "addl %3, %%ecx                        \n\t"
413
        "negl %%ecx                        \n\t"
414
        "shll %%cl, %1                        \n\t"
415
        "bswapl %1                        \n\t"
416
        "movl %0, %%ecx                        \n\t"
417
        "shrl $3, %%ecx                        \n\t"
418
        "orl %1, (%%ecx, %2)                \n\t"
419
        "addl %3, %0                        \n\t"
420
        "movl $0, 4(%%ecx, %2)                \n\t"
421
        : "=&r" (s->index), "=&r" (value)
422
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
423
        : "%ecx"
424
    );
425
#        else
426
    int index= s->index;
427
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
428
    
429
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
430
    ptr[1] = 0;
431
//if(n>24) printf("%d %d\n", n, value);
432
    index+= n;
433
    s->index= index;
434
#        endif
435
#    endif //!ALIGNED_BITSTREAM_WRITER
436
}
437
#endif
438

    
439

    
440
static inline uint8_t* pbBufPtr(PutBitContext *s)
441
{
442
#ifdef ALT_BITSTREAM_WRITER
443
        return s->buf + (s->index>>3);
444
#else
445
        return s->buf_ptr;
446
#endif
447
}
448

    
449
/* Bitstream reader API docs:
450
name
451
    abritary name which is used as prefix for the internal variables
452

453
gb
454
    getbitcontext
455

456
OPEN_READER(name, gb)
457
    loads gb into local variables
458

459
CLOSE_READER(name, gb)
460
    stores local vars in gb
461

462
UPDATE_CACHE(name, gb)
463
    refills the internal cache from the bitstream
464
    after this call at least MIN_CACHE_BITS will be available,
465

466
GET_CACHE(name, gb)
467
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
468

469
SHOW_UBITS(name, gb, num)
470
    will return the nest num bits
471

472
SHOW_SBITS(name, gb, num)
473
    will return the nest num bits and do sign extension
474

475
SKIP_BITS(name, gb, num)
476
    will skip over the next num bits
477
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
478

479
SKIP_CACHE(name, gb, num)
480
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
481

482
SKIP_COUNTER(name, gb, num)
483
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
484

485
LAST_SKIP_CACHE(name, gb, num)
486
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
487

488
LAST_SKIP_BITS(name, gb, num)
489
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
490

491
for examples see get_bits, show_bits, skip_bits, get_vlc
492
*/
493

    
494
#ifdef ALT_BITSTREAM_READER
495
#   define MIN_CACHE_BITS 25
496

    
497
#   define OPEN_READER(name, gb)\
498
        int name##_index= (gb)->index;\
499
        int name##_cache= 0;\
500

    
501
#   define CLOSE_READER(name, gb)\
502
        (gb)->index= name##_index;\
503

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

    
507
#   define SKIP_CACHE(name, gb, num)\
508
        name##_cache <<= (num);\
509

    
510
// FIXME name?
511
#   define SKIP_COUNTER(name, gb, num)\
512
        name##_index += (num);\
513

    
514
#   define SKIP_BITS(name, gb, num)\
515
        {\
516
            SKIP_CACHE(name, gb, num)\
517
            SKIP_COUNTER(name, gb, num)\
518
        }\
519

    
520
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
521
#   define LAST_SKIP_CACHE(name, gb, num) ;
522

    
523
#   define SHOW_UBITS(name, gb, num)\
524
        NEG_USR32(name##_cache, num)
525

    
526
#   define SHOW_SBITS(name, gb, num)\
527
        NEG_SSR32(name##_cache, num)
528

    
529
#   define GET_CACHE(name, gb)\
530
        ((uint32_t)name##_cache)
531

    
532
static inline int get_bits_count(GetBitContext *s){
533
    return s->index;
534
}
535
#elif defined LIBMPEG2_BITSTREAM_READER
536
//libmpeg2 like reader
537

    
538
#   define MIN_CACHE_BITS 16
539

    
540
#   define OPEN_READER(name, gb)\
541
        int name##_bit_count=(gb)->bit_count;\
542
        int name##_cache= (gb)->cache;\
543
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
544

    
545
#   define CLOSE_READER(name, gb)\
546
        (gb)->bit_count= name##_bit_count;\
547
        (gb)->cache= name##_cache;\
548
        (gb)->buffer_ptr= name##_buffer_ptr;\
549

    
550
#   define UPDATE_CACHE(name, gb)\
551
    if(name##_bit_count > 0){\
552
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
553
        name##_buffer_ptr+=2;\
554
        name##_bit_count-= 16;\
555
    }\
556

    
557
#   define SKIP_CACHE(name, gb, num)\
558
        name##_cache <<= (num);\
559

    
560
#   define SKIP_COUNTER(name, gb, num)\
561
        name##_bit_count += (num);\
562

    
563
#   define SKIP_BITS(name, gb, num)\
564
        {\
565
            SKIP_CACHE(name, gb, num)\
566
            SKIP_COUNTER(name, gb, num)\
567
        }\
568

    
569
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
570
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
571

    
572
#   define SHOW_UBITS(name, gb, num)\
573
        NEG_USR32(name##_cache, num)
574

    
575
#   define SHOW_SBITS(name, gb, num)\
576
        NEG_SSR32(name##_cache, num)
577

    
578
#   define GET_CACHE(name, gb)\
579
        ((uint32_t)name##_cache)
580

    
581
static inline int get_bits_count(GetBitContext *s){
582
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
583
}
584

    
585
#elif defined A32_BITSTREAM_READER
586

    
587
#   define MIN_CACHE_BITS 32
588

    
589
#   define OPEN_READER(name, gb)\
590
        int name##_bit_count=(gb)->bit_count;\
591
        uint32_t name##_cache0= (gb)->cache0;\
592
        uint32_t name##_cache1= (gb)->cache1;\
593
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
594

    
595
#   define CLOSE_READER(name, gb)\
596
        (gb)->bit_count= name##_bit_count;\
597
        (gb)->cache0= name##_cache0;\
598
        (gb)->cache1= name##_cache1;\
599
        (gb)->buffer_ptr= name##_buffer_ptr;\
600

    
601
#   define UPDATE_CACHE(name, gb)\
602
    if(name##_bit_count > 0){\
603
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
604
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
605
        name##_cache1 |= next<<name##_bit_count;\
606
        name##_buffer_ptr++;\
607
        name##_bit_count-= 32;\
608
    }\
609

    
610
#ifdef ARCH_X86
611
#   define SKIP_CACHE(name, gb, num)\
612
        asm(\
613
            "shldl %2, %1, %0                \n\t"\
614
            "shll %2, %1                \n\t"\
615
            : "+r" (name##_cache0), "+r" (name##_cache1)\
616
            : "Ic" ((uint8_t)num)\
617
           );
618
#else
619
#   define SKIP_CACHE(name, gb, num)\
620
        name##_cache0 <<= (num);\
621
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
622
        name##_cache1 <<= (num);
623
#endif
624

    
625
#   define SKIP_COUNTER(name, gb, num)\
626
        name##_bit_count += (num);\
627

    
628
#   define SKIP_BITS(name, gb, num)\
629
        {\
630
            SKIP_CACHE(name, gb, num)\
631
            SKIP_COUNTER(name, gb, num)\
632
        }\
633

    
634
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
635
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
636

    
637
#   define SHOW_UBITS(name, gb, num)\
638
        NEG_USR32(name##_cache0, num)
639

    
640
#   define SHOW_SBITS(name, gb, num)\
641
        NEG_SSR32(name##_cache0, num)
642

    
643
#   define GET_CACHE(name, gb)\
644
        (name##_cache0)
645

    
646
static inline int get_bits_count(GetBitContext *s){
647
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
648
}
649

    
650
#endif
651

    
652
static inline unsigned int get_bits(GetBitContext *s, int n){
653
    register int tmp;
654
    OPEN_READER(re, s)
655
    UPDATE_CACHE(re, s)
656
    tmp= SHOW_UBITS(re, s, n);
657
    LAST_SKIP_BITS(re, s, n)
658
    CLOSE_READER(re, s)
659
    return tmp;
660
}
661

    
662
static inline unsigned int show_bits(GetBitContext *s, int n){
663
    register int tmp;
664
    OPEN_READER(re, s)
665
    UPDATE_CACHE(re, s)
666
    tmp= SHOW_UBITS(re, s, n);
667
//    CLOSE_READER(re, s)
668
    return tmp;
669
}
670

    
671
static inline void skip_bits(GetBitContext *s, int n){
672
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
673
    OPEN_READER(re, s)
674
    UPDATE_CACHE(re, s)
675
    LAST_SKIP_BITS(re, s, n)
676
    CLOSE_READER(re, s)
677
}
678

    
679
static inline unsigned int get_bits1(GetBitContext *s){
680
#ifdef ALT_BITSTREAM_READER
681
    int index= s->index;
682
    uint8_t result= s->buffer[ index>>3 ];
683
    result<<= (index&0x07);
684
    result>>= 8 - 1;
685
    index++;
686
    s->index= index;
687

    
688
    return result;
689
#else
690
    return get_bits(s, 1);
691
#endif
692
}
693

    
694
static inline unsigned int show_bits1(GetBitContext *s){
695
    return show_bits(s, 1);
696
}
697

    
698
static inline void skip_bits1(GetBitContext *s){
699
    skip_bits(s, 1);
700
}
701

    
702
void init_get_bits(GetBitContext *s,
703
                   UINT8 *buffer, int buffer_size);
704

    
705
int check_marker(GetBitContext *s, const char *msg);
706
void align_get_bits(GetBitContext *s);
707
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
708
             const void *bits, int bits_wrap, int bits_size,
709
             const void *codes, int codes_wrap, int codes_size);
710
void free_vlc(VLC *vlc);
711

    
712
/**
713
 *
714
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
715
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
716
 * is undefined
717
 */
718
#define GET_VLC(code, name, gb, table, bits, max_depth)\
719
{\
720
    int n, index, nb_bits;\
721
\
722
    index= SHOW_UBITS(name, gb, bits);\
723
    code = table[index][0];\
724
    n    = table[index][1];\
725
\
726
    if(max_depth > 1 && n < 0){\
727
        LAST_SKIP_BITS(name, gb, bits)\
728
        UPDATE_CACHE(name, gb)\
729
\
730
        nb_bits = -n;\
731
\
732
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
733
        code = table[index][0];\
734
        n    = table[index][1];\
735
        if(max_depth > 2 && n < 0){\
736
            LAST_SKIP_BITS(name, gb, nb_bits)\
737
            UPDATE_CACHE(name, gb)\
738
\
739
            nb_bits = -n;\
740
\
741
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
742
            code = table[index][0];\
743
            n    = table[index][1];\
744
        }\
745
    }\
746
    SKIP_BITS(name, gb, n)\
747
}
748

    
749
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
750
{\
751
    int n, index, nb_bits;\
752
\
753
    index= SHOW_UBITS(name, gb, bits);\
754
    level = table[index].level;\
755
    n     = table[index].len;\
756
\
757
    if(max_depth > 1 && n < 0){\
758
        LAST_SKIP_BITS(name, gb, bits)\
759
        UPDATE_CACHE(name, gb)\
760
\
761
        nb_bits = -n;\
762
\
763
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
764
        level = table[index].level;\
765
        n     = table[index].len;\
766
    }\
767
    run= table[index].run;\
768
    SKIP_BITS(name, gb, n)\
769
}
770

    
771
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
772
static inline int get_vlc(GetBitContext *s, VLC *vlc)
773
{
774
    int code;
775
    VLC_TYPE (*table)[2]= vlc->table;
776
    
777
    OPEN_READER(re, s)
778
    UPDATE_CACHE(re, s)
779

    
780
    GET_VLC(code, re, s, table, vlc->bits, 3)    
781

    
782
    CLOSE_READER(re, s)
783
    return code;
784
}
785

    
786
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
787
                                  int bits, int max_depth)
788
{
789
    int code;
790
    
791
    OPEN_READER(re, s)
792
    UPDATE_CACHE(re, s)
793

    
794
    GET_VLC(code, re, s, table, bits, max_depth)
795

    
796
    CLOSE_READER(re, s)
797
    return code;
798
}
799

    
800

    
801
/* define it to include statistics code (useful only for optimizing
802
   codec efficiency */
803
//#define STATS
804

    
805
#ifdef STATS
806

    
807
enum {
808
    ST_UNKNOWN,
809
    ST_DC,
810
    ST_INTRA_AC,
811
    ST_INTER_AC,
812
    ST_INTRA_MB,
813
    ST_INTER_MB,
814
    ST_MV,
815
    ST_NB,
816
};
817

    
818
extern int st_current_index;
819
extern unsigned int st_bit_counts[ST_NB];
820
extern unsigned int st_out_bit_counts[ST_NB];
821

    
822
void print_stats(void);
823
#endif
824

    
825
/* misc math functions */
826
extern const uint8_t ff_log2_tab[256];
827

    
828
static inline int av_log2(unsigned int v)
829
{
830
    int n;
831

    
832
    n = 0;
833
    if (v & 0xffff0000) {
834
        v >>= 16;
835
        n += 16;
836
    }
837
    if (v & 0xff00) {
838
        v >>= 8;
839
        n += 8;
840
    }
841
    n += ff_log2_tab[v];
842

    
843
    return n;
844
}
845

    
846
static inline int av_log2_16bit(unsigned int v)
847
{
848
    int n;
849

    
850
    n = 0;
851
    if (v & 0xff00) {
852
        v >>= 8;
853
        n += 8;
854
    }
855
    n += ff_log2_tab[v];
856

    
857
    return n;
858
}
859

    
860

    
861
/* median of 3 */
862
static inline int mid_pred(int a, int b, int c)
863
{
864
    int vmin, vmax;
865
    vmax = vmin = a;
866
    if (b < vmin)
867
        vmin = b;
868
    else
869
        vmax = b;
870

    
871
    if (c < vmin)
872
        vmin = c;
873
    else if (c > vmax)
874
        vmax = c;
875

    
876
    return a + b + c - vmin - vmax;
877
}
878

    
879
static inline int clip(int a, int amin, int amax)
880
{
881
    if (a < amin)
882
        return amin;
883
    else if (a > amax)
884
        return amax;
885
    else
886
        return a;
887
}
888

    
889
/* math */
890
extern const uint8_t ff_sqrt_tab[128];
891

    
892
int ff_gcd(int a, int b);
893

    
894
static inline int ff_sqrt(int a)
895
{
896
    int ret=0;
897
    int s;
898
    int ret_sq=0;
899
    
900
    if(a<128) return ff_sqrt_tab[a];
901
    
902
    for(s=15; s>=0; s--){
903
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
904
        if(b<=a){
905
            ret_sq=b;
906
            ret+= 1<<s;
907
        }
908
    }
909
    return ret;
910
}
911

    
912
/**
913
 * converts fourcc string to int
914
 */
915
static inline int ff_get_fourcc(const char *s){
916
    assert( strlen(s)==4 );
917
    
918
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
919
}
920

    
921
void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
922

    
923

    
924
#ifdef ARCH_X86
925
#define MASK_ABS(mask, level)\
926
            asm volatile(\
927
                "cdq                        \n\t"\
928
                "xorl %1, %0                \n\t"\
929
                "subl %1, %0                \n\t"\
930
                : "+a" (level), "=&d" (mask)\
931
            );
932
#else
933
#define MASK_ABS(mask, level)\
934
            mask= level>>31;\
935
            level= (level^mask)-mask;
936
#endif
937

    
938

    
939
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
940
#define COPY3_IF_LT(x,y,a,b,c,d)\
941
asm volatile (\
942
    "cmpl %0, %3        \n\t"\
943
    "cmovl %3, %0        \n\t"\
944
    "cmovl %4, %1        \n\t"\
945
    "cmovl %5, %2        \n\t"\
946
    : "+r" (x), "+r" (a), "+r" (c)\
947
    : "r" (y), "r" (b), "r" (d)\
948
);
949
#else
950
#define COPY3_IF_LT(x,y,a,b,c,d)\
951
if((y)<(x)){\
952
     (x)=(y);\
953
     (a)=(b);\
954
     (c)=(d);\
955
}
956
#endif
957

    
958
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
959

    
960
/* avoid usage of various functions */
961
#define malloc please_use_av_malloc
962
#define free please_use_av_free
963
#define realloc please_use_av_realloc
964

    
965
#endif /* HAVE_AV_CONFIG_H */
966

    
967
#endif /* COMMON_H */