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