1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "private/bitmath.h"
39 #include "private/bitreader.h"
40 #include "private/crc.h"
41 #include "private/macros.h"
42 #include "FLAC/assert.h"
43 #include "share/compat.h"
44 #include "share/endswap.h"
46 /* Things should be fastest when this matches the machine word size */
47 /* WATCHOUT: if you change this you must also change the following #defines down to FLAC__clz_uint32 below to match */
48 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
49 /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
50 #define FLAC__BYTES_PER_WORD 4 /* sizeof uint32_t */
51 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD)
52 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
53 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
55 #define SWAP_BE_WORD_TO_HOST(x) (x)
57 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
61 * This should be at least twice as large as the largest number of words
62 * required to represent any 'number' (in any encoding) you are going to
63 * read. With FLAC this is on the order of maybe a few hundred bits.
64 * If the buffer is smaller than that, the decoder won't be able to read
65 * in a whole number that is in a variable length encoding (e.g. Rice).
66 * But to be practical it should be at least 1K bytes.
68 * Increase this number to decrease the number of read callbacks, at the
69 * expense of using more memory. Or decrease for the reverse effect,
70 * keeping in mind the limit from the first paragraph. The optimal size
71 * also depends on the CPU cache size and other factors; some twiddling
72 * may be necessary to squeeze out the best performance.
74 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
76 /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
77 struct FLAC__BitReader {
78 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
79 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
81 unsigned capacity; /* in words */
82 unsigned words; /* # of completed words in buffer */
83 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
84 unsigned consumed_words; /* #words ... */
85 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
86 unsigned read_crc16; /* the running frame CRC */
87 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
88 FLAC__BitReaderReadCallback read_callback;
90 FLAC__CPUInfo cpu_info;
93 static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
95 register unsigned crc = br->read_crc16;
96 #if FLAC__BYTES_PER_WORD == 4
97 switch(br->crc16_align) {
98 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
99 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
100 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
101 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
103 #elif FLAC__BYTES_PER_WORD == 8
104 switch(br->crc16_align) {
105 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
106 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
107 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
108 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
109 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
110 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
111 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
112 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
115 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
116 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
117 br->read_crc16 = crc;
122 /* would be static except it needs to be called by asm routines */
123 FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
129 /* first shift the unconsumed buffer data toward the front as much as possible */
130 if(br->consumed_words > 0) {
131 start = br->consumed_words;
132 end = br->words + (br->bytes? 1:0);
133 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
136 br->consumed_words = 0;
140 * set the target for reading, taking into account word alignment and endianness
142 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
144 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
145 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
147 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
148 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
149 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
150 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
151 * ^^-------target, bytes=3
152 * on LE machines, have to byteswap the odd tail word so nothing is
158 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
161 /* now it looks like:
162 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
163 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
164 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
165 * ^^-------target, bytes=3
168 /* read in the data; note that the callback may return a smaller number of bytes */
169 if(!br->read_callback(target, &bytes, br->client_data))
172 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
173 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
174 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
175 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
176 * now have to byteswap on LE machines:
180 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
181 for(start = br->words; start < end; start++)
182 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
185 /* now it looks like:
186 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
187 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
188 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
189 * finally we'll update the reader values:
191 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
192 br->words = end / FLAC__BYTES_PER_WORD;
193 br->bytes = end % FLAC__BYTES_PER_WORD;
198 /***********************************************************************
200 * Class constructor/destructor
202 ***********************************************************************/
204 FLAC__BitReader *FLAC__bitreader_new(void)
206 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
209 memset(br, 0, sizeof(FLAC__BitReader));
212 br->words = br->bytes = 0;
213 br->consumed_words = br->consumed_bits = 0;
214 br->read_callback = 0;
220 void FLAC__bitreader_delete(FLAC__BitReader *br)
222 FLAC__ASSERT(0 != br);
224 FLAC__bitreader_free(br);
228 /***********************************************************************
230 * Public class methods
232 ***********************************************************************/
234 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
236 FLAC__ASSERT(0 != br);
238 br->words = br->bytes = 0;
239 br->consumed_words = br->consumed_bits = 0;
240 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
241 br->buffer = malloc(sizeof(uint32_t) * br->capacity);
244 br->read_callback = rcb;
245 br->client_data = cd;
251 void FLAC__bitreader_free(FLAC__BitReader *br)
253 FLAC__ASSERT(0 != br);
259 br->words = br->bytes = 0;
260 br->consumed_words = br->consumed_bits = 0;
261 br->read_callback = 0;
265 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
267 br->words = br->bytes = 0;
268 br->consumed_words = br->consumed_bits = 0;
272 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
276 fprintf(out, "bitreader is NULL\n");
279 fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
281 for(i = 0; i < br->words; i++) {
282 fprintf(out, "%08X: ", i);
283 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
284 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
287 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
291 fprintf(out, "%08X: ", i);
292 for(j = 0; j < br->bytes*8; j++)
293 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
296 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
302 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
304 FLAC__ASSERT(0 != br);
305 FLAC__ASSERT(0 != br->buffer);
306 FLAC__ASSERT((br->consumed_bits & 7) == 0);
308 br->read_crc16 = (unsigned)seed;
309 br->crc16_align = br->consumed_bits;
312 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
314 FLAC__ASSERT(0 != br);
315 FLAC__ASSERT(0 != br->buffer);
316 FLAC__ASSERT((br->consumed_bits & 7) == 0);
317 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
319 /* CRC any tail bytes in a partially-consumed word */
320 if(br->consumed_bits) {
321 const uint32_t tail = br->buffer[br->consumed_words];
322 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
323 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
325 return br->read_crc16;
328 inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
330 return ((br->consumed_bits & 7) == 0);
333 inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
335 return 8 - (br->consumed_bits & 7);
338 inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
340 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
343 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
345 FLAC__ASSERT(0 != br);
346 FLAC__ASSERT(0 != br->buffer);
348 FLAC__ASSERT(bits <= 32);
349 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
350 FLAC__ASSERT(br->consumed_words <= br->words);
352 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
353 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
355 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
360 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
361 if(!bitreader_read_from_client_(br))
364 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
365 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
366 if(br->consumed_bits) {
367 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
368 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
369 const uint32_t word = br->buffer[br->consumed_words];
371 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
372 br->consumed_bits += bits;
375 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
377 crc16_update_word_(br, word);
378 br->consumed_words++;
379 br->consumed_bits = 0;
380 if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
382 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
383 br->consumed_bits = bits;
388 const uint32_t word = br->buffer[br->consumed_words];
389 if(bits < FLAC__BITS_PER_WORD) {
390 *val = word >> (FLAC__BITS_PER_WORD-bits);
391 br->consumed_bits = bits;
394 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
396 crc16_update_word_(br, word);
397 br->consumed_words++;
402 /* in this case we're starting our read at a partial tail word;
403 * the reader has guaranteed that we have at least 'bits' bits
404 * available to read, which makes this case simpler.
406 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
407 if(br->consumed_bits) {
408 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
409 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
410 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
411 br->consumed_bits += bits;
415 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
416 br->consumed_bits += bits;
422 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
424 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
425 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
433 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
438 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
440 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
447 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
454 inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
456 FLAC__uint32 x8, x32 = 0;
458 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
460 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
463 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
467 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
471 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
479 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
482 * OPT: a faster implementation is possible but probably not that useful
483 * since this is only called a couple of times in the metadata readers.
485 FLAC__ASSERT(0 != br);
486 FLAC__ASSERT(0 != br->buffer);
489 const unsigned n = br->consumed_bits & 7;
494 m = flac_min(8-n, bits);
495 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
501 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
506 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
514 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
518 FLAC__ASSERT(0 != br);
519 FLAC__ASSERT(0 != br->buffer);
520 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
522 /* step 1: skip over partial head word to get word aligned */
523 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
524 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
530 /* step 2: skip whole words in chunks */
531 while(nvals >= FLAC__BYTES_PER_WORD) {
532 if(br->consumed_words < br->words) {
533 br->consumed_words++;
534 nvals -= FLAC__BYTES_PER_WORD;
536 else if(!bitreader_read_from_client_(br))
539 /* step 3: skip any remainder from partial tail bytes */
541 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
549 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
553 FLAC__ASSERT(0 != br);
554 FLAC__ASSERT(0 != br->buffer);
555 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
557 /* step 1: read from partial head word to get word aligned */
558 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
559 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
561 *val++ = (FLAC__byte)x;
566 /* step 2: read whole words in chunks */
567 while(nvals >= FLAC__BYTES_PER_WORD) {
568 if(br->consumed_words < br->words) {
569 const uint32_t word = br->buffer[br->consumed_words++];
570 #if FLAC__BYTES_PER_WORD == 4
571 val[0] = (FLAC__byte)(word >> 24);
572 val[1] = (FLAC__byte)(word >> 16);
573 val[2] = (FLAC__byte)(word >> 8);
574 val[3] = (FLAC__byte)word;
575 #elif FLAC__BYTES_PER_WORD == 8
576 val[0] = (FLAC__byte)(word >> 56);
577 val[1] = (FLAC__byte)(word >> 48);
578 val[2] = (FLAC__byte)(word >> 40);
579 val[3] = (FLAC__byte)(word >> 32);
580 val[4] = (FLAC__byte)(word >> 24);
581 val[5] = (FLAC__byte)(word >> 16);
582 val[6] = (FLAC__byte)(word >> 8);
583 val[7] = (FLAC__byte)word;
585 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
586 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
588 val += FLAC__BYTES_PER_WORD;
589 nvals -= FLAC__BYTES_PER_WORD;
591 else if(!bitreader_read_from_client_(br))
594 /* step 3: read any remainder from partial tail bytes */
596 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
598 *val++ = (FLAC__byte)x;
605 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
606 #if 0 /* slow but readable version */
610 FLAC__ASSERT(0 != br);
611 FLAC__ASSERT(0 != br->buffer);
615 if(!FLAC__bitreader_read_bit(br, &bit))
628 FLAC__ASSERT(0 != br);
629 FLAC__ASSERT(0 != br->buffer);
633 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
634 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
636 i = FLAC__clz_uint32(b);
639 br->consumed_bits += i;
640 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
641 crc16_update_word_(br, br->buffer[br->consumed_words]);
642 br->consumed_words++;
643 br->consumed_bits = 0;
648 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
649 crc16_update_word_(br, br->buffer[br->consumed_words]);
650 br->consumed_words++;
651 br->consumed_bits = 0;
652 /* didn't find stop bit yet, have to keep going... */
655 /* at this point we've eaten up all the whole words; have to try
656 * reading through any tail bytes before calling the read callback.
657 * this is a repeat of the above logic adjusted for the fact we
658 * don't have a whole word. note though if the client is feeding
659 * us data a byte at a time (unlikely), br->consumed_bits may not
662 if(br->bytes*8 > br->consumed_bits) {
663 const unsigned end = br->bytes * 8;
664 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
666 i = FLAC__clz_uint32(b);
669 br->consumed_bits += i;
670 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
674 *val += end - br->consumed_bits;
675 br->consumed_bits = end;
676 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
677 /* didn't find stop bit yet, have to keep going... */
680 if(!bitreader_read_from_client_(br))
686 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
688 FLAC__uint32 lsbs = 0, msbs = 0;
691 FLAC__ASSERT(0 != br);
692 FLAC__ASSERT(0 != br->buffer);
693 FLAC__ASSERT(parameter <= 31);
695 /* read the unary MSBs and end bit */
696 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
699 /* read the binary LSBs */
700 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
703 /* compose the value */
704 uval = (msbs << parameter) | lsbs;
706 *val = -((int)(uval >> 1)) - 1;
708 *val = (int)(uval >> 1);
713 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
714 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
716 /* try and get br->consumed_words and br->consumed_bits into register;
717 * must remember to flush them back to *br before calling other
718 * bitreader functions that use them, and before returning */
719 unsigned cwords, words, lsbs, msbs, x, y;
720 unsigned ucbits; /* keep track of the number of unconsumed bits in word */
724 FLAC__ASSERT(0 != br);
725 FLAC__ASSERT(0 != br->buffer);
726 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
727 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
728 FLAC__ASSERT(parameter < 32);
729 /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
736 /* read the unary MSBs and end bit */
737 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
740 *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1);
746 FLAC__ASSERT(parameter > 0);
748 cwords = br->consumed_words;
751 /* if we've not consumed up to a partial tail word... */
752 if(cwords >= words) {
757 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
758 b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */
761 /* read the unary MSBs and end bit */
762 x = y = FLAC__clz2_uint32(b);
763 if(x == FLAC__BITS_PER_WORD) {
766 /* didn't find stop bit yet, have to keep going... */
767 crc16_update_word_(br, br->buffer[cwords++]);
769 goto incomplete_msbs;
770 b = br->buffer[cwords];
771 y = FLAC__clz2_uint32(b);
773 } while(y == FLAC__BITS_PER_WORD);
776 b <<= 1; /* account for stop bit */
777 ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD;
780 /* read the binary LSBs */
781 x = b >> (FLAC__BITS_PER_WORD - parameter);
782 if(parameter <= ucbits) {
786 /* there are still bits left to read, they will all be in the next word */
787 crc16_update_word_(br, br->buffer[cwords++]);
789 goto incomplete_lsbs;
790 b = br->buffer[cwords];
791 ucbits += FLAC__BITS_PER_WORD - parameter;
793 b <<= FLAC__BITS_PER_WORD - ucbits;
797 /* compose the value */
798 x = (msbs << parameter) | lsbs;
799 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
803 /* at this point we've eaten up all the whole words */
808 br->consumed_bits = 0;
809 br->consumed_words = cwords;
812 /* read the unary MSBs and end bit */
813 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
820 br->consumed_bits = 0;
821 br->consumed_words = cwords;
824 /* read the binary LSBs */
825 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits))
829 /* compose the value */
830 x = (msbs << parameter) | lsbs;
831 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
834 cwords = br->consumed_words;
836 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
837 b = br->buffer[cwords] << br->consumed_bits;
838 } while(cwords >= words && val < end);
841 if(ucbits == 0 && cwords < words) {
842 /* don't leave the head word with no unconsumed bits */
843 crc16_update_word_(br, br->buffer[cwords++]);
844 ucbits = FLAC__BITS_PER_WORD;
847 br->consumed_bits = FLAC__BITS_PER_WORD - ucbits;
848 br->consumed_words = cwords;
854 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
856 FLAC__uint32 lsbs = 0, msbs = 0;
857 unsigned bit, uval, k;
859 FLAC__ASSERT(0 != br);
860 FLAC__ASSERT(0 != br->buffer);
862 k = FLAC__bitmath_ilog2(parameter);
864 /* read the unary MSBs and end bit */
865 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
868 /* read the binary LSBs */
869 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
872 if(parameter == 1u<<k) {
873 /* compose the value */
874 uval = (msbs << k) | lsbs;
877 unsigned d = (1 << (k+1)) - parameter;
879 if(!FLAC__bitreader_read_bit(br, &bit))
885 /* compose the value */
886 uval = msbs * parameter + lsbs;
889 /* unfold unsigned to signed */
891 *val = -((int)(uval >> 1)) - 1;
893 *val = (int)(uval >> 1);
898 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
900 FLAC__uint32 lsbs, msbs = 0;
903 FLAC__ASSERT(0 != br);
904 FLAC__ASSERT(0 != br->buffer);
906 k = FLAC__bitmath_ilog2(parameter);
908 /* read the unary MSBs and end bit */
909 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
912 /* read the binary LSBs */
913 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
916 if(parameter == 1u<<k) {
917 /* compose the value */
918 *val = (msbs << k) | lsbs;
921 unsigned d = (1 << (k+1)) - parameter;
923 if(!FLAC__bitreader_read_bit(br, &bit))
929 /* compose the value */
930 *val = msbs * parameter + lsbs;
937 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
938 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
944 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
947 raw[(*rawlen)++] = (FLAC__byte)x;
948 if(!(x & 0x80)) { /* 0xxxxxxx */
952 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
956 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
960 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
964 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
968 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
977 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
980 raw[(*rawlen)++] = (FLAC__byte)x;
981 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
992 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
993 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
999 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1002 raw[(*rawlen)++] = (FLAC__byte)x;
1003 if(!(x & 0x80)) { /* 0xxxxxxx */
1007 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1011 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1015 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1019 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1023 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1027 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1032 *val = FLAC__U64L(0xffffffffffffffff);
1036 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1039 raw[(*rawlen)++] = (FLAC__byte)x;
1040 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1041 *val = FLAC__U64L(0xffffffffffffffff);