summaryrefslogtreecommitdiff
path: root/audio_codec/libflac/get_bits.h (plain)
blob: f1f60274cc5f9b89d45d387d9b660557ec8ed2b7
1/*
2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file libavcodec/get_bits.h
23 * bitstream reader API header.
24 */
25
26#ifndef AVCODEC_GET_BITS_H
27#define AVCODEC_GET_BITS_H
28
29#include "mathops.h"
30#include "bswap.h"
31//#include <ffmpeg/libavutil/common.h>
32#include "intreadwrite.h"
33//#include <core/dsp.h>
34
35#include <android/log.h>
36
37#define LOG_TAG "get_bits"
38
39#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
40#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
41
42//goes the A32_BITSTREAM_READER passage
43#if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER)
44#define ALT_BITSTREAM_READER
45#endif
46
47#if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER)
48#if defined(ARCH_ARM) && !defined(HAVE_FAST_UNALIGNED)
49#define A32_BITSTREAM_READER
50#else
51#define ALT_BITSTREAM_READER
52//#define LIBMPEG2_BITSTREAM_READER
53//#define A32_BITSTREAM_READER
54#endif
55#endif
56
57#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
58#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
59
60/* bit input */
61/* buffer, buffer_end and size_in_bits must be present and used by every reader */
62typedef struct GetBitContext {
63 const uint8_t *buffer, *buffer_end;
64#ifdef ALT_BITSTREAM_READER
65 int index;
66#elif defined LIBMPEG2_BITSTREAM_READER
67 uint8_t *buffer_ptr;
68 uint32_t cache;
69 int bit_count;
70#elif defined A32_BITSTREAM_READER
71 uint32_t *buffer_ptr;
72 uint32_t cache0;
73 uint32_t cache1;
74 int bit_count;
75#endif
76 int size_in_bits;
77} GetBitContext;
78
79
80/* Bitstream reader API docs:
81name
82 arbitrary name which is used as prefix for the internal variables
83
84gb
85 getbitcontext
86
87OPEN_READER(name, gb)
88 loads gb into local variables
89
90CLOSE_READER(name, gb)
91 stores local vars in gb
92
93UPDATE_CACHE(name, gb)
94 refills the internal cache from the bitstream
95 after this call at least MIN_CACHE_BITS will be available,
96
97GET_CACHE(name, gb)
98 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
99
100SHOW_UBITS(name, gb, num)
101 will return the next num bits
102
103SHOW_SBITS(name, gb, num)
104 will return the next num bits and do sign extension
105
106SKIP_BITS(name, gb, num)
107 will skip over the next num bits
108 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
109
110SKIP_CACHE(name, gb, num)
111 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
112
113SKIP_COUNTER(name, gb, num)
114 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
115
116LAST_SKIP_CACHE(name, gb, num)
117 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
118
119LAST_SKIP_BITS(name, gb, num)
120 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
121
122for examples see get_bits, show_bits, skip_bits, get_vlc
123*/
124
125#ifdef ALT_BITSTREAM_READER
126#define MIN_CACHE_BITS 25
127
128#define OPEN_READER(name, gb)\
129 int name##_index= (gb)->index;\
130 int name##_cache= 0;\
131
132#define CLOSE_READER(name, gb)\
133 (gb)->index= name##_index;\
134
135#ifdef ALT_BITSTREAM_READER_LE
136#define UPDATE_CACHE(name, gb)\
137 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
138
139#define SKIP_CACHE(name, gb, num)\
140 name##_cache >>= (num);
141#else
142#define UPDATE_CACHE(name, gb)\
143 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
144
145#define SKIP_CACHE(name, gb, num)\
146 name##_cache <<= (num);
147#endif
148
149// FIXME name?
150#define SKIP_COUNTER(name, gb, num)\
151 name##_index += (num);\
152
153#define SKIP_BITS(name, gb, num)\
154 {\
155 SKIP_CACHE(name, gb, num)\
156 SKIP_COUNTER(name, gb, num)\
157 }\
158
159#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
160#define LAST_SKIP_CACHE(name, gb, num) ;
161
162#ifdef ALT_BITSTREAM_READER_LE
163#define SHOW_UBITS(name, gb, num)\
164 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
165
166#define SHOW_SBITS(name, gb, num)\
167 NEG_SSR32((name##_cache)<<(32-(num)), num)
168#else
169#define SHOW_UBITS(name, gb, num)\
170 NEG_USR32(name##_cache, num)
171
172#define SHOW_SBITS(name, gb, num)\
173 NEG_SSR32(name##_cache, num)
174#endif
175
176#define GET_CACHE(name, gb)\
177 ((uint32_t)name##_cache)
178
179static inline int get_bits_count(GetBitContext *s)
180{
181 return s->index;
182}
183
184static inline void skip_bits_long(GetBitContext *s, int n)
185{
186 s->index += n;
187}
188
189#elif defined LIBMPEG2_BITSTREAM_READER
190//libmpeg2 like reader
191
192#define MIN_CACHE_BITS 17
193
194#define OPEN_READER(name, gb)\
195 int name##_bit_count=(gb)->bit_count;\
196 int name##_cache= (gb)->cache;\
197 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\
198
199#define CLOSE_READER(name, gb)\
200 (gb)->bit_count= name##_bit_count;\
201 (gb)->cache= name##_cache;\
202 (gb)->buffer_ptr= name##_buffer_ptr;\
203
204#define UPDATE_CACHE(name, gb)\
205 if(name##_bit_count >= 0){\
206 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \
207 name##_buffer_ptr+=2;\
208 name##_bit_count-= 16;\
209 }\
210
211#define SKIP_CACHE(name, gb, num)\
212 name##_cache <<= (num);\
213
214#define SKIP_COUNTER(name, gb, num)\
215 name##_bit_count += (num);\
216
217#define SKIP_BITS(name, gb, num)\
218 {\
219 SKIP_CACHE(name, gb, num)\
220 SKIP_COUNTER(name, gb, num)\
221 }\
222
223#define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
224#define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
225
226#define SHOW_UBITS(name, gb, num)\
227 NEG_USR32(name##_cache, num)
228
229#define SHOW_SBITS(name, gb, num)\
230 NEG_SSR32(name##_cache, num)
231
232#define GET_CACHE(name, gb)\
233 ((uint32_t)name##_cache)
234
235static inline int get_bits_count(GetBitContext *s)
236{
237 return (s->buffer_ptr - s->buffer) * 8 - 16 + s->bit_count;
238}
239
240static inline void skip_bits_long(GetBitContext *s, int n)
241{
242 OPEN_READER(re, s)
243 re_bit_count += n;
244 re_buffer_ptr += 2 * (re_bit_count >> 4);
245 re_bit_count &= 15;
246 re_cache = ((re_buffer_ptr[-2] << 8) + re_buffer_ptr[-1]) << (16 + re_bit_count);
247 UPDATE_CACHE(re, s)
248 CLOSE_READER(re, s)
249}
250
251#elif defined A32_BITSTREAM_READER
252
253#define MIN_CACHE_BITS 32
254
255#define OPEN_READER(name, gb)\
256 int name##_bit_count=(gb)->bit_count;\
257 uint32_t name##_cache0= (gb)->cache0;\
258 uint32_t name##_cache1= (gb)->cache1;\
259 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
260
261#define CLOSE_READER(name, gb)\
262 (gb)->bit_count= name##_bit_count;\
263 (gb)->cache0= name##_cache0;\
264 (gb)->cache1= name##_cache1;\
265 (gb)->buffer_ptr= name##_buffer_ptr;\
266
267#define UPDATE_CACHE(name, gb)\
268 if(name##_bit_count > 0){\
269 const uint32_t next= be2me_32( *name##_buffer_ptr );\
270 name##_cache0 |= NEG_USR32(next,name##_bit_count);\
271 name##_cache1 |= next<<name##_bit_count;\
272 name##_buffer_ptr++;\
273 name##_bit_count-= 32;\
274 }\
275
276#if ARCH_X86
277#define SKIP_CACHE(name, gb, num)\
278 __asm__(\
279 "shldl %2, %1, %0 \n\t"\
280 "shll %2, %1 \n\t"\
281 : "+r" (name##_cache0), "+r" (name##_cache1)\
282 : "Ic" ((uint8_t)(num))\
283 );
284#else
285#define SKIP_CACHE(name, gb, num)\
286 name##_cache0 <<= (num);\
287 name##_cache0 |= NEG_USR32(name##_cache1,num);\
288 name##_cache1 <<= (num);
289#endif
290
291#define SKIP_COUNTER(name, gb, num)\
292 name##_bit_count += (num);\
293
294#define SKIP_BITS(name, gb, num)\
295 {\
296 SKIP_CACHE(name, gb, num)\
297 SKIP_COUNTER(name, gb, num)\
298 }\
299
300#define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
301#define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
302
303#define SHOW_UBITS(name, gb, num)\
304 NEG_USR32(name##_cache0, num)
305
306#define SHOW_SBITS(name, gb, num)\
307 NEG_SSR32(name##_cache0, num)
308
309#define GET_CACHE(name, gb)\
310 (name##_cache0)
311
312static inline int get_bits_count(GetBitContext *s)
313{
314 return ((uint8_t*)s->buffer_ptr - s->buffer) * 8 - 32 + s->bit_count;
315}
316
317static inline void skip_bits_long(GetBitContext *s, int n)
318{
319 OPEN_READER(re, s)
320 re_bit_count += n;
321 re_buffer_ptr += re_bit_count >> 5;
322 re_bit_count &= 31;
323 re_cache0 = be2me_32(re_buffer_ptr[-1]) << re_bit_count;
324 re_cache1 = 0;
325 UPDATE_CACHE(re, s)
326 CLOSE_READER(re, s)
327}
328
329#endif
330
331/**
332 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
333 * if MSB not set it is negative
334 * @param n length in bits
335 * @author BERO
336 */
337static inline int get_xbits(GetBitContext *s, int n)
338{
339 register int sign;
340 register int32_t cache;
341 OPEN_READER(re, s)
342 UPDATE_CACHE(re, s)
343 cache = GET_CACHE(re, s);
344 sign = (~cache) >> 31;
345 LAST_SKIP_BITS(re, s, n)
346 CLOSE_READER(re, s)
347 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
348}
349
350static inline int get_sbits(GetBitContext *s, int n)
351{
352 register int tmp;
353 OPEN_READER(re, s)
354 UPDATE_CACHE(re, s)
355 tmp = SHOW_SBITS(re, s, n);
356 LAST_SKIP_BITS(re, s, n)
357 CLOSE_READER(re, s)
358 return tmp;
359}
360
361/**
362 * reads 1-17 bits.
363 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
364 */
365static inline unsigned int get_bits(GetBitContext *s, int n)
366{
367 register int tmp;
368 OPEN_READER(re, s)
369 UPDATE_CACHE(re, s)
370 tmp = SHOW_UBITS(re, s, n);
371 LAST_SKIP_BITS(re, s, n)
372 CLOSE_READER(re, s)
373 return tmp;
374}
375
376/**
377 * shows 1-17 bits.
378 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
379 */
380static inline unsigned int show_bits(GetBitContext *s, int n)
381{
382 register int tmp;
383 OPEN_READER(re, s)
384 UPDATE_CACHE(re, s)
385 tmp = SHOW_UBITS(re, s, n);
386 // CLOSE_READER(re, s)
387 return tmp;
388}
389
390static inline void skip_bits(GetBitContext *s, int n)
391{
392 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
393 OPEN_READER(re, s)
394 UPDATE_CACHE(re, s)
395 LAST_SKIP_BITS(re, s, n)
396 CLOSE_READER(re, s)
397}
398
399static inline unsigned int get_bits1(GetBitContext *s)
400{
401#ifdef ALT_BITSTREAM_READER
402 int index = s->index;
403 uint8_t result = s->buffer[ index >> 3 ];
404#ifdef ALT_BITSTREAM_READER_LE
405 result >>= (index & 0x07);
406 result &= 1;
407#else
408 result <<= (index & 0x07);
409 result >>= 8 - 1;
410#endif
411 index++;
412 s->index = index;
413
414 return result;
415#else
416 return get_bits(s, 1);
417#endif
418}
419
420static inline unsigned int show_bits1(GetBitContext *s)
421{
422 return show_bits(s, 1);
423}
424
425static inline void skip_bits1(GetBitContext *s)
426{
427 skip_bits(s, 1);
428}
429
430/**
431 * reads 0-32 bits.
432 */
433static inline unsigned int get_bits_long(GetBitContext *s, int n)
434{
435 if (n <= 17) {
436 return get_bits(s, n);
437 } else {
438#ifdef ALT_BITSTREAM_READER_LE
439 int ret = get_bits(s, 16);
440 return ret | (get_bits(s, n - 16) << 16);
441#else
442 int ret = get_bits(s, 16) << (n - 16);
443 return ret | get_bits(s, n - 16);
444#endif
445 }
446}
447
448/**
449 * reads 0-32 bits as a signed integer.
450 */
451static inline int get_sbits_long(GetBitContext *s, int n)
452{
453 return sign_extend(get_bits_long(s, n), n);
454}
455
456/**
457 * shows 0-32 bits.
458 */
459static inline unsigned int show_bits_long(GetBitContext *s, int n)
460{
461 if (n <= 17) {
462 return show_bits(s, n);
463 } else {
464 GetBitContext gb = *s;
465 return get_bits_long(&gb, n);
466 }
467}
468
469static inline int check_marker(GetBitContext *s, const char *msg)
470{
471 int bit = get_bits1(s);
472 if (!bit) {
473 LOGD("Marker bit missing %s\n", msg);
474 }
475
476 return bit;
477}
478
479/**
480 * init GetBitContext.
481 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
482 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
483 * @param bit_size the size of the buffer in bits
484 *
485 * While GetBitContext stores the buffer size, for performance reasons you are
486 * responsible for checking for the buffer end yourself (take advantage of the padding)!
487 */
488static inline void init_get_bits(GetBitContext *s,
489 const uint8_t *buffer, int bit_size)
490{
491 int buffer_size = (bit_size + 7) >> 3;
492 if (buffer_size < 0 || bit_size < 0) {
493 buffer_size = bit_size = 0;
494 buffer = NULL;
495 }
496
497 s->buffer = buffer;
498 s->size_in_bits = bit_size;
499 s->buffer_end = buffer + buffer_size;
500#ifdef ALT_BITSTREAM_READER
501 s->index = 0;
502#elif defined LIBMPEG2_BITSTREAM_READER
503 s->buffer_ptr = (uint8_t*)((intptr_t)buffer & (~1));
504 s->bit_count = 16 + 8 * ((intptr_t)buffer & 1);
505 skip_bits_long(s, 0);
506#elif defined A32_BITSTREAM_READER
507 s->buffer_ptr = (uint32_t*)((intptr_t)buffer & (~3));
508 s->bit_count = 32 + 8 * ((intptr_t)buffer & 3);
509 skip_bits_long(s, 0);
510#endif
511}
512
513static inline void align_get_bits(GetBitContext *s)
514{
515 int n = (-get_bits_count(s)) & 7;
516 if (n) {
517 skip_bits(s, n);
518 }
519}
520
521//#define TRACE
522
523#ifdef TRACE
524static inline void print_bin(int bits, int n)
525{
526 int i;
527
528 for (i = n - 1; i >= 0; i--) {
529 LOGD("%d", (bits >> i) & 1);
530 }
531 for (i = n; i < 24; i++) {
532 LOGD(" ");
533 }
534}
535
536static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line)
537{
538 int r = get_bits(s, n);
539
540 print_bin(r, n);
541 LOGD("%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s) - n, file, func, line);
542 return r;
543}
544static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line)
545{
546 int show = show_bits(s, n);
547 int r = get_xbits(s, n);
548
549 print_bin(show, n);
550 LOGD("%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s) - n, file, func, line);
551 return r;
552}
553
554#define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
555#define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__)
556#define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__)
557#endif
558
559static inline int get_bits_left(GetBitContext *gb)
560{
561 return gb->size_in_bits - get_bits_count(gb);
562}
563
564#endif /* AVCODEC_GET_BITS_H */
565