]> git.sesse.net Git - ffmpeg/blob - libavcodec/get_bits.h
Merge remote-tracking branch 'qatar/master'
[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 #if UNCHECKED_BITSTREAM_READER
122 #define OPEN_READER(name, gb)                   \
123     unsigned int name##_index = (gb)->index;    \
124     av_unused unsigned int name##_cache
125
126 #define HAVE_BITS_REMAINING(name, gb) 1
127 #else
128 #define OPEN_READER(name, gb)                   \
129     unsigned int name##_index = (gb)->index;    \
130     unsigned int av_unused name##_cache = 0;    \
131     unsigned int av_unused name##_size_plus8 =  \
132                 (gb)->size_in_bits_plus8
133
134 #define HAVE_BITS_REMAINING(name, gb)           \
135     name##_index < name##_size_plus8
136 #endif
137
138 #define CLOSE_READER(name, gb) (gb)->index = name##_index
139
140 #ifdef BITSTREAM_READER_LE
141
142 # ifdef LONG_BITSTREAM_READER
143 #   define UPDATE_CACHE(name, gb) name##_cache = \
144         AV_RL64((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
145 # else
146 #   define UPDATE_CACHE(name, gb) name##_cache = \
147         AV_RL32((gb)->buffer + (name##_index >> 3)) >> (name##_index & 7)
148 # endif
149
150 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num)
151
152 #else
153
154 # ifdef LONG_BITSTREAM_READER
155 #   define UPDATE_CACHE(name, gb) name##_cache = \
156         AV_RB64((gb)->buffer + (name##_index >> 3)) >> (32 - (name##_index & 7))
157 # else
158 #   define UPDATE_CACHE(name, gb) name##_cache = \
159         AV_RB32((gb)->buffer + (name##_index >> 3)) << (name##_index & 7)
160 # endif
161
162 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num)
163
164 #endif
165
166 #if UNCHECKED_BITSTREAM_READER
167 #   define SKIP_COUNTER(name, gb, num) name##_index += (num)
168 #else
169 #   define SKIP_COUNTER(name, gb, num) \
170     name##_index = FFMIN(name##_size_plus8, name##_index + (num))
171 #endif
172
173 #define SKIP_BITS(name, gb, num) do {           \
174         SKIP_CACHE(name, gb, num);              \
175         SKIP_COUNTER(name, gb, num);            \
176     } while (0)
177
178 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
179
180 #ifdef BITSTREAM_READER_LE
181 #   define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num)
182 #   define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num)
183 #else
184 #   define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num)
185 #   define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num)
186 #endif
187
188 #define GET_CACHE(name, gb) ((uint32_t)name##_cache)
189
190 static inline int get_bits_count(const GetBitContext *s)
191 {
192     return s->index;
193 }
194
195 static inline void skip_bits_long(GetBitContext *s, int n){
196 #if UNCHECKED_BITSTREAM_READER
197     s->index += n;
198 #else
199     s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
200 #endif
201 }
202
203 /**
204  * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
205  * if MSB not set it is negative
206  * @param n length in bits
207  */
208 static inline int get_xbits(GetBitContext *s, int n)
209 {
210     register int sign;
211     register int32_t cache;
212     OPEN_READER(re, s);
213     UPDATE_CACHE(re, s);
214     cache = GET_CACHE(re, s);
215     sign = ~cache >> 31;
216     LAST_SKIP_BITS(re, s, n);
217     CLOSE_READER(re, s);
218     return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
219 }
220
221 static inline int get_sbits(GetBitContext *s, int n)
222 {
223     register int tmp;
224     OPEN_READER(re, s);
225     UPDATE_CACHE(re, s);
226     tmp = SHOW_SBITS(re, s, n);
227     LAST_SKIP_BITS(re, s, n);
228     CLOSE_READER(re, s);
229     return tmp;
230 }
231
232 /**
233  * Read 1-25 bits.
234  */
235 static inline unsigned int get_bits(GetBitContext *s, int n)
236 {
237     register int tmp;
238     OPEN_READER(re, s);
239     UPDATE_CACHE(re, s);
240     tmp = SHOW_UBITS(re, s, n);
241     LAST_SKIP_BITS(re, s, n);
242     CLOSE_READER(re, s);
243     return tmp;
244 }
245
246 /**
247  * Show 1-25 bits.
248  */
249 static inline unsigned int show_bits(GetBitContext *s, int n)
250 {
251     register int tmp;
252     OPEN_READER(re, s);
253     UPDATE_CACHE(re, s);
254     tmp = SHOW_UBITS(re, s, n);
255     return tmp;
256 }
257
258 static inline void skip_bits(GetBitContext *s, int n)
259 {
260     OPEN_READER(re, s);
261     UPDATE_CACHE(re, s);
262     LAST_SKIP_BITS(re, s, n);
263     CLOSE_READER(re, s);
264 }
265
266 static inline unsigned int get_bits1(GetBitContext *s)
267 {
268     unsigned int index = s->index;
269     uint8_t result = s->buffer[index>>3];
270 #ifdef BITSTREAM_READER_LE
271     result >>= index & 7;
272     result &= 1;
273 #else
274     result <<= index & 7;
275     result >>= 8 - 1;
276 #endif
277 #if !UNCHECKED_BITSTREAM_READER
278     if (s->index < s->size_in_bits_plus8)
279 #endif
280         index++;
281     s->index = index;
282
283     return result;
284 }
285
286 static inline unsigned int show_bits1(GetBitContext *s)
287 {
288     return show_bits(s, 1);
289 }
290
291 static inline void skip_bits1(GetBitContext *s)
292 {
293     skip_bits(s, 1);
294 }
295
296 /**
297  * Read 0-32 bits.
298  */
299 static inline unsigned int get_bits_long(GetBitContext *s, int n)
300 {
301     if (n <= MIN_CACHE_BITS)
302         return get_bits(s, n);
303     else {
304 #ifdef BITSTREAM_READER_LE
305         int ret = get_bits(s, 16);
306         return ret | (get_bits(s, n-16) << 16);
307 #else
308         int ret = get_bits(s, 16) << (n-16);
309         return ret | get_bits(s, n-16);
310 #endif
311     }
312 }
313
314 /**
315  * Read 0-32 bits as a signed integer.
316  */
317 static inline int get_sbits_long(GetBitContext *s, int n)
318 {
319     return sign_extend(get_bits_long(s, n), n);
320 }
321
322 /**
323  * Show 0-32 bits.
324  */
325 static inline unsigned int show_bits_long(GetBitContext *s, int n)
326 {
327     if (n <= MIN_CACHE_BITS)
328         return show_bits(s, n);
329     else {
330         GetBitContext gb = *s;
331         return get_bits_long(&gb, n);
332     }
333 }
334
335 static inline int check_marker(GetBitContext *s, const char *msg)
336 {
337     int bit = get_bits1(s);
338     if (!bit)
339         av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
340
341     return bit;
342 }
343
344 /**
345  * Inititalize GetBitContext.
346  * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger than the actual read bits
347  * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
348  * @param bit_size the size of the buffer in bits
349  */
350 static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,
351                                  int bit_size)
352 {
353     int buffer_size = (bit_size+7)>>3;
354     if (buffer_size < 0 || bit_size < 0) {
355         buffer_size = bit_size = 0;
356         buffer = NULL;
357     }
358
359     s->buffer       = buffer;
360     s->size_in_bits = bit_size;
361     s->size_in_bits_plus8 = bit_size + 8;
362     s->buffer_end   = buffer + buffer_size;
363     s->index        = 0;
364 }
365
366 static inline void align_get_bits(GetBitContext *s)
367 {
368     int n = -get_bits_count(s) & 7;
369     if (n) skip_bits(s, n);
370 }
371
372 #define init_vlc(vlc, nb_bits, nb_codes,                \
373                  bits, bits_wrap, bits_size,            \
374                  codes, codes_wrap, codes_size,         \
375                  flags)                                 \
376         ff_init_vlc_sparse(vlc, nb_bits, nb_codes,         \
377                            bits, bits_wrap, bits_size,     \
378                            codes, codes_wrap, codes_size,  \
379                            NULL, 0, 0, flags)
380
381 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
382              const void *bits, int bits_wrap, int bits_size,
383              const void *codes, int codes_wrap, int codes_size,
384              const void *symbols, int symbols_wrap, int symbols_size,
385              int flags);
386 #define INIT_VLC_LE         2
387 #define INIT_VLC_USE_NEW_STATIC 4
388 void ff_free_vlc(VLC *vlc);
389
390 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do {     \
391         static VLC_TYPE table[static_size][2];                          \
392         (vlc)->table = table;                                           \
393         (vlc)->table_allocated = static_size;                           \
394         init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);    \
395     } while (0)
396
397
398 /**
399  * If the vlc code is invalid and max_depth=1, then no bits will be removed.
400  * If the vlc code is invalid and max_depth>1, then the number of bits removed
401  * is undefined.
402  */
403 #define GET_VLC(code, name, gb, table, bits, max_depth)         \
404     do {                                                        \
405         int n, nb_bits;                                         \
406         unsigned int index;                                     \
407                                                                 \
408         index = SHOW_UBITS(name, gb, bits);                     \
409         code  = table[index][0];                                \
410         n     = table[index][1];                                \
411                                                                 \
412         if (max_depth > 1 && n < 0) {                           \
413             LAST_SKIP_BITS(name, gb, bits);                     \
414             UPDATE_CACHE(name, gb);                             \
415                                                                 \
416             nb_bits = -n;                                       \
417                                                                 \
418             index = SHOW_UBITS(name, gb, nb_bits) + code;       \
419             code  = table[index][0];                            \
420             n     = table[index][1];                            \
421             if (max_depth > 2 && n < 0) {                       \
422                 LAST_SKIP_BITS(name, gb, nb_bits);              \
423                 UPDATE_CACHE(name, gb);                         \
424                                                                 \
425                 nb_bits = -n;                                   \
426                                                                 \
427                 index = SHOW_UBITS(name, gb, nb_bits) + code;   \
428                 code  = table[index][0];                        \
429                 n     = table[index][1];                        \
430             }                                                   \
431         }                                                       \
432         SKIP_BITS(name, gb, n);                                 \
433     } while (0)
434
435 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) \
436     do {                                                                \
437         int n, nb_bits;                                                 \
438         unsigned int index;                                             \
439                                                                         \
440         index = SHOW_UBITS(name, gb, bits);                             \
441         level = table[index].level;                                     \
442         n     = table[index].len;                                       \
443                                                                         \
444         if (max_depth > 1 && n < 0) {                                   \
445             SKIP_BITS(name, gb, bits);                                  \
446             if (need_update) {                                          \
447                 UPDATE_CACHE(name, gb);                                 \
448             }                                                           \
449                                                                         \
450             nb_bits = -n;                                               \
451                                                                         \
452             index = SHOW_UBITS(name, gb, nb_bits) + level;              \
453             level = table[index].level;                                 \
454             n     = table[index].len;                                   \
455         }                                                               \
456         run = table[index].run;                                         \
457         SKIP_BITS(name, gb, n);                                         \
458     } while (0)
459
460
461 /**
462  * Parse a vlc code.
463  * @param bits is the number of bits which will be read at once, must be
464  *             identical to nb_bits in init_vlc()
465  * @param max_depth is the number of times bits bits must be read to completely
466  *                  read the longest vlc code
467  *                  = (max_vlc_length + bits - 1) / bits
468  */
469 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
470                                      int bits, int max_depth)
471 {
472     int code;
473
474     OPEN_READER(re, s);
475     UPDATE_CACHE(re, s);
476
477     GET_VLC(code, re, s, table, bits, max_depth);
478
479     CLOSE_READER(re, s);
480     return code;
481 }
482
483 static inline int decode012(GetBitContext *gb)
484 {
485     int n;
486     n = get_bits1(gb);
487     if (n == 0)
488         return 0;
489     else
490         return get_bits1(gb) + 1;
491 }
492
493 static inline int decode210(GetBitContext *gb)
494 {
495     if (get_bits1(gb))
496         return 0;
497     else
498         return 2 - get_bits1(gb);
499 }
500
501 static inline int get_bits_left(GetBitContext *gb)
502 {
503     return gb->size_in_bits - get_bits_count(gb);
504 }
505
506 //#define TRACE
507
508 #ifdef TRACE
509 static inline void print_bin(int bits, int n)
510 {
511     int i;
512
513     for (i = n-1; i >= 0; i--) {
514         av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1);
515     }
516     for (i = n; i < 24; i++)
517         av_log(NULL, AV_LOG_DEBUG, " ");
518 }
519
520 static inline int get_bits_trace(GetBitContext *s, int n, char *file,
521                                  const char *func, int line)
522 {
523     int r = get_bits(s, n);
524
525     print_bin(r, n);
526     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
527            r, n, r, get_bits_count(s)-n, file, func, line);
528     return r;
529 }
530 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
531                                 int bits, int max_depth, char *file,
532                                 const char *func, int line)
533 {
534     int show  = show_bits(s, 24);
535     int pos   = get_bits_count(s);
536     int r     = get_vlc2(s, table, bits, max_depth);
537     int len   = get_bits_count(s) - pos;
538     int bits2 = show >> (24-len);
539
540     print_bin(bits2, len);
541
542     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
543            bits2, len, r, pos, file, func, line);
544     return r;
545 }
546 static inline int get_xbits_trace(GetBitContext *s, int n, char *file,
547                                   const char *func, int line)
548 {
549     int show = show_bits(s, n);
550     int r    = get_xbits(s, n);
551
552     print_bin(show, n);
553     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
554            show, n, r, get_bits_count(s)-n, file, func, line);
555     return r;
556 }
557
558 #define get_bits(s, n)  get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
559 #define get_bits1(s)    get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
560 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
561 #define get_vlc(s, vlc)            get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
562 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
563
564 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
565
566 #else //TRACE
567 #define tprintf(p, ...) {}
568 #endif
569
570 #endif /* AVCODEC_GET_BITS_H */