Open code SKIP_BITS
[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 #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
18
19 /** Structure for bistream I/O. */
20 struct getbit_context {
21 /* Start of the internal buffer. */
22 const uint8_t *buffer;
23 /* End of the internal buffer. */
24 const uint8_t *buffer_end;
25 /** Bit counter. */
26 int index;
27 };
28
29 #define VLC_TYPE int16_t
30
31 struct vlc {
32 int bits;
33 VLC_TYPE(*table)[2]; ///< code, bits
34 int table_size, table_allocated;
35 };
36
37 /** Load \a gb into local variables. */
38 #define OPEN_READER(name, gb)\
39 int name##_index= (gb)->index;\
40 int name##_cache= 0;\
41
42 /** Store local vars in gb. */
43 #define CLOSE_READER(name, gb)\
44 (gb)->index= name##_index;\
45
46 /**
47 * Refill the internal cache from the bitstream.
48 */
49 #define UPDATE_CACHE(name, gb)\
50 name##_cache= AV_RB32( ((gb)->buffer) \
51 + (name##_index >> 3) ) << (name##_index & 0x07);\
52
53 /**
54 * Remove the next num bits from the cache.
55 *
56 * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
57 */
58 #define SKIP_CACHE(name, gb, num)\
59 name##_cache <<= (num);
60
61 /**
62 * Increment the internal bit counter.
63 *
64 * \sa SKIP_CACHE.
65 */
66 #define SKIP_COUNTER(name, gb, num)\
67 name##_index += (num);\
68
69 /** Return the next num bits. */
70 #define SHOW_UBITS(name, gb, num)\
71 NEG_USR32(name##_cache, num)
72
73 static inline int get_bits_count(struct getbit_context *s)
74 {
75 return s->index;
76 }
77
78 /**
79 * Read 1-17 bits.
80 */
81 static inline unsigned int get_bits(struct getbit_context *s, int n)
82 {
83 register int tmp;
84 OPEN_READER(re, s)
85 UPDATE_CACHE(re, s)
86 tmp = SHOW_UBITS(re, s, n);
87 SKIP_COUNTER(re, s, n)
88 CLOSE_READER(re, s)
89 return tmp;
90 }
91
92 static inline void skip_bits(struct getbit_context *s, int n)
93 {
94 /* gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
95 OPEN_READER(re, s)
96 UPDATE_CACHE(re, s)
97 SKIP_COUNTER(re, s, n)
98 CLOSE_READER(re, s)
99 }
100
101 static inline unsigned int get_bits1(struct getbit_context *s)
102 {
103 int idx = s->index;
104 uint8_t result = s->buffer[idx >> 3];
105
106 result <<= (idx & 0x07);
107 result >>= 8 - 1;
108 idx++;
109 s->index = idx;
110 return result;
111 }
112
113 /**
114 * Initialize a getbit_context structure.
115 *
116 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
117 * bytes larger then the actual read bits * because some optimized bitstream
118 * readers read 32 or 64 bit at once and could read over the end.
119 *
120 * \param bit_size The size of the buffer in bytes.
121 */
122 static inline void init_get_bits(struct getbit_context *s,
123 const uint8_t *buffer, int size)
124 {
125 s->buffer = buffer;
126 s->buffer_end = buffer + size;
127 s->index = 0;
128 }
129
130 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
131 const void *codes, int codes_size);
132
133 void free_vlc(struct vlc *vlc);
134
135 /**
136 * Parse a vlc code.
137 *
138 * \param bits The number of bits which will be read at once, must be
139 * identical to nb_bits in init_vlc()
140 *
141 * \param max_depth The number of times bits bits must be read to completely
142 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
143 */
144 static inline int get_vlc(struct getbit_context *gb, VLC_TYPE(*table)[2],
145 int bits, int max_depth)
146 {
147 int n, idx, nb_bits, code;
148
149 OPEN_READER(re, gb)
150 UPDATE_CACHE(re, gb)
151 idx = SHOW_UBITS(re, gb, bits);
152 code = table[idx][0];
153 n = table[idx][1];
154 if (max_depth > 1 && n < 0) {
155 SKIP_COUNTER(re, gb, bits)
156 UPDATE_CACHE(re, gb)
157 nb_bits = -n;
158 idx = SHOW_UBITS(re, gb, nb_bits) + code;
159 code = table[idx][0];
160 n = table[idx][1];
161 if (max_depth > 2 && n < 0) {
162 SKIP_COUNTER(re, gb, nb_bits)
163 UPDATE_CACHE(re, gb)
164 nb_bits = -n;
165 idx = SHOW_UBITS(re, gb, nb_bits) + code;
166 code = table[idx][0];
167 n = table[idx][1];
168 }
169 }
170 SKIP_CACHE(re, gb, n)
171 SKIP_COUNTER(re, gb, n)
172 CLOSE_READER(re, gb)
173 return code;
174 }