Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 20646267

History | View | Annotate | Download (31.3 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
#    ifndef __BEOS__
34
#        include <errno.h>
35
#    else
36
#        include "berrno.h"
37
#    endif
38
#    include <math.h>
39

    
40
#    ifndef ENODATA
41
#        define ENODATA  61
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
#ifndef always_inline
80
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
81
#    define always_inline __attribute__((always_inline)) inline
82
#else
83
#    define always_inline inline
84
#endif
85
#endif
86

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

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

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

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

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

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

    
132
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
133
static inline float floorf(float f) { 
134
    return floor(f); 
135
}
136
#endif
137

    
138
#ifdef CONFIG_WIN32
139

    
140
/* windows */
141

    
142
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
143
#        define int64_t_C(c)     (c ## i64)
144
#        define uint64_t_C(c)    (c ## i64)
145

    
146
#    ifdef HAVE_AV_CONFIG_H
147
#            define inline __inline
148
#    endif
149

    
150
#    else
151
#        define int64_t_C(c)     (c ## LL)
152
#        define uint64_t_C(c)    (c ## ULL)
153
#    endif /* __MINGW32__ */
154

    
155
#    ifdef HAVE_AV_CONFIG_H
156
#        ifdef _DEBUG
157
#            define DEBUG
158
#        endif
159

    
160
#        define snprintf _snprintf
161
#        define vsnprintf _vsnprintf
162
#    endif
163

    
164
/* CONFIG_WIN32 end */
165
#elif defined (CONFIG_OS2)
166
/* OS/2 EMX */
167

    
168
#ifndef int64_t_C
169
#define int64_t_C(c)     (c ## LL)
170
#define uint64_t_C(c)    (c ## ULL)
171
#endif
172

    
173
#ifdef HAVE_AV_CONFIG_H
174

    
175
#ifdef USE_FASTMEMCPY
176
#include "fastmemcpy.h"
177
#endif
178

    
179
#include <float.h>
180

    
181
#endif /* HAVE_AV_CONFIG_H */
182

    
183
/* CONFIG_OS2 end */
184
#else
185

    
186
/* unix */
187

    
188
#ifndef int64_t_C
189
#define int64_t_C(c)     (c ## LL)
190
#define uint64_t_C(c)    (c ## ULL)
191
#endif
192

    
193
#ifdef HAVE_AV_CONFIG_H
194

    
195
#        ifdef USE_FASTMEMCPY
196
#            include "fastmemcpy.h"
197
#        endif
198
#    endif /* HAVE_AV_CONFIG_H */
199

    
200
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
201

    
202
#ifdef HAVE_AV_CONFIG_H
203

    
204
#    include "bswap.h"
205

    
206
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
207
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
208
#        define MANGLE(a) "_" #a
209
#    else
210
#        define MANGLE(a) #a
211
#    endif
212

    
213
/* debug stuff */
214

    
215
#    ifndef DEBUG
216
#        define NDEBUG
217
#    endif
218
#    include <assert.h>
219

    
220
/* dprintf macros */
221
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
222

    
223
inline void dprintf(const char* fmt,...) {}
224

    
225
#    else
226

    
227
#        ifdef DEBUG
228
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
229
#        else
230
#            define dprintf(fmt,...)
231
#        endif
232

    
233
#    endif /* !CONFIG_WIN32 */
234

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

    
237
//rounded divison & shift
238
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
239
/* assume b>0 */
240
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
241
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
242

    
243
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
244
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
245

    
246
extern const uint32_t inverse[256];
247

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

    
286
/* bit output */
287

    
288
struct PutBitContext;
289

    
290
typedef void (*WriteDataFunc)(void *, uint8_t *, int);
291

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

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

    
319
/* return the number of bits output */
320
static inline int put_bits_count(PutBitContext *s)
321
{
322
#ifdef ALT_BITSTREAM_WRITER
323
    return s->index;
324
#else
325
    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
326
#endif
327
}
328

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

    
347
void align_put_bits(PutBitContext *s);
348
void put_string(PutBitContext * pbc, char *s, int put_zero);
349

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

    
369
#define VLC_TYPE int16_t
370

    
371
typedef struct VLC {
372
    int bits;
373
    VLC_TYPE (*table)[2]; ///< code, bits
374
    int table_size, table_allocated;
375
} VLC;
376

    
377
typedef struct RL_VLC_ELEM {
378
    int16_t level;
379
    int8_t len;
380
    uint8_t run;
381
} RL_VLC_ELEM;
382

    
383
#ifdef ARCH_SPARC
384
#define UNALIGNED_STORES_ARE_BAD
385
#endif
386

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

    
397
    return ((const struct Unaligned *) v)->i;
398
}
399
#    elif defined(__DECC)
400
static inline uint32_t unaligned32(const void *v) {
401
    return *(const __unaligned uint32_t *) v;
402
}
403
#    else
404
static inline uint32_t unaligned32(const void *v) {
405
    return *(const uint32_t *) v;
406
}
407
#    endif
408
#endif //!ARCH_X86
409

    
410
#ifndef ALT_BITSTREAM_WRITER
411
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
412
{
413
    unsigned int bit_buf;
414
    int bit_left;
415

    
416
#ifdef STATS
417
    st_out_bit_counts[st_current_index] += n;
418
#endif
419
    //    printf("put_bits=%d %x\n", n, value);
420
    assert(n == 32 || value < (1U << n));
421
    
422
    bit_buf = s->bit_buf;
423
    bit_left = s->bit_left;
424

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

    
448
    s->bit_buf = bit_buf;
449
    s->bit_left = bit_left;
450
}
451
#endif
452

    
453

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

    
520

    
521
static inline uint8_t* pbBufPtr(PutBitContext *s)
522
{
523
#ifdef ALT_BITSTREAM_WRITER
524
        return s->buf + (s->index>>3);
525
#else
526
        return s->buf_ptr;
527
#endif
528
}
529

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

    
545
/**
546
 * Changes the end of the buffer.
547
 */
