1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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.
36 #include <stdlib.h> /* for malloc() */
37 #include <string.h> /* for memcpy(), memset() */
38 #if defined(_MSC_VER) && _MSC_VER <= 1200
39 #include <winsock.h> /* for ntohl() */
41 #include <netinet/in.h> /* for ntohl() */
43 #include "private/bitmath.h"
44 #include "private/bitreader.h"
45 #include "private/crc.h"
46 #include "FLAC/assert.h"
49 * Along the way you will see two versions of some functions, selected
50 * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
51 * readable, and slow version, and the other is the same function
52 * where crucial parts have been manually inlined and are much faster.
56 /* Things should be fastest when this matches the machine word size */
57 /* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
58 /* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
59 /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
60 typedef FLAC__uint32 brword;
61 #define FLAC__BYTES_PER_WORD 4
62 #define FLAC__BITS_PER_WORD 32
63 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
64 #define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
65 /* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
67 #define SWAP_BE_WORD_TO_HOST(x) (x)
69 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
71 /* counts the # of zero MSBs in a word */
72 #define ALIGNED_UNARY_BITS(word) ( \
74 ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
75 ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
77 /* this alternate might be slightly faster on some systems/compilers: */
78 #define ALIGNED_UNARY_BITS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
82 * This should be at least twice as large as the largest number of words
83 * required to represent any 'number' (in any encoding) you are going to
84 * read. With FLAC this is on the order of maybe a few hundred bits.
85 * If the buffer is smaller than that, the decoder won't be able to read
86 * in a whole number that is in a variable length encoding (e.g. Rice).
87 * But to be practical it should be at least 1K bytes.
89 * Increase this number to decrease the number of read callbacks, at the
90 * expense of using more memory. Or decrease for the reverse effect,
91 * keeping in mind the limit from the first paragraph. The optimal size
92 * also depends on the CPU cache size and other factors; some twiddling
93 * may be necessary to squeeze out the best performance.
95 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
97 static const unsigned char byte_to_unary_table[] = {
98 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
99 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
100 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
101 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
104 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
119 #define min(x,y) ((x)<(y)?(x):(y))
123 #define max(x,y) ((x)>(y)?(x):(y))
125 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
127 #define FLAC__U64L(x) x
129 #define FLAC__U64L(x) x##LLU
136 struct FLAC__BitReader {
137 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
138 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
140 unsigned capacity; /* in words */
141 unsigned words; /* # of completed words in buffer */
142 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
143 unsigned consumed_words, consumed_bits; /* #words+(#bits of head word) already consumed from the front of buffer */
144 unsigned read_crc16; /* the running frame CRC */
145 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
146 FLAC__BitReaderReadCallback read_callback;
150 static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
152 register unsigned crc = br->read_crc16;
153 #if FLAC__BYTES_PER_WORD == 4
154 switch(br->crc16_align) {
155 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
156 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
157 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
158 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
160 #elif FLAC__BYTES_PER_WORD == 8
161 switch(br->crc16_align) {
162 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
163 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
164 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
165 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
166 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
167 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
168 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
169 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
172 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
173 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
174 br->read_crc16 = crc;
179 static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
185 /* first shift the unconsumed buffer data toward the front as much as possible */
186 if(br->consumed_words > 0) {
187 start = br->consumed_words;
188 end = br->words + (br->bytes? 1:0);
189 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
192 br->consumed_words = 0;
196 * set the target for reading, taking into account word alignment and endianness
198 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
200 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
201 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
203 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
204 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
205 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
206 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
207 * ^^-------target, bytes=3
208 * on LE machines, have to byteswap the odd tail word so nothing is
214 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
217 /* now it looks like:
218 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
219 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
220 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
221 * ^^-------target, bytes=3
224 /* read in the data; note that the callback may return a smaller number of bytes */
225 if(!br->read_callback(target, &bytes, br->client_data))
228 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
229 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
230 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
231 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
232 * now have to byteswap on LE machines:
236 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
237 for(start = br->words; start < end; start++)
238 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
241 /* now it looks like:
242 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
243 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
244 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
245 * finally we'll update the reader values:
247 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
248 br->words = end / FLAC__BYTES_PER_WORD;
249 br->bytes = end % FLAC__BYTES_PER_WORD;
254 /***********************************************************************
256 * Class constructor/destructor
258 ***********************************************************************/
260 FLAC__BitReader *FLAC__bitreader_new()
262 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
265 memset(br, 0, sizeof(FLAC__BitReader));
268 br->words = br->bytes = 0;
269 br->consumed_words = br->consumed_bits = 0;
270 br->read_callback = 0;
276 void FLAC__bitreader_delete(FLAC__BitReader *br)
278 FLAC__ASSERT(0 != br);
280 FLAC__bitreader_free(br);
284 /***********************************************************************
286 * Public class methods
288 ***********************************************************************/
290 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
292 FLAC__ASSERT(0 != br);
294 br->words = br->bytes = 0;
295 br->consumed_words = br->consumed_bits = 0;
296 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
297 br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
300 br->read_callback = rcb;
301 br->client_data = cd;
306 void FLAC__bitreader_free(FLAC__BitReader *br)
308 FLAC__ASSERT(0 != br);
314 br->words = br->bytes = 0;
315 br->consumed_words = br->consumed_bits = 0;
316 br->read_callback = 0;
320 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
322 br->words = br->bytes = 0;
323 br->consumed_words = br->consumed_bits = 0;
327 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
331 fprintf(out, "bitreader is NULL\n");
334 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);
336 for(i = 0; i < br->words; i++) {
337 fprintf(out, "%08X: ", i);
338 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
339 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
342 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
346 fprintf(out, "%08X: ", i);
347 for(j = 0; j < br->bytes*8; j++)
348 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
351 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
357 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
359 FLAC__ASSERT(0 != br);
360 FLAC__ASSERT(0 != br->buffer);
361 FLAC__ASSERT((br->consumed_bits & 7) == 0);
363 br->read_crc16 = (unsigned)seed;
364 br->crc16_align = br->consumed_bits;
367 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
369 FLAC__ASSERT(0 != br);
370 FLAC__ASSERT(0 != br->buffer);
371 FLAC__ASSERT((br->consumed_bits & 7) == 0);
372 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
374 /* CRC any tail bytes in a partially-consumed word */
375 if(br->consumed_bits) {
376 const brword tail = br->buffer[br->consumed_words];
378 if(br->crc16_align)fprintf(stderr,"@@@@@@ FLAC__bitreader_get_read_crc16() got nonzero crc align = %u\n",br->crc16_align);
380 /* non-zero crc align here can probably never happen with FLAC but check for consistency */
381 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
382 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
384 return br->read_crc16;
387 FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
389 return ((br->consumed_bits & 7) == 0);
392 FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
394 return 8 - (br->consumed_bits & 7);
397 FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
399 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
402 FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
404 FLAC__ASSERT(0 != br);
405 FLAC__ASSERT(0 != br->buffer);
407 FLAC__ASSERT(bits <= 32);
408 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
409 FLAC__ASSERT(br->consumed_words <= br->words);
411 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
412 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
414 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
419 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
420 if(!bitreader_read_from_client_(br))
423 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
424 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
425 if(br->consumed_bits) {
426 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
427 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
428 const brword word = br->buffer[br->consumed_words];
430 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
431 br->consumed_bits += bits;
434 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
436 crc16_update_word_(br, word);
437 br->consumed_words++;
438 br->consumed_bits = 0;
439 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 */
441 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
442 br->consumed_bits = bits;
447 const brword word = br->buffer[br->consumed_words];
448 if(bits < FLAC__BITS_PER_WORD) {
449 *val = word >> (FLAC__BITS_PER_WORD-bits);
450 br->consumed_bits = bits;
453 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
455 crc16_update_word_(br, word);
456 br->consumed_words++;
461 /* in this case we're starting our read at a partial tail word;
462 * the reader has guaranteed that we have at least 'bits' bits
463 * available to read, which makes this case simpler.
465 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
466 if(br->consumed_bits) {
467 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
468 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
469 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
470 br->consumed_bits += bits;
474 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
475 br->consumed_bits += bits;
481 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
483 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
484 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
492 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
497 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
499 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
506 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
513 FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
515 FLAC__uint32 x8, x32 = 0;
517 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
519 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
522 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
526 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
530 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
538 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
541 * OPT: a faster implementation is possible but probably not that useful
542 * since this is only called a couple of times in the metadata readers.
544 FLAC__ASSERT(0 != br);
545 FLAC__ASSERT(0 != br->buffer);
548 const unsigned n = br->consumed_bits & 7;
554 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
560 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
565 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
573 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
577 FLAC__ASSERT(0 != br);
578 FLAC__ASSERT(0 != br->buffer);
579 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
581 /* step 1: skip over partial head word to get word aligned */
582 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
583 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
589 /* step 2: skip whole words in chunks */
590 while(nvals >= FLAC__BYTES_PER_WORD) {
591 if(br->consumed_words < br->words) {
592 br->consumed_words++;
593 nvals -= FLAC__BYTES_PER_WORD;
595 else if(!bitreader_read_from_client_(br))
598 /* step 3: skip any remainder from partial tail bytes */
600 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
608 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
612 FLAC__ASSERT(0 != br);
613 FLAC__ASSERT(0 != br->buffer);
614 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
616 /* step 1: read from partial head word to get word aligned */
617 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
618 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
620 *val++ = (FLAC__byte)x;
625 /* step 2: read whole words in chunks */
626 while(nvals >= FLAC__BYTES_PER_WORD) {
627 if(br->consumed_words < br->words) {
628 const brword word = br->buffer[br->consumed_words++];
629 #if FLAC__BYTES_PER_WORD == 4
630 val[0] = (FLAC__byte)(word >> 24);
631 val[1] = (FLAC__byte)(word >> 16);
632 val[2] = (FLAC__byte)(word >> 8);
633 val[3] = (FLAC__byte)word;
634 #elif FLAC__BYTES_PER_WORD == 8
635 val[0] = (FLAC__byte)(word >> 56);
636 val[1] = (FLAC__byte)(word >> 48);
637 val[2] = (FLAC__byte)(word >> 40);
638 val[3] = (FLAC__byte)(word >> 32);
639 val[4] = (FLAC__byte)(word >> 24);
640 val[5] = (FLAC__byte)(word >> 16);
641 val[6] = (FLAC__byte)(word >> 8);
642 val[7] = (FLAC__byte)word;
644 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
645 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
647 val += FLAC__BYTES_PER_WORD;
648 nvals -= FLAC__BYTES_PER_WORD;
650 else if(!bitreader_read_from_client_(br))
653 /* step 3: read any remainder from partial tail bytes */
655 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
657 *val++ = (FLAC__byte)x;
664 FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
665 #ifdef FLAC__NO_MANUAL_INLINING
669 FLAC__ASSERT(0 != br);
670 FLAC__ASSERT(0 != br->buffer);
674 if(!FLAC__bitreader_read_bit(br, &bit))
687 FLAC__ASSERT(0 != br);
688 FLAC__ASSERT(0 != br->buffer);
692 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
693 brword b = br->buffer[br->consumed_words] << br->consumed_bits;
695 #if 0 /* too slow, but this is the idea: */
696 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
699 i = ALIGNED_UNARY_BITS(b);
703 br->consumed_bits += i;
704 if(br->consumed_bits == FLAC__BITS_PER_WORD) {
705 crc16_update_word_(br, br->buffer[br->consumed_words]);
706 br->consumed_words++;
707 br->consumed_bits = 0;
712 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
713 crc16_update_word_(br, br->buffer[br->consumed_words]);
714 br->consumed_words++;
715 br->consumed_bits = 0;
716 /* didn't find stop bit yet, have to keep going... */
719 /* at this point we've eaten up all the whole words; have to try
720 * reading through any tail bytes before calling the read callback.
721 * this is a repeat of the above logic adjusted for the fact we
722 * don't have a whole word. note though if the client is feeding
723 * us data a byte at a time (unlikely), br->consumed_bits may not
727 const unsigned end = br->bytes * 8;
728 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
730 #if 0 /* too slow, but this is the idea: */
731 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
734 i = ALIGNED_UNARY_BITS(b);
738 br->consumed_bits += i;
739 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
743 *val += end - br->consumed_bits;
744 br->consumed_bits += end;
745 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
746 /* didn't find stop bit yet, have to keep going... */
749 if(!bitreader_read_from_client_(br))
755 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
757 FLAC__uint32 lsbs = 0, msbs = 0;
760 FLAC__ASSERT(0 != br);
761 FLAC__ASSERT(0 != br->buffer);
762 FLAC__ASSERT(parameter <= 31);
764 /* read the unary MSBs and end bit */
765 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
768 /* read the binary LSBs */
769 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
772 /* compose the value */
773 uval = (msbs << parameter) | lsbs;
775 *val = -((int)(uval >> 1)) - 1;
777 *val = (int)(uval >> 1);
782 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
783 /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
784 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
788 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
790 /* try and get br->consumed_words and br->consumed_bits into register;
791 * must remember to flush them back to *br before calling other
792 * bitwriter functions that use them, and before returning */
793 register unsigned cwords;
794 register unsigned cbits;
796 FLAC__ASSERT(0 != br);
797 FLAC__ASSERT(0 != br->buffer);
798 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
799 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
800 FLAC__ASSERT(parameter < 32);
801 /* 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 */
806 cbits = br->consumed_bits;
807 cwords = br->consumed_words;
811 /* read unary part */
813 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
814 brword b = br->buffer[cwords] << cbits;
816 #if 0 /* too slow, but this is the idea: */
817 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
820 i = ALIGNED_UNARY_BITS(b);
826 if(cbits == FLAC__BITS_PER_WORD) {
827 crc16_update_word_(br, br->buffer[cwords]);
834 uval += FLAC__BITS_PER_WORD - cbits;
835 crc16_update_word_(br, br->buffer[cwords]);
838 /* didn't find stop bit yet, have to keep going... */
841 /* at this point we've eaten up all the whole words; have to try
842 * reading through any tail bytes before calling the read callback.
843 * this is a repeat of the above logic adjusted for the fact we
844 * don't have a whole word. note though if the client is feeding
845 * us data a byte at a time (unlikely), br->consumed_bits may not
849 const unsigned end = br->bytes * 8;
850 brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
852 #if 0 /* too slow, but this is the idea: */
853 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
856 i = ALIGNED_UNARY_BITS(b);
862 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
868 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
869 /* didn't find stop bit yet, have to keep going... */
872 /* flush registers and read; bitreader_read_from_client_() does
873 * not touch br->consumed_bits at all but we still need to set
874 * it in case it fails and we have to return false.
876 br->consumed_bits = cbits;
877 br->consumed_words = cwords;
878 if(!bitreader_read_from_client_(br))
880 cwords = br->consumed_words;
883 /* read binary part */
884 FLAC__ASSERT(cwords <= br->words);
887 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
888 /* flush registers and read; bitreader_read_from_client_() does
889 * not touch br->consumed_bits at all but we still need to set
890 * it in case it fails and we have to return false.
892 br->consumed_bits = cbits;
893 br->consumed_words = cwords;
894 if(!bitreader_read_from_client_(br))
896 cwords = br->consumed_words;
898 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
900 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
901 const unsigned n = FLAC__BITS_PER_WORD - cbits;
902 const brword word = br->buffer[cwords];
905 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
910 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
912 crc16_update_word_(br, word);
915 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 */
917 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
923 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
925 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
931 /* in this case we're starting our read at a partial tail word;
932 * the reader has guaranteed that we have at least 'bits' bits
933 * available to read, which makes this case simpler.
937 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
938 FLAC__ASSERT(cbits + bits <= br->bytes*8);
939 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
944 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
951 /* compose the value */
952 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
957 br->consumed_bits = cbits;
958 br->consumed_words = cwords;
969 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
971 FLAC__uint32 lsbs = 0, msbs = 0;
972 unsigned bit, uval, k;
974 FLAC__ASSERT(0 != br);
975 FLAC__ASSERT(0 != br->buffer);
977 k = FLAC__bitmath_ilog2(parameter);
979 /* read the unary MSBs and end bit */
980 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
983 /* read the binary LSBs */
984 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
987 if(parameter == 1u<<k) {
988 /* compose the value */
989 uval = (msbs << k) | lsbs;
992 unsigned d = (1 << (k+1)) - parameter;
994 if(!FLAC__bitreader_read_bit(br, &bit))
1000 /* compose the value */
1001 uval = msbs * parameter + lsbs;
1004 /* unfold unsigned to signed */
1006 *val = -((int)(uval >> 1)) - 1;
1008 *val = (int)(uval >> 1);
1013 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1015 FLAC__uint32 lsbs, msbs = 0;
1018 FLAC__ASSERT(0 != br);
1019 FLAC__ASSERT(0 != br->buffer);
1021 k = FLAC__bitmath_ilog2(parameter);
1023 /* read the unary MSBs and end bit */
1024 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1027 /* read the binary LSBs */
1028 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1031 if(parameter == 1u<<k) {
1032 /* compose the value */
1033 *val = (msbs << k) | lsbs;
1036 unsigned d = (1 << (k+1)) - parameter;
1038 if(!FLAC__bitreader_read_bit(br, &bit))
1044 /* compose the value */
1045 *val = msbs * parameter + lsbs;
1052 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1053 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1059 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1062 raw[(*rawlen)++] = (FLAC__byte)x;
1063 if(!(x & 0x80)) { /* 0xxxxxxx */
1067 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1071 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1075 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1079 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1083 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1092 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1095 raw[(*rawlen)++] = (FLAC__byte)x;
1096 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1107 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1108 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1114 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1117 raw[(*rawlen)++] = (FLAC__byte)x;
1118 if(!(x & 0x80)) { /* 0xxxxxxx */
1122 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1126 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1130 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1134 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1138 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1142 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1147 *val = FLAC__U64L(0xffffffffffffffff);
1151 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1154 raw[(*rawlen)++] = (FLAC__byte)x;
1155 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1156 *val = FLAC__U64L(0xffffffffffffffff);