Statistics
| Branch: | Revision:

ffmpeg / libavcodec / common.h @ 8083185f

History | View | Annotate | Download (20 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
#else
61
#define INT64_C(c)     (c ## LL)
62
#define UINT64_C(c)    (c ## ULL)
63
#endif /* __MINGW32__ */
64

    
65
#define M_PI    3.14159265358979323846
66
#define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
67

    
68
#ifdef _DEBUG
69
#define DEBUG
70
#endif
71

    
72
// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
73
#define bswap_32(x) \
74
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
75
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
76
#define be2me_32(x) bswap_32(x)
77

    
78
#define snprintf _snprintf
79

    
80
#ifndef __MINGW32__
81
/* no config.h with VC */
82
#define CONFIG_ENCODERS 1
83
#define CONFIG_DECODERS 1
84
#define CONFIG_AC3      1
85
#endif
86

    
87
#else
88

    
89
/* unix */
90

    
91
#include <inttypes.h>
92

    
93
#ifndef __WINE_WINDEF16_H
94
/* workaround for typedef conflict in MPlayer (wine typedefs) */
95
typedef unsigned short UINT16;
96
typedef signed short INT16;
97
#endif
98

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

    
106
#ifdef HAVE_AV_CONFIG_H
107

    
108
#ifdef __FreeBSD__
109
#include <sys/param.h>
110
#endif
111

    
112
#ifndef INT64_C
113
#define INT64_C(c)     (c ## LL)
114
#define UINT64_C(c)    (c ## ULL)
115
#endif
116

    
117
#include "bswap.h"
118

    
119
#ifdef USE_FASTMEMCPY
120
#include "fastmemcpy.h"
121
#endif
122

    
123
#endif /* HAVE_AV_CONFIG_H */
124

    
125
#endif /* !CONFIG_WIN32 */
126

    
127

    
128
/* debug stuff */
129
#ifdef HAVE_AV_CONFIG_H
130

    
131
#ifndef DEBUG
132
#define NDEBUG
133
#endif
134
#include <assert.h>
135

    
136
/* dprintf macros */
137
#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
138

    
139
inline void dprintf(const char* fmt,...) {}
140

    
141
#else
142

    
143
#ifdef DEBUG
144
#define dprintf(fmt,args...) printf(fmt, ## args)
145
#else
146
#define dprintf(fmt,args...)
147
#endif
148

    
149
#endif /* !CONFIG_WIN32 */
150

    
151
#endif /* HAVE_AV_CONFIG_H */
152

    
153
/* assume b>0 */
154
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
155
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
156

    
157
/* bit output */
158

    
159
struct PutBitContext;
160

    
161
typedef void (*WriteDataFunc)(void *, UINT8 *, int);
162

    
163
typedef struct PutBitContext {
164
#ifdef ALT_BITSTREAM_WRITER
165
    UINT8 *buf, *buf_end;
166
    int index;
167
#else
168
    UINT32 bit_buf;
169
    int bit_left;
170
    UINT8 *buf, *buf_ptr, *buf_end;
171
#endif
172
    INT64 data_out_size; /* in bytes */
173
} PutBitContext;
174

    
175
void init_put_bits(PutBitContext *s, 
176
                   UINT8 *buffer, int buffer_size,
177
                   void *opaque,
178
                   void (*write_data)(void *, UINT8 *, int));
179

    
180
INT64 get_bit_count(PutBitContext *s); /* XXX: change function name */
181
void align_put_bits(PutBitContext *s);
182
void flush_put_bits(PutBitContext *s);
183
void put_string(PutBitContext * pbc, char *s);
184

    
185
/* jpeg specific put_bits */
186
void jflush_put_bits(PutBitContext *s);
187

    
188
/* bit input */
189

    
190
typedef struct GetBitContext {
191
#ifdef ALT_BITSTREAM_READER
192
    int index;
193
    UINT8 *buffer;
194
#else
195
    UINT32 bit_buf;
196
    int bit_cnt;
197
    UINT8 *buf, *buf_ptr, *buf_end;
198
#endif
199
    int size;
200
} GetBitContext;
201

    
202
static inline int get_bits_count(GetBitContext *s);
203

    
204
typedef struct VLC {
205
    int bits;
206
    INT16 *table_codes;
207
    INT8 *table_bits;
208
    int table_size, table_allocated;
209
} VLC;
210

    
211
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
212
#ifdef ARCH_X86
213
#define unaligned32(a) (*(UINT32*)(a))
214
#else
215
#ifdef __GNUC__
216
static inline uint32_t unaligned32(const void *v) {
217
    struct Unaligned {
218
        uint32_t i;
219
    } __attribute__((packed));
220

    
221
    return ((const struct Unaligned *) v)->i;
222
}
223
#elif defined(__DECC)
224
static inline uint32_t unaligned32(const void *v) {
225
    return *(const __unaligned uint32_t *) v;
226
}
227
#else
228
static inline uint32_t unaligned32(const void *v) {
229
    return *(const uint32_t *) v;
230
}
231
#endif
232
#endif //!ARCH_X86
233

    
234
#ifndef ALT_BITSTREAM_WRITER
235
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
236
{
237
    unsigned int bit_buf;
238
    int bit_left;
239

    
240
#ifdef STATS
241
    st_out_bit_counts[st_current_index] += n;
242
#endif
243
    //    printf("put_bits=%d %x\n", n, value);
244
    assert(n == 32 || value < (1U << n));
245
    
246
    bit_buf = s->bit_buf;
247
    bit_left = s->bit_left;
248

    
249
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
250
    /* XXX: optimize */
251
    if (n < bit_left) {
252
        bit_buf = (bit_buf<<n) | value;
253
        bit_left-=n;
254
    } else {
255
        bit_buf<<=bit_left;
256
        bit_buf |= value >> (n - bit_left);
257
        *(UINT32 *)s->buf_ptr = be2me_32(bit_buf);
258
        //printf("bitbuf = %08x\n", bit_buf);
259
        s->buf_ptr+=4;
260
        bit_left+=32 - n;
261
        bit_buf = value;
262
    }
263

    
264
    s->bit_buf = bit_buf;
265
    s->bit_left = bit_left;
266
}
267
#endif
268

    
269

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

    
336
#ifndef ALT_BITSTREAM_WRITER
337
/* for jpeg : escape 0xff with 0x00 after it */
338
static inline void jput_bits(PutBitContext *s, int n, unsigned int value)
339
{
340
    unsigned int bit_buf, b;
341
    int bit_left, i;
342
    
343
    assert(n == 32 || value < (1U << n));
344

    
345
    bit_buf = s->bit_buf;
346
    bit_left = s->bit_left;
347

    
348
    //printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
349
    /* XXX: optimize */
350
    if (n < bit_left) {
351
        bit_buf = (bit_buf<<n) | value;
352
        bit_left-=n;
353
    } else {
354
        bit_buf<<=bit_left;
355
        bit_buf |= value >> (n - bit_left);
356
        /* handle escape */
357
        for(i=0;i<4;i++) {
358
            b = (bit_buf >> 24);
359
            *(s->buf_ptr++) = b;
360
            if (b == 0xff)
361
                *(s->buf_ptr++) = 0;
362
            bit_buf <<= 8;
363
        }
364

    
365
        bit_left+= 32 - n;
366
        bit_buf = value;
367
    }
368
    
369
    s->bit_buf = bit_buf;
370
    s->bit_left = bit_left;
371
}
372
#endif
373

    
374

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

    
392
        *(bPtr++)= v>>24;
393
        if((v&0xFF000000)==0xFF000000 && numChecked>0){
394
                *(bPtr++)= 0x00;
395
                index+=8;
396
        }
397
        *(bPtr++)= (v>>16)&0xFF;
398
        if((v&0x00FF0000)==0x00FF0000 && numChecked>1){
399
                *(bPtr++)= 0x00;
400
                index+=8;
401
        }
402
        *(bPtr++)= (v>>8)&0xFF;
403
        if((v&0x0000FF00)==0x0000FF00 && numChecked>2){
404
                *(bPtr++)= 0x00;
405
                index+=8;
406
        }
407
        *(bPtr++)= v&0xFF;
408
        if((v&0x000000FF)==0x000000FF && numChecked>3){
409
                *(bPtr++)= 0x00;
410
                index+=8;
411
        }
412
        *((uint32_t*)bPtr)= 0;
413
    }
