2 * Copyright (c) 2016 Alexandra Hájková
4 * This file is part of Libav.
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.
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.
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
23 * functions for reading bits from a buffer
26 #ifndef AVCODEC_BITSTREAM_H
27 #define AVCODEC_BITSTREAM_H
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/log.h"
38 typedef struct BitstreamContext {
39 uint64_t bits; // stores bits read from the buffer
40 const uint8_t *buffer, *buffer_end;
41 const uint8_t *ptr; // position inside a buffer
42 unsigned bits_left; // number of bits left in bits field
43 unsigned size_in_bits;
46 static inline void refill_64(BitstreamContext *bc)
48 if (bc->ptr >= bc->buffer_end)
51 #ifdef BITSTREAM_READER_LE
52 bc->bits = AV_RL64(bc->ptr);
54 bc->bits = AV_RB64(bc->ptr);
60 static inline void refill_32(BitstreamContext *bc)
62 if (bc->ptr >= bc->buffer_end)
65 #ifdef BITSTREAM_READER_LE
66 bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
68 bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
74 /* Initialize BitstreamContext. Input buffer must have an additional zero
75 * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */
76 static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,
81 if (bit_size > INT_MAX - 7 || !buffer) {
86 return AVERROR_INVALIDDATA;
89 buffer_size = (bit_size + 7) >> 3;
92 bc->buffer_end = buffer + buffer_size;
94 bc->size_in_bits = bit_size;
103 /* Initialize BitstreamContext with buffer size in bytes instead of bits. */
104 static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
107 if (byte_size > INT_MAX / 8)
108 return AVERROR_INVALIDDATA;
109 return bitstream_init(bc, buffer, byte_size * 8);
112 /* Return number of bits already read. */
113 static inline int bitstream_tell(const BitstreamContext *bc)
115 return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
118 /* Return buffer size in bits. */
119 static inline int bitstream_tell_size(const BitstreamContext *bc)
121 return bc->size_in_bits;
124 /* Return the number of the bits left in a buffer. */
125 static inline int bitstream_bits_left(const BitstreamContext *bc)
127 return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
130 static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
132 #ifdef BITSTREAM_READER_LE
133 uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
136 uint64_t ret = bc->bits >> (64 - n);
144 /* Return one bit from the buffer. */
145 static inline unsigned bitstream_read_bit(BitstreamContext *bc)
150 return get_val(bc, 1);
153 /* Return n bits from the buffer. n has to be in the 0-63 range. */
154 static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n)
157 #ifdef BITSTREAM_READER_LE
164 if (n > bc->bits_left) {
166 #ifdef BITSTREAM_READER_LE
167 left = bc->bits_left;
169 ret = get_val(bc, bc->bits_left);
173 #ifdef BITSTREAM_READER_LE
174 ret = get_val(bc, n) << left | ret;
176 ret = get_val(bc, n) | ret << n;
182 /* Return n bits from the buffer. n has to be in the 0-32 range. */
183 static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)
188 if (n > bc->bits_left) {
190 if (bc->bits_left < 32)
194 return get_val(bc, n);
197 /* Return n bits from the buffer as a signed integer.
198 * n has to be in the 0-32 range. */
199 static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)
201 return sign_extend(bitstream_read(bc, n), n);
204 static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
206 #ifdef BITSTREAM_READER_LE
207 return bc->bits & ((UINT64_C(1) << n) - 1);
209 return bc->bits >> (64 - n);
213 /* Return n bits from the buffer, but do not change the buffer state.
214 * n has to be in the 0-32 range. */
215 static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)
217 if (n > bc->bits_left)
220 return show_val(bc, n);
223 /* Return n bits from the buffer as a signed integer, but do not change the
224 * buffer state. n has to be in the 0-32 range. */
225 static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n)
227 return sign_extend(bitstream_peek(bc, n), n);
230 static inline void skip_remaining(BitstreamContext *bc, unsigned n)
232 #ifdef BITSTREAM_READER_LE
240 /* Skip n bits in the buffer. */
241 static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
243 if (n <= bc->bits_left)
244 skip_remaining(bc, n);
247 skip_remaining(bc, bc->bits_left);
249 unsigned skip = n / 8;
256 skip_remaining(bc, n);
260 /* Seek to the given bit position. */
261 static inline void bitstream_seek(BitstreamContext *bc, unsigned pos)
263 bc->ptr = bc->buffer;
267 bitstream_skip(bc, pos);
270 /* Skip bits to a byte boundary. */
271 static inline const uint8_t *bitstream_align(BitstreamContext *bc)
273 unsigned n = -bitstream_tell(bc) & 7;
275 bitstream_skip(bc, n);
276 return bc->buffer + (bitstream_tell(bc) >> 3);
279 /* Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
280 * If MSB not set it is negative. */
281 static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length)
283 int32_t cache = bitstream_peek(bc, 32);
284 int sign = ~cache >> 31;
285 skip_remaining(bc, length);
287 return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;
290 /* Return the LUT element for the given bitstream configuration. */
291 static inline int set_idx(BitstreamContext *bc, int code, int *n, int *nb_bits,
292 VLC_TYPE (*table)[2])
297 idx = bitstream_peek(bc, *nb_bits) + code;
300 return table[idx][0];
305 * @param bits is the number of bits which will be read at once, must be
306 * identical to nb_bits in init_vlc()
307 * @param max_depth is the number of times bits bits must be read to completely
308 * read the longest VLC code
309 * = (max_vlc_length + bits - 1) / bits
310 * If the VLC code is invalid and max_depth = 1, then no bits will be removed.
311 * If the VLC code is invalid and max_depth > 1, then the number of bits removed
313 static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],
314 int bits, int max_depth)
317 unsigned idx = bitstream_peek(bc, bits);
318 int code = table[idx][0];
319 int n = table[idx][1];
321 if (max_depth > 1 && n < 0) {
322 skip_remaining(bc, bits);
323 code = set_idx(bc, code, &n, &nb_bits, table);
324 if (max_depth > 2 && n < 0) {
325 skip_remaining(bc, nb_bits);
326 code = set_idx(bc, code, &n, &nb_bits, table);
329 skip_remaining(bc, n);
334 #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
337 unsigned index = bitstream_peek(bc, bits); \
338 level = table[index].level; \
339 n = table[index].len; \
341 if (max_depth > 1 && n < 0) { \
342 bitstream_skip(bc, bits); \
346 index = bitstream_peek(bc, nb_bits) + level; \
347 level = table[index].level; \
348 n = table[index].len; \
349 if (max_depth > 2 && n < 0) { \
350 bitstream_skip(bc, nb_bits); \
353 index = bitstream_peek(bc, nb_bits) + level; \
354 level = table[index].level; \
355 n = table[index].len; \
358 run = table[index].run; \
359 bitstream_skip(bc, n); \
362 /* Return decoded truncated unary code for the values 0, 1, 2. */
363 static inline int bitstream_decode012(BitstreamContext *bc)
365 if (!bitstream_read_bit(bc))
368 return bitstream_read_bit(bc) + 1;
371 /* Return decoded truncated unary code for the values 2, 1, 0. */
372 static inline int bitstream_decode210(BitstreamContext *bc)
374 if (bitstream_read_bit(bc))
377 return 2 - bitstream_read_bit(bc);
380 /* Read sign bit and flip the sign of the provided value accordingly. */
381 static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
383 int sign = bitstream_read_signed(bc, 1);
384 return (val ^ sign) - sign;
387 #endif /* AVCODEC_BITSTREAM_H */