Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ a8380f44

History | View | Annotate | Download (27.6 KB)

1
/**
2
 * @file common.h
3
 * common internal api header.
4
 */
5

    
6
#ifndef COMMON_H
7
#define COMMON_H
8

    
9
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10
#    define CONFIG_WIN32
11
#endif
12

    
13
//#define ALT_BITSTREAM_WRITER
14
//#define ALIGNED_BITSTREAM_WRITER
15

    
16
#define ALT_BITSTREAM_READER
17
//#define LIBMPEG2_BITSTREAM_READER
18
//#define A32_BITSTREAM_READER
19
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
20

    
21
#ifdef HAVE_AV_CONFIG_H
22
/* only include the following when compiling package */
23
#    include "config.h"
24

    
25
#    include <stdlib.h>
26
#    include <stdio.h>
27
#    include <string.h>
28
#    include <ctype.h>
29
#    ifndef __BEOS__
30
#        include <errno.h>
31
#    else
32
#        include "berrno.h"
33
#    endif
34
#    include <math.h>
35

    
36
#    ifndef ENODATA
37
#        define ENODATA  61
38
#    endif
39

    
40
#ifndef M_PI
41
#define M_PI    3.14159265358979323846
42
#endif
43

    
44
#include <stddef.h>
45
#ifndef offsetof
46
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
47
#endif
48

    
49
#define AVOPTION_CODEC_BOOL(name, help, field) \
50
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
51
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
52
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
53
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
54
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
55
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
56
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
57
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
58
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
59
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
60
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
61
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
62
#define AVOPTION_END() AVOPTION_SUB(NULL)
63

    
64
struct AVOption;
65
#ifdef HAVE_MMX
66
extern const struct AVOption avoptions_common[3 + 5];
67
#else
68
extern const struct AVOption avoptions_common[3];
69
#endif
70
extern const struct AVOption avoptions_workaround_bug[11];
71

    
72
#endif /* HAVE_AV_CONFIG_H */
73

    
74
/* Suppress restrict if it was not defined in config.h.  */
75
#ifndef restrict
76
#    define restrict
77
#endif
78

    
79
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
80
#    define always_inline __attribute__((always_inline)) inline
81
#else
82
#    define always_inline inline
83
#endif
84

    
85
#ifdef EMULATE_FAST_INT
86
/* note that we don't emulate 64bit ints */
87
typedef signed char int_fast8_t;
88
typedef signed int  int_fast16_t;
89
typedef signed int  int_fast32_t;
90
typedef unsigned char uint_fast8_t;
91
typedef unsigned int  uint_fast16_t;
92
typedef unsigned int  uint_fast32_t;
93
#endif
94

    
95
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
96
static inline float floorf(float f) { 
97
    return floor(f); 
98
}
99
#endif
100

    
101
#ifdef CONFIG_WIN32
102

    
103
/* windows */
104

    
105
typedef unsigned short uint16_t;
106
typedef signed short int16_t;
107
typedef unsigned char uint8_t;
108
typedef unsigned int uint32_t;
109
typedef unsigned __int64 uint64_t;
110
typedef signed char int8_t;
111
typedef signed int int32_t;
112
typedef signed __int64 int64_t;
113

    
114
#    ifndef __MINGW32__
115
#        define int64_t_C(c)     (c ## i64)
116
#        define uint64_t_C(c)    (c ## i64)
117

    
118
#    ifdef HAVE_AV_CONFIG_H
119
#            define inline __inline
120
#    endif
121

    
122
#    else
123
#        define int64_t_C(c)     (c ## LL)
124
#        define uint64_t_C(c)    (c ## ULL)
125
#    endif /* __MINGW32__ */
126

    
127
#    ifdef HAVE_AV_CONFIG_H
128
#        ifdef _DEBUG
129
#            define DEBUG
130
#        endif
131

    
132
#        define snprintf _snprintf
133
#        define vsnprintf _vsnprintf
134
#    endif
135

    
136
/* CONFIG_WIN32 end */
137
#elif defined (CONFIG_OS2)
138
/* OS/2 EMX */
139

    
140
#include <inttypes.h>
141

    
142
#ifndef int64_t_C
143
#define int64_t_C(c)     (c ## LL)
144
#define uint64_t_C(c)    (c ## ULL)
145
#endif
146

    
147
#ifdef HAVE_AV_CONFIG_H
148

    
149
#ifdef USE_FASTMEMCPY
150
#include "fastmemcpy.h"
151
#endif
152

    
153
#include <float.h>
154

    
155
#endif /* HAVE_AV_CONFIG_H */
156

    
157
/* CONFIG_OS2 end */
158
#else
159

    
160
/* unix */
161

    
162
#include <inttypes.h>
163

    
164
#ifndef int64_t_C
165
#define int64_t_C(c)     (c ## LL)
166
#define uint64_t_C(c)    (c ## ULL)
167
#endif
168

    
169
#ifdef HAVE_AV_CONFIG_H
170

    
171
#        ifdef USE_FASTMEMCPY
172
#            include "fastmemcpy.h"
173
#        endif
174
#    endif /* HAVE_AV_CONFIG_H */
175

    
176
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
177

    
178
#ifdef HAVE_AV_CONFIG_H
179

    
180
#    include "bswap.h"
181

    
182
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
183
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
184
#        define MANGLE(a) "_" #a
185
#    else
186
#        define MANGLE(a) #a
187
#    endif
188

    
189
/* debug stuff */
190

    
191
#    ifndef DEBUG
192
#        define NDEBUG
193
#    endif
194
#    include <assert.h>
195

    
196
/* dprintf macros */
197
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
198

    
199
inline void dprintf(const char* fmt,...) {}
200

    
201
#    else
202

    
203
#        ifdef DEBUG
204
#            define dprintf(fmt,...) printf(fmt, __VA_ARGS__)
205
#        else
206
#            define dprintf(fmt,...)
207
#        endif
208

    
209
#    endif /* !CONFIG_WIN32 */
210

    
211
#    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
212

    
213
//rounded divison & shift
214
#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
215
/* assume b>0 */
216
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
217
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
218

    
219
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
220
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
221

    
222
extern const uint32_t inverse[256];
223

    
224
#ifdef ARCH_X86
225
#    define FASTDIV(a,b) \
226
    ({\
227
        int ret,dmy;\
228
        asm volatile(\
229
            "mull %3"\
230
            :"=d"(ret),"=a"(dmy)\
231
            :"1"(a),"g"(inverse[b])\
232
            );\
233
        ret;\
234
    })
