Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bitstream.h @ 71c465a1

History | View | Annotate | Download (25.9 KB)

1
/*
2
 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
/**
22
 * @file bitstream.h
23
 * bitstream api header.
24
 */
25

    
26
#ifndef FFMPEG_BITSTREAM_H
27
#define FFMPEG_BITSTREAM_H
28

    
29
#include <stdint.h>
30
#include <stdlib.h>
31
#include <assert.h>
32
#include "libavutil/bswap.h"
33
#include "libavutil/common.h"
34
#include "libavutil/intreadwrite.h"
35
#include "libavutil/log.h"
36

    
37
#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
38
#   define ALT_BITSTREAM_READER
39
#endif
40

    
41
//#define ALT_BITSTREAM_WRITER
42
//#define ALIGNED_BITSTREAM_WRITER
43
#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
44
#   ifdef ARCH_ARMV4L
45
#       define A32_BITSTREAM_READER
46
#   else
47
#       define ALT_BITSTREAM_READER
48
//#define LIBMPEG2_BITSTREAM_READER
49
//#define A32_BITSTREAM_READER
50
#   endif
51
#endif
52

    
53
extern const uint8_t ff_reverse[256];
54

    
55
#if defined(ARCH_X86)
56
// avoid +32 for shift optimization (gcc should do that ...)
57
static inline  int32_t NEG_SSR32( int32_t a, int8_t s){
58
    asm ("sarl %1, %0\n\t"
59
         : "+r" (a)
60
         : "ic" ((uint8_t)(-s))
61
    );
62
    return a;
63
}
64
static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
65
    asm ("shrl %1, %0\n\t"
66
         : "+r" (a)
67
         : "ic" ((uint8_t)(-s))
68
    );
69
    return a;
70
}
71
#else
72
#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
73
#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
74
#endif
75

    
76
/* bit output */
77

    
78
/* buf and buf_end must be present and used by every alternative writer. */
79
typedef struct PutBitContext {
80
#ifdef ALT_BITSTREAM_WRITER
81
    uint8_t *buf, *buf_end;
82
    int index;
83
#else
84
    uint32_t bit_buf;
85
    int bit_left;
86
    uint8_t *buf, *buf_ptr, *buf_end;
87
#endif
88
} PutBitContext;
89

    
90
static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
91
{
92
    if(buffer_size < 0) {
93
        buffer_size = 0;
94
        buffer = NULL;
95
    }
96

    
97
    s->buf = buffer;
98
    s->buf_end = s->buf + buffer_size;
99
#ifdef ALT_BITSTREAM_WRITER
100
    s->index=0;
101
    ((uint32_t*)(s->buf))[0]=0;
102
//    memset(buffer, 0, buffer_size);
103
#else
104
    s->buf_ptr = s->buf;
105
    s->bit_left=32;
106
    s->bit_buf=0;
107
#endif
108
}
109

    
110
/* return the number of bits output */
111
static inline int put_bits_count(PutBitContext *s)
112
{
113
#ifdef ALT_BITSTREAM_WRITER
114
    return s->index;
115
#else
116
    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
117
#endif
118
}
119

    
120
/* pad the end of the output stream with zeros */
121
static inline void flush_put_bits(PutBitContext *s)
122
{
123
#ifdef ALT_BITSTREAM_WRITER
124
    align_put_bits(s);
125
#else
126
    s->bit_buf<<= s->bit_left;
127
    while (s->bit_left < 32) {
128
        /* XXX: should test end of buffer */
129
        *s->buf_ptr++=s->bit_buf >> 24;
130
        s->bit_buf<<=8;
131
        s->bit_left+=8;
132
    }
133
    s->bit_left=32;
134
    s->bit_buf=0;
135
#endif
136
}
137

    
138
void align_put_bits(PutBitContext *s);
139
void ff_put_string(PutBitContext * pbc, const char *s, int put_zero);
140
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
141

    
142
/* bit input */
143
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
144
typedef struct GetBitContext {
145
    const uint8_t *buffer, *buffer_end;
146
#ifdef ALT_BITSTREAM_READER
147
    int index;
148
#elif defined LIBMPEG2_BITSTREAM_READER
149
    uint8_t *buffer_ptr;
150
    uint32_t cache;
151
    int bit_count;
152
#elif defined A32_BITSTREAM_READER
153
    uint32_t *buffer_ptr;
154
    uint32_t cache0;
155
    uint32_t cache1;
156
    int bit_count;
157
#endif
158
    int size_in_bits;
159
} GetBitContext;
160

    
161
#define VLC_TYPE int16_t
162

    
163
typedef struct VLC {
164
    int bits;
165
    VLC_TYPE (*table)[2]; ///< code, bits
166
    int table_size, table_allocated;
167
} VLC;
168

    
169
typedef struct RL_VLC_ELEM {
170
    int16_t level;
171
    int8_t len;
172
    uint8_t run;
173
} RL_VLC_ELEM;
174

    
175
#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L) || defined(ARCH_MIPS) || defined(ARCH_BFIN)
176
#define UNALIGNED_STORES_ARE_BAD
177
#endif
178

    
179
/* used to avoid misaligned exceptions on some archs (alpha, ...) */
180
#if defined(ARCH_X86)
181
#    define unaligned16(a) (*(const uint16_t*)(a))
182
#    define unaligned32(a) (*(const uint32_t*)(a))
183
#    define unaligned64(a) (*(const uint64_t*)(a))
184
#else
185
#    ifdef __GNUC__
186
#    define unaligned(x)                                \
187
static inline uint##x##_t unaligned##x(const void *v) { \
188
    struct Unaligned {                                  \
189
        uint##x##_t i;                                  \
190
    } __attribute__((packed));                          \
