]> git.sesse.net Git - ffmpeg/blob - libavcodec/bitstream.h
vp9: split superframes in the filtering stage before actual decoding
[ffmpeg] / libavcodec / bitstream.h
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"
36 #include "vlc.h"
37
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;
44 } BitstreamContext;
45
46 static inline void refill_64(BitstreamContext *bc)
47 {
48     if (bc->ptr >= bc->buffer_end)
49         return;
50
51 #ifdef BITSTREAM_READER_LE
52     bc->bits       = AV_RL64(bc->ptr);
53 #else
54     bc->bits       = AV_RB64(bc->ptr);
55 #endif
56     bc->ptr       += 8;
57     bc->bits_left  = 64;
58 }
59
60 static inline void refill_32(BitstreamContext *bc)
61 {
62     if (bc->ptr >= bc->buffer_end)
63         return;
64
65 #ifdef BITSTREAM_READER_LE
66     bc->bits       = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
67 #else
68     bc->bits       = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
69 #endif
70     bc->ptr       += 4;
71     bc->bits_left += 32;
72 }
73
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,
77                                  unsigned bit_size)
78 {
79     unsigned buffer_size;
80
81     if (bit_size > INT_MAX - 7 || !buffer) {
82         buffer        =
83         bc->buffer    =
84         bc->ptr       = NULL;
85         bc->bits_left = 0;
86         return AVERROR_INVALIDDATA;
87     }
88
89     buffer_size = (bit_size + 7) >> 3;
90
91     bc->buffer       = buffer;
92     bc->buffer_end   = buffer + buffer_size;
93     bc->ptr          = bc->buffer;
94     bc->size_in_bits = bit_size;
95     bc->bits_left    = 0;
96     bc->bits         = 0;
97
98     refill_64(bc);
99
100     return 0;
101 }
102
103 /* Initialize BitstreamContext with buffer size in bytes instead of bits. */
104 static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,
105                                   unsigned byte_size)
106 {
107     if (byte_size > INT_MAX / 8)
108         return AVERROR_INVALIDDATA;
109     return bitstream_init(bc, buffer, byte_size * 8);
110 }
111
112 /* Return number of bits already read. */
113 static inline int bitstream_tell(const BitstreamContext *bc)
114 {
115     return (bc->ptr - bc->buffer) * 8 - bc->bits_left;
116 }
117
118 /* Return buffer size in bits. */
119 static inline int bitstream_tell_size(const BitstreamContext *bc)
120 {
121     return bc->size_in_bits;
122 }
123
124 /* Return the number of the bits left in a buffer. */
125 static inline int bitstream_bits_left(const BitstreamContext *bc)
126 {
127     return (bc->buffer - bc->ptr) * 8 + bc->size_in_bits + bc->bits_left;
128 }
129
130 static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
131 {
132 #ifdef BITSTREAM_READER_LE
133     uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
134     bc->bits >>= n;
135 #else
136     uint64_t ret = bc->bits >> (64 - n);
137     bc->bits <<= n;
138 #endif
139     bc->bits_left -= n;
140
141     return ret;
142 }
143
144 /* Return one bit from the buffer. */
145 static inline unsigned bitstream_read_bit(BitstreamContext *bc)
146 {
147     if (!bc->bits_left)
148         refill_64(bc);
149
150     return get_val(bc, 1);
151 }
152
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)
155 {
156     uint64_t ret = 0;
157 #ifdef BITSTREAM_READER_LE
158     uint64_t left = 0;
159 #endif
160
161     if (!n)
162         return 0;
163
164     if (n > bc->bits_left) {
165         n -= bc->bits_left;
166 #ifdef BITSTREAM_READER_LE
167         left = bc->bits_left;
168 #endif
169         ret = get_val(bc, bc->bits_left);
170         refill_64(bc);
171     }
172
173 #ifdef BITSTREAM_READER_LE
174     ret = get_val(bc, n) << left | ret;
175 #else
176     ret = get_val(bc, n) | ret << n;
177 #endif
178
179     return ret;
180 }
181
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)
184 {
185     if (!n)
186         return 0;
187
188     if (n > bc->bits_left) {
189         refill_32(bc);
190         if (bc->bits_left < 32)
191             bc->bits_left = n;
192     }
193
194     return get_val(bc, n);
195 }
196
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)
200 {
201     return sign_extend(bitstream_read(bc, n), n);
202 }
203
204 static inline unsigned show_val(const BitstreamContext *bc, unsigned n)
205 {
206 #ifdef BITSTREAM_READER_LE
207     return bc->bits & ((UINT64_C(1) << n) - 1);
208 #else
209     return bc->bits >> (64 - n);
210 #endif
211 }
212
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)
216 {
217     if (n > bc->bits_left)
218         refill_32(bc);
219
220     return show_val(bc, n);
221 }
222
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)
226 {
227     return sign_extend(bitstream_peek(bc, n), n);
228 }
229
230 static inline void skip_remaining(BitstreamContext *bc, unsigned n)
231 {
232 #ifdef BITSTREAM_READER_LE
233     bc->bits >>= n;
234 #else
235     bc->bits <<= n;
236 #endif
237     bc->bits_left -= n;
238 }
239
240 /* Skip n bits in the buffer. */
241 static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
242 {
243     if (n <= bc->bits_left)
244         skip_remaining(bc, n);
245     else {
246         n -= bc->bits_left;
247         skip_remaining(bc, bc->bits_left);
248         if (n >= 64) {
249             unsigned skip = n / 8;
250
251             n -= skip * 8;
252             bc->ptr += skip;
253         }
254         refill_64(bc);
255         if (n)
256             skip_remaining(bc, n);
257     }
258 }
259
260 /* Seek to the given bit position. */
261 static inline void bitstream_seek(BitstreamContext *bc, unsigned pos)
262 {
263     bc->ptr       = bc->buffer;
264     bc->bits      = 0;
265     bc->bits_left = 0;
266
267     bitstream_skip(bc, pos);
268 }
269
270 /* Skip bits to a byte boundary. */
271 static inline const uint8_t *bitstream_align(BitstreamContext *bc)
272 {
273     unsigned n = -bitstream_tell(bc) & 7;
274     if (n)
275         bitstream_skip(bc, n);
276     return bc->buffer + (bitstream_tell(bc) >> 3);
277 }
278
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)
282 {
283     int32_t cache = bitstream_peek(bc, 32);
284     int sign = ~cache >> 31;
285     skip_remaining(bc, length);
286
287     return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;
288 }
289
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])
293 {
294     unsigned idx;
295
296     *nb_bits = -*n;
297     idx = bitstream_peek(bc, *nb_bits) + code;
298     *n = table[idx][1];
299
300     return table[idx][0];
301 }
302
303 /**
304  * Parse a VLC code.
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
312  * is undefined. */
313 static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],
314                                      int bits, int max_depth)
315 {
316     int nb_bits;
317     unsigned idx = bitstream_peek(bc, bits);
318     int code = table[idx][0];
319     int n    = table[idx][1];
320
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);
327         }
328     }
329     skip_remaining(bc, n);
330
331     return code;
332 }
333
334 #define BITSTREAM_RL_VLC(level, run, bc, table, bits, max_depth) \
335     do {                                                         \
336         int n, nb_bits;                                          \
337         unsigned index = bitstream_peek(bc, bits);               \
338         level = table[index].level;                              \
339         n     = table[index].len;                                \
340                                                                  \
341         if (max_depth > 1 && n < 0) {                            \
342             bitstream_skip(bc, bits);                            \
343                                                                  \
344             nb_bits = -n;                                        \
345                                                                  \
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);                     \
351                 nb_bits = -n;                                    \
352                                                                  \
353                 index = bitstream_peek(bc, nb_bits) + level;     \
354                 level = table[index].level;                      \
355                 n     = table[index].len;                        \
356             }                                                    \
357         }                                                        \
358         run = table[index].run;                                  \
359         bitstream_skip(bc, n);                                   \
360     } while (0)
361
362 /* Return decoded truncated unary code for the values 0, 1, 2. */
363 static inline int bitstream_decode012(BitstreamContext *bc)
364 {
365     if (!bitstream_read_bit(bc))
366         return 0;
367     else
368         return bitstream_read_bit(bc) + 1;
369 }
370
371 /* Return decoded truncated unary code for the values 2, 1, 0. */
372 static inline int bitstream_decode210(BitstreamContext *bc)
373 {
374     if (bitstream_read_bit(bc))
375         return 0;
376     else
377         return 2 - bitstream_read_bit(bc);
378 }
379
380 /* Read sign bit and flip the sign of the provided value accordingly. */
381 static inline int bitstream_apply_sign(BitstreamContext *bc, int val)
382 {
383     int sign = bitstream_read_signed(bc, 1);
384     return (val ^ sign) - sign;
385 }
386
387 #endif /* AVCODEC_BITSTREAM_H */