Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bitstream.h @ 6e4703ca

History | View | Annotate | Download (24 KB)

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

    
6
#ifndef BITSTREAM_H
7
#define BITSTREAM_H
8

    
9
//#define ALT_BITSTREAM_WRITER
10
//#define ALIGNED_BITSTREAM_WRITER
11
#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
12
#define ALT_BITSTREAM_READER
13
//#define LIBMPEG2_BITSTREAM_READER
14
//#define A32_BITSTREAM_READER
15
#endif
16
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
17

    
18
extern const uint8_t ff_reverse[256];
19

    
20
#if defined(ARCH_X86) || defined(ARCH_X86_64)
21
// avoid +32 for shift optimization (gcc should do that ...)
22
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
23
    asm ("sarl %1, %0\n\t"
24
         : "+r" (a)
25
         : "ic" ((uint8_t)(-s))
26
    );
27
    return a;
28
}
29
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
30
    asm ("shrl %1, %0\n\t"
31
         : "+r" (a)
32
         : "ic" ((uint8_t)(-s))
33
    );
34
    return a;
35
}
36
#else
37
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
38
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
39
#endif
40

    
41
/* bit output */
42

    
43
/* buf and buf_end must be present and used by every alternative writer. */
44
typedef struct PutBitContext {
45
#ifdef ALT_BITSTREAM_WRITER
46
    uint8_t *buf, *buf_end;
47
    int index;
48
#else
49
    uint32_t bit_buf;
50
    int bit_left;
51
    uint8_t *buf, *buf_ptr, *buf_end;
52
#endif
53
} PutBitContext;
54

    
55
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
56
{
57
    if(buffer_size < 0) {
58
        buffer_size = 0;
59
        buffer = NULL;
60
    }
61

    
62
    s->buf = buffer;
63
    s->buf_end = s->buf + buffer_size;
64
#ifdef ALT_BITSTREAM_WRITER
65
    s->index=0;
66
    ((uint32_t*)(s->buf))[0]=0;
67
//    memset(buffer, 0, buffer_size);
68
#else
69
    s->buf_ptr = s->buf;
70
    s->bit_left=32;
71
    s->bit_buf=0;
72
#endif
73
}
74

    
75
/* return the number of bits output */
76
static inline int put_bits_count(PutBitContext *s)
77
{
78
#ifdef ALT_BITSTREAM_WRITER
79
    return s->index;
80
#else
81
    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
82
#endif
83
}
84

    
85
/* pad the end of the output stream with zeros */
86
static inline void flush_put_bits(PutBitContext *s)
87
{
88
#ifdef ALT_BITSTREAM_WRITER
89
    align_put_bits(s);
90
#else
91
    s->bit_buf<<= s->bit_left;
92
    while (s->bit_left < 32) {
93
        /* XXX: should test end of buffer */
94
        *s->buf_ptr++=s->bit_buf >> 24;
95
        s->bit_buf<<=8;
96
        s->bit_left+=8;
97
    }
98
    s->bit_left=32;
99
    s->bit_buf=0;
100
#endif
101
}
102

    
103
void align_put_bits(PutBitContext *s);
104
void ff_put_string(PutBitContext * pbc, char *s, int put_zero);
105

    
106
/* bit input */
107
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
108
typedef struct GetBitContext {
109
    const uint8_t *buffer, *buffer_end;
110
#ifdef ALT_BITSTREAM_READER
111
    int index;
112
#elif defined LIBMPEG2_BITSTREAM_READER
113
    uint8_t *buffer_ptr;
114
    uint32_t cache;
115
    int bit_count;
116
#elif defined A32_BITSTREAM_READER
117
    uint32_t *buffer_ptr;
118
    uint32_t cache0;
119
    uint32_t cache1;
120
    int bit_count;
121
#endif
122
    int size_in_bits;
123
} GetBitContext;
124

    
125
#define VLC_TYPE int16_t
126

    
127
typedef struct VLC {
128
    int bits;
129
    VLC_TYPE (*table)[2]; ///< code, bits
130
    int table_size, table_allocated;
131
} VLC;
132

    
133
typedef struct RL_VLC_ELEM {
134
    int16_t level;
135
    int8_t len;
136
    uint8_t run;
137
} RL_VLC_ELEM;
138

    
139
#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS)
140
#define UNALIGNED_STORES_ARE_BAD
141
#endif
142

    
143
/* used to avoid missaligned exceptions on some archs (alpha, ...) */
144
#if defined(ARCH_X86) || defined(ARCH_X86_64)
145
#    define unaligned16(a) (*(const uint16_t*)(a))
146
#    define unaligned32(a) (*(const uint32_t*)(a))
147
#    define unaligned64(a) (*(const uint64_t*)(a))
148
#else
149
#    ifdef __GNUC__
150
#    define unaligned(x)                                \
151
static inline uint##x##_t unaligned##x(const void *v) { \
152
    struct Unaligned {                                  \
153
        uint##x##_t i;                                  \
154
    } __attribute__((packed));                          \
