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