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