Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ ea937d01

History | View | Annotate | Download (21.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
#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
#    define vsnprintf _vsnprintf
92

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

    
97
#include <inttypes.h>
98

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

    
108
#ifdef HAVE_AV_CONFIG_H
109

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

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

    
119
#include <float.h>
120

    
121
#endif /* HAVE_AV_CONFIG_H */
122

    
123
/* CONFIG_OS2 end */
124
#else
125

    
126
/* unix */
127

    
128
#    include <inttypes.h>
129

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

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

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

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

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

    
156
#ifdef HAVE_AV_CONFIG_H
157

    
158
#    include "bswap.h"
159

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

    
167
/* debug stuff */
168

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

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

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

    
179
#    else
180

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

    
187
#    endif /* !CONFIG_WIN32 */
188

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

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

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

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

    
221
/* bit output */
222

    
223
struct PutBitContext;
224

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

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

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

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

    
249
/* bit input */
250

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

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

    
270
#define VLC_TYPE INT16
271

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

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

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

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

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

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

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

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

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

    
354

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

    
421

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

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

435
gb
436
    getbitcontext
437

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

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

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

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

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

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

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

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

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

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

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

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

    
476
#ifdef ALT_BITSTREAM_READER
477
#   define MIN_CACHE_BITS 25
478

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

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

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

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

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

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

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

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

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

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

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

    
520
#   define MIN_CACHE_BITS 16
521

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

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

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

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

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

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

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

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

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

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

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

    
567
#elif defined A32_BITSTREAM_READER
568

    
569
#   define MIN_CACHE_BITS 32
570

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

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

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

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

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

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

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

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

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

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

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

    
632
#endif
633

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

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

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

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

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

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

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

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

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

    
694
/**
695
 *
696
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
697
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
698
 * is undefined
699
 */
700
#define GET_VLC(code, name, gb, table, bits, max_depth)\
701
{\
702
    int n, index, nb_bits;\
703
\
704
    index= SHOW_UBITS(name, gb, bits);\
705
    code = table[index][0];\
706
    n    = table[index][1];\
707
\
708
    if(max_depth > 1 && n < 0){\
709
        LAST_SKIP_BITS(name, gb, bits)\
710
        UPDATE_CACHE(name, gb)\
711
\
712
        nb_bits = -n;\
713
\
714
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
715
        code = table[index][0];\
716
        n    = table[index][1];\
717
        if(max_depth > 2 && n < 0){\
718
            LAST_SKIP_BITS(name, gb, nb_bits)\
719
            UPDATE_CACHE(name, gb)\
720
\
721
            nb_bits = -n;\
722
\
723
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
724
            code = table[index][0];\
725
            n    = table[index][1];\
726
        }\
727
    }\
728
    SKIP_BITS(name, gb, n)\
729
}
730

    
731
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
732
{\
733
    int n, index, nb_bits;\
734
\
735
    index= SHOW_UBITS(name, gb, bits);\
736
    level = table[index].level;\
737
    n     = table[index].len;\
738
\
739
    if(max_depth > 1 && n < 0){\
740
        LAST_SKIP_BITS(name, gb, bits)\
741
        UPDATE_CACHE(name, gb)\
742
\
743
        nb_bits = -n;\
744
\
745
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
746
        level = table[index].level;\
747
        n     = table[index].len;\
748
    }\
749
    run= table[index].run;\
750
    SKIP_BITS(name, gb, n)\
751
}
752

    
753
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
754
static inline int get_vlc(GetBitContext *s, VLC *vlc)
755
{
756
    int code;
757
    VLC_TYPE (*table)[2]= vlc->table;
758
    
759
    OPEN_READER(re, s)
760
    UPDATE_CACHE(re, s)
761

    
762
    GET_VLC(code, re, s, table, vlc->bits, 3)    
763

    
764
    CLOSE_READER(re, s)
765
    return code;
766
}
767

    
768
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
769
                                  int bits, int max_depth)
770
{
771
    int code;
772
    
773
    OPEN_READER(re, s)
774
    UPDATE_CACHE(re, s)
775

    
776
    GET_VLC(code, re, s, table, bits, max_depth)
777

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

    
782

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

    
787
#ifdef STATS
788

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

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

    
804
void print_stats(void);
805
#endif
806

    
807
/* misc math functions */
808
extern const uint8_t ff_log2_tab[256];
809

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

    
814
    n = 0;
815
    if (v & 0xffff0000) {
816
        v >>= 16;
817
        n += 16;
818
    }
819
    if (v & 0xff00) {
820
        v >>= 8;
821
        n += 8;
822
    }
823
    n += ff_log2_tab[v];
824

    
825
    return n;
826
}
827

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

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

    
839
    return n;
840
}
841

    
842

    
843
/* median of 3 */
844
static inline int mid_pred(int a, int b, int c)
845
{
846
    int vmin, vmax;
847
    vmax = vmin = a;
848
    if (b < vmin)
849
        vmin = b;
850
    else
851
        vmax = b;
852

    
853
    if (c < vmin)
854
        vmin = c;
855
    else if (c > vmax)
856
        vmax = c;
857

    
858
    return a + b + c - vmin - vmax;
859
}
860

    
861
static inline int clip(int a, int amin, int amax)
862
{
863
    if (a < amin)
864
        return amin;
865
    else if (a > amax)
866
        return amax;
867
    else
868
        return a;
869
}
870

    
871
/* math */
872
extern const uint8_t ff_sqrt_tab[128];
873

    
874
int ff_gcd(int a, int b);
875

    
876
static inline int ff_sqrt(int a)
877
{
878
    int ret=0;
879
    int s;
880
    int ret_sq=0;
881
    
882
    if(a<128) return ff_sqrt_tab[a];
883
    
884
    for(s=15; s>=0; s--){
885
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
886
        if(b<=a){
887
            ret_sq=b;
888
            ret+= 1<<s;
889
        }
890
    }
891
    return ret;
892
}
893

    
894
/**
895
 * converts fourcc string to int
896
 */
897
static inline int ff_get_fourcc(const char *s){
898
    assert( strlen(s)==4 );
899
    
900
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
901
}
902

    
903
void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max);
904

    
905

    
906
#ifdef ARCH_X86
907
#define MASK_ABS(mask, level)\
908
            asm volatile(\
909
                "cdq                        \n\t"\
910
                "xorl %1, %0                \n\t"\
911
                "subl %1, %0                \n\t"\
912
                : "+a" (level), "=&d" (mask)\
913
            );
914
#else
915
#define MASK_ABS(mask, level)\
916
            mask= level>>31;\
917
            level= (level^mask)-mask;
918
#endif
919

    
920

    
921
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
922
#define COPY3_IF_LT(x,y,a,b,c,d)\
923
asm volatile (\
924
    "cmpl %0, %3        \n\t"\
925
    "cmovl %3, %0        \n\t"\
926
    "cmovl %4, %1        \n\t"\
927
    "cmovl %5, %2        \n\t"\
928
    : "+r" (x), "+r" (a), "+r" (c)\
929
    : "r" (y), "r" (b), "r" (d)\
930
);
931
#else
932
#define COPY3_IF_LT(x,y,a,b,c,d)\
933
if((y)<(x)){\
934
     (x)=(y);\
935
     (a)=(b);\
936
     (c)=(d);\
937
}
938
#endif
939

    
940
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
941

    
942
/* avoid usage of various functions */
943
#define malloc please_use_av_malloc
944
#define free please_use_av_free
945
#define realloc please_use_av_realloc
946

    
947
#endif /* HAVE_AV_CONFIG_H */
948

    
949
#endif /* COMMON_H */