548
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
549
    s->buf_end= s->buf + size;
550
}
551

    
552
/* Bitstream reader API docs:
553
name
554
    abritary name which is used as prefix for the internal variables
555

556
gb
557
    getbitcontext
558

559
OPEN_READER(name, gb)
560
    loads gb into local variables
561

562
CLOSE_READER(name, gb)
563
    stores local vars in gb
564

565
UPDATE_CACHE(name, gb)
566
    refills the internal cache from the bitstream
567
    after this call at least MIN_CACHE_BITS will be available,
568

569
GET_CACHE(name, gb)
570
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
571

572
SHOW_UBITS(name, gb, num)
573
    will return the nest num bits
574

575
SHOW_SBITS(name, gb, num)
576
    will return the nest num bits and do sign extension
577

578
SKIP_BITS(name, gb, num)
579
    will skip over the next num bits
580
    note, this is equinvalent to SKIP_CACHE; SKIP_COUNTER
581

582
SKIP_CACHE(name, gb, num)
583
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
584

585
SKIP_COUNTER(name, gb, num)
586
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
587

588
LAST_SKIP_CACHE(name, gb, num)
589
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
590

591
LAST_SKIP_BITS(name, gb, num)
592
    is equinvalent to SKIP_LAST_CACHE; SKIP_COUNTER
593

594
for examples see get_bits, show_bits, skip_bits, get_vlc
595
*/
596

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

    
607
#ifdef ALT_BITSTREAM_READER
608
#   define MIN_CACHE_BITS 25
609

    
610
#   define OPEN_READER(name, gb)\
611
        int name##_index= (gb)->index;\
612
        int name##_cache= 0;\
613

    
614
#   define CLOSE_READER(name, gb)\
615
        (gb)->index= name##_index;\
616

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

    
620
#   define SKIP_CACHE(name, gb, num)\
621
        name##_cache <<= (num);\
622

    
623
// FIXME name?
624
#   define SKIP_COUNTER(name, gb, num)\
625
        name##_index += (num);\
626

    
627
#   define SKIP_BITS(name, gb, num)\
628
        {\
629
            SKIP_CACHE(name, gb, num)\
630
            SKIP_COUNTER(name, gb, num)\
631
        }\