191
                                                        \
192
    return ((const struct Unaligned *) v)->i;           \
193
}
194
#    elif defined(__DECC)
195
#    define unaligned(x)                                        \
196
static inline uint##x##_t unaligned##x(const void *v) {         \
197
    return *(const __unaligned uint##x##_t *) v;                \
198
}
199
#    else
200
#    define unaligned(x)                                        \
201
static inline uint##x##_t unaligned##x(const void *v) {         \
202
    return *(const uint##x##_t *) v;                            \
203
}
204
#    endif
205
unaligned(16)
206
unaligned(32)
207
unaligned(64)
208
#undef unaligned
209
#endif /* defined(ARCH_X86) */
210

    
211
#ifndef ALT_BITSTREAM_WRITER
212
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
213
{
214
    unsigned int bit_buf;
215
    int bit_left;
216

    
217
    //    printf("put_bits=%d %x\n", n, value);
218
    assert(n == 32 || value < (1U << n));
219

    
220
    bit_buf = s->bit_buf;
221
    bit_left = s->bit_left;
222

    
223
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
224
    /* XXX: optimize */
225
    if (n < bit_left) {
226
        bit_buf = (bit_buf<<n) | value;
227
        bit_left-=n;
228
    } else {
229
        bit_buf<<=bit_left;
230
        bit_buf |= value >> (n - bit_left);
231
#ifdef UNALIGNED_STORES_ARE_BAD
232
        if (3 & (intptr_t) s->buf_ptr) {
233
            s->buf_ptr[0] = bit_buf >> 24;
234
            s->buf_ptr[1] = bit_buf >> 16;
235
            s->buf_ptr[2] = bit_buf >>  8;
236
            s->buf_ptr[3] = bit_buf      ;
237
        } else
238
#endif
239
        *(uint32_t *)s->buf_ptr = be2me_32(bit_buf);
240
        //printf("bitbuf = %08x\n", bit_buf);
241
        s->buf_ptr+=4;
242
        bit_left+=32 - n;
243
        bit_buf = value;
244
    }
245

    
246
    s->bit_buf = bit_buf;
247
    s->bit_left = bit_left;
248
}
249
#endif
250

    
251

    
252
#ifdef ALT_BITSTREAM_WRITER
253
static inline void put_bits(PutBitContext *s, int n, unsigned int value)
254
{
255
#    ifdef ALIGNED_BITSTREAM_WRITER
256
#        if defined(ARCH_X86)
257
    asm volatile(
258
        "movl %0, %%ecx                 \n\t"
259
        "xorl %%eax, %%eax              \n\t"
260
        "shrdl %%cl, %1, %%eax          \n\t"
261
        "shrl %%cl, %1                  \n\t"
262
        "movl %0, %%ecx                 \n\t"
263
        "shrl $3, %%ecx                 \n\t"
264
        "andl $0xFFFFFFFC, %%ecx        \n\t"
265
        "bswapl %1                      \n\t"
266
        "orl %1, (%2, %%ecx)            \n\t"
267
        "bswapl %%eax                   \n\t"
268
        "addl %3, %0                    \n\t"
269
        "movl %%eax, 4(%2, %%ecx)       \n\t"
270
        : "=&r" (s->index), "=&r" (value)
271
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
272
        : "%eax", "%ecx"
273
    );
274
#        else
275
    int index= s->index;
276
    uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
277

    
278
    value<<= 32-n;
279

    
280
    ptr[0] |= be2me_32(value>>(index&31));
281
    ptr[1]  = be2me_32(value<<(32-(index&31)));
282
//if(n>24) printf("%d %d\n", n, value);
283
    index+= n;
284
    s->index= index;
285
#        endif
286
#    else //ALIGNED_BITSTREAM_WRITER
287
#        if defined(ARCH_X86)
288
    asm volatile(
289
        "movl $7, %%ecx                 \n\t"
290
        "andl %0, %%ecx                 \n\t"
291
        "addl %3, %%ecx                 \n\t"
292
        "negl %%ecx                     \n\t"
293
        "shll %%cl, %1                  \n\t"
294
        "bswapl %1                      \n\t"
295
        "movl %0, %%ecx                 \n\t"
296
        "shrl $3, %%ecx                 \n\t"
297
        "orl %1, (%%ecx, %2)            \n\t"
298
        "addl %3, %0                    \n\t"
299
        "movl $0, 4(%%ecx, %2)          \n\t"
300
        : "=&r" (s->index), "=&r" (value)
301
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
302
        : "%ecx"
303
    );
304
#        else
305
    int index= s->index;
306
    uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
307

    
308
    ptr[0] |= be2me_32(value<<(32-n-(index&7) ));
309
    ptr[1] = 0;
310
//if(n>24) printf("%d %d\n", n, value);
311
    index+= n;
312
    s->index= index;
313
#        endif
314
#    endif //!ALIGNED_BITSTREAM_WRITER
315
}
316
#endif
317

    
318

    
319
static inline uint8_t* pbBufPtr(PutBitContext *s)
320
{
321
#ifdef ALT_BITSTREAM_WRITER
322
        return s->buf + (s->index>>3);
323
#else
324
        return s->buf_ptr;
325
#endif
326
}
327

    
328
/**
329
 *
330
 * PutBitContext must be flushed & aligned to a byte boundary before calling this.
331
 */
