Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ ab35de18

History | View | Annotate | Download (31.4 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
#ifndef M_PI
22
#define M_PI    3.14159265358979323846
23
#endif
24

    
25
#ifdef HAVE_AV_CONFIG_H
26
/* only include the following when compiling package */
27
#    include "config.h"
28

    
29
#    include <stdlib.h>
30
#    include <stdio.h>
31
#    include <string.h>
32
#    include <ctype.h>
33
#    include <limits.h>
34
#    ifndef __BEOS__
35
#        include <errno.h>
36
#    else
37
#        include "berrno.h"
38
#    endif
39
#    include <math.h>
40

    
41
#    ifndef ENODATA
42
#        define ENODATA  61
43
#    endif
44

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

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

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

    
73
#endif /* HAVE_AV_CONFIG_H */
74

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

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

    
88
#ifndef attribute_used
89
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90
#    define attribute_used __attribute__((used))
91
#else
92
#    define attribute_used
93
#endif
94
#endif
95

    
96
#ifndef EMULATE_INTTYPES
97
#   include <inttypes.h>
98
#else
99
    typedef signed char  int8_t;
100
    typedef signed short int16_t;
101
    typedef signed int   int32_t;
102
    typedef unsigned char  uint8_t;
103
    typedef unsigned short uint16_t;
104
    typedef unsigned int   uint32_t;
105

    
106
#   ifdef CONFIG_WIN32
107
        typedef signed __int64   int64_t;
108
        typedef unsigned __int64 uint64_t;
109
#   else /* other OS */
110
        typedef signed long long   int64_t;
111
        typedef unsigned long long uint64_t;
112
#   endif /* other OS */
113
#endif /* HAVE_INTTYPES_H */
114

    
115
#ifndef INT64_MAX
116
#define INT64_MAX int64_t_C(9223372036854775807)
117
#endif
118

    
119
#ifndef UINT64_MAX
120
#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
121
#endif
122

    
123
#ifdef EMULATE_FAST_INT
124
/* note that we don't emulate 64bit ints */
125
typedef signed char int_fast8_t;
126
typedef signed int  int_fast16_t;
127
typedef signed int  int_fast32_t;
128
typedef unsigned char uint_fast8_t;
129
typedef unsigned int  uint_fast16_t;
130
typedef unsigned int  uint_fast32_t;
131
#endif
132

    
133
#ifndef INT_BIT
134
#    if INT_MAX != 2147483647
135
#        define INT_BIT 64
136
#    else
137
#        define INT_BIT 32
138
#    endif
139
#endif
140

    
141
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
142
static inline float floorf(float f) { 
143
    return floor(f); 
144
}
145
#endif
146

    
147
#ifdef CONFIG_WIN32
148

    
149
/* windows */
150

    
151
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
152
#        define int64_t_C(c)     (c ## i64)
153
#        define uint64_t_C(c)    (c ## i64)
154

    
155
#    ifdef HAVE_AV_CONFIG_H
156
#            define inline __inline
157
#    endif
158

    
159
#    else
160
#        define int64_t_C(c)     (c ## LL)
161
#        define uint64_t_C(c)    (c ## ULL)
162
#    endif /* __MINGW32__ */
163

    
164
#    ifdef HAVE_AV_CONFIG_H
165
#        ifdef _DEBUG
166
#            define DEBUG
167
#        endif
168

    
169
#        define snprintf _snprintf
170
#        define vsnprintf _vsnprintf
171
#    endif
172

    
173
/* CONFIG_WIN32 end */
174
#elif defined (CONFIG_OS2)
175
/* OS/2 EMX */
176

    
177
#ifndef int64_t_C
178
#define int64_t_C(c)     (c ## LL)
179
#define uint64_t_C(c)    (c ## ULL)
180
#endif
181

    
182
#ifdef HAVE_AV_CONFIG_H
183

    
184
#ifdef USE_FASTMEMCPY
185
#include "fastmemcpy.h"
186
#endif
187

    
188
#include <float.h>
189

    
190
#endif /* HAVE_AV_CONFIG_H */
191

    
192
/* CONFIG_OS2 end */
193
#else
194

    
195
/* unix */
196

    
197
#ifndef int64_t_C
198
#define int64_t_C(c)     (c ## LL)
199
#define uint64_t_C(c)    (c ## ULL)
200
#endif
201

    
202
#ifdef HAVE_AV_CONFIG_H
203

    
204
#        ifdef USE_FASTMEMCPY
205
#            include "fastmemcpy.h"
206
#        endif
207
#    endif /* HAVE_AV_CONFIG_H */
208

    
209
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
210

    
211
#ifdef HAVE_AV_CONFIG_H
212

    
213
#    include "bswap.h"
214

    
215
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
216
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
217
#        define MANGLE(a) "_" #a
218
#    else
219
#        define MANGLE(a) #a
220
#    endif
221

    
222
/* debug stuff */
223

    
224
#    ifndef DEBUG
225
#        define NDEBUG
226
#    endif
227
#    include <assert.h>
228

    
229
/* dprintf macros */
230
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
231

    
232
inline void dprintf(const char* fmt,...) {}
233

    
234
#    else
235

    
236
#        ifdef DEBUG
237
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
238
#        else
239
#            define dprintf(fmt,...)
240
#        endif
241

    
242
#    endif /* !CONFIG_WIN32 */
243

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

    
246
//rounded divison & shift
247
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
248
/* assume b>0 */
249
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
250
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
251

    
252
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
253
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
254

    
255
extern const uint32_t inverse[256];
256

    
257
#ifdef ARCH_X86
258
#    define FASTDIV(a,b) \
259
    ({\
260
        int ret,dmy;\
261
        asm volatile(\
262
            "mull %3"\
263
            :"=d"(ret),"=a"(dmy)\
264
            :"1"(a),"g"(inverse[b])\
265
            );\
266
        ret;\
267
    })
268
#elif defined(CONFIG_FASTDIV)
269
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
270
#else
271
#    define FASTDIV(a,b)   ((a)/(b))
272
#endif
273
 
274
#ifdef ARCH_X86
275
// avoid +32 for shift optimization (gcc should do that ...)
276
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
277
    asm ("sarl %1, %0\n\t"
278
         : "+r" (a)
279
         : "ic" ((uint8_t)(-s))
280
    );
281
    return a;
282
}
283
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
284
    asm ("shrl %1, %0\n\t"
285
         : "+r" (a)
286
         : "ic" ((uint8_t)(-s))
287
    );
