Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 4cc281d9

History | View | Annotate | Download (20.2 KB)

1
#ifndef COMMON_H
2
#define COMMON_H
3

    
4
#define FFMPEG_VERSION_INT 0x000406
5
#define FFMPEG_VERSION     "0.4.6"
6

    
7
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
8
#define CONFIG_WIN32
9
#endif
10

    
11
//#define ALT_BITSTREAM_WRITER
12
//#define ALIGNED_BITSTREAM_WRITER
13
//#define ALT_BITSTREAM_READER
14
//#define ALIGNED_BITSTREAM
15
#define FAST_GET_FIRST_VLC
16
//#define DUMP_STREAM // only works with the ALT_BITSTREAM_READER
17

    
18
#ifdef HAVE_AV_CONFIG_H
19
/* only include the following when compiling package */
20
#include "config.h"
21

    
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <errno.h>
26
#include <math.h>
27

    
28
#ifndef ENODATA
29
#define ENODATA  61
30
#endif
31

    
32
#endif /* HAVE_AV_CONFIG_H */
33

    
34
#ifdef CONFIG_WIN32
35

    
36
/* windows */
37

    
38
typedef unsigned short UINT16;
39
typedef signed short INT16;
40
typedef unsigned char UINT8;
41
typedef unsigned int UINT32;
42
typedef unsigned __int64 UINT64;
43
typedef signed char INT8;
44
typedef signed int INT32;
45
typedef signed __int64 INT64;
46

    
47
typedef UINT8 uint8_t;
48
typedef INT8 int8_t;
49
typedef UINT16 uint16_t;
50
typedef INT16 int16_t;
51
typedef UINT32 uint32_t;
52
typedef INT32 int32_t;
53

    
54
#ifndef __MINGW32__
55
#define INT64_C(c)     (c ## i64)
56
#define UINT64_C(c)    (c ## i64)
57

    
58
#define inline __inline
59

    
60
/*
61
  Disable warning messages:
62
    warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
63
    warning C4305: 'argument' : truncation from 'const double' to 'float'
64
*/
65
#pragma warning( disable : 4244 )
66
#pragma warning( disable : 4305 )
67

    
68
#else
69
#define INT64_C(c)     (c ## LL)
70
#define UINT64_C(c)    (c ## ULL)
71
#endif /* __MINGW32__ */
72

    
73
#define M_PI    3.14159265358979323846
74
#define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
75

    
76
#ifdef _DEBUG
77
#define DEBUG
78
#endif
79

    
80
// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
81
#define bswap_32(x) \
82
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
83
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
84
#define be2me_32(x) bswap_32(x)
85

    
86
#define snprintf _snprintf
87

    
88
#ifndef __MINGW32__
89
/* no config.h with VC */
90
#define CONFIG_ENCODERS 1
91
#define CONFIG_DECODERS 1
92
#define CONFIG_AC3      1
93
#endif
94

    
95
#else
96

    
97
/* unix */
98

    
99
#include <inttypes.h>
100

    
101
#ifndef __WINE_WINDEF16_H
102
/* workaround for typedef conflict in MPlayer (wine typedefs) */
103
typedef unsigned short UINT16;
104
typedef signed short INT16;
105
#endif
106

    
107
typedef unsigned char UINT8;
108
typedef unsigned int UINT32;
109
typedef unsigned long long UINT64;
110
typedef signed char INT8;
111
typedef signed int INT32;
112
typedef signed long long INT64;
113

    
114
#ifdef HAVE_AV_CONFIG_H
115

    
116
#ifdef __FreeBSD__
117
#include <sys/param.h>
118
#endif
119

    
120
#ifndef INT64_C
121
#define INT64_C(c)     (c ## LL)
122
#define UINT64_C(c)    (c ## ULL)
123
#endif
124

    
125
#include "bswap.h"
126

    
127
#ifdef USE_FASTMEMCPY
128
#include "fastmemcpy.h"
129
#endif
130

    
131
#endif /* HAVE_AV_CONFIG_H */
132

    
133
#endif /* !CONFIG_WIN32 */
134

    
135

    
136
/* debug stuff */
137
#ifdef HAVE_AV_CONFIG_H
138

    
139
#ifndef DEBUG
140
#define NDEBUG
141
#endif
142
#include <assert.h>
143

    
144
/* dprintf macros */
145
#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
146

    
147
inline void dprintf(const char* fmt,...) {}
148

    
149
#else
150

    
151
#ifdef DEBUG
152
#define dprintf(fmt,args...) printf(fmt, ## args)
153
#else
154
#define dprintf(fmt,args...)
155
#endif
156

    
157
#endif /* !CONFIG_WIN32 */
158

    
159
#endif /* HAVE_AV_CONFIG_H */
160

    
161
/* assume b>0 */
162
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
163
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
164

    
165
/* bit output */
166

    
167
struct PutBitContext;
168

    
169
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
170

    
171
typedef struct PutBitContext {
172
#ifdef ALT_BITSTREAM_WRITER
173
    UINT8 *buf, *buf_end;
174
    int index;
175
#else
176
    UINT32 bit_buf;
177
    int bit_left;
178
    UINT8 *buf, *buf_ptr, *buf_end;
179
#endif
180
    INT64 data_out_size; /* in bytes */
181
} PutBitContext;
182

    
183
void init_put_bits(PutBitContext *s, 
184
                   UINT8 *buffer, int buffer_size,
185
                   void *opaque,
186
                   void (*write_data)(void *, UINT8 *, int));