332
static inline void skip_put_bytes(PutBitContext *s, int n){
333
        assert((put_bits_count(s)&7)==0);
334
#ifdef ALT_BITSTREAM_WRITER
335
        FIXME may need some cleaning of the buffer
336
        s->index += n<<3;
337
#else
338
        assert(s->bit_left==32);
339
        s->buf_ptr += n;
340
#endif
341
}
342

    
343
/**
344
 * Skips the given number of bits.
345
 * Must only be used if the actual values in the bitstream do not matter.
346
 */
347
static inline void skip_put_bits(PutBitContext *s, int n){
348
#ifdef ALT_BITSTREAM_WRITER
349
    s->index += n;
350
#else
351
    s->bit_left -= n;
352
    s->buf_ptr-= s->bit_left>>5;
353
    s->bit_left &= 31;
354
#endif
355
}
356

    
357
/**
358
 * Changes the end of the buffer.
359
 */
360
static inline void set_put_bits_buffer_size(PutBitContext *s, int size){
361
    s->buf_end= s->buf + size;
362
}
363

    
364
/* Bitstream reader API docs:
365
name
366
    abritary name which is used as prefix for the internal variables
367

368
gb
369
    getbitcontext
370

371
OPEN_READER(name, gb)
372
    loads gb into local variables
373

374
CLOSE_READER(name, gb)
375
    stores local vars in gb
376

377
UPDATE_CACHE(name, gb)
378
    refills the internal cache from the bitstream
379
    after this call at least MIN_CACHE_BITS will be available,
380

381
GET_CACHE(name, gb)
382
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
383

384
SHOW_UBITS(name, gb, num)
385
    will return the next num bits
386

387
SHOW_SBITS(name, gb, num)
388
    will return the next num bits and do sign extension
389

390
SKIP_BITS(name, gb, num)
391
    will skip over the next num bits
392
    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
393

394
SKIP_CACHE(name, gb, num)
395
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
396

397
SKIP_COUNTER(name, gb, num)
398
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
399

400
LAST_SKIP_CACHE(name, gb, num)
401
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
402

403
LAST_SKIP_BITS(name, gb, num)
404
    is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
405

406
for examples see get_bits, show_bits, skip_bits, get_vlc
407
*/
408

    
409
#ifdef ALT_BITSTREAM_READER
410
#   define MIN_CACHE_BITS 25
411

    
412
#   define OPEN_READER(name, gb)\
413
        int name##_index= (gb)->index;\
