yading@10
|
1 /*
|
yading@10
|
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
yading@10
|
3 *
|
yading@10
|
4 * This file is part of FFmpeg.
|
yading@10
|
5 *
|
yading@10
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
8 * License as published by the Free Software Foundation; either
|
yading@10
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
10 *
|
yading@10
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
14 * Lesser General Public License for more details.
|
yading@10
|
15 *
|
yading@10
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
19 */
|
yading@10
|
20
|
yading@10
|
21 /**
|
yading@10
|
22 * @file
|
yading@10
|
23 * bitstream reader API header.
|
yading@10
|
24 */
|
yading@10
|
25
|
yading@10
|
26 #ifndef AVCODEC_GET_BITS_H
|
yading@10
|
27 #define AVCODEC_GET_BITS_H
|
yading@10
|
28
|
yading@10
|
29 #include <stdint.h>
|
yading@10
|
30
|
yading@10
|
31 #include "libavutil/common.h"
|
yading@10
|
32 #include "libavutil/intreadwrite.h"
|
yading@10
|
33 #include "libavutil/log.h"
|
yading@10
|
34 #include "libavutil/avassert.h"
|
yading@10
|
35 #include "mathops.h"
|
yading@10
|
36
|
yading@10
|
37 /*
|
yading@10
|
38 * Safe bitstream reading:
|
yading@10
|
39 * optionally, the get_bits API can check to ensure that we
|
yading@10
|
40 * don't read past input buffer boundaries. This is protected
|
yading@10
|
41 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
|
yading@10
|
42 * then below that with UNCHECKED_BITSTREAM_READER at the per-
|
yading@10
|
43 * decoder level. This means that decoders that check internally
|
yading@10
|
44 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
|
yading@10
|
45 * overread checks.
|
yading@10
|
46 * Boundary checking causes a minor performance penalty so for
|
yading@10
|
47 * applications that won't want/need this, it can be disabled
|
yading@10
|
48 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
|
yading@10
|
49 */
|
yading@10
|
50 #ifndef UNCHECKED_BITSTREAM_READER
|
yading@10
|
51 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
|
yading@10
|
52 #endif
|
yading@10
|
53
|
yading@10
|
54 typedef struct GetBitContext {
|
yading@10
|
55 const uint8_t *buffer, *buffer_end;
|
yading@10
|
56 int index;
|
yading@10
|
57 int size_in_bits;
|
yading@10
|
58 int size_in_bits_plus8;
|
yading@10
|
59 } GetBitContext;
|
yading@10
|
60
|
yading@10
|
61 #define VLC_TYPE int16_t
|
yading@10
|
62
|
yading@10
|
63 typedef struct VLC {
|
yading@10
|
64 int bits;
|
yading@10
|
65 VLC_TYPE (*table)[2]; ///< code, bits
|
yading@10
|
66 int table_size, table_allocated;
|
yading@10
|
67 } VLC;
|
yading@10
|
68
|
yading@10
|
69 typedef struct RL_VLC_ELEM {
|
yading@10
|
70 int16_t level;
|
yading@10
|
71 int8_t len;
|
yading@10
|
72 uint8_t run;
|
yading@10
|
73 } RL_VLC_ELEM;
|
yading@10
|
74
|
yading@10
|
75 /* Bitstream reader API docs:
|
yading@10
|
76 * name
|
yading@10
|
77 * arbitrary name which is used as prefix for the internal variables
|
yading@10
|
78 *
|
yading@10
|
79 * gb
|
yading@10
|
80 * getbitcontext
|
yading@10
|
81 *
|
yading@10
|
82 * OPEN_READER(name, gb)
|
yading@10
|
83 * load gb into local variables
|
yading@10
|
84 *
|
yading@10
|
85 * CLOSE_READER(name, gb)
|
yading@10
|
86 * store local vars in gb
|
yading@10
|
87 *
|
yading@10
|
88 * UPDATE_CACHE(name, gb)
|
yading@10
|
89 * Refill the internal cache from the bitstream.
|
yading@10
|
90 * After this call at least MIN_CACHE_BITS will be available.
|
yading@10
|
91 *
|
yading@10
|
92 * GET_CACHE(name, gb)
|
yading@10
|
93 * Will output the contents of the internal cache,
|
yading@10
|
94 * next bit is MSB of 32 or 64 bit (FIXME 64bit).
|
yading@10
|
95 *
|
yading@10
|
96 * SHOW_UBITS(name, gb, num)
|
yading@10
|
97 * Will return the next num bits.
|
yading@10
|
98 *
|
yading@10
|
99 * SHOW_SBITS(name, gb, num)
|
yading@10
|
100 * Will return the next num bits and do sign extension.
|
yading@10
|
101 *
|
yading@10
|
102 * SKIP_BITS(name, gb, num)
|
yading@10
|
103 * Will skip over the next num bits.
|
yading@10
|
104 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
|
yading@10
|
105 *
|
yading@10
|
106 * SKIP_CACHE(name, gb, num)
|
yading@10
|
107 * Will remove the next num bits from the cache (note SKIP_COUNTER
|
yading@10
|
108 * MUST be called before UPDATE_CACHE / CLOSE_READER).
|
yading@10
|
109 *
|
yading@10
|
110 * SKIP_COUNTER(name, gb, num)
|
yading@10
|
111 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
|
yading@10
|
112 *
|
yading@10
|
113 * LAST_SKIP_BITS(name, gb, num)
|
yading@10
|
114 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
|
yading@10
|
115 *
|
yading@10
|
116 * For examples see get_bits, show_bits, skip_bits, get_vlc.
|
yading@10
|
117 */
|
yading@10
|
118
|
yading@10
|
119 #ifdef LONG_BITSTREAM_READER
|
yading@10
|
120 # define MIN_CACHE_BITS 32
|
yading@10
|
121 #else
|
yading@10
|
122 # define MIN_CACHE_BITS 25
|
yading@10
|
123 #endif
|
yading@10
|
124
|
yading@10
|
125 #if UNCHECKED_BITSTREAM_READER
|
yading@10
|
126 #define OPEN_READER(name, gb) \
|
yading@10
|
127 unsigned int name ## _index = (gb)->index; \
|
yading@10
|
128 unsigned int av_unused name ## _cache
|
yading@10
|
129
|
yading@10
|
130 #define HAVE_BITS_REMAINING(name, gb) 1
|
yading@10
|
131 #else
|
yading@10
|
132 #define OPEN_READER(name, gb) \
|
yading@10
|
133 unsigned int name ## _index = (gb)->index; \
|
yading@10
|
134 unsigned int av_unused name ## _cache = 0; \
|
yading@10
|
135 unsigned int av_unused name ## _size_plus8 = (gb)->size_in_bits_plus8
|
yading@10
|
136
|
yading@10
|
137 #define HAVE_BITS_REMAINING(name, gb) name ## _index < name ## _size_plus8
|
yading@10
|
138 #endif
|
yading@10
|
139
|
yading@10
|
140 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
|
yading@10
|
141
|
yading@10
|
142 #ifdef BITSTREAM_READER_LE
|
yading@10
|
143
|
yading@10
|
144 # ifdef LONG_BITSTREAM_READER
|
yading@10
|
145 # define UPDATE_CACHE(name, gb) name ## _cache = \
|
yading@10
|
146 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
|
yading@10
|
147 # else
|
yading@10
|
148 # define UPDATE_CACHE(name, gb) name ## _cache = \
|
yading@10
|
149 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
|
yading@10
|
150 # endif
|
yading@10
|
151
|
yading@10
|
152 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
|
yading@10
|
153
|
yading@10
|
154 #else
|
yading@10
|
155
|
yading@10
|
156 # ifdef LONG_BITSTREAM_READER
|
yading@10
|
157 # define UPDATE_CACHE(name, gb) name ## _cache = \
|
yading@10
|
158 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
|
yading@10
|
159 # else
|
yading@10
|
160 # define UPDATE_CACHE(name, gb) name ## _cache = \
|
yading@10
|
161 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
|
yading@10
|
162 # endif
|
yading@10
|
163
|
yading@10
|
164 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
|
yading@10
|
165
|
yading@10
|
166 #endif
|
yading@10
|
167
|
yading@10
|
168 #if UNCHECKED_BITSTREAM_READER
|
yading@10
|
169 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
|
yading@10
|
170 #else
|
yading@10
|
171 # define SKIP_COUNTER(name, gb, num) \
|
yading@10
|
172 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
|
yading@10
|
173 #endif
|
yading@10
|
174
|
yading@10
|
175 #define SKIP_BITS(name, gb, num) \
|
yading@10
|
176 do { \
|
yading@10
|
177 SKIP_CACHE(name, gb, num); \
|
yading@10
|
178 SKIP_COUNTER(name, gb, num); \
|
yading@10
|
179 } while (0)
|
yading@10
|
180
|
yading@10
|
181 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
|
yading@10
|
182
|
yading@10
|
183 #ifdef BITSTREAM_READER_LE
|
yading@10
|
184 # define SHOW_UBITS(name, gb, num) zero_extend(name ## _cache, num)
|
yading@10
|
185 # define SHOW_SBITS(name, gb, num) sign_extend(name ## _cache, num)
|
yading@10
|
186 #else
|
yading@10
|
187 # define SHOW_UBITS(name, gb, num) NEG_USR32(name ## _cache, num)
|
yading@10
|
188 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name ## _cache, num)
|
yading@10
|
189 #endif
|
yading@10
|
190
|
yading@10
|
191 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
|
yading@10
|
192
|
yading@10
|
193 static inline int get_bits_count(const GetBitContext *s)
|
yading@10
|
194 {
|
yading@10
|
195 return s->index;
|
yading@10
|
196 }
|
yading@10
|
197
|
yading@10
|
198 static inline void skip_bits_long(GetBitContext *s, int n)
|
yading@10
|
199 {
|
yading@10
|
200 #if UNCHECKED_BITSTREAM_READER
|
yading@10
|
201 s->index += n;
|
yading@10
|
202 #else
|
yading@10
|
203 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
|
yading@10
|
204 #endif
|
yading@10
|
205 }
|
yading@10
|
206
|
yading@10
|
207 /**
|
yading@10
|
208 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
|
yading@10
|
209 * if MSB not set it is negative
|
yading@10
|
210 * @param n length in bits
|
yading@10
|
211 */
|
yading@10
|
212 static inline int get_xbits(GetBitContext *s, int n)
|
yading@10
|
213 {
|
yading@10
|
214 register int sign;
|
yading@10
|
215 register int32_t cache;
|
yading@10
|
216 OPEN_READER(re, s);
|
yading@10
|
217 UPDATE_CACHE(re, s);
|
yading@10
|
218 cache = GET_CACHE(re, s);
|
yading@10
|
219 sign = ~cache >> 31;
|
yading@10
|
220 LAST_SKIP_BITS(re, s, n);
|
yading@10
|
221 CLOSE_READER(re, s);
|
yading@10
|
222 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
|
yading@10
|
223 }
|
yading@10
|
224
|
yading@10
|
225 static inline int get_sbits(GetBitContext *s, int n)
|
yading@10
|
226 {
|
yading@10
|
227 register int tmp;
|
yading@10
|
228 OPEN_READER(re, s);
|
yading@10
|
229 av_assert2(n>0 && n<=25);
|
yading@10
|
230 UPDATE_CACHE(re, s);
|
yading@10
|
231 tmp = SHOW_SBITS(re, s, n);
|
yading@10
|
232 LAST_SKIP_BITS(re, s, n);
|
yading@10
|
233 CLOSE_READER(re, s);
|
yading@10
|
234 return tmp;
|
yading@10
|
235 }
|
yading@10
|
236
|
yading@10
|
237 /**
|
yading@10
|
238 * Read 1-25 bits.
|
yading@10
|
239 */
|
yading@10
|
240 static inline unsigned int get_bits(GetBitContext *s, int n)
|
yading@10
|
241 {
|
yading@10
|
242 register int tmp;
|
yading@10
|
243 OPEN_READER(re, s);
|
yading@10
|
244 av_assert2(n>0 && n<=25);
|
yading@10
|
245 UPDATE_CACHE(re, s);
|
yading@10
|
246 tmp = SHOW_UBITS(re, s, n);
|
yading@10
|
247 LAST_SKIP_BITS(re, s, n);
|
yading@10
|
248 CLOSE_READER(re, s);
|
yading@10
|
249 return tmp;
|
yading@10
|
250 }
|
yading@10
|
251
|
yading@10
|
252 /**
|
yading@10
|
253 * Show 1-25 bits.
|
yading@10
|
254 */
|
yading@10
|
255 static inline unsigned int show_bits(GetBitContext *s, int n)
|
yading@10
|
256 {
|
yading@10
|
257 register int tmp;
|
yading@10
|
258 OPEN_READER(re, s);
|
yading@10
|
259 av_assert2(n>0 && n<=25);
|
yading@10
|
260 UPDATE_CACHE(re, s);
|
yading@10
|
261 tmp = SHOW_UBITS(re, s, n);
|
yading@10
|
262 return tmp;
|
yading@10
|
263 }
|
yading@10
|
264
|
yading@10
|
265 static inline void skip_bits(GetBitContext *s, int n)
|
yading@10
|
266 {
|
yading@10
|
267 OPEN_READER(re, s);
|
yading@10
|
268 UPDATE_CACHE(re, s);
|
yading@10
|
269 LAST_SKIP_BITS(re, s, n);
|
yading@10
|
270 CLOSE_READER(re, s);
|
yading@10
|
271 }
|
yading@10
|
272
|
yading@10
|
273 static inline unsigned int get_bits1(GetBitContext *s)
|
yading@10
|
274 {
|
yading@10
|
275 unsigned int index = s->index;
|
yading@10
|
276 uint8_t result = s->buffer[index >> 3];
|
yading@10
|
277 #ifdef BITSTREAM_READER_LE
|
yading@10
|
278 result >>= index & 7;
|
yading@10
|
279 result &= 1;
|
yading@10
|
280 #else
|
yading@10
|
281 result <<= index & 7;
|
yading@10
|
282 result >>= 8 - 1;
|
yading@10
|
283 #endif
|
yading@10
|
284 #if !UNCHECKED_BITSTREAM_READER
|
yading@10
|
285 if (s->index < s->size_in_bits_plus8)
|
yading@10
|
286 #endif
|
yading@10
|
287 index++;
|
yading@10
|
288 s->index = index;
|
yading@10
|
289
|
yading@10
|
290 return result;
|
yading@10
|
291 }
|
yading@10
|
292
|
yading@10
|
293 static inline unsigned int show_bits1(GetBitContext *s)
|
yading@10
|
294 {
|
yading@10
|
295 return show_bits(s, 1);
|
yading@10
|
296 }
|
yading@10
|
297
|
yading@10
|
298 static inline void skip_bits1(GetBitContext *s)
|
yading@10
|
299 {
|
yading@10
|
300 skip_bits(s, 1);
|
yading@10
|
301 }
|
yading@10
|
302
|
yading@10
|
303 /**
|
yading@10
|
304 * Read 0-32 bits.
|
yading@10
|
305 */
|
yading@10
|
306 static inline unsigned int get_bits_long(GetBitContext *s, int n)
|
yading@10
|
307 {
|
yading@10
|
308 if (!n) {
|
yading@10
|
309 return 0;
|
yading@10
|
310 } else if (n <= MIN_CACHE_BITS) {
|
yading@10
|
311 return get_bits(s, n);
|
yading@10
|
312 } else {
|
yading@10
|
313 #ifdef BITSTREAM_READER_LE
|
yading@10
|
314 unsigned ret = get_bits(s, 16);
|
yading@10
|
315 return ret | (get_bits(s, n - 16) << 16);
|
yading@10
|
316 #else
|
yading@10
|
317 unsigned ret = get_bits(s, 16) << (n - 16);
|
yading@10
|
318 return ret | get_bits(s, n - 16);
|
yading@10
|
319 #endif
|
yading@10
|
320 }
|
yading@10
|
321 }
|
yading@10
|
322
|
yading@10
|
323 /**
|
yading@10
|
324 * Read 0-64 bits.
|
yading@10
|
325 */
|
yading@10
|
326 static inline uint64_t get_bits64(GetBitContext *s, int n)
|
yading@10
|
327 {
|
yading@10
|
328 if (n <= 32) {
|
yading@10
|
329 return get_bits_long(s, n);
|
yading@10
|
330 } else {
|
yading@10
|
331 #ifdef BITSTREAM_READER_LE
|
yading@10
|
332 uint64_t ret = get_bits_long(s, 32);
|
yading@10
|
333 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
|
yading@10
|
334 #else
|
yading@10
|
335 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
|
yading@10
|
336 return ret | get_bits_long(s, 32);
|
yading@10
|
337 #endif
|
yading@10
|
338 }
|
yading@10
|
339 }
|
yading@10
|
340
|
yading@10
|
341 /**
|
yading@10
|
342 * Read 0-32 bits as a signed integer.
|
yading@10
|
343 */
|
yading@10
|
344 static inline int get_sbits_long(GetBitContext *s, int n)
|
yading@10
|
345 {
|
yading@10
|
346 return sign_extend(get_bits_long(s, n), n);
|
yading@10
|
347 }
|
yading@10
|
348
|
yading@10
|
349 /**
|
yading@10
|
350 * Show 0-32 bits.
|
yading@10
|
351 */
|
yading@10
|
352 static inline unsigned int show_bits_long(GetBitContext *s, int n)
|
yading@10
|
353 {
|
yading@10
|
354 if (n <= MIN_CACHE_BITS) {
|
yading@10
|
355 return show_bits(s, n);
|
yading@10
|
356 } else {
|
yading@10
|
357 GetBitContext gb = *s;
|
yading@10
|
358 return get_bits_long(&gb, n);
|
yading@10
|
359 }
|
yading@10
|
360 }
|
yading@10
|
361
|
yading@10
|
362 static inline int check_marker(GetBitContext *s, const char *msg)
|
yading@10
|
363 {
|
yading@10
|
364 int bit = get_bits1(s);
|
yading@10
|
365 if (!bit)
|
yading@10
|
366 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg);
|
yading@10
|
367
|
yading@10
|
368 return bit;
|
yading@10
|
369 }
|
yading@10
|
370
|
yading@10
|
371 /**
|
yading@10
|
372 * Initialize GetBitContext.
|
yading@10
|
373 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
|
yading@10
|
374 * larger than the actual read bits because some optimized bitstream
|
yading@10
|
375 * readers read 32 or 64 bit at once and could read over the end
|
yading@10
|
376 * @param bit_size the size of the buffer in bits
|
yading@10
|
377 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
|
yading@10
|
378 */
|
yading@10
|
379 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
|
yading@10
|
380 int bit_size)
|
yading@10
|
381 {
|
yading@10
|
382 int buffer_size;
|
yading@10
|
383 int ret = 0;
|
yading@10
|
384
|
yading@10
|
385 if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) {
|
yading@10
|
386 buffer_size = bit_size = 0;
|
yading@10
|
387 buffer = NULL;
|
yading@10
|
388 ret = AVERROR_INVALIDDATA;
|
yading@10
|
389 }
|
yading@10
|
390
|
yading@10
|
391 buffer_size = (bit_size + 7) >> 3;
|
yading@10
|
392
|
yading@10
|
393 s->buffer = buffer;
|
yading@10
|
394 s->size_in_bits = bit_size;
|
yading@10
|
395 s->size_in_bits_plus8 = bit_size + 8;
|
yading@10
|
396 s->buffer_end = buffer + buffer_size;
|
yading@10
|
397 s->index = 0;
|
yading@10
|
398
|
yading@10
|
399 return ret;
|
yading@10
|
400 }
|
yading@10
|
401
|
yading@10
|
402 /**
|
yading@10
|
403 * Initialize GetBitContext.
|
yading@10
|
404 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes
|
yading@10
|
405 * larger than the actual read bits because some optimized bitstream
|
yading@10
|
406 * readers read 32 or 64 bit at once and could read over the end
|
yading@10
|
407 * @param byte_size the size of the buffer in bytes
|
yading@10
|
408 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
|
yading@10
|
409 */
|
yading@10
|
410 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
|
yading@10
|
411 int byte_size)
|
yading@10
|
412 {
|
yading@10
|
413 if (byte_size > INT_MAX / 8 || byte_size < 0)
|
yading@10
|
414 byte_size = -1;
|
yading@10
|
415 return init_get_bits(s, buffer, byte_size * 8);
|
yading@10
|
416 }
|
yading@10
|
417
|
yading@10
|
418 static inline const uint8_t *align_get_bits(GetBitContext *s)
|
yading@10
|
419 {
|
yading@10
|
420 int n = -get_bits_count(s) & 7;
|
yading@10
|
421 if (n)
|
yading@10
|
422 skip_bits(s, n);
|
yading@10
|
423 return s->buffer + (s->index >> 3);
|
yading@10
|
424 }
|
yading@10
|
425
|
yading@10
|
426 #define init_vlc(vlc, nb_bits, nb_codes, \
|
yading@10
|
427 bits, bits_wrap, bits_size, \
|
yading@10
|
428 codes, codes_wrap, codes_size, \
|
yading@10
|
429 flags) \
|
yading@10
|
430 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
|
yading@10
|
431 bits, bits_wrap, bits_size, \
|
yading@10
|
432 codes, codes_wrap, codes_size, \
|
yading@10
|
433 NULL, 0, 0, flags)
|
yading@10
|
434
|
yading@10
|
435 int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
|
yading@10
|
436 const void *bits, int bits_wrap, int bits_size,
|
yading@10
|
437 const void *codes, int codes_wrap, int codes_size,
|
yading@10
|
438 const void *symbols, int symbols_wrap, int symbols_size,
|
yading@10
|
439 int flags);
|
yading@10
|
440 void ff_free_vlc(VLC *vlc);
|
yading@10
|
441
|
yading@10
|
442 #define INIT_VLC_LE 2
|
yading@10
|
443 #define INIT_VLC_USE_NEW_STATIC 4
|
yading@10
|
444
|
yading@10
|
445 #define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
|
yading@10
|
446 do { \
|
yading@10
|
447 static VLC_TYPE table[static_size][2]; \
|
yading@10
|
448 (vlc)->table = table; \
|
yading@10
|
449 (vlc)->table_allocated = static_size; \
|
yading@10
|
450 init_vlc(vlc, bits, a, b, c, d, e, f, g, INIT_VLC_USE_NEW_STATIC); \
|
yading@10
|
451 } while (0)
|
yading@10
|
452
|
yading@10
|
453 /**
|
yading@10
|
454 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
|
yading@10
|
455 * If the vlc code is invalid and max_depth>1, then the number of bits removed
|
yading@10
|
456 * is undefined.
|
yading@10
|
457 */
|
yading@10
|
458 #define GET_VLC(code, name, gb, table, bits, max_depth) \
|
yading@10
|
459 do { \
|
yading@10
|
460 int n, nb_bits; \
|
yading@10
|
461 unsigned int index; \
|
yading@10
|
462 \
|
yading@10
|
463 index = SHOW_UBITS(name, gb, bits); \
|
yading@10
|
464 code = table[index][0]; \
|
yading@10
|
465 n = table[index][1]; \
|
yading@10
|
466 \
|
yading@10
|
467 if (max_depth > 1 && n < 0) { \
|
yading@10
|
468 LAST_SKIP_BITS(name, gb, bits); \
|
yading@10
|
469 UPDATE_CACHE(name, gb); \
|
yading@10
|
470 \
|
yading@10
|
471 nb_bits = -n; \
|
yading@10
|
472 \
|
yading@10
|
473 index = SHOW_UBITS(name, gb, nb_bits) + code; \
|
yading@10
|
474 code = table[index][0]; \
|
yading@10
|
475 n = table[index][1]; \
|
yading@10
|
476 if (max_depth > 2 && n < 0) { \
|
yading@10
|
477 LAST_SKIP_BITS(name, gb, nb_bits); \
|
yading@10
|
478 UPDATE_CACHE(name, gb); \
|
yading@10
|
479 \
|
yading@10
|
480 nb_bits = -n; \
|
yading@10
|
481 \
|
yading@10
|
482 index = SHOW_UBITS(name, gb, nb_bits) + code; \
|
yading@10
|
483 code = table[index][0]; \
|
yading@10
|
484 n = table[index][1]; \
|
yading@10
|
485 } \
|
yading@10
|
486 } \
|
yading@10
|
487 SKIP_BITS(name, gb, n); \
|
yading@10
|
488 } while (0)
|
yading@10
|
489
|
yading@10
|
490 #define GET_RL_VLC(level, run, name, gb, table, bits, \
|
yading@10
|
491 max_depth, need_update) \
|
yading@10
|
492 do { \
|
yading@10
|
493 int n, nb_bits; \
|
yading@10
|
494 unsigned int index; \
|
yading@10
|
495 \
|
yading@10
|
496 index = SHOW_UBITS(name, gb, bits); \
|
yading@10
|
497 level = table[index].level; \
|
yading@10
|
498 n = table[index].len; \
|
yading@10
|
499 \
|
yading@10
|
500 if (max_depth > 1 && n < 0) { \
|
yading@10
|
501 SKIP_BITS(name, gb, bits); \
|
yading@10
|
502 if (need_update) { \
|
yading@10
|
503 UPDATE_CACHE(name, gb); \
|
yading@10
|
504 } \
|
yading@10
|
505 \
|
yading@10
|
506 nb_bits = -n; \
|
yading@10
|
507 \
|
yading@10
|
508 index = SHOW_UBITS(name, gb, nb_bits) + level; \
|
yading@10
|
509 level = table[index].level; \
|
yading@10
|
510 n = table[index].len; \
|
yading@10
|
511 } \
|
yading@10
|
512 run = table[index].run; \
|
yading@10
|
513 SKIP_BITS(name, gb, n); \
|
yading@10
|
514 } while (0)
|
yading@10
|
515
|
yading@10
|
516 /**
|
yading@10
|
517 * Parse a vlc code.
|
yading@10
|
518 * @param bits is the number of bits which will be read at once, must be
|
yading@10
|
519 * identical to nb_bits in init_vlc()
|
yading@10
|
520 * @param max_depth is the number of times bits bits must be read to completely
|
yading@10
|
521 * read the longest vlc code
|
yading@10
|
522 * = (max_vlc_length + bits - 1) / bits
|
yading@10
|
523 */
|
yading@10
|
524 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
|
yading@10
|
525 int bits, int max_depth)
|
yading@10
|
526 {
|
yading@10
|
527 int code;
|
yading@10
|
528
|
yading@10
|
529 OPEN_READER(re, s);
|
yading@10
|
530 UPDATE_CACHE(re, s);
|
yading@10
|
531
|
yading@10
|
532 GET_VLC(code, re, s, table, bits, max_depth);
|
yading@10
|
533
|
yading@10
|
534 CLOSE_READER(re, s);
|
yading@10
|
535
|
yading@10
|
536 return code;
|
yading@10
|
537 }
|
yading@10
|
538
|
yading@10
|
539 static inline int decode012(GetBitContext *gb)
|
yading@10
|
540 {
|
yading@10
|
541 int n;
|
yading@10
|
542 n = get_bits1(gb);
|
yading@10
|
543 if (n == 0)
|
yading@10
|
544 return 0;
|
yading@10
|
545 else
|
yading@10
|
546 return get_bits1(gb) + 1;
|
yading@10
|
547 }
|
yading@10
|
548
|
yading@10
|
549 static inline int decode210(GetBitContext *gb)
|
yading@10
|
550 {
|
yading@10
|
551 if (get_bits1(gb))
|
yading@10
|
552 return 0;
|
yading@10
|
553 else
|
yading@10
|
554 return 2 - get_bits1(gb);
|
yading@10
|
555 }
|
yading@10
|
556
|
yading@10
|
557 static inline int get_bits_left(GetBitContext *gb)
|
yading@10
|
558 {
|
yading@10
|
559 return gb->size_in_bits - get_bits_count(gb);
|
yading@10
|
560 }
|
yading@10
|
561
|
yading@10
|
562 //#define TRACE
|
yading@10
|
563
|
yading@10
|
564 #ifdef TRACE
|
yading@10
|
565 static inline void print_bin(int bits, int n)
|
yading@10
|
566 {
|
yading@10
|
567 int i;
|
yading@10
|
568
|
yading@10
|
569 for (i = n - 1; i >= 0; i--)
|
yading@10
|
570 av_log(NULL, AV_LOG_DEBUG, "%d", (bits >> i) & 1);
|
yading@10
|
571 for (i = n; i < 24; i++)
|
yading@10
|
572 av_log(NULL, AV_LOG_DEBUG, " ");
|
yading@10
|
573 }
|
yading@10
|
574
|
yading@10
|
575 static inline int get_bits_trace(GetBitContext *s, int n, const char *file,
|
yading@10
|
576 const char *func, int line)
|
yading@10
|
577 {
|
yading@10
|
578 int r = get_bits(s, n);
|
yading@10
|
579
|
yading@10
|
580 print_bin(r, n);
|
yading@10
|
581 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n",
|
yading@10
|
582 r, n, r, get_bits_count(s) - n, file, func, line);
|
yading@10
|
583
|
yading@10
|
584 return r;
|
yading@10
|
585 }
|
yading@10
|
586
|
yading@10
|
587 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2],
|
yading@10
|
588 int bits, int max_depth, const char *file,
|
yading@10
|
589 const char *func, int line)
|
yading@10
|
590 {
|
yading@10
|
591 int show = show_bits(s, 24);
|
yading@10
|
592 int pos = get_bits_count(s);
|
yading@10
|
593 int r = get_vlc2(s, table, bits, max_depth);
|
yading@10
|
594 int len = get_bits_count(s) - pos;
|
yading@10
|
595 int bits2 = show >> (24 - len);
|
yading@10
|
596
|
yading@10
|
597 print_bin(bits2, len);
|
yading@10
|
598
|
yading@10
|
599 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n",
|
yading@10
|
600 bits2, len, r, pos, file, func, line);
|
yading@10
|
601
|
yading@10
|
602 return r;
|
yading@10
|
603 }
|
yading@10
|
604
|
yading@10
|
605 static inline int get_xbits_trace(GetBitContext *s, int n, const char *file,
|
yading@10
|
606 const char *func, int line)
|
yading@10
|
607 {
|
yading@10
|
608 int show = show_bits(s, n);
|
yading@10
|
609 int r = get_xbits(s, n);
|
yading@10
|
610
|
yading@10
|
611 print_bin(show, n);
|
yading@10
|
612 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n",
|
yading@10
|
613 show, n, r, get_bits_count(s) - n, file, func, line);
|
yading@10
|
614
|
yading@10
|
615 return r;
|
yading@10
|
616 }
|
yading@10
|
617
|
yading@10
|
618 #define get_bits(s, n) get_bits_trace(s , n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
yading@10
|
619 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
yading@10
|
620 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
yading@10
|
621
|
yading@10
|
622 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
yading@10
|
623 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
yading@10
|
624
|
yading@10
|
625 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__)
|
yading@10
|
626
|
yading@10
|
627 #else //TRACE
|
yading@10
|
628 #define tprintf(p, ...) { }
|
yading@10
|
629 #endif
|
yading@10
|
630
|
yading@10
|
631 #endif /* AVCODEC_GET_BITS_H */
|