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"
37 typedef struct BitstreamContext {
38 uint64_t bits; // stores bits read from the buffer
39 const uint8_t *buffer, *buffer_end;
40 const uint8_t *ptr; // position inside a buffer
41 unsigned bits_left; // number of bits left in bits field
42 unsigned size_in_bits;
45 static inline void refill_64(BitstreamContext *bc)
47 if (bc->ptr >= bc->buffer_end)
50 #ifdef BITSTREAM_READER_LE
51 bc->bits = AV_RL64(bc->ptr);
53 bc->bits = AV_RB64(bc->ptr);
59 static inline void refill_32(BitstreamContext *bc)
61 if (bc->ptr >= bc->buffer_end)
64 #ifdef BITSTREAM_READER_LE
65 bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
67 bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
73 /* Initialize BitstreamContext. Input buffer must have an additional zero
74 * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */
75 static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,
80 if (bit_size > INT_MAX - 7 || !buffer) {
85 return AVERROR_INVALIDDATA;
88 buffer_size = (bit_size + 7) >> 3;
91 bc->buffer_end = buffer + buffer_size;
93 bc->size_in_bits = bit_size;
102 /* Initialize BitstreamContext with buffer size in bytes instead of bits. */
103 static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
106 if (byte_size > INT_MAX / 8)
107 return AVERROR_INVALIDDATA;
108 return bitstream_init(bc, buffer, byte_size * 8);
111 /* Return number of bits already read. */
112 static inline int bitstream_tell(const BitstreamContext *bc)
114 return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
117 /* Return buffer size in bits. */
118 static inline int bitstream_tell_size(const BitstreamContext *bc)
120 return bc->size_in_bits;
123 /* Return the number of the bits left in a buffer. */
124 static inline int bitstream_bits_left(const BitstreamContext *bc)
126 return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
129 static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
131 #ifdef BITSTREAM_READER_LE
132 uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
135 uint64_t ret = bc->bits >> (64 - n);
143 /* Return one bit from the buffer. */
144 static inline unsigned bitstream_read_bit(BitstreamContext *bc)
149 return get_val(bc, 1);
152 /* Return n bits from the buffer. n has to be in the 0-63 range. */
153 static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n)
156 #ifdef BITSTREAM_READER_LE
163 if (n > bc->bits_left) {
165 #ifdef BITSTREAM_READER_LE
166 left = bc->bits_left;
168 ret = get_val(bc, bc->bits_left);
172 #ifdef BITSTREAM_READER_LE
173 ret = get_val(bc, n) << left | ret;
175 ret = get_val(bc, n) | ret << n;
181 /* Return n bits from the buffer. n has to be in the 0-32 range. */
182 static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)
187 if (n > bc->bits_left) {
189 if (bc->bits_left < 32)
193 return get_val(bc, n);
196 /* Return n bits from the buffer as a signed integer.
197 * n has to be in the 0-32 range. */
198 static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)
200 return sign_extend(bitstream_read(bc, n), n);
203 static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
205 #ifdef BITSTREAM_READER_LE
206 return bc->bits & ((UINT64_C(1) << n) - 1);
208 return bc->bits >> (64 - n);
212 /* Return n bits from the buffer, but do not change the buffer state.
213 * n has to be in the 0-32 range. */
214 static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)
216 if (n > bc->bits_left)
219 return show_val(bc, n);
222 /* Return n bits from the buffer as a signed integer, but do not change the
223 * buffer state. n has to be in the 0-32 range. */
224 static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n)
226 return sign_extend(bitstream_peek(bc, n), n);
229 static inline void skip_remaining(BitstreamContext *bc, unsigned n)
231 #ifdef BITSTREAM_READER_LE
239 /* Skip n bits in the buffer. */
240 static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
242 if (n < bc->bits_left)
243 skip_remaining(bc, n);
250 unsigned skip = n / 8;
257 skip_remaining(bc, n);
261 /* Seek to the given bit position. */
262 static inline void bitstream_seek(BitstreamContext *bc, unsigned pos)
264 bc->ptr = bc->buffer;
268 bitstream_skip(bc, pos);
271 /* Skip bits to a byte boundary. */
272 static inline const uint8_t *bitstream_align(BitstreamContext *bc)
274 unsigned n = -bitstream_tell(bc) & 7;
276 bitstream_skip(bc, n);
277 return bc->buffer + (bitstream_tell(bc) >> 3);
280 /* Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
281 * If MSB not set it is negative. */
282 static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length)
284 int32_t cache = bitstream_peek(bc, 32);
285 int sign = ~cache >> 31;
286 skip_remaining(bc, length);
288 return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;
291 /* Return decoded truncated unary code for the values 0, 1, 2. */
292 static inline int bitstream_decode012(BitstreamContext *bc)
294 if (!bitstream_read_bit(bc))
297 return bitstream_read_bit(bc) + 1;
300 /* Return decoded truncated unary code for the values 2, 1, 0. */
301 static inline int bitstream_decode210(BitstreamContext *bc)
303 if (bitstream_read_bit(bc))
306 return 2 - bitstream_read_bit(bc);
309 /* Read sign bit and flip the sign of the provided value accordingly. */
310 static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
312 int sign = bitstream_read_signed(bc, 1);
313 return (val ^ sign) - sign;
316 /* Unwind the cache so a refill_32 can fill it again. */
317 static inline void bitstream_unwind(BitstreamContext *bc)
320 int unwind_bits = unwind * 8;
322 if (bc->bits_left < unwind_bits)
325 bc->bits >>= unwind_bits;
326 bc->bits <<= unwind_bits;
327 bc->bits_left -= unwind_bits;
331 /* Unget up to 32 bits. */
332 static inline void bitstream_unget(BitstreamContext *bc, uint64_t value,
335 size_t cache_size = sizeof(bc->bits) * 8;
337 if (bc->bits_left + amount > cache_size)
338 bitstream_unwind(bc);
340 bc->bits = (bc->bits >> amount) | (value << (cache_size - amount));
341 bc->bits_left += amount;
344 #endif /* AVCODEC_BITSTREAM_H */