414
    else
415
    {
416
        ptr[0] = v;
417
        ptr[1] = 0;
418
    }
419

    
420
    index+= n;
421
    s->index= index;
422
 }
423
#endif
424

    
425

    
426
static inline uint8_t* pbBufPtr(PutBitContext *s)
427
{
428
#ifdef ALT_BITSTREAM_WRITER
429
        return s->buf + (s->index>>3);
430
#else
431
        return s->buf_ptr;
432
#endif
433
}
434

    
435
void init_get_bits(GetBitContext *s, 
436
                   UINT8 *buffer, int buffer_size);
437

    
438
#ifndef ALT_BITSTREAM_READER
439
unsigned int get_bits_long(GetBitContext *s, int n);
440
unsigned int show_bits_long(GetBitContext *s, int n);
441
#endif
442

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

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

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

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

    
549
    result<<= (index&0x07);
550
    result>>= 32 - n;
551
    
552
    return result;
553
#endif //!ALIGNED_BITSTREAM
554
#else //ALT_BITSTREAM_READER
555
    if(s->bit_cnt>=n) {
556
        /* most common case here */
557
        unsigned int val = s->bit_buf >> (32 - n);
558
        return val;
559
    }
560
    return show_bits_long(s,n);
561
#endif //!ALT_BITSTREAM_READER
562
}
563

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

    
587
    result>>= 32 - n;
