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