187

    
188
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
189
void align_put_bits(PutBitContext *s);
190
void flush_put_bits(PutBitContext *s);
191
void put_string(PutBitContext * pbc, char *s);
192

    
193
/* jpeg specific put_bits */
194
void jflush_put_bits(PutBitContext *s);
195

    
196
/* bit input */
197

    
198
typedef struct GetBitContext {
199
#ifdef ALT_BITSTREAM_READER
200
    int index;
201
    UINT8 *buffer;
202
#else
203
    UINT32 bit_buf;
204
    int bit_cnt;
205
    UINT8 *buf, *buf_ptr, *buf_end;
206
#endif
207
    int size;
208
} GetBitContext;
209

    
210
static inline int get_bits_count(GetBitContext *s);
211

    
212
typedef struct VLC {
213
    int bits;
214
    INT16 *table_codes;
215
    INT8 *table_bits;
216
    int table_size, table_allocated;
217
} VLC;
218

    
219
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
220
#ifdef ARCH_X86
221
#define unaligned32(a) (*(UINT32*)(a))
222
#else
223
#ifdef __GNUC__
224
static inline uint32_t unaligned32(const void *v) {
225
    struct Unaligned {
226
        uint32_t i;
227
    } __attribute__((packed));
228

    
229
    return ((const struct Unaligned *) v)->i;
230
}
231
#elif defined(__DECC)
232
static inline uint32_t unaligned32(const void *v) {
233
    return *(const __unaligned uint32_t *) v;
234
}
235
#else
236
static inline uint32_t unaligned32(const void *v) {
237
    return *(const uint32_t *) v;
238
}
239
#endif
240
#endif //!ARCH_X86
241

    
242
#ifndef ALT_BITSTREAM_WRITER
243
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
244
{
245
    unsigned int bit_buf;
246
    int bit_left;
247

    
248
#ifdef STATS
249
    st_out_bit_counts[st_current_index] += n;
250
#endif
251
    //    printf("put_bits=%d %x\n", n, value);
252
    assert(n == 32 || value < (1U << n));
253
    
254
    bit_buf = s->bit_buf;
255
    bit_left = s->bit_left;
256

    
257
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
258
    /* XXX: optimize */
259
    if (n < bit_left) {
260
        bit_buf = (bit_buf<<n) | value;
261
        bit_left-=n;
262
    } else {
263
        bit_buf<<=bit_left;
264
        bit_buf |= value >> (n - bit_left);
265
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
266
        //printf("bitbuf = %08x\n", bit_buf);
267
        s->buf_ptr+=4;
268
        bit_left+=32 - n;
269
        bit_buf = value;
270
    }
271

    
272
    s->bit_buf = bit_buf;
273
    s->bit_left = bit_left;
274
}
275
#endif
276

    
277

    
278
#ifdef ALT_BITSTREAM_WRITER
279
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
280
{
281
#ifdef ALIGNED_BITSTREAM_WRITER
282
#ifdef ARCH_X86
283
    asm volatile(
284
        "movl %0, %%ecx                        \n\t"
285
        "xorl %%eax, %%eax                \n\t"
286
        "shrdl %%cl, %1, %%eax                \n\t"
287
        "shrl %%cl, %1                        \n\t"
288
        "movl %0, %%ecx                        \n\t"
289
        "shrl $3, %%ecx                        \n\t"
290
        "andl $0xFFFFFFFC, %%ecx        \n\t"
291
        "bswapl %1                        \n\t"
292
        "orl %1, (%2, %%ecx)                \n\t"
293
        "bswapl %%eax                        \n\t"
294
        "addl %3, %0                        \n\t"
295
        "movl %%eax, 4(%2, %%ecx)        \n\t"
296
        : "=&r" (s->index), "=&r" (value)
297
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
298
        : "%eax", "%ecx"
299
    );
300
#else
301
    int index= s->index;
302
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
303
    
304
    value<<= 32-n; 
305
    
306
    ptr[0] |= be2me_32(value>>(index&31));
307
    ptr[1]  = be2me_32(value<<(32-(index&31)));
308
//if(n>24) printf("%d %d\n", n, value);
309
    index+= n;
310
    s->index= index;
311
#endif
312
#else //ALIGNED_BITSTREAM_WRITER
313
#ifdef ARCH_X86
314
    asm volatile(
315
        "movl $7, %%ecx                        \n\t"
316
        "andl %0, %%ecx                        \n\t"
317
        "addl %3, %%ecx                        \n\t"
318
        "negl %%ecx                        \n\t"
319
        "shll %%cl, %1                        \n\t"
320
        "bswapl %1                        \n\t"
321
        "movl %0, %%ecx                        \n\t"
322
        "shrl $3, %%ecx                        \n\t"
323
        "orl %1, (%%ecx, %2)                \n\t"
324
        "addl %3, %0                        \n\t"
325
        "movl $0, 4(%%ecx, %2)                \n\t"
326
        : "=&r" (s->index), "=&r" (value)
327
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
328
        : "%ecx"
329
    );
330
#else
331
    int index= s->index;
332
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
333
    
334
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
335
    ptr[1] = 0;
336
//if(n>24) printf("%d %d\n", n, value);
337
    index+= n;
338
    s->index= index;
339
#endif
340
#endif //!ALIGNED_BITSTREAM_WRITER
341
}
342
#endif
343

    
344
#ifndef ALT_BITSTREAM_WRITER
345
/* for jpeg : escape 0xff with 0x00 after it */
346
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
347
{
348
    unsigned int bit_buf, b;
349
    int bit_left, i;
350
    
351
    assert(n == 32 || value < (1U << n));
352

    
353
    bit_buf = s->bit_buf;
354
    bit_left = s->bit_left;
355

    
356
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
357
    /* XXX: optimize */
358
    if (n < bit_left) {
359
        bit_buf = (bit_buf<<n) | value;
360
        bit_left-=n;
361
    } else {
362
        bit_buf<<=bit_left;
363
        bit_buf |= value >> (n - bit_left);
364
        /* handle escape */
365
        for(i=0;i<4;i++) {
366
            b = (bit_buf >> 24);
367
            *(s->buf_ptr++) = b;
368
            if (b == 0xff)
369
                *(s->buf_ptr++) = 0;
370
            bit_buf <<= 8;
371
        }
372

    
373
        bit_left+= 32 - n;
374
        bit_buf = value;
375
    }
376
    
377
    s->bit_buf = bit_buf;
378
    s->bit_left = bit_left;
379
}
380
#endif
381

    
382

    
383
#ifdef ALT_BITSTREAM_WRITER
384
static inline void jput_bits(PutBitContext *s, int n, int value)
385
{
386
    int index= s->index;
387
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
388
    int v= ptr[0];
389
//if(n>24) printf("%d %d\n", n, value);
390
    
391
    v |= be2me_32(value<<(32-n-(index&7) ));
392
    if(((v+0x01010101)^0xFFFFFFFF)&v&0x80808080)
393
    {
394
        /* handle idiotic (m)jpeg escapes */
395
        uint8_t *bPtr= (uint8_t*)ptr;
396
        int numChecked= ((index+n)>>3) - (index>>3);
397
        
398
        v= be2me_32(v);
399

    
400
        *(bPtr++)= v>>24;
401
        if((v&0xFF000000)==0xFF000000 && numChecked>0){
402
                *(bPtr++)= 0x00;
403
                index+=8;
404
        }
405
        *(bPtr++)= (v>>16)&0xFF;
406
        if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
407
                *(bPtr++)= 0x00;
408
                index+=8;
409
        }
410
        *(bPtr++)= (v>>8)&0xFF;
411
        if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
412
                *(bPtr++)= 0x00;
413
                index+=8;
414
        }
