Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 18f77016

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
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
165
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
166

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

    
188
/* bit output */
189

    
190
struct PutBitContext;
191

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

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

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

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

    
216
/* bit input */
217

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

    
235
static inline int get_bits_count(GetBitContext *s);
236

    
237
#define VLC_TYPE INT16
238

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

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

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

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

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

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

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

    
304
    s->bit_buf = bit_buf;
305
    s->bit_left = bit_left;
306
}
307
#endif
308

    
309

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

    
376

    
377
static inline uint8_t* pbBufPtr(PutBitContext *s)
378
{
379
#ifdef ALT_BITSTREAM_WRITER
380
        return s->buf + (s->index>>3);
381
#else
382
        return s->buf_ptr;
383
#endif
384
}
385

    
386
/* Bitstream reader API docs:
387
name
388
    abritary name which is used as prefix for the internal variables
389

390
gb
391
    getbitcontext
392

393
OPEN_READER(name, gb)
394
    loads gb into local variables
395

396
CLOSE_READER(name, gb)
397
    stores local vars in gb
398

399
UPDATE_CACHE(name, gb)
400
    refills the internal cache from the bitstream
401
    after this call at least MIN_CACHE_BITS will be available,
402

403
GET_CACHE(name, gb)
404
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
405

406
SHOW_UBITS(name, gb, num)
407
    will return the nest num bits
408

409
SHOW_SBITS(name, gb, num)
410
    will return the nest num bits and do sign extension
411

412
SKIP_BITS(name, gb, num)
413
    will skip over the next num bits
414
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
415

416
SKIP_CACHE(name, gb, num)
417
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
418

419
SKIP_COUNTER(name, gb, num)
420
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
421

422
LAST_SKIP_CACHE(name, gb, num)
423
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
424

425
LAST_SKIP_BITS(name, gb, num)
426
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
427

428
for examples see get_bits, show_bits, skip_bits, get_vlc
429
*/
430

    
431
#ifdef ALT_BITSTREAM_READER
432
#   define MIN_CACHE_BITS 25
433

    
434
#   define OPEN_READER(name, gb)\
435
        int name##_index= (gb)->index;\
436
        int name##_cache= 0;\
437

    
438
#   define CLOSE_READER(name, gb)\
439
        (gb)->index= name##_index;\
440

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

    
444
#   define SKIP_CACHE(name, gb, num)\
445
        name##_cache <<= (num);\
446

    
447
// FIXME name?
448
#   define SKIP_COUNTER(name, gb, num)\
449
        name##_index += (num);\
450

    
451
#   define SKIP_BITS(name, gb, num)\
452
        {\
453
            SKIP_CACHE(name, gb, num)\
454
            SKIP_COUNTER(name, gb, num)\
455
        }\