288
    return a;
289
}
290
#else
291
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
292
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
293
#endif
294

    
295
/* bit output */
296

    
297
/* buf and buf_end must be present and used by every alternative writer. */
298
typedef struct PutBitContext {
299
#ifdef ALT_BITSTREAM_WRITER
300
    uint8_t *buf, *buf_end;
301
    int index;
302
#else
303
    uint32_t bit_buf;
304
    int bit_left;
305
    uint8_t *buf, *buf_ptr, *buf_end;
306
#endif
307
} PutBitContext;
308

    
309
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
310
{
311
    s->buf = buffer;
312
    s->buf_end = s->buf + buffer_size;
313
#ifdef ALT_BITSTREAM_WRITER
314
    s->index=0;
315
    ((uint32_t*)(s->buf))[0]=0;
316
//    memset(buffer, 0, buffer_size);
317
#else
318
    s->buf_ptr = s->buf;
319
    s->bit_left=32;
320
    s->bit_buf=0;
321
#endif
322
}
323

    
324
/* return the number of bits output */
325
static inline int put_bits_count(PutBitContext *s)
326
{
327
#ifdef ALT_BITSTREAM_WRITER
328
    return s->index;
329
#else
330
    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
331
#endif
332
}
333

    
334
/* pad the end of the output stream with zeros */
335
static inline void flush_put_bits(PutBitContext *s)
336
{
337
#ifdef ALT_BITSTREAM_WRITER
338
    align_put_bits(s);
339
#else
340
    s->bit_buf<<= s->bit_left;
341
    while (s->bit_left < 32) {
342
        /* XXX: should test end of buffer */
343
        *s->buf_ptr++=s->bit_buf >> 24;
344
        s->bit_buf<<=8;
345
        s->bit_left+=8;
346
    }
347
    s->bit_left=32;
348
    s->bit_buf=0;
349
#endif
350
}
351

    
352
void align_put_bits(PutBitContext *s);
353
void put_string(PutBitContext * pbc, char *s, int put_zero);
354

    
355
/* bit input */
356
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
357
typedef struct GetBitContext {
358
    const uint8_t *buffer, *buffer_end;
359
#ifdef ALT_BITSTREAM_READER
360
    int index;
361
#elif defined LIBMPEG2_BITSTREAM_READER
362
    uint8_t *buffer_ptr;
363
    uint32_t cache;
364
    int bit_count;
365
#elif defined A32_BITSTREAM_READER
366
    uint32_t *buffer_ptr;
367
    uint32_t cache0;
368
    uint32_t cache1;
369
    int bit_count;
370
#endif
371
    int size_in_bits;
372
} GetBitContext;
373

    
374
#define VLC_TYPE int16_t
375

    
376
typedef struct VLC {
377
    int bits;
378
    VLC_TYPE (*table)[2]; ///< code, bits
379
    int table_size, table_allocated;
380
} VLC;
381

    
382
typedef struct RL_VLC_ELEM {
383
    int16_t level;
384
    int8_t len;
385
    uint8_t run;
386
} RL_VLC_ELEM;
387

    
388
#ifdef ARCH_SPARC
389
#define UNALIGNED_STORES_ARE_BAD
390
#endif
391

    
392
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
393
#ifdef ARCH_X86
394
#    define unaligned32(a) (*(uint32_t*)(a))
395
#else
396
#    ifdef __GNUC__
397
static inline uint32_t unaligned32(const void *v) {
398
    struct Unaligned {
399
        uint32_t i;
400
    } __attribute__((packed));
401

    
402
    return ((const struct Unaligned *) v)->i;
403
}
404
#    elif defined(__DECC)
405
static inline uint32_t unaligned32(const void *v) {
406
    return *(const __unaligned uint32_t *) v;
407
}
408
#    else
409
static inline uint32_t unaligned32(const void *v) {
410
    return *(const uint32_t *) v;
411
}
412
#    endif
413
#endif //!ARCH_X86
414

    
415
#ifndef ALT_BITSTREAM_WRITER
416
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
417
{
418
    unsigned int bit_buf;
419
    int bit_left;
420

    
421
#ifdef STATS
422
    st_out_bit_counts[st_current_index] += n;
423
#endif
424
    //    printf("put_bits=%d %x\n", n, value);
425
    assert(n == 32 || value < (1U << n));
426
    
427
    bit_buf = s->bit_buf;
428
    bit_left = s->bit_left;
429

    
430
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
431
    /* XXX: optimize */
432
    if (n < bit_left) {
433
        bit_buf = (bit_buf<<n) | value;
434
        bit_left-=n;
435
    } else {
436
        bit_buf<<=bit_left;
437
        bit_buf |= value >> (n - bit_left);
438
#ifdef UNALIGNED_STORES_ARE_BAD
439
        if (3 & (intptr_t) s->buf_ptr) {
440
            s->buf_ptr[0] = bit_buf >> 24;
441
            s->buf_ptr[1] = bit_buf >> 16;
442
            s->buf_ptr[2] = bit_buf >>  8;
443
            s->buf_ptr[3] = bit_buf      ;
444
        } else
445
#endif
446
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
447
        //printf("bitbuf = %08x\n", bit_buf);
448
        s->buf_ptr+=4;
449
        bit_left+=32 - n;
450
        bit_buf = value;
451
    }
452

    
453
    s->bit_buf = bit_buf;
454
    s->bit_left = bit_left;
455
}
456
#endif
457

    
458

    
459
#ifdef ALT_BITSTREAM_WRITER
460
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
461
{
462
#    ifdef ALIGNED_BITSTREAM_WRITER
463
#        ifdef ARCH_X86
464
    asm volatile(
465
        "movl %0, %%ecx                        \n\t"
466
        "xorl %%eax, %%eax                \n\t"
467
        "shrdl %%cl, %1, %%eax                \n\t"
468
        "shrl %%cl, %1                        \n\t"
469
        "movl %0, %%ecx                        \n\t"
470
        "shrl $3, %%ecx                        \n\t"
471
        "andl $0xFFFFFFFC, %%ecx        \n\t"
472
        "bswapl %1                        \n\t"
473
        "orl %1, (%2, %%ecx)                \n\t"
474
        "bswapl %%eax                        \n\t"
475
        "addl %3, %0                        \n\t"
476
        "movl %%eax, 4(%2, %%ecx)        \n\t"
477
        : "=&r" (s->index), "=&r" (value)
478
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
479
        : "%eax", "%ecx"
480
    );
481
#        else
482
    int index= s->index;
483
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
484
    
485
    value<<= 32-n; 
486
    
487
    ptr[0] |= be2me_32(value>>(index&31));
488
    ptr[1]  = be2me_32(value<<(32-(index&31)));
489
//if(n>24) printf("%d %d\n", n, value);
490
    index+= n;
491
    s->index= index;
492
#        endif
493
#    else //ALIGNED_BITSTREAM_WRITER
494
#        ifdef ARCH_X86
495
    asm volatile(
496
        "movl $7, %%ecx                        \n\t"
497
        "andl %0, %%ecx                        \n\t"
498
        "addl %3, %%ecx                        \n\t"
499
        "negl %%ecx                        \n\t"
500
        "shll %%cl, %1                        \n\t"
501
        "bswapl %1                        \n\t"
502
        "movl %0, %%ecx                        \n\t"
503
        "shrl $3, %%ecx                        \n\t"
504
        "orl %1, (%%ecx, %2)                \n\t"
505
        "addl %3, %0                        \n\t"
506
        "movl $0, 4(%%ecx, %2)                \n\t"
507
        : "=&r" (s->index), "=&r" (value)
508
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
509
        : "%ecx"
510
    );
511
#        else
512
    int index= s->index;
513
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
514
    
515
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
516
    ptr[1] = 0;
517
//if(n>24) printf("%d %d\n", n, value);
518
    index+= n;
519
    s->index= index;
520
#        endif
521
#    endif //!ALIGNED_BITSTREAM_WRITER
522
}
523
#endif
524

    
525

    
526
static inline uint8_t* pbBufPtr(PutBitContext *s)
527
{
528
#ifdef ALT_BITSTREAM_WRITER
529
        return s->buf + (s->index>>3);
530
#else
531
        return s->buf_ptr;
532
#endif
533
}
534

    
535
/**
536
 *
537
 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
538
 */