415
        *(bPtr++)= v&0xFF;
416
        if((v&0x000000FF)==0x000000FF && numChecked>3){
417
                *(bPtr++)= 0x00;
418
                index+=8;
419
        }
420
        *((uint32_t*)bPtr)= 0;
421
    }
422
    else
423
    {
424
        ptr[0] = v;
425
        ptr[1] = 0;
426
    }
427

    
428
    index+= n;
429
    s->index= index;
430
 }
431
#endif
432

    
433

    
434
static inline uint8_t* pbBufPtr(PutBitContext *s)
435
{
436
#ifdef ALT_BITSTREAM_WRITER
437
        return s->buf + (s->index>>3);
438
#else
439
        return s->buf_ptr;
440
#endif
441
}
442

    
443
void init_get_bits(GetBitContext *s, 
444
                   UINT8 *buffer, int buffer_size);
445

    
446
#ifndef ALT_BITSTREAM_READER
447
unsigned int get_bits_long(GetBitContext *s, int n);
448
unsigned int show_bits_long(GetBitContext *s, int n);
449
#endif
450

    
451
static inline unsigned int get_bits(GetBitContext *s, int n){
452
#ifdef ALT_BITSTREAM_READER
453
#ifdef ALIGNED_BITSTREAM
454
    int index= s->index;
455
    uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
456
    uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
457
#ifdef ARCH_X86
458
    asm ("shldl %%cl, %2, %0\n\t"
459
         : "=r" (result1)
460
         : "0" (result1), "r" (result2), "c" (index));
461
#else
462
    result1<<= (index&0x1F);
463
    result2= (result2>>1) >> (31-(index&0x1F));
464
    result1|= result2;
465
#endif
466
    result1>>= 32 - n;
467
    index+= n;
468
    s->index= index;
469
    
470
    return result1;
471
#else //ALIGNED_BITSTREAM
472
    int index= s->index;
473
    uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
474

    
475
    result<<= (index&0x07);
476
    result>>= 32 - n;
477
    index+= n;
478
    s->index= index;
479
#ifdef DUMP_STREAM
480
    while(n){
481
        printf("%d", (result>>(n-1))&1);
482
        n--;
483
    }
484
    printf(" ");
485
#endif
486
    return result;
487
#endif //!ALIGNED_BITSTREAM
488
#else //ALT_BITSTREAM_READER
489
    if(s->bit_cnt>=n){
490
        /* most common case here */
491
        unsigned int val = s->bit_buf >> (32 - n);
492
        s->bit_buf <<= n;
493
        s->bit_cnt -= n;
494
#ifdef STATS
495
        st_bit_counts[st_current_index] += n;
496
#endif
497
        return val;
498
    }
499
    return get_bits_long(s,n);
500
#endif //!ALT_BITSTREAM_READER
501
}
502

    
503
static inline unsigned int get_bits1(GetBitContext *s){
504
#ifdef ALT_BITSTREAM_READER
505
    int index= s->index;
506
    uint8_t result= s->buffer[ index>>3 ];
507
    result<<= (index&0x07);
508
    result>>= 8 - 1;
509
    index++;
510
    s->index= index;
511
    
512
#ifdef DUMP_STREAM
513
    printf("%d ", result);
514
#endif
515
    return result;
516
#else
517
    if(s->bit_cnt>0){
518
        /* most common case here */
519
        unsigned int val = s->bit_buf >> 31;
520
        s->bit_buf <<= 1;
521
        s->bit_cnt--;
522
#ifdef STATS
523
        st_bit_counts[st_current_index]++;
524
#endif
525
        return val;
526
    }
527
    return get_bits_long(s,1);
528
#endif
529
}
530

    
531
/* This function is identical to get_bits(), the only */
532
/* diference is that it doesn't touch the buffer      */
533
/* it is usefull to see the buffer.                   */
534
static inline unsigned int show_bits(GetBitContext *s, int n)
535
{
536
#ifdef ALT_BITSTREAM_READER
537
#ifdef ALIGNED_BITSTREAM
538
    int index= s->index;
539
    uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
540
    uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
541
#ifdef ARCH_X86
542
    asm ("shldl %%cl, %2, %0\n\t"
543
         : "=r" (result1)
544
         : "0" (result1), "r" (result2), "c" (index));
545
#else
546
    result1<<= (index&0x1F);
547
    result2= (result2>>1) >> (31-(index&0x1F));
548
    result1|= result2;
549
#endif
550
    result1>>= 32 - n;
551
    
552
    return result1;
553
#else //ALIGNED_BITSTREAM
554
    int index= s->index;
555
    uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+(index>>3) ) );
