Statistics
| Branch: | Revision:

ffmpeg / libavcodec / get_bits.h @ 2912e87a

History | View | Annotate | Download (19.6 KB)

1
/*
2
 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav 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
 * Libav 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 Libav; 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
23
 * bitstream reader API header.
24
 */
25

    
26
#ifndef AVCODEC_GET_BITS_H
27
#define AVCODEC_GET_BITS_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
#include "mathops.h"
37

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

    
42
#if !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
43
#   if ARCH_ARM && !HAVE_FAST_UNALIGNED
44
#       define A32_BITSTREAM_READER
45
#   else
46
#       define ALT_BITSTREAM_READER
47
//#define A32_BITSTREAM_READER
48
#   endif
49
#endif
50

    
51
/* bit input */
52
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
53
typedef struct GetBitContext {
54
    const uint8_t *buffer, *buffer_end;
55
#ifdef ALT_BITSTREAM_READER
56
    int index;
57
#elif defined A32_BITSTREAM_READER
58
    uint32_t *buffer_ptr;
59
    uint32_t cache0;
60
    uint32_t cache1;
61
    int bit_count;
62
#endif
63
    int size_in_bits;
64
} GetBitContext;
65

    
66
#define VLC_TYPE int16_t
67

    
68
typedef struct VLC {
69
    int bits;
70
    VLC_TYPE (*table)[2]; ///< code, bits
71
    int table_size, table_allocated;
72
} VLC;
73

    
74
typedef struct RL_VLC_ELEM {
75
    int16_t level;
76
    int8_t len;
77
    uint8_t run;
78
} RL_VLC_ELEM;
79

    
80
/* Bitstream reader API docs:
81
name
82
    arbitrary name which is used as prefix for the internal variables
83

84
gb
85
    getbitcontext
86

87
OPEN_READER(name, gb)
88
    loads gb into local variables
89

90
CLOSE_READER(name, gb)
91
    stores local vars in gb
92

93
UPDATE_CACHE(name, gb)
94
    refills the internal cache from the bitstream
95
    after this call at least MIN_CACHE_BITS will be available,
96

97
GET_CACHE(name, gb)
98
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
99

100
SHOW_UBITS(name, gb, num)
101
    will return the next num bits
102

103
SHOW_SBITS(name, gb, num)
104
    will return the next num bits and do sign extension
105

106
SKIP_BITS(name, gb, num)
107
    will skip over the next num bits
108
    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
109

110
SKIP_CACHE(name, gb, num)
111
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
112

113
SKIP_COUNTER(name, gb, num)
114
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
115

116
LAST_SKIP_CACHE(name, gb, num)
117
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
118

119
LAST_SKIP_BITS(name, gb, num)
120
    is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
121

122
for examples see get_bits, show_bits, skip_bits, get_vlc
123
*/
124

    
125
#ifdef ALT_BITSTREAM_READER
126
#   define MIN_CACHE_BITS 25
127

    
128
#   define OPEN_READER(name, gb)                \
129
    unsigned int name##_index = (gb)->index;    \
130
    int name##_cache          = 0
131

    
132
#   define CLOSE_READER(name, gb) (gb)->index = name##_index
133

    
134
# ifdef ALT_BITSTREAM_READER_LE
135
#   define UPDATE_CACHE(name, gb) \
136
    name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07)
137

    
138
#   define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
139
# else
140
#   define UPDATE_CACHE(name, gb) \
141
    name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07)
142

    
143
#   define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
144
# endif
145

    
146
// FIXME name?
147
#   define SKIP_COUNTER(name, gb, num) name##_index += (num)
148

    
149
#   define SKIP_BITS(name, gb, num) do {        \
150
        SKIP_CACHE(name, gb, num);              \
151
        SKIP_COUNTER(name, gb, num);            \
152
    } while (0)