235
#elif defined(CONFIG_FASTDIV)
236
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
237
#else
238
#    define FASTDIV(a,b)   ((a)/(b))
239
#endif
240
 
241
#ifdef ARCH_X86
242
// avoid +32 for shift optimization (gcc should do that ...)
243
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
244
    asm ("sarl %1, %0\n\t"
245
         : "+r" (a)
246
         : "ic" ((uint8_t)(-s))
247
    );
248
    return a;
249
}
250
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
251
    asm ("shrl %1, %0\n\t"
252
         : "+r" (a)
253
         : "ic" ((uint8_t)(-s))
254
    );
255
    return a;
256
}
257
#else
258
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
259
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
260
#endif
261

    
262
/* bit output */
263

    
264
struct PutBitContext;
265

    
266
typedef void (*WriteDataFunc)(void *, uint8_t *, int);
267

    
268
typedef struct PutBitContext {
269
#ifdef ALT_BITSTREAM_WRITER
270
    uint8_t *buf, *buf_end;
271
    int index;
272
#else
273
    uint32_t bit_buf;
274
    int bit_left;
275
    uint8_t *buf, *buf_ptr, *buf_end;
276
#endif
277
} PutBitContext;
278

    
279
void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
280

    
281
int get_bit_count(PutBitContext *s); /* XXX: change function name */
282
void align_put_bits(PutBitContext *s);
283
void flush_put_bits(PutBitContext *s);
284
void put_string(PutBitContext * pbc, char *s);
285

    
286
/* bit input */
287

    
288
typedef struct GetBitContext {
289
    const uint8_t *buffer, *buffer_end;
290
#ifdef ALT_BITSTREAM_READER
291
    int index;
292
#elif defined LIBMPEG2_BITSTREAM_READER
293
    uint8_t *buffer_ptr;
294
    uint32_t cache;
295
    int bit_count;
296
#elif defined A32_BITSTREAM_READER
297
    uint32_t *buffer_ptr;
298
    uint32_t cache0;
299
    uint32_t cache1;
300
    int bit_count;
301
#endif
302
    int size_in_bits;
303
} GetBitContext;
304

    
305
static inline int get_bits_count(GetBitContext *s);
306

    
307
#define VLC_TYPE int16_t
308

    
309
typedef struct VLC {
310
    int bits;
311
    VLC_TYPE (*table)[2]; ///< code, bits
312
    int table_size, table_allocated;
313
} VLC;
314

    
315
typedef struct RL_VLC_ELEM {
316
    int16_t level;
317
    int8_t len;
318
    uint8_t run;
319
} RL_VLC_ELEM;
320

    
321
#ifdef ARCH_SPARC64
322
#define UNALIGNED_STORES_ARE_BAD
323
#endif
324

    
325
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
326
#ifdef ARCH_X86
327
#    define unaligned32(a) (*(uint32_t*)(a))
328
#else
329
#    ifdef __GNUC__
330
static inline uint32_t unaligned32(const void *v) {
331
    struct Unaligned {
332
        uint32_t i;
333
    } __attribute__((packed));
334

    
335
    return ((const struct Unaligned *) v)->i;
336
}
337
#    elif defined(__DECC)
338
static inline uint32_t unaligned32(const void *v) {
339
    return *(const __unaligned uint32_t *) v;
340
}
341
#    else
342
static inline uint32_t unaligned32(const void *v) {
343
    return *(const uint32_t *) v;
344
}
345
#    endif
346
#endif //!ARCH_X86
347

    
348
#ifndef ALT_BITSTREAM_WRITER
349
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
350
{
351
    unsigned int bit_buf;
352
    int bit_left;
353

    
354
#ifdef STATS
355
    st_out_bit_counts[st_current_index] += n;
356
#endif
357
    //    printf("put_bits=%d %x\n", n, value);
358
    assert(n == 32 || value < (1U << n));
359
    
360
    bit_buf = s->bit_buf;
361
    bit_left = s->bit_left;
362

    
363
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
364
    /* XXX: optimize */
365
    if (n < bit_left) {
366
        bit_buf = (bit_buf<<n) | value;
367
        bit_left-=n;
368
    } else {
369
        bit_buf<<=bit_left;
370
        bit_buf |= value >> (n - bit_left);
371
#ifdef UNALIGNED_STORES_ARE_BAD
372
        if (3 & (int) s->buf_ptr) {
373
            s->buf_ptr[0] = bit_buf >> 24;
374
            s->buf_ptr[1] = bit_buf >> 16;
375
            s->buf_ptr[2] = bit_buf >>  8;
376
            s->buf_ptr[3] = bit_buf      ;
377
        } else
378
#endif
379
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
380
        //printf("bitbuf = %08x\n", bit_buf);
381
        s->buf_ptr+=4;
382
        bit_left+=32 - n;
383
        bit_buf = value;
384
    }
385

    
386
    s->bit_buf = bit_buf;
387
    s->bit_left = bit_left;
388
}
389
#endif
390

    
391

    
392
#ifdef ALT_BITSTREAM_WRITER
393
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
394
{
395
#    ifdef ALIGNED_BITSTREAM_WRITER
396
#        ifdef ARCH_X86
397
    asm volatile(
398
        "movl %0, %%ecx                        \n\t"
399
        "xorl %%eax, %%eax                \n\t"
400
        "shrdl %%cl, %1, %%eax                \n\t"
401
        "shrl %%cl, %1                        \n\t"
402
        "movl %0, %%ecx                        \n\t"
403
        "shrl $3, %%ecx                        \n\t"
404
        "andl $0xFFFFFFFC, %%ecx        \n\t"
405
        "bswapl %1                        \n\t"
406
        "orl %1, (%2, %%ecx)                \n\t"
407
        "bswapl %%eax                        \n\t"
408
        "addl %3, %0                        \n\t"
409
        "movl %%eax, 4(%2, %%ecx)        \n\t"
410
        : "=&r" (s->index), "=&r" (value)
411
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
412
        : "%eax", "%ecx"
413
    );
414
#        else
415
    int index= s->index;
416
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
417
    
418
    value<<= 32-n; 
419
    
420
    ptr[0] |= be2me_32(value>>(index&31));
421
    ptr[1]  = be2me_32(value<<(32-(index&31)));
422
//if(n>24) printf("%d %d\n", n, value);
423
    index+= n;
424
    s->index= index;
425
#        endif
426
#    else //ALIGNED_BITSTREAM_WRITER
427
#        ifdef ARCH_X86
428
    asm volatile(
429
        "movl $7, %%ecx                        \n\t"
430
        "andl %0, %%ecx                        \n\t"
431
        "addl %3, %%ecx                        \n\t"
432
        "negl %%ecx                        \n\t"
433
        "shll %%cl, %1                        \n\t"
434
        "bswapl %1                        \n\t"
435
        "movl %0, %%ecx                        \n\t"
436
        "shrl $3, %%ecx                        \n\t"
437
        "orl %1, (%%ecx, %2)                \n\t"
438
        "addl %3, %0                        \n\t"
439
        "movl $0, 4(%%ecx, %2)                \n\t"
440
        : "=&r" (s->index), "=&r" (value)
441
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
442
        : "%ecx"
443
    );
444
#        else
445
    int index= s->index;
446
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
447
    
448
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
449
    ptr[1] = 0;
450
//if(n>24) printf("%d %d\n", n, value);
451
    index+= n;
452
    s->index= index;
453
#        endif
454
#    endif //!ALIGNED_BITSTREAM_WRITER
455
}
456
#endif
457

    
458

    
459
static inline uint8_t* pbBufPtr(PutBitContext *s)
460
{
461
#ifdef ALT_BITSTREAM_WRITER
462
        return s->buf + (s->index>>3);
463
#else
464
        return s->buf_ptr;
465
#endif
466
}
467

    
468
/* Bitstream reader API docs:
469
name
470
    abritary name which is used as prefix for the internal variables
471

472
gb
473
    getbitcontext
474

475
OPEN_READER(name, gb)
476
    loads gb into local variables
477

478
CLOSE_READER(name, gb)
479
    stores local vars in gb
480

481
UPDATE_CACHE(name, gb)
482
    refills the internal cache from the bitstream
483
    after this call at least MIN_CACHE_BITS will be available,
484

485
GET_CACHE(name, gb)
486
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
487

488
SHOW_UBITS(name, gb, num)
489
    will return the nest num bits
490

491
SHOW_SBITS(name, gb, num)
492
    will return the nest num bits and do sign extension
493

494
SKIP_BITS(name, gb, num)
495
    will skip over the next num bits
496
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
497

498
SKIP_CACHE(name, gb, num)
499
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
500

501
SKIP_COUNTER(name, gb, num)
502
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
503

504
LAST_SKIP_CACHE(name, gb, num)
505
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
506

507
LAST_SKIP_BITS(name, gb, num)
508
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
509

510
for examples see get_bits, show_bits, skip_bits, get_vlc
511
*/
512

    
513
static inline int unaligned32_be(const void *v)
514
{
515
#ifdef CONFIG_ALIGN
516
        const uint8_t *p=v;
517
        return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
518
#else
519
        return be2me_32( unaligned32(v)); //original
520
#endif
521
}
522

    
523
#ifdef ALT_BITSTREAM_READER
524
#   define MIN_CACHE_BITS 25
525

    
526
#   define OPEN_READER(name, gb)\
527
        int name##_index= (gb)->index;\
