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