]> git.tuebingen.mpg.de Git - paraslash.git/blob - bitstream.h
Simplify get_bits1().
[paraslash.git] / bitstream.h
1 /*
2  * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.h.
3  *
4  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * Licensed under the GNU Lesser General Public License.
7  * For licencing details see COPYING.LIB.
8  */
9
10 /** \file bitstream.h Bitstream structures and inline functions. */
11
12 #define AV_RB32(x)  ((((const uint8_t*)(x))[0] << 24) | \
13                      (((const uint8_t*)(x))[1] << 16) | \
14                      (((const uint8_t*)(x))[2] <<  8) | \
15                       ((const uint8_t*)(x))[3])
16
17 /** Structure for bistream I/O. */
18 struct getbit_context {
19         /* Start of the internal buffer. */
20         const uint8_t *buffer;
21         /* End of the internal buffer. */
22         const uint8_t *buffer_end;
23         /** Bit counter. */
24         int index;
25 };
26
27 #define VLC_TYPE int16_t
28
29 struct vlc {
30         int bits;
31         VLC_TYPE(*table)[2];    ///< code, bits
32         int table_size, table_allocated;
33 };
34
35 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
36 {
37         int idx = gbc->index;
38         uint32_t x = AV_RB32(gbc->buffer + (idx >> 3)) << (idx & 7);
39         return x >> (32 - num);
40 }
41
42 static inline int get_bits_count(struct getbit_context *gbc)
43 {
44         return gbc->index;
45 }
46
47 static inline void skip_bits(struct getbit_context *gbc, int n)
48 {
49         gbc->index += n;
50 }
51
52 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
53 {
54         unsigned int ret = show_bits(gbc, n);
55         skip_bits(gbc, n);
56         return ret;
57 }
58
59 /* This is rather hot, we can do better than get_bits(gbc, 1). */
60 static inline unsigned int get_bits1(struct getbit_context *gbc)
61 {
62         int idx = gbc->index++;
63         uint8_t tmp = gbc->buffer[idx >> 3], mask = (1 << (7 - (idx & 7)));
64         return !!(tmp & mask);
65 }
66
67 /**
68  * Initialize a getbit_context structure.
69  *
70  * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
71  * bytes larger then the actual read bits * because some optimized bitstream
72  * readers read 32 or 64 bit at once and could read over the end.
73  *
74  * \param bit_size The size of the buffer in bytes.
75  */
76 static inline void init_get_bits(struct getbit_context *gbc,
77                 const uint8_t *buffer, int size)
78 {
79         gbc->buffer = buffer;
80         gbc->buffer_end = buffer + size;
81         gbc->index = 0;
82 }
83
84 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
85                 const void *codes, int codes_size);
86
87 void free_vlc(struct vlc *vlc);
88
89 /**
90  * Parse a vlc code.
91  *
92  * \param bits The number of bits which will be read at once, must be
93  * identical to nb_bits in init_vlc()
94  *
95  * \param max_depth The number of times bits bits must be read to completely
96  * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
97  */
98 static inline int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2],
99                 int bits, int max_depth)
100 {
101         int n, idx, nb_bits, code;
102
103         idx = show_bits(gbc, bits);
104         code = table[idx][0];
105         n = table[idx][1];
106         if (max_depth > 1 && n < 0) {
107                 skip_bits(gbc, bits);
108                 nb_bits = -n;
109                 idx = show_bits(gbc, nb_bits) + code;
110                 code = table[idx][0];
111                 n = table[idx][1];
112                 if (max_depth > 2 && n < 0) {
113                         skip_bits(gbc, nb_bits);
114                         nb_bits = -n;
115                         idx = show_bits(gbc, nb_bits) + code;
116                         code = table[idx][0];
117                         n = table[idx][1];
118                 }
119         }
120         skip_bits(gbc, n);
121         return code;
122 }