588
    
589
    return result;
590
#endif //!ALIGNED_BITSTREAM
591
#else //ALT_BITSTREAM_READER
592
    int index= (get_bits_count(s) + offset + 7)>>3;
593
    uint32_t result= be2me_32( unaligned32( ((uint8_t *)s->buf)+index ) );
594

    
595
    result>>= 32 - n;
596
//printf(" %X %X %d \n", (int)(((uint8_t *)s->buf)+index ), (int)s->buf_ptr, s->bit_cnt);    
597
    return result;
598
#endif //!ALT_BITSTREAM_READER
599
}
600

    
601
static inline void skip_bits(GetBitContext *s, int n){
602
#ifdef ALT_BITSTREAM_READER
603
    s->index+= n;
604
#ifdef DUMP_STREAM
605
    {
606
        int result;
607
        s->index-= n;
608
        result= get_bits(s, n);
609
    }
610
#endif
611

    
612
#else
613
    if(s->bit_cnt>=n){
614
        /* most common case here */
615
        s->bit_buf <<= n;
616
        s->bit_cnt -= n;
617
#ifdef STATS
618
        st_bit_counts[st_current_index] += n;
619
#endif
620
    } else {
621
        get_bits_long(s,n);
622
    }
623
#endif
624
}
625

    
626
static inline void skip_bits1(GetBitContext *s){
627
#ifdef ALT_BITSTREAM_READER
628
    s->index++;
629
#ifdef DUMP_STREAM
630
    s->index--;
631
    printf("%d ", get_bits1(s));
632
#endif
633
#else
634
    if(s->bit_cnt>0){
635
        /* most common case here */
636
        s->bit_buf <<= 1;
637
        s->bit_cnt--;
638
#ifdef STATS
639
        st_bit_counts[st_current_index]++;
640
#endif
641
    } else {
642
        get_bits_long(s,1);
643
    }
644
#endif
645
}
646

    
647
static inline int get_bits_count(GetBitContext *s)
648
{
649
#ifdef ALT_BITSTREAM_READER
650
    return s->index;
651
#else
652
    return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
653
#endif
654
}
655

    
656
int check_marker(GetBitContext *s, char *msg);
657
void align_get_bits(GetBitContext *s);
658
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
659
             const void *bits, int bits_wrap, int bits_size,
660
             const void *codes, int codes_wrap, int codes_size);
661
void free_vlc(VLC *vlc);
662

    
663
#ifdef ALT_BITSTREAM_READER
664
#ifdef ALIGNED_BITSTREAM
665
#ifdef ARCH_X86
666
#define SHOW_BITS(s, val, n) \
667
    val= be2me_32( ((uint32_t *)(s)->buffer)[bit_cnt>>5] );\
668
    {uint32_t result2= be2me_32( ((uint32_t *)(s)->buffer)[(bit_cnt>>5) + 1] );\
669
    asm ("shldl %%cl, %2, %0\n\t"\
670
         : "=r" (val)\
671
         : "0" (val), "r" (result2), "c" (bit_cnt));\
672
    ((uint32_t)val)>>= 32 - n;}
673
#else //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
    val<<= (bit_cnt&0x1F);\
678
    result2= (result2>>1) >> (31-(bit_cnt&0x1F));\
679
    val|= result2;\
680
    ((uint32_t)val)>>= 32 - n;}
681
#endif //!ARCH_X86
682
#else //ALIGNED_BITSTREAM
683
#define SHOW_BITS(s, val, n) \
684
    val= be2me_32( unaligned32( ((uint8_t *)(s)->buffer)+(bit_cnt>>3) ) );\
685
    val<<= (bit_cnt&0x07);\
686
    ((uint32_t)val)>>= 32 - n;
