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/endswap.h"
45 /* Things should be fastest when this matches the machine word size */
46 /* WATCHOUT: if you change this you must also change the following #defines down to FLAC__clz_uint32 below to match */
47 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
48 /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
49 #define FLAC__BYTES_PER_WORD 4 /* sizeof uint32_t */
50 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD)
51 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
52 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
54 #define SWAP_BE_WORD_TO_HOST(x) (x)
56 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
60 * This should be at least twice as large as the largest number of words
61 * required to represent any 'number' (in any encoding) you are going to
62 * read. With FLAC this is on the order of maybe a few hundred bits.
63 * If the buffer is smaller than that, the decoder won't be able to read
64 * in a whole number that is in a variable length encoding (e.g. Rice).
65 * But to be practical it should be at least 1K bytes.
67 * Increase this number to decrease the number of read callbacks, at the
68 * expense of using more memory. Or decrease for the reverse effect,
69 * keeping in mind the limit from the first paragraph. The optimal size
70 * also depends on the CPU cache size and other factors; some twiddling
71 * may be necessary to squeeze out the best performance.
73 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
75 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
77 #define FLAC__U64L(x) x
79 #define FLAC__U64L(x) x##LLU
82 /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
83 struct FLAC__BitReader {
84 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
85 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
87 unsigned capacity; /* in words */
88 unsigned words; /* # of completed words in buffer */
89 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
90 unsigned consumed_words; /* #words ... */
91 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
92 unsigned read_crc16; /* the running frame CRC */
93 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
94 FLAC__BitReaderReadCallback read_callback;
96 FLAC__CPUInfo cpu_info;
99 static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
101 register unsigned crc = br->read_crc16;
102 #if FLAC__BYTES_PER_WORD == 4
103 switch(br->crc16_align) {
104 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
105 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
106 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
107 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
109 #elif FLAC__BYTES_PER_WORD == 8
110 switch(br->crc16_align) {
111 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
112 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
113 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
114 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
115 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
116 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
117 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
118 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
121 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
122 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
123 br->read_crc16 = crc;
128 /* would be static except it needs to be called by asm routines */
129 FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
135 /* first shift the unconsumed buffer data toward the front as much as possible */
136 if(br->consumed_words > 0) {
137 start = br->consumed_words;
138 end = br->words + (br->bytes? 1:0);
139 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
142 br->consumed_words = 0;
146 * set the target for reading, taking into account word alignment and endianness
148 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
150 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
151 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
153 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
154 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
155 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
156 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
157 * ^^-------target, bytes=3
158 * on LE machines, have to byteswap the odd tail word so nothing is
164 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
167 /* now it looks like:
168 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
169 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
170 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
171 * ^^-------target, bytes=3
174 /* read in the data; note that the callback may return a smaller number of bytes */
175 if(!br->read_callback(target, &bytes, br->client_data))
178 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
179 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
180 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
181 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
182 * now have to byteswap on LE machines:
186 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
187 for(start = br->words; start < end; start++)
188 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
191 /* now it looks like:
192 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
193 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
194 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
195 * finally we'll update the reader values:
197 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
198 br->words = end / FLAC__BYTES_PER_WORD;
199 br->bytes = end % FLAC__BYTES_PER_WORD;
204 /***********************************************************************
206 * Class constructor/destructor
208 ***********************************************************************/
210 FLAC__BitReader *FLAC__bitreader_new(void)
212 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
215 memset(br, 0, sizeof(FLAC__BitReader));
218 br->words = br->bytes = 0;
219 br->consumed_words = br->consumed_bits = 0;
220 br->read_callback = 0;
226 void FLAC__bitreader_delete(FLAC__BitReader *br)
228 FLAC__ASSERT(0 != br);
230 FLAC__bitreader_free(br);
234 /***********************************************************************
236 * Public class methods
238 ***********************************************************************/
240 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
242 FLAC__ASSERT(0 != br);
244 br->words = br->bytes = 0;
245 br->consumed_words = br->consumed_bits = 0;
246 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
247 br->buffer = malloc(sizeof(uint32_t) * br->capacity);
250 br->read_callback = rcb;
251 br->client_data = cd;
257 void FLAC__bitreader_free(FLAC__BitReader *br)
259 FLAC__ASSERT(0 != br);
265 br->words = br->bytes = 0;
266 br->consumed_words = br->consumed_bits = 0;
267 br->read_callback = 0;
271 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
273 br->words = br->bytes = 0;
274 br->consumed_words = br->consumed_bits = 0;
278 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
282 fprintf(out, "bitreader is NULL\n");
285 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);
287 for(i = 0; i < br->words; i++) {
288 fprintf(out, "%08X: ", i);
289 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
290 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
293 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
297 fprintf(out, "%08X: ", i);
298 for(j = 0; j < br->bytes*8; j++)
299 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
302 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
308 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
310 FLAC__ASSERT(0 != br);
311 FLAC__ASSERT(0 != br->buffer);
312 FLAC__ASSERT((br->consumed_bits & 7) == 0);
314 br->read_crc16 = (unsigned)seed;
315 br->crc16_align = br->consumed_bits;
318 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
320 FLAC__ASSERT(0 != br);
321 FLAC__ASSERT(0 != br->buffer);
322 FLAC__ASSERT((br->consumed_bits & 7) == 0);
323 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
325 /* CRC any tail bytes in a partially-consumed word */
326 if(br->consumed_bits) {
327 const uint32_t tail = br->buffer[br->consumed_words];
328 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
329 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
331 return br->read_crc16;
334 inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
336 return ((br->consumed_bits & 7) == 0);
339 inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
341 return 8 - (br->consumed_bits & 7);
344 inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
346 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
349 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
351 FLAC__ASSERT(0 != br);
352 FLAC__ASSERT(0 != br->buffer);
354 FLAC__ASSERT(bits <= 32);
355 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
356 FLAC__ASSERT(br->consumed_words <= br->words);
358 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
359 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
361 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
366 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
367 if(!bitreader_read_from_client_(br))
370 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
371 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
372 if(br->consumed_bits) {
373 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
374 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
375 const uint32_t word = br->buffer[br->consumed_words];
377 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
378 br->consumed_bits += bits;
381 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
383 crc16_update_word_(br, word);
384 br->consumed_words++;
385 br->consumed_bits = 0;
386 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 */
388 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
389 br->consumed_bits = bits;
394 const uint32_t word = br->buffer[br->consumed_words];
395 if(bits < FLAC__BITS_PER_WORD) {
396 *val = word >> (FLAC__BITS_PER_WORD-bits);
397 br->consumed_bits = bits;
400 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
402 crc16_update_word_(br, word);
403 br->consumed_words++;
408 /* in this case we're starting our read at a partial tail word;
409 * the reader has guaranteed that we have at least 'bits' bits
410 * available to read, which makes this case simpler.
412 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
413 if(br->consumed_bits) {
414 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
415 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
416 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
417 br->consumed_bits += bits;
421 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
422 br->consumed_bits += bits;
428 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
430 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
431 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
439 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
444 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
446 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
453 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
460 inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
462 FLAC__uint32 x8, x32 = 0;
464 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
466 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
469 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
473 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
477 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
485 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
488 * OPT: a faster implementation is possible but probably not that useful
489 * since this is only called a couple of times in the metadata readers.
491 FLAC__ASSERT(0 != br);
492 FLAC__ASSERT(0 != br->buffer);
495 const unsigned n = br->consumed_bits & 7;
500 m = flac_min(8-n, bits);
501 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
507 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
512 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
520 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
524 FLAC__ASSERT(0 != br);
525 FLAC__ASSERT(0 != br->buffer);
526 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
528 /* step 1: skip over partial head word to get word aligned */
529 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
530 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
536 /* step 2: skip whole words in chunks */
537 while(nvals >= FLAC__BYTES_PER_WORD) {
538 if(br->consumed_words < br->words) {
539 br->consumed_words++;
540 nvals -= FLAC__BYTES_PER_WORD;
542 else if(!bitreader_read_from_client_(br))
545 /* step 3: skip any remainder from partial tail bytes */
547 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
555 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
559 FLAC__ASSERT(0 != br);
560 FLAC__ASSERT(0 != br->buffer);
561 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
563 /* step 1: read from partial head word to get word aligned */
564 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
565 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
567 *val++ = (FLAC__byte)x;
572 /* step 2: read whole words in chunks */
573 while(nvals >= FLAC__BYTES_PER_WORD) {
574 if(br->consumed_words < br->words) {
575 const uint32_t word = br->buffer[br->consumed_words++];
576 #if FLAC__BYTES_PER_WORD == 4
577 val[0] = (FLAC__byte)(word >> 24);
578 val[1] = (FLAC__byte)(word >> 16);
579 val[2] = (FLAC__byte)(word >> 8);
580 val[3] = (FLAC__byte)word;
581 #elif FLAC__BYTES_PER_WORD == 8
582 val[0] = (FLAC__byte)(word >> 56);
583 val[1] = (FLAC__byte)(word >> 48);
584 val[2] = (FLAC__byte)(word >> 40);
585 val[3] = (FLAC__byte)(word >> 32);
586 val[4] = (FLAC__byte)(word >> 24);
587 val[5] = (FLAC__byte)(word >> 16);
588 val[6] = (FLAC__byte)(word >> 8);
589 val[7] = (FLAC__byte)word;
591 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
592 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
594 val += FLAC__BYTES_PER_WORD;
595 nvals -= FLAC__BYTES_PER_WORD;
597 else if(!bitreader_read_from_client_(br))
600 /* step 3: read any remainder from partial tail bytes */
602 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
604 *val++ = (FLAC__byte)x;
611 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
612 #if 0 /* slow but readable version */
616 FLAC__ASSERT(0 != br);
617 FLAC__ASSERT(0 != br->buffer);
621 if(!FLAC__bitreader_read_bit(br, &bit))
634 FLAC__ASSERT(0 != br);
635 FLAC__ASSERT(0 != br->buffer);
639 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
640 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
642 i = FLAC__clz_uint32(b);
645 br->consumed_bits += i;
646 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
647 crc16_update_word_(br, br->buffer[br->consumed_words]);
648 br->consumed_words++;
649 br->consumed_bits = 0;
654 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
655 crc16_update_word_(br, br->buffer[br->consumed_words]);
656 br->consumed_words++;
657 br->consumed_bits = 0;
658 /* didn't find stop bit yet, have to keep going... */
661 /* at this point we've eaten up all the whole words; have to try
662 * reading through any tail bytes before calling the read callback.
663 * this is a repeat of the above logic adjusted for the fact we
664 * don't have a whole word. note though if the client is feeding
665 * us data a byte at a time (unlikely), br->consumed_bits may not
668 if(br->bytes*8 > br->consumed_bits) {
669 const unsigned end = br->bytes * 8;
670 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
672 i = FLAC__clz_uint32(b);
675 br->consumed_bits += i;
676 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
680 *val += end - br->consumed_bits;
681 br->consumed_bits = end;
682 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
683 /* didn't find stop bit yet, have to keep going... */
686 if(!bitreader_read_from_client_(br))
692 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
694 FLAC__uint32 lsbs = 0, msbs = 0;
697 FLAC__ASSERT(0 != br);
698 FLAC__ASSERT(0 != br->buffer);
699 FLAC__ASSERT(parameter <= 31);
701 /* read the unary MSBs and end bit */
702 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
705 /* read the binary LSBs */
706 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
709 /* compose the value */
710 uval = (msbs << parameter) | lsbs;
712 *val = -((int)(uval >> 1)) - 1;
714 *val = (int)(uval >> 1);
719 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
720 /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
721 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
722 /* OPT: possibly faster version for use with MSVC */
727 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
729 /* try and get br->consumed_words and br->consumed_bits into register;
730 * must remember to flush them back to *br before calling other
731 * bitwriter functions that use them, and before returning */
732 register unsigned cwords;
733 register unsigned cbits;
735 FLAC__ASSERT(0 != br);
736 FLAC__ASSERT(0 != br->buffer);
737 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
738 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
739 FLAC__ASSERT(parameter < 32);
740 /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
745 cbits = br->consumed_bits;
746 cwords = br->consumed_words;
750 /* read unary part */
752 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
753 uint32_t b = br->buffer[cwords] << cbits;
755 #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
763 i = FLAC__clz_uint32(b);
769 if(cbits == FLAC__BITS_PER_WORD) {
770 crc16_update_word_(br, br->buffer[cwords]);
777 uval += FLAC__BITS_PER_WORD - cbits;
778 crc16_update_word_(br, br->buffer[cwords]);
781 /* didn't find stop bit yet, have to keep going... */
784 /* at this point we've eaten up all the whole words; have to try
785 * reading through any tail bytes before calling the read callback.
786 * this is a repeat of the above logic adjusted for the fact we
787 * don't have a whole word. note though if the client is feeding
788 * us data a byte at a time (unlikely), br->consumed_bits may not
791 if(br->bytes*8 > cbits) {
792 const unsigned end = br->bytes * 8;
793 uint32_t b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
795 i = FLAC__clz_uint32(b);
800 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
806 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
807 /* didn't find stop bit yet, have to keep going... */
810 /* flush registers and read; bitreader_read_from_client_() does
811 * not touch br->consumed_bits at all but we still need to set
812 * it in case it fails and we have to return false.
814 br->consumed_bits = cbits;
815 br->consumed_words = cwords;
816 if(!bitreader_read_from_client_(br))
818 cwords = br->consumed_words;
821 /* read binary part */
822 FLAC__ASSERT(cwords <= br->words);
825 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
826 /* flush registers and read; bitreader_read_from_client_() does
827 * not touch br->consumed_bits at all but we still need to set
828 * it in case it fails and we have to return false.
830 br->consumed_bits = cbits;
831 br->consumed_words = cwords;
832 if(!bitreader_read_from_client_(br))
834 cwords = br->consumed_words;
836 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
838 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
839 const unsigned n = FLAC__BITS_PER_WORD - cbits;
840 const uint32_t word = br->buffer[cwords];
843 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
848 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
850 crc16_update_word_(br, word);
853 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 */
855 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
861 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
863 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
869 /* in this case we're starting our read at a partial tail word;
870 * the reader has guaranteed that we have at least 'bits' bits
871 * available to read, which makes this case simpler.
875 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
876 FLAC__ASSERT(cbits + bits <= br->bytes*8);
877 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
882 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
889 /* compose the value */
890 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
895 br->consumed_bits = cbits;
896 br->consumed_words = cwords;
910 /* try and get br->consumed_words and br->consumed_bits into register;
911 * must remember to flush them back to *br before calling other
912 * bitwriter functions that use them, and before returning */
913 register unsigned cwords;
914 register unsigned cbits;
915 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
917 FLAC__ASSERT(0 != br);
918 FLAC__ASSERT(0 != br->buffer);
919 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
920 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
921 FLAC__ASSERT(parameter < 32);
922 /* 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 */
927 cbits = br->consumed_bits;
928 cwords = br->consumed_words;
929 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
933 /* read unary part */
935 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
936 uint32_t b = br->buffer[cwords] << cbits;
938 #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
947 i = FLAC__clz_uint32(b);
951 cbits++; /* skip over stop bit */
952 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
953 crc16_update_word_(br, br->buffer[cwords]);
960 uval += FLAC__BITS_PER_WORD - cbits;
961 crc16_update_word_(br, br->buffer[cwords]);
964 /* didn't find stop bit yet, have to keep going... */
967 /* at this point we've eaten up all the whole words; have to try
968 * reading through any tail bytes before calling the read callback.
969 * this is a repeat of the above logic adjusted for the fact we
970 * don't have a whole word. note though if the client is feeding
971 * us data a byte at a time (unlikely), br->consumed_bits may not
974 if(br->bytes*8 > cbits) {
975 const unsigned end = br->bytes * 8;
976 uint32_t b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
978 i = FLAC__clz_uint32(b);
981 cbits++; /* skip over stop bit */
982 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
988 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
989 /* didn't find stop bit yet, have to keep going... */
992 /* flush registers and read; bitreader_read_from_client_() does
993 * not touch br->consumed_bits at all but we still need to set
994 * it in case it fails and we have to return false.
996 br->consumed_bits = cbits;
997 br->consumed_words = cwords;
998 if(!bitreader_read_from_client_(br))
1000 cwords = br->consumed_words;
1001 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
1002 /* + uval to offset our count by the # of unary bits already
1003 * consumed before the read, because we will add these back
1004 * in all at once at break1
1009 ucbits--; /* account for stop bit */
1011 /* read binary part */
1012 FLAC__ASSERT(cwords <= br->words);
1015 while(ucbits < parameter) {
1016 /* flush registers and read; bitreader_read_from_client_() does
1017 * not touch br->consumed_bits at all but we still need to set
1018 * it in case it fails and we have to return false.
1020 br->consumed_bits = cbits;
1021 br->consumed_words = cwords;
1022 if(!bitreader_read_from_client_(br))
1024 cwords = br->consumed_words;
1025 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
1027 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1029 /* this also works when consumed_bits==0, it's just slower than necessary for that case */
1030 const unsigned n = FLAC__BITS_PER_WORD - cbits;
1031 const uint32_t word = br->buffer[cwords];
1034 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1039 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1040 crc16_update_word_(br, word);
1042 cbits = parameter - n;
1043 if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
1045 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1052 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1056 /* in this case we're starting our read at a partial tail word;
1057 * the reader has guaranteed that we have at least 'parameter'
1058 * bits available to read, which makes this case simpler.
1062 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
1063 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1064 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1069 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1074 ucbits -= parameter;
1076 /* compose the value */
1077 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1082 br->consumed_bits = cbits;
1083 br->consumed_words = cwords;
1095 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1097 FLAC__uint32 lsbs = 0, msbs = 0;
1098 unsigned bit, uval, k;
1100 FLAC__ASSERT(0 != br);
1101 FLAC__ASSERT(0 != br->buffer);
1103 k = FLAC__bitmath_ilog2(parameter);
1105 /* read the unary MSBs and end bit */
1106 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1109 /* read the binary LSBs */
1110 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1113 if(parameter == 1u<<k) {
1114 /* compose the value */
1115 uval = (msbs << k) | lsbs;
1118 unsigned d = (1 << (k+1)) - parameter;
1120 if(!FLAC__bitreader_read_bit(br, &bit))
1126 /* compose the value */
1127 uval = msbs * parameter + lsbs;
1130 /* unfold unsigned to signed */
1132 *val = -((int)(uval >> 1)) - 1;
1134 *val = (int)(uval >> 1);
1139 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1141 FLAC__uint32 lsbs, msbs = 0;
1144 FLAC__ASSERT(0 != br);
1145 FLAC__ASSERT(0 != br->buffer);
1147 k = FLAC__bitmath_ilog2(parameter);
1149 /* read the unary MSBs and end bit */
1150 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1153 /* read the binary LSBs */
1154 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1157 if(parameter == 1u<<k) {
1158 /* compose the value */
1159 *val = (msbs << k) | lsbs;
1162 unsigned d = (1 << (k+1)) - parameter;
1164 if(!FLAC__bitreader_read_bit(br, &bit))
1170 /* compose the value */
1171 *val = msbs * parameter + lsbs;
1178 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1179 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1185 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1188 raw[(*rawlen)++] = (FLAC__byte)x;
1189 if(!(x & 0x80)) { /* 0xxxxxxx */
1193 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1197 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1201 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1205 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1209 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1218 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1221 raw[(*rawlen)++] = (FLAC__byte)x;
1222 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1233 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1234 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1240 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1243 raw[(*rawlen)++] = (FLAC__byte)x;
1244 if(!(x & 0x80)) { /* 0xxxxxxx */
1248 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1252 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1256 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1260 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1264 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1268 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1273 *val = FLAC__U64L(0xffffffffffffffff);
1277 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1280 raw[(*rawlen)++] = (FLAC__byte)x;
1281 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1282 *val = FLAC__U64L(0xffffffffffffffff);