blob: 7e17fd8225263b60366d860f4224eb1992f35a92 [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"
Cristian Rodríguezf0296252012-04-05 19:39:37 -030041#include "private/macros.h"
Josh Coalson423f8042007-01-28 17:40:26 +000042#include "FLAC/assert.h"
Erik de Castro Lopoa5d1d4f2012-02-04 22:06:23 +110043#include "share/endswap.h"
Josh Coalson423f8042007-01-28 17:40:26 +000044
Josh Coalson423f8042007-01-28 17:40:26 +000045/* Things should be fastest when this matches the machine word size */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +000046/* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +110047/* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
Josh Coalson423f8042007-01-28 17:40:26 +000048/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +110049#define FLAC__BYTES_PER_WORD 4 /* sizeof uint32_t */
50#define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD)
Josh Coalson423f8042007-01-28 17:40:26 +000051#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +110052/* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
Josh Coalson423f8042007-01-28 17:40:26 +000053#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
Josh Coalson423f8042007-01-28 17:40:26 +0000113/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
114#ifdef _MSC_VER
115#define FLAC__U64L(x) x
116#else
117#define FLAC__U64L(x) x##LLU
118#endif
119
120#ifndef FLaC__INLINE
121#define FLaC__INLINE
122#endif
123
Josh Coalsonc63cf412007-03-17 05:21:36 +0000124/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
Josh Coalson423f8042007-01-28 17:40:26 +0000125struct 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 */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100128 uint32_t *buffer;
Josh Coalson423f8042007-01-28 17:40:26 +0000129 unsigned capacity; /* in words */
130 unsigned words; /* # of completed words in buffer */
131 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
Josh Coalsonc63cf412007-03-17 05:21:36 +0000132 unsigned consumed_words; /* #words ... */
133 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000134 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;
137 void *client_data;
Josh Coalson65454092007-03-13 16:14:36 +0000138 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000139};
140
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100141static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
Josh Coalson423f8042007-01-28 17:40:26 +0000142{
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);
150 }
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);
161 }
162#else
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;
166#endif
167 br->crc16_align = 0;
168}
169
Josh Coalsonc63cf412007-03-17 05:21:36 +0000170/* would be static except it needs to be called by asm routines */
171FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000172{
173 unsigned start, end;
174 size_t bytes;
175 FLAC__byte *target;
176
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));
182
183 br->words -= start;
184 br->consumed_words = 0;
185 }
186
187 /*
188 * set the target for reading, taking into account word alignment and endianness
189 */
190 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
191 if(bytes == 0)
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;
194
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100195 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
Josh Coalson423f8042007-01-28 17:40:26 +0000196 * 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
201 * overwritten:
202 */
203#if WORDS_BIGENDIAN
204#else
205 if(br->bytes)
206 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
207#endif
208
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
214 */
215
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))
218 return false;
219
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:
225 */
226#if WORDS_BIGENDIAN
227#else
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]);
231#endif
232
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:
238 */
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;
242
243 return true;
244}
245
246/***********************************************************************
247 *
248 * Class constructor/destructor
249 *
250 ***********************************************************************/
251
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000252FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000253{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000254 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
Josh Coalson423f8042007-01-28 17:40:26 +0000255
256 /* calloc() implies:
257 memset(br, 0, sizeof(FLAC__BitReader));
258 br->buffer = 0;
259 br->capacity = 0;
260 br->words = br->bytes = 0;
261 br->consumed_words = br->consumed_bits = 0;
262 br->read_callback = 0;
263 br->client_data = 0;
264 */
265 return br;
266}
267
268void FLAC__bitreader_delete(FLAC__BitReader *br)
269{
270 FLAC__ASSERT(0 != br);
271
272 FLAC__bitreader_free(br);
273 free(br);
274}
275
276/***********************************************************************
277 *
278 * Public class methods
279 *
280 ***********************************************************************/
281
Josh Coalson65454092007-03-13 16:14:36 +0000282FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000283{
284 FLAC__ASSERT(0 != br);
285
286 br->words = br->bytes = 0;
287 br->consumed_words = br->consumed_bits = 0;
288 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000289 br->buffer = malloc(sizeof(uint32_t) * br->capacity);
Josh Coalson423f8042007-01-28 17:40:26 +0000290 if(br->buffer == 0)
291 return false;
292 br->read_callback = rcb;
293 br->client_data = cd;
Josh Coalson65454092007-03-13 16:14:36 +0000294 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000295
296 return true;
297}
298
299void FLAC__bitreader_free(FLAC__BitReader *br)
300{
301 FLAC__ASSERT(0 != br);
302
303 if(0 != br->buffer)
304 free(br->buffer);
305 br->buffer = 0;
306 br->capacity = 0;
307 br->words = br->bytes = 0;
308 br->consumed_words = br->consumed_bits = 0;
309 br->read_callback = 0;
310 br->client_data = 0;
311}
312
313FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
314{
315 br->words = br->bytes = 0;
316 br->consumed_words = br->consumed_bits = 0;
317 return true;
318}
319
320void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
321{
322 unsigned i, j;
323 if(br == 0) {
324 fprintf(out, "bitreader is NULL\n");
325 }
326 else {
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);
328
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))
333 fprintf(out, ".");
334 else
335 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
336 fprintf(out, "\n");
337 }
338 if(br->bytes > 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))
342 fprintf(out, ".");
343 else
344 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
345 fprintf(out, "\n");
346 }
347 }
348}
349
350void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
351{
352 FLAC__ASSERT(0 != br);
353 FLAC__ASSERT(0 != br->buffer);
354 FLAC__ASSERT((br->consumed_bits & 7) == 0);
355
356 br->read_crc16 = (unsigned)seed;
357 br->crc16_align = br->consumed_bits;
358}
359
360FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
361{
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);
366
367 /* CRC any tail bytes in a partially-consumed word */
368 if(br->consumed_bits) {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100369 const uint32_t tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000370 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);
372 }
373 return br->read_crc16;
374}
375
376FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
377{
378 return ((br->consumed_bits & 7) == 0);
379}
380
381FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
382{
383 return 8 - (br->consumed_bits & 7);
384}
385
386FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
387{
388 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
389}
390
Erik de Castro Lopo6b3b1372012-02-01 19:49:54 +1100391FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
Josh Coalson423f8042007-01-28 17:40:26 +0000392{
393 FLAC__ASSERT(0 != br);
394 FLAC__ASSERT(0 != br->buffer);
395
396 FLAC__ASSERT(bits <= 32);
397 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
398 FLAC__ASSERT(br->consumed_words <= br->words);
399
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);
402
403 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
404 *val = 0;
405 return true;
406 }
407
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))
410 return false;
411 }
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;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100417 const uint32_t word = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000418 if(bits < n) {
419 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
420 br->consumed_bits += bits;
421 return true;
422 }
423 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
424 bits -= n;
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 */
429 *val <<= bits;
430 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
431 br->consumed_bits = bits;
432 }
433 return true;
434 }
435 else {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100436 const uint32_t word = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000437 if(bits < FLAC__BITS_PER_WORD) {
438 *val = word >> (FLAC__BITS_PER_WORD-bits);
439 br->consumed_bits = bits;
440 return true;
441 }
442 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
443 *val = word;
444 crc16_update_word_(br, word);
445 br->consumed_words++;
446 return true;
447 }
448 }
449 else {
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.
453 */
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;
460 return true;
461 }
462 else {
463 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
464 br->consumed_bits += bits;
465 return true;
466 }
467 }
468}
469
470FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
471{
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))
474 return false;
475 /* sign-extend: */
476 *val <<= (32-bits);
477 *val >>= (32-bits);
478 return true;
479}
480
481FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
482{
483 FLAC__uint32 hi, lo;
484
485 if(bits > 32) {
486 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
487 return false;
488 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
489 return false;
490 *val = hi;
491 *val <<= 32;
492 *val |= lo;
493 }
494 else {
495 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
496 return false;
497 *val = lo;
498 }
499 return true;
500}
501
502FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
503{
504 FLAC__uint32 x8, x32 = 0;
505
506 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
507
508 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
509 return false;
510
511 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
512 return false;
513 x32 |= (x8 << 8);
514
515 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
516 return false;
517 x32 |= (x8 << 16);
518
519 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
520 return false;
521 x32 |= (x8 << 24);
522
523 *val = x32;
524 return true;
525}
526
527FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
528{
529 /*
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.
532 */
533 FLAC__ASSERT(0 != br);
534 FLAC__ASSERT(0 != br->buffer);
535
536 if(bits > 0) {
537 const unsigned n = br->consumed_bits & 7;
538 unsigned m;
539 FLAC__uint32 x;
540
541 if(n != 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -0300542 m = flac_min(8-n, bits);
Josh Coalson423f8042007-01-28 17:40:26 +0000543 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
544 return false;
545 bits -= m;
546 }
547 m = bits / 8;
548 if(m > 0) {
549 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
550 return false;
551 bits %= 8;
552 }
553 if(bits > 0) {
554 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
555 return false;
556 }
557 }
558
559 return true;
560}
561
562FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
563{
564 FLAC__uint32 x;
565
566 FLAC__ASSERT(0 != br);
567 FLAC__ASSERT(0 != br->buffer);
568 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
569
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))
573 return false;
574 nvals--;
575 }
576 if(0 == nvals)
577 return true;
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;
583 }
584 else if(!bitreader_read_from_client_(br))
585 return false;
586 }
587 /* step 3: skip any remainder from partial tail bytes */
588 while(nvals) {
589 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
590 return false;
591 nvals--;
592 }
593
594 return true;
595}
596
597FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
598{
599 FLAC__uint32 x;
600
601 FLAC__ASSERT(0 != br);
602 FLAC__ASSERT(0 != br->buffer);
603 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
604
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))
608 return false;
609 *val++ = (FLAC__byte)x;
610 nvals--;
611 }
612 if(0 == nvals)
613 return true;
614 /* step 2: read whole words in chunks */
615 while(nvals >= FLAC__BYTES_PER_WORD) {
616 if(br->consumed_words < br->words) {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100617 const uint32_t word = br->buffer[br->consumed_words++];
Josh Coalson423f8042007-01-28 17:40:26 +0000618#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;
632#else
633 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
634 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
635#endif
636 val += FLAC__BYTES_PER_WORD;
637 nvals -= FLAC__BYTES_PER_WORD;
638 }
639 else if(!bitreader_read_from_client_(br))
640 return false;
641 }
642 /* step 3: read any remainder from partial tail bytes */
643 while(nvals) {
644 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
645 return false;
646 *val++ = (FLAC__byte)x;
647 nvals--;
648 }
649
650 return true;
651}
652
Josh Coalson8e28e432009-01-03 02:10:18 +0000653FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000654#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000655{
656 unsigned bit;
657
658 FLAC__ASSERT(0 != br);
659 FLAC__ASSERT(0 != br->buffer);
660
661 *val = 0;
662 while(1) {
663 if(!FLAC__bitreader_read_bit(br, &bit))
664 return false;
665 if(bit)
666 break;
667 else
668 *val++;
669 }
670 return true;
671}
672#else
673{
674 unsigned i;
675
676 FLAC__ASSERT(0 != br);
677 FLAC__ASSERT(0 != br->buffer);
678
679 *val = 0;
680 while(1) {
681 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100682 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
Josh Coalson423f8042007-01-28 17:40:26 +0000683 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000684 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000685 *val += i;
686 i++;
687 br->consumed_bits += i;
Josh Coalson276d6162007-03-22 03:20:12 +0000688 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 +0000689 crc16_update_word_(br, br->buffer[br->consumed_words]);
690 br->consumed_words++;
691 br->consumed_bits = 0;
692 }
693 return true;
694 }
695 else {
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... */
701 }
702 }
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
708 * be zero.
709 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000710 if(br->bytes*8 > br->consumed_bits) {
Josh Coalson423f8042007-01-28 17:40:26 +0000711 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100712 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
Josh Coalson423f8042007-01-28 17:40:26 +0000713 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000714 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000715 *val += i;
716 i++;
717 br->consumed_bits += i;
718 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
719 return true;
720 }
721 else {
722 *val += end - br->consumed_bits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000723 br->consumed_bits = end;
Josh Coalson423f8042007-01-28 17:40:26 +0000724 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
725 /* didn't find stop bit yet, have to keep going... */
726 }
727 }
728 if(!bitreader_read_from_client_(br))
729 return false;
730 }
731}
732#endif
733
734FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
735{
736 FLAC__uint32 lsbs = 0, msbs = 0;
737 unsigned uval;
738
739 FLAC__ASSERT(0 != br);
740 FLAC__ASSERT(0 != br->buffer);
741 FLAC__ASSERT(parameter <= 31);
742
743 /* read the unary MSBs and end bit */
744 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
745 return false;
746
747 /* read the binary LSBs */
748 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
749 return false;
750
751 /* compose the value */
752 uval = (msbs << parameter) | lsbs;
753 if(uval & 1)
754 *val = -((int)(uval >> 1)) - 1;
755 else
756 *val = (int)(uval >> 1);
757
758 return true;
759}
760
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() */
763FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
Josh Coalsone289ae52007-07-23 16:14:35 +0000764/* OPT: possibly faster version for use with MSVC */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000765#ifdef _MSC_VER
766{
767 unsigned i;
768 unsigned uval = 0;
769 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
770
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;
776
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 */
783
784 if(nvals == 0)
785 return true;
786
787 cbits = br->consumed_bits;
788 cwords = br->consumed_words;
789
790 while(1) {
791
792 /* read unary part */
793 while(1) {
794 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100795 uint32_t b = br->buffer[cwords] << cbits;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000796 if(b) {
797#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
798 __asm {
799 bsr eax, b
800 not eax
801 and eax, 31
802 mov i, eax
803 }
804#else
805 i = COUNT_ZERO_MSBS(b);
806#endif
807 uval += i;
808 bits = parameter;
809 i++;
810 cbits += i;
811 if(cbits == FLAC__BITS_PER_WORD) {
812 crc16_update_word_(br, br->buffer[cwords]);
813 cwords++;
814 cbits = 0;
815 }
816 goto break1;
817 }
818 else {
819 uval += FLAC__BITS_PER_WORD - cbits;
820 crc16_update_word_(br, br->buffer[cwords]);
821 cwords++;
822 cbits = 0;
823 /* didn't find stop bit yet, have to keep going... */
824 }
825 }
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
831 * be zero.
832 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000833 if(br->bytes*8 > cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000834 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100835 uint32_t b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000836 if(b) {
837 i = COUNT_ZERO_MSBS(b);
838 uval += i;
839 bits = parameter;
840 i++;
841 cbits += i;
842 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
843 goto break1;
844 }
845 else {
846 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000847 cbits = end;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000848 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
849 /* didn't find stop bit yet, have to keep going... */
850 }
851 }
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.
855 */
856 br->consumed_bits = cbits;
857 br->consumed_words = cwords;
858 if(!bitreader_read_from_client_(br))
859 return false;
860 cwords = br->consumed_words;
861 }
862break1:
863 /* read binary part */
864 FLAC__ASSERT(cwords <= br->words);
865
866 if(bits) {
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.
871 */
872 br->consumed_bits = cbits;
873 br->consumed_words = cwords;
874 if(!bitreader_read_from_client_(br))
875 return false;
876 cwords = br->consumed_words;
877 }
878 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
879 if(cbits) {
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;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100882 const uint32_t word = br->buffer[cwords];
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000883 if(bits < n) {
884 uval <<= bits;
885 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
886 cbits += bits;
887 goto break2;
888 }
889 uval <<= n;
890 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
891 bits -= n;
892 crc16_update_word_(br, word);
893 cwords++;
894 cbits = 0;
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 */
896 uval <<= bits;
897 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
898 cbits = bits;
899 }
900 goto break2;
901 }
902 else {
903 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
904 uval <<= bits;
905 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
906 cbits = bits;
907 goto break2;
908 }
909 }
910 else {
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.
914 */
915 uval <<= bits;
916 if(cbits) {
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);
920 cbits += bits;
921 goto break2;
922 }
923 else {
924 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
925 cbits += bits;
926 goto break2;
927 }
928 }
929 }
930break2:
931 /* compose the value */
932 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
933
934 /* are we done? */
935 --nvals;
936 if(nvals == 0) {
937 br->consumed_bits = cbits;
938 br->consumed_words = cwords;
939 return true;
940 }
941
942 uval = 0;
943 ++vals;
944
945 }
946}
947#else
Josh Coalson423f8042007-01-28 17:40:26 +0000948{
949 unsigned i;
950 unsigned uval = 0;
Josh Coalson423f8042007-01-28 17:40:26 +0000951
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;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000957 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000958
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);
Josh Coalsonc63cf412007-03-17 05:21:36 +0000964 /* 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 +0000965
966 if(nvals == 0)
967 return true;
968
969 cbits = br->consumed_bits;
970 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000971 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000972
973 while(1) {
974
975 /* read unary part */
976 while(1) {
977 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100978 uint32_t b = br->buffer[cwords] << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000979 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000980#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
981 asm volatile (
982 "bsrl %1, %0;"
983 "notl %0;"
984 "andl $31, %0;"
985 : "=r"(i)
986 : "r"(b)
987 );
Josh Coalson423f8042007-01-28 17:40:26 +0000988#else
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000989 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000990#endif
991 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000992 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000993 cbits++; /* skip over stop bit */
Josh Coalson276d6162007-03-22 03:20:12 +0000994 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +0000995 crc16_update_word_(br, br->buffer[cwords]);
996 cwords++;
997 cbits = 0;
998 }
999 goto break1;
1000 }
1001 else {
1002 uval += FLAC__BITS_PER_WORD - cbits;
1003 crc16_update_word_(br, br->buffer[cwords]);
1004 cwords++;
1005 cbits = 0;
1006 /* didn't find stop bit yet, have to keep going... */
1007 }
1008 }
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
1014 * be zero.
1015 */
Josh Coalsonee51fc02009-01-06 17:14:31 +00001016 if(br->bytes*8 > cbits) {
Josh Coalson423f8042007-01-28 17:40:26 +00001017 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +11001018 uint32_t b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001019 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001020 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +00001021 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +00001022 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +00001023 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +00001024 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1025 goto break1;
1026 }
1027 else {
1028 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +00001029 cbits = end;
Josh Coalson423f8042007-01-28 17:40:26 +00001030 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1031 /* didn't find stop bit yet, have to keep going... */
1032 }
1033 }
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.
1037 */
1038 br->consumed_bits = cbits;
1039 br->consumed_words = cwords;
1040 if(!bitreader_read_from_client_(br))
1041 return false;
1042 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001043 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
1047 */
Josh Coalson423f8042007-01-28 17:40:26 +00001048 }
1049break1:
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001050 ucbits -= uval;
1051 ucbits--; /* account for stop bit */
1052
Josh Coalson423f8042007-01-28 17:40:26 +00001053 /* read binary part */
1054 FLAC__ASSERT(cwords <= br->words);
1055
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001056 if(parameter) {
1057 while(ucbits < parameter) {
Josh Coalson423f8042007-01-28 17:40:26 +00001058 /* 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.
1061 */
1062 br->consumed_bits = cbits;
1063 br->consumed_words = cwords;
1064 if(!bitreader_read_from_client_(br))
1065 return false;
1066 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001067 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001068 }
1069 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1070 if(cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001071 /* this also works when consumed_bits==0, it's just slower than necessary for that case */
Josh Coalson423f8042007-01-28 17:40:26 +00001072 const unsigned n = FLAC__BITS_PER_WORD - cbits;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +11001073 const uint32_t word = br->buffer[cwords];
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001074 if(parameter < n) {
1075 uval <<= parameter;
1076 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1077 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001078 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001079 else {
1080 uval <<= n;
1081 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1082 crc16_update_word_(br, word);
1083 cwords++;
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 */
1086 uval <<= cbits;
1087 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1088 }
Josh Coalson423f8042007-01-28 17:40:26 +00001089 }
Josh Coalson423f8042007-01-28 17:40:26 +00001090 }
1091 else {
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001092 cbits = parameter;
Josh Coalson276d6162007-03-22 03:20:12 +00001093 uval <<= parameter;
1094 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001095 }
1096 }
1097 else {
1098 /* in this case we're starting our read at a partial tail word;
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001099 * the reader has guaranteed that we have at least 'parameter'
1100 * bits available to read, which makes this case simpler.
Josh Coalson423f8042007-01-28 17:40:26 +00001101 */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001102 uval <<= parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001103 if(cbits) {
1104 /* 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 +00001105 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1106 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1107 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001108 }
1109 else {
Josh Coalson276d6162007-03-22 03:20:12 +00001110 cbits = parameter;
1111 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001112 }
1113 }
1114 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001115
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001116 ucbits -= parameter;
1117
Josh Coalson423f8042007-01-28 17:40:26 +00001118 /* compose the value */
1119 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1120
1121 /* are we done? */
1122 --nvals;
1123 if(nvals == 0) {
1124 br->consumed_bits = cbits;
1125 br->consumed_words = cwords;
1126 return true;
1127 }
1128
1129 uval = 0;
1130 ++vals;
1131
1132 }
1133}
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001134#endif
Josh Coalson423f8042007-01-28 17:40:26 +00001135
1136#if 0 /* UNUSED */
1137FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1138{
1139 FLAC__uint32 lsbs = 0, msbs = 0;
1140 unsigned bit, uval, k;
1141
1142 FLAC__ASSERT(0 != br);
1143 FLAC__ASSERT(0 != br->buffer);
1144
1145 k = FLAC__bitmath_ilog2(parameter);
1146
1147 /* read the unary MSBs and end bit */
1148 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1149 return false;
1150
1151 /* read the binary LSBs */
1152 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1153 return false;
1154
1155 if(parameter == 1u<<k) {
1156 /* compose the value */
1157 uval = (msbs << k) | lsbs;
1158 }
1159 else {
1160 unsigned d = (1 << (k+1)) - parameter;
1161 if(lsbs >= d) {
1162 if(!FLAC__bitreader_read_bit(br, &bit))
1163 return false;
1164 lsbs <<= 1;
1165 lsbs |= bit;
1166 lsbs -= d;
1167 }
1168 /* compose the value */
1169 uval = msbs * parameter + lsbs;
1170 }
1171
1172 /* unfold unsigned to signed */
1173 if(uval & 1)
1174 *val = -((int)(uval >> 1)) - 1;
1175 else
1176 *val = (int)(uval >> 1);
1177
1178 return true;
1179}
1180
1181FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1182{
1183 FLAC__uint32 lsbs, msbs = 0;
1184 unsigned bit, k;
1185
1186 FLAC__ASSERT(0 != br);
1187 FLAC__ASSERT(0 != br->buffer);
1188
1189 k = FLAC__bitmath_ilog2(parameter);
1190
1191 /* read the unary MSBs and end bit */
1192 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1193 return false;
1194
1195 /* read the binary LSBs */
1196 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1197 return false;
1198
1199 if(parameter == 1u<<k) {
1200 /* compose the value */
1201 *val = (msbs << k) | lsbs;
1202 }
1203 else {
1204 unsigned d = (1 << (k+1)) - parameter;
1205 if(lsbs >= d) {
1206 if(!FLAC__bitreader_read_bit(br, &bit))
1207 return false;
1208 lsbs <<= 1;
1209 lsbs |= bit;
1210 lsbs -= d;
1211 }
1212 /* compose the value */
1213 *val = msbs * parameter + lsbs;
1214 }
1215
1216 return true;
1217}
1218#endif /* UNUSED */
1219
1220/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1221FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1222{
1223 FLAC__uint32 v = 0;
1224 FLAC__uint32 x;
1225 unsigned i;
1226
1227 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1228 return false;
1229 if(raw)
1230 raw[(*rawlen)++] = (FLAC__byte)x;
1231 if(!(x & 0x80)) { /* 0xxxxxxx */
1232 v = x;
1233 i = 0;
1234 }
1235 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1236 v = x & 0x1F;
1237 i = 1;
1238 }
1239 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1240 v = x & 0x0F;
1241 i = 2;
1242 }
1243 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1244 v = x & 0x07;
1245 i = 3;
1246 }
1247 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1248 v = x & 0x03;
1249 i = 4;
1250 }
1251 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1252 v = x & 0x01;
1253 i = 5;
1254 }
1255 else {
1256 *val = 0xffffffff;
1257 return true;
1258 }
1259 for( ; i; i--) {
1260 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1261 return false;
1262 if(raw)
1263 raw[(*rawlen)++] = (FLAC__byte)x;
1264 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1265 *val = 0xffffffff;
1266 return true;
1267 }
1268 v <<= 6;
1269 v |= (x & 0x3F);
1270 }
1271 *val = v;
1272 return true;
1273}
1274
1275/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1276FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1277{
1278 FLAC__uint64 v = 0;
1279 FLAC__uint32 x;
1280 unsigned i;
1281
1282 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1283 return false;
1284 if(raw)
1285 raw[(*rawlen)++] = (FLAC__byte)x;
1286 if(!(x & 0x80)) { /* 0xxxxxxx */
1287 v = x;
1288 i = 0;
1289 }
1290 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1291 v = x & 0x1F;
1292 i = 1;
1293 }
1294 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1295 v = x & 0x0F;
1296 i = 2;
1297 }
1298 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1299 v = x & 0x07;
1300 i = 3;
1301 }
1302 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1303 v = x & 0x03;
1304 i = 4;
1305 }
1306 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1307 v = x & 0x01;
1308 i = 5;
1309 }
1310 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1311 v = 0;
1312 i = 6;
1313 }
1314 else {
1315 *val = FLAC__U64L(0xffffffffffffffff);
1316 return true;
1317 }
1318 for( ; i; i--) {
1319 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1320 return false;
1321 if(raw)
1322 raw[(*rawlen)++] = (FLAC__byte)x;
1323 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1324 *val = FLAC__U64L(0xffffffffffffffff);
1325 return true;
1326 }
1327 v <<= 6;
1328 v |= (x & 0x3F);
1329 }
1330 *val = v;
1331 return true;
1332}