456

    
457
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
458
#   define LAST_SKIP_CACHE(name, gb, num) ;
459

    
460
#   define SHOW_UBITS(name, gb, num)\
461
        NEG_USR32(name##_cache, num)
462

    
463
#   define SHOW_SBITS(name, gb, num)\
464
        NEG_SSR32(name##_cache, num)
465

    
466
#   define GET_CACHE(name, gb)\
467
        ((uint32_t)name##_cache)
468

    
469
static inline int get_bits_count(GetBitContext *s){
470
    return s->index;
471
}
472
#elif defined LIBMPEG2_BITSTREAM_READER
473
//libmpeg2 like reader
474

    
475
#   define MIN_CACHE_BITS 16
476

    
477
#   define OPEN_READER(name, gb)\
478
        int name##_bit_count=(gb)->bit_count;\
479
        int name##_cache= (gb)->cache;\
480
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
481

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

    
487
#   define UPDATE_CACHE(name, gb)\
488
    if(name##_bit_count > 0){\
489
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
490
        name##_buffer_ptr+=2;\
491
        name##_bit_count-= 16;\
492
    }\
493

    
494
#   define SKIP_CACHE(name, gb, num)\
495
        name##_cache <<= (num);\
496

    
497
#   define SKIP_COUNTER(name, gb, num)\
498
        name##_bit_count += (num);\
499

    
500
#   define SKIP_BITS(name, gb, num)\
501
        {\
502
            SKIP_CACHE(name, gb, num)\
503
            SKIP_COUNTER(name, gb, num)\
504
        }\
505

    
506
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
507
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
508

    
509
#   define SHOW_UBITS(name, gb, num)\
510
        NEG_USR32(name##_cache, num)
511

    
512
#   define SHOW_SBITS(name, gb, num)\
513
        NEG_SSR32(name##_cache, num)
514

    
515
#   define GET_CACHE(name, gb)\
516
        ((uint32_t)name##_cache)
517

    
518
static inline int get_bits_count(GetBitContext *s){
519
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
520
}
521

    
522
#elif defined A32_BITSTREAM_READER
523

    
524
#   define MIN_CACHE_BITS 32
525

    
526
#   define OPEN_READER(name, gb)\
527
        int name##_bit_count=(gb)->bit_count;\
528
        uint32_t name##_cache0= (gb)->cache0;\
529
        uint32_t name##_cache1= (gb)->cache1;\
530
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
531

    
532
#   define CLOSE_READER(name, gb)\
533
        (gb)->bit_count= name##_bit_count;\
534
        (gb)->cache0= name##_cache0;\
535
        (gb)->cache1= name##_cache1;\
536
        (gb)->buffer_ptr= name##_buffer_ptr;\
537

    
538
#   define UPDATE_CACHE(name, gb)\
539
    if(name##_bit_count > 0){\
540
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
541
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
542
        name##_cache1 |= next<<name##_bit_count;\
543
        name##_buffer_ptr++;\
544
        name##_bit_count-= 32;\
545
    }\
546

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

    
562
#   define SKIP_COUNTER(name, gb, num)\
563
        name##_bit_count += (num);\
564

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

    
571
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
572
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
573

    
574
#   define SHOW_UBITS(name, gb, num)\
575
        NEG_USR32(name##_cache0, num)
576

    
577
#   define SHOW_SBITS(name, gb, num)\
578
        NEG_SSR32(name##_cache0, num)
579

    
580
#   define GET_CACHE(name, gb)\
581
        (name##_cache0)
582

    
583
static inline int get_bits_count(GetBitContext *s){
584
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
585
}
586

    
587
#endif
588

    
589
static inline unsigned int get_bits(GetBitContext *s, int n){
590
    register int tmp;
591
    OPEN_READER(re, s)
592
    UPDATE_CACHE(re, s)
593
    tmp= SHOW_UBITS(re, s, n);
594
    LAST_SKIP_BITS(re, s, n)
595
    CLOSE_READER(re, s)
596
    return tmp;
597
}
598

    
599
static inline unsigned int show_bits(GetBitContext *s, int n){
600
    register int tmp;
601
    OPEN_READER(re, s)
602
    UPDATE_CACHE(re, s)
603
    tmp= SHOW_UBITS(re, s, n);
604
//    CLOSE_READER(re, s)
605
    return tmp;
606
}
607

    
608
static inline void skip_bits(GetBitContext *s, int n){
609
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
610
    OPEN_READER(re, s)
611
    UPDATE_CACHE(re, s)
612
    LAST_SKIP_BITS(re, s, n)
613
    CLOSE_READER(re, s)
614
}
615

    
616
static inline unsigned int get_bits1(GetBitContext *s){
617
#ifdef ALT_BITSTREAM_READER
618
    int index= s->index;
619
    uint8_t result= s->buffer[ index>>3 ];
620
    result<<= (index&0x07);
621
    result>>= 8 - 1;
622
    index++;
623
    s->index= index;
624

    
625
    return result;
626
#else
627
    return get_bits(s, 1);
628
#endif
629
}
630

    
631
static inline unsigned int show_bits1(GetBitContext *s){
632
    return show_bits(s, 1);
633
}
634

    
635
static inline void skip_bits1(GetBitContext *s){
636
    skip_bits(s, 1);
637
}
638

    
639
void init_get_bits(GetBitContext *s,
640
                   UINT8 *buffer, int buffer_size);
641

    
642
int check_marker(GetBitContext *s, const char *msg);
643
void align_get_bits(GetBitContext *s);
644
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
645
             const void *bits, int bits_wrap, int bits_size,
646
             const void *codes, int codes_wrap, int codes_size);
647
void free_vlc(VLC *vlc);
648

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

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

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

    
711
    GET_VLC(code, re, s, table, vlc->bits, 3)    
712

    
713
    CLOSE_READER(re, s)
714
    return code;
715
}
716

    
717
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
718
                                  int bits, int max_depth)
719
{
720
    int code;
721
    
722
    OPEN_READER(re, s)
723
    UPDATE_CACHE(re, s)
724

    
725
    GET_VLC(code, re, s, table, bits, max_depth)
726

    
727
    CLOSE_READER(re, s)
728
    return code;
729
}
730

    
731

    
732
/* define it to include statistics code (useful only for optimizing
733
   codec efficiency */
734
//#define STATS
735

    
736
#ifdef STATS
737

    
738
enum {
739
    ST_UNKNOWN,
740
    ST_DC,
741
    ST_INTRA_AC,
742
    ST_INTER_AC,
743
    ST_INTRA_MB,
744
    ST_INTER_MB,
745
    ST_MV,
746
    ST_NB,
747
};
748

    
749
extern int st_current_index;
750
extern unsigned int st_bit_counts[ST_NB];
751
extern unsigned int st_out_bit_counts[ST_NB];
752

    
753
void print_stats(void);
754
#endif
755

    
756
/* misc math functions */
757

    
758
static inline int av_log2(unsigned int v)
759
{
760
    int n;
761

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

    
785
/* median of 3 */
786
static inline int mid_pred(int a, int b, int c)
787
{
788
    int vmin, vmax;
789
    vmax = vmin = a;
790
    if (b < vmin)
791
        vmin = b;
792
    else
793
        vmax = b;
794

    
795
    if (c < vmin)
796
        vmin = c;
797
    else if (c > vmax)
798
        vmax = c;
799

    
800
    return a + b + c - vmin - vmax;
801
}
802

    
803
static inline int clip(int a, int amin, int amax)
804
{
805
    if (a < amin)
806
        return amin;
807
    else if (a > amax)
808
        return amax;
809
    else
810
        return a;
811
}
812

    
813
/* math */
814
extern const UINT8 ff_sqrt_tab[128];
815

    
816
int ff_gcd(int a, int b);
817

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

    
836
/**
837
 * converts fourcc string to int
838
 */
839
static inline int ff_get_fourcc(char *s){
840
    assert( strlen(s)==4 );
841
    
842
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
843
}
844

    
845

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

    
860

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

    
880
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
881

    
882
#endif /* HAVE_AV_CONFIG_H */
883

    
884
#endif /* COMMON_H */