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 COUNT_ZERO_MSBS 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 /* "int __builtin_clz (unsigned int x) If x is 0, the result is undefined" */
61 static inline uint32_t
62 COUNT_ZERO_MSBS (uint32_t word)
66 return __builtin_clz (word);
69 /* counts the # of zero MSBs in a word */
70 #define COUNT_ZERO_MSBS(word) ( \
72 ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
73 ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
79 * This should be at least twice as large as the largest number of words
80 * required to represent any 'number' (in any encoding) you are going to
81 * read. With FLAC this is on the order of maybe a few hundred bits.
82 * If the buffer is smaller than that, the decoder won't be able to read
83 * in a whole number that is in a variable length encoding (e.g. Rice).
84 * But to be practical it should be at least 1K bytes.
86 * Increase this number to decrease the number of read callbacks, at the
87 * expense of using more memory. Or decrease for the reverse effect,
88 * keeping in mind the limit from the first paragraph. The optimal size
89 * also depends on the CPU cache size and other factors; some twiddling
90 * may be necessary to squeeze out the best performance.
92 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
94 static const unsigned char byte_to_unary_table[] = {
95 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
96 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
97 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
98 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
99 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
113 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
115 #define FLAC__U64L(x) x
117 #define FLAC__U64L(x) x##LLU
124 /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
125 struct FLAC__BitReader {
126 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
127 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
129 unsigned capacity; /* in words */
130 unsigned words; /* # of completed words in buffer */
131 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
132 unsigned consumed_words; /* #words ... */
133 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
134 unsigned read_crc16; /* the running frame CRC */
135 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
136 FLAC__BitReaderReadCallback read_callback;
138 FLAC__CPUInfo cpu_info;
141 static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
143 register unsigned crc = br->read_crc16;
144 #if FLAC__BYTES_PER_WORD == 4
145 switch(br->crc16_align) {
146 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
147 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
148 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
149 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
151 #elif FLAC__BYTES_PER_WORD == 8
152 switch(br->crc16_align) {
153 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
154 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
155 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
156 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
157 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
158 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
159 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
160 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
163 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
164 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
165 br->read_crc16 = crc;
170 /* would be static except it needs to be called by asm routines */
171 FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
177 /* first shift the unconsumed buffer data toward the front as much as possible */
178 if(br->consumed_words > 0) {
179 start = br->consumed_words;
180 end = br->words + (br->bytes? 1:0);
181 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
184 br->consumed_words = 0;
188 * set the target for reading, taking into account word alignment and endianness
190 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
192 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
193 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
195 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
196 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
197 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
198 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
199 * ^^-------target, bytes=3
200 * on LE machines, have to byteswap the odd tail word so nothing is
206 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
209 /* now it looks like:
210 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
211 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
212 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
213 * ^^-------target, bytes=3
216 /* read in the data; note that the callback may return a smaller number of bytes */
217 if(!br->read_callback(target, &bytes, br->client_data))
220 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
221 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
222 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
223 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
224 * now have to byteswap on LE machines:
228 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
229 for(start = br->words; start < end; start++)
230 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
233 /* now it looks like:
234 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
235 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
236 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
237 * finally we'll update the reader values:
239 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
240 br->words = end / FLAC__BYTES_PER_WORD;
241 br->bytes = end % FLAC__BYTES_PER_WORD;
246 /***********************************************************************
248 * Class constructor/destructor
250 ***********************************************************************/
252 FLAC__BitReader *FLAC__bitreader_new(void)
254 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
257 memset(br, 0, sizeof(FLAC__BitReader));
260 br->words = br->bytes = 0;
261 br->consumed_words = br->consumed_bits = 0;
262 br->read_callback = 0;
268 void FLAC__bitreader_delete(FLAC__BitReader *br)
270 FLAC__ASSERT(0 != br);
272 FLAC__bitreader_free(br);
276 /***********************************************************************
278 * Public class methods
280 ***********************************************************************/
282 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
284 FLAC__ASSERT(0 != br);
286 br->words = br->bytes = 0;
287 br->consumed_words = br->consumed_bits = 0;
288 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
289 br->buffer = malloc(sizeof(uint32_t) * br->capacity);
292 br->read_callback = rcb;
293 br->client_data = cd;
299 void FLAC__bitreader_free(FLAC__BitReader *br)
301 FLAC__ASSERT(0 != br);
307 br->words = br->bytes = 0;
308 br->consumed_words = br->consumed_bits = 0;
309 br->read_callback = 0;
313 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
315 br->words = br->bytes = 0;
316 br->consumed_words = br->consumed_bits = 0;
320 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
324 fprintf(out, "bitreader is NULL\n");
327 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);
329 for(i = 0; i < br->words; i++) {
330 fprintf(out, "%08X: ", i);
331 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
332 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
335 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
339 fprintf(out, "%08X: ", i);
340 for(j = 0; j < br->bytes*8; j++)
341 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
344 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
350 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
352 FLAC__ASSERT(0 != br);
353 FLAC__ASSERT(0 != br->buffer);
354 FLAC__ASSERT((br->consumed_bits & 7) == 0);
356 br->read_crc16 = (unsigned)seed;
357 br->crc16_align = br->consumed_bits;
360 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
362 FLAC__ASSERT(0 != br);
363 FLAC__ASSERT(0 != br->buffer);
364 FLAC__ASSERT((br->consumed_bits & 7) == 0);
365 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
367 /* CRC any tail bytes in a partially-consumed word */
368 if(br->consumed_bits) {
369 const uint32_t tail = br->buffer[br->consumed_words];
370 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
371 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
373 return br->read_crc16;
376 FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
378 return ((br->consumed_bits & 7) == 0);
381 FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
383 return 8 - (br->consumed_bits & 7);
386 FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
388 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
391 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
393 FLAC__ASSERT(0 != br);
394 FLAC__ASSERT(0 != br->buffer);
396 FLAC__ASSERT(bits <= 32);
397 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
398 FLAC__ASSERT(br->consumed_words <= br->words);
400 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
401 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
403 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
408 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
409 if(!bitreader_read_from_client_(br))
412 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
413 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
414 if(br->consumed_bits) {
415 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
416 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
417 const uint32_t word = br->buffer[br->consumed_words];
419 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
420 br->consumed_bits += bits;
423 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
425 crc16_update_word_(br, word);
426 br->consumed_words++;
427 br->consumed_bits = 0;
428 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 */
430 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
431 br->consumed_bits = bits;
436 const uint32_t word = br->buffer[br->consumed_words];
437 if(bits < FLAC__BITS_PER_WORD) {
438 *val = word >> (FLAC__BITS_PER_WORD-bits);
439 br->consumed_bits = bits;
442 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
444 crc16_update_word_(br, word);
445 br->consumed_words++;
450 /* in this case we're starting our read at a partial tail word;
451 * the reader has guaranteed that we have at least 'bits' bits
452 * available to read, which makes this case simpler.
454 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
455 if(br->consumed_bits) {
456 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
457 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
458 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
459 br->consumed_bits += bits;
463 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
464 br->consumed_bits += bits;
470 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
472 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
473 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
481 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
486 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
488 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
495 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
502 FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
504 FLAC__uint32 x8, x32 = 0;
506 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
508 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
511 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
515 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
519 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
527 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
530 * OPT: a faster implementation is possible but probably not that useful
531 * since this is only called a couple of times in the metadata readers.
533 FLAC__ASSERT(0 != br);
534 FLAC__ASSERT(0 != br->buffer);
537 const unsigned n = br->consumed_bits & 7;
542 m = flac_min(8-n, bits);
543 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
549 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
554 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
562 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
566 FLAC__ASSERT(0 != br);
567 FLAC__ASSERT(0 != br->buffer);
568 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
570 /* step 1: skip over partial head word to get word aligned */
571 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
572 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
578 /* step 2: skip whole words in chunks */
579 while(nvals >= FLAC__BYTES_PER_WORD) {
580 if(br->consumed_words < br->words) {
581 br->consumed_words++;
582 nvals -= FLAC__BYTES_PER_WORD;
584 else if(!bitreader_read_from_client_(br))
587 /* step 3: skip any remainder from partial tail bytes */
589 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
597 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
601 FLAC__ASSERT(0 != br);
602 FLAC__ASSERT(0 != br->buffer);
603 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
605 /* step 1: read from partial head word to get word aligned */
606 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
607 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
609 *val++ = (FLAC__byte)x;
614 /* step 2: read whole words in chunks */
615 while(nvals >= FLAC__BYTES_PER_WORD) {
616 if(br->consumed_words < br->words) {
617 const uint32_t word = br->buffer[br->consumed_words++];
618 #if FLAC__BYTES_PER_WORD == 4
619 val[0] = (FLAC__byte)(word >> 24);
620 val[1] = (FLAC__byte)(word >> 16);
621 val[2] = (FLAC__byte)(word >> 8);
622 val[3] = (FLAC__byte)word;
623 #elif FLAC__BYTES_PER_WORD == 8
624 val[0] = (FLAC__byte)(word >> 56);
625 val[1] = (FLAC__byte)(word >> 48);
626 val[2] = (FLAC__byte)(word >> 40);
627 val[3] = (FLAC__byte)(word >> 32);
628 val[4] = (FLAC__byte)(word >> 24);
629 val[5] = (FLAC__byte)(word >> 16);
630 val[6] = (FLAC__byte)(word >> 8);
631 val[7] = (FLAC__byte)word;
633 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
634 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
636 val += FLAC__BYTES_PER_WORD;
637 nvals -= FLAC__BYTES_PER_WORD;
639 else if(!bitreader_read_from_client_(br))
642 /* step 3: read any remainder from partial tail bytes */
644 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
646 *val++ = (FLAC__byte)x;
653 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
654 #if 0 /* slow but readable version */
658 FLAC__ASSERT(0 != br);
659 FLAC__ASSERT(0 != br->buffer);
663 if(!FLAC__bitreader_read_bit(br, &bit))
676 FLAC__ASSERT(0 != br);
677 FLAC__ASSERT(0 != br->buffer);
681 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
682 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
684 i = COUNT_ZERO_MSBS(b);
687 br->consumed_bits += i;
688 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
689 crc16_update_word_(br, br->buffer[br->consumed_words]);
690 br->consumed_words++;
691 br->consumed_bits = 0;
696 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
697 crc16_update_word_(br, br->buffer[br->consumed_words]);
698 br->consumed_words++;
699 br->consumed_bits = 0;
700 /* didn't find stop bit yet, have to keep going... */
703 /* at this point we've eaten up all the whole words; have to try
704 * reading through any tail bytes before calling the read callback.
705 * this is a repeat of the above logic adjusted for the fact we
706 * don't have a whole word. note though if the client is feeding
707 * us data a byte at a time (unlikely), br->consumed_bits may not
710 if(br->bytes*8 > br->consumed_bits) {
711 const unsigned end = br->bytes * 8;
712 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
714 i = COUNT_ZERO_MSBS(b);
717 br->consumed_bits += i;
718 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
722 *val += end - br->consumed_bits;
723 br->consumed_bits = end;
724 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
725 /* didn't find stop bit yet, have to keep going... */
728 if(!bitreader_read_from_client_(br))
734 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
736 FLAC__uint32 lsbs = 0, msbs = 0;
739 FLAC__ASSERT(0 != br);
740 FLAC__ASSERT(0 != br->buffer);
741 FLAC__ASSERT(parameter <= 31);
743 /* read the unary MSBs and end bit */
744 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
747 /* read the binary LSBs */
748 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
751 /* compose the value */
752 uval = (msbs << parameter) | lsbs;
754 *val = -((int)(uval >> 1)) - 1;
756 *val = (int)(uval >> 1);
761 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
762 /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
763 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
764 /* OPT: possibly faster version for use with MSVC */
769 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
771 /* try and get br->consumed_words and br->consumed_bits into register;
772 * must remember to flush them back to *br before calling other
773 * bitwriter functions that use them, and before returning */
774 register unsigned cwords;
775 register unsigned cbits;
777 FLAC__ASSERT(0 != br);
778 FLAC__ASSERT(0 != br->buffer);
779 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
780 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
781 FLAC__ASSERT(parameter < 32);
782 /* 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 */
787 cbits = br->consumed_bits;
788 cwords = br->consumed_words;
792 /* read unary part */
794 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
795 uint32_t b = br->buffer[cwords] << cbits;
797 #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
805 i = COUNT_ZERO_MSBS(b);
811 if(cbits == FLAC__BITS_PER_WORD) {
812 crc16_update_word_(br, br->buffer[cwords]);
819 uval += FLAC__BITS_PER_WORD - cbits;
820 crc16_update_word_(br, br->buffer[cwords]);
823 /* didn't find stop bit yet, have to keep going... */
826 /* at this point we've eaten up all the whole words; have to try
827 * reading through any tail bytes before calling the read callback.
828 * this is a repeat of the above logic adjusted for the fact we
829 * don't have a whole word. note though if the client is feeding
830 * us data a byte at a time (unlikely), br->consumed_bits may not
833 if(br->bytes*8 > cbits) {
834 const unsigned end = br->bytes * 8;
835 uint32_t b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
837 i = COUNT_ZERO_MSBS(b);
842 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
848 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
849 /* didn't find stop bit yet, have to keep going... */
852 /* flush registers and read; bitreader_read_from_client_() does
853 * not touch br->consumed_bits at all but we still need to set
854 * it in case it fails and we have to return false.
856 br->consumed_bits = cbits;
857 br->consumed_words = cwords;
858 if(!bitreader_read_from_client_(br))
860 cwords = br->consumed_words;
863 /* read binary part */
864 FLAC__ASSERT(cwords <= br->words);
867 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
868 /* flush registers and read; bitreader_read_from_client_() does
869 * not touch br->consumed_bits at all but we still need to set
870 * it in case it fails and we have to return false.
872 br->consumed_bits = cbits;
873 br->consumed_words = cwords;
874 if(!bitreader_read_from_client_(br))
876 cwords = br->consumed_words;
878 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
880 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
881 const unsigned n = FLAC__BITS_PER_WORD - cbits;
882 const uint32_t word = br->buffer[cwords];
885 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
890 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
892 crc16_update_word_(br, word);
895 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 */
897 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
903 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
905 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
911 /* in this case we're starting our read at a partial tail word;
912 * the reader has guaranteed that we have at least 'bits' bits
913 * available to read, which makes this case simpler.
917 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
918 FLAC__ASSERT(cbits + bits <= br->bytes*8);
919 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
924 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
931 /* compose the value */
932 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
937 br->consumed_bits = cbits;
938 br->consumed_words = cwords;
952 /* try and get br->consumed_words and br->consumed_bits into register;
953 * must remember to flush them back to *br before calling other
954 * bitwriter functions that use them, and before returning */
955 register unsigned cwords;
956 register unsigned cbits;
957 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
959 FLAC__ASSERT(0 != br);
960 FLAC__ASSERT(0 != br->buffer);
961 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
962 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
963 FLAC__ASSERT(parameter < 32);
964 /* 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 */
969 cbits = br->consumed_bits;
970 cwords = br->consumed_words;
971 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
975 /* read unary part */
977 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
978 uint32_t b = br->buffer[cwords] << cbits;
980 #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
989 i = COUNT_ZERO_MSBS(b);
993 cbits++; /* skip over stop bit */
994 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
995 crc16_update_word_(br, br->buffer[cwords]);
1002 uval += FLAC__BITS_PER_WORD - cbits;
1003 crc16_update_word_(br, br->buffer[cwords]);
1006 /* didn't find stop bit yet, have to keep going... */
1009 /* at this point we've eaten up all the whole words; have to try
1010 * reading through any tail bytes before calling the read callback.
1011 * this is a repeat of the above logic adjusted for the fact we
1012 * don't have a whole word. note though if the client is feeding
1013 * us data a byte at a time (unlikely), br->consumed_bits may not
1016 if(br->bytes*8 > cbits) {
1017 const unsigned end = br->bytes * 8;
1018 uint32_t b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
1020 i = COUNT_ZERO_MSBS(b);
1023 cbits++; /* skip over stop bit */
1024 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1028 uval += end - cbits;
1030 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1031 /* didn't find stop bit yet, have to keep going... */
1034 /* flush registers and read; bitreader_read_from_client_() does
1035 * not touch br->consumed_bits at all but we still need to set
1036 * it in case it fails and we have to return false.
1038 br->consumed_bits = cbits;
1039 br->consumed_words = cwords;
1040 if(!bitreader_read_from_client_(br))
1042 cwords = br->consumed_words;
1043 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
1044 /* + uval to offset our count by the # of unary bits already
1045 * consumed before the read, because we will add these back
1046 * in all at once at break1
1051 ucbits--; /* account for stop bit */
1053 /* read binary part */
1054 FLAC__ASSERT(cwords <= br->words);
1057 while(ucbits < parameter) {
1058 /* flush registers and read; bitreader_read_from_client_() does
1059 * not touch br->consumed_bits at all but we still need to set
1060 * it in case it fails and we have to return false.
1062 br->consumed_bits = cbits;
1063 br->consumed_words = cwords;
1064 if(!bitreader_read_from_client_(br))
1066 cwords = br->consumed_words;
1067 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
1069 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1071 /* this also works when consumed_bits==0, it's just slower than necessary for that case */
1072 const unsigned n = FLAC__BITS_PER_WORD - cbits;
1073 const uint32_t word = br->buffer[cwords];
1076 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1081 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1082 crc16_update_word_(br, word);
1084 cbits = parameter - n;
1085 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 */
1087 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1094 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1098 /* in this case we're starting our read at a partial tail word;
1099 * the reader has guaranteed that we have at least 'parameter'
1100 * bits available to read, which makes this case simpler.
1104 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
1105 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1106 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1111 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1116 ucbits -= parameter;
1118 /* compose the value */
1119 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1124 br->consumed_bits = cbits;
1125 br->consumed_words = cwords;
1137 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1139 FLAC__uint32 lsbs = 0, msbs = 0;
1140 unsigned bit, uval, k;
1142 FLAC__ASSERT(0 != br);
1143 FLAC__ASSERT(0 != br->buffer);
1145 k = FLAC__bitmath_ilog2(parameter);
1147 /* read the unary MSBs and end bit */
1148 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1151 /* read the binary LSBs */
1152 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1155 if(parameter == 1u<<k) {
1156 /* compose the value */
1157 uval = (msbs << k) | lsbs;
1160 unsigned d = (1 << (k+1)) - parameter;
1162 if(!FLAC__bitreader_read_bit(br, &bit))
1168 /* compose the value */
1169 uval = msbs * parameter + lsbs;
1172 /* unfold unsigned to signed */
1174 *val = -((int)(uval >> 1)) - 1;
1176 *val = (int)(uval >> 1);
1181 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1183 FLAC__uint32 lsbs, msbs = 0;
1186 FLAC__ASSERT(0 != br);
1187 FLAC__ASSERT(0 != br->buffer);
1189 k = FLAC__bitmath_ilog2(parameter);
1191 /* read the unary MSBs and end bit */
1192 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1195 /* read the binary LSBs */
1196 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1199 if(parameter == 1u<<k) {
1200 /* compose the value */
1201 *val = (msbs << k) | lsbs;
1204 unsigned d = (1 << (k+1)) - parameter;
1206 if(!FLAC__bitreader_read_bit(br, &bit))
1212 /* compose the value */
1213 *val = msbs * parameter + lsbs;
1220 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1221 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1227 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1230 raw[(*rawlen)++] = (FLAC__byte)x;
1231 if(!(x & 0x80)) { /* 0xxxxxxx */
1235 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1239 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1243 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1247 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1251 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1260 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1263 raw[(*rawlen)++] = (FLAC__byte)x;
1264 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1275 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1276 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1282 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1285 raw[(*rawlen)++] = (FLAC__byte)x;
1286 if(!(x & 0x80)) { /* 0xxxxxxx */
1290 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1294 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1298 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1302 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1306 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1310 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1315 *val = FLAC__U64L(0xffffffffffffffff);
1319 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1322 raw[(*rawlen)++] = (FLAC__byte)x;
1323 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1324 *val = FLAC__U64L(0xffffffffffffffff);