155
                                                        \
156
    return ((const struct Unaligned *) v)->i;           \
157
}
158
#    elif defined(__DECC)
159
#    define unaligned(x)                                        \
160
static inline uint##x##_t unaligned##x##(const void *v) {       \
161
    return *(const __unaligned uint##x##_t *) v;                \
162
}
163
#    else
164
#    define unaligned(x)                                        \
165
static inline uint##x##_t unaligned##x##(const void *v) {       \
166
    return *(const uint##x##_t *) v;                            \
167
}
168
#    endif
169
unaligned(16)
170
unaligned(32)
171
unaligned(64)
172
#undef unaligned
173
#endif //!ARCH_X86
174

    
175
#ifndef ALT_BITSTREAM_WRITER
176
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
177
{
178
    unsigned int bit_buf;
179
    int bit_left;
180

    
181
    //    printf("put_bits=%d %x\n", n, value);
182
    assert(n == 32 || value < (1U << n));
183

    
184
    bit_buf = s->bit_buf;
185
    bit_left = s->bit_left;
186

    
187
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
188
    /* XXX: optimize */
189
    if (n < bit_left) {
190
        bit_buf = (bit_buf<<n) | value;
191
        bit_left-=n;
192
    } else {
193
        bit_buf<<=bit_left;
194
        bit_buf |= value >> (n - bit_left);
195
#ifdef UNALIGNED_STORES_ARE_BAD
196
        if (3 & (intptr_t) s->buf_ptr) {
197
            s->buf_ptr[0] = bit_buf >> 24;
198
            s->buf_ptr[1] = bit_buf >> 16;
199
            s->buf_ptr[2] = bit_buf >>  8;
200
            s->buf_ptr[3] = bit_buf      ;
201
        } else
202
#endif
203
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
204
        //printf("bitbuf = %08x\n", bit_buf);
205
        s->buf_ptr+=4;
206
        bit_left+=32 - n;
207
        bit_buf = value;
208
    }
209

    
210
    s->bit_buf = bit_buf;
211
    s->bit_left = bit_left;
212
}
213
#endif
214

    
215

    
216
#ifdef ALT_BITSTREAM_WRITER
217
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
218
{
219
#    ifdef ALIGNED_BITSTREAM_WRITER
220
#        if defined(ARCH_X86) || defined(ARCH_X86_64)
221
    asm volatile(
222
        "movl %0, %%ecx                 \n\t"
223
        "xorl %%eax, %%eax              \n\t"
224
        "shrdl %%cl, %1, %%eax          \n\t"
225
        "shrl %%cl, %1                  \n\t"
226
        "movl %0, %%ecx                 \n\t"
227
        "shrl $3, %%ecx                 \n\t"
228
        "andl $0xFFFFFFFC, %%ecx        \n\t"
229
        "bswapl %1                      \n\t"
230
        "orl %1, (%2, %%ecx)            \n\t"
231
        "bswapl %%eax                   \n\t"
232
        "addl %3, %0                    \n\t"
233
        "movl %%eax, 4(%2, %%ecx)       \n\t"
234
        : "=&r" (s->index), "=&r" (value)
235
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
236
        : "%eax", "%ecx"
237
    );
238
#        else
239
    int index= s->index;
240
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
241

    
242
    value<<= 32-n;
243

    
244
    ptr[0] |= be2me_32(value>>(index&31));
245
    ptr[1]  = be2me_32(value<<(32-(index&31)));
246
//if(n>24) printf("%d %d\n", n, value);
247
    index+= n;
248
    s->index= index;
249
#        endif
250
#    else //ALIGNED_BITSTREAM_WRITER
251
#        if defined(ARCH_X86) || defined(ARCH_X86_64)
252
    asm volatile(
253
        "movl $7, %%ecx                 \n\t"
254
        "andl %0, %%ecx                 \n\t"
255
        "addl %3, %%ecx                 \n\t"
256
        "negl %%ecx                     \n\t"
257
        "shll %%cl, %1                  \n\t"
258
        "bswapl %1                      \n\t"
259
        "movl %0, %%ecx                 \n\t"
260
        "shrl $3, %%ecx                 \n\t"
261
        "orl %1, (%%ecx, %2)            \n\t"
262
        "addl %3, %0                    \n\t"
263
        "movl $0, 4(%%ecx, %2)          \n\t"
264
        : "=&r" (s->index), "=&r" (value)
265
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
266
        : "%ecx"
267
    );
268
#        else
269
    int index= s->index;
270
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
271

    
272
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
273
    ptr[1] = 0;
274
//if(n>24) printf("%d %d\n", n, value);
275
    index+= n;
276
    s->index= index;
277
#        endif
278
#    endif //!ALIGNED_BITSTREAM_WRITER
279
}
280
#endif
281

    
282

    
283
static inline uint8_t* pbBufPtr(PutBitContext *s)
284
{
285
#ifdef ALT_BITSTREAM_WRITER
286
        return s->buf + (s->index>>3);
287
#else
288
        return s->buf_ptr;
289
#endif
290
}
291

    
292
/**
293
 *
294
 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
295
 */
