Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bitstream.h @ 5b21bdab

History | View | Annotate | Download (25.8 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 "common.h"
33
#include "bswap.h"
34
#include "intreadwrite.h"
35
#include "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
#define LIBMPEG2_BITSTREAM_READER_HACK //add BERO
53

    
54
extern const uint8_t ff_reverse[256];
55

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

    
77
/* bit output */
78

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

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

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

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

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

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

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

    
162
#define VLC_TYPE int16_t
163

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

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

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

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

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

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

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

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

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

    
252

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

    
279
    value<<= 32-n;
280

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

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

    
319

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

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

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

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

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

369
gb
370
    getbitcontext
371

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

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

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

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

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

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

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

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

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

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

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

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

    
410
#ifdef ALT_BITSTREAM_READER
411
#   define MIN_CACHE_BITS 25
412

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
475
#   define MIN_CACHE_BITS 17
476

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

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

    
487
#ifdef LIBMPEG2_BITSTREAM_READER_HACK
488

    
489
#   define UPDATE_CACHE(name, gb)\
490
    if(name##_bit_count >= 0){\
491
        name##_cache+= (int)be2me_16(*(uint16_t*)name##_buffer_ptr) << name##_bit_count;\
492
        name##_buffer_ptr += 2;\
493
        name##_bit_count-= 16;\
494
    }\
495

    
496
#else
497

    
498
#   define UPDATE_CACHE(name, gb)\
499
    if(name##_bit_count >= 0){\
500
        name##_cache+= ((name##_buffer_ptr[0]<<8) + name##_buffer_ptr[1]) << name##_bit_count;\
501
        name##_buffer_ptr+=2;\
502
        name##_bit_count-= 16;\
503
    }\
504

    
505
#endif
506

    
507
#   define SKIP_CACHE(name, gb, num)\
508
        name##_cache <<= (num);\
509

    
510
#   define SKIP_COUNTER(name, gb, num)\
511
        name##_bit_count += (num);\
512

    
513
#   define SKIP_BITS(name, gb, num)\
514
        {\
515
            SKIP_CACHE(name, gb, num)\
516
            SKIP_COUNTER(name, gb, num)\
517
        }\
518

    
519
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
520
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
521

    
522
#   define SHOW_UBITS(name, gb, num)\
523
        NEG_USR32(name##_cache, num)
524

    
525
#   define SHOW_SBITS(name, gb, num)\
526
        NEG_SSR32(name##_cache, num)
527

    
528
#   define GET_CACHE(name, gb)\
529
        ((uint32_t)name##_cache)
530

    
531
static inline int get_bits_count(GetBitContext *s){
532
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
533
}
534

    
535
static inline void skip_bits_long(GetBitContext *s, int n){
536
    OPEN_READER(re, s)
537
    re_bit_count += n;
538
    re_buffer_ptr += 2*(re_bit_count>>4);
539
    re_bit_count &= 15;
540
    re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
541
    UPDATE_CACHE(re, s)
542
    CLOSE_READER(re, s)
543
}
544

    
545
#elif defined A32_BITSTREAM_READER
546

    
547
#   define MIN_CACHE_BITS 32
548

    
549
#   define OPEN_READER(name, gb)\
550
        int name##_bit_count=(gb)->bit_count;\
551
        uint32_t name##_cache0= (gb)->cache0;\
552
        uint32_t name##_cache1= (gb)->cache1;\
553
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
554

    
555
#   define CLOSE_READER(name, gb)\
556
        (gb)->bit_count= name##_bit_count;\
557
        (gb)->cache0= name##_cache0;\
558
        (gb)->cache1= name##_cache1;\
559
        (gb)->buffer_ptr= name##_buffer_ptr;\
560

    
561
#   define UPDATE_CACHE(name, gb)\
562
    if(name##_bit_count > 0){\
563
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
564
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
565
        name##_cache1 |= next<<name##_bit_count;\
566
        name##_buffer_ptr++;\
567
        name##_bit_count-= 32;\
568
    }\
569

    
570
#if defined(ARCH_X86)
571
#   define SKIP_CACHE(name, gb, num)\
572
        asm(\
573
            "shldl %2, %1, %0          \n\t"\
574
            "shll %2, %1               \n\t"\
575
            : "+r" (name##_cache0), "+r" (name##_cache1)\
576
            : "Ic" ((uint8_t)(num))\
577
           );
578
#else
579
#   define SKIP_CACHE(name, gb, num)\
580
        name##_cache0 <<= (num);\
581
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
582
        name##_cache1 <<= (num);
583
#endif
584

    
585
#   define SKIP_COUNTER(name, gb, num)\
586
        name##_bit_count += (num);\
587

    
588
#   define SKIP_BITS(name, gb, num)\
589
        {\
590
            SKIP_CACHE(name, gb, num)\
591
            SKIP_COUNTER(name, gb, num)\
592
        }\
593

    
594
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
595
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
596

    
597
#   define SHOW_UBITS(name, gb, num)\
598
        NEG_USR32(name##_cache0, num)
599

    
600
#   define SHOW_SBITS(name, gb, num)\
601
        NEG_SSR32(name##_cache0, num)
602

    
603
#   define GET_CACHE(name, gb)\
604
        (name##_cache0)
605

    
606
static inline int get_bits_count(GetBitContext *s){
607
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
608
}
609

    
610
static inline void skip_bits_long(GetBitContext *s, int n){
611
    OPEN_READER(re, s)
612
    re_bit_count += n;
613
    re_buffer_ptr += re_bit_count>>5;
614
    re_bit_count &= 31;
615
    re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
616
    re_cache1 = 0;
617
    UPDATE_CACHE(re, s)
618
    CLOSE_READER(re, s)
619
}
620

    
621
#endif
622

    
623
/**
624
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
625
 * if MSB not set it is negative
626
 * @param n length in bits
627
 * @author BERO
628
 */
629
static inline int get_xbits(GetBitContext *s, int n){
630
    register int sign;
631
    register int32_t cache;
632
    OPEN_READER(re, s)
633
    UPDATE_CACHE(re, s)
634
    cache = GET_CACHE(re,s);
635
    sign=(~cache)>>31;
636
    LAST_SKIP_BITS(re, s, n)
637
    CLOSE_READER(re, s)
638
    return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
639
}
640

    
641
static inline int get_sbits(GetBitContext *s, int n){
642
    register int tmp;
643
    OPEN_READER(re, s)
644
    UPDATE_CACHE(re, s)
645
    tmp= SHOW_SBITS(re, s, n);
646
    LAST_SKIP_BITS(re, s, n)
647
    CLOSE_READER(re, s)
648
    return tmp;
649
}
650

    
651
/**
652
 * reads 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 get_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
    LAST_SKIP_BITS(re, s, n)
661
    CLOSE_READER(re, s)
662
    return tmp;
663
}
664

    
665
/**
666
 * shows 1-17 bits.
667
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
668
 */
669
static inline unsigned int show_bits(GetBitContext *s, int n){
670
    register int tmp;
671
    OPEN_READER(re, s)
672
    UPDATE_CACHE(re, s)
673
    tmp= SHOW_UBITS(re, s, n);
674
//    CLOSE_READER(re, s)
675
    return tmp;
676
}
677

    
678
static inline void skip_bits(GetBitContext *s, int n){
679
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
680
    OPEN_READER(re, s)
681
    UPDATE_CACHE(re, s)
682
    LAST_SKIP_BITS(re, s, n)
683
    CLOSE_READER(re, s)
684
}
685

    
686
static inline unsigned int get_bits1(GetBitContext *s){
687
#ifdef ALT_BITSTREAM_READER
688
    int index= s->index;
689
    uint8_t result= s->buffer[ index>>3 ];
690
#ifdef ALT_BITSTREAM_READER_LE
691
    result>>= (index&0x07);
692
    result&= 1;
693
#else
694
    result<<= (index&0x07);
695
    result>>= 8 - 1;
696
#endif
697
    index++;
698
    s->index= index;
699

    
700
    return result;
701
#else
702
    return get_bits(s, 1);
703
#endif
704
}
705

    
706
static inline unsigned int show_bits1(GetBitContext *s){
707
    return show_bits(s, 1);
708
}
709

    
710
static inline void skip_bits1(GetBitContext *s){
711
    skip_bits(s, 1);
712
}
713

    
714
/**
715
 * reads 0-32 bits.
716
 */
717
static inline unsigned int get_bits_long(GetBitContext *s, int n){
718
    if(n<=17) return get_bits(s, n);
719
    else{
720
#ifdef ALT_BITSTREAM_READER_LE
721
        int ret= get_bits(s, 16);
722
        return ret | (get_bits(s, n-16) << 16);
723
#else
724
        int ret= get_bits(s, 16) << (n-16);
725
        return ret | get_bits(s, n-16);
726
#endif
727
    }
728
}
729

    
730
/**
731
 * shows 0-32 bits.
732
 */
733
static inline unsigned int show_bits_long(GetBitContext *s, int n){
734
    if(n<=17) return show_bits(s, n);
735
    else{
736
        GetBitContext gb= *s;
737
        int ret= get_bits_long(s, n);
738
        *s= gb;
739
        return ret;
740
    }
741
}
742

    
743
static inline int check_marker(GetBitContext *s, const char *msg)
744
{
745
    int bit= get_bits1(s);
746
    if(!bit)
747
        av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
748

    
749
    return bit;
750
}
751

    
752
/**
753
 * init GetBitContext.
754
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
755
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
756
 * @param bit_size the size of the buffer in bits
757
 */
758
static inline void init_get_bits(GetBitContext *s,
759
                   const uint8_t *buffer, int bit_size)
760
{
761
    int buffer_size= (bit_size+7)>>3;
762
    if(buffer_size < 0 || bit_size < 0) {
763
        buffer_size = bit_size = 0;
764
        buffer = NULL;
765
    }
766

    
767
    s->buffer= buffer;
768
    s->size_in_bits= bit_size;
769
    s->buffer_end= buffer + buffer_size;
770
#ifdef ALT_BITSTREAM_READER
771
    s->index=0;
772
#elif defined LIBMPEG2_BITSTREAM_READER
773
    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
774
    s->bit_count = 16 + 8*((intptr_t)buffer&1);
775
    skip_bits_long(s, 0);
776
#elif defined A32_BITSTREAM_READER
777
    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
778
    s->bit_count = 32 + 8*((intptr_t)buffer&3);
779
    skip_bits_long(s, 0);
780
#endif
781
}
782

    
783
static inline void align_get_bits(GetBitContext *s)
784
{
785
    int n= (-get_bits_count(s)) & 7;
786
    if(n) skip_bits(s, n);
787
}
788

    
789
#define init_vlc(vlc, nb_bits, nb_codes,\
790
                 bits, bits_wrap, bits_size,\
791
                 codes, codes_wrap, codes_size,\
792
                 flags)\
793
        init_vlc_sparse(vlc, nb_bits, nb_codes,\
794
                 bits, bits_wrap, bits_size,\
795
                 codes, codes_wrap, codes_size,\
796
                 NULL, 0, 0, flags)
797

    
798
int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
799
             const void *bits, int bits_wrap, int bits_size,
800
             const void *codes, int codes_wrap, int codes_size,
801
             const void *symbols, int symbols_wrap, int symbols_size,
802
             int flags);
803
#define INIT_VLC_USE_STATIC 1
804
#define INIT_VLC_LE         2
805
void free_vlc(VLC *vlc);
806

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

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

    
868

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

    
882
    OPEN_READER(re, s)
883
    UPDATE_CACHE(re, s)
884

    
885
    GET_VLC(code, re, s, table, bits, max_depth)
886

    
887
    CLOSE_READER(re, s)
888
    return code;
889
}
890

    
891
//#define TRACE
892

    
893
#ifdef TRACE
894
static inline void print_bin(int bits, int n){
895
    int i;
896

    
897
    for(i=n-1; i>=0; i--){
898
        av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
899
    }
900
    for(i=n; i<24; i++)
901
        av_log(NULL, AV_LOG_DEBUG, " ");
902
}
903

    
904
static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
905
    int r= get_bits(s, n);
906

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

    
918
    print_bin(bits2, len);
919

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

    
927
    print_bin(show, n);
928
    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);
929
    return r;
930
}
931

    
932
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
933
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
934
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
935
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
936
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
937

    
938
#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
939

    
940
#else //TRACE
941
#define tprintf(p, ...) {}
942
#endif
943

    
944
static inline int decode012(GetBitContext *gb){
945
    int n;
946
    n = get_bits1(gb);
947
    if (n == 0)
948
        return 0;
949
    else
950
        return get_bits1(gb) + 1;
951
}
952

    
953
#endif /* FFMPEG_BITSTREAM_H */