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