539
static inline void skip_put_bytes(PutBitContext *s, int n){
540
        assert((put_bits_count(s)&7)==0);
541
#ifdef ALT_BITSTREAM_WRITER
542
        FIXME may need some cleaning of the buffer
543
        s->index += n<<3;
544
#else
545
        assert(s->bit_left==32);
546
        s->buf_ptr += n;
547
#endif    
548
}
549

    
550
/**
551
 * Changes the end of the buffer.
552
 */
553
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
554
    s->buf_end= s->buf + size;
555
}
556

    
557
/* Bitstream reader API docs:
558
name
559
    abritary name which is used as prefix for the internal variables
560

561
gb
562
    getbitcontext
563

564
OPEN_READER(name, gb)
565
    loads gb into local variables
566

567
CLOSE_READER(name, gb)
568
    stores local vars in gb
569

570
UPDATE_CACHE(name, gb)
571
    refills the internal cache from the bitstream
572
    after this call at least MIN_CACHE_BITS will be available,
573

574
GET_CACHE(name, gb)
575
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
576

577
SHOW_UBITS(name, gb, num)
578
    will return the nest num bits
579

580
SHOW_SBITS(name, gb, num)
581
    will return the nest num bits and do sign extension
582

583
SKIP_BITS(name, gb, num)
584
    will skip over the next num bits
585
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
586

587
SKIP_CACHE(name, gb, num)
588
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
589

590
SKIP_COUNTER(name, gb, num)
591
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
592

593
LAST_SKIP_CACHE(name, gb, num)
594
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
595

596
LAST_SKIP_BITS(name, gb, num)
597
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
598

599
for examples see get_bits, show_bits, skip_bits, get_vlc
600
*/
601

    
602
static inline int unaligned32_be(const void *v)
603
{
604
#ifdef CONFIG_ALIGN
605
        const uint8_t *p=v;
606
        return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
607
#else
608
        return be2me_32( unaligned32(v)); //original
609
#endif
610
}
611

    
612
#ifdef ALT_BITSTREAM_READER
613
#   define MIN_CACHE_BITS 25
614

    
615
#   define OPEN_READER(name, gb)\
616
        int name##_index= (gb)->index;\
