blob: 4f3874fd1e3133990665ac2dc44bd636f5175836 [file] [log] [blame]
Josh Coalson423f8042007-01-28 17:40:26 +00001/* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
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.
14 *
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.
18 *
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.
30 */
31
32#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
36#include <stdlib.h> /* for malloc() */
37#include <string.h> /* for memcpy(), memset() */
Josh Coalson67757782007-02-17 01:16:20 +000038#ifdef _MSC_VER
Josh Coalson423f8042007-01-28 17:40:26 +000039#include <winsock.h> /* for ntohl() */
Josh Coalsonf67f2062007-02-13 02:12:13 +000040#elif defined FLAC__SYS_DARWIN
41#include <machine/endian.h> /* for ntohl() */
Josh Coalson423f8042007-01-28 17:40:26 +000042#else
43#include <netinet/in.h> /* for ntohl() */
44#endif
45#include "private/bitmath.h"
46#include "private/bitreader.h"
47#include "private/crc.h"
48#include "FLAC/assert.h"
49
Josh Coalson423f8042007-01-28 17:40:26 +000050/* Things should be fastest when this matches the machine word size */
51/* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
52/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
53/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
54typedef FLAC__uint32 brword;
55#define FLAC__BYTES_PER_WORD 4
56#define FLAC__BITS_PER_WORD 32
57#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
58#define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
59/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
60#if WORDS_BIGENDIAN
61#define SWAP_BE_WORD_TO_HOST(x) (x)
62#else
Josh Coalsonc85056b2007-02-04 02:57:48 +000063#ifdef _MSC_VER
64#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
65#else
Josh Coalson423f8042007-01-28 17:40:26 +000066#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
67#endif
Josh Coalsonc85056b2007-02-04 02:57:48 +000068#endif
Josh Coalson423f8042007-01-28 17:40:26 +000069/* counts the # of zero MSBs in a word */
70#define ALIGNED_UNARY_BITS(word) ( \
71 (word) <= 0xffff ? \
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] ) \
74)
75/* this alternate might be slightly faster on some systems/compilers: */
76#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])) )
77
78
79/*
80 * This should be at least twice as large as the largest number of words
81 * required to represent any 'number' (in any encoding) you are going to
82 * read. With FLAC this is on the order of maybe a few hundred bits.
83 * If the buffer is smaller than that, the decoder won't be able to read
84 * in a whole number that is in a variable length encoding (e.g. Rice).
85 * But to be practical it should be at least 1K bytes.
86 *
87 * Increase this number to decrease the number of read callbacks, at the
88 * expense of using more memory. Or decrease for the reverse effect,
89 * keeping in mind the limit from the first paragraph. The optimal size
90 * also depends on the CPU cache size and other factors; some twiddling
91 * may be necessary to squeeze out the best performance.
92 */
93static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
94
95static const unsigned char byte_to_unary_table[] = {
96 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
97 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
98 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
99 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
112};
113
114#ifdef min
115#undef min
116#endif
117#define min(x,y) ((x)<(y)?(x):(y))
118#ifdef max
119#undef max
120#endif
121#define max(x,y) ((x)>(y)?(x):(y))
122
123/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
124#ifdef _MSC_VER
125#define FLAC__U64L(x) x
126#else
127#define FLAC__U64L(x) x##LLU
128#endif
129
130#ifndef FLaC__INLINE
131#define FLaC__INLINE
132#endif
133
134struct FLAC__BitReader {
135 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
136 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
137 brword *buffer;
138 unsigned capacity; /* in words */
139 unsigned words; /* # of completed words in buffer */
140 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
141 unsigned consumed_words, consumed_bits; /* #words+(#bits of head word) already consumed from the front of buffer */
142 unsigned read_crc16; /* the running frame CRC */
143 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
144 FLAC__BitReaderReadCallback read_callback;
145 void *client_data;
Josh Coalson65454092007-03-13 16:14:36 +0000146 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000147};
148
Josh Coalsonc85056b2007-02-04 02:57:48 +0000149#ifdef _MSC_VER
150/* OPT: an MSVC built-in would be better */
151static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
152{
153 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
154 return (x>>16) | (x<<16);
155}
Josh Coalson65454092007-03-13 16:14:36 +0000156static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
157{
158 __asm {
159 mov edx, start
160 mov ecx, len
161 test ecx, ecx
162loop1:
163 jz done1
164 mov eax, [edx]
165 bswap eax
166 mov [edx], eax
167 add edx, 4
168 dec ecx
169 jmp short loop1
170done1:
171 }
172}
Josh Coalsonc85056b2007-02-04 02:57:48 +0000173#endif
174
Josh Coalson423f8042007-01-28 17:40:26 +0000175static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
176{
177 register unsigned crc = br->read_crc16;
178#if FLAC__BYTES_PER_WORD == 4
179 switch(br->crc16_align) {
180 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
181 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
182 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
183 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
184 }
185#elif FLAC__BYTES_PER_WORD == 8
186 switch(br->crc16_align) {
187 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
188 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
189 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
190 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
191 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
192 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
193 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
194 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
195 }
196#else
197 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
198 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
199 br->read_crc16 = crc;
200#endif
201 br->crc16_align = 0;
202}
203
204static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
205{
206 unsigned start, end;
207 size_t bytes;
208 FLAC__byte *target;
209
210 /* first shift the unconsumed buffer data toward the front as much as possible */
211 if(br->consumed_words > 0) {
212 start = br->consumed_words;
213 end = br->words + (br->bytes? 1:0);
214 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
215
216 br->words -= start;
217 br->consumed_words = 0;
218 }
219
220 /*
221 * set the target for reading, taking into account word alignment and endianness
222 */
223 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
224 if(bytes == 0)
225 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
226 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
227
228 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
229 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
230 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
231 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
232 * ^^-------target, bytes=3
233 * on LE machines, have to byteswap the odd tail word so nothing is
234 * overwritten:
235 */
236#if WORDS_BIGENDIAN
237#else
238 if(br->bytes)
239 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
240#endif
241
242 /* now it looks like:
243 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
244 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
245 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
246 * ^^-------target, bytes=3
247 */
248
249 /* read in the data; note that the callback may return a smaller number of bytes */
250 if(!br->read_callback(target, &bytes, br->client_data))
251 return false;
252
253 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
254 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
255 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
256 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
257 * now have to byteswap on LE machines:
258 */
259#if WORDS_BIGENDIAN
260#else
261 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
Josh Coalson65454092007-03-13 16:14:36 +0000262# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
263 if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
264 start = br->words;
265 local_swap32_block_(br->buffer + start, end - start);
266 }
267 else
268# endif
Josh Coalson423f8042007-01-28 17:40:26 +0000269 for(start = br->words; start < end; start++)
270 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
271#endif
272
273 /* now it looks like:
274 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
275 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
276 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
277 * finally we'll update the reader values:
278 */
279 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
280 br->words = end / FLAC__BYTES_PER_WORD;
281 br->bytes = end % FLAC__BYTES_PER_WORD;
282
283 return true;
284}
285
286/***********************************************************************
287 *
288 * Class constructor/destructor
289 *
290 ***********************************************************************/
291
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000292FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000293{
294 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
295
296 /* calloc() implies:
297 memset(br, 0, sizeof(FLAC__BitReader));
298 br->buffer = 0;
299 br->capacity = 0;
300 br->words = br->bytes = 0;
301 br->consumed_words = br->consumed_bits = 0;
302 br->read_callback = 0;
303 br->client_data = 0;
304 */
305 return br;
306}
307
308void FLAC__bitreader_delete(FLAC__BitReader *br)
309{
310 FLAC__ASSERT(0 != br);
311
312 FLAC__bitreader_free(br);
313 free(br);
314}
315
316/***********************************************************************
317 *
318 * Public class methods
319 *
320 ***********************************************************************/
321
Josh Coalson65454092007-03-13 16:14:36 +0000322FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000323{
324 FLAC__ASSERT(0 != br);
325
326 br->words = br->bytes = 0;
327 br->consumed_words = br->consumed_bits = 0;
328 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
329 br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
330 if(br->buffer == 0)
331 return false;
332 br->read_callback = rcb;
333 br->client_data = cd;
Josh Coalson65454092007-03-13 16:14:36 +0000334 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000335
336 return true;
337}
338
339void FLAC__bitreader_free(FLAC__BitReader *br)
340{
341 FLAC__ASSERT(0 != br);
342
343 if(0 != br->buffer)
344 free(br->buffer);
345 br->buffer = 0;
346 br->capacity = 0;
347 br->words = br->bytes = 0;
348 br->consumed_words = br->consumed_bits = 0;
349 br->read_callback = 0;
350 br->client_data = 0;
351}
352
353FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
354{
355 br->words = br->bytes = 0;
356 br->consumed_words = br->consumed_bits = 0;
357 return true;
358}
359
360void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
361{
362 unsigned i, j;
363 if(br == 0) {
364 fprintf(out, "bitreader is NULL\n");
365 }
366 else {
367 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);
368
369 for(i = 0; i < br->words; i++) {
370 fprintf(out, "%08X: ", i);
371 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
372 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
373 fprintf(out, ".");
374 else
375 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
376 fprintf(out, "\n");
377 }
378 if(br->bytes > 0) {
379 fprintf(out, "%08X: ", i);
380 for(j = 0; j < br->bytes*8; j++)
381 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
382 fprintf(out, ".");
383 else
384 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
385 fprintf(out, "\n");
386 }
387 }
388}
389
390void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
391{
392 FLAC__ASSERT(0 != br);
393 FLAC__ASSERT(0 != br->buffer);
394 FLAC__ASSERT((br->consumed_bits & 7) == 0);
395
396 br->read_crc16 = (unsigned)seed;
397 br->crc16_align = br->consumed_bits;
398}
399
400FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
401{
402 FLAC__ASSERT(0 != br);
403 FLAC__ASSERT(0 != br->buffer);
404 FLAC__ASSERT((br->consumed_bits & 7) == 0);
405 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
406
407 /* CRC any tail bytes in a partially-consumed word */
408 if(br->consumed_bits) {
409 const brword tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000410 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
411 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
412 }
413 return br->read_crc16;
414}
415
416FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
417{
418 return ((br->consumed_bits & 7) == 0);
419}
420
421FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
422{
423 return 8 - (br->consumed_bits & 7);
424}
425
426FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
427{
428 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
429}
430
431FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
432{
433 FLAC__ASSERT(0 != br);
434 FLAC__ASSERT(0 != br->buffer);
435
436 FLAC__ASSERT(bits <= 32);
437 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
438 FLAC__ASSERT(br->consumed_words <= br->words);
439
440 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
441 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
442
443 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
444 *val = 0;
445 return true;
446 }
447
448 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
449 if(!bitreader_read_from_client_(br))
450 return false;
451 }
452 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
453 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
454 if(br->consumed_bits) {
455 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
456 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
457 const brword word = br->buffer[br->consumed_words];
458 if(bits < n) {
459 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
460 br->consumed_bits += bits;
461 return true;
462 }
463 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
464 bits -= n;
465 crc16_update_word_(br, word);
466 br->consumed_words++;
467 br->consumed_bits = 0;
468 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 */
469 *val <<= bits;
470 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
471 br->consumed_bits = bits;
472 }
473 return true;
474 }
475 else {
476 const brword word = br->buffer[br->consumed_words];
477 if(bits < FLAC__BITS_PER_WORD) {
478 *val = word >> (FLAC__BITS_PER_WORD-bits);
479 br->consumed_bits = bits;
480 return true;
481 }
482 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
483 *val = word;
484 crc16_update_word_(br, word);
485 br->consumed_words++;
486 return true;
487 }
488 }
489 else {
490 /* in this case we're starting our read at a partial tail word;
491 * the reader has guaranteed that we have at least 'bits' bits
492 * available to read, which makes this case simpler.
493 */
494 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
495 if(br->consumed_bits) {
496 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
497 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
498 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
499 br->consumed_bits += bits;
500 return true;
501 }
502 else {
503 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
504 br->consumed_bits += bits;
505 return true;
506 }
507 }
508}
509
510FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
511{
512 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
513 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
514 return false;
515 /* sign-extend: */
516 *val <<= (32-bits);
517 *val >>= (32-bits);
518 return true;
519}
520
521FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
522{
523 FLAC__uint32 hi, lo;
524
525 if(bits > 32) {
526 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
527 return false;
528 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
529 return false;
530 *val = hi;
531 *val <<= 32;
532 *val |= lo;
533 }
534 else {
535 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
536 return false;
537 *val = lo;
538 }
539 return true;
540}
541
542FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
543{
544 FLAC__uint32 x8, x32 = 0;
545
546 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
547
548 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
549 return false;
550
551 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
552 return false;
553 x32 |= (x8 << 8);
554
555 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
556 return false;
557 x32 |= (x8 << 16);
558
559 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
560 return false;
561 x32 |= (x8 << 24);
562
563 *val = x32;
564 return true;
565}
566
567FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
568{
569 /*
570 * OPT: a faster implementation is possible but probably not that useful
571 * since this is only called a couple of times in the metadata readers.
572 */
573 FLAC__ASSERT(0 != br);
574 FLAC__ASSERT(0 != br->buffer);
575
576 if(bits > 0) {
577 const unsigned n = br->consumed_bits & 7;
578 unsigned m;
579 FLAC__uint32 x;
580
581 if(n != 0) {
582 m = min(8-n, bits);
583 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
584 return false;
585 bits -= m;
586 }
587 m = bits / 8;
588 if(m > 0) {
589 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
590 return false;
591 bits %= 8;
592 }
593 if(bits > 0) {
594 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
595 return false;
596 }
597 }
598
599 return true;
600}
601
602FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
603{
604 FLAC__uint32 x;
605
606 FLAC__ASSERT(0 != br);
607 FLAC__ASSERT(0 != br->buffer);
608 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
609
610 /* step 1: skip over partial head word to get word aligned */
611 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
612 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
613 return false;
614 nvals--;
615 }
616 if(0 == nvals)
617 return true;
618 /* step 2: skip whole words in chunks */
619 while(nvals >= FLAC__BYTES_PER_WORD) {
620 if(br->consumed_words < br->words) {
621 br->consumed_words++;
622 nvals -= FLAC__BYTES_PER_WORD;
623 }
624 else if(!bitreader_read_from_client_(br))
625 return false;
626 }
627 /* step 3: skip any remainder from partial tail bytes */
628 while(nvals) {
629 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
630 return false;
631 nvals--;
632 }
633
634 return true;
635}
636
637FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
638{
639 FLAC__uint32 x;
640
641 FLAC__ASSERT(0 != br);
642 FLAC__ASSERT(0 != br->buffer);
643 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
644
645 /* step 1: read from partial head word to get word aligned */
646 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
647 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
648 return false;
649 *val++ = (FLAC__byte)x;
650 nvals--;
651 }
652 if(0 == nvals)
653 return true;
654 /* step 2: read whole words in chunks */
655 while(nvals >= FLAC__BYTES_PER_WORD) {
656 if(br->consumed_words < br->words) {
657 const brword word = br->buffer[br->consumed_words++];
658#if FLAC__BYTES_PER_WORD == 4
659 val[0] = (FLAC__byte)(word >> 24);
660 val[1] = (FLAC__byte)(word >> 16);
661 val[2] = (FLAC__byte)(word >> 8);
662 val[3] = (FLAC__byte)word;
663#elif FLAC__BYTES_PER_WORD == 8
664 val[0] = (FLAC__byte)(word >> 56);
665 val[1] = (FLAC__byte)(word >> 48);
666 val[2] = (FLAC__byte)(word >> 40);
667 val[3] = (FLAC__byte)(word >> 32);
668 val[4] = (FLAC__byte)(word >> 24);
669 val[5] = (FLAC__byte)(word >> 16);
670 val[6] = (FLAC__byte)(word >> 8);
671 val[7] = (FLAC__byte)word;
672#else
673 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
674 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
675#endif
676 val += FLAC__BYTES_PER_WORD;
677 nvals -= FLAC__BYTES_PER_WORD;
678 }
679 else if(!bitreader_read_from_client_(br))
680 return false;
681 }
682 /* step 3: read any remainder from partial tail bytes */
683 while(nvals) {
684 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
685 return false;
686 *val++ = (FLAC__byte)x;
687 nvals--;
688 }
689
690 return true;
691}
692
693FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000694#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000695{
696 unsigned bit;
697
698 FLAC__ASSERT(0 != br);
699 FLAC__ASSERT(0 != br->buffer);
700
701 *val = 0;
702 while(1) {
703 if(!FLAC__bitreader_read_bit(br, &bit))
704 return false;
705 if(bit)
706 break;
707 else
708 *val++;
709 }
710 return true;
711}
712#else
713{
714 unsigned i;
715
716 FLAC__ASSERT(0 != br);
717 FLAC__ASSERT(0 != br->buffer);
718
719 *val = 0;
720 while(1) {
721 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
722 brword b = br->buffer[br->consumed_words] << br->consumed_bits;
723 if(b) {
724#if 0 /* too slow, but this is the idea: */
725 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
726 b <<= 1;
727#else
728 i = ALIGNED_UNARY_BITS(b);
729#endif
730 *val += i;
731 i++;
732 br->consumed_bits += i;
733 if(br->consumed_bits == FLAC__BITS_PER_WORD) {
734 crc16_update_word_(br, br->buffer[br->consumed_words]);
735 br->consumed_words++;
736 br->consumed_bits = 0;
737 }
738 return true;
739 }
740 else {
741 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
742 crc16_update_word_(br, br->buffer[br->consumed_words]);
743 br->consumed_words++;
744 br->consumed_bits = 0;
745 /* didn't find stop bit yet, have to keep going... */
746 }
747 }
748 /* at this point we've eaten up all the whole words; have to try
749 * reading through any tail bytes before calling the read callback.
750 * this is a repeat of the above logic adjusted for the fact we
751 * don't have a whole word. note though if the client is feeding
752 * us data a byte at a time (unlikely), br->consumed_bits may not
753 * be zero.
754 */
755 if(br->bytes) {
756 const unsigned end = br->bytes * 8;
757 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
758 if(b) {
759#if 0 /* too slow, but this is the idea: */
760 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
761 b <<= 1;
762#else
763 i = ALIGNED_UNARY_BITS(b);
764#endif
765 *val += i;
766 i++;
767 br->consumed_bits += i;
768 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
769 return true;
770 }
771 else {
772 *val += end - br->consumed_bits;
773 br->consumed_bits += end;
774 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
775 /* didn't find stop bit yet, have to keep going... */
776 }
777 }
778 if(!bitreader_read_from_client_(br))
779 return false;
780 }
781}
782#endif
783
784FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
785{
786 FLAC__uint32 lsbs = 0, msbs = 0;
787 unsigned uval;
788
789 FLAC__ASSERT(0 != br);
790 FLAC__ASSERT(0 != br->buffer);
791 FLAC__ASSERT(parameter <= 31);
792
793 /* read the unary MSBs and end bit */
794 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
795 return false;
796
797 /* read the binary LSBs */
798 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
799 return false;
800
801 /* compose the value */
802 uval = (msbs << parameter) | lsbs;
803 if(uval & 1)
804 *val = -((int)(uval >> 1)) - 1;
805 else
806 *val = (int)(uval >> 1);
807
808 return true;
809}
810
811/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
812/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
813FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
814{
815 unsigned i;
816 unsigned uval = 0;
817 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
818
819 /* try and get br->consumed_words and br->consumed_bits into register;
820 * must remember to flush them back to *br before calling other
821 * bitwriter functions that use them, and before returning */
822 register unsigned cwords;
823 register unsigned cbits;
824
825 FLAC__ASSERT(0 != br);
826 FLAC__ASSERT(0 != br->buffer);
827 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
828 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
829 FLAC__ASSERT(parameter < 32);
830 /* 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 */
831
832 if(nvals == 0)
833 return true;
834
835 cbits = br->consumed_bits;
836 cwords = br->consumed_words;
837
838 while(1) {
839
840 /* read unary part */
841 while(1) {
842 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
843 brword b = br->buffer[cwords] << cbits;
844 if(b) {
845#if 0 /* too slow, but this is the idea: */
846 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
847 b <<= 1;
848#else
849 i = ALIGNED_UNARY_BITS(b);
850#endif
851 uval += i;
852 bits = parameter;
853 i++;
854 cbits += i;
855 if(cbits == FLAC__BITS_PER_WORD) {
856 crc16_update_word_(br, br->buffer[cwords]);
857 cwords++;
858 cbits = 0;
859 }
860 goto break1;
861 }
862 else {
863 uval += FLAC__BITS_PER_WORD - cbits;
864 crc16_update_word_(br, br->buffer[cwords]);
865 cwords++;
866 cbits = 0;
867 /* didn't find stop bit yet, have to keep going... */
868 }
869 }
870 /* at this point we've eaten up all the whole words; have to try
871 * reading through any tail bytes before calling the read callback.
872 * this is a repeat of the above logic adjusted for the fact we
873 * don't have a whole word. note though if the client is feeding
874 * us data a byte at a time (unlikely), br->consumed_bits may not
875 * be zero.
876 */
877 if(br->bytes) {
878 const unsigned end = br->bytes * 8;
879 brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
880 if(b) {
881#if 0 /* too slow, but this is the idea: */
882 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
883 b <<= 1;
884#else
885 i = ALIGNED_UNARY_BITS(b);
886#endif
887 uval += i;
888 bits = parameter;
889 i++;
890 cbits += i;
891 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
892 goto break1;
893 }
894 else {
895 uval += end - cbits;
896 cbits += end;
897 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
898 /* didn't find stop bit yet, have to keep going... */
899 }
900 }
901 /* flush registers and read; bitreader_read_from_client_() does
902 * not touch br->consumed_bits at all but we still need to set
903 * it in case it fails and we have to return false.
904 */
905 br->consumed_bits = cbits;
906 br->consumed_words = cwords;
907 if(!bitreader_read_from_client_(br))
908 return false;
909 cwords = br->consumed_words;
910 }
911break1:
912 /* read binary part */
913 FLAC__ASSERT(cwords <= br->words);
914
915 if(bits) {
916 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
917 /* flush registers and read; bitreader_read_from_client_() does
918 * not touch br->consumed_bits at all but we still need to set
919 * it in case it fails and we have to return false.
920 */
921 br->consumed_bits = cbits;
922 br->consumed_words = cwords;
923 if(!bitreader_read_from_client_(br))
924 return false;
925 cwords = br->consumed_words;
926 }
927 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
928 if(cbits) {
929 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
930 const unsigned n = FLAC__BITS_PER_WORD - cbits;
931 const brword word = br->buffer[cwords];
932 if(bits < n) {
933 uval <<= bits;
934 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
935 cbits += bits;
936 goto break2;
937 }
938 uval <<= n;
939 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
940 bits -= n;
941 crc16_update_word_(br, word);
942 cwords++;
943 cbits = 0;
944 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 */
945 uval <<= bits;
946 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
947 cbits = bits;
948 }
949 goto break2;
950 }
951 else {
952 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
953 uval <<= bits;
954 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
955 cbits = bits;
956 goto break2;
957 }
958 }
959 else {
960 /* in this case we're starting our read at a partial tail word;
961 * the reader has guaranteed that we have at least 'bits' bits
962 * available to read, which makes this case simpler.
963 */
964 uval <<= bits;
965 if(cbits) {
966 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
967 FLAC__ASSERT(cbits + bits <= br->bytes*8);
968 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
969 cbits += bits;
970 goto break2;
971 }
972 else {
973 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
974 cbits += bits;
975 goto break2;
976 }
977 }
978 }
979break2:
980 /* compose the value */
981 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
982
983 /* are we done? */
984 --nvals;
985 if(nvals == 0) {
986 br->consumed_bits = cbits;
987 br->consumed_words = cwords;
988 return true;
989 }
990
991 uval = 0;
992 ++vals;
993
994 }
995}
996
997#if 0 /* UNUSED */
998FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
999{
1000 FLAC__uint32 lsbs = 0, msbs = 0;
1001 unsigned bit, uval, k;
1002
1003 FLAC__ASSERT(0 != br);
1004 FLAC__ASSERT(0 != br->buffer);
1005
1006 k = FLAC__bitmath_ilog2(parameter);
1007
1008 /* read the unary MSBs and end bit */
1009 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1010 return false;
1011
1012 /* read the binary LSBs */
1013 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1014 return false;
1015
1016 if(parameter == 1u<<k) {
1017 /* compose the value */
1018 uval = (msbs << k) | lsbs;
1019 }
1020 else {
1021 unsigned d = (1 << (k+1)) - parameter;
1022 if(lsbs >= d) {
1023 if(!FLAC__bitreader_read_bit(br, &bit))
1024 return false;
1025 lsbs <<= 1;
1026 lsbs |= bit;
1027 lsbs -= d;
1028 }
1029 /* compose the value */
1030 uval = msbs * parameter + lsbs;
1031 }
1032
1033 /* unfold unsigned to signed */
1034 if(uval & 1)
1035 *val = -((int)(uval >> 1)) - 1;
1036 else
1037 *val = (int)(uval >> 1);
1038
1039 return true;
1040}
1041
1042FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1043{
1044 FLAC__uint32 lsbs, msbs = 0;
1045 unsigned bit, k;
1046
1047 FLAC__ASSERT(0 != br);
1048 FLAC__ASSERT(0 != br->buffer);
1049
1050 k = FLAC__bitmath_ilog2(parameter);
1051
1052 /* read the unary MSBs and end bit */
1053 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1054 return false;
1055
1056 /* read the binary LSBs */
1057 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1058 return false;
1059
1060 if(parameter == 1u<<k) {
1061 /* compose the value */
1062 *val = (msbs << k) | lsbs;
1063 }
1064 else {
1065 unsigned d = (1 << (k+1)) - parameter;
1066 if(lsbs >= d) {
1067 if(!FLAC__bitreader_read_bit(br, &bit))
1068 return false;
1069 lsbs <<= 1;
1070 lsbs |= bit;
1071 lsbs -= d;
1072 }
1073 /* compose the value */
1074 *val = msbs * parameter + lsbs;
1075 }
1076
1077 return true;
1078}
1079#endif /* UNUSED */
1080
1081/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1082FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1083{
1084 FLAC__uint32 v = 0;
1085 FLAC__uint32 x;
1086 unsigned i;
1087
1088 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1089 return false;
1090 if(raw)
1091 raw[(*rawlen)++] = (FLAC__byte)x;
1092 if(!(x & 0x80)) { /* 0xxxxxxx */
1093 v = x;
1094 i = 0;
1095 }
1096 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1097 v = x & 0x1F;
1098 i = 1;
1099 }
1100 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1101 v = x & 0x0F;
1102 i = 2;
1103 }
1104 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1105 v = x & 0x07;
1106 i = 3;
1107 }
1108 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1109 v = x & 0x03;
1110 i = 4;
1111 }
1112 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1113 v = x & 0x01;
1114 i = 5;
1115 }
1116 else {
1117 *val = 0xffffffff;
1118 return true;
1119 }
1120 for( ; i; i--) {
1121 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1122 return false;
1123 if(raw)
1124 raw[(*rawlen)++] = (FLAC__byte)x;
1125 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1126 *val = 0xffffffff;
1127 return true;
1128 }
1129 v <<= 6;
1130 v |= (x & 0x3F);
1131 }
1132 *val = v;
1133 return true;
1134}
1135
1136/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1137FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1138{
1139 FLAC__uint64 v = 0;
1140 FLAC__uint32 x;
1141 unsigned i;
1142
1143 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1144 return false;
1145 if(raw)
1146 raw[(*rawlen)++] = (FLAC__byte)x;
1147 if(!(x & 0x80)) { /* 0xxxxxxx */
1148 v = x;
1149 i = 0;
1150 }
1151 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1152 v = x & 0x1F;
1153 i = 1;
1154 }
1155 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1156 v = x & 0x0F;
1157 i = 2;
1158 }
1159 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1160 v = x & 0x07;
1161 i = 3;
1162 }
1163 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1164 v = x & 0x03;
1165 i = 4;
1166 }
1167 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1168 v = x & 0x01;
1169 i = 5;
1170 }
1171 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1172 v = 0;
1173 i = 6;
1174 }
1175 else {
1176 *val = FLAC__U64L(0xffffffffffffffff);
1177 return true;
1178 }
1179 for( ; i; i--) {
1180 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1181 return false;
1182 if(raw)
1183 raw[(*rawlen)++] = (FLAC__byte)x;
1184 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1185 *val = FLAC__U64L(0xffffffffffffffff);
1186 return true;
1187 }
1188 v <<= 6;
1189 v |= (x & 0x3F);
1190 }
1191 *val = v;
1192 return true;
1193}