153

    
154
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
155
#   define LAST_SKIP_CACHE(name, gb, num)
156

    
157
# ifdef ALT_BITSTREAM_READER_LE
158
#   define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
159

    
160
#   define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
161
# else
162
#   define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
163

    
164
#   define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
165
# endif
166

    
167
#   define GET_CACHE(name, gb) ((uint32_t)name##_cache)
168

    
169
static inline int get_bits_count(const GetBitContext *s){
170
    return s->index;
171
}
172

    
173
static inline void skip_bits_long(GetBitContext *s, int n){
174
    s->index += n;
175
}
176

    
177
#elif defined A32_BITSTREAM_READER
178

    
179
#   define MIN_CACHE_BITS 32
180

    
181
#   define OPEN_READER(name, gb)                        \
182
    int name##_bit_count        = (gb)->bit_count;      \
183
    uint32_t name##_cache0      = (gb)->cache0;         \
184
    uint32_t name##_cache1      = (gb)->cache1;         \
185
    uint32_t *name##_buffer_ptr = (gb)->buffer_ptr
186

    
187
#   define CLOSE_READER(name, gb) do {          \
188
        (gb)->bit_count  = name##_bit_count;    \
189
        (gb)->cache0     = name##_cache0;       \
190
        (gb)->cache1     = name##_cache1;       \
191
        (gb)->buffer_ptr = name##_buffer_ptr;   \
192
    } while (0)
193

    
194
#   define UPDATE_CACHE(name, gb) do {                                  \
195
        if(name##_bit_count > 0){                                       \
196
            const uint32_t next = av_be2ne32(*name##_buffer_ptr);       \
197
            name##_cache0 |= NEG_USR32(next, name##_bit_count);         \
198
            name##_cache1 |= next << name##_bit_count;                  \
199
            name##_buffer_ptr++;                                        \
200
            name##_bit_count -= 32;                                     \
201
        }                                                               \
202
    } while (0)
203

    
204
#if ARCH_X86
205
#   define SKIP_CACHE(name, gb, num)                            \
206
    __asm__("shldl %2, %1, %0          \n\t"                    \
207
            "shll  %2, %1              \n\t"                    \
208
            : "+r" (name##_cache0), "+r" (name##_cache1)        \
209
            : "Ic" ((uint8_t)(num)))
210
#else
211
#   define SKIP_CACHE(name, gb, num) do {               \
212
        name##_cache0 <<= (num);                        \
213
        name##_cache0 |= NEG_USR32(name##_cache1,num);  \
214
        name##_cache1 <<= (num);                        \
215
    } while (0)
216
#endif
217

    
218
#   define SKIP_COUNTER(name, gb, num) name##_bit_count += (num)
219

    
220
#   define SKIP_BITS(name, gb, num) do {        \
221
        SKIP_CACHE(name, gb, num);              \
222
        SKIP_COUNTER(name, gb, num);            \
223
    } while (0)
224

    
225
#   define LAST_SKIP_BITS(name, gb, num)  SKIP_BITS(name, gb, num)
226
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
227

    
228
#   define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache0, num)
229

    
230
#   define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache0, num)
231

    
232
#   define GET_CACHE(name, gb) name##_cache0
233

    
234
static inline int get_bits_count(const GetBitContext *s) {
235
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
236
}
237

    
238
static inline void skip_bits_long(GetBitContext *s, int n){
239
    OPEN_READER(re, s);
240
    re_bit_count += n;
241
    re_buffer_ptr += re_bit_count>>5;
242
    re_bit_count &= 31;
243
    re_cache0 = av_be2ne32(re_buffer_ptr[-1]) << re_bit_count;
244
    re_cache1 = 0;
245
    UPDATE_CACHE(re, s);
246
    CLOSE_READER(re, s);
247
}
248

    
249
#endif
250

    
251
/**
252
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
253
 * if MSB not set it is negative
254
 * @param n length in bits
255
 * @author BERO
256
 */