556

    
557
    result<<= (index&0x07);
558
    result>>= 32 - n;
559
    
560
    return result;
561
#endif //!ALIGNED_BITSTREAM
562
#else //ALT_BITSTREAM_READER
563
    if(s->bit_cnt>=n) {
564
        /* most common case here */
565
        unsigned int val = s->bit_buf >> (32 - n);
566
        return val;
567
    }
568
    return show_bits_long(s,n);
569
#endif //!ALT_BITSTREAM_READER
570
}
571

    
572
static inline int show_aligned_bits(GetBitContext *s, int offset, int n)
573
{
574
#ifdef ALT_BITSTREAM_READER
575
#ifdef ALIGNED_BITSTREAM
576
    int index= (s->index + offset + 7)&(~7);
577
    uint32_t result1= be2me_32( ((uint32_t *)s->buffer)[index>>5] );
578
    uint32_t result2= be2me_32( ((uint32_t *)s->buffer)[(index>>5) + 1] );
579
#ifdef ARCH_X86
580
    asm ("shldl %%cl, %2, %0\n\t"
581
         : "=r" (result1)
582
         : "0" (result1), "r" (result2), "c" (index));
583
#else
584
    result1<<= (index&0x1F);
585
    result2= (result2>>1) >> (31-(index&0x1F));
586
    result1|= result2;
587
#endif
588
    result1>>= 32 - n;
589
    
590
    return result1;
591
#else //ALIGNED_BITSTREAM
592
    int index= (s->index + offset + 7)>>3;
593
    uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buffer)+index ) );