617
        int name##_cache= 0;\
618

    
619
#   define CLOSE_READER(name, gb)\
620
        (gb)->index= name##_index;\
621

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

    
625
#   define SKIP_CACHE(name, gb, num)\
626
        name##_cache <<= (num);\
627

    
628
// FIXME name?
629
#   define SKIP_COUNTER(name, gb, num)\
630
        name##_index += (num);\
631

    
632
#   define SKIP_BITS(name, gb, num)\
633
        {\
634
            SKIP_CACHE(name, gb, num)\
635
            SKIP_COUNTER(name, gb, num)\
636
        }\
637

    
638
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
639
#   define LAST_SKIP_CACHE(name, gb, num) ;
640

    
641
#   define SHOW_UBITS(name, gb, num)\
642
        NEG_USR32(name##_cache, num)
643

    
644
#   define SHOW_SBITS(name, gb, num)\
645
        NEG_SSR32(name##_cache, num)
646

    
647
#   define GET_CACHE(name, gb)\
648
        ((uint32_t)name##_cache)
649

    
650
static inline int get_bits_count(GetBitContext *s){
651
    return s->index;
652
}
653
#elif defined LIBMPEG2_BITSTREAM_READER
654
//libmpeg2 like reader
655

    
656
#   define MIN_CACHE_BITS 17
657

    
658
#   define OPEN_READER(name, gb)\
659
        int name##_bit_count=(gb)->bit_count;\
660
        int name##_cache= (gb)->cache;\
661
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
662

    
663
#   define CLOSE_READER(name, gb)\
664
        (gb)->bit_count= name##_bit_count;\
665
        (gb)->cache= name##_cache;\
666
        (gb)->buffer_ptr= name##_buffer_ptr;\
667

    
668
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
669

    
670
#   define UPDATE_CACHE(name, gb)\
671
    if(name##_bit_count >= 0){\
672
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
673
        ((uint16_t*)name##_buffer_ptr)++;\
674
        name##_bit_count-= 16;\
675
    }\
676

    
677
#else
678

    
679
#   define UPDATE_CACHE(name, gb)\
680
    if(name##_bit_count >= 0){\
681
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
682
        name##_buffer_ptr+=2;\
683
        name##_bit_count-= 16;\
684
    }\
685

    
686
#endif
687

    
688
#   define SKIP_CACHE(name, gb, num)\
689
        name##_cache <<= (num);\
690

    
691
#   define SKIP_COUNTER(name, gb, num)\
692
        name##_bit_count += (num);\
693

    
694
#   define SKIP_BITS(name, gb, num)\
695
        {\
696
            SKIP_CACHE(name, gb, num)\
697
            SKIP_COUNTER(name, gb, num)\
698
        }\
699

    
700
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
701
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
702

    
703
#   define SHOW_UBITS(name, gb, num)\
704
        NEG_USR32(name##_cache, num)
705

    
706
#   define SHOW_SBITS(name, gb, num)\
707
        NEG_SSR32(name##_cache, num)
708

    
709
#   define GET_CACHE(name, gb)\
710
        ((uint32_t)name##_cache)
711

    
712
static inline int get_bits_count(GetBitContext *s){
713
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
714
}
715

    
716
#elif defined A32_BITSTREAM_READER
717

    
718
#   define MIN_CACHE_BITS 32
719

    
720
#   define OPEN_READER(name, gb)\
721
        int name##_bit_count=(gb)->bit_count;\
722
        uint32_t name##_cache0= (gb)->cache0;\
723
        uint32_t name##_cache1= (gb)->cache1;\
724
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
725

    
726
#   define CLOSE_READER(name, gb)\
727
        (gb)->bit_count= name##_bit_count;\
728
        (gb)->cache0= name##_cache0;\
729
        (gb)->cache1= name##_cache1;\
730
        (gb)->buffer_ptr= name##_buffer_ptr;\
731

    
732
#   define UPDATE_CACHE(name, gb)\
733
    if(name##_bit_count > 0){\
734
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
735
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
736
        name##_cache1 |= next<<name##_bit_count;\
737
        name##_buffer_ptr++;\
738
        name##_bit_count-= 32;\
739
    }\
740

    
741
#ifdef ARCH_X86
742
#   define SKIP_CACHE(name, gb, num)\
743
        asm(\
744
            "shldl %2, %1, %0                \n\t"\
745
            "shll %2, %1                \n\t"\
746
            : "+r" (name##_cache0), "+r" (name##_cache1)\
747
            : "Ic" ((uint8_t)num)\
748
           );
749
#else
750
#   define SKIP_CACHE(name, gb, num)\
751
        name##_cache0 <<= (num);\
752
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
753
        name##_cache1 <<= (num);
754
#endif
755

    
756
#   define SKIP_COUNTER(name, gb, num)\
757
        name##_bit_count += (num);\
758

    
759
#   define SKIP_BITS(name, gb, num)\
760
        {\
761
            SKIP_CACHE(name, gb, num)\
762
            SKIP_COUNTER(name, gb, num)\
763
        }\
764

    
765
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
766
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
767

    
768
#   define SHOW_UBITS(name, gb, num)\
769
        NEG_USR32(name##_cache0, num)
770

    
771
#   define SHOW_SBITS(name, gb, num)\
772
        NEG_SSR32(name##_cache0, num)
773

    
774
#   define GET_CACHE(name, gb)\
775
        (name##_cache0)
776

    
777
static inline int get_bits_count(GetBitContext *s){
778
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
779
}
780

    
781
#endif
782

    
783
/**
784
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
785
 * if MSB not set it is negative 
786
 * @param n length in bits
787
 * @author BERO  
788
 */
789
static inline int get_xbits(GetBitContext *s, int n){
790
    register int tmp;
791
    register int32_t cache;
792
    OPEN_READER(re, s)
793
    UPDATE_CACHE(re, s)
794
    cache = GET_CACHE(re,s);
795
    if ((int32_t)cache<0) { //MSB=1
796
        tmp = NEG_USR32(cache,n);
797
    } else {
798
    //   tmp = (-1<<n) | NEG_USR32(cache,n) + 1; mpeg12.c algo
799
    //   tmp = - (NEG_USR32(cache,n) ^ ((1 << n) - 1)); h263.c algo
800
        tmp = - NEG_USR32(~cache,n);
801
    }
802
    LAST_SKIP_BITS(re, s, n)
803
    CLOSE_READER(re, s)
804
    return tmp;
805
}
806

    
807
static inline int get_sbits(GetBitContext *s, int n){
808
    register int tmp;
809
    OPEN_READER(re, s)
810
    UPDATE_CACHE(re, s)
811
    tmp= SHOW_SBITS(re, s, n);
812
    LAST_SKIP_BITS(re, s, n)
813
    CLOSE_READER(re, s)
814
    return tmp;
815
}
816

    
817
/**
818
 * reads 0-17 bits.
819
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
820
 */
821
static inline unsigned int get_bits(GetBitContext *s, int n){
822
    register int tmp;
823
    OPEN_READER(re, s)
824
    UPDATE_CACHE(re, s)
825
    tmp= SHOW_UBITS(re, s, n);
826
    LAST_SKIP_BITS(re, s, n)
827
    CLOSE_READER(re, s)
828
    return tmp;
829
}
830

    
831
unsigned int get_bits_long(GetBitContext *s, int n);
832

    
833
/**
834
 * shows 0-17 bits.
835
 * Note, the alt bitstream reader can read upto 25 bits, but the libmpeg2 reader cant
836
 */
837
static inline unsigned int show_bits(GetBitContext *s, int n){
838
    register int tmp;
839
    OPEN_READER(re, s)
840
    UPDATE_CACHE(re, s)
841
    tmp= SHOW_UBITS(re, s, n);
842
//    CLOSE_READER(re, s)
843
    return tmp;
844
}
845

    
846
unsigned int show_bits_long(GetBitContext *s, int n);
847

    
848
static inline void skip_bits(GetBitContext *s, int n){
849
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
850
    OPEN_READER(re, s)
851
    UPDATE_CACHE(re, s)
852
    LAST_SKIP_BITS(re, s, n)
853
    CLOSE_READER(re, s)
854
}
855

    
856
static inline unsigned int get_bits1(GetBitContext *s){
857
#ifdef ALT_BITSTREAM_READER
858
    int index= s->index;
859
    uint8_t result= s->buffer[ index>>3 ];
860
    result<<= (index&0x07);
861
    result>>= 8 - 1;
862
    index++;
863
    s->index= index;
864

    
865
    return result;
866
#else
867
    return get_bits(s, 1);
868
#endif
869
}
870

    
871
static inline unsigned int show_bits1(GetBitContext *s){
872
    return show_bits(s, 1);
873
}
874

    
875
static inline void skip_bits1(GetBitContext *s){
876
    skip_bits(s, 1);
877
}
878

    
879
/**
880
 * init GetBitContext.
881
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
882
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
883
 * @param bit_size the size of the buffer in bits
884
 */
885
static inline void init_get_bits(GetBitContext *s,
886
                   const uint8_t *buffer, int bit_size)
887
{
888
    const int buffer_size= (bit_size+7)>>3;
889

    
890
    s->buffer= buffer;
891
    s->size_in_bits= bit_size;
892
    s->buffer_end= buffer + buffer_size;
893
#ifdef ALT_BITSTREAM_READER
894
    s->index=0;
895
#elif defined LIBMPEG2_BITSTREAM_READER
896
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
897
  if ((int)buffer&1) {
898
     /* word alignment */
899
    s->cache = (*buffer++)<<24;
900
    s->buffer_ptr = buffer;
901
    s->bit_count = 16-8;
902
  } else
903
#endif
904
  {
905
    s->buffer_ptr = buffer;
906
    s->bit_count = 16;
907
    s->cache = 0;
908
  }
909
#elif defined A32_BITSTREAM_READER
910
    s->buffer_ptr = (uint32_t*)buffer;
911
    s->bit_count = 32;
912
    s->cache0 = 0;
913
    s->cache1 = 0;
914
#endif
915
    {
916
        OPEN_READER(re, s)
917
        UPDATE_CACHE(re, s)
918
        UPDATE_CACHE(re, s)
919
        CLOSE_READER(re, s)
920
    }
921
#ifdef A32_BITSTREAM_READER
922
    s->cache1 = 0;
923
#endif
924
}
925

    
926
int check_marker(GetBitContext *s, const char *msg);
927
void align_get_bits(GetBitContext *s);
928
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
929
             const void *bits, int bits_wrap, int bits_size,
930
             const void *codes, int codes_wrap, int codes_size);
931
void free_vlc(VLC *vlc);
932

    
933
/**
934
 *
935
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
936
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
937
 * is undefined
938
 */
939
#define GET_VLC(code, name, gb, table, bits, max_depth)\
940
{\
941
    int n, index, nb_bits;\
942
\
943
    index= SHOW_UBITS(name, gb, bits);\
944
    code = table[index][0];\
945
    n    = table[index][1];\
946
\
947
    if(max_depth > 1 && n < 0){\
948
        LAST_SKIP_BITS(name, gb, bits)\
949
        UPDATE_CACHE(name, gb)\
950
\
951
        nb_bits = -n;\
952
\
953
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
954
        code = table[index][0];\
955
        n    = table[index][1];\
956
        if(max_depth > 2 && n < 0){\
957
            LAST_SKIP_BITS(name, gb, nb_bits)\
958
            UPDATE_CACHE(name, gb)\
959
\
960
            nb_bits = -n;\
961
\
962
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
963
            code = table[index][0];\
964
            n    = table[index][1];\
965
        }\
966
    }\
967
    SKIP_BITS(name, gb, n)\
968
}
969

    
970
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
971
{\
972
    int n, index, nb_bits;\
973
\
974
    index= SHOW_UBITS(name, gb, bits);\
975
    level = table[index].level;\
976
    n     = table[index].len;\
977
\
978
    if(max_depth > 1 && n < 0){\
979
        LAST_SKIP_BITS(name, gb, bits)\
980
        UPDATE_CACHE(name, gb)\
981
\
982
        nb_bits = -n;\
983
\
984
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
985
        level = table[index].level;\
986
        n     = table[index].len;\
987
    }\
988
    run= table[index].run;\
989
    SKIP_BITS(name, gb, n)\
990
}
991

    
992
// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
993
static inline int get_vlc(GetBitContext *s, VLC *vlc)
994
{
995
    int code;
996
    VLC_TYPE (*table)[2]= vlc->table;
997
    
998
    OPEN_READER(re, s)
999
    UPDATE_CACHE(re, s)
1000

    
1001
    GET_VLC(code, re, s, table, vlc->bits, 3)    
1002

    
1003
    CLOSE_READER(re, s)
1004
    return code;
1005
}
1006

    
1007
/**
1008
 * parses a vlc code, faster then get_vlc()
1009
 * @param bits is the number of bits which will be read at once, must be 
1010
 *             identical to nb_bits in init_vlc()
1011
 * @param max_depth is the number of times bits bits must be readed to completly
1012
 *                  read the longest vlc code 
1013
 *                  = (max_vlc_length + bits - 1) / bits
1014
 */
1015
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
1016
                                  int bits, int max_depth)
1017
{
1018
    int code;
1019
    
1020
    OPEN_READER(re, s)
1021
    UPDATE_CACHE(re, s)
1022

    
1023
    GET_VLC(code, re, s, table, bits, max_depth)
1024

    
1025
    CLOSE_READER(re, s)
1026
    return code;
1027
}
1028

    
1029
//#define TRACE
1030

    
1031
#ifdef TRACE
1032

    
1033
static inline void print_bin(int bits, int n){
1034
    int i;
1035
    
1036
    for(i=n-1; i>=0; i--){
1037
        printf("%d", (bits>>i)&1);
1038
    }
1039
    for(i=n; i<24; i++)
1040
        printf(" ");
1041
}
1042

    
1043
static inline int get_bits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1044
    int r= get_bits(s, n);
1045
    
1046
    print_bin(r, n);
1047
    printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
1048
    return r;
1049
}
1050
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, char *func, int line){
1051
    int show= show_bits(s, 24);
1052
    int pos= get_bits_count(s);
1053
    int r= get_vlc2(s, table, bits, max_depth);
1054
    int len= get_bits_count(s) - pos;
1055
    int bits2= show>>(24-len);
1056
    
1057
    print_bin(bits2, len);
1058
    
1059
    printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
1060
    return r;
1061
}
1062
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, char *func, int line){
1063
    int show= show_bits(s, n);
1064
    int r= get_xbits(s, n);
1065
    
1066
    print_bin(show, n);
1067
    printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
1068
    return r;
1069
}
1070

    
1071
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1072
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1073
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1074
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1075
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1076

    
1077
#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1078

    
1079
#else //TRACE
1080
#define tprintf(...) {}
1081
#endif
1082

    
1083
/* define it to include statistics code (useful only for optimizing
1084
   codec efficiency */
1085
//#define STATS
1086

    
1087
#ifdef STATS
1088

    
1089
enum {
1090
    ST_UNKNOWN,
1091
    ST_DC,
1092
    ST_INTRA_AC,
1093
    ST_INTER_AC,
1094
    ST_INTRA_MB,
1095
    ST_INTER_MB,
1096
    ST_MV,
1097
    ST_NB,
1098
};
1099

    
1100
extern int st_current_index;
1101
extern unsigned int st_bit_counts[ST_NB];
1102
extern unsigned int st_out_bit_counts[ST_NB];
1103

    
1104
void print_stats(void);
1105
#endif
1106

    
1107
/* misc math functions */
1108
extern const uint8_t ff_log2_tab[256];
1109

    
1110
static inline int av_log2(unsigned int v)
1111
{
1112
    int n;
1113

    
1114
    n = 0;
1115
    if (v & 0xffff0000) {
1116
        v >>= 16;
1117
        n += 16;
1118
    }
1119
    if (v & 0xff00) {
1120
        v >>= 8;
1121
        n += 8;
1122
    }
1123
    n += ff_log2_tab[v];
1124

    
1125
    return n;
1126
}
1127

    
1128
static inline int av_log2_16bit(unsigned int v)
1129
{
1130
    int n;
1131

    
1132
    n = 0;
1133
    if (v & 0xff00) {
1134
        v >>= 8;
1135
        n += 8;
1136
    }
1137
    n += ff_log2_tab[v];
1138

    
1139
    return n;
1140
}
1141

    
1142
/* median of 3 */
1143
static inline int mid_pred(int a, int b, int c)
1144
{
1145
#if 0
1146
    int t= (a-b)&((a-b)>>31);
1147
    a-=t;
1148
    b+=t;
1149
    b-= (b-c)&((b-c)>>31);
1150
    b+= (a-b)&((a-b)>>31);
1151

1152
    return b;
1153
#else
1154
    if(a>b){
1155
        if(c>b){
1156
            if(c>a) b=a;
1157
            else    b=c;
1158
        }
1159
    }else{
1160
        if(b>c){
1161
            if(c>a) b=c;
1162
            else    b=a;
1163
        }
1164
    }
1165
    return b;
1166
#endif
1167
}
1168

    
1169
static inline int clip(int a, int amin, int amax)
1170
{
1171
    if (a < amin)
1172
        return amin;
1173
    else if (a > amax)
1174
        return amax;
1175
    else
1176
        return a;
1177
}
1178

    
1179
static inline int clip_uint8(int a)
1180
{
1181
    if (a&(~255)) return (-a)>>31;
1182
    else          return a;
1183
}
1184

    
1185
/* math */
1186
extern const uint8_t ff_sqrt_tab[128];
1187

    
1188
int64_t ff_gcd(int64_t a, int64_t b);
1189

    
1190
static inline int ff_sqrt(int a)
1191
{
1192
    int ret=0;
1193
    int s;
1194
    int ret_sq=0;
1195
    
1196
    if(a<128) return ff_sqrt_tab[a];
1197
    
1198
    for(s=15; s>=0; s--){
1199
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
1200
        if(b<=a){
1201
            ret_sq=b;
1202
            ret+= 1<<s;
1203
        }
1204
    }
1205
    return ret;
1206
}
1207

    
1208
/**
1209
 * converts fourcc string to int
1210
 */
1211
static inline int ff_get_fourcc(const char *s){
1212
    assert( strlen(s)==4 );
1213

    
1214
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1215
}
1216

    
1217
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1218
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1219

    
1220

    
1221
#ifdef ARCH_X86
1222
#define MASK_ABS(mask, level)\
1223
            asm volatile(\
1224
                "cdq                        \n\t"\
1225
                "xorl %1, %0                \n\t"\
1226
                "subl %1, %0                \n\t"\
1227
                : "+a" (level), "=&d" (mask)\
1228
            );
1229
#else
1230
#define MASK_ABS(mask, level)\
1231
            mask= level>>31;\
1232
            level= (level^mask)-mask;
1233
#endif
1234

    
1235

    
1236
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
1237
#define COPY3_IF_LT(x,y,a,b,c,d)\
1238
asm volatile (\
1239
    "cmpl %0, %3        \n\t"\
1240
    "cmovl %3, %0        \n\t"\
1241
    "cmovl %4, %1        \n\t"\
1242
    "cmovl %5, %2        \n\t"\
1243
    : "+r" (x), "+r" (a), "+r" (c)\
1244
    : "r" (y), "r" (b), "r" (d)\
1245
);
1246
#else
1247
#define COPY3_IF_LT(x,y,a,b,c,d)\
1248
if((y)<(x)){\
1249
     (x)=(y);\
1250
     (a)=(b);\
1251
     (c)=(d);\
1252
}
1253
#endif
1254

    
1255
#ifdef ARCH_X86
1256
static inline long long rdtsc()
1257
{
1258
        long long l;
1259
        asm volatile(        "rdtsc\n\t"
1260
                : "=A" (l)
1261
        );
1262
        return l;
1263
}
1264

    
1265
#define START_TIMER \
1266
uint64_t tend;\
1267
uint64_t tstart= rdtsc();\
1268

    
1269
#define STOP_TIMER(id) \
1270
tend= rdtsc();\
1271
{\
1272
  static uint64_t tsum=0;\
1273
  static int tcount=0;\
1274
  static int tskip_count=0;\
1275
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
1276
      tsum+= tend - tstart;\
1277
      tcount++;\
1278
  }else\
1279
      tskip_count++;\
1280
  if(256*256*256*64%(tcount+tskip_count)==0){\
1281
      av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
1282
  }\
1283
}
1284
#endif
1285

    
1286
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1287

    
1288
/* avoid usage of various functions */
1289
#define malloc please_use_av_malloc
1290
#define free please_use_av_free
1291
#define realloc please_use_av_realloc
1292
#define time time_is_forbidden_due_to_security_issues
1293
#define rand rand_is_forbidden_due_to_state_trashing
1294
#define srand srand_is_forbidden_due_to_state_trashing
1295
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
1296
#define printf please_use_av_log
1297
#define fprintf please_use_av_log
1298
#endif
1299

    
1300
#define CHECKED_ALLOCZ(p, size)\
1301
{\
1302
    p= av_mallocz(size);\
1303
    if(p==NULL && (size)!=0){\
1304
        perror("malloc");\
1305
        goto fail;\
1306
    }\
1307
}
1308

    
1309
#endif /* HAVE_AV_CONFIG_H */
1310

    
1311
#endif /* COMMON_H */