Revision 9106a698

View differences:

libavcodec/4xm.c
27 27
#include "libavutil/intreadwrite.h"
28 28
#include "avcodec.h"
29 29
#include "dsputil.h"
30
#include "bitstream.h"
30
#include "get_bits.h"
31 31
#include "bytestream.h"
32 32

  
33 33
//#undef NDEBUG
libavcodec/aac.c
78 78

  
79 79
#include "avcodec.h"
80 80
#include "internal.h"
81
#include "bitstream.h"
81
#include "get_bits.h"
82 82
#include "dsputil.h"
83 83
#include "lpc.h"
84 84

  
libavcodec/aac_parser.c
23 23
#include "parser.h"
24 24
#include "aac_ac3_parser.h"
25 25
#include "aac_parser.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27
#include "mpeg4audio.h"
28 28

  
29 29
#define AAC_HEADER_SIZE 7
libavcodec/aac_parser.h
25 25

  
26 26
#include <stdint.h>
27 27
#include "aac_ac3_parser.h"
28
#include "bitstream.h"
28
#include "get_bits.h"
29 29

  
30 30
typedef struct {
31 31
    uint32_t sample_rate;
libavcodec/aacenc.c
32 32
 ***********************************/
33 33

  
34 34
#include "avcodec.h"
35
#include "bitstream.h"
35
#include "get_bits.h"
36 36
#include "dsputil.h"
37 37
#include "mpeg4audio.h"
38 38

  
libavcodec/ac3.c
26 26

  
27 27
#include "avcodec.h"
28 28
#include "ac3.h"
29
#include "bitstream.h"
29
#include "get_bits.h"
30 30

  
31 31
static uint8_t band_start_tab[51];
32 32
static uint8_t bin_to_band_tab[253];
libavcodec/ac3_parser.c
23 23
#include "parser.h"
24 24
#include "ac3_parser.h"
25 25
#include "aac_ac3_parser.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27

  
28 28

  
29 29
#define AC3_HEADER_SIZE 7
libavcodec/ac3_parser.h
24 24
#define AVCODEC_AC3_PARSER_H
25 25

  
26 26
#include "ac3.h"
27
#include "bitstream.h"
27
#include "get_bits.h"
28 28

  
29 29
/**
30 30
 * Parses AC-3 frame header.
libavcodec/ac3dec.h
30 30
#include "libavutil/internal.h"
31 31
#include "libavutil/lfg.h"
32 32
#include "ac3.h"
33
#include "bitstream.h"
33
#include "get_bits.h"
34 34
#include "dsputil.h"
35 35

  
36 36
/* override ac3.h to include coupling channel */
libavcodec/ac3enc.c
27 27
//#define DEBUG_BITALLOC
28 28
#include "libavutil/crc.h"
29 29
#include "avcodec.h"
30
#include "bitstream.h" // for ff_reverse
30
#include "get_bits.h" // for ff_reverse
31 31
#include "put_bits.h"
32 32
#include "ac3.h"
33 33

  
libavcodec/adpcm.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21
#include "avcodec.h"
22
#include "bitstream.h"
22
#include "get_bits.h"
23 23
#include "put_bits.h"
24 24
#include "bytestream.h"
25 25

  
libavcodec/alac.c
53 53

  
54 54

  
55 55
#include "avcodec.h"
56
#include "bitstream.h"
56
#include "get_bits.h"
57 57
#include "bytestream.h"
58 58
#include "unary.h"
59 59
#include "mathops.h"
libavcodec/alacenc.c
20 20
 */
21 21

  
22 22
#include "avcodec.h"
23
#include "bitstream.h"
23
#include "get_bits.h"
24 24
#include "put_bits.h"
25 25
#include "dsputil.h"
26 26
#include "lpc.h"
libavcodec/apedec.c
23 23
#define ALT_BITSTREAM_READER_LE
24 24
#include "avcodec.h"
25 25
#include "dsputil.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27
#include "bytestream.h"
28 28

  
29 29
/**
libavcodec/asv1.c
25 25
 */
26 26

  
27 27
#include "avcodec.h"
28
#include "bitstream.h"
28
#include "get_bits.h"
29 29
#include "put_bits.h"
30 30
#include "dsputil.h"
31 31
#include "mpeg12data.h"
libavcodec/atrac3.c
37 37
#include <stdio.h>
38 38

  
39 39
#include "avcodec.h"
40
#include "bitstream.h"
40
#include "get_bits.h"
41 41
#include "dsputil.h"
42 42
#include "bytestream.h"
43 43

  
libavcodec/avs.c
20 20
 */
21 21

  
22 22
#include "avcodec.h"
23
#include "bitstream.h"
23
#include "get_bits.h"
24 24

  
25 25

  
26 26
typedef struct {
libavcodec/bitstream.c
28 28
 */
29 29

  
30 30
#include "avcodec.h"
31
#include "bitstream.h"
31
#include "get_bits.h"
32 32
#include "put_bits.h"
33 33

  
34 34
const uint8_t ff_log2_run[32]={
libavcodec/bitstream.h
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 libavcodec/bitstream.h
23
 * bitstream api header.
24
 */
25

  
26
#ifndef AVCODEC_BITSTREAM_H
27
#define AVCODEC_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
#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(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
43
#   if ARCH_ARM
44
#       define A32_BITSTREAM_READER
45
#   else
46
#       define ALT_BITSTREAM_READER
47
//#define LIBMPEG2_BITSTREAM_READER
48
//#define A32_BITSTREAM_READER
49
#   endif
50
#endif
51

  
52
extern const uint8_t ff_reverse[256];
53

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

  
75
/* bit input */
76
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
77
typedef struct GetBitContext {
78
    const uint8_t *buffer, *buffer_end;
79
#ifdef ALT_BITSTREAM_READER
80
    int index;
81
#elif defined LIBMPEG2_BITSTREAM_READER
82
    uint8_t *buffer_ptr;
83
    uint32_t cache;
84
    int bit_count;
85
#elif defined A32_BITSTREAM_READER
86
    uint32_t *buffer_ptr;
87
    uint32_t cache0;
88
    uint32_t cache1;
89
    int bit_count;
90
#endif
91
    int size_in_bits;
92
} GetBitContext;
93

  
94
#define VLC_TYPE int16_t
95

  
96
typedef struct VLC {
97
    int bits;
98
    VLC_TYPE (*table)[2]; ///< code, bits
99
    int table_size, table_allocated;
100
} VLC;
101

  
102
typedef struct RL_VLC_ELEM {
103
    int16_t level;
104
    int8_t len;
105
    uint8_t run;
106
} RL_VLC_ELEM;
107

  
108
/* Bitstream reader API docs:
109
name
110
    arbitrary name which is used as prefix for the internal variables
111

  
112
gb
113
    getbitcontext
114

  
115
OPEN_READER(name, gb)
116
    loads gb into local variables
117

  
118
CLOSE_READER(name, gb)
119
    stores local vars in gb
120

  
121
UPDATE_CACHE(name, gb)
122
    refills the internal cache from the bitstream
123
    after this call at least MIN_CACHE_BITS will be available,
124

  
125
GET_CACHE(name, gb)
126
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
127

  
128
SHOW_UBITS(name, gb, num)
129
    will return the next num bits
130

  
131
SHOW_SBITS(name, gb, num)
132
    will return the next num bits and do sign extension
133

  
134
SKIP_BITS(name, gb, num)
135
    will skip over the next num bits
136
    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
137

  
138
SKIP_CACHE(name, gb, num)
139
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
140

  
141
SKIP_COUNTER(name, gb, num)
142
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
143

  
144
LAST_SKIP_CACHE(name, gb, num)
145
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
146

  
147
LAST_SKIP_BITS(name, gb, num)
148
    is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
149

  
150
for examples see get_bits, show_bits, skip_bits, get_vlc
151
*/
152

  
153
#ifdef ALT_BITSTREAM_READER
154
#   define MIN_CACHE_BITS 25
155

  
156
#   define OPEN_READER(name, gb)\
157
        int name##_index= (gb)->index;\
158
        int name##_cache= 0;\
159

  
160
#   define CLOSE_READER(name, gb)\
161
        (gb)->index= name##_index;\
162

  
163
# ifdef ALT_BITSTREAM_READER_LE
164
#   define UPDATE_CACHE(name, gb)\
165
        name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
166

  
167
#   define SKIP_CACHE(name, gb, num)\
168
        name##_cache >>= (num);
169
# else
170
#   define UPDATE_CACHE(name, gb)\
171
        name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
172

  
173
#   define SKIP_CACHE(name, gb, num)\
174
        name##_cache <<= (num);
175
# endif
176

  
177
// FIXME name?
178
#   define SKIP_COUNTER(name, gb, num)\
179
        name##_index += (num);\
180

  
181
#   define SKIP_BITS(name, gb, num)\
182
        {\
183
            SKIP_CACHE(name, gb, num)\
184
            SKIP_COUNTER(name, gb, num)\
185
        }\
186

  
187
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
188
#   define LAST_SKIP_CACHE(name, gb, num) ;
189

  
190
# ifdef ALT_BITSTREAM_READER_LE
191
#   define SHOW_UBITS(name, gb, num)\
192
        ((name##_cache) & (NEG_USR32(0xffffffff,num)))
193

  
194
#   define SHOW_SBITS(name, gb, num)\
195
        NEG_SSR32((name##_cache)<<(32-(num)), num)
196
# else
197
#   define SHOW_UBITS(name, gb, num)\
198
        NEG_USR32(name##_cache, num)
199

  
200
#   define SHOW_SBITS(name, gb, num)\
201
        NEG_SSR32(name##_cache, num)
202
# endif
203

  
204
#   define GET_CACHE(name, gb)\
205
        ((uint32_t)name##_cache)
206

  
207
static inline int get_bits_count(GetBitContext *s){
208
    return s->index;
209
}
210

  
211
static inline void skip_bits_long(GetBitContext *s, int n){
212
    s->index += n;
213
}
214

  
215
#elif defined LIBMPEG2_BITSTREAM_READER
216
//libmpeg2 like reader
217

  
218
#   define MIN_CACHE_BITS 17
219

  
220
#   define OPEN_READER(name, gb)\
221
        int name##_bit_count=(gb)->bit_count;\
222
        int name##_cache= (gb)->cache;\
223
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
224

  
225
#   define CLOSE_READER(name, gb)\
226
        (gb)->bit_count= name##_bit_count;\
227
        (gb)->cache= name##_cache;\
228
        (gb)->buffer_ptr= name##_buffer_ptr;\
229

  
230
#   define UPDATE_CACHE(name, gb)\
231
    if(name##_bit_count >= 0){\
232
        name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
233
        name##_buffer_ptr+=2;\
234
        name##_bit_count-= 16;\
235
    }\
236

  
237
#   define SKIP_CACHE(name, gb, num)\
238
        name##_cache <<= (num);\
239

  
240
#   define SKIP_COUNTER(name, gb, num)\
241
        name##_bit_count += (num);\
242

  
243
#   define SKIP_BITS(name, gb, num)\
244
        {\
245
            SKIP_CACHE(name, gb, num)\
246
            SKIP_COUNTER(name, gb, num)\
247
        }\
248

  
249
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
250
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
251

  
252
#   define SHOW_UBITS(name, gb, num)\
253
        NEG_USR32(name##_cache, num)
254

  
255
#   define SHOW_SBITS(name, gb, num)\
256
        NEG_SSR32(name##_cache, num)
257

  
258
#   define GET_CACHE(name, gb)\
259
        ((uint32_t)name##_cache)
260

  
261
static inline int get_bits_count(GetBitContext *s){
262
    return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count;
263
}
264

  
265
static inline void skip_bits_long(GetBitContext *s, int n){
266
    OPEN_READER(re, s)
267
    re_bit_count += n;
268
    re_buffer_ptr += 2*(re_bit_count>>4);
269
    re_bit_count &= 15;
270
    re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count);
271
    UPDATE_CACHE(re, s)
272
    CLOSE_READER(re, s)
273
}
274

  
275
#elif defined A32_BITSTREAM_READER
276

  
277
#   define MIN_CACHE_BITS 32
278

  
279
#   define OPEN_READER(name, gb)\
280
        int name##_bit_count=(gb)->bit_count;\
281
        uint32_t name##_cache0= (gb)->cache0;\
282
        uint32_t name##_cache1= (gb)->cache1;\
283
        uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
284

  
285
#   define CLOSE_READER(name, gb)\
286
        (gb)->bit_count= name##_bit_count;\
287
        (gb)->cache0= name##_cache0;\
288
        (gb)->cache1= name##_cache1;\
289
        (gb)->buffer_ptr= name##_buffer_ptr;\
290

  
291
#   define UPDATE_CACHE(name, gb)\
292
    if(name##_bit_count > 0){\
293
        const uint32_t next= be2me_32( *name##_buffer_ptr );\
294
        name##_cache0 |= NEG_USR32(next,name##_bit_count);\
295
        name##_cache1 |= next<<name##_bit_count;\
296
        name##_buffer_ptr++;\
297
        name##_bit_count-= 32;\
298
    }\
299

  
300
#if ARCH_X86
301
#   define SKIP_CACHE(name, gb, num)\
302
        __asm__(\
303
            "shldl %2, %1, %0          \n\t"\
304
            "shll %2, %1               \n\t"\
305
            : "+r" (name##_cache0), "+r" (name##_cache1)\
306
            : "Ic" ((uint8_t)(num))\
307
           );
308
#else
309
#   define SKIP_CACHE(name, gb, num)\
310
        name##_cache0 <<= (num);\
311
        name##_cache0 |= NEG_USR32(name##_cache1,num);\
312
        name##_cache1 <<= (num);
313
#endif
314

  
315
#   define SKIP_COUNTER(name, gb, num)\
316
        name##_bit_count += (num);\
317

  
318
#   define SKIP_BITS(name, gb, num)\
319
        {\
320
            SKIP_CACHE(name, gb, num)\
321
            SKIP_COUNTER(name, gb, num)\
322
        }\
323

  
324
#   define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
325
#   define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
326

  
327
#   define SHOW_UBITS(name, gb, num)\
328
        NEG_USR32(name##_cache0, num)
329

  
330
#   define SHOW_SBITS(name, gb, num)\
331
        NEG_SSR32(name##_cache0, num)
332

  
333
#   define GET_CACHE(name, gb)\
334
        (name##_cache0)
335

  
336
static inline int get_bits_count(GetBitContext *s){
337
    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
338
}
339

  
340
static inline void skip_bits_long(GetBitContext *s, int n){
341
    OPEN_READER(re, s)
342
    re_bit_count += n;
343
    re_buffer_ptr += re_bit_count>>5;
344
    re_bit_count &= 31;
345
    re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
346
    re_cache1 = 0;
347
    UPDATE_CACHE(re, s)
348
    CLOSE_READER(re, s)
349
}
350

  
351
#endif
352

  
353
/**
354
 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
355
 * if MSB not set it is negative
356
 * @param n length in bits
357
 * @author BERO
358
 */
359
static inline int get_xbits(GetBitContext *s, int n){
360
    register int sign;
361
    register int32_t cache;
362
    OPEN_READER(re, s)
363
    UPDATE_CACHE(re, s)
364
    cache = GET_CACHE(re,s);
365
    sign=(~cache)>>31;
366
    LAST_SKIP_BITS(re, s, n)
367
    CLOSE_READER(re, s)
368
    return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
369
}
370

  
371
static inline int get_sbits(GetBitContext *s, int n){
372
    register int tmp;
373
    OPEN_READER(re, s)
374
    UPDATE_CACHE(re, s)
375
    tmp= SHOW_SBITS(re, s, n);
376
    LAST_SKIP_BITS(re, s, n)
377
    CLOSE_READER(re, s)
378
    return tmp;
379
}
380

  
381
/**
382
 * reads 1-17 bits.
383
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
384
 */
385
static inline unsigned int get_bits(GetBitContext *s, int n){
386
    register int tmp;
387
    OPEN_READER(re, s)
388
    UPDATE_CACHE(re, s)
389
    tmp= SHOW_UBITS(re, s, n);
390
    LAST_SKIP_BITS(re, s, n)
391
    CLOSE_READER(re, s)
392
    return tmp;
393
}
394

  
395
/**
396
 * shows 1-17 bits.
397
 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
398
 */
399
static inline unsigned int show_bits(GetBitContext *s, int n){
400
    register int tmp;
401
    OPEN_READER(re, s)
402
    UPDATE_CACHE(re, s)
403
    tmp= SHOW_UBITS(re, s, n);
404
//    CLOSE_READER(re, s)
405
    return tmp;
406
}
407

  
408
static inline void skip_bits(GetBitContext *s, int n){
409
 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
410
    OPEN_READER(re, s)
411
    UPDATE_CACHE(re, s)
412
    LAST_SKIP_BITS(re, s, n)
413
    CLOSE_READER(re, s)
414
}
415

  
416
static inline unsigned int get_bits1(GetBitContext *s){
417
#ifdef ALT_BITSTREAM_READER
418
    int index= s->index;
419
    uint8_t result= s->buffer[ index>>3 ];
420
#ifdef ALT_BITSTREAM_READER_LE
421
    result>>= (index&0x07);
422
    result&= 1;
423
#else
424
    result<<= (index&0x07);
425
    result>>= 8 - 1;
426
#endif
427
    index++;
428
    s->index= index;
429

  
430
    return result;
431
#else
432
    return get_bits(s, 1);
433
#endif
434
}
435

  
436
static inline unsigned int show_bits1(GetBitContext *s){
437
    return show_bits(s, 1);
438
}
439

  
440
static inline void skip_bits1(GetBitContext *s){
441
    skip_bits(s, 1);
442
}
443

  
444
/**
445
 * reads 0-32 bits.
446
 */
447
static inline unsigned int get_bits_long(GetBitContext *s, int n){
448
    if(n<=17) return get_bits(s, n);
449
    else{
450
#ifdef ALT_BITSTREAM_READER_LE
451
        int ret= get_bits(s, 16);
452
        return ret | (get_bits(s, n-16) << 16);
453
#else
454
        int ret= get_bits(s, 16) << (n-16);
455
        return ret | get_bits(s, n-16);
456
#endif
457
    }
458
}
459

  
460
/**
461
 * reads 0-32 bits as a signed integer.
462
 */
463
static inline int get_sbits_long(GetBitContext *s, int n) {
464
    return sign_extend(get_bits_long(s, n), n);
465
}
466

  
467
/**
468
 * shows 0-32 bits.
469
 */
470
static inline unsigned int show_bits_long(GetBitContext *s, int n){
471
    if(n<=17) return show_bits(s, n);
472
    else{
473
        GetBitContext gb= *s;
474
        return get_bits_long(&gb, n);
475
    }
476
}
477

  
478
static inline int check_marker(GetBitContext *s, const char *msg)
479
{
480
    int bit= get_bits1(s);
481
    if(!bit)
482
        av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
483

  
484
    return bit;
485
}
486

  
487
/**
488
 * init GetBitContext.
489
 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
490
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
491
 * @param bit_size the size of the buffer in bits
492
 */
493
static inline void init_get_bits(GetBitContext *s,
494
                   const uint8_t *buffer, int bit_size)
495
{
496
    int buffer_size= (bit_size+7)>>3;
497
    if(buffer_size < 0 || bit_size < 0) {
498
        buffer_size = bit_size = 0;
499
        buffer = NULL;
500
    }
501

  
502
    s->buffer= buffer;
503
    s->size_in_bits= bit_size;
504
    s->buffer_end= buffer + buffer_size;
505
#ifdef ALT_BITSTREAM_READER
506
    s->index=0;
507
#elif defined LIBMPEG2_BITSTREAM_READER
508
    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));
509
    s->bit_count = 16 + 8*((intptr_t)buffer&1);
510
    skip_bits_long(s, 0);
511
#elif defined A32_BITSTREAM_READER
512
    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
513
    s->bit_count = 32 + 8*((intptr_t)buffer&3);
514
    skip_bits_long(s, 0);
515
#endif
516
}
517

  
518
static inline void align_get_bits(GetBitContext *s)
519
{
520
    int n= (-get_bits_count(s)) & 7;
521
    if(n) skip_bits(s, n);
522
}
523

  
524
#define init_vlc(vlc, nb_bits, nb_codes,\
525
                 bits, bits_wrap, bits_size,\
526
                 codes, codes_wrap, codes_size,\
527
                 flags)\
528
        init_vlc_sparse(vlc, nb_bits, nb_codes,\
529
                 bits, bits_wrap, bits_size,\
530
                 codes, codes_wrap, codes_size,\
531
                 NULL, 0, 0, flags)
532

  
533
int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
534
             const void *bits, int bits_wrap, int bits_size,
535
             const void *codes, int codes_wrap, int codes_size,
536
             const void *symbols, int symbols_wrap, int symbols_size,
537
             int flags);
538
#define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden
539
#define INIT_VLC_LE         2
540
#define INIT_VLC_USE_NEW_STATIC 4
541
void free_vlc(VLC *vlc);
542

  
543
#define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\
544
{\
545
    static VLC_TYPE table[static_size][2];\
546
    (vlc)->table= table;\
547
    (vlc)->table_allocated= static_size;\
548
    init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\
549
}
550

  
551

  
552
/**
553
 *
554
 * if the vlc code is invalid and max_depth=1 than no bits will be removed
555
 * if the vlc code is invalid and max_depth>1 than the number of bits removed
556
 * is undefined
557
 */
558
#define GET_VLC(code, name, gb, table, bits, max_depth)\
559
{\
560
    int n, index, nb_bits;\
561
\
562
    index= SHOW_UBITS(name, gb, bits);\
563
    code = table[index][0];\
564
    n    = table[index][1];\
565
\
566
    if(max_depth > 1 && n < 0){\
567
        LAST_SKIP_BITS(name, gb, bits)\
568
        UPDATE_CACHE(name, gb)\
569
\
570
        nb_bits = -n;\
571
\
572
        index= SHOW_UBITS(name, gb, nb_bits) + code;\
573
        code = table[index][0];\
574
        n    = table[index][1];\
575
        if(max_depth > 2 && n < 0){\
576
            LAST_SKIP_BITS(name, gb, nb_bits)\
577
            UPDATE_CACHE(name, gb)\
578
\
579
            nb_bits = -n;\
580
\
581
            index= SHOW_UBITS(name, gb, nb_bits) + code;\
582
            code = table[index][0];\
583
            n    = table[index][1];\
584
        }\
585
    }\
586
    SKIP_BITS(name, gb, n)\
587
}
588

  
589
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\
590
{\
591
    int n, index, nb_bits;\
592
\
593
    index= SHOW_UBITS(name, gb, bits);\
594
    level = table[index].level;\
595
    n     = table[index].len;\
596
\
597
    if(max_depth > 1 && n < 0){\
598
        SKIP_BITS(name, gb, bits)\
599
        if(need_update){\
600
            UPDATE_CACHE(name, gb)\
601
        }\
602
\
603
        nb_bits = -n;\
604
\
605
        index= SHOW_UBITS(name, gb, nb_bits) + level;\
606
        level = table[index].level;\
607
        n     = table[index].len;\
608
    }\
609
    run= table[index].run;\
610
    SKIP_BITS(name, gb, n)\
611
}
612

  
613

  
614
/**
615
 * parses a vlc code, faster then get_vlc()
616
 * @param bits is the number of bits which will be read at once, must be
617
 *             identical to nb_bits in init_vlc()
618
 * @param max_depth is the number of times bits bits must be read to completely
619
 *                  read the longest vlc code
620
 *                  = (max_vlc_length + bits - 1) / bits
621
 */
622
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
623
                                  int bits, int max_depth)
624
{
625
    int code;
626

  
627
    OPEN_READER(re, s)
628
    UPDATE_CACHE(re, s)
629

  
630
    GET_VLC(code, re, s, table, bits, max_depth)
631

  
632
    CLOSE_READER(re, s)
633
    return code;
634
}
635

  
636
//#define TRACE
637

  
638
#ifdef TRACE
639
static inline void print_bin(int bits, int n){
640
    int i;
641

  
642
    for(i=n-1; i>=0; i--){
643
        av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
644
    }
645
    for(i=n; i<24; i++)
646
        av_log(NULL, AV_LOG_DEBUG, " ");
647
}
648

  
649
static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
650
    int r= get_bits(s, n);
651

  
652
    print_bin(r, n);
653
    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);
654
    return r;
655
}
656
static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){
657
    int show= show_bits(s, 24);
658
    int pos= get_bits_count(s);
659
    int r= get_vlc2(s, table, bits, max_depth);
660
    int len= get_bits_count(s) - pos;
661
    int bits2= show>>(24-len);
662

  
663
    print_bin(bits2, len);
664

  
665
    av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line);
666
    return r;
667
}
668
static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){
669
    int show= show_bits(s, n);
670
    int r= get_xbits(s, n);
671

  
672
    print_bin(show, n);
673
    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);
674
    return r;
675
}
676

  
677
#define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
678
#define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
679
#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
680
#define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
681
#define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
682

  
683
#define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
684

  
685
#else //TRACE
686
#define tprintf(p, ...) {}
687
#endif
688

  
689
static inline int decode012(GetBitContext *gb){
690
    int n;
691
    n = get_bits1(gb);
692
    if (n == 0)
693
        return 0;
694
    else
695
        return get_bits1(gb) + 1;
696
}
697

  
698
static inline int decode210(GetBitContext *gb){
699
    if (get_bits1(gb))
700
        return 0;
701
    else
702
        return 2 - get_bits1(gb);
703
}
704

  
705
#endif /* AVCODEC_BITSTREAM_H */
libavcodec/cabac.c
27 27
#include <string.h>
28 28

  
29 29
#include "libavutil/common.h"
30
#include "bitstream.h"
30
#include "get_bits.h"
31 31
#include "cabac.h"
32 32

  
33 33
static const uint8_t lps_range[64][4]= {
libavcodec/cavs.c
26 26
 */
27 27

  
28 28
#include "avcodec.h"
29
#include "bitstream.h"
29
#include "get_bits.h"
30 30
#include "golomb.h"
31 31
#include "mathops.h"
32 32
#include "cavs.h"
libavcodec/cavsdec.c
26 26
 */
27 27

  
28 28
#include "avcodec.h"
29
#include "bitstream.h"
29
#include "get_bits.h"
30 30
#include "golomb.h"
31 31
#include "cavs.h"
32 32

  
libavcodec/cljr.c
26 26

  
27 27
#include "avcodec.h"
28 28
#include "dsputil.h"
29
#include "bitstream.h"
29
#include "get_bits.h"
30 30

  
31 31
/* Disable the encoder. */
32 32
#undef CONFIG_CLJR_ENCODER
libavcodec/cook.c
49 49
#include "libavutil/lfg.h"
50 50
#include "libavutil/random_seed.h"
51 51
#include "avcodec.h"
52
#include "bitstream.h"
52
#include "get_bits.h"
53 53
#include "dsputil.h"
54 54
#include "bytestream.h"
55 55

  
libavcodec/dca.c
32 32

  
33 33
#include "avcodec.h"
34 34
#include "dsputil.h"
35
#include "bitstream.h"
35
#include "get_bits.h"
36 36
#include "put_bits.h"
37 37
#include "dcadata.h"
38 38
#include "dcahuff.h"
libavcodec/dnxhddec.c
23 23
//#define DEBUG
24 24

  
25 25
#include "avcodec.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27
#include "dnxhddata.h"
28 28
#include "dsputil.h"
29 29

  
libavcodec/dv.c
40 40
#define ALT_BITSTREAM_READER
41 41
#include "avcodec.h"
42 42
#include "dsputil.h"
43
#include "bitstream.h"
43
#include "get_bits.h"
44 44
#include "put_bits.h"
45 45
#include "simple_idct.h"
46 46
#include "dvdata.h"
libavcodec/dvbsub_parser.c
20 20
 */
21 21
#include "avcodec.h"
22 22
#include "dsputil.h"
23
#include "bitstream.h"
23
#include "get_bits.h"
24 24

  
25 25
//#define DEBUG
26 26
//#define DEBUG_PACKET_CONTENTS
libavcodec/dvbsubdec.c
20 20
 */
21 21
#include "avcodec.h"
22 22
#include "dsputil.h"
23
#include "bitstream.h"
23
#include "get_bits.h"
24 24
#include "colorspace.h"
25 25

  
26 26
//#define DEBUG
libavcodec/dvdsubdec.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21
#include "avcodec.h"
22
#include "bitstream.h"
22
#include "get_bits.h"
23 23
#include "colorspace.h"
24 24
#include "dsputil.h"
25 25

  
libavcodec/eatgq.c
30 30

  
31 31
#include "avcodec.h"
32 32
#define ALT_BITSTREAM_READER_LE
33
#include "bitstream.h"
33
#include "get_bits.h"
34 34
#include "bytestream.h"
35 35
#include "dsputil.h"
36 36
#include "aandcttab.h"
libavcodec/eatgv.c
30 30

  
31 31
#include "avcodec.h"
32 32
#define ALT_BITSTREAM_READER_LE
33
#include "bitstream.h"
33
#include "get_bits.h"
34 34
#include "libavutil/lzo.h"
35 35

  
36 36
#define EA_PREAMBLE_SIZE    8
libavcodec/eatqi.c
29 29
 */
30 30

  
31 31
#include "avcodec.h"
32
#include "bitstream.h"
32
#include "get_bits.h"
33 33
#include "dsputil.h"
34 34
#include "aandcttab.h"
35 35
#include "mpeg12.h"
libavcodec/escape124.c
22 22
#include "avcodec.h"
23 23

  
24 24
#define ALT_BITSTREAM_READER_LE
25
#include "bitstream.h"
25
#include "get_bits.h"
26 26

  
27 27
typedef union MacroBlock {
28 28
    uint16_t pixels[4];
libavcodec/faxcompr.c
25 25
 * @author Konstantin Shishkov
26 26
 */
27 27
#include "avcodec.h"
28
#include "bitstream.h"
28
#include "get_bits.h"
29 29
#include "put_bits.h"
30 30
#include "faxcompr.h"
31 31

  
libavcodec/ffv1.c
26 26
 */
27 27

  
28 28
#include "avcodec.h"
29
#include "bitstream.h"
29
#include "get_bits.h"
30 30
#include "put_bits.h"
31 31
#include "dsputil.h"
32 32
#include "rangecoder.h"
libavcodec/flacdec.c
38 38
#include "libavutil/crc.h"
39 39
#include "avcodec.h"
40 40
#include "internal.h"
41
#include "bitstream.h"
41
#include "get_bits.h"
42 42
#include "bytestream.h"
43 43
#include "golomb.h"
44 44
#include "flac.h"
libavcodec/flacenc.c
23 23
#include "libavutil/lls.h"
24 24
#include "libavutil/md5.h"
25 25
#include "avcodec.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27
#include "dsputil.h"
28 28
#include "golomb.h"
29 29
#include "lpc.h"
libavcodec/flashsv.c
51 51
#include <stdlib.h>
52 52

  
53 53
#include "avcodec.h"
54
#include "bitstream.h"
54
#include "get_bits.h"
55 55

  
56 56
#include <zlib.h>
57 57

  
libavcodec/fraps.c
32 32
 */
33 33

  
34 34
#include "avcodec.h"
35
#include "bitstream.h"
35
#include "get_bits.h"
36 36
#include "huffman.h"
37 37
#include "bytestream.h"
38 38
#include "dsputil.h"
libavcodec/g726.c
23 23
 */
24 24
#include <limits.h>
25 25
#include "avcodec.h"
26
#include "bitstream.h"
26
#include "get_bits.h"
27 27
#include "put_bits.h"
28 28

  
29 29
/**
libavcodec/g729dec.c
28 28

  
29 29
#include "avcodec.h"
30 30
#include "libavutil/avutil.h"
31
#include "bitstream.h"
31
#include "get_bits.h"
32 32

  
33 33
#include "g729.h"
34 34
#include "lsp.h"
libavcodec/get_bits.h
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 libavcodec/get_bits.h
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(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
43
#   if ARCH_ARM
44
#       define A32_BITSTREAM_READER
45
#   else
46
#       define ALT_BITSTREAM_READER
47
//#define LIBMPEG2_BITSTREAM_READER
48
//#define A32_BITSTREAM_READER
49
#   endif
50
#endif
51

  
52
extern const uint8_t ff_reverse[256];
53

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

  
75
/* bit input */
76
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
77
typedef struct GetBitContext {
78
    const uint8_t *buffer, *buffer_end;
79
#ifdef ALT_BITSTREAM_READER
80
    int index;
81
#elif defined LIBMPEG2_BITSTREAM_READER
82
    uint8_t *buffer_ptr;
83
    uint32_t cache;
84
    int bit_count;
85
#elif defined A32_BITSTREAM_READER
86
    uint32_t *buffer_ptr;
87
    uint32_t cache0;
88
    uint32_t cache1;
89
    int bit_count;
90
#endif
91
    int size_in_bits;
92
} GetBitContext;
93

  
94
#define VLC_TYPE int16_t
95

  
96
typedef struct VLC {
97
    int bits;
98
    VLC_TYPE (*table)[2]; ///< code, bits
99
    int table_size, table_allocated;
100
} VLC;
101

  
102
typedef struct RL_VLC_ELEM {
103
    int16_t level;
104
    int8_t len;
105
    uint8_t run;
106
} RL_VLC_ELEM;
107

  
108
/* Bitstream reader API docs:
109
name
110
    arbitrary name which is used as prefix for the internal variables
111

  
112
gb
113
    getbitcontext
114

  
115
OPEN_READER(name, gb)
116
    loads gb into local variables
117

  
118
CLOSE_READER(name, gb)
119
    stores local vars in gb
120

  
121
UPDATE_CACHE(name, gb)
122
    refills the internal cache from the bitstream
123
    after this call at least MIN_CACHE_BITS will be available,
124

  
125
GET_CACHE(name, gb)
126
    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
127

  
128
SHOW_UBITS(name, gb, num)
129
    will return the next num bits
130

  
131
SHOW_SBITS(name, gb, num)
132
    will return the next num bits and do sign extension
133

  
134
SKIP_BITS(name, gb, num)
135
    will skip over the next num bits
136
    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
137

  
138
SKIP_CACHE(name, gb, num)
139
    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
140

  
141
SKIP_COUNTER(name, gb, num)
142
    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
143

  
144
LAST_SKIP_CACHE(name, gb, num)
145
    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
146

  
147
LAST_SKIP_BITS(name, gb, num)
148
    is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
149

  
150
for examples see get_bits, show_bits, skip_bits, get_vlc
151
*/
152

  
153
#ifdef ALT_BITSTREAM_READER
154
#   define MIN_CACHE_BITS 25
155

  
156
#   define OPEN_READER(name, gb)\
157
        int name##_index= (gb)->index;\
158
        int name##_cache= 0;\
159

  
160
#   define CLOSE_READER(name, gb)\
161
        (gb)->index= name##_index;\
162

  
163
# ifdef ALT_BITSTREAM_READER_LE
164
#   define UPDATE_CACHE(name, gb)\
165
        name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
166

  
167
#   define SKIP_CACHE(name, gb, num)\
168
        name##_cache >>= (num);
169
# else
170
#   define UPDATE_CACHE(name, gb)\
171
        name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
172

  
173
#   define SKIP_CACHE(name, gb, num)\
174
        name##_cache <<= (num);
175
# endif
176

  
177
// FIXME name?
178
#   define SKIP_COUNTER(name, gb, num)\
179
        name##_index += (num);\
180

  
181
#   define SKIP_BITS(name, gb, num)\
182
        {\
183
            SKIP_CACHE(name, gb, num)\
184
            SKIP_COUNTER(name, gb, num)\
185
        }\
186

  
187
#   define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
188
#   define LAST_SKIP_CACHE(name, gb, num) ;
189

  
190
# ifdef ALT_BITSTREAM_READER_LE
191
#   define SHOW_UBITS(name, gb, num)\
192
        ((name##_cache) & (NEG_USR32(0xffffffff,num)))
193

  
194
#   define SHOW_SBITS(name, gb, num)\
195
        NEG_SSR32((name##_cache)<<(32-(num)), num)
196
# else
197
#   define SHOW_UBITS(name, gb, num)\
198
        NEG_USR32(name##_cache, num)
199

  
200
#   define SHOW_SBITS(name, gb, num)\
201
        NEG_SSR32(name##_cache, num)
202
# endif
203

  
204
#   define GET_CACHE(name, gb)\
205
        ((uint32_t)name##_cache)
206

  
207
static inline int get_bits_count(GetBitContext *s){
208
    return s->index;
209
}
210

  
211
static inline void skip_bits_long(GetBitContext *s, int n){
212
    s->index += n;
213
}
214

  
215
#elif defined LIBMPEG2_BITSTREAM_READER
216
//libmpeg2 like reader
217

  
218
#   define MIN_CACHE_BITS 17
219

  
220
#   define OPEN_READER(name, gb)\
221
        int name##_bit_count=(gb)->bit_count;\
222
        int name##_cache= (gb)->cache;\
223
        uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
224

  
225
#   define CLOSE_READER(name, gb)\
226
        (gb)->bit_count= name##_bit_count;\
227
        (gb)->cache= name##_cache;\
228
        (gb)->buffer_ptr= name##_buffer_ptr;\
229

  
230
#   define UPDATE_CACHE(name, gb)\
231
    if(name##_bit_count >= 0){\
232
        name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
233
        name##_buffer_ptr+=2;\
234
        name##_bit_count-= 16;\
235
    }\
236

  
237
#   define SKIP_CACHE(name, gb, num)\
238
        name##_cache <<= (num);\
239

  
240
#   define SKIP_COUNTER(name, gb, num)\
241
        name##_bit_count += (num);\
242

  
243
#   define SKIP_BITS(name, gb, num)\
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff