diff options
author | Mohamed Tarek <mt@rockbox.org> | 2009-08-15 22:06:42 +0000 |
---|---|---|
committer | Mohamed Tarek <mt@rockbox.org> | 2009-08-15 22:06:42 +0000 |
commit | adf3f09251db5269214952820732ae39f859ba25 (patch) | |
tree | 0f352b8619b9d1906beaf397cca0bf8350da72a5 /apps/codecs/lib/ffmpeg_bitstream.h | |
parent | 29ac0a6278d6f5967e80b82333d40a46b77e5e64 (diff) | |
download | rockbox-adf3f09251db5269214952820732ae39f859ba25.tar.gz rockbox-adf3f09251db5269214952820732ae39f859ba25.zip |
Move bitstream.[ch] to codecs/lib/ffmpeg_bitstream.[ch] to avoid duplicate copies across codecs.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22337 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/lib/ffmpeg_bitstream.h')
-rw-r--r-- | apps/codecs/lib/ffmpeg_bitstream.h | 963 |
1 files changed, 963 insertions, 0 deletions
diff --git a/apps/codecs/lib/ffmpeg_bitstream.h b/apps/codecs/lib/ffmpeg_bitstream.h new file mode 100644 index 0000000000..f0105a4afe --- /dev/null +++ b/apps/codecs/lib/ffmpeg_bitstream.h | |||
@@ -0,0 +1,963 @@ | |||
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 | #ifndef BITSTREAM_H | ||
22 | #define BITSTREAM_H | ||
23 | |||
24 | #include <inttypes.h> | ||
25 | #include <stdlib.h> | ||
26 | //#include <assert.h> | ||
27 | #include <string.h> | ||
28 | #include <stdio.h> | ||
29 | #include "ffmpeg_bswap.h" | ||
30 | |||
31 | /* The following 2 defines are taken from libavutil/intreadwrite.h */ | ||
32 | #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \ | ||
33 | (((const uint8_t*)(x))[1] << 16) | \ | ||
34 | (((const uint8_t*)(x))[2] << 8) | \ | ||
35 | ((const uint8_t*)(x))[3]) | ||
36 | #define AV_WB32(p, d) do { \ | ||
37 | ((uint8_t*)(p))[3] = (d); \ | ||
38 | ((uint8_t*)(p))[2] = (d)>>8; \ | ||
39 | ((uint8_t*)(p))[1] = (d)>>16; \ | ||
40 | ((uint8_t*)(p))[0] = (d)>>24; } while(0) | ||
41 | |||
42 | #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER) | ||
43 | # define ALT_BITSTREAM_READER | ||
44 | #endif | ||
45 | |||
46 | //#define ALT_BITSTREAM_WRITER | ||
47 | //#define ALIGNED_BITSTREAM_WRITER | ||
48 | #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER) | ||
49 | # if defined(ARCH_ARM) | ||
50 | # define A32_BITSTREAM_READER | ||
51 | # else | ||
52 | # define ALT_BITSTREAM_READER | ||
53 | //#define LIBMPEG2_BITSTREAM_READER | ||
54 | //#define A32_BITSTREAM_READER | ||
55 | # endif | ||
56 | #endif | ||
57 | |||
58 | extern const uint8_t ff_reverse[256]; | ||
59 | |||
60 | #if defined(ARCH_X86) | ||
61 | // avoid +32 for shift optimization (gcc should do that ...) | ||
62 | static inline int32_t NEG_SSR32( int32_t a, int8_t s){ | ||
63 | __asm__ ("sarl %1, %0\n\t" | ||
64 | : "+r" (a) | ||
65 | : "ic" ((uint8_t)(-s)) | ||
66 | ); | ||
67 | return a; | ||
68 | } | ||
69 | static inline uint32_t NEG_USR32(uint32_t a, int8_t s){ | ||
70 | __asm__ ("shrl %1, %0\n\t" | ||
71 | : "+r" (a) | ||
72 | : "ic" ((uint8_t)(-s)) | ||
73 | ); | ||
74 | return a; | ||
75 | } | ||
76 | #else | ||
77 | # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) | ||
78 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) | ||
79 | #endif | ||
80 | |||
81 | /* bit output */ | ||
82 | |||
83 | /* buf and buf_end must be present and used by every alternative writer. */ | ||
84 | typedef struct PutBitContext { | ||
85 | #ifdef ALT_BITSTREAM_WRITER | ||
86 | uint8_t *buf, *buf_end; | ||
87 | int index; | ||
88 | #else | ||
89 | uint32_t bit_buf; | ||
90 | int bit_left; | ||
91 | uint8_t *buf, *buf_ptr, *buf_end; | ||
92 | #endif | ||
93 | int size_in_bits; | ||
94 | } PutBitContext; | ||
95 | |||
96 | static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) | ||
97 | { | ||
98 | if(buffer_size < 0) { | ||
99 | buffer_size = 0; | ||
100 | buffer = NULL; | ||
101 | } | ||
102 | |||
103 | s->size_in_bits= 8*buffer_size; | ||
104 | s->buf = buffer; | ||
105 | s->buf_end = s->buf + buffer_size; | ||
106 | #ifdef ALT_BITSTREAM_WRITER | ||
107 | s->index=0; | ||
108 | ((uint32_t*)(s->buf))[0]=0; | ||
109 | // memset(buffer, 0, buffer_size); | ||
110 | #else | ||
111 | s->buf_ptr = s->buf; | ||
112 | s->bit_left=32; | ||
113 | s->bit_buf=0; | ||
114 | #endif | ||
115 | } | ||
116 | |||
117 | /* return the number of bits output */ | ||
118 | static inline int put_bits_count(PutBitContext *s) | ||
119 | { | ||
120 | #ifdef ALT_BITSTREAM_WRITER | ||
121 | return s->index; | ||
122 | #else | ||
123 | return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left; | ||
124 | #endif | ||
125 | } | ||
126 | |||
127 | /* pad the end of the output stream with zeros */ | ||
128 | static inline void flush_put_bits(PutBitContext *s) | ||
129 | { | ||
130 | #ifdef ALT_BITSTREAM_WRITER | ||
131 | align_put_bits(s); | ||
132 | #else | ||
133 | #ifndef BITSTREAM_WRITER_LE | ||
134 | s->bit_buf<<= s->bit_left; | ||
135 | #endif | ||
136 | while (s->bit_left < 32) { | ||
137 | /* XXX: should test end of buffer */ | ||
138 | #ifdef BITSTREAM_WRITER_LE | ||
139 | *s->buf_ptr++=s->bit_buf; | ||
140 | s->bit_buf>>=8; | ||
141 | #else | ||
142 | *s->buf_ptr++=s->bit_buf >> 24; | ||
143 | s->bit_buf<<=8; | ||
144 | #endif | ||
145 | s->bit_left+=8; | ||
146 | } | ||
147 | s->bit_left=32; | ||
148 | s->bit_buf=0; | ||
149 | #endif | ||
150 | } | ||
151 | |||
152 | void align_put_bits(PutBitContext *s); | ||
153 | void ff_put_string(PutBitContext * pbc, const char *s, int put_zero); | ||
154 | void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length); | ||
155 | |||
156 | /* bit input */ | ||
157 | /* buffer, buffer_end and size_in_bits must be present and used by every reader */ | ||
158 | typedef struct GetBitContext { | ||
159 | const uint8_t *buffer, *buffer_end; | ||
160 | #ifdef ALT_BITSTREAM_READER | ||
161 | int index; | ||
162 | #elif defined LIBMPEG2_BITSTREAM_READER | ||
163 | uint8_t *buffer_ptr; | ||
164 | uint32_t cache; | ||
165 | int bit_count; | ||
166 | #elif defined A32_BITSTREAM_READER | ||
167 | uint32_t *buffer_ptr; | ||
168 | uint32_t cache0; | ||
169 | uint32_t cache1; | ||
170 | int bit_count; | ||
171 | #endif | ||
172 | int size_in_bits; | ||
173 | } GetBitContext; | ||
174 | |||
175 | #define VLC_TYPE int16_t | ||
176 | |||
177 | typedef struct VLC { | ||
178 | int bits; | ||
179 | VLC_TYPE (*table)[2]; ///< code, bits | ||
180 | int table_size, table_allocated; | ||
181 | } VLC; | ||
182 | |||
183 | typedef struct RL_VLC_ELEM { | ||
184 | int16_t level; | ||
185 | int8_t len; | ||
186 | uint8_t run; | ||
187 | } RL_VLC_ELEM; | ||
188 | |||
189 | #ifndef ALT_BITSTREAM_WRITER | ||
190 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) | ||
191 | { | ||
192 | unsigned int bit_buf; | ||
193 | int bit_left; | ||
194 | |||
195 | // printf("put_bits=%d %x\n", n, value); | ||
196 | //assert(n == 32 || value < (1U << n)); | ||
197 | |||
198 | bit_buf = s->bit_buf; | ||
199 | bit_left = s->bit_left; | ||
200 | |||
201 | // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); | ||
202 | /* XXX: optimize */ | ||
203 | #ifdef BITSTREAM_WRITER_LE | ||
204 | bit_buf |= value << (32 - bit_left); | ||
205 | if (n >= bit_left) { | ||
206 | #if !HAVE_FAST_UNALIGNED | ||
207 | if (3 & (intptr_t) s->buf_ptr) { | ||
208 | AV_WL32(s->buf_ptr, bit_buf); | ||
209 | } else | ||
210 | #endif | ||
211 | *(uint32_t *)s->buf_ptr = le2me_32(bit_buf); | ||
212 | s->buf_ptr+=4; | ||
213 | bit_buf = (bit_left==32)?0:value >> bit_left; | ||
214 | bit_left+=32; | ||
215 | } | ||
216 | bit_left-=n; | ||
217 | #else | ||
218 | if (n < bit_left) { | ||
219 | bit_buf = (bit_buf<<n) | value; | ||
220 | bit_left-=n; | ||
221 | } else { | ||
222 | bit_buf<<=bit_left; | ||
223 | bit_buf |= value >> (n - bit_left); | ||
224 | #if !defined(HAVE_FAST_UNALIGNED) | ||
225 | if (3 & (intptr_t) s->buf_ptr) { | ||
226 | AV_WB32(s->buf_ptr, bit_buf); | ||
227 | } else | ||
228 | #endif | ||
229 | *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); | ||
230 | //printf("bitbuf = %08x\n", bit_buf); | ||
231 | s->buf_ptr+=4; | ||
232 | bit_left+=32 - n; | ||
233 | bit_buf = value; | ||
234 | } | ||
235 | #endif | ||
236 | |||
237 | s->bit_buf = bit_buf; | ||
238 | s->bit_left = bit_left; | ||
239 | } | ||
240 | #endif | ||
241 | |||
242 | |||
243 | #ifdef ALT_BITSTREAM_WRITER | ||
244 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) | ||
245 | { | ||
246 | # ifdef ALIGNED_BITSTREAM_WRITER | ||
247 | # if ARCH_X86 | ||
248 | __asm__ volatile( | ||
249 | "movl %0, %%ecx \n\t" | ||
250 | "xorl %%eax, %%eax \n\t" | ||
251 | "shrdl %%cl, %1, %%eax \n\t" | ||
252 | "shrl %%cl, %1 \n\t" | ||
253 | "movl %0, %%ecx \n\t" | ||
254 | "shrl $3, %%ecx \n\t" | ||
255 | "andl $0xFFFFFFFC, %%ecx \n\t" | ||
256 | "bswapl %1 \n\t" | ||
257 | "orl %1, (%2, %%ecx) \n\t" | ||
258 | "bswapl %%eax \n\t" | ||
259 | "addl %3, %0 \n\t" | ||
260 | "movl %%eax, 4(%2, %%ecx) \n\t" | ||
261 | : "=&r" (s->index), "=&r" (value) | ||
262 | : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n)) | ||
263 | : "%eax", "%ecx" | ||
264 | ); | ||
265 | # else | ||
266 | int index= s->index; | ||
267 | uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5); | ||
268 | |||
269 | value<<= 32-n; | ||
270 | |||
271 | ptr[0] |= be2me_32(value>>(index&31)); | ||
272 | ptr[1] = be2me_32(value<<(32-(index&31))); | ||
273 | //if(n>24) printf("%d %d\n", n, value); | ||
274 | index+= n; | ||
275 | s->index= index; | ||
276 | # endif | ||
277 | # else //ALIGNED_BITSTREAM_WRITER | ||
278 | # if ARCH_X86 | ||
279 | __asm__ volatile( | ||
280 | "movl $7, %%ecx \n\t" | ||
281 | "andl %0, %%ecx \n\t" | ||
282 | "addl %3, %%ecx \n\t" | ||
283 | "negl %%ecx \n\t" | ||
284 | "shll %%cl, %1 \n\t" | ||
285 | "bswapl %1 \n\t" | ||
286 | "movl %0, %%ecx \n\t" | ||
287 | "shrl $3, %%ecx \n\t" | ||
288 | "orl %1, (%%ecx, %2) \n\t" | ||
289 | "addl %3, %0 \n\t" | ||
290 | "movl $0, 4(%%ecx, %2) \n\t" | ||
291 | : "=&r" (s->index), "=&r" (value) | ||
292 | : "r" (s->buf), "r" (n), "0" (s->index), "1" (value) | ||
293 | : "%ecx" | ||
294 | ); | ||
295 | # else | ||
296 | int index= s->index; | ||
297 | uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3)); | ||
298 | |||
299 | ptr[0] |= be2me_32(value<<(32-n-(index&7) )); | ||
300 | ptr[1] = 0; | ||
301 | //if(n>24) printf("%d %d\n", n, value); | ||
302 | index+= n; | ||
303 | s->index= index; | ||
304 | # endif | ||
305 | # endif //!ALIGNED_BITSTREAM_WRITER | ||
306 | } | ||
307 | #endif | ||
308 | |||
309 | static inline void put_sbits(PutBitContext *pb, int bits, int32_t val) | ||
310 | { | ||
311 | //assert(bits >= 0 && bits <= 31); | ||
312 | |||
313 | put_bits(pb, bits, val & ((1<<bits)-1)); | ||
314 | } | ||
315 | |||
316 | |||
317 | static inline uint8_t* pbBufPtr(PutBitContext *s) | ||
318 | { | ||
319 | #ifdef ALT_BITSTREAM_WRITER | ||
320 | return s->buf + (s->index>>3); | ||
321 | #else | ||
322 | return s->buf_ptr; | ||
323 | #endif | ||
324 | } | ||
325 | |||
326 | /** | ||
327 | * | ||
328 | * PutBitContext must be flushed & aligned to a byte boundary before calling this. | ||
329 | */ | ||
330 | static inline void skip_put_bytes(PutBitContext *s, int n){ | ||
331 | //assert((put_bits_count(s)&7)==0); | ||
332 | #ifdef ALT_BITSTREAM_WRITER | ||
333 | FIXME may need some cleaning of the buffer | ||
334 | s->index += n<<3; | ||
335 | #else | ||
336 | //assert(s->bit_left==32); | ||
337 | s->buf_ptr += n; | ||
338 | #endif | ||
339 | } | ||
340 | |||
341 | /** | ||
342 | * Skips the given number of bits. | ||
343 | * Must only be used if the actual values in the bitstream do not matter. | ||
344 | */ | ||
345 | static inline void skip_put_bits(PutBitContext *s, int n){ | ||
346 | #ifdef ALT_BITSTREAM_WRITER | ||
347 | s->index += n; | ||
348 | #else | ||
349 | s->bit_left -= n; | ||
350 | s->buf_ptr-= s->bit_left>>5; | ||
351 | s->bit_left &= 31; | ||
352 | #endif | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * Changes the end of the buffer. | ||
357 | */ | ||
358 | static inline void set_put_bits_buffer_size(PutBitContext *s, int size){ | ||
359 | s->buf_end= s->buf + size; | ||
360 | } | ||
361 | |||
362 | /* Bitstream reader API docs: | ||
363 | name | ||
364 | arbitrary name which is used as prefix for the internal variables | ||
365 | |||
366 | gb | ||
367 | getbitcontext | ||
368 | |||
369 | OPEN_READER(name, gb) | ||
370 | loads gb into local variables | ||
371 | |||
372 | CLOSE_READER(name, gb) | ||
373 | stores local vars in gb | ||
374 | |||
375 | UPDATE_CACHE(name, gb) | ||
376 | refills the internal cache from the bitstream | ||
377 | after this call at least MIN_CACHE_BITS will be available, | ||
378 | |||
379 | GET_CACHE(name, gb) | ||
380 | will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) | ||
381 | |||
382 | SHOW_UBITS(name, gb, num) | ||
383 | will return the next num bits | ||
384 | |||
385 | SHOW_SBITS(name, gb, num) | ||
386 | will return the next num bits and do sign extension | ||
387 | |||
388 | SKIP_BITS(name, gb, num) | ||
389 | will skip over the next num bits | ||
390 | note, this is equivalent to SKIP_CACHE; SKIP_COUNTER | ||
391 | |||
392 | SKIP_CACHE(name, gb, num) | ||
393 | will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) | ||
394 | |||
395 | SKIP_COUNTER(name, gb, num) | ||
396 | will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) | ||
397 | |||
398 | LAST_SKIP_CACHE(name, gb, num) | ||
399 | will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing | ||
400 | |||
401 | LAST_SKIP_BITS(name, gb, num) | ||
402 | is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER | ||
403 | |||
404 | for examples see get_bits, show_bits, skip_bits, get_vlc | ||
405 | */ | ||
406 | |||
407 | #ifdef ALT_BITSTREAM_READER | ||
408 | # define MIN_CACHE_BITS 25 | ||
409 | |||
410 | # define OPEN_READER(name, gb)\ | ||
411 | int name##_index= (gb)->index;\ | ||
412 | int name##_cache= 0;\ | ||
413 | |||
414 | # define CLOSE_READER(name, gb)\ | ||
415 | (gb)->index= name##_index;\ | ||
416 | |||
417 | # ifdef ALT_BITSTREAM_READER_LE | ||
418 | # define UPDATE_CACHE(name, gb)\ | ||
419 | name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\ | ||
420 | |||
421 | # define SKIP_CACHE(name, gb, num)\ | ||
422 | name##_cache >>= (num); | ||
423 | # else | ||
424 | # define UPDATE_CACHE(name, gb)\ | ||
425 | name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ | ||
426 | |||
427 | # define SKIP_CACHE(name, gb, num)\ | ||
428 | name##_cache <<= (num); | ||
429 | # endif | ||
430 | |||
431 | // FIXME name? | ||
432 | # define SKIP_COUNTER(name, gb, num)\ | ||
433 | name##_index += (num);\ | ||
434 | |||
435 | # define SKIP_BITS(name, gb, num)\ | ||
436 | {\ | ||
437 | SKIP_CACHE(name, gb, num)\ | ||
438 | SKIP_COUNTER(name, gb, num)\ | ||
439 | }\ | ||
440 | |||
441 | # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | ||
442 | # define LAST_SKIP_CACHE(name, gb, num) ; | ||
443 | |||
444 | # ifdef ALT_BITSTREAM_READER_LE | ||
445 | # define SHOW_UBITS(name, gb, num)\ | ||
446 | ((name##_cache) & (NEG_USR32(0xffffffff,num))) | ||
447 | |||
448 | # define SHOW_SBITS(name, gb, num)\ | ||
449 | NEG_SSR32((name##_cache)<<(32-(num)), num) | ||
450 | # else | ||
451 | # define SHOW_UBITS(name, gb, num)\ | ||
452 | NEG_USR32(name##_cache, num) | ||
453 | |||
454 | # define SHOW_SBITS(name, gb, num)\ | ||
455 | NEG_SSR32(name##_cache, num) | ||
456 | # endif | ||
457 | |||
458 | # define GET_CACHE(name, gb)\ | ||
459 | ((uint32_t)name##_cache) | ||
460 | |||
461 | static inline int get_bits_count(GetBitContext *s){ | ||
462 | return s->index; | ||
463 | } | ||
464 | |||
465 | static inline void skip_bits_long(GetBitContext *s, int n){ | ||
466 | s->index += n; | ||
467 | } | ||
468 | |||
469 | #elif defined LIBMPEG2_BITSTREAM_READER | ||
470 | //libmpeg2 like reader | ||
471 | |||
472 | # define MIN_CACHE_BITS 17 | ||
473 | |||
474 | # define OPEN_READER(name, gb)\ | ||
475 | int name##_bit_count=(gb)->bit_count;\ | ||
476 | int name##_cache= (gb)->cache;\ | ||
477 | uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | ||
478 | |||
479 | # define CLOSE_READER(name, gb)\ | ||
480 | (gb)->bit_count= name##_bit_count;\ | ||
481 | (gb)->cache= name##_cache;\ | ||
482 | (gb)->buffer_ptr= name##_buffer_ptr;\ | ||
483 | |||
484 | # define UPDATE_CACHE(name, gb)\ | ||
485 | if(name##_bit_count >= 0){\ | ||
486 | name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \ | ||
487 | name##_buffer_ptr+=2;\ | ||
488 | name##_bit_count-= 16;\ | ||
489 | }\ | ||
490 | |||
491 | # define SKIP_CACHE(name, gb, num)\ | ||
492 | name##_cache <<= (num);\ | ||
493 | |||
494 | # define SKIP_COUNTER(name, gb, num)\ | ||
495 | name##_bit_count += (num);\ | ||
496 | |||
497 | # define SKIP_BITS(name, gb, num)\ | ||
498 | {\ | ||
499 | SKIP_CACHE(name, gb, num)\ | ||
500 | SKIP_COUNTER(name, gb, num)\ | ||
501 | }\ | ||
502 | |||
503 | # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | ||
504 | # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | ||
505 | |||
506 | # define SHOW_UBITS(name, gb, num)\ | ||
507 | NEG_USR32(name##_cache, num) | ||
508 | |||
509 | # define SHOW_SBITS(name, gb, num)\ | ||
510 | NEG_SSR32(name##_cache, num) | ||
511 | |||
512 | # define GET_CACHE(name, gb)\ | ||
513 | ((uint32_t)name##_cache) | ||
514 | |||
515 | static inline int get_bits_count(GetBitContext *s){ | ||
516 | return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; | ||
517 | } | ||
518 | |||
519 | static inline void skip_bits_long(GetBitContext *s, int n){ | ||
520 | OPEN_READER(re, s) | ||
521 | re_bit_count += n; | ||
522 | re_buffer_ptr += 2*(re_bit_count>>4); | ||
523 | re_bit_count &= 15; | ||
524 | re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count); | ||
525 | UPDATE_CACHE(re, s) | ||
526 | CLOSE_READER(re, s) | ||
527 | } | ||
528 | |||
529 | #elif defined A32_BITSTREAM_READER | ||
530 | |||
531 | # define MIN_CACHE_BITS 32 | ||
532 | |||
533 | # define OPEN_READER(name, gb)\ | ||
534 | int name##_bit_count=(gb)->bit_count;\ | ||
535 | uint32_t name##_cache0= (gb)->cache0;\ | ||
536 | uint32_t name##_cache1= (gb)->cache1;\ | ||
537 | uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | ||
538 | |||
539 | # define CLOSE_READER(name, gb)\ | ||
540 | (gb)->bit_count= name##_bit_count;\ | ||
541 | (gb)->cache0= name##_cache0;\ | ||
542 | (gb)->cache1= name##_cache1;\ | ||
543 | (gb)->buffer_ptr= name##_buffer_ptr;\ | ||
544 | |||
545 | # define UPDATE_CACHE(name, gb)\ | ||
546 | if(name##_bit_count > 0){\ | ||
547 | const uint32_t next= be2me_32( *name##_buffer_ptr );\ | ||
548 | name##_cache0 |= NEG_USR32(next,name##_bit_count);\ | ||
549 | name##_cache1 |= next<<name##_bit_count;\ | ||
550 | name##_buffer_ptr++;\ | ||
551 | name##_bit_count-= 32;\ | ||
552 | }\ | ||
553 | |||
554 | #if ARCH_X86 | ||
555 | # define SKIP_CACHE(name, gb, num)\ | ||
556 | __asm__(\ | ||
557 | "shldl %2, %1, %0 \n\t"\ | ||
558 | "shll %2, %1 \n\t"\ | ||
559 | : "+r" (name##_cache0), "+r" (name##_cache1)\ | ||
560 | : "Ic" ((uint8_t)(num))\ | ||
561 | ); | ||
562 | #else | ||
563 | # define SKIP_CACHE(name, gb, num)\ | ||
564 | name##_cache0 <<= (num);\ | ||
565 | name##_cache0 |= NEG_USR32(name##_cache1,num);\ | ||
566 | name##_cache1 <<= (num); | ||
567 | #endif | ||
568 | |||
569 | # define SKIP_COUNTER(name, gb, num)\ | ||
570 | name##_bit_count += (num);\ | ||
571 | |||
572 | # define SKIP_BITS(name, gb, num)\ | ||
573 | {\ | ||
574 | SKIP_CACHE(name, gb, num)\ | ||
575 | SKIP_COUNTER(name, gb, num)\ | ||
576 | }\ | ||
577 | |||
578 | # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | ||
579 | # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | ||
580 | |||
581 | # define SHOW_UBITS(name, gb, num)\ | ||
582 | NEG_USR32(name##_cache0, num) | ||
583 | |||
584 | # define SHOW_SBITS(name, gb, num)\ | ||
585 | NEG_SSR32(name##_cache0, num) | ||
586 | |||
587 | # define GET_CACHE(name, gb)\ | ||
588 | (name##_cache0) | ||
589 | |||
590 | static inline int get_bits_count(GetBitContext *s){ | ||
591 | return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; | ||
592 | } | ||
593 | |||
594 | static inline void skip_bits_long(GetBitContext *s, int n){ | ||
595 | OPEN_READER(re, s) | ||
596 | re_bit_count += n; | ||
597 | re_buffer_ptr += re_bit_count>>5; | ||
598 | re_bit_count &= 31; | ||
599 | re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count; | ||
600 | re_cache1 = 0; | ||
601 | UPDATE_CACHE(re, s) | ||
602 | CLOSE_READER(re, s) | ||
603 | } | ||
604 | |||
605 | #endif | ||
606 | |||
607 | /** | ||
608 | * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). | ||
609 | * if MSB not set it is negative | ||
610 | * @param n length in bits | ||
611 | * @author BERO | ||
612 | */ | ||
613 | static inline int get_xbits(GetBitContext *s, int n){ | ||
614 | register int sign; | ||
615 | register int32_t cache; | ||
616 | OPEN_READER(re, s) | ||
617 | UPDATE_CACHE(re, s) | ||
618 | cache = GET_CACHE(re,s); | ||
619 | sign=(~cache)>>31; | ||
620 | LAST_SKIP_BITS(re, s, n) | ||
621 | CLOSE_READER(re, s) | ||
622 | return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | ||
623 | } | ||
624 | |||
625 | static inline int get_sbits(GetBitContext *s, int n){ | ||
626 | register int tmp; | ||
627 | OPEN_READER(re, s) | ||
628 | UPDATE_CACHE(re, s) | ||
629 | tmp= SHOW_SBITS(re, s, n); | ||
630 | LAST_SKIP_BITS(re, s, n) | ||
631 | CLOSE_READER(re, s) | ||
632 | return tmp; | ||
633 | } | ||
634 | |||
635 | /** | ||
636 | * reads 1-17 bits. | ||
637 | * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | ||
638 | */ | ||
639 | static inline unsigned int get_bits(GetBitContext *s, int n){ | ||
640 | register int tmp; | ||
641 | OPEN_READER(re, s) | ||
642 | UPDATE_CACHE(re, s) | ||
643 | tmp= SHOW_UBITS(re, s, n); | ||
644 | LAST_SKIP_BITS(re, s, n) | ||
645 | CLOSE_READER(re, s) | ||
646 | return tmp; | ||
647 | } | ||
648 | |||
649 | /** | ||
650 | * shows 1-17 bits. | ||
651 | * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | ||
652 | */ | ||
653 | static inline unsigned int show_bits(GetBitContext *s, int n){ | ||
654 | register int tmp; | ||
655 | OPEN_READER(re, s) | ||
656 | UPDATE_CACHE(re, s) | ||
657 | tmp= SHOW_UBITS(re, s, n); | ||
658 | // CLOSE_READER(re, s) | ||
659 | return tmp; | ||
660 | } | ||
661 | |||
662 | static inline void skip_bits(GetBitContext *s, int n){ | ||
663 | //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) | ||
664 | OPEN_READER(re, s) | ||
665 | UPDATE_CACHE(re, s) | ||
666 | LAST_SKIP_BITS(re, s, n) | ||
667 | CLOSE_READER(re, s) | ||
668 | } | ||
669 | |||
670 | static inline unsigned int get_bits1(GetBitContext *s){ | ||
671 | #ifdef ALT_BITSTREAM_READER | ||
672 | int index= s->index; | ||
673 | uint8_t result= s->buffer[ index>>3 ]; | ||
674 | #ifdef ALT_BITSTREAM_READER_LE | ||
675 | result>>= (index&0x07); | ||
676 | result&= 1; | ||
677 | #else | ||
678 | result<<= (index&0x07); | ||
679 | result>>= 8 - 1; | ||
680 | #endif | ||
681 | index++; | ||
682 | s->index= index; | ||
683 | |||
684 | return result; | ||
685 | #else | ||
686 | return get_bits(s, 1); | ||
687 | #endif | ||
688 | } | ||
689 | |||
690 | static inline unsigned int show_bits1(GetBitContext *s){ | ||
691 | return show_bits(s, 1); | ||
692 | } | ||
693 | |||
694 | static inline void skip_bits1(GetBitContext *s){ | ||
695 | skip_bits(s, 1); | ||
696 | } | ||
697 | |||
698 | /** | ||
699 | * reads 0-32 bits. | ||
700 | */ | ||
701 | static inline unsigned int get_bits_long(GetBitContext *s, int n){ | ||
702 | if(n<=17) return get_bits(s, n); | ||
703 | else{ | ||
704 | #ifdef ALT_BITSTREAM_READER_LE | ||
705 | int ret= get_bits(s, 16); | ||
706 | return ret | (get_bits(s, n-16) << 16); | ||
707 | #else | ||
708 | int ret= get_bits(s, 16) << (n-16); | ||
709 | return ret | get_bits(s, n-16); | ||
710 | #endif | ||
711 | } | ||
712 | } | ||
713 | |||
714 | #if 0 | ||
715 | /** | ||
716 | * reads 0-32 bits as a signed integer. | ||
717 | */ | ||
718 | static inline int get_sbits_long(GetBitContext *s, int n) { | ||
719 | return sign_extend(get_bits_long(s, n), n); | ||
720 | } | ||
721 | #endif | ||
722 | |||
723 | /** | ||
724 | * shows 0-32 bits. | ||
725 | */ | ||
726 | static inline unsigned int show_bits_long(GetBitContext *s, int n){ | ||
727 | if(n<=17) return show_bits(s, n); | ||
728 | else{ | ||
729 | GetBitContext gb= *s; | ||
730 | return get_bits_long(&gb, n); | ||
731 | } | ||
732 | } | ||
733 | |||
734 | #if 0 | ||
735 | static inline int check_marker(GetBitContext *s, const char *msg) | ||
736 | { | ||
737 | int bit= get_bits1(s); | ||
738 | if(!bit) | ||
739 | printf("Marker bit missing %s\n", msg); | ||
740 | |||
741 | return bit; | ||
742 | } | ||
743 | #endif | ||
744 | |||
745 | /** | ||
746 | * init GetBitContext. | ||
747 | * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits | ||
748 | * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end | ||
749 | * @param bit_size the size of the buffer in bits | ||
750 | */ | ||
751 | static inline void init_get_bits(GetBitContext *s, | ||
752 | const uint8_t *buffer, int bit_size) | ||
753 | { | ||
754 | int buffer_size= (bit_size+7)>>3; | ||
755 | if(buffer_size < 0 || bit_size < 0) { | ||
756 | buffer_size = bit_size = 0; | ||
757 | buffer = NULL; | ||
758 | } | ||
759 | |||
760 | s->buffer= buffer; | ||
761 | s->size_in_bits= bit_size; | ||
762 | s->buffer_end= buffer + buffer_size; | ||
763 | #ifdef ALT_BITSTREAM_READER | ||
764 | s->index=0; | ||
765 | #elif defined LIBMPEG2_BITSTREAM_READER | ||
766 | s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1)); | ||
767 | s->bit_count = 16 + 8*((intptr_t)buffer&1); | ||
768 | skip_bits_long(s, 0); | ||
769 | #elif defined A32_BITSTREAM_READER | ||
770 | s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3)); | ||
771 | s->bit_count = 32 + 8*((intptr_t)buffer&3); | ||
772 | skip_bits_long(s, 0); | ||
773 | #endif | ||
774 | } | ||
775 | |||
776 | static inline void align_get_bits(GetBitContext *s) | ||
777 | { | ||
778 | int n= (-get_bits_count(s)) & 7; | ||
779 | if(n) skip_bits(s, n); | ||
780 | } | ||
781 | |||
782 | #define init_vlc(vlc, nb_bits, nb_codes,\ | ||
783 | bits, bits_wrap, bits_size,\ | ||
784 | codes, codes_wrap, codes_size,\ | ||
785 | flags)\ | ||
786 | init_vlc_sparse(vlc, nb_bits, nb_codes,\ | ||
787 | bits, bits_wrap, bits_size,\ | ||
788 | codes, codes_wrap, codes_size,\ | ||
789 | NULL, 0, 0, flags) | ||
790 | |||
791 | int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, | ||
792 | const void *bits, int bits_wrap, int bits_size, | ||
793 | const void *codes, int codes_wrap, int codes_size, | ||
794 | const void *symbols, int symbols_wrap, int symbols_size, | ||
795 | int flags); | ||
796 | #define INIT_VLC_USE_STATIC 1 ///< VERY strongly deprecated and forbidden | ||
797 | #define INIT_VLC_LE 2 | ||
798 | #define INIT_VLC_USE_NEW_STATIC 4 | ||
799 | void free_vlc(VLC *vlc); | ||
800 | |||
801 | #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\ | ||
802 | {\ | ||
803 | static VLC_TYPE table[static_size][2];\ | ||
804 | (vlc)->table= table;\ | ||
805 | (vlc)->table_allocated= static_size;\ | ||
806 | init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\ | ||
807 | } | ||
808 | |||
809 | |||
810 | /** | ||
811 | * | ||
812 | * if the vlc code is invalid and max_depth=1 than no bits will be removed | ||
813 | * if the vlc code is invalid and max_depth>1 than the number of bits removed | ||
814 | * is undefined | ||
815 | */ | ||
816 | #define GET_VLC(code, name, gb, table, bits, max_depth)\ | ||
817 | {\ | ||
818 | int n, index, nb_bits;\ | ||
819 | \ | ||
820 | index= SHOW_UBITS(name, gb, bits);\ | ||
821 | code = table[index][0];\ | ||
822 | n = table[index][1];\ | ||
823 | \ | ||
824 | if(max_depth > 1 && n < 0){\ | ||
825 | LAST_SKIP_BITS(name, gb, bits)\ | ||
826 | UPDATE_CACHE(name, gb)\ | ||
827 | \ | ||
828 | nb_bits = -n;\ | ||
829 | \ | ||
830 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ | ||
831 | code = table[index][0];\ | ||
832 | n = table[index][1];\ | ||
833 | if(max_depth > 2 && n < 0){\ | ||
834 | LAST_SKIP_BITS(name, gb, nb_bits)\ | ||
835 | UPDATE_CACHE(name, gb)\ | ||
836 | \ | ||
837 | nb_bits = -n;\ | ||
838 | \ | ||
839 | index= SHOW_UBITS(name, gb, nb_bits) + code;\ | ||
840 | code = table[index][0];\ | ||
841 | n = table[index][1];\ | ||
842 | }\ | ||
843 | }\ | ||
844 | SKIP_BITS(name, gb, n)\ | ||
845 | } | ||
846 | |||
847 | #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\ | ||
848 | {\ | ||
849 | int n, index, nb_bits;\ | ||
850 | \ | ||
851 | index= SHOW_UBITS(name, gb, bits);\ | ||
852 | level = table[index].level;\ | ||
853 | n = table[index].len;\ | ||
854 | \ | ||
855 | if(max_depth > 1 && n < 0){\ | ||
856 | SKIP_BITS(name, gb, bits)\ | ||
857 | if(need_update){\ | ||
858 | UPDATE_CACHE(name, gb)\ | ||
859 | }\ | ||
860 | \ | ||
861 | nb_bits = -n;\ | ||
862 | \ | ||
863 | index= SHOW_UBITS(name, gb, nb_bits) + level;\ | ||
864 | level = table[index].level;\ | ||
865 | n = table[index].len;\ | ||
866 | }\ | ||
867 | run= table[index].run;\ | ||
868 | SKIP_BITS(name, gb, n)\ | ||
869 | } | ||
870 | |||
871 | |||
872 | /** | ||
873 | * parses a vlc code, faster then get_vlc() | ||
874 | * @param bits is the number of bits which will be read at once, must be | ||
875 | * identical to nb_bits in init_vlc() | ||
876 | * @param max_depth is the number of times bits bits must be read to completely | ||
877 | * read the longest vlc code | ||
878 | * = (max_vlc_length + bits - 1) / bits | ||
879 | */ | ||
880 | static inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], | ||
881 | int bits, int max_depth) | ||
882 | { | ||
883 | int code; | ||
884 | |||
885 | OPEN_READER(re, s) | ||
886 | UPDATE_CACHE(re, s) | ||
887 | |||
888 | GET_VLC(code, re, s, table, bits, max_depth) | ||
889 | |||
890 | CLOSE_READER(re, s) | ||
891 | return code; | ||
892 | } | ||
893 | |||
894 | //#define TRACE | ||
895 | |||
896 | #ifdef TRACE | ||
897 | static inline void print_bin(int bits, int n){ | ||
898 | int i; | ||
899 | |||
900 | for(i=n-1; i>=0; i--){ | ||
901 | printf("%d", (bits>>i)&1); | ||
902 | } | ||
903 | for(i=n; i<24; i++) | ||
904 | printf(" "); | ||
905 | } | ||
906 | |||
907 | static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ | ||
908 | int r= get_bits(s, n); | ||
909 | |||
910 | print_bin(r, n); | ||
911 | printf("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line); | ||
912 | return r; | ||
913 | } | ||
914 | static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){ | ||
915 | int show= show_bits(s, 24); | ||
916 | int pos= get_bits_count(s); | ||
917 | int r= get_vlc2(s, table, bits, max_depth); | ||
918 | int len= get_bits_count(s) - pos; | ||
919 | int bits2= show>>(24-len); | ||
920 | |||
921 | print_bin(bits2, len); | ||
922 | |||
923 | printf("%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line); | ||
924 | return r; | ||
925 | } | ||
926 | static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ | ||
927 | int show= show_bits(s, n); | ||
928 | int r= get_xbits(s, n); | ||
929 | |||
930 | print_bin(show, n); | ||
931 | printf("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line); | ||
932 | return r; | ||
933 | } | ||
934 | |||
935 | #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) | ||
936 | #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__) | ||
937 | #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) | ||
938 | #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) | ||
939 | #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) | ||
940 | |||
941 | #define tprintf(p, ...) printf | ||
942 | |||
943 | #else //TRACE | ||
944 | #define tprintf(p, ...) {} | ||
945 | #endif | ||
946 | |||
947 | static inline int decode012(GetBitContext *gb){ | ||
948 | int n; | ||
949 | n = get_bits1(gb); | ||
950 | if (n == 0) | ||
951 | return 0; | ||
952 | else | ||
953 | return get_bits1(gb) + 1; | ||
954 | } | ||
955 | |||
956 | static inline int decode210(GetBitContext *gb){ | ||
957 | if (get_bits1(gb)) | ||
958 | return 0; | ||
959 | else | ||
960 | return 2 - get_bits1(gb); | ||
961 | } | ||
962 | |||
963 | #endif /* BITSTREAM_H */ | ||