Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 81ada1ce

History | View | Annotate | Download (20.3 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
#        ifndef INT64_C
112
#            define INT64_C(c)     (c ## LL)
113
#            define UINT64_C(c)    (c ## ULL)
114
#        endif
115

    
116
#        ifdef USE_FASTMEMCPY
117
#            include "fastmemcpy.h"
118
#        endif
119
#    endif /* HAVE_AV_CONFIG_H */
120

    
121
#endif /* !CONFIG_WIN32 */
122

    
123
#ifdef HAVE_AV_CONFIG_H
124

    
125
#    include "bswap.h"
126

    
127
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
128
        defined(__OS2__) || defined (__OpenBSD__)
129
#        define MANGLE(a) "_" #a
130
#    else
131
#        define MANGLE(a) #a
132
#    endif
133

    
134
/* debug stuff */
135

    
136
#    ifndef DEBUG
137
#        define NDEBUG
138
#    endif
139
#    include <assert.h>
140

    
141
/* dprintf macros */
142
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
143

    
144
inline void dprintf(const char* fmt,...) {}
145

    
146
#    else
147

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

    
154
#    endif /* !CONFIG_WIN32 */
155

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

    
158
//rounded divison & shift
159
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
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

    
164
#ifndef MAX
165
#    define MAX(a,b) ((a) > (b) ? (a) : (b))
166
#endif
167

    
168
#ifndef MIN
169
#    define MIN(a,b) ((a) > (b) ? (b) : (a))
170
#endif
171

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

    
193
/* bit output */
194

    
195
struct PutBitContext;
196

    
197
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
198

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

    
211
void init_put_bits(PutBitContext *s, 
212
                   UINT8 *buffer, int buffer_size,
213
                   void *opaque,
214
                   void (*write_data)(void *, UINT8 *, int));
215

    
216
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
217
void align_put_bits(PutBitContext *s);
218
void flush_put_bits(PutBitContext *s);
219
void put_string(PutBitContext * pbc, char *s);
220

    
221
/* bit input */
222

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

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

    
242
#define VLC_TYPE INT16
243

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

    
250
typedef struct RL_VLC_ELEM {
251
    int16_t level;
252
    int8_t len;
253
    uint8_t run;
254
} RL_VLC_ELEM;
255

    
256
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
257
#ifdef ARCH_X86
258
#    define unaligned32(a) (*(UINT32*)(a))
259
#else
260
#    ifdef __GNUC__
261
static inline uint32_t unaligned32(const void *v) {
262
    struct Unaligned {
263
        uint32_t i;
264
    } __attribute__((packed));
265

    
266
    return ((const struct Unaligned *) v)->i;
267
}
268
#    elif defined(__DECC)
269
static inline uint32_t unaligned32(const void *v) {
270
    return *(const __unaligned uint32_t *) v;
271
}
272
#    else
273
static inline uint32_t unaligned32(const void *v) {
274
    return *(const uint32_t *) v;
275
}
276
#    endif
277
#endif //!ARCH_X86
278

    
279
#ifndef ALT_BITSTREAM_WRITER
280
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
281
{
282
    unsigned int bit_buf;
283
    int bit_left;
284

    
285
#ifdef STATS
286
    st_out_bit_counts[st_current_index] += n;
287
#endif
288
    //    printf("put_bits=%d %x\n", n, value);
289
    assert(n == 32 || value < (1U << n));
290
    
291
    bit_buf = s->bit_buf;
292
    bit_left = s->bit_left;
293

    
294
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
295
    /* XXX: optimize */
296
    if (n < bit_left) {
297
        bit_buf = (bit_buf<<n) | value;
298
        bit_left-=n;
299
    } else {
300
        bit_buf<<=bit_left;
301
        bit_buf |= value >> (n - bit_left);
302
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
303
        //printf("bitbuf = %08x\n", bit_buf);
304
        s->buf_ptr+=4;
305
        bit_left+=32 - n;
306
        bit_buf = value;
307
    }
308

    
309
    s->bit_buf = bit_buf;
310
    s->bit_left = bit_left;
311
}
312
#endif
313

    
314

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

    
381

    
382
static inline uint8_t* pbBufPtr(PutBitContext *s)
383
{
384
#ifdef ALT_BITSTREAM_WRITER
385
        return s->buf + (s->index>>3);
386
#else
387
        return s->buf_ptr;
388
#endif
389
}
390

    
391
/* Bitstream reader API docs:
392
name
393
    abritary name which is used as prefix for the internal variables
394

395
gb
396
    getbitcontext
397

398
OPEN_READER(name, gb)
399
    loads gb into local variables
400

401
CLOSE_READER(name, gb)
402
    stores local vars in gb
403

404
UPDATE_CACHE(name, gb)
405
    refills the internal cache from the bitstream
406
    after this call at least MIN_CACHE_BITS will be available,
407

408
GET_CACHE(name, gb)
409
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
410

411
SHOW_UBITS(name, gb, num)
412
    will return the nest num bits
413

414
SHOW_SBITS(name, gb, num)
415
    will return the nest num bits and do sign extension
416

417
SKIP_BITS(name, gb, num)
418
    will skip over the next num bits
419
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
420

421
SKIP_CACHE(name, gb, num)
422
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
423

424
SKIP_COUNTER(name, gb, num)
425
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
426

427
LAST_SKIP_CACHE(name, gb, num)
428
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
429

430
LAST_SKIP_BITS(name, gb, num)
431
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
432

433
for examples see get_bits, show_bits, skip_bits, get_vlc
434
*/
435

    
436
#ifdef ALT_BITSTREAM_READER
437
#   define MIN_CACHE_BITS 25
438

    
439
#   define OPEN_READER(name, gb)\
440
        int name##_index= (gb)->index;\
441
        int name##_cache= 0;\
442

    
443
#   define CLOSE_READER(name, gb)\
444
        (gb)->index= name##_index;\
445

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

    
449
#   define SKIP_CACHE(name, gb, num)\
450
        name##_cache <<= (num);\
451

    
452
// FIXME name?
453
#   define SKIP_COUNTER(name, gb, num)\
454
        name##_index += (num);\
455

    
456
#   define SKIP_BITS(name, gb, num)\
457
        {\
458
            SKIP_CACHE(name, gb, num)\
459
            SKIP_COUNTER(name, gb, num)\
460
        }\
461

    
462
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
463
#   define LAST_SKIP_CACHE(name, gb, num) ;
464

    
465
#   define SHOW_UBITS(name, gb, num)\
466
        NEG_USR32(name##_cache, num)
467

    
468
#   define SHOW_SBITS(name, gb, num)\
469
        NEG_SSR32(name##_cache, num)
470

    
471
#   define GET_CACHE(name, gb)\
472
        ((uint32_t)name##_cache)
473

    
474
static inline int get_bits_count(GetBitContext *s){
475
    return s->index;
476
}
477
#elif defined LIBMPEG2_BITSTREAM_READER
478
//libmpeg2 like reader
479

    
480
#   define MIN_CACHE_BITS 16
481

    
482
#   define OPEN_READER(name, gb)\
483
        int name##_bit_count=(gb)->bit_count;\
484
        int name##_cache= (gb)->cache;\
485
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
486

    
487
#   define CLOSE_READER(name, gb)\
488
        (gb)->bit_count= name##_bit_count;\
489
        (gb)->cache= name##_cache;\
490
        (gb)->buffer_ptr= name##_buffer_ptr;\
491

    
492
#   define UPDATE_CACHE(name, gb)\
493
    if(name##_bit_count > 0){\
494
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
495
        name##_buffer_ptr+=2;\
496
        name##_bit_count-= 16;\
497
    }\
498

    
499
#   define SKIP_CACHE(name, gb, num)\
500
        name##_cache <<= (num);\
501

    
502
#   define SKIP_COUNTER(name, gb, num)\
503
        name##_bit_count += (num);\
504

    
505
#   define SKIP_BITS(name, gb, num)\
506
        {\
507
            SKIP_CACHE(name, gb, num)\
508
            SKIP_COUNTER(name, gb, num)\
509
        }\
510

    
511
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
512
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
513

    
514
#   define SHOW_UBITS(name, gb, num)\
515
        NEG_USR32(name##_cache, num)
516

    
517
#   define SHOW_SBITS(name, gb, num)\
518
        NEG_SSR32(name##_cache, num)
519

    
520
#   define GET_CACHE(name, gb)\
521
        ((uint32_t)name##_cache)
522

    
523
static inline int get_bits_count(GetBitContext *s){
524
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
525
}
526

    
527
#elif defined A32_BITSTREAM_READER
528

    
529
#   define MIN_CACHE_BITS 32
530

    
531
#   define OPEN_READER(name, gb)\
532
        int name##_bit_count=(gb)->bit_count;\
533
        uint32_t name##_cache0= (gb)->cache0;\
534
        uint32_t name##_cache1= (gb)->cache1;\
535
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
536

    
537
#   define CLOSE_READER(name, gb)\
538
        (gb)->bit_count= name##_bit_count;\
539
        (gb)->cache0= name##_cache0;\
540
        (gb)->cache1= name##_cache1;\
541
        (gb)->buffer_ptr= name##_buffer_ptr;\
542

    
543
#   define UPDATE_CACHE(name, gb)\
544
    if(name##_bit_count > 0){\
545
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
546
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
547
        name##_cache1 |= next<<name##_bit_count;\
548
        name##_buffer_ptr++;\
549
        name##_bit_count-= 32;\
550
    }\
551

    
552
#ifdef ARCH_X86
553
#   define SKIP_CACHE(name, gb, num)\
554
        asm(\
555
            "shldl %2, %1, %0                \n\t"\
556
            "shll %2, %1                \n\t"\
557
            : "+r" (name##_cache0), "+r" (name##_cache1)\
558
            : "Ic" ((uint8_t)num)\
559
           );
560
#else
561
#   define SKIP_CACHE(name, gb, num)\
562
        name##_cache0 <<= (num);\
563
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
564
        name##_cache1 <<= (num);
565
#endif
566

    
567
#   define SKIP_COUNTER(name, gb, num)\
568
        name##_bit_count += (num);\
569

    
570
#   define SKIP_BITS(name, gb, num)\
571
        {\
572
            SKIP_CACHE(name, gb, num)\
573
            SKIP_COUNTER(name, gb, num)\
574
        }\
575

    
576
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
577
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
578

    
579
#   define SHOW_UBITS(name, gb, num)\
580
        NEG_USR32(name##_cache0, num)
581

    
582
#   define SHOW_SBITS(name, gb, num)\
583
        NEG_SSR32(name##_cache0, num)
584

    
585
#   define GET_CACHE(name, gb)\
586
        (name##_cache0)
587

    
588
static inline int get_bits_count(GetBitContext *s){
589
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
590
}
591

    
592
#endif
593

    
594
static inline unsigned int get_bits(GetBitContext *s, int n){
595
    register int tmp;
596
    OPEN_READER(re, s)
597
    UPDATE_CACHE(re, s)
598
    tmp= SHOW_UBITS(re, s, n);
599
    LAST_SKIP_BITS(re, s, n)
600
    CLOSE_READER(re, s)
601
    return tmp;
602
}
603

    
604
static inline unsigned int show_bits(GetBitContext *s, int n){
605
    register int tmp;
606
    OPEN_READER(re, s)
607
    UPDATE_CACHE(re, s)
608
    tmp= SHOW_UBITS(re, s, n);
609
//    CLOSE_READER(re, s)
610
    return tmp;
611
}
612

    
613
static inline void skip_bits(GetBitContext *s, int n){
614
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
615
    OPEN_READER(re, s)
616
    UPDATE_CACHE(re, s)
617
    LAST_SKIP_BITS(re, s, n)
618
    CLOSE_READER(re, s)
619
}
620

    
621
static inline unsigned int get_bits1(GetBitContext *s){
622
#ifdef ALT_BITSTREAM_READER
623
    int index= s->index;
624
    uint8_t result= s->buffer[ index>>3 ];
625
    result<<= (index&0x07);
626
    result>>= 8 - 1;
627
    index++;
628
    s->index= index;
629

    
630
    return result;
631
#else
632
    return get_bits(s, 1);
633
#endif
634
}
635

    
636
static inline unsigned int show_bits1(GetBitContext *s){
637
    return show_bits(s, 1);
638
}
639

    
640
static inline void skip_bits1(GetBitContext *s){
641
    skip_bits(s, 1);
642
}
643

    
644
void init_get_bits(GetBitContext *s,
645
                   UINT8 *buffer, int buffer_size);
646

    
647
int check_marker(GetBitContext *s, char *msg);
648
void align_get_bits(GetBitContext *s);
649
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
650
             const void *bits, int bits_wrap, int bits_size,
651
             const void *codes, int codes_wrap, int codes_size);
652
void free_vlc(VLC *vlc);
653

    
654
#define GET_VLC(code, name, gb, table, bits, max_depth)\
655
{\
656
    int n, index, nb_bits;\
657
\
658
    index= SHOW_UBITS(name, gb, bits);\
659
    code = table[index][0];\
660
    n    = table[index][1];\
661
\
662
    if(max_depth > 1 && n < 0){\
663
        LAST_SKIP_BITS(name, gb, bits)\
664
        UPDATE_CACHE(name, gb)\
665
\
666
        nb_bits = -n;\
667
\
668
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
669
        code = table[index][0];\
670
        n    = table[index][1];\
671
        if(max_depth > 2 && n < 0){\
672
            LAST_SKIP_BITS(name, gb, nb_bits)\
673
            UPDATE_CACHE(name, gb)\
674
\
675
            nb_bits = -n;\
676
\
677
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
678
            code = table[index][0];\
679
            n    = table[index][1];\
680
        }\
681
    }\
682
    SKIP_BITS(name, gb, n)\
683
}
684

    
685
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
686
{\
687
    int n, index, nb_bits;\
688
\
689
    index= SHOW_UBITS(name, gb, bits);\
690
    level = table[index].level;\
691
    n     = table[index].len;\
692
\
693
    if(max_depth > 1 && n < 0){\
694
        LAST_SKIP_BITS(name, gb, bits)\
695
        UPDATE_CACHE(name, gb)\
696
\
697
        nb_bits = -n;\
698
\
699
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
700
        level = table[index].level;\
701
        n     = table[index].len;\
702
    }\
703
    run= table[index].run;\
704
    SKIP_BITS(name, gb, n)\
705
}
706

    
707
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
708
static inline int get_vlc(GetBitContext *s, VLC *vlc)
709
{
710
    int code;
711
    VLC_TYPE (*table)[2]= vlc->table;
712
    
713
    OPEN_READER(re, s)
714
    UPDATE_CACHE(re, s)
715

    
716
    GET_VLC(code, re, s, table, vlc->bits, 3)    
717

    
718
    CLOSE_READER(re, s)
719
    return code;
720
}
721

    
722
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
723
                                  int bits, int max_depth)
724
{
725
    int code;
726
    
727
    OPEN_READER(re, s)
728
    UPDATE_CACHE(re, s)
729

    
730
    GET_VLC(code, re, s, table, bits, max_depth)
731

    
732
    CLOSE_READER(re, s)
733
    return code;
734
}
735

    
736

    
737
/* define it to include statistics code (useful only for optimizing
738
   codec efficiency */
739
//#define STATS
740

    
741
#ifdef STATS
742

    
743
enum {
744
    ST_UNKNOWN,
745
    ST_DC,
746
    ST_INTRA_AC,
747
    ST_INTER_AC,
748
    ST_INTRA_MB,
749
    ST_INTER_MB,
750
    ST_MV,
751
    ST_NB,
752
};
753

    
754
extern int st_current_index;
755
extern unsigned int st_bit_counts[ST_NB];
756
extern unsigned int st_out_bit_counts[ST_NB];
757

    
758
void print_stats(void);
759
#endif
760

    
761
/* misc math functions */
762

    
763
static inline int av_log2(unsigned int v)
764
{
765
    int n;
766

    
767
    n = 0;
768
    if (v & 0xffff0000) {
769
        v >>= 16;
770
        n += 16;
771
    }
772
    if (v & 0xff00) {
773
        v >>= 8;
774
        n += 8;
775
    }
776
    if (v & 0xf0) {
777
        v >>= 4;
778
        n += 4;
779
    }
780
    if (v & 0xc) {
781
        v >>= 2;
782
        n += 2;
783
    }
784
    if (v & 0x2) {
785
        n++;
786
    }
787
    return n;
788
}
789

    
790
/* median of 3 */
791
static inline int mid_pred(int a, int b, int c)
792
{
793
    int vmin, vmax;
794
    vmax = vmin = a;
795
    if (b < vmin)
796
        vmin = b;
797
    else
798
        vmax = b;
799

    
800
    if (c < vmin)
801
        vmin = c;
802
    else if (c > vmax)
803
        vmax = c;
804

    
805
    return a + b + c - vmin - vmax;
806
}
807

    
808
static inline int clip(int a, int amin, int amax)
809
{
810
    if (a < amin)
811
        return amin;
812
    else if (a > amax)
813
        return amax;
814
    else
815
        return a;
816
}
817

    
818
/* math */
819
extern const UINT8 ff_sqrt_tab[128];
820

    
821
int ff_gcd(int a, int b);
822

    
823
static inline int ff_sqrt(int a)
824
{
825
    int ret=0;
826
    int s;
827
    int ret_sq=0;
828
    
829
    if(a<128) return ff_sqrt_tab[a];
830
    
831
    for(s=15; s>=0; s--){
832
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
833
        if(b<=a){
834
            ret_sq=b;
835
            ret+= 1<<s;
836
        }
837
    }
838
    return ret;
839
}
840

    
841
/**
842
 * converts fourcc string to int
843
 */
844
static inline int ff_get_fourcc(char *s){
845
    assert( strlen(s)==4 );
846
    
847
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
848
}
849

    
850

    
851
#ifdef ARCH_X86
852
#define MASK_ABS(mask, level)\
853
            asm volatile(\
854
                "cdq                        \n\t"\
855
                "xorl %1, %0                \n\t"\
856
                "subl %1, %0                \n\t"\
857
                : "+a" (level), "=&d" (mask)\
858
            );
859
#else
860
#define MASK_ABS(mask, level)\
861
            mask= level>>31;\
862
            level= (level^mask)-mask;
863
#endif
864

    
865

    
866
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
867
#define COPY3_IF_LT(x,y,a,b,c,d)\
868
asm volatile (\
869
    "cmpl %0, %3        \n\t"\
870
    "cmovl %3, %0        \n\t"\
871
    "cmovl %4, %1        \n\t"\
872
    "cmovl %5, %2        \n\t"\
873
    : "+r" (x), "+r" (a), "+r" (c)\
874
    : "r" (y), "r" (b), "r" (d)\
875
);
876
#else
877
#define COPY3_IF_LT(x,y,a,b,c,d)\
878
if((y)<(x)){\
879
     (x)=(y);\
880
     (a)=(b);\
881
     (c)=(d);\
882
}
883
#endif
884

    
885
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
886

    
887
#endif /* HAVE_AV_CONFIG_H */
888

    
889
#endif /* COMMON_H */