257
static inline int get_xbits(GetBitContext *s, int n){
258
    register int sign;
259
    register int32_t cache;
260
    OPEN_READER(re, s);
261
    UPDATE_CACHE(re, s);
262
    cache = GET_CACHE(re, s);
263
    sign = ~cache >> 31;
264
    LAST_SKIP_BITS(re, s, n);
265
    CLOSE_READER(re, s);
266
    return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
267
}
268

    
269
static inline int get_sbits(GetBitContext *s, int n){
270
    register int tmp;
271
    OPEN_READER(re, s);
272
    UPDATE_CACHE(re, s);
273
    tmp = SHOW_SBITS(re, s, n);
274
    LAST_SKIP_BITS(re, s, n);
275
    CLOSE_READER(re, s);
276
    return tmp;
277
}
278

    
279
/**
280
 * Read 1-25 bits.
281
 */
282
static inline unsigned int get_bits(GetBitContext *s, int n){
283
    register int tmp;
284
    OPEN_READER(re, s);
285
    UPDATE_CACHE(re, s);
286
    tmp = SHOW_UBITS(re, s, n);
287
    LAST_SKIP_BITS(re, s, n);
288
    CLOSE_READER(re, s);
289
    return tmp;
290
}
291

    
292
/**
293
 * Shows 1-25 bits.
294
 */
295
static inline unsigned int show_bits(GetBitContext *s, int n){
296
    register int tmp;
297
    OPEN_READER(re, s);
298
    UPDATE_CACHE(re, s);
299
    tmp = SHOW_UBITS(re, s, n);
300
    return tmp;
301
}
302

    
303
static inline void skip_bits(GetBitContext *s, int n){
304
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
305
    OPEN_READER(re, s);
306
    UPDATE_CACHE(re, s);
307
    LAST_SKIP_BITS(re, s, n);
308
    CLOSE_READER(re, s);
309
}
310

    
311
static inline unsigned int get_bits1(GetBitContext *s){
312
#ifdef ALT_BITSTREAM_READER
313
    unsigned int index = s->index;
314
    uint8_t result = s->buffer[index>>3];
315
#ifdef ALT_BITSTREAM_READER_LE
316
    result >>= index & 7;
317
    result &= 1;
318
#else
319
    result <<= index & 7;
320
    result >>= 8 - 1;
321
#endif
322
    index++;
323
    s->index = index;
324

    
325
    return result;
326
#else
327
    return get_bits(s, 1);
328
#endif
329
}
330

    
331
static inline unsigned int show_bits1(GetBitContext *s){
332
    return show_bits(s, 1);
333
}
334

    
335
static inline void skip_bits1(GetBitContext *s){
336
    skip_bits(s, 1);
337
}
338

    
339
/**
340
 * reads 0-32 bits.
341
 */
342
static inline unsigned int get_bits_long(GetBitContext *s, int n){
343
    if (n <= MIN_CACHE_BITS) return get_bits(s, n);
344
    else {
345
#ifdef ALT_BITSTREAM_READER_LE
346
        int ret = get_bits(s, 16);
347
        return ret | (get_bits(s, n-16) << 16);
348
#else
349
        int ret = get_bits(s, 16) << (n-16);
350
        return ret | get_bits(s, n-16);
351
#endif
352
    }
353
}
354

    
355
/**
356
 * reads 0-32 bits as a signed integer.
357
 */
358
static inline int get_sbits_long(GetBitContext *s, int n) {
359
    return sign_extend(get_bits_long(s, n), n);
360
}
361

    
362
/**
363
 * shows 0-32 bits.
364
 */
365
static inline unsigned int show_bits_long(GetBitContext *s, int n){
366
    if (n <= MIN_CACHE_BITS) return show_bits(s, n);
367
    else {
368
        GetBitContext gb = *s;
369
        return get_bits_long(&gb, n);
370
    }
371
}
372

    
373
static inline int check_marker(GetBitContext *s, const char *msg)
374
{
375
    int bit = get_bits1(s);
376
    if (!bit)
377
        av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
378

    
379
    return bit;
380
}
381

    
382
/**
383
 * init GetBitContext.
384
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
385
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
386
 * @param bit_size the size of the buffer in bits
387
 *
388
 * While GetBitContext stores the buffer size, for performance reasons you are
389
 * responsible for checking for the buffer end yourself (take advantage of the padding)!
390
 */