594

    
595
    result>>= 32 - n;
596
    
597
    return result;
598
#endif //!ALIGNED_BITSTREAM
599
#else //ALT_BITSTREAM_READER
600
    int index= (get_bits_count(s) + offset + 7)>>3;
601
    uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
602

    
603
    result>>= 32 - n;
604
//printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);    
605
    return result;
606
#endif //!ALT_BITSTREAM_READER
607
}
608

    
609
static inline void skip_bits(GetBitContext *s, int n){
610
#ifdef ALT_BITSTREAM_READER
611
    s->index+= n;
612
#ifdef DUMP_STREAM
613
    {
614
        int result;
615
        s->index-= n;
616
        result= get_bits(s, n);
617
    }
618
#endif
619

    
620
#else
621
    if(s->bit_cnt>=n){
622
        /* most common case here */
623
        s->bit_buf <<= n;
624
        s->bit_cnt -= n;
625
#ifdef STATS
626
        st_bit_counts[st_current_index] += n;
627
#endif
628
    } else {
629
        get_bits_long(s,n);
630
    }
631
#endif
632
}
633

    
634
static inline void skip_bits1(GetBitContext *s){
635
#ifdef ALT_BITSTREAM_READER
636
    s->index++;
637
#ifdef DUMP_STREAM
638
    s->index--;
639
    printf("%d ", get_bits1(s));
640
#endif
641
#else
642
    if(s->bit_cnt>0){
643
        /* most common case here */
644
        s->bit_buf <<= 1;
645
        s->bit_cnt--;
646
#ifdef STATS
647
        st_bit_counts[st_current_index]++;
648
#endif
649
    } else {
650
        get_bits_long(s,1);
651
    }
652
#endif
653
}
654

    
655
static inline int get_bits_count(GetBitContext *s)
656
{
657
#ifdef ALT_BITSTREAM_READER
658
    return s->index;
659
#else
660
    return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
661
#endif
662
}
663

    
664
int check_marker(GetBitContext *s, char *msg);
665
void align_get_bits(GetBitContext *s);
666
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
667
             const void *bits, int bits_wrap, int bits_size,
