d13ef4c2bd6946deff17e9104432052b1319b25e
2 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.h.
4 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
6 * Licensed under the GNU Lesser General Public License.
7 * For licencing details see COPYING.LIB.
10 /** \file bitstream.h Bitstream structures and inline functions. */
12 #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
13 (((const uint8_t*)(x))[1] << 16) | \
14 (((const uint8_t*)(x))[2] << 8) | \
15 ((const uint8_t*)(x))[3])
17 /** Structure for bistream I/O. */
18 struct getbit_context
{
19 /* Start of the internal buffer. */
20 const uint8_t *buffer
;
21 /* End of the internal buffer. */
22 const uint8_t *buffer_end
;
27 #define VLC_TYPE int16_t
31 VLC_TYPE(*table
)[2]; ///< code, bits
32 int table_size
, table_allocated
;
35 static inline uint32_t show_bits(struct getbit_context
*gbc
, int num
)
38 uint32_t x
= AV_RB32(gbc
->buffer
+ (idx
>> 3)) << (idx
& 7);
39 return x
>> (32 - num
);
42 static inline int get_bits_count(struct getbit_context
*gbc
)
47 static inline void skip_bits(struct getbit_context
*gbc
, int n
)
52 static inline unsigned int get_bits(struct getbit_context
*gbc
, int n
)
54 unsigned int ret
= show_bits(gbc
, n
);
59 /* This is rather hot, we can do better than get_bits(gbc, 1). */
60 static inline unsigned int get_bits1(struct getbit_context
*gbc
)
62 int idx
= gbc
->index
++;
63 uint8_t tmp
= gbc
->buffer
[idx
>> 3], mask
= (1 << (7 - (idx
& 7)));
64 return !!(tmp
& mask
);
68 * Initialize a getbit_context structure.
70 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
71 * bytes larger then the actual read bits * because some optimized bitstream
72 * readers read 32 or 64 bit at once and could read over the end.
74 * \param bit_size The size of the buffer in bytes.
76 static inline void init_get_bits(struct getbit_context
*gbc
,
77 const uint8_t *buffer
, int size
)
80 gbc
->buffer_end
= buffer
+ size
;
84 void init_vlc(struct vlc
*vlc
, int nb_bits
, int nb_codes
, const void *bits
,
85 const void *codes
, int codes_size
);
87 void free_vlc(struct vlc
*vlc
);
92 * \param bits The number of bits which will be read at once, must be
93 * identical to nb_bits in init_vlc()
95 * \param max_depth The number of times bits bits must be read to completely
96 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
98 static inline int get_vlc(struct getbit_context
*gbc
, VLC_TYPE(*table
)[2],
99 int bits
, int max_depth
)
101 int n
, idx
, nb_bits
, code
;
103 idx
= show_bits(gbc
, bits
);
104 code
= table
[idx
][0];
106 if (max_depth
> 1 && n
< 0) {
107 skip_bits(gbc
, bits
);
109 idx
= show_bits(gbc
, nb_bits
) + code
;
110 code
= table
[idx
][0];
112 if (max_depth
> 2 && n
< 0) {
113 skip_bits(gbc
, nb_bits
);
115 idx
= show_bits(gbc
, nb_bits
) + code
;
116 code
= table
[idx
][0];