391
static inline void init_get_bits(GetBitContext *s,
392
                   const uint8_t *buffer, int bit_size)
393
{
394
    int buffer_size = (bit_size+7)>>3;
395
    if (buffer_size < 0 || bit_size < 0) {
396
        buffer_size = bit_size = 0;
397
        buffer = NULL;
398
    }
399

    
400
    s->buffer       = buffer;
401
    s->size_in_bits = bit_size;
402
    s->buffer_end   = buffer + buffer_size;
403
#ifdef ALT_BITSTREAM_READER
404
    s->index        = 0;
405
#elif defined A32_BITSTREAM_READER
406
    s->buffer_ptr   = (uint32_t*)((intptr_t)buffer & ~3);
407
    s->bit_count    = 32 +     8*((intptr_t)buffer &  3);
408
    skip_bits_long(s, 0);
409
#endif
410
}
411

    
412
static inline void align_get_bits(GetBitContext *s)
413
{
414
    int n = -get_bits_count(s) & 7;
415
    if (n) skip_bits(s, n);
416
}
417

    
418
#define init_vlc(vlc, nb_bits, nb_codes,                \
419
                 bits, bits_wrap, bits_size,            \
420
                 codes, codes_wrap, codes_size,         \
421
                 flags)                                 \
422
        init_vlc_sparse(vlc, nb_bits, nb_codes,         \
423
                        bits, bits_wrap, bits_size,     \
424
                        codes, codes_wrap, codes_size,  \
425
                        NULL, 0, 0, flags)
426

    
427
int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
428
             const void *bits, int bits_wrap, int bits_size,
429
             const void *codes, int codes_wrap, int codes_size,
430
             const void *symbols, int symbols_wrap, int symbols_size,
431
             int flags);
432
#define INIT_VLC_LE         2
433
#define INIT_VLC_USE_NEW_STATIC 4
434
void free_vlc(VLC *vlc);
435

    
436
#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do {     \
437
        static VLC_TYPE table[static_size][2];                          \
438
        (vlc)->table = table;                                           \
439
        (vlc)->table_allocated = static_size;                           \
440
        init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);    \
441
    } while (0)
442

    
443

    
444
/**
445
 *
446
 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
447
 * If the vlc code is invalid and max_depth>1, then the number of bits removed
448
 * is undefined.
449
 */
450
#define GET_VLC(code, name, gb, table, bits, max_depth) do {    \
451
        int n, nb_bits;                                         \
452
        unsigned int index;                                     \
453
                                                                \
454
        index = SHOW_UBITS(name, gb, bits);                     \
455
        code  = table[index][0];                                \
456
        n     = table[index][1];                                \
457
                                                                \
458
        if (max_depth > 1 && n < 0) {                           \
459
            LAST_SKIP_BITS(name, gb, bits);                     \
460
            UPDATE_CACHE(name, gb);                             \
461
                                                                \
462
            nb_bits = -n;                                       \
463
                                                                \
464
            index = SHOW_UBITS(name, gb, nb_bits) + code;       \
465
            code  = table[index][0];                            \
466
            n     = table[index][1];                            \
467
            if (max_depth > 2 && n < 0) {                       \
468
                LAST_SKIP_BITS(name, gb, nb_bits);              \
469
                UPDATE_CACHE(name, gb);                         \
470
                                                                \
471
                nb_bits = -n;                                   \
472
                                                                \
473
                index = SHOW_UBITS(name, gb, nb_bits) + code;   \
474
                code  = table[index][0];                        \
475
                n     = table[index][1];                        \
476
            }                                                   \
477
        }                                                       \
478
        SKIP_BITS(name, gb, n);                                 \
479
    } while (0)