668
             const void *codes, int codes_wrap, int codes_size);
669
void free_vlc(VLC *vlc);
670

    
671
#ifdef ALT_BITSTREAM_READER
672
#ifdef ALIGNED_BITSTREAM
673
#ifdef ARCH_X86
674
#define SHOW_BITS(s, val, n) \
675
    val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
676
    {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
677
    asm ("shldl %%cl, %2, %0\n\t"\
678
         : "=r" (val)\
679
         : "0" (val), "r" (result2), "c" (bit_cnt));\
680
    ((uint32_t)val)>>= 32 - n;}
681
#else //ARCH_X86
682
#define SHOW_BITS(s, val, n) \
683
    val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
684
    {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
685
    val<<= (bit_cnt&0x1F);\
686
    result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
687
    val|= result2;\
688
    ((uint32_t)val)>>= 32 - n;}
689
#endif //!ARCH_X86
690
#else //ALIGNED_BITSTREAM
691
#define SHOW_BITS(s, val, n) \
692
    val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
693
    val<<= (bit_cnt&0x07);\
694
    ((uint32_t)val)>>= 32 - n;
695
#endif // !ALIGNED_BITSTREAM
696
#define FLUSH_BITS(n) bit_cnt+=n; 
697
#define SAVE_BITS(s) bit_cnt= (s)->index;
698
#define RESTORE_BITS(s) (s)->index= bit_cnt;
699
#else
700

    
701
/* macro to go faster */
702
/* n must be <= 24 */
703
/* XXX: optimize buffer end test */
704
#define SHOW_BITS(s, val, n)\
705
{\
706
    if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
707
        bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
708
        bit_cnt += 8;\
709
        if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
710
            bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
711
            bit_cnt += 8;\
712
            if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
713
                bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
714
                bit_cnt += 8;\
715
            }\
716
        }\
717
    }\
718
    val = bit_buf >> (32 - n);\