528
        int name##_cache= 0;\
529

    
530
#   define CLOSE_READER(name, gb)\
531
        (gb)->index= name##_index;\
532

    
533
#   define UPDATE_CACHE(name, gb)\
534
        name##_cache= unaligned32_be( ((uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
535

    
536
#   define SKIP_CACHE(name, gb, num)\
537
        name##_cache <<= (num);\
538

    
539
// FIXME name?
540
#   define SKIP_COUNTER(name, gb, num)\
541
        name##_index += (num);\
542

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

    
549
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
550
#   define LAST_SKIP_CACHE(name, gb, num) ;
551

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

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

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

    
561
static inline int get_bits_count(GetBitContext *s){
562
    return s->index;
563
}
564
#elif defined LIBMPEG2_BITSTREAM_READER
565
//libmpeg2 like reader
566

    
567
#   define MIN_CACHE_BITS 17
568

    
569
#   define OPEN_READER(name, gb)\
570
        int name##_bit_count=(gb)->bit_count;\
571
        int name##_cache= (gb)->cache;\
572
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
573

    
574
#   define CLOSE_READER(name, gb)\
575
        (gb)->bit_count= name##_bit_count;\
576
        (gb)->cache= name##_cache;\
577
        (gb)->buffer_ptr= name##_buffer_ptr;\
