blob: ef24951c490ff0a517101e598797d92aad7cf488 [file] [log] [blame]
Josh Coalson423f8042007-01-28 17:40:26 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsondea0f5a2009-01-07 07:31:28 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
Josh Coalson423f8042007-01-28 17:40:26 +00003 *
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
Erik de Castro Lopoa5d1d4f2012-02-04 22:06:23 +110036#include <stdlib.h>
37#include <string.h>
Josh Coalson423f8042007-01-28 17:40:26 +000038#include "private/bitmath.h"
39#include "private/bitreader.h"
40#include "private/crc.h"
41#include "FLAC/assert.h"
Erik de Castro Lopoa5d1d4f2012-02-04 22:06:23 +110042#include "share/endswap.h"
Josh Coalson423f8042007-01-28 17:40:26 +000043
Josh Coalson423f8042007-01-28 17:40:26 +000044/* Things should be fastest when this matches the machine word size */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +000045/* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */
Josh Coalson423f8042007-01-28 17:40:26 +000046/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
47/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
48typedef FLAC__uint32 brword;
49#define FLAC__BYTES_PER_WORD 4
50#define FLAC__BITS_PER_WORD 32
51#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
Josh Coalson423f8042007-01-28 17:40:26 +000052/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
53#if WORDS_BIGENDIAN
54#define SWAP_BE_WORD_TO_HOST(x) (x)
55#else
Erik de Castro Lopo2f8b6a02012-03-05 21:12:20 +110056#define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
Josh Coalsonc85056b2007-02-04 02:57:48 +000057#endif
Erik de Castro Lopo5c44cd72012-03-30 21:55:08 +110058
59#if defined(__GNUC__)
60/* "int __builtin_clz (unsigned int x) If x is 0, the result is undefined" */
61static inline uint32_t
62COUNT_ZERO_MSBS (uint32_t word)
63{
64 if (word == 0)
65 return 32;
66 return __builtin_clz (word);
67}
68#else
Josh Coalson423f8042007-01-28 17:40:26 +000069/* counts the # of zero MSBs in a word */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +000070#define COUNT_ZERO_MSBS(word) ( \
Josh Coalson423f8042007-01-28 17:40:26 +000071 (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)
Erik de Castro Lopo5c44cd72012-03-30 21:55:08 +110075#endif
Josh Coalson423f8042007-01-28 17:40:26 +000076
77
78/*
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.
85 *
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.
91 */
92static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
93
94static 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
111};
112
113#ifdef min
114#undef min
115#endif
116#define min(x,y) ((x)<(y)?(x):(y))
117#ifdef max
118#undef max
119#endif
120#define max(x,y) ((x)>(y)?(x):(y))
121
122/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
123#ifdef _MSC_VER
124#define FLAC__U64L(x) x
125#else
126#define FLAC__U64L(x) x##LLU
127#endif
128
129#ifndef FLaC__INLINE
130#define FLaC__INLINE
131#endif
132
Josh Coalsonc63cf412007-03-17 05:21:36 +0000133/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
Josh Coalson423f8042007-01-28 17:40:26 +0000134struct 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] */
Josh Coalsonc63cf412007-03-17 05:21:36 +0000141 unsigned consumed_words; /* #words ... */
142 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000143 unsigned read_crc16; /* the running frame CRC */
144 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
145 FLAC__BitReaderReadCallback read_callback;
146 void *client_data;
Josh Coalson65454092007-03-13 16:14:36 +0000147 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000148};
149
150static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
151{
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);
159 }
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);
170 }
171#else
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;
175#endif
176 br->crc16_align = 0;
177}
178
Josh Coalsonc63cf412007-03-17 05:21:36 +0000179/* would be static except it needs to be called by asm routines */
180FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000181{
182 unsigned start, end;
183 size_t bytes;
184 FLAC__byte *target;
185
186 /* first shift the unconsumed buffer data toward the front as much as possible */
187 if(br->consumed_words > 0) {
188 start = br->consumed_words;
189 end = br->words + (br->bytes? 1:0);
190 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
191
192 br->words -= start;
193 br->consumed_words = 0;
194 }
195
196 /*
197 * set the target for reading, taking into account word alignment and endianness
198 */
199 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
200 if(bytes == 0)
201 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
202 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
203
204 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
205 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
206 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
207 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
208 * ^^-------target, bytes=3
209 * on LE machines, have to byteswap the odd tail word so nothing is
210 * overwritten:
211 */
212#if WORDS_BIGENDIAN
213#else
214 if(br->bytes)
215 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
216#endif
217
218 /* now it looks like:
219 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
220 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
221 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
222 * ^^-------target, bytes=3
223 */
224
225 /* read in the data; note that the callback may return a smaller number of bytes */
226 if(!br->read_callback(target, &bytes, br->client_data))
227 return false;
228
229 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
230 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
231 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
232 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
233 * now have to byteswap on LE machines:
234 */
235#if WORDS_BIGENDIAN
236#else
237 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
238 for(start = br->words; start < end; start++)
239 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
240#endif
241
242 /* now it looks like:
243 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
244 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
245 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
246 * finally we'll update the reader values:
247 */
248 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
249 br->words = end / FLAC__BYTES_PER_WORD;
250 br->bytes = end % FLAC__BYTES_PER_WORD;
251
252 return true;
253}
254
255/***********************************************************************
256 *
257 * Class constructor/destructor
258 *
259 ***********************************************************************/
260
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000261FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000262{
263 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
264
265 /* calloc() implies:
266 memset(br, 0, sizeof(FLAC__BitReader));
267 br->buffer = 0;
268 br->capacity = 0;
269 br->words = br->bytes = 0;
270 br->consumed_words = br->consumed_bits = 0;
271 br->read_callback = 0;
272 br->client_data = 0;
273 */
274 return br;
275}
276
277void FLAC__bitreader_delete(FLAC__BitReader *br)
278{
279 FLAC__ASSERT(0 != br);
280
281 FLAC__bitreader_free(br);
282 free(br);
283}
284
285/***********************************************************************
286 *
287 * Public class methods
288 *
289 ***********************************************************************/
290
Josh Coalson65454092007-03-13 16:14:36 +0000291FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000292{
293 FLAC__ASSERT(0 != br);
294
295 br->words = br->bytes = 0;
296 br->consumed_words = br->consumed_bits = 0;
297 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
298 br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
299 if(br->buffer == 0)
300 return false;
301 br->read_callback = rcb;
302 br->client_data = cd;
Josh Coalson65454092007-03-13 16:14:36 +0000303 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000304
305 return true;
306}
307
308void FLAC__bitreader_free(FLAC__BitReader *br)
309{
310 FLAC__ASSERT(0 != br);
311
312 if(0 != br->buffer)
313 free(br->buffer);
314 br->buffer = 0;
315 br->capacity = 0;
316 br->words = br->bytes = 0;
317 br->consumed_words = br->consumed_bits = 0;
318 br->read_callback = 0;
319 br->client_data = 0;
320}
321
322FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
323{
324 br->words = br->bytes = 0;
325 br->consumed_words = br->consumed_bits = 0;
326 return true;
327}
328
329void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
330{
331 unsigned i, j;
332 if(br == 0) {
333 fprintf(out, "bitreader is NULL\n");
334 }
335 else {
336 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);
337
338 for(i = 0; i < br->words; i++) {
339 fprintf(out, "%08X: ", i);
340 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
341 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
342 fprintf(out, ".");
343 else
344 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
345 fprintf(out, "\n");
346 }
347 if(br->bytes > 0) {
348 fprintf(out, "%08X: ", i);
349 for(j = 0; j < br->bytes*8; j++)
350 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
351 fprintf(out, ".");
352 else
353 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
354 fprintf(out, "\n");
355 }
356 }
357}
358
359void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
360{
361 FLAC__ASSERT(0 != br);
362 FLAC__ASSERT(0 != br->buffer);
363 FLAC__ASSERT((br->consumed_bits & 7) == 0);
364
365 br->read_crc16 = (unsigned)seed;
366 br->crc16_align = br->consumed_bits;
367}
368
369FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
370{
371 FLAC__ASSERT(0 != br);
372 FLAC__ASSERT(0 != br->buffer);
373 FLAC__ASSERT((br->consumed_bits & 7) == 0);
374 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
375
376 /* CRC any tail bytes in a partially-consumed word */
377 if(br->consumed_bits) {
378 const brword tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000379 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
380 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
381 }
382 return br->read_crc16;
383}
384
385FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
386{
387 return ((br->consumed_bits & 7) == 0);
388}
389
390FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
391{
392 return 8 - (br->consumed_bits & 7);
393}
394
395FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
396{
397 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
398}
399
Erik de Castro Lopo6b3b1372012-02-01 19:49:54 +1100400FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
Josh Coalson423f8042007-01-28 17:40:26 +0000401{
402 FLAC__ASSERT(0 != br);
403 FLAC__ASSERT(0 != br->buffer);
404
405 FLAC__ASSERT(bits <= 32);
406 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
407 FLAC__ASSERT(br->consumed_words <= br->words);
408
409 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
410 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
411
412 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
413 *val = 0;
414 return true;
415 }
416
417 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
418 if(!bitreader_read_from_client_(br))
419 return false;
420 }
421 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
422 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
423 if(br->consumed_bits) {
424 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
425 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
426 const brword word = br->buffer[br->consumed_words];
427 if(bits < n) {
428 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
429 br->consumed_bits += bits;
430 return true;
431 }
432 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
433 bits -= n;
434 crc16_update_word_(br, word);
435 br->consumed_words++;
436 br->consumed_bits = 0;
437 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 */
438 *val <<= bits;
439 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
440 br->consumed_bits = bits;
441 }
442 return true;
443 }
444 else {
445 const brword word = br->buffer[br->consumed_words];
446 if(bits < FLAC__BITS_PER_WORD) {
447 *val = word >> (FLAC__BITS_PER_WORD-bits);
448 br->consumed_bits = bits;
449 return true;
450 }
451 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
452 *val = word;
453 crc16_update_word_(br, word);
454 br->consumed_words++;
455 return true;
456 }
457 }
458 else {
459 /* in this case we're starting our read at a partial tail word;
460 * the reader has guaranteed that we have at least 'bits' bits
461 * available to read, which makes this case simpler.
462 */
463 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
464 if(br->consumed_bits) {
465 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
466 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
467 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
468 br->consumed_bits += bits;
469 return true;
470 }
471 else {
472 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
473 br->consumed_bits += bits;
474 return true;
475 }
476 }
477}
478
479FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
480{
481 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
482 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
483 return false;
484 /* sign-extend: */
485 *val <<= (32-bits);
486 *val >>= (32-bits);
487 return true;
488}
489
490FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
491{
492 FLAC__uint32 hi, lo;
493
494 if(bits > 32) {
495 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
496 return false;
497 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
498 return false;
499 *val = hi;
500 *val <<= 32;
501 *val |= lo;
502 }
503 else {
504 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
505 return false;
506 *val = lo;
507 }
508 return true;
509}
510
511FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
512{
513 FLAC__uint32 x8, x32 = 0;
514
515 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
516
517 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
518 return false;
519
520 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
521 return false;
522 x32 |= (x8 << 8);
523
524 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
525 return false;
526 x32 |= (x8 << 16);
527
528 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
529 return false;
530 x32 |= (x8 << 24);
531
532 *val = x32;
533 return true;
534}
535
536FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
537{
538 /*
539 * OPT: a faster implementation is possible but probably not that useful
540 * since this is only called a couple of times in the metadata readers.
541 */
542 FLAC__ASSERT(0 != br);
543 FLAC__ASSERT(0 != br->buffer);
544
545 if(bits > 0) {
546 const unsigned n = br->consumed_bits & 7;
547 unsigned m;
548 FLAC__uint32 x;
549
550 if(n != 0) {
551 m = min(8-n, bits);
552 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
553 return false;
554 bits -= m;
555 }
556 m = bits / 8;
557 if(m > 0) {
558 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
559 return false;
560 bits %= 8;
561 }
562 if(bits > 0) {
563 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
564 return false;
565 }
566 }
567
568 return true;
569}
570
571FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
572{
573 FLAC__uint32 x;
574
575 FLAC__ASSERT(0 != br);
576 FLAC__ASSERT(0 != br->buffer);
577 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
578
579 /* step 1: skip over partial head word to get word aligned */
580 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
581 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
582 return false;
583 nvals--;
584 }
585 if(0 == nvals)
586 return true;
587 /* step 2: skip whole words in chunks */
588 while(nvals >= FLAC__BYTES_PER_WORD) {
589 if(br->consumed_words < br->words) {
590 br->consumed_words++;
591 nvals -= FLAC__BYTES_PER_WORD;
592 }
593 else if(!bitreader_read_from_client_(br))
594 return false;
595 }
596 /* step 3: skip any remainder from partial tail bytes */
597 while(nvals) {
598 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
599 return false;
600 nvals--;
601 }
602
603 return true;
604}
605
606FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
607{
608 FLAC__uint32 x;
609
610 FLAC__ASSERT(0 != br);
611 FLAC__ASSERT(0 != br->buffer);
612 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
613
614 /* step 1: read from partial head word to get word aligned */
615 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
616 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
617 return false;
618 *val++ = (FLAC__byte)x;
619 nvals--;
620 }
621 if(0 == nvals)
622 return true;
623 /* step 2: read whole words in chunks */
624 while(nvals >= FLAC__BYTES_PER_WORD) {
625 if(br->consumed_words < br->words) {
626 const brword word = br->buffer[br->consumed_words++];
627#if FLAC__BYTES_PER_WORD == 4
628 val[0] = (FLAC__byte)(word >> 24);
629 val[1] = (FLAC__byte)(word >> 16);
630 val[2] = (FLAC__byte)(word >> 8);
631 val[3] = (FLAC__byte)word;
632#elif FLAC__BYTES_PER_WORD == 8
633 val[0] = (FLAC__byte)(word >> 56);
634 val[1] = (FLAC__byte)(word >> 48);
635 val[2] = (FLAC__byte)(word >> 40);
636 val[3] = (FLAC__byte)(word >> 32);
637 val[4] = (FLAC__byte)(word >> 24);
638 val[5] = (FLAC__byte)(word >> 16);
639 val[6] = (FLAC__byte)(word >> 8);
640 val[7] = (FLAC__byte)word;
641#else
642 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
643 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
644#endif
645 val += FLAC__BYTES_PER_WORD;
646 nvals -= FLAC__BYTES_PER_WORD;
647 }
648 else if(!bitreader_read_from_client_(br))
649 return false;
650 }
651 /* step 3: read any remainder from partial tail bytes */
652 while(nvals) {
653 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
654 return false;
655 *val++ = (FLAC__byte)x;
656 nvals--;
657 }
658
659 return true;
660}
661
Josh Coalson8e28e432009-01-03 02:10:18 +0000662FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000663#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000664{
665 unsigned bit;
666
667 FLAC__ASSERT(0 != br);
668 FLAC__ASSERT(0 != br->buffer);
669
670 *val = 0;
671 while(1) {
672 if(!FLAC__bitreader_read_bit(br, &bit))
673 return false;
674 if(bit)
675 break;
676 else
677 *val++;
678 }
679 return true;
680}
681#else
682{
683 unsigned i;
684
685 FLAC__ASSERT(0 != br);
686 FLAC__ASSERT(0 != br->buffer);
687
688 *val = 0;
689 while(1) {
690 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
691 brword b = br->buffer[br->consumed_words] << br->consumed_bits;
692 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000693 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000694 *val += i;
695 i++;
696 br->consumed_bits += i;
Josh Coalson276d6162007-03-22 03:20:12 +0000697 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +0000698 crc16_update_word_(br, br->buffer[br->consumed_words]);
699 br->consumed_words++;
700 br->consumed_bits = 0;
701 }
702 return true;
703 }
704 else {
705 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
706 crc16_update_word_(br, br->buffer[br->consumed_words]);
707 br->consumed_words++;
708 br->consumed_bits = 0;
709 /* didn't find stop bit yet, have to keep going... */
710 }
711 }
712 /* at this point we've eaten up all the whole words; have to try
713 * reading through any tail bytes before calling the read callback.
714 * this is a repeat of the above logic adjusted for the fact we
715 * don't have a whole word. note though if the client is feeding
716 * us data a byte at a time (unlikely), br->consumed_bits may not
717 * be zero.
718 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000719 if(br->bytes*8 > br->consumed_bits) {
Josh Coalson423f8042007-01-28 17:40:26 +0000720 const unsigned end = br->bytes * 8;
721 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
722 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000723 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000724 *val += i;
725 i++;
726 br->consumed_bits += i;
727 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
728 return true;
729 }
730 else {
731 *val += end - br->consumed_bits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000732 br->consumed_bits = end;
Josh Coalson423f8042007-01-28 17:40:26 +0000733 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
734 /* didn't find stop bit yet, have to keep going... */
735 }
736 }
737 if(!bitreader_read_from_client_(br))
738 return false;
739 }
740}
741#endif
742
743FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
744{
745 FLAC__uint32 lsbs = 0, msbs = 0;
746 unsigned uval;
747
748 FLAC__ASSERT(0 != br);
749 FLAC__ASSERT(0 != br->buffer);
750 FLAC__ASSERT(parameter <= 31);
751
752 /* read the unary MSBs and end bit */
753 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
754 return false;
755
756 /* read the binary LSBs */
757 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
758 return false;
759
760 /* compose the value */
761 uval = (msbs << parameter) | lsbs;
762 if(uval & 1)
763 *val = -((int)(uval >> 1)) - 1;
764 else
765 *val = (int)(uval >> 1);
766
767 return true;
768}
769
770/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
771/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
772FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
Josh Coalsone289ae52007-07-23 16:14:35 +0000773/* OPT: possibly faster version for use with MSVC */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000774#ifdef _MSC_VER
775{
776 unsigned i;
777 unsigned uval = 0;
778 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
779
780 /* try and get br->consumed_words and br->consumed_bits into register;
781 * must remember to flush them back to *br before calling other
782 * bitwriter functions that use them, and before returning */
783 register unsigned cwords;
784 register unsigned cbits;
785
786 FLAC__ASSERT(0 != br);
787 FLAC__ASSERT(0 != br->buffer);
788 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
789 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
790 FLAC__ASSERT(parameter < 32);
791 /* 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 */
792
793 if(nvals == 0)
794 return true;
795
796 cbits = br->consumed_bits;
797 cwords = br->consumed_words;
798
799 while(1) {
800
801 /* read unary part */
802 while(1) {
803 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
804 brword b = br->buffer[cwords] << cbits;
805 if(b) {
806#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
807 __asm {
808 bsr eax, b
809 not eax
810 and eax, 31
811 mov i, eax
812 }
813#else
814 i = COUNT_ZERO_MSBS(b);
815#endif
816 uval += i;
817 bits = parameter;
818 i++;
819 cbits += i;
820 if(cbits == FLAC__BITS_PER_WORD) {
821 crc16_update_word_(br, br->buffer[cwords]);
822 cwords++;
823 cbits = 0;
824 }
825 goto break1;
826 }
827 else {
828 uval += FLAC__BITS_PER_WORD - cbits;
829 crc16_update_word_(br, br->buffer[cwords]);
830 cwords++;
831 cbits = 0;
832 /* didn't find stop bit yet, have to keep going... */
833 }
834 }
835 /* at this point we've eaten up all the whole words; have to try
836 * reading through any tail bytes before calling the read callback.
837 * this is a repeat of the above logic adjusted for the fact we
838 * don't have a whole word. note though if the client is feeding
839 * us data a byte at a time (unlikely), br->consumed_bits may not
840 * be zero.
841 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000842 if(br->bytes*8 > cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000843 const unsigned end = br->bytes * 8;
844 brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
845 if(b) {
846 i = COUNT_ZERO_MSBS(b);
847 uval += i;
848 bits = parameter;
849 i++;
850 cbits += i;
851 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
852 goto break1;
853 }
854 else {
855 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000856 cbits = end;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000857 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
858 /* didn't find stop bit yet, have to keep going... */
859 }
860 }
861 /* flush registers and read; bitreader_read_from_client_() does
862 * not touch br->consumed_bits at all but we still need to set
863 * it in case it fails and we have to return false.
864 */
865 br->consumed_bits = cbits;
866 br->consumed_words = cwords;
867 if(!bitreader_read_from_client_(br))
868 return false;
869 cwords = br->consumed_words;
870 }
871break1:
872 /* read binary part */
873 FLAC__ASSERT(cwords <= br->words);
874
875 if(bits) {
876 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
877 /* flush registers and read; bitreader_read_from_client_() does
878 * not touch br->consumed_bits at all but we still need to set
879 * it in case it fails and we have to return false.
880 */
881 br->consumed_bits = cbits;
882 br->consumed_words = cwords;
883 if(!bitreader_read_from_client_(br))
884 return false;
885 cwords = br->consumed_words;
886 }
887 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
888 if(cbits) {
889 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
890 const unsigned n = FLAC__BITS_PER_WORD - cbits;
891 const brword word = br->buffer[cwords];
892 if(bits < n) {
893 uval <<= bits;
894 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
895 cbits += bits;
896 goto break2;
897 }
898 uval <<= n;
899 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
900 bits -= n;
901 crc16_update_word_(br, word);
902 cwords++;
903 cbits = 0;
904 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 */
905 uval <<= bits;
906 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
907 cbits = bits;
908 }
909 goto break2;
910 }
911 else {
912 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
913 uval <<= bits;
914 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
915 cbits = bits;
916 goto break2;
917 }
918 }
919 else {
920 /* in this case we're starting our read at a partial tail word;
921 * the reader has guaranteed that we have at least 'bits' bits
922 * available to read, which makes this case simpler.
923 */
924 uval <<= bits;
925 if(cbits) {
926 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
927 FLAC__ASSERT(cbits + bits <= br->bytes*8);
928 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
929 cbits += bits;
930 goto break2;
931 }
932 else {
933 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
934 cbits += bits;
935 goto break2;
936 }
937 }
938 }
939break2:
940 /* compose the value */
941 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
942
943 /* are we done? */
944 --nvals;
945 if(nvals == 0) {
946 br->consumed_bits = cbits;
947 br->consumed_words = cwords;
948 return true;
949 }
950
951 uval = 0;
952 ++vals;
953
954 }
955}
956#else
Josh Coalson423f8042007-01-28 17:40:26 +0000957{
958 unsigned i;
959 unsigned uval = 0;
Josh Coalson423f8042007-01-28 17:40:26 +0000960
961 /* try and get br->consumed_words and br->consumed_bits into register;
962 * must remember to flush them back to *br before calling other
963 * bitwriter functions that use them, and before returning */
964 register unsigned cwords;
965 register unsigned cbits;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000966 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000967
968 FLAC__ASSERT(0 != br);
969 FLAC__ASSERT(0 != br->buffer);
970 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
971 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
972 FLAC__ASSERT(parameter < 32);
Josh Coalsonc63cf412007-03-17 05:21:36 +0000973 /* 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 */
Josh Coalson423f8042007-01-28 17:40:26 +0000974
975 if(nvals == 0)
976 return true;
977
978 cbits = br->consumed_bits;
979 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000980 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000981
982 while(1) {
983
984 /* read unary part */
985 while(1) {
986 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
987 brword b = br->buffer[cwords] << cbits;
988 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000989#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
990 asm volatile (
991 "bsrl %1, %0;"
992 "notl %0;"
993 "andl $31, %0;"
994 : "=r"(i)
995 : "r"(b)
996 );
Josh Coalson423f8042007-01-28 17:40:26 +0000997#else
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000998 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000999#endif
1000 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +00001001 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +00001002 cbits++; /* skip over stop bit */
Josh Coalson276d6162007-03-22 03:20:12 +00001003 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +00001004 crc16_update_word_(br, br->buffer[cwords]);
1005 cwords++;
1006 cbits = 0;
1007 }
1008 goto break1;
1009 }
1010 else {
1011 uval += FLAC__BITS_PER_WORD - cbits;
1012 crc16_update_word_(br, br->buffer[cwords]);
1013 cwords++;
1014 cbits = 0;
1015 /* didn't find stop bit yet, have to keep going... */
1016 }
1017 }
1018 /* at this point we've eaten up all the whole words; have to try
1019 * reading through any tail bytes before calling the read callback.
1020 * this is a repeat of the above logic adjusted for the fact we
1021 * don't have a whole word. note though if the client is feeding
1022 * us data a byte at a time (unlikely), br->consumed_bits may not
1023 * be zero.
1024 */
Josh Coalsonee51fc02009-01-06 17:14:31 +00001025 if(br->bytes*8 > cbits) {
Josh Coalson423f8042007-01-28 17:40:26 +00001026 const unsigned end = br->bytes * 8;
Josh Coalsonc63cf412007-03-17 05:21:36 +00001027 brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001028 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001029 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +00001030 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +00001031 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +00001032 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +00001033 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1034 goto break1;
1035 }
1036 else {
1037 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +00001038 cbits = end;
Josh Coalson423f8042007-01-28 17:40:26 +00001039 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1040 /* didn't find stop bit yet, have to keep going... */
1041 }
1042 }
1043 /* flush registers and read; bitreader_read_from_client_() does
1044 * not touch br->consumed_bits at all but we still need to set
1045 * it in case it fails and we have to return false.
1046 */
1047 br->consumed_bits = cbits;
1048 br->consumed_words = cwords;
1049 if(!bitreader_read_from_client_(br))
1050 return false;
1051 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001052 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
1053 /* + uval to offset our count by the # of unary bits already
1054 * consumed before the read, because we will add these back
1055 * in all at once at break1
1056 */
Josh Coalson423f8042007-01-28 17:40:26 +00001057 }
1058break1:
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001059 ucbits -= uval;
1060 ucbits--; /* account for stop bit */
1061
Josh Coalson423f8042007-01-28 17:40:26 +00001062 /* read binary part */
1063 FLAC__ASSERT(cwords <= br->words);
1064
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001065 if(parameter) {
1066 while(ucbits < parameter) {
Josh Coalson423f8042007-01-28 17:40:26 +00001067 /* flush registers and read; bitreader_read_from_client_() does
1068 * not touch br->consumed_bits at all but we still need to set
1069 * it in case it fails and we have to return false.
1070 */
1071 br->consumed_bits = cbits;
1072 br->consumed_words = cwords;
1073 if(!bitreader_read_from_client_(br))
1074 return false;
1075 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001076 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001077 }
1078 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1079 if(cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001080 /* this also works when consumed_bits==0, it's just slower than necessary for that case */
Josh Coalson423f8042007-01-28 17:40:26 +00001081 const unsigned n = FLAC__BITS_PER_WORD - cbits;
1082 const brword word = br->buffer[cwords];
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001083 if(parameter < n) {
1084 uval <<= parameter;
1085 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1086 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001087 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001088 else {
1089 uval <<= n;
1090 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1091 crc16_update_word_(br, word);
1092 cwords++;
1093 cbits = parameter - n;
1094 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 */
1095 uval <<= cbits;
1096 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1097 }
Josh Coalson423f8042007-01-28 17:40:26 +00001098 }
Josh Coalson423f8042007-01-28 17:40:26 +00001099 }
1100 else {
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001101 cbits = parameter;
Josh Coalson276d6162007-03-22 03:20:12 +00001102 uval <<= parameter;
1103 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001104 }
1105 }
1106 else {
1107 /* in this case we're starting our read at a partial tail word;
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001108 * the reader has guaranteed that we have at least 'parameter'
1109 * bits available to read, which makes this case simpler.
Josh Coalson423f8042007-01-28 17:40:26 +00001110 */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001111 uval <<= parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001112 if(cbits) {
1113 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001114 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1115 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1116 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001117 }
1118 else {
Josh Coalson276d6162007-03-22 03:20:12 +00001119 cbits = parameter;
1120 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001121 }
1122 }
1123 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001124
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001125 ucbits -= parameter;
1126
Josh Coalson423f8042007-01-28 17:40:26 +00001127 /* compose the value */
1128 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1129
1130 /* are we done? */
1131 --nvals;
1132 if(nvals == 0) {
1133 br->consumed_bits = cbits;
1134 br->consumed_words = cwords;
1135 return true;
1136 }
1137
1138 uval = 0;
1139 ++vals;
1140
1141 }
1142}
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001143#endif
Josh Coalson423f8042007-01-28 17:40:26 +00001144
1145#if 0 /* UNUSED */
1146FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1147{
1148 FLAC__uint32 lsbs = 0, msbs = 0;
1149 unsigned bit, uval, k;
1150
1151 FLAC__ASSERT(0 != br);
1152 FLAC__ASSERT(0 != br->buffer);
1153
1154 k = FLAC__bitmath_ilog2(parameter);
1155
1156 /* read the unary MSBs and end bit */
1157 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1158 return false;
1159
1160 /* read the binary LSBs */
1161 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1162 return false;
1163
1164 if(parameter == 1u<<k) {
1165 /* compose the value */
1166 uval = (msbs << k) | lsbs;
1167 }
1168 else {
1169 unsigned d = (1 << (k+1)) - parameter;
1170 if(lsbs >= d) {
1171 if(!FLAC__bitreader_read_bit(br, &bit))
1172 return false;
1173 lsbs <<= 1;
1174 lsbs |= bit;
1175 lsbs -= d;
1176 }
1177 /* compose the value */
1178 uval = msbs * parameter + lsbs;
1179 }
1180
1181 /* unfold unsigned to signed */
1182 if(uval & 1)
1183 *val = -((int)(uval >> 1)) - 1;
1184 else
1185 *val = (int)(uval >> 1);
1186
1187 return true;
1188}
1189
1190FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1191{
1192 FLAC__uint32 lsbs, msbs = 0;
1193 unsigned bit, k;
1194
1195 FLAC__ASSERT(0 != br);
1196 FLAC__ASSERT(0 != br->buffer);
1197
1198 k = FLAC__bitmath_ilog2(parameter);
1199
1200 /* read the unary MSBs and end bit */
1201 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1202 return false;
1203
1204 /* read the binary LSBs */
1205 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1206 return false;
1207
1208 if(parameter == 1u<<k) {
1209 /* compose the value */
1210 *val = (msbs << k) | lsbs;
1211 }
1212 else {
1213 unsigned d = (1 << (k+1)) - parameter;
1214 if(lsbs >= d) {
1215 if(!FLAC__bitreader_read_bit(br, &bit))
1216 return false;
1217 lsbs <<= 1;
1218 lsbs |= bit;
1219 lsbs -= d;
1220 }
1221 /* compose the value */
1222 *val = msbs * parameter + lsbs;
1223 }
1224
1225 return true;
1226}
1227#endif /* UNUSED */
1228
1229/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1230FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1231{
1232 FLAC__uint32 v = 0;
1233 FLAC__uint32 x;
1234 unsigned i;
1235
1236 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1237 return false;
1238 if(raw)
1239 raw[(*rawlen)++] = (FLAC__byte)x;
1240 if(!(x & 0x80)) { /* 0xxxxxxx */
1241 v = x;
1242 i = 0;
1243 }
1244 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1245 v = x & 0x1F;
1246 i = 1;
1247 }
1248 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1249 v = x & 0x0F;
1250 i = 2;
1251 }
1252 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1253 v = x & 0x07;
1254 i = 3;
1255 }
1256 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1257 v = x & 0x03;
1258 i = 4;
1259 }
1260 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1261 v = x & 0x01;
1262 i = 5;
1263 }
1264 else {
1265 *val = 0xffffffff;
1266 return true;
1267 }
1268 for( ; i; i--) {
1269 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1270 return false;
1271 if(raw)
1272 raw[(*rawlen)++] = (FLAC__byte)x;
1273 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1274 *val = 0xffffffff;
1275 return true;
1276 }
1277 v <<= 6;
1278 v |= (x & 0x3F);
1279 }
1280 *val = v;
1281 return true;
1282}
1283
1284/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1285FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1286{
1287 FLAC__uint64 v = 0;
1288 FLAC__uint32 x;
1289 unsigned i;
1290
1291 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1292 return false;
1293 if(raw)
1294 raw[(*rawlen)++] = (FLAC__byte)x;
1295 if(!(x & 0x80)) { /* 0xxxxxxx */
1296 v = x;
1297 i = 0;
1298 }
1299 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1300 v = x & 0x1F;
1301 i = 1;
1302 }
1303 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1304 v = x & 0x0F;
1305 i = 2;
1306 }
1307 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1308 v = x & 0x07;
1309 i = 3;
1310 }
1311 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1312 v = x & 0x03;
1313 i = 4;
1314 }
1315 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1316 v = x & 0x01;
1317 i = 5;
1318 }
1319 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1320 v = 0;
1321 i = 6;
1322 }
1323 else {
1324 *val = FLAC__U64L(0xffffffffffffffff);
1325 return true;
1326 }
1327 for( ; i; i--) {
1328 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1329 return false;
1330 if(raw)
1331 raw[(*rawlen)++] = (FLAC__byte)x;
1332 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1333 *val = FLAC__U64L(0xffffffffffffffff);
1334 return true;
1335 }
1336 v <<= 6;
1337 v |= (x & 0x3F);
1338 }
1339 *val = v;
1340 return true;
1341}