632

    
633
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
634
#   define LAST_SKIP_CACHE(name, gb, num) ;
635

    
636
#   define SHOW_UBITS(name, gb, num)\
637
        NEG_USR32(name##_cache, num)
638

    
639
#   define SHOW_SBITS(name, gb, num)\
640
        NEG_SSR32(name##_cache, num)
641

    
642
#   define GET_CACHE(name, gb)\
643
        ((uint32_t)name##_cache)
644

    
645
static inline int get_bits_count(GetBitContext *s){
646
    return s->index;
647
}
648
#elif defined LIBMPEG2_BITSTREAM_READER
649
//libmpeg2 like reader
650

    
651
#   define MIN_CACHE_BITS 17
652

    
653
#   define OPEN_READER(name, gb)\
654
        int name##_bit_count=(gb)->bit_count;\
655
        int name##_cache= (gb)->cache;\
656
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
657

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

    
663
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
664

    
665
#   define UPDATE_CACHE(name, gb)\
666
    if(name##_bit_count >= 0){\
667
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
668
        ((uint16_t*)name##_buffer_ptr)++;\
669
        name##_bit_count-= 16;\
670
    }\
671

    
672
#else
673

    
674
#   define UPDATE_CACHE(name, gb)\
675
    if(name##_bit_count >= 0){\
676
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
677
        name##_buffer_ptr+=2;\
678
        name##_bit_count-= 16;\
679
    }\
680

    
681
#endif
682

    
683
#   define SKIP_CACHE(name, gb, num)\
684
        name##_cache <<= (num);\
685

    
686
#   define SKIP_COUNTER(name, gb, num)\
687
        name##_bit_count += (num);\
688

    
689
#   define SKIP_BITS(name, gb, num)\
690
        {\
691
            SKIP_CACHE(name, gb, num)\
692
            SKIP_COUNTER(name, gb, num)\
693
        }\
694

    
695
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
696
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
697

    
698
#   define SHOW_UBITS(name, gb, num)\
699
        NEG_USR32(name##_cache, num)
700

    
701
#   define SHOW_SBITS(name, gb, num)\
702
        NEG_SSR32(name##_cache, num)
703

    
704
#   define GET_CACHE(name, gb)\
705
        ((uint32_t)name##_cache)
706

    
707
static inline int get_bits_count(GetBitContext *s){
708
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
709
}
710

    
711
#elif defined A32_BITSTREAM_READER
712

    
713
#   define MIN_CACHE_BITS 32
714

    
715
#   define OPEN_READER(name, gb)\
716
        int name##_bit_count=(gb)->bit_count;\
717
        uint32_t name##_cache0= (gb)->cache0;\
718
        uint32_t name##_cache1= (gb)->cache1;\
719
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
720

    
721
#   define CLOSE_READER(name, gb)\
722
        (gb)->bit_count= name##_bit_count;\
723
        (gb)->cache0= name##_cache0;\
724
        (gb)->cache1= name##_cache1;\
725
        (gb)->buffer_ptr= name##_buffer_ptr;\
726

    
727
#   define UPDATE_CACHE(name, gb)\
728
    if(name##_bit_count > 0){\
729
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
730
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
731
        name##_cache1 |= next<<name##_bit_count;\
732
        name##_buffer_ptr++;\
733
        name##_bit_count-= 32;\
734
    }\
735

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

    
751
#   define SKIP_COUNTER(name, gb, num)\
752
        name##_bit_count += (num);\
753

    
754
#   define SKIP_BITS(name, gb, num)\
755
        {\
756
            SKIP_CACHE(name, gb, num)\
757
            SKIP_COUNTER(name, gb, num)\
758
        }\
759

    
760
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
761
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
762

    
763
#   define SHOW_UBITS(name, gb, num)\
764
        NEG_USR32(name##_cache0, num)
765

    
766
#   define SHOW_SBITS(name, gb, num)\
767
        NEG_SSR32(name##_cache0, num)
768

    
769
#   define GET_CACHE(name, gb)\
770
        (name##_cache0)
771

    
772
static inline int get_bits_count(GetBitContext *s){
773
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
774
}
775

    
776
#endif
777

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

    
802
static inline int get_sbits(GetBitContext *s, int n){
803
    register int tmp;
804
    OPEN_READER(re, s)
805
    UPDATE_CACHE(re, s)
806
    tmp= SHOW_SBITS(re, s, n);
807
    LAST_SKIP_BITS(re, s, n)
808
    CLOSE_READER(re, s)
809
    return tmp;
810
}
811

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

    
826
unsigned int get_bits_long(GetBitContext *s, int n);
827

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

    
841
unsigned int show_bits_long(GetBitContext *s, int n);
842

    
843
static inline void skip_bits(GetBitContext *s, int n){
844
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
845
    OPEN_READER(re, s)
846
    UPDATE_CACHE(re, s)
847
    LAST_SKIP_BITS(re, s, n)
848
    CLOSE_READER(re, s)
849
}
850

    
851
static inline unsigned int get_bits1(GetBitContext *s){
852
#ifdef ALT_BITSTREAM_READER
853
    int index= s->index;
854
    uint8_t result= s->buffer[ index>>3 ];
855
    result<<= (index&0x07);
856
    result>>= 8 - 1;
857
    index++;
858
    s->index= index;
859

    
860
    return result;
861
#else
862
    return get_bits(s, 1);
863
#endif
864
}
865

    
866
static inline unsigned int show_bits1(GetBitContext *s){
867
    return show_bits(s, 1);
868
}
869

    
870
static inline void skip_bits1(GetBitContext *s){
871
    skip_bits(s, 1);
872
}
873

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

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

    
921
int check_marker(GetBitContext *s, const char *msg);
922
void align_get_bits(GetBitContext *s);
923
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
924
             const void *bits, int bits_wrap, int bits_size,
925
             const void *codes, int codes_wrap, int codes_size);
926
void free_vlc(VLC *vlc);
927

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

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

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

    
996
    GET_VLC(code, re, s, table, vlc->bits, 3)    
997

    
998
    CLOSE_READER(re, s)
999
    return code;
1000
}
1001

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

    
1018
    GET_VLC(code, re, s, table, bits, max_depth)
1019

    
1020
    CLOSE_READER(re, s)
1021
    return code;
1022
}
1023

    
1024
//#define TRACE
1025

    
1026
#ifdef TRACE
1027

    
1028
static inline void print_bin(int bits, int n){
1029
    int i;
1030
    
1031
    for(i=n-1; i>=0; i--){
1032
        printf("%d", (bits>>i)&1);
1033
    }
1034
    for(i=n; i<24; i++)
1035
        printf(" ");
1036
}
1037

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

    
1066
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1067
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1068
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1069
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1070
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
1071

    
1072
#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
1073

    
1074
#else //TRACE
1075
#define tprintf(...) {}
1076
#endif
1077

    
1078
/* define it to include statistics code (useful only for optimizing
1079
   codec efficiency */
1080
//#define STATS
1081

    
1082
#ifdef STATS
1083

    
1084
enum {
1085
    ST_UNKNOWN,
1086
    ST_DC,
1087
    ST_INTRA_AC,
1088
    ST_INTER_AC,
1089
    ST_INTRA_MB,
1090
    ST_INTER_MB,
1091
    ST_MV,
1092
    ST_NB,
1093
};
1094

    
1095
extern int st_current_index;
1096
extern unsigned int st_bit_counts[ST_NB];
1097
extern unsigned int st_out_bit_counts[ST_NB];
1098

    
1099
void print_stats(void);
1100
#endif
1101

    
1102
/* misc math functions */
1103
extern const uint8_t ff_log2_tab[256];
1104

    
1105
static inline int av_log2(unsigned int v)
1106
{
1107
    int n;
1108

    
1109
    n = 0;
1110
    if (v & 0xffff0000) {
1111
        v >>= 16;
1112
        n += 16;
1113
    }
1114
    if (v & 0xff00) {
1115
        v >>= 8;
1116
        n += 8;
1117
    }
1118
    n += ff_log2_tab[v];
1119

    
1120
    return n;
1121
}
1122

    
1123
static inline int av_log2_16bit(unsigned int v)
1124
{
1125
    int n;
1126

    
1127
    n = 0;
1128
    if (v & 0xff00) {
1129
        v >>= 8;
1130
        n += 8;
1131
    }
1132
    n += ff_log2_tab[v];
1133

    
1134
    return n;
1135
}
1136

    
1137
/* median of 3 */
1138
static inline int mid_pred(int a, int b, int c)
1139
{
1140
#if 0
1141
    int t= (a-b)&((a-b)>>31);
1142
    a-=t;
1143
    b+=t;
1144
    b-= (b-c)&((b-c)>>31);
1145
    b+= (a-b)&((a-b)>>31);
1146

1147
    return b;
1148
#else
1149
    if(a>b){
1150
        if(c>b){
1151
            if(c>a) b=a;
1152
            else    b=c;
1153
        }
1154
    }else{
1155
        if(b>c){
1156
            if(c>a) b=c;
1157
            else    b=a;
1158
        }
1159
    }
1160
    return b;
1161
#endif
1162
}
1163

    
1164
static inline int clip(int a, int amin, int amax)
1165
{
1166
    if (a < amin)
1167
        return amin;
1168
    else if (a > amax)
1169
        return amax;
1170
    else
1171
        return a;
1172
}
1173

    
1174
static inline int clip_uint8(int a)
1175
{
1176
    if (a&(~255)) return (-a)>>31;
1177
    else          return a;
1178
}
1179

    
1180
/* math */
1181
extern const uint8_t ff_sqrt_tab[128];
1182

    
1183
int64_t ff_gcd(int64_t a, int64_t b);
1184

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

    
1203
/**
1204
 * converts fourcc string to int
1205
 */
1206
static inline int ff_get_fourcc(const char *s){
1207
    assert( strlen(s)==4 );
1208

    
1209
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
1210
}
1211

    
1212
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
1213
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
1214

    
1215

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

    
1230

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

    
1250
#ifdef ARCH_X86
1251
static inline long long rdtsc()
1252
{
1253
        long long l;
1254
        asm volatile(        "rdtsc\n\t"
1255
                : "=A" (l)
1256
        );
1257
        return l;
1258
}
1259

    
1260
#define START_TIMER \
1261
uint64_t tend;\
1262
uint64_t tstart= rdtsc();\
1263

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

    
1281
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
1282

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

    
1295
#define CHECKED_ALLOCZ(p, size)\
1296
{\
1297
    p= av_mallocz(size);\
1298
    if(p==NULL && (size)!=0){\
1299
        perror("malloc");\
1300
        goto fail;\
1301
    }\
1302
}
1303

    
1304
#endif /* HAVE_AV_CONFIG_H */
1305

    
1306
#endif /* COMMON_H */