296
static inline void skip_put_bytes(PutBitContext *s, int n){
297
        assert((put_bits_count(s)&7)==0);
298
#ifdef ALT_BITSTREAM_WRITER
299
        FIXME may need some cleaning of the buffer
300
        s->index += n<<3;
301
#else
302
        assert(s->bit_left==32);
303
        s->buf_ptr += n;
304
#endif
305
}
306

    
307
/**
308
 * skips the given number of bits.
309
 * must only be used if the actual values in the bitstream dont matter
310
 */
311
static inline void skip_put_bits(PutBitContext *s, int n){
312
#ifdef ALT_BITSTREAM_WRITER
313
    s->index += n;
314
#else
315
    s->bit_left -= n;
316
    s->buf_ptr-= s->bit_left>>5;
317
    s->bit_left &= 31;
318
#endif
319
}
320

    
321
/**
322
 * Changes the end of the buffer.
323
 */
324
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
325
    s->buf_end= s->buf + size;
326
}
327

    
328
/* Bitstream reader API docs:
329
name
330
    abritary name which is used as prefix for the internal variables
331

332
gb
333
    getbitcontext
334

335
OPEN_READER(name, gb)
336
    loads gb into local variables
337

338
CLOSE_READER(name, gb)
339
    stores local vars in gb
340

341
UPDATE_CACHE(name, gb)
342
    refills the internal cache from the bitstream
343
    after this call at least MIN_CACHE_BITS will be available,
344

345
GET_CACHE(name, gb)
346
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
347

348
SHOW_UBITS(name, gb, num)
349
    will return the next num bits
350

351
SHOW_SBITS(name, gb, num)
352
    will return the next num bits and do sign extension
353

354
SKIP_BITS(name, gb, num)
355
    will skip over the next num bits
356
    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
357

358
SKIP_CACHE(name, gb, num)
359
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
360

361
SKIP_COUNTER(name, gb, num)
362
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
363

364
LAST_SKIP_CACHE(name, gb, num)
365
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
366

367
LAST_SKIP_BITS(name, gb, num)
368
    is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
369

370
for examples see get_bits, show_bits, skip_bits, get_vlc
371
*/
372

    
373
static inline int unaligned32_be(const void *v)
374
{
375
#ifdef CONFIG_ALIGN
376
        const uint8_t *p=v;
377
        return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
378
#else
379
        return be2me_32( unaligned32(v)); //original
380
#endif
381
}
382

    
383
static inline int unaligned32_le(const void *v)
384
{
385
#ifdef CONFIG_ALIGN
386
       const uint8_t *p=v;
387
       return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
388
#else
389
       return le2me_32( unaligned32(v)); //original
390
#endif
391
}
392

    
393
#ifdef ALT_BITSTREAM_READER
394
#   define MIN_CACHE_BITS 25
395

    
396
#   define OPEN_READER(name, gb)\
397
        int name##_index= (gb)->index;\