687
#endif // !ALIGNED_BITSTREAM
688
#define FLUSH_BITS(n) bit_cnt+=n; 
689
#define SAVE_BITS(s) bit_cnt= (s)->index;
690
#define RESTORE_BITS(s) (s)->index= bit_cnt;
691
#else
692

    
693
/* macro to go faster */
694
/* n must be <= 24 */
695
/* XXX: optimize buffer end test */
696
#define SHOW_BITS(s, val, n)\
697
{\
698
    if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
699
        bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
700
        bit_cnt += 8;\
701
        if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
702
            bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
703
            bit_cnt += 8;\
704
            if (bit_cnt < n && buf_ptr < (s)->buf_end) {\
705
                bit_buf |= *buf_ptr++ << (24 - bit_cnt);\
706
                bit_cnt += 8;\
707
            }\
708
        }\
709
    }\
710
    val = bit_buf >> (32 - n);\
711
}
712

    
713
/* SHOW_BITS with n1 >= n must be been done before */
714
#define FLUSH_BITS(n)\
715
{\
716
    bit_buf <<= n;\
717
    bit_cnt -= n;\
718
}
719

    
720
#define SAVE_BITS(s) \
721
{\
722
    bit_cnt = (s)->bit_cnt;\
723
    bit_buf = (s)->bit_buf;\
724
    buf_ptr = (s)->buf_ptr;\
725
}
726

    
727
#define RESTORE_BITS(s) \
728
{\
729
    (s)->buf_ptr = buf_ptr;\
730
    (s)->bit_buf = bit_buf;\
731
    (s)->bit_cnt = bit_cnt;\
732
}
733
#endif // !ALT_BITSTREAM_READER
734

    
735
static inline int get_vlc(GetBitContext *s, VLC *vlc)
736
{
737
    int code, n, nb_bits, index;
738
    INT16 *table_codes;
739
    INT8 *table_bits;
740
    int bit_cnt;
741
#ifndef ALT_BITSTREAM_READER
742
    UINT32 bit_buf;
743
    UINT8 *buf_ptr;
744
#endif
745

    
746
    SAVE_BITS(s);
747
    nb_bits = vlc->bits;
748
    table_codes = vlc->table_codes;
749
    table_bits = vlc->table_bits;
750

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

    
810

    
811
/* define it to include statistics code (useful only for optimizing
812
   codec efficiency */
813
//#define STATS
814

    
815
#ifdef STATS
816

    
817
enum {
818
    ST_UNKNOWN,
819
    ST_DC,
820
    ST_INTRA_AC,
821
    ST_INTER_AC,
822
    ST_INTRA_MB,
823
    ST_INTER_MB,
824
    ST_MV,
825
    ST_NB,
826
};
827

    
828
extern int st_current_index;
829
extern unsigned int st_bit_counts[ST_NB];
830
extern unsigned int st_out_bit_counts[ST_NB];
831

    
832
void print_stats(void);
833
#endif
834

    
835
/* misc math functions */
836

    
837
static inline int av_log2(unsigned int v)
838
{
839
    int n;
840

    
841
    n = 0;
842
    if (v & 0xffff0000) {
843
        v >>= 16;
844
        n += 16;
845
    }
846
    if (v & 0xff00) {
847
        v >>= 8;
848
        n += 8;
849
    }
850
    if (v & 0xf0) {
851
        v >>= 4;
852
        n += 4;
853
    }
854
    if (v & 0xc) {
855
        v >>= 2;
856
        n += 2;
857
    }
858
    if (v & 0x2) {
859
        n++;
860
    }
861
    return n;
862
}
863

    
864
/* median of 3 */
865
static inline int mid_pred(int a, int b, int c)
866
{
867
    int vmin, vmax;
868
    vmax = vmin = a;
869
    if (b < vmin)
870
        vmin = b;
871
    else
872
        vmax = b;
873

    
874
    if (c < vmin)
875
        vmin = c;
876
    else if (c > vmax)
877
        vmax = c;
878

    
879
    return a + b + c - vmin - vmax;
880
}
881

    
882
static inline int clip(int a, int amin, int amax)
883
{
884
    if (a < amin)
885
        return amin;
886
    else if (a > amax)
887
        return amax;
888
    else
889
        return a;
890
}
891

    
892
/* memory */
893
void *av_malloc(int size);
894
void *av_mallocz(int size);
895
void av_free(void *ptr);
896
void __av_freep(void **ptr);
897
#define av_freep(p) __av_freep((void **)(p))
898

    
899
/* math */
900
int ff_gcd(int a, int b);
901

    
902
#define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
903

    
904
#endif