578

    
579
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
580

    
581
#   define UPDATE_CACHE(name, gb)\
582
    if(name##_bit_count >= 0){\
583
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
584
        ((uint16_t*)name##_buffer_ptr)++;\
585
        name##_bit_count-= 16;\
586
    }\
587

    
588
#else
589

    
590
#   define UPDATE_CACHE(name, gb)\
591
    if(name##_bit_count >= 0){\
592
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
593
        name##_buffer_ptr+=2;\
594
        name##_bit_count-= 16;\
595
    }\
596

    
597
#endif
598

    
599
#   define SKIP_CACHE(name, gb, num)\
600
        name##_cache <<= (num);\
601

    
602
#   define SKIP_COUNTER(name, gb, num)\
603
        name##_bit_count += (num);\
604

    
605
#   define SKIP_BITS(name, gb, num)\
606
        {\
607
            SKIP_CACHE(name, gb, num)\
608
            SKIP_COUNTER(name, gb, num)\
609
        }\
610

    
611
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
612
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
613

    
614
#   define SHOW_UBITS(name, gb, num)\
615
        NEG_USR32(name##_cache, num)
616

    
617
#   define SHOW_SBITS(name, gb, num)\
618
        NEG_SSR32(name##_cache, num)
619

    
620
#   define GET_CACHE(name, gb)\
621
        ((uint32_t)name##_cache)
622

    
623
static inline int get_bits_count(GetBitContext *s){
624
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
625
}
626

    
627
#elif defined A32_BITSTREAM_READER
628

    
629
#   define MIN_CACHE_BITS 32
630

    
631
#   define OPEN_READER(name, gb)\
632
        int name##_bit_count=(gb)->bit_count;\
633
        uint32_t name##_cache0= (gb)->cache0;\
634
        uint32_t name##_cache1= (gb)->cache1;\
635
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
636

    
637
#   define CLOSE_READER(name, gb)\
638
        (gb)->bit_count= name##_bit_count;\
639
        (gb)->cache0= name##_cache0;\
640
        (gb)->cache1= name##_cache1;\
641
        (gb)->buffer_ptr= name##_buffer_ptr;\
642

    
643
#   define UPDATE_CACHE(name, gb)\
644
    if(name##_bit_count > 0){\
645
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
646
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
647
        name##_cache1 |= next<<name##_bit_count;\
648
        name##_buffer_ptr++;\
649
        name##_bit_count-= 32;\
650
    }\
651

    
652
#ifdef ARCH_X86
653
#   define SKIP_CACHE(name, gb, num)\
654
        asm(\
655
            "shldl %2, %1, %0                \n\t"\
656
            "shll %2, %1                \n\t"\
657
            : "+r" (name##_cache0), "+r" (name##_cache1)\
658
            : "Ic" ((uint8_t)num)\
659
           );
660
#else
661
#   define SKIP_CACHE(name, gb, num)\
662
        name##_cache0 <<= (num);\
663
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
664
        name##_cache1 <<= (num);
665
#endif
666

    
667
#   define SKIP_COUNTER(name, gb, num)\
668
        name##_bit_count += (num);\
669

    
670
#   define SKIP_BITS(name, gb, num)\
671
        {\
672
            SKIP_CACHE(name, gb, num)\
673
            SKIP_COUNTER(name, gb, num)\
674
        }\
675

    
676
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
677
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
678

    
679
#   define SHOW_UBITS(name, gb, num)\
680
        NEG_USR32(name##_cache0, num)
681

    
682
#   define SHOW_SBITS(name, gb, num)\
683
        NEG_SSR32(name##_cache0, num)
684

    
685
#   define GET_CACHE(name, gb)\
686
        (name##_cache0)
687

    
688
static inline int get_bits_count(GetBitContext *s){
689
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
690
}
691

    
692
#endif
693

    
694
/**
695
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
696
 * if MSB not set it is negative 
697
 * @param n length in bits
698
 * @author BERO  
699
 */
700
static inline int get_xbits(GetBitContext *s, int n){
701
    register int tmp;
702
    register int32_t cache;
703
    OPEN_READER(re, s)
704
    UPDATE_CACHE(re, s)
705
    cache = GET_CACHE(re,s);
706
    if ((int32_t)cache<0) { //MSB=1
707
        tmp = NEG_USR32(cache,n);
708
    } else {
709
    //   tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
710
    //   tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
711
        tmp = - NEG_USR32(~cache,n);
712
    }
713
    LAST_SKIP_BITS(re, s, n)
714
    CLOSE_READER(re, s)
715
    return tmp;
716
}
717

    
718
static inline int get_sbits(GetBitContext *s, int n){
719
    register int tmp;
720
    OPEN_READER(re, s)
721
    UPDATE_CACHE(re, s)
722
    tmp= SHOW_SBITS(re, s, n);
723
    LAST_SKIP_BITS(re, s, n)
724
    CLOSE_READER(re, s)
725
    return tmp;
726
}
727

    
728
/**
729
 * reads 0-17 bits.
730
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
731
 */
732
static inline unsigned int get_bits(GetBitContext *s, int n){
733
    register int tmp;
734
    OPEN_READER(re, s)
735
    UPDATE_CACHE(re, s)
736
    tmp= SHOW_UBITS(re, s, n);
737
    LAST_SKIP_BITS(re, s, n)
738
    CLOSE_READER(re, s)
739
    return tmp;
740
}
741

    
742
unsigned int get_bits_long(GetBitContext *s, int n);
743

    
744
/**
745
 * shows 0-17 bits.
746
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
747
 */
748
static inline unsigned int show_bits(GetBitContext *s, int n){
749
    register int tmp;
750
    OPEN_READER(re, s)
751
    UPDATE_CACHE(re, s)
752
    tmp= SHOW_UBITS(re, s, n);
753
//    CLOSE_READER(re, s)
754
    return tmp;
755
}
756

    
757
unsigned int show_bits_long(GetBitContext *s, int n);
758

    
759
static inline void skip_bits(GetBitContext *s, int n){
760
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
761
    OPEN_READER(re, s)
762
    UPDATE_CACHE(re, s)
763
    LAST_SKIP_BITS(re, s, n)
764
    CLOSE_READER(re, s)
765
}
766

    
767
static inline unsigned int get_bits1(GetBitContext *s){
768
#ifdef ALT_BITSTREAM_READER
769
    int index= s->index;
770
    uint8_t result= s->buffer[ index>>3 ];
771
    result<<= (index&0x07);
772
    result>>= 8 - 1;
773
    index++;
774
    s->index= index;
775

    
776
    return result;
777
#else
778
    return get_bits(s, 1);
779
#endif
780
}
781

    
782
static inline unsigned int show_bits1(GetBitContext *s){
783
    return show_bits(s, 1);
784
}
785

    
786
static inline void skip_bits1(GetBitContext *s){
787
    skip_bits(s, 1);
788
}
789

    
790
void init_get_bits(GetBitContext *s,
791
                   const uint8_t *buffer, int buffer_size);
792

    
793
int check_marker(GetBitContext *s, const char *msg);
794
void align_get_bits(GetBitContext *s);
795
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
796
             const void *bits, int bits_wrap, int bits_size,
797
             const void *codes, int codes_wrap, int codes_size);
798
void free_vlc(VLC *vlc);
799

    
800
/**
801
 *
802
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
803
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
804
 * is undefined
805
 */
806
#define GET_VLC(code, name, gb, table, bits, max_depth)\
807
{\
808
    int n, index, nb_bits;\
809
\
810
    index= SHOW_UBITS(name, gb, bits);\
811
    code = table[index][0];\
812
    n    = table[index][1];\
813
\
814
    if(max_depth > 1 && n < 0){\
815
        LAST_SKIP_BITS(name, gb, bits)\
816
        UPDATE_CACHE(name, gb)\
817
\
818
        nb_bits = -n;\
819
\
820
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
821
        code = table[index][0];\
822
        n    = table[index][1];\
823
        if(max_depth > 2 && n < 0){\
824
            LAST_SKIP_BITS(name, gb, nb_bits)\
825
            UPDATE_CACHE(name, gb)\
826
\
827
            nb_bits = -n;\
828
\
829
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
830
            code = table[index][0];\
831
            n    = table[index][1];\
832
        }\
833
    }\
834
    SKIP_BITS(name, gb, n)\
835
}
836

    
837
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
838
{\
839
    int n, index, nb_bits;\
840
\
841
    index= SHOW_UBITS(name, gb, bits);\
842
    level = table[index].level;\
843
    n     = table[index].len;\
844
\
845
    if(max_depth > 1 && n < 0){\
846
        LAST_SKIP_BITS(name, gb, bits)\
847
        UPDATE_CACHE(name, gb)\
848
\
849
        nb_bits = -n;\
850
\
851
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
852
        level = table[index].level;\
853
        n     = table[index].len;\
854
    }\
855
    run= table[index].run;\
856
    SKIP_BITS(name, gb, n)\
857
}
858

    
859
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
860
static inline int get_vlc(GetBitContext *s, VLC *vlc)
861
{
862
    int code;
863
    VLC_TYPE (*table)[2]= vlc->table;
864
    
865
    OPEN_READER(re, s)
866
    UPDATE_CACHE(re, s)
867

    
868
    GET_VLC(code, re, s, table, vlc->bits, 3)    
869

    
870
    CLOSE_READER(re, s)
871
    return code;
872
}
873

    
874
/**
875
 * parses a vlc code, faster then get_vlc()
876
 * @param bits is the number of bits which will be read at once, must be 
877
 *             identical to nb_bits in init_vlc()
878
 * @param max_depth is the number of times bits bits must be readed to completly
879
 *                  read the longest vlc code 
880
 *                  = (max_vlc_length + bits - 1) / bits
881
 */
882
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
883
                                  int bits, int max_depth)
884
{
885
    int code;
886
    
887
    OPEN_READER(re, s)
888
    UPDATE_CACHE(re, s)
889

    
890
    GET_VLC(code, re, s, table, bits, max_depth)
891

    
892
    CLOSE_READER(re, s)
893
    return code;
894
}
895

    
896
//#define TRACE
897

    
898
#ifdef TRACE
899

    
900
static inline void print_bin(int bits, int n){
901
    int i;
902
    
903
    for(i=n-1; i>=0; i--){
904
        printf("%d", (bits>>i)&1);
905
    }
906
    for(i=n; i<24; i++)
907
        printf(" ");
908
}
909

    
910
static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
911
    int r= get_bits(s, n);
912
    
913
    print_bin(r, n);
914
    printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
915
    return r;
916
}
917
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
918
    int show= show_bits(s, 24);
919
    int pos= get_bits_count(s);
920
    int r= get_vlc2(s, table, bits, max_depth);
921
    int len= get_bits_count(s) - pos;
922
    int bits2= show>>(24-len);
923
    
924
    print_bin(bits2, len);
925
    
926
    printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
927
    return r;
928
}
929
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
930
    int show= show_bits(s, n);
931
    int r= get_xbits(s, n);
932
    
933
    print_bin(show, n);
934
    printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
935
    return r;
936
}
937

    
938
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
939
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
940
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
941
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
942
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
943

    
944
#define tprintf printf
945

    
946
#else //TRACE
947
#define tprintf(_arg...) {}
948
#endif
949

    
950
/* define it to include statistics code (useful only for optimizing
951
   codec efficiency */
952
//#define STATS
953

    
954
#ifdef STATS
955

    
956
enum {
957
    ST_UNKNOWN,
958
    ST_DC,
959
    ST_INTRA_AC,
960
    ST_INTER_AC,
961
    ST_INTRA_MB,
962
    ST_INTER_MB,
963
    ST_MV,
964
    ST_NB,
965
};
966

    
967
extern int st_current_index;
968
extern unsigned int st_bit_counts[ST_NB];
969
extern unsigned int st_out_bit_counts[ST_NB];
970

    
971
void print_stats(void);
972
#endif
973

    
974
/* misc math functions */
975
extern const uint8_t ff_log2_tab[256];
976

    
977
static inline int av_log2(unsigned int v)
978
{
979
    int n;
980

    
981
    n = 0;
982
    if (v & 0xffff0000) {
983
        v >>= 16;
984
        n += 16;
985
    }
986
    if (v & 0xff00) {
987
        v >>= 8;
988
        n += 8;
989
    }
990
    n += ff_log2_tab[v];
991

    
992
    return n;
993
}
994

    
995
static inline int av_log2_16bit(unsigned int v)
996
{
997
    int n;
998

    
999
    n = 0;
1000
    if (v & 0xff00) {
1001
        v >>= 8;
1002
        n += 8;
1003
    }
1004
    n += ff_log2_tab[v];
1005

    
1006
    return n;
1007
}
1008

    
1009

    
1010
/* median of 3 */
1011
static inline int mid_pred(int a, int b, int c)
1012
{
1013
    int vmin, vmax;
1014
    vmax = vmin = a;
1015
    if (b < vmin)
1016
        vmin = b;
1017
    else
1018
        vmax = b;
1019

    
1020
    if (c < vmin)
1021
        vmin = c;
1022
    else if (c > vmax)
1023
        vmax = c;
1024

    
1025
    return a + b + c - vmin - vmax;
1026
}
1027

    
1028
static inline int clip(int a, int amin, int amax)
1029
{
1030
    if (a < amin)
1031
        return amin;
1032
    else if (a > amax)
1033
        return amax;
1034
    else
1035
        return a;
1036
}
1037

    
1038
/* math */
1039
extern const uint8_t ff_sqrt_tab[128];
1040

    
1041
int64_t ff_gcd(int64_t a, int64_t b);
1042

    
1043
static inline int ff_sqrt(int a)
1044
{
1045
    int ret=0;
1046
    int s;
1047
    int ret_sq=0;
1048
    
1049
    if(a<128) return ff_sqrt_tab[a];
1050
    
1051
    for(s=15; s>=0; s--){
1052
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1053
        if(b<=a){
1054
            ret_sq=b;
1055
            ret+= 1<<s;
1056
        }
1057
    }
1058
    return ret;
1059
}
1060

    
1061
/**
1062
 * converts fourcc string to int
1063
 */
1064
static inline int ff_get_fourcc(const char *s){
1065
    assert( strlen(s)==4 );
1066

    
1067
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1068
}
1069

    
1070
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1071
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1072

    
1073

    
1074
#ifdef ARCH_X86
1075
#define MASK_ABS(mask, level)\
1076
            asm volatile(\
1077
                "cdq                        \n\t"\
1078
                "xorl %1, %0                \n\t"\
1079
                "subl %1, %0                \n\t"\
1080
                : "+a" (level), "=&d" (mask)\
1081
            );
1082
#else
1083
#define MASK_ABS(mask, level)\
1084
            mask= level>>31;\
1085
            level= (level^mask)-mask;
1086
#endif
1087

    
1088

    
1089
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1090
#define COPY3_IF_LT(x,y,a,b,c,d)\
1091
asm volatile (\
1092
    "cmpl %0, %3        \n\t"\
1093
    "cmovl %3, %0        \n\t"\
1094
    "cmovl %4, %1        \n\t"\
1095
    "cmovl %5, %2        \n\t"\
1096
    : "+r" (x), "+r" (a), "+r" (c)\
1097
    : "r" (y), "r" (b), "r" (d)\
1098
);
1099
#else
1100
#define COPY3_IF_LT(x,y,a,b,c,d)\
1101
if((y)<(x)){\
1102
     (x)=(y);\
1103
     (a)=(b);\
1104
     (c)=(d);\
1105
}
1106
#endif
1107

    
1108
#ifdef ARCH_X86
1109
static inline long long rdtsc()
1110
{
1111
        long long l;
1112
        asm volatile(        "rdtsc\n\t"
1113
                : "=A" (l)
1114
        );
1115
        return l;
1116
}
1117

    
1118
#define START_TIMER \
1119
static uint64_t tsum=0;\
1120
static int tcount=0;\
1121
static int tskip_count=0;\
1122
uint64_t tend;\
1123
uint64_t tstart= rdtsc();\
1124

    
1125
#define STOP_TIMER(id) \
1126
tend= rdtsc();\
1127
if(tcount<2 || tend - tstart < 4*tsum/tcount){\
1128
    tsum+= tend - tstart;\
1129
    tcount++;\
1130
}else\
1131
    tskip_count++;\
1132
if(256*256*256*64%(tcount+tskip_count)==0){\
1133
    fprintf(stderr, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1134
}
1135
#endif
1136

    
1137
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1138

    
1139
/* avoid usage of various functions */
1140
#define malloc please_use_av_malloc
1141
#define free please_use_av_free
1142
#define realloc please_use_av_realloc
1143

    
1144
#define CHECKED_ALLOCZ(p, size)\
1145
{\
1146
    p= av_mallocz(size);\
1147
    if(p==NULL && (size)!=0){\
1148
        perror("malloc");\
1149
        goto fail;\
1150
    }\
1151
}
1152

    
1153
#endif /* HAVE_AV_CONFIG_H */
1154

    
1155
#endif /* COMMON_H */