398
        int name##_cache= 0;\
399

    
400
#   define CLOSE_READER(name, gb)\
401
        (gb)->index= name##_index;\
402

    
403
# ifdef ALT_BITSTREAM_READER_LE
404
#   define UPDATE_CACHE(name, gb)\
405
        name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
406

    
407
#   define SKIP_CACHE(name, gb, num)\
408
        name##_cache >>= (num);
409
# else
410
#   define UPDATE_CACHE(name, gb)\
411
        name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
412

    
413
#   define SKIP_CACHE(name, gb, num)\
414
        name##_cache <<= (num);
415
# endif
416

    
417
// FIXME name?
418
#   define SKIP_COUNTER(name, gb, num)\
419
        name##_index += (num);\
420

    
421
#   define SKIP_BITS(name, gb, num)\
422
        {\
423
            SKIP_CACHE(name, gb, num)\
424
            SKIP_COUNTER(name, gb, num)\
425
        }\
426

    
427
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
428
#   define LAST_SKIP_CACHE(name, gb, num) ;
429

    
430
# ifdef ALT_BITSTREAM_READER_LE
431
#   define SHOW_UBITS(name, gb, num)\
432
        ((name##_cache) & (NEG_USR32(0xffffffff,num)))
433
# else
434
#   define SHOW_UBITS(name, gb, num)\
435
        NEG_USR32(name##_cache, num)
436
# endif
437

    
438
#   define SHOW_SBITS(name, gb, num)\
439
        NEG_SSR32(name##_cache, num)
440

    
441
#   define GET_CACHE(name, gb)\
442
        ((uint32_t)name##_cache)
443

    
444
static inline int get_bits_count(GetBitContext *s){
445
    return s->index;
446
}
447

    
448
static inline void skip_bits_long(GetBitContext *s, int n){
449
    s->index += n;
450
}
451

    
452
#elif defined LIBMPEG2_BITSTREAM_READER
453
//libmpeg2 like reader
454

    
455
#   define MIN_CACHE_BITS 17
456

    
457
#   define OPEN_READER(name, gb)\
458
        int name##_bit_count=(gb)->bit_count;\
459
        int name##_cache= (gb)->cache;\
460
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
461

    
462
#   define CLOSE_READER(name, gb)\
463
        (gb)->bit_count= name##_bit_count;\
464
        (gb)->cache= name##_cache;\
465
        (gb)->buffer_ptr= name##_buffer_ptr;\
466

    
467
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
468

    
469
#   define UPDATE_CACHE(name, gb)\
470
    if(name##_bit_count >= 0){\
471
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
472
        name##_buffer_ptr += 2;\
473
        name##_bit_count-= 16;\
474
    }\
475

    
476
#else
477

    
478
#   define UPDATE_CACHE(name, gb)\
479
    if(name##_bit_count >= 0){\
480
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
481
        name##_buffer_ptr+=2;\
482
        name##_bit_count-= 16;\
483
    }\
484

    
485
#endif
486

    
487
#   define SKIP_CACHE(name, gb, num)\
488
        name##_cache <<= (num);\
489

    
490
#   define SKIP_COUNTER(name, gb, num)\
491
        name##_bit_count += (num);\
492

    
493
#   define SKIP_BITS(name, gb, num)\
494
        {\
495
            SKIP_CACHE(name, gb, num)\
496
            SKIP_COUNTER(name, gb, num)\
497
        }\
498

    
499
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
500
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
501

    
502
#   define SHOW_UBITS(name, gb, num)\
503
        NEG_USR32(name##_cache, num)
504

    
505
#   define SHOW_SBITS(name, gb, num)\
506
        NEG_SSR32(name##_cache, num)
507

    
508
#   define GET_CACHE(name, gb)\
509
        ((uint32_t)name##_cache)
510

    
511
static inline int get_bits_count(GetBitContext *s){
512
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
513
}
514

    
515
static inline void skip_bits_long(GetBitContext *s, int n){
516
    OPEN_READER(re, s)
517
    re_bit_count += n;
518
    re_buffer_ptr += 2*(re_bit_count>>4);
519
    re_bit_count &= 15;
520
    re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
521
    UPDATE_CACHE(re, s)
522
    CLOSE_READER(re, s)
523
}
524

    
525
#elif defined A32_BITSTREAM_READER
526

    
527
#   define MIN_CACHE_BITS 32
528

    
529
#   define OPEN_READER(name, gb)\
530
        int name##_bit_count=(gb)->bit_count;\
531
        uint32_t name##_cache0= (gb)->cache0;\
532
        uint32_t name##_cache1= (gb)->cache1;\
533
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
534

    
535
#   define CLOSE_READER(name, gb)\
536
        (gb)->bit_count= name##_bit_count;\
537
        (gb)->cache0= name##_cache0;\
538
        (gb)->cache1= name##_cache1;\
539
        (gb)->buffer_ptr= name##_buffer_ptr;\
540

    
541
#   define UPDATE_CACHE(name, gb)\
542
    if(name##_bit_count > 0){\
543
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
544
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
545
        name##_cache1 |= next<<name##_bit_count;\
546
        name##_buffer_ptr++;\
547
        name##_bit_count-= 32;\
548
    }\
549

    
550
#if defined(ARCH_X86) || defined(ARCH_X86_64)
551
#   define SKIP_CACHE(name, gb, num)\
552
        asm(\
553
            "shldl %2, %1, %0          \n\t"\
554
            "shll %2, %1               \n\t"\
555
            : "+r" (name##_cache0), "+r" (name##_cache1)\
556
            : "Ic" ((uint8_t)num)\
557
           );
558
#else
559
#   define SKIP_CACHE(name, gb, num)\
560
        name##_cache0 <<= (num);\
561
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
562
        name##_cache1 <<= (num);
563
#endif
564

    
565
#   define SKIP_COUNTER(name, gb, num)\
566
        name##_bit_count += (num);\
567

    
568
#   define SKIP_BITS(name, gb, num)\
569
        {\
570
            SKIP_CACHE(name, gb, num)\
571
            SKIP_COUNTER(name, gb, num)\
572
        }\
573

    
574
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
575
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
576

    
577
#   define SHOW_UBITS(name, gb, num)\
578
        NEG_USR32(name##_cache0, num)
579

    
580
#   define SHOW_SBITS(name, gb, num)\
581
        NEG_SSR32(name##_cache0, num)
582

    
583
#   define GET_CACHE(name, gb)\
584
        (name##_cache0)
585

    
586
static inline int get_bits_count(GetBitContext *s){
587
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
588
}
589

    
590
static inline void skip_bits_long(GetBitContext *s, int n){
591
    OPEN_READER(re, s)
592
    re_bit_count += n;
593
    re_buffer_ptr += re_bit_count>>5;
594
    re_bit_count &= 31;
595
    re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
596
    re_cache1 = 0;
597
    UPDATE_CACHE(re, s)
598
    CLOSE_READER(re, s)
599
}
600

    
601
#endif
602

    
603
/**
604
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
605
 * if MSB not set it is negative
606
 * @param n length in bits
607
 * @author BERO
608
 */
609
static inline int get_xbits(GetBitContext *s, int n){
610
    register int sign;
611
    register int32_t cache;
612
    OPEN_READER(re, s)
613
    UPDATE_CACHE(re, s)
614
    cache = GET_CACHE(re,s);
615
    sign=(~cache)>>31;
616
    LAST_SKIP_BITS(re, s, n)
617
    CLOSE_READER(re, s)
618
    return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
619
}
620

    
621
static inline int get_sbits(GetBitContext *s, int n){
622
    register int tmp;
623
    OPEN_READER(re, s)
624
    UPDATE_CACHE(re, s)
625
    tmp= SHOW_SBITS(re, s, n);
626
    LAST_SKIP_BITS(re, s, n)
627
    CLOSE_READER(re, s)
628
    return tmp;
629
}
630

    
631
/**
632
 * reads 0-17 bits.
633
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
634
 */
635
static inline unsigned int get_bits(GetBitContext *s, int n){
636
    register int tmp;
637
    OPEN_READER(re, s)
638
    UPDATE_CACHE(re, s)
639
    tmp= SHOW_UBITS(re, s, n);
640
    LAST_SKIP_BITS(re, s, n)
641
    CLOSE_READER(re, s)
642
    return tmp;
643
}
644

    
645
unsigned int get_bits_long(GetBitContext *s, int n);
646

    
647
/**
648
 * shows 0-17 bits.
649
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
650
 */
651
static inline unsigned int show_bits(GetBitContext *s, int n){
652
    register int tmp;
653
    OPEN_READER(re, s)
654
    UPDATE_CACHE(re, s)
655
    tmp= SHOW_UBITS(re, s, n);
656
//    CLOSE_READER(re, s)
657
    return tmp;
658
}
659

    
660
unsigned int show_bits_long(GetBitContext *s, int n);
661

    
662
static inline void skip_bits(GetBitContext *s, int n){
663
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
664
    OPEN_READER(re, s)
665
    UPDATE_CACHE(re, s)
666
    LAST_SKIP_BITS(re, s, n)
667
    CLOSE_READER(re, s)
668
}
669

    
670
static inline unsigned int get_bits1(GetBitContext *s){
671
#ifdef ALT_BITSTREAM_READER
672
    int index= s->index;
673
    uint8_t result= s->buffer[ index>>3 ];
674
#ifdef ALT_BITSTREAM_READER_LE
675
    result>>= (index&0x07);
676
    result&= 1;
677
#else
678
    result<<= (index&0x07);
679
    result>>= 8 - 1;
680
#endif
681
    index++;
682
    s->index= index;
683

    
684
    return result;
685
#else
686
    return get_bits(s, 1);
687
#endif
688
}
689

    
690
static inline unsigned int show_bits1(GetBitContext *s){
691
    return show_bits(s, 1);
692
}
693

    
694
static inline void skip_bits1(GetBitContext *s){
695
    skip_bits(s, 1);
696
}
697

    
698
/**
699
 * init GetBitContext.
700
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
701
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
702
 * @param bit_size the size of the buffer in bits
703
 */
704
static inline void init_get_bits(GetBitContext *s,
705
                   const uint8_t *buffer, int bit_size)
706
{
707
    int buffer_size= (bit_size+7)>>3;
708
    if(buffer_size < 0 || bit_size < 0) {
709
        buffer_size = bit_size = 0;
710
        buffer = NULL;
711
    }
712

    
713
    s->buffer= buffer;
714
    s->size_in_bits= bit_size;
715
    s->buffer_end= buffer + buffer_size;
716
#ifdef ALT_BITSTREAM_READER
717
    s->index=0;
718
#elif defined LIBMPEG2_BITSTREAM_READER
719
    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
720
    s->bit_count = 16 + 8*((intptr_t)buffer&1);
721
    skip_bits_long(s, 0);
722
#elif defined A32_BITSTREAM_READER
723
    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
724
    s->bit_count = 32 + 8*((intptr_t)buffer&3);
725
    skip_bits_long(s, 0);
726
#endif
727
}
728

    
729
static void align_get_bits(GetBitContext *s)
730
{
731
    int n= (-get_bits_count(s)) & 7;
732
    if(n) skip_bits(s, n);
733
}
734

    
735
int check_marker(GetBitContext *s, const char *msg);
736
int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
737
             const void *bits, int bits_wrap, int bits_size,
738
             const void *codes, int codes_wrap, int codes_size,
739
             int flags);
740
#define INIT_VLC_USE_STATIC 1
741
#define INIT_VLC_LE         2
742
void free_vlc(VLC *vlc);
743

    
744
/**
745
 *
746
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
747
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
748
 * is undefined
749
 */
750
#define GET_VLC(code, name, gb, table, bits, max_depth)\
751
{\
752
    int n, index, nb_bits;\
753
\
754
    index= SHOW_UBITS(name, gb, bits);\
755
    code = table[index][0];\
756
    n    = table[index][1];\
757
\
758
    if(max_depth > 1 && n < 0){\
759
        LAST_SKIP_BITS(name, gb, bits)\
760
        UPDATE_CACHE(name, gb)\
761
\
762
        nb_bits = -n;\
763
\
764
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
765
        code = table[index][0];\
766
        n    = table[index][1];\
767
        if(max_depth > 2 && n < 0){\
768
            LAST_SKIP_BITS(name, gb, nb_bits)\
769
            UPDATE_CACHE(name, gb)\
770
\
771
            nb_bits = -n;\
772
\
773
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
774
            code = table[index][0];\
775
            n    = table[index][1];\
776
        }\
777
    }\
778
    SKIP_BITS(name, gb, n)\
779
}
780

    
781
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
782
{\
783
    int n, index, nb_bits;\
784
\
785
    index= SHOW_UBITS(name, gb, bits);\
786
    level = table[index].level;\
787
    n     = table[index].len;\
788
\
789
    if(max_depth > 1 && n < 0){\
790
        SKIP_BITS(name, gb, bits)\
791
        if(need_update){\
792
            UPDATE_CACHE(name, gb)\
793
        }\
794
\
795
        nb_bits = -n;\
796
\
797
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
798
        level = table[index].level;\
799
        n     = table[index].len;\
800
    }\
801
    run= table[index].run;\
802
    SKIP_BITS(name, gb, n)\
803
}
804

    
805

    
806
/**
807
 * parses a vlc code, faster then get_vlc()
808
 * @param bits is the number of bits which will be read at once, must be
809
 *             identical to nb_bits in init_vlc()
810
 * @param max_depth is the number of times bits bits must be readed to completly
811
 *                  read the longest vlc code
812
 *                  = (max_vlc_length + bits - 1) / bits
813
 */
814
static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
815
                                  int bits, int max_depth)
816
{
817
    int code;
818

    
819
    OPEN_READER(re, s)
820
    UPDATE_CACHE(re, s)
821

    
822
    GET_VLC(code, re, s, table, bits, max_depth)
823

    
824
    CLOSE_READER(re, s)
825
    return code;
826
}
827

    
828
//#define TRACE
829

    
830
#ifdef TRACE
831
#include "avcodec.h"
832
static inline void print_bin(int bits, int n){
833
    int i;
834

    
835
    for(i=n-1; i>=0; i--){
836
        av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
837
    }
838
    for(i=n; i<24; i++)
839
        av_log(NULL, AV_LOG_DEBUG, " ");
840
}
841

    
842
static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
843
    int r= get_bits(s, n);
844

    
845
    print_bin(r, n);
846
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line);
847
    return r;
848
}
849
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
850
    int show= show_bits(s, 24);
851
    int pos= get_bits_count(s);
852
    int r= get_vlc2(s, table, bits, max_depth);
853
    int len= get_bits_count(s) - pos;
854
    int bits2= show>>(24-len);
855

    
856
    print_bin(bits2, len);
857

    
858
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
859
    return r;
860
}
861
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
862
    int show= show_bits(s, n);
863
    int r= get_xbits(s, n);
864

    
865
    print_bin(show, n);
866
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line);
867
    return r;
868
}
869

    
870
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
871
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
872
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
873
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
874
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
875

    
876
#define tprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
877

    
878
#else //TRACE
879
#define tprintf(...) {}
880
#endif
881

    
882
static inline int decode012(GetBitContext *gb){
883
    int n;
884
    n = get_bits1(gb);
885
    if (n == 0)
886
        return 0;
887
    else
888
        return get_bits1(gb) + 1;
889
}
890

    
891
#endif /* BITSTREAM_H */