414
        int name##_cache= 0;\
415

    
416
#   define CLOSE_READER(name, gb)\
417
        (gb)->index= name##_index;\
418

    
419
# ifdef ALT_BITSTREAM_READER_LE
420
#   define UPDATE_CACHE(name, gb)\
421
        name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
422

    
423
#   define SKIP_CACHE(name, gb, num)\
424
        name##_cache >>= (num);
425
# else
426
#   define UPDATE_CACHE(name, gb)\
427
        name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
428

    
429
#   define SKIP_CACHE(name, gb, num)\
430
        name##_cache <<= (num);
431
# endif
432

    
433
// FIXME name?
434
#   define SKIP_COUNTER(name, gb, num)\
435
        name##_index += (num);\
436

    
437
#   define SKIP_BITS(name, gb, num)\
438
        {\
439
            SKIP_CACHE(name, gb, num)\
440
            SKIP_COUNTER(name, gb, num)\
441
        }\
442

    
443
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
444
#   define LAST_SKIP_CACHE(name, gb, num) ;
445

    
446
# ifdef ALT_BITSTREAM_READER_LE
447
#   define SHOW_UBITS(name, gb, num)\
448
        ((name##_cache) & (NEG_USR32(0xffffffff,num)))
449

    
450
#   define SHOW_SBITS(name, gb, num)\
451
        NEG_SSR32((name##_cache)<<(32-(num)), num)
452
# else
453
#   define SHOW_UBITS(name, gb, num)\
454
        NEG_USR32(name##_cache, num)
455

    
456
#   define SHOW_SBITS(name, gb, num)\
457
        NEG_SSR32(name##_cache, num)
458
# endif
459

    
460
#   define GET_CACHE(name, gb)\
461
        ((uint32_t)name##_cache)
462

    
463
static inline int get_bits_count(GetBitContext *s){
464
    return s->index;
465
}
466

    
467
static inline void skip_bits_long(GetBitContext *s, int n){
468
    s->index += n;
469
}
470

    
471
#elif defined LIBMPEG2_BITSTREAM_READER
472
//libmpeg2 like reader
473

    
474
#   define MIN_CACHE_BITS 17
475

    
476
#   define OPEN_READER(name, gb)\
477
        int name##_bit_count=(gb)->bit_count;\
478
        int name##_cache= (gb)->cache;\
479
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
480

    
481
#   define CLOSE_READER(name, gb)\
482
        (gb)->bit_count= name##_bit_count;\
483
        (gb)->cache= name##_cache;\
484
        (gb)->buffer_ptr= name##_buffer_ptr;\
485

    
486
#   define UPDATE_CACHE(name, gb)\
487
    if(name##_bit_count >= 0){\
488
        name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
489
        name##_buffer_ptr+=2;\
490
        name##_bit_count-= 16;\
491
    }\
492

    
493
#   define SKIP_CACHE(name, gb, num)\
494
        name##_cache <<= (num);\
495

    
496
#   define SKIP_COUNTER(name, gb, num)\
497
        name##_bit_count += (num);\
498

    
499
#   define SKIP_BITS(name, gb, num)\
500
        {\
501
            SKIP_CACHE(name, gb, num)\
502
            SKIP_COUNTER(name, gb, num)\
503
        }\
504

    
505
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
506
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
507

    
508
#   define SHOW_UBITS(name, gb, num)\
509
        NEG_USR32(name##_cache, num)
510

    
511
#   define SHOW_SBITS(name, gb, num)\
512
        NEG_SSR32(name##_cache, num)
513

    
514
#   define GET_CACHE(name, gb)\
515
        ((uint32_t)name##_cache)
516

    
517
static inline int get_bits_count(GetBitContext *s){
518
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
519
}
520

    
521
static inline void skip_bits_long(GetBitContext *s, int n){
522
    OPEN_READER(re, s)
523
    re_bit_count += n;
524
    re_buffer_ptr += 2*(re_bit_count>>4);
525
    re_bit_count &= 15;
526
    re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
527
    UPDATE_CACHE(re, s)
528
    CLOSE_READER(re, s)
529
}
530

    
531
#elif defined A32_BITSTREAM_READER
532

    
533
#   define MIN_CACHE_BITS 32
534

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

    
541
#   define CLOSE_READER(name, gb)\
542
        (gb)->bit_count= name##_bit_count;\
543
        (gb)->cache0= name##_cache0;\
544
        (gb)->cache1= name##_cache1;\
545
        (gb)->buffer_ptr= name##_buffer_ptr;\
546

    
547
#   define UPDATE_CACHE(name, gb)\
548
    if(name##_bit_count > 0){\
549
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
550
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
551
        name##_cache1 |= next<<name##_bit_count;\
552
        name##_buffer_ptr++;\
553
        name##_bit_count-= 32;\
554
    }\
555

    
556
#if defined(ARCH_X86)
557
#   define SKIP_CACHE(name, gb, num)\
558
        asm(\
559
            "shldl %2, %1, %0          \n\t"\
560
            "shll %2, %1               \n\t"\
561
            : "+r" (name##_cache0), "+r" (name##_cache1)\
562
            : "Ic" ((uint8_t)(num))\
563
           );
564
#else
565
#   define SKIP_CACHE(name, gb, num)\
566
        name##_cache0 <<= (num);\
567
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
568
        name##_cache1 <<= (num);
569
#endif
570

    
571
#   define SKIP_COUNTER(name, gb, num)\
572
        name##_bit_count += (num);\
573

    
574
#   define SKIP_BITS(name, gb, num)\
575
        {\
576
            SKIP_CACHE(name, gb, num)\
577
            SKIP_COUNTER(name, gb, num)\
578
        }\
579

    
580
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
581
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
582

    
583
#   define SHOW_UBITS(name, gb, num)\
584
        NEG_USR32(name##_cache0, num)
585

    
586
#   define SHOW_SBITS(name, gb, num)\
587
        NEG_SSR32(name##_cache0, num)
588

    
589
#   define GET_CACHE(name, gb)\
590
        (name##_cache0)
591

    
592
static inline int get_bits_count(GetBitContext *s){
593
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
594
}
595

    
596
static inline void skip_bits_long(GetBitContext *s, int n){
597
    OPEN_READER(re, s)
598
    re_bit_count += n;
599
    re_buffer_ptr += re_bit_count>>5;
600
    re_bit_count &= 31;
601
    re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
602
    re_cache1 = 0;
603
    UPDATE_CACHE(re, s)
604
    CLOSE_READER(re, s)
605
}
606

    
607
#endif
608

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

    
627
static inline int get_sbits(GetBitContext *s, int n){
628
    register int tmp;
629
    OPEN_READER(re, s)
630
    UPDATE_CACHE(re, s)
631
    tmp= SHOW_SBITS(re, s, n);
632
    LAST_SKIP_BITS(re, s, n)
633
    CLOSE_READER(re, s)
634
    return tmp;
635
}
636

    
637
/**
638
 * reads 1-17 bits.
639
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
640
 */
641
static inline unsigned int get_bits(GetBitContext *s, int n){
642
    register int tmp;
643
    OPEN_READER(re, s)
644
    UPDATE_CACHE(re, s)
645
    tmp= SHOW_UBITS(re, s, n);
646
    LAST_SKIP_BITS(re, s, n)
647
    CLOSE_READER(re, s)
648
    return tmp;
649
}
650

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

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

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

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

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

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

    
700
/**
701
 * reads 0-32 bits.
702
 */
703
static inline unsigned int get_bits_long(GetBitContext *s, int n){
704
    if(n<=17) return get_bits(s, n);
705
    else{
706
#ifdef ALT_BITSTREAM_READER_LE
707
        int ret= get_bits(s, 16);
708
        return ret | (get_bits(s, n-16) << 16);
709
#else
710
        int ret= get_bits(s, 16) << (n-16);
711
        return ret | get_bits(s, n-16);
712
#endif
713
    }
714
}
715

    
716
/**
717
 * shows 0-32 bits.
718
 */
719
static inline unsigned int show_bits_long(GetBitContext *s, int n){
720
    if(n<=17) return show_bits(s, n);
721
    else{
722
        GetBitContext gb= *s;
723
        int ret= get_bits_long(s, n);
724
        *s= gb;
725
        return ret;
726
    }
727
}
728

    
729
static inline int check_marker(GetBitContext *s, const char *msg)
730
{
731
    int bit= get_bits1(s);
732
    if(!bit)
733
        av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
734

    
735
    return bit;
736
}
737

    
738
/**
739
 * init GetBitContext.
740
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
741
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
742
 * @param bit_size the size of the buffer in bits
743
 */
744
static inline void init_get_bits(GetBitContext *s,
745
                   const uint8_t *buffer, int bit_size)
746
{
747
    int buffer_size= (bit_size+7)>>3;
748
    if(buffer_size < 0 || bit_size < 0) {
749
        buffer_size = bit_size = 0;
750
        buffer = NULL;
751
    }
752

    
753
    s->buffer= buffer;
754
    s->size_in_bits= bit_size;
755
    s->buffer_end= buffer + buffer_size;
756
#ifdef ALT_BITSTREAM_READER
757
    s->index=0;
758
#elif defined LIBMPEG2_BITSTREAM_READER
759
    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
760
    s->bit_count = 16 + 8*((intptr_t)buffer&1);
761
    skip_bits_long(s, 0);
762
#elif defined A32_BITSTREAM_READER
763
    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
764
    s->bit_count = 32 + 8*((intptr_t)buffer&3);
765
    skip_bits_long(s, 0);
766
#endif
767
}
768

    
769
static inline void align_get_bits(GetBitContext *s)
770
{
771
    int n= (-get_bits_count(s)) & 7;
772
    if(n) skip_bits(s, n);
773
}
774

    
775
#define init_vlc(vlc, nb_bits, nb_codes,\
776
                 bits, bits_wrap, bits_size,\
777
                 codes, codes_wrap, codes_size,\
778
                 flags)\
779
        init_vlc_sparse(vlc, nb_bits, nb_codes,\
780
                 bits, bits_wrap, bits_size,\
781
                 codes, codes_wrap, codes_size,\
782
                 NULL, 0, 0, flags)
783

    
784
int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
785
             const void *bits, int bits_wrap, int bits_size,
786
             const void *codes, int codes_wrap, int codes_size,
787
             const void *symbols, int symbols_wrap, int symbols_size,
788
             int flags);
789
#define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
790
#define INIT_VLC_LE         2
791
#define INIT_VLC_USE_NEW_STATIC 4
792
void free_vlc(VLC *vlc);
793

    
794
#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
795
{\
796
    static VLC_TYPE table[static_size][2];\
797
    (vlc)->table= table;\
798
    (vlc)->table_allocated= static_size;\
799
    init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
800
}
801

    
802

    
803
/**
804
 *
805
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
806
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
807
 * is undefined
808
 */
809
#define GET_VLC(code, name, gb, table, bits, max_depth)\
810
{\
811
    int n, index, nb_bits;\
812
\
813
    index= SHOW_UBITS(name, gb, bits);\
814
    code = table[index][0];\
815
    n    = table[index][1];\
816
\
817
    if(max_depth > 1 && n < 0){\
818
        LAST_SKIP_BITS(name, gb, bits)\
819
        UPDATE_CACHE(name, gb)\
820
\
821
        nb_bits = -n;\
822
\
823
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
824
        code = table[index][0];\
825
        n    = table[index][1];\
826
        if(max_depth > 2 && n < 0){\
827
            LAST_SKIP_BITS(name, gb, nb_bits)\
828
            UPDATE_CACHE(name, gb)\
829
\
830
            nb_bits = -n;\
831
\
832
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
833
            code = table[index][0];\
834
            n    = table[index][1];\
835
        }\
836
    }\
837
    SKIP_BITS(name, gb, n)\
838
}
839

    
840
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
841
{\
842
    int n, index, nb_bits;\
843
\
844
    index= SHOW_UBITS(name, gb, bits);\
845
    level = table[index].level;\
846
    n     = table[index].len;\
847
\
848
    if(max_depth > 1 && n < 0){\
849
        SKIP_BITS(name, gb, bits)\
850
        if(need_update){\
851
            UPDATE_CACHE(name, gb)\
852
        }\
853
\
854
        nb_bits = -n;\
855
\
856
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
857
        level = table[index].level;\
858
        n     = table[index].len;\
859
    }\
860
    run= table[index].run;\
861
    SKIP_BITS(name, gb, n)\
862
}
863

    
864

    
865
/**
866
 * parses a vlc code, faster then get_vlc()
867
 * @param bits is the number of bits which will be read at once, must be
868
 *             identical to nb_bits in init_vlc()
869
 * @param max_depth is the number of times bits bits must be read to completely
870
 *                  read the longest vlc code
871
 *                  = (max_vlc_length + bits - 1) / bits
872
 */
873
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
874
                                  int bits, int max_depth)
875
{
876
    int code;
877

    
878
    OPEN_READER(re, s)
879
    UPDATE_CACHE(re, s)
880

    
881
    GET_VLC(code, re, s, table, bits, max_depth)
882

    
883
    CLOSE_READER(re, s)
884
    return code;
885
}
886

    
887
//#define TRACE
888

    
889
#ifdef TRACE
890
static inline void print_bin(int bits, int n){
891
    int i;
892

    
893
    for(i=n-1; i>=0; i--){
894
        av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
895
    }
896
    for(i=n; i<24; i++)
897
        av_log(NULL, AV_LOG_DEBUG, " ");
898
}
899

    
900
static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
901
    int r= get_bits(s, n);
902

    
903
    print_bin(r, n);
904
    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);
905
    return r;
906
}
907
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
908
    int show= show_bits(s, 24);
909
    int pos= get_bits_count(s);
910
    int r= get_vlc2(s, table, bits, max_depth);
911
    int len= get_bits_count(s) - pos;
912
    int bits2= show>>(24-len);
913

    
914
    print_bin(bits2, len);
915

    
916
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
917
    return r;
918
}
919
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
920
    int show= show_bits(s, n);
921
    int r= get_xbits(s, n);
922

    
923
    print_bin(show, n);
924
    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);
925
    return r;
926
}
927

    
928
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
929
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
930
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
931
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
932
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
933

    
934
#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
935

    
936
#else //TRACE
937
#define tprintf(p, ...) {}
938
#endif
939

    
940
static inline int decode012(GetBitContext *gb){
941
    int n;
942
    n = get_bits1(gb);
943
    if (n == 0)
944
        return 0;
945
    else
946
        return get_bits1(gb) + 1;
947
}
948

    
949
static inline int decode210(GetBitContext *gb){
950
    if (get_bits1(gb))
951
        return 0;
952
    else
953
        return 2 - get_bits1(gb);
954
}
955

    
956
#endif /* FFMPEG_BITSTREAM_H */