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