719
}
720

    
721
/* SHOW_BITS with n1 >= n must be been done before */
722
#define FLUSH_BITS(n)\
723
{\
724
    bit_buf <<= n;\
725
    bit_cnt -= n;\
726
}
727

    
728
#define SAVE_BITS(s) \
729
{\
730
    bit_cnt = (s)->bit_cnt;\
731
    bit_buf = (s)->bit_buf;\
732
    buf_ptr = (s)->buf_ptr;\
733
}
734

    
735
#define RESTORE_BITS(s) \
736
{\
737
    (s)->buf_ptr = buf_ptr;\
738
    (s)->bit_buf = bit_buf;\
739
    (s)->bit_cnt = bit_cnt;\
740
}
741
#endif // !ALT_BITSTREAM_READER
742

    
743
static inline int get_vlc(GetBitContext *s, VLC *vlc)
744
{
745
    int code, n, nb_bits, index;
746
    INT16 *table_codes;
747
    INT8 *table_bits;
748
    int bit_cnt;
749
#ifndef ALT_BITSTREAM_READER
750
    UINT32 bit_buf;
751
    UINT8 *buf_ptr;
752
#endif
753

    
754
    SAVE_BITS(s);
755
    nb_bits = vlc->bits;
756
    table_codes = vlc->table_codes;
757
    table_bits = vlc->table_bits;
758

    
759
#ifdef FAST_GET_FIRST_VLC
760
    SHOW_BITS(s, index, nb_bits);
761
    code = table_codes[index];
762
    n = table_bits[index];
763
    if (n > 0) {
764
        /* most common case (90%)*/
765
        FLUSH_BITS(n);
766
#ifdef DUMP_STREAM
767
        {
768
            int n= bit_cnt - s->index;
769
            skip_bits(s, n);
770
            RESTORE_BITS(s);
771
        }
772
#endif
773
        RESTORE_BITS(s);
774
        return code;
775
    } else if (n == 0) {
776
        return -1;
777
    } else {
778
        FLUSH_BITS(nb_bits);
779
        nb_bits = -n;
780
        table_codes = vlc->table_codes + code;
781
        table_bits = vlc->table_bits + code;
782
    }
783
#endif
784
    for(;;) {
785
        SHOW_BITS(s, index, nb_bits);
786
        code = table_codes[index];
787
        n = table_bits[index];
788
        if (n > 0) {
789
            /* most common case */
790
            FLUSH_BITS(n);
791
#ifdef STATS
792
            st_bit_counts[st_current_index] += n;
793
#endif
794
            break;
795
        } else if (n == 0) {
796
            return -1;
797
        } else {
798
            FLUSH_BITS(nb_bits);
799
#ifdef STATS
800
            st_bit_counts[st_current_index] += nb_bits;
801
#endif
802
            nb_bits = -n;
803
            table_codes = vlc->table_codes + code;
804
            table_bits = vlc->table_bits + code;
805
        }
806
    }
807
#ifdef DUMP_STREAM
808
    {
809
        int n= bit_cnt - s->index;
810
        skip_bits(s, n);
811
        RESTORE_BITS(s);
812
    }
813
#endif
814
    RESTORE_BITS(s);
815
    return code;
816
}
817

    
818

    
819
/* define it to include statistics code (useful only for optimizing
820
   codec efficiency */
821
//#define STATS
822

    
823
#ifdef STATS
824

    
825
enum {
826
    ST_UNKNOWN,
827
    ST_DC,
828
    ST_INTRA_AC,
829
    ST_INTER_AC,
830
    ST_INTRA_MB,
831
    ST_INTER_MB,
832
    ST_MV,
833
    ST_NB,
834
};
835

    
836
extern int st_current_index;
837
extern unsigned int st_bit_counts[ST_NB];
838
extern unsigned int st_out_bit_counts[ST_NB];
839

    
840
void print_stats(void);
841
#endif
842

    
843
/* misc math functions */
844

    
845
static inline int av_log2(unsigned int v)
846
{
847
    int n;
848

    
849
    n = 0;
850
    if (v & 0xffff0000) {
851
        v >>= 16;
852
        n += 16;
853
    }
854
    if (v & 0xff00) {
855
        v >>= 8;
856
        n += 8;
857
    }
858
    if (v & 0xf0) {
859
        v >>= 4;
860
        n += 4;
861
    }
862
    if (v & 0xc) {
863
        v >>= 2;
864
        n += 2;
865
    }
866
    if (v & 0x2) {
867
        n++;
868
    }
869
    return n;
870
}
871

    
872
/* median of 3 */
873
static inline int mid_pred(int a, int b, int c)
874
{
875
    int vmin, vmax;
876
    vmax = vmin = a;
877
    if (b < vmin)
878
        vmin = b;
879
    else
880
        vmax = b;
881

    
882
    if (c < vmin)
883
        vmin = c;
884
    else if (c > vmax)
885
        vmax = c;
886

    
887
    return a + b + c - vmin - vmax;
888
}
889

    
890
static inline int clip(int a, int amin, int amax)
891
{
892
    if (a < amin)
893
        return amin;
894
    else if (a > amax)
895
        return amax;
896
    else
897
        return a;
898
}
899

    
900
/* memory */
901
void *av_malloc(int size);
902
void *av_mallocz(int size);
903
void av_free(void *ptr);
904
#define av_freep(p) do { av_free(*p); *p = NULL; } while (0)
905

    
906
/* math */
907
int ff_gcd(int a, int b);
908

    
909
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
910

    
911
#endif