bitstream: Avoid undefined behavior in bitstream_skip()
[libav.git] / libavcodec / bitstream.h
CommitLineData
dd3d7ddf
AH
1/*
2 * Copyright (c) 2016 Alexandra Hájková
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 * functions for reading bits from a buffer
24 */
25
26#ifndef AVCODEC_BITSTREAM_H
27#define AVCODEC_BITSTREAM_H
28
29#include <stdint.h>
30
31#include "libavutil/common.h"
32#include "libavutil/intreadwrite.h"
33#include "libavutil/log.h"
34
35#include "mathops.h"
dd3d7ddf
AH
36
37typedef 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;
43} BitstreamContext;
44
45static inline void refill_64(BitstreamContext *bc)
46{
47 if (bc->ptr >= bc->buffer_end)
48 return;
49
50#ifdef BITSTREAM_READER_LE
51 bc->bits = AV_RL64(bc->ptr);
52#else
53 bc->bits = AV_RB64(bc->ptr);
54#endif
55 bc->ptr += 8;
56 bc->bits_left = 64;
57}
58
59static inline void refill_32(BitstreamContext *bc)
60{
61 if (bc->ptr >= bc->buffer_end)
62 return;
63
64#ifdef BITSTREAM_READER_LE
65 bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
66#else
67 bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
68#endif
69 bc->ptr += 4;
70 bc->bits_left += 32;
71}
72
73/* Initialize BitstreamContext. Input buffer must have an additional zero
74 * padding of AV_INPUT_BUFFER_PADDING_SIZE bytes at the end. */
75static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,
76 unsigned bit_size)
77{
78 unsigned buffer_size;
79
80 if (bit_size > INT_MAX - 7 || !buffer) {
81 buffer =
82 bc->buffer =
83 bc->ptr = NULL;
84 bc->bits_left = 0;
85 return AVERROR_INVALIDDATA;
86 }
87
88 buffer_size = (bit_size + 7) >> 3;
89
90 bc->buffer = buffer;
91 bc->buffer_end = buffer + buffer_size;
92 bc->ptr = bc->buffer;
93 bc->size_in_bits = bit_size;
94 bc->bits_left = 0;
95 bc->bits = 0;
96
97 refill_64(bc);
98
99 return 0;
100}
101
102/* Initialize BitstreamContext with buffer size in bytes instead of bits. */
103static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
104 unsigned byte_size)
105{
106 if (byte_size > INT_MAX / 8)
107 return AVERROR_INVALIDDATA;
108 return bitstream_init(bc, buffer, byte_size * 8);
109}
110
111/* Return number of bits already read. */
112static inline int bitstream_tell(const BitstreamContext *bc)
113{
114 return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
115}
116
117/* Return buffer size in bits. */
118static inline int bitstream_tell_size(const BitstreamContext *bc)
119{
120 return bc->size_in_bits;
121}
122
123/* Return the number of the bits left in a buffer. */
124static inline int bitstream_bits_left(const BitstreamContext *bc)
125{
126 return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
127}
128
129static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
130{
131#ifdef BITSTREAM_READER_LE
132 uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
133 bc->bits >>= n;
134#else
135 uint64_t ret = bc->bits >> (64 - n);
136 bc->bits <<= n;
137#endif
138 bc->bits_left -= n;
139
140 return ret;
141}
142
143/* Return one bit from the buffer. */
144static inline unsigned bitstream_read_bit(BitstreamContext *bc)
145{
146 if (!bc->bits_left)
147 refill_64(bc);
148
149 return get_val(bc, 1);
150}
151
152/* Return n bits from the buffer. n has to be in the 0-63 range. */
153static inline uint64_t bitstream_read_63(BitstreamContext *bc, unsigned n)
154{
155 uint64_t ret = 0;
156#ifdef BITSTREAM_READER_LE
157 uint64_t left = 0;
158#endif
159
160 if (!n)
161 return 0;
162
163 if (n > bc->bits_left) {
164 n -= bc->bits_left;
165#ifdef BITSTREAM_READER_LE
166 left = bc->bits_left;
167#endif
168 ret = get_val(bc, bc->bits_left);
169 refill_64(bc);
170 }
171
172#ifdef BITSTREAM_READER_LE
173 ret = get_val(bc, n) << left | ret;
174#else
175 ret = get_val(bc, n) | ret << n;
176#endif
177
178 return ret;
179}
180
181/* Return n bits from the buffer. n has to be in the 0-32 range. */
182static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)
183{
184 if (!n)
185 return 0;
186
187 if (n > bc->bits_left) {
188 refill_32(bc);
189 if (bc->bits_left < 32)
190 bc->bits_left = n;
191 }
192
193 return get_val(bc, n);
194}
195
196/* Return n bits from the buffer as a signed integer.
197 * n has to be in the 0-32 range. */
198static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)
199{
200 return sign_extend(bitstream_read(bc, n), n);
201}
202
203static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
204{
205#ifdef BITSTREAM_READER_LE
206 return bc->bits & ((UINT64_C(1) << n) - 1);
207#else
208 return bc->bits >> (64 - n);
209#endif
210}
211
212/* Return n bits from the buffer, but do not change the buffer state.
213 * n has to be in the 0-32 range. */
214static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)
215{
216 if (n > bc->bits_left)
217 refill_32(bc);
218
219 return show_val(bc, n);
220}
221
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. */
224static inline int bitstream_peek_signed(BitstreamContext *bc, unsigned n)
225{
226 return sign_extend(bitstream_peek(bc, n), n);
227}
228
229static inline void skip_remaining(BitstreamContext *bc, unsigned n)
230{
231#ifdef BITSTREAM_READER_LE
232 bc->bits >>= n;
233#else
234 bc->bits <<= n;
235#endif
236 bc->bits_left -= n;
237}
238
239/* Skip n bits in the buffer. */
240static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
241{
79f64f7e 242 if (n < bc->bits_left)
dd3d7ddf
AH
243 skip_remaining(bc, n);
244 else {
245 n -= bc->bits_left;
79f64f7e
LB
246 bc->bits = 0;
247 bc->bits_left = 0;
248
dd3d7ddf
AH
249 if (n >= 64) {
250 unsigned skip = n / 8;
251
252 n -= skip * 8;
253 bc->ptr += skip;
254 }
255 refill_64(bc);
256 if (n)
257 skip_remaining(bc, n);
258 }
259}
260
261/* Seek to the given bit position. */
262static inline void bitstream_seek(BitstreamContext *bc, unsigned pos)
263{
264 bc->ptr = bc->buffer;
265 bc->bits = 0;
266 bc->bits_left = 0;
267
268 bitstream_skip(bc, pos);
269}
270
271/* Skip bits to a byte boundary. */
272static inline const uint8_t *bitstream_align(BitstreamContext *bc)
273{
274 unsigned n = -bitstream_tell(bc) & 7;
275 if (n)
276 bitstream_skip(bc, n);
277 return bc->buffer + (bitstream_tell(bc) >> 3);
278}
279
280/* Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
281 * If MSB not set it is negative. */
282static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length)
283{
284 int32_t cache = bitstream_peek(bc, 32);
285 int sign = ~cache >> 31;
286 skip_remaining(bc, length);
287
288 return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;
289}
290
dd3d7ddf
AH
291/* Return decoded truncated unary code for the values 0, 1, 2. */
292static inline int bitstream_decode012(BitstreamContext *bc)
293{
294 if (!bitstream_read_bit(bc))
295 return 0;
296 else
297 return bitstream_read_bit(bc) + 1;
298}
299
300/* Return decoded truncated unary code for the values 2, 1, 0. */
301static inline int bitstream_decode210(BitstreamContext *bc)
302{
303 if (bitstream_read_bit(bc))
304 return 0;
305 else
306 return 2 - bitstream_read_bit(bc);
307}
308
309/* Read sign bit and flip the sign of the provided value accordingly. */
310static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
311{
312 int sign = bitstream_read_signed(bc, 1);
313 return (val ^ sign) - sign;
314}
315
9c2d36fc
LB
316/* Unwind the cache so a refill_32 can fill it again. */
317static inline void bitstream_unwind(BitstreamContext *bc)
318{
319 int unwind = 4;
320 int unwind_bits = unwind * 8;
321
322 if (bc->bits_left < unwind_bits)
323 return;
324
325 bc->bits >>= unwind_bits;
326 bc->bits <<= unwind_bits;
327 bc->bits_left -= unwind_bits;
328 bc->ptr -= unwind;
329}
330
331/* Unget up to 32 bits. */
332static inline void bitstream_unget(BitstreamContext *bc, uint64_t value,
333 size_t amount)
334{
335 size_t cache_size = sizeof(bc->bits) * 8;
336
337 if (bc->bits_left + amount > cache_size)
338 bitstream_unwind(bc);
339
340 bc->bits = (bc->bits >> amount) | (value << (cache_size - amount));
341 bc->bits_left += amount;
342}
343
dd3d7ddf 344#endif /* AVCODEC_BITSTREAM_H */