480

    
481
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \
482
        int n, nb_bits;                                                 \
483
        unsigned int index;                                             \
484
                                                                        \
485
        index = SHOW_UBITS(name, gb, bits);                             \
486
        level = table[index].level;                                     \
487
        n     = table[index].len;                                       \
488
                                                                        \
489
        if (max_depth > 1 && n < 0) {                                   \
490
            SKIP_BITS(name, gb, bits);                                  \
491
            if (need_update) {                                          \
492
                UPDATE_CACHE(name, gb);                                 \
493
            }                                                           \
494
                                                                        \
495
            nb_bits = -n;                                               \
496
                                                                        \
497
            index = SHOW_UBITS(name, gb, nb_bits) + level;              \
498
            level = table[index].level;                                 \
499
            n     = table[index].len;                                   \
500
        }                                                               \
501
        run = table[index].run;                                         \
502
        SKIP_BITS(name, gb, n);                                         \
503
    } while (0)
504

    
505

    
506
/**
507
 * parses a vlc code, faster then get_vlc()
508
 * @param bits is the number of bits which will be read at once, must be
509
 *             identical to nb_bits in init_vlc()
510
 * @param max_depth is the number of times bits bits must be read to completely
511
 *                  read the longest vlc code
512
 *                  = (max_vlc_length + bits - 1) / bits
513
 */
514
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
515
                                  int bits, int max_depth)
516
{
517
    int code;
518

    
519
    OPEN_READER(re, s);
520
    UPDATE_CACHE(re, s);
521

    
522
    GET_VLC(code, re, s, table, bits, max_depth);
523

    
524
    CLOSE_READER(re, s);
525
    return code;
526
}
527

    
528
static inline int decode012(GetBitContext *gb){
529
    int n;
530
    n = get_bits1(gb);
531
    if (n == 0)
532
        return 0;
533
    else
534
        return get_bits1(gb) + 1;
535
}
536

    
537
static inline int decode210(GetBitContext *gb){
538
    if (get_bits1(gb))
539
        return 0;
540
    else
541
        return 2 - get_bits1(gb);
542
}
543

    
544
static inline int get_bits_left(GetBitContext *gb)
545
{
546
    return gb->size_in_bits - get_bits_count(gb);
547
}
548

    
549
//#define TRACE
550

    
551
#ifdef TRACE
552
static inline void print_bin(int bits, int n){
553
    int i;
554

    
555
    for (i = n-1; i >= 0; i--) {
556
        av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
557
    }
558
    for (i = n; i < 24; i++)
559
        av_log(NULL, AV_LOG_DEBUG, " ");
560
}
561

    
562
static inline int get_bits_trace(GetBitContext *s, int n, char *file,
563
                                 const char *func, int line){
564
    int r = get_bits(s, n);
565

    
566
    print_bin(r, n);
567
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
568
           r, n, r, get_bits_count(s)-n, file, func, line);
569
    return r;
570
}
571
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
572
                                int bits, int max_depth, char *file,
573
                                const char *func, int line){
574
    int show  = show_bits(s, 24);
575
    int pos   = get_bits_count(s);
576
    int r     = get_vlc2(s, table, bits, max_depth);
577
    int len   = get_bits_count(s) - pos;
578
    int bits2 = show >> (24-len);
579

    
580
    print_bin(bits2, len);
581

    
582
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
583
           bits2, len, r, pos, file, func, line);
584
    return r;
585
}
586
static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
587
                                  const char *func, int line){
588
    int show = show_bits(s, n);
589
    int r    = get_xbits(s, n);
590

    
591
    print_bin(show, n);
592
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
593
           show, n, r, get_bits_count(s)-n, file, func, line);
594
    return r;
595
}
596

    
597
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
598
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
599
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
600
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
601
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
602

    
603
#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
604

    
605
#else //TRACE
606
#define tprintf(p, ...) {}
607
#endif
608

    
609
#endif /* AVCODEC_GET_BITS_H */