]> git.sesse.net Git - ffmpeg/blob - libavcodec/get_bits.h
vda: cosmetic.
[ffmpeg] / libavcodec / get_bits.h
1 /*
2  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  * bitstream reader API header.
24  */
25
26 #ifndef AVCODEC_GET_BITS_H
27 #define AVCODEC_GET_BITS_H
28
29 #include <stdint.h>
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/log.h"
33 #include "mathops.h"
34
35 /*
36  * Safe bitstream reading:
37  * optionally, the get_bits API can check to ensure that we
38  * don't read past input buffer boundaries. This is protected
39  * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
40  * then below that with UNCHECKED_BITSTREAM_READER at the per-
41  * decoder level. This means that decoders that check internally
42  * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
43  * overread checks.
44  * Boundary checking causes a minor performance penalty so for
45  * applications that won't want/need this, it can be disabled
46  * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
47  */
48 #ifndef UNCHECKED_BITSTREAM_READER
49 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
50 #endif
51
52 typedef struct GetBitContext {
53     const uint8_t *buffer, *buffer_end;
54     int index;
55     int size_in_bits;
56     int size_in_bits_plus8;
57 } GetBitContext;
58
59 #define VLC_TYPE int16_t
60
61 typedef struct VLC {
62     int bits;
63     VLC_TYPE (*table)[2]; ///< code, bits
64     int table_size, table_allocated;
65 } VLC;
66
67 typedef struct RL_VLC_ELEM {
68     int16_t level;
69     int8_t len;
70     uint8_t run;
71 } RL_VLC_ELEM;
72
73 /* Bitstream reader API docs:
74 name
75     arbitrary name which is used as prefix for the internal variables
76
77 gb
78     getbitcontext
79
80 OPEN_READER(name, gb)
81     load gb into local variables
82
83 CLOSE_READER(name, gb)
84     store local vars in gb
85
86 UPDATE_CACHE(name, gb)
87     refill the internal cache from the bitstream
88     after this call at least MIN_CACHE_BITS will be available,
89
90 GET_CACHE(name, gb)
91     will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
92
93 SHOW_UBITS(name, gb, num)
94     will return the next num bits
95
96 SHOW_SBITS(name, gb, num)
97     will return the next num bits and do sign extension
98
99 SKIP_BITS(name, gb, num)
100     will skip over the next num bits
101     note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
102
103 SKIP_CACHE(name, gb, num)
104     will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
105
106 SKIP_COUNTER(name, gb, num)
107     will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
108
109 LAST_SKIP_BITS(name, gb, num)
110     like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER
111
112 for examples see get_bits, show_bits, skip_bits, get_vlc
113 */
114
115 #ifdef LONG_BITSTREAM_READER
116 #   define MIN_CACHE_BITS 32
117 #else
118 #   define MIN_CACHE_BITS 25
119 #endif
120
121 #define OPEN_READER(name, gb)                   \
122     unsigned int name##_index = (gb)->index;    \
123     av_unused unsigned int name##_cache
124
125 #define CLOSE_READER(name, gb) (gb)->index = name##_index
126
127 #ifdef BITSTREAM_READER_LE
128
129 # ifdef LONG_BITSTREAM_READER
130 #   define UPDATE_CACHE(name, gb) name##_cache = \
131         AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
132 # else
133 #   define UPDATE_CACHE(name, gb) name##_cache = \
134         AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
135 # endif
136
137 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
138
139 #else
140
141 # ifdef LONG_BITSTREAM_READER
142 #   define UPDATE_CACHE(name, gb) name##_cache = \
143         AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
144 # else
145 #   define UPDATE_CACHE(name, gb) name##_cache = \
146         AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
147 # endif
148
149 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
150
151 #endif
152
153 #if UNCHECKED_BITSTREAM_READER
154 #   define SKIP_COUNTER(name, gb, num) name##_index += (num)
155 #else
156 #   define SKIP_COUNTER(name, gb, num) \
157     name##_index = FFMIN((gb)->size_in_bits_plus8, name##_index + (num))
158 #endif
159
160 #define SKIP_BITS(name, gb, num) do {           \
161         SKIP_CACHE(name, gb, num);              \
162         SKIP_COUNTER(name, gb, num);            \
163     } while (0)
164
165 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
166
167 #ifdef BITSTREAM_READER_LE
168 #   define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
169 #   define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
170 #else
171 #   define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
172 #   define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
173 #endif
174
175 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
176
177 static inline int get_bits_count(const GetBitContext *s)
178 {
179     return s->index;
180 }
181
182 static inline void skip_bits_long(GetBitContext *s, int n){
183 #if UNCHECKED_BITSTREAM_READER
184     s->index += n;
185 #else
186     s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
187 #endif
188 }
189
190 /**
191  * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
192  * if MSB not set it is negative
193  * @param n length in bits
194  */
195 static inline int get_xbits(GetBitContext *s, int n)
196 {
197     register int sign;
198     register int32_t cache;
199     OPEN_READER(re, s);
200     UPDATE_CACHE(re, s);
201     cache = GET_CACHE(re, s);
202     sign = ~cache >> 31;
203     LAST_SKIP_BITS(re, s, n);
204     CLOSE_READER(re, s);
205     return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
206 }
207
208 static inline int get_sbits(GetBitContext *s, int n)
209 {
210     register int tmp;
211     OPEN_READER(re, s);
212     UPDATE_CACHE(re, s);
213     tmp = SHOW_SBITS(re, s, n);
214     LAST_SKIP_BITS(re, s, n);
215     CLOSE_READER(re, s);
216     return tmp;
217 }
218
219 /**
220  * Read 1-25 bits.
221  */
222 static inline unsigned int get_bits(GetBitContext *s, int n)
223 {
224     register int tmp;
225     OPEN_READER(re, s);
226     UPDATE_CACHE(re, s);
227     tmp = SHOW_UBITS(re, s, n);
228     LAST_SKIP_BITS(re, s, n);
229     CLOSE_READER(re, s);
230     return tmp;
231 }
232
233 /**
234  * Show 1-25 bits.
235  */
236 static inline unsigned int show_bits(GetBitContext *s, int n)
237 {
238     register int tmp;
239     OPEN_READER(re, s);
240     UPDATE_CACHE(re, s);
241     tmp = SHOW_UBITS(re, s, n);
242     return tmp;
243 }
244
245 static inline void skip_bits(GetBitContext *s, int n)
246 {
247     OPEN_READER(re, s);
248     UPDATE_CACHE(re, s);
249     LAST_SKIP_BITS(re, s, n);
250     CLOSE_READER(re, s);
251 }
252
253 static inline unsigned int get_bits1(GetBitContext *s)
254 {
255     unsigned int index = s->index;
256     uint8_t result = s->buffer[index>>3];
257 #ifdef BITSTREAM_READER_LE
258     result >>= index & 7;
259     result &= 1;
260 #else
261     result <<= index & 7;
262     result >>= 8 - 1;
263 #endif
264 #if !UNCHECKED_BITSTREAM_READER
265     if (s->index < s->size_in_bits_plus8)
266 #endif
267         index++;
268     s->index = index;
269
270     return result;
271 }
272
273 static inline unsigned int show_bits1(GetBitContext *s)
274 {
275     return show_bits(s, 1);
276 }
277
278 static inline void skip_bits1(GetBitContext *s)
279 {
280     skip_bits(s, 1);
281 }
282
283 /**
284  * Read 0-32 bits.
285  */
286 static inline unsigned int get_bits_long(GetBitContext *s, int n)
287 {
288     if (n <= MIN_CACHE_BITS)
289         return get_bits(s, n);
290     else {
291 #ifdef BITSTREAM_READER_LE
292         int ret = get_bits(s, 16);
293         return ret | (get_bits(s, n-16) << 16);
294 #else
295         int ret = get_bits(s, 16) << (n-16);
296         return ret | get_bits(s, n-16);
297 #endif
298     }
299 }
300
301 /**
302  * Read 0-32 bits as a signed integer.
303  */
304 static inline int get_sbits_long(GetBitContext *s, int n)
305 {
306     return sign_extend(get_bits_long(s, n), n);
307 }
308
309 /**
310  * Show 0-32 bits.
311  */
312 static inline unsigned int show_bits_long(GetBitContext *s, int n)
313 {
314     if (n <= MIN_CACHE_BITS)
315         return show_bits(s, n);
316     else {
317         GetBitContext gb = *s;
318         return get_bits_long(&gb, n);
319     }
320 }
321
322 static inline int check_marker(GetBitContext *s, const char *msg)
323 {
324     int bit = get_bits1(s);
325     if (!bit)
326         av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
327
328     return bit;
329 }
330
331 /**
332  * Inititalize GetBitContext.
333  * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
334  * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
335  * @param bit_size the size of the buffer in bits
336  */
337 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
338                                  int bit_size)
339 {
340     int buffer_size = (bit_size+7)>>3;
341     if (buffer_size < 0 || bit_size < 0) {
342         buffer_size = bit_size = 0;
343         buffer = NULL;
344     }
345
346     s->buffer       = buffer;
347     s->size_in_bits = bit_size;
348     s->size_in_bits_plus8 = bit_size + 8;
349     s->buffer_end   = buffer + buffer_size;
350     s->index        = 0;
351 }
352
353 static inline void align_get_bits(GetBitContext *s)
354 {
355     int n = -get_bits_count(s) & 7;
356     if (n) skip_bits(s, n);
357 }
358
359 #define init_vlc(vlc, nb_bits, nb_codes,                \
360                  bits, bits_wrap, bits_size,            \
361                  codes, codes_wrap, codes_size,         \
362                  flags)                                 \
363         init_vlc_sparse(vlc, nb_bits, nb_codes,         \
364                         bits, bits_wrap, bits_size,     \
365                         codes, codes_wrap, codes_size,  \
366                         NULL, 0, 0, flags)
367
368 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
369              const void *bits, int bits_wrap, int bits_size,
370              const void *codes, int codes_wrap, int codes_size,
371              const void *symbols, int symbols_wrap, int symbols_size,
372              int flags);
373 #define INIT_VLC_LE         2
374 #define INIT_VLC_USE_NEW_STATIC 4
375 void free_vlc(VLC *vlc);
376
377 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do {     \
378         static VLC_TYPE table[static_size][2];                          \
379         (vlc)->table = table;                                           \
380         (vlc)->table_allocated = static_size;                           \
381         init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);    \
382     } while (0)
383
384
385 /**
386  * If the vlc code is invalid and max_depth=1, then no bits will be removed.
387  * If the vlc code is invalid and max_depth>1, then the number of bits removed
388  * is undefined.
389  */
390 #define GET_VLC(code, name, gb, table, bits, max_depth)         \
391     do {                                                        \
392         int n, nb_bits;                                         \
393         unsigned int index;                                     \
394                                                                 \
395         index = SHOW_UBITS(name, gb, bits);                     \
396         code  = table[index][0];                                \
397         n     = table[index][1];                                \
398                                                                 \
399         if (max_depth > 1 && n < 0) {                           \
400             LAST_SKIP_BITS(name, gb, bits);                     \
401             UPDATE_CACHE(name, gb);                             \
402                                                                 \
403             nb_bits = -n;                                       \
404                                                                 \
405             index = SHOW_UBITS(name, gb, nb_bits) + code;       \
406             code  = table[index][0];                            \
407             n     = table[index][1];                            \
408             if (max_depth > 2 && n < 0) {                       \
409                 LAST_SKIP_BITS(name, gb, nb_bits);              \
410                 UPDATE_CACHE(name, gb);                         \
411                                                                 \
412                 nb_bits = -n;                                   \
413                                                                 \
414                 index = SHOW_UBITS(name, gb, nb_bits) + code;   \
415                 code  = table[index][0];                        \
416                 n     = table[index][1];                        \
417             }                                                   \
418         }                                                       \
419         SKIP_BITS(name, gb, n);                                 \
420     } while (0)
421
422 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
423     do {                                                                \
424         int n, nb_bits;                                                 \
425         unsigned int index;                                             \
426                                                                         \
427         index = SHOW_UBITS(name, gb, bits);                             \
428         level = table[index].level;                                     \
429         n     = table[index].len;                                       \
430                                                                         \
431         if (max_depth > 1 && n < 0) {                                   \
432             SKIP_BITS(name, gb, bits);                                  \
433             if (need_update) {                                          \
434                 UPDATE_CACHE(name, gb);                                 \
435             }                                                           \
436                                                                         \
437             nb_bits = -n;                                               \
438                                                                         \
439             index = SHOW_UBITS(name, gb, nb_bits) + level;              \
440             level = table[index].level;                                 \
441             n     = table[index].len;                                   \
442         }                                                               \
443         run = table[index].run;                                         \
444         SKIP_BITS(name, gb, n);                                         \
445     } while (0)
446
447
448 /**
449  * Parse a vlc code.
450  * @param bits is the number of bits which will be read at once, must be
451  *             identical to nb_bits in init_vlc()
452  * @param max_depth is the number of times bits bits must be read to completely
453  *                  read the longest vlc code
454  *                  = (max_vlc_length + bits - 1) / bits
455  */
456 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
457                                      int bits, int max_depth)
458 {
459     int code;
460
461     OPEN_READER(re, s);
462     UPDATE_CACHE(re, s);
463
464     GET_VLC(code, re, s, table, bits, max_depth);
465
466     CLOSE_READER(re, s);
467     return code;
468 }
469
470 static inline int decode012(GetBitContext *gb)
471 {
472     int n;
473     n = get_bits1(gb);
474     if (n == 0)
475         return 0;
476     else
477         return get_bits1(gb) + 1;
478 }
479
480 static inline int decode210(GetBitContext *gb)
481 {
482     if (get_bits1(gb))
483         return 0;
484     else
485         return 2 - get_bits1(gb);
486 }
487
488 static inline int get_bits_left(GetBitContext *gb)
489 {
490     return gb->size_in_bits - get_bits_count(gb);
491 }
492
493 //#define TRACE
494
495 #ifdef TRACE
496 static inline void print_bin(int bits, int n)
497 {
498     int i;
499
500     for (i = n-1; i >= 0; i--) {
501         av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
502     }
503     for (i = n; i < 24; i++)
504         av_log(NULL, AV_LOG_DEBUG, " ");
505 }
506
507 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
508                                  const char *func, int line)
509 {
510     int r = get_bits(s, n);
511
512     print_bin(r, n);
513     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
514            r, n, r, get_bits_count(s)-n, file, func, line);
515     return r;
516 }
517 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
518                                 int bits, int max_depth, char *file,
519                                 const char *func, int line)
520 {
521     int show  = show_bits(s, 24);
522     int pos   = get_bits_count(s);
523     int r     = get_vlc2(s, table, bits, max_depth);
524     int len   = get_bits_count(s) - pos;
525     int bits2 = show >> (24-len);
526
527     print_bin(bits2, len);
528
529     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
530            bits2, len, r, pos, file, func, line);
531     return r;
532 }
533 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
534                                   const char *func, int line)
535 {
536     int show = show_bits(s, n);
537     int r    = get_xbits(s, n);
538
539     print_bin(show, n);
540     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
541            show, n, r, get_bits_count(s)-n, file, func, line);
542     return r;
543 }
544
545 #define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
546 #define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
547 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
548 #define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
549 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
550
551 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
552
553 #else //TRACE
554 #define tprintf(p, ...) {}
555 #endif
556
557 #endif /* AVCODEC_GET_BITS_H */