blob: ae515a070810b6ffa001dde60e9c5875b9a4ef83 [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
Josh Coalsonc63cf412007-03-17 05:21:36 +0000120/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
Josh Coalson423f8042007-01-28 17:40:26 +0000121struct FLAC__BitReader {
122 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
123 /* 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 +1100124 uint32_t *buffer;
Josh Coalson423f8042007-01-28 17:40:26 +0000125 unsigned capacity; /* in words */
126 unsigned words; /* # of completed words in buffer */
127 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
Josh Coalsonc63cf412007-03-17 05:21:36 +0000128 unsigned consumed_words; /* #words ... */
129 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000130 unsigned read_crc16; /* the running frame CRC */
131 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
132 FLAC__BitReaderReadCallback read_callback;
133 void *client_data;
Josh Coalson65454092007-03-13 16:14:36 +0000134 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000135};
136
Cristian Rodríguez9b7cb222012-04-07 19:24:21 -0300137static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
Josh Coalson423f8042007-01-28 17:40:26 +0000138{
139 register unsigned crc = br->read_crc16;
140#if FLAC__BYTES_PER_WORD == 4
141 switch(br->crc16_align) {
142 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
143 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
144 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
145 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
146 }
147#elif FLAC__BYTES_PER_WORD == 8
148 switch(br->crc16_align) {
149 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
150 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
151 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
152 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
153 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
154 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
155 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
156 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
157 }
158#else
159 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
160 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
161 br->read_crc16 = crc;
162#endif
163 br->crc16_align = 0;
164}
165
Josh Coalsonc63cf412007-03-17 05:21:36 +0000166/* would be static except it needs to be called by asm routines */
167FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000168{
169 unsigned start, end;
170 size_t bytes;
171 FLAC__byte *target;
172
173 /* first shift the unconsumed buffer data toward the front as much as possible */
174 if(br->consumed_words > 0) {
175 start = br->consumed_words;
176 end = br->words + (br->bytes? 1:0);
177 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
178
179 br->words -= start;
180 br->consumed_words = 0;
181 }
182
183 /*
184 * set the target for reading, taking into account word alignment and endianness
185 */
186 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
187 if(bytes == 0)
188 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
189 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
190
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100191 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
Josh Coalson423f8042007-01-28 17:40:26 +0000192 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
193 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
194 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
195 * ^^-------target, bytes=3
196 * on LE machines, have to byteswap the odd tail word so nothing is
197 * overwritten:
198 */
199#if WORDS_BIGENDIAN
200#else
201 if(br->bytes)
202 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
203#endif
204
205 /* now it looks like:
206 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
207 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
208 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
209 * ^^-------target, bytes=3
210 */
211
212 /* read in the data; note that the callback may return a smaller number of bytes */
213 if(!br->read_callback(target, &bytes, br->client_data))
214 return false;
215
216 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
217 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
218 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
219 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
220 * now have to byteswap on LE machines:
221 */
222#if WORDS_BIGENDIAN
223#else
224 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
225 for(start = br->words; start < end; start++)
226 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
227#endif
228
229 /* now it looks like:
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 88 77 66 55 CC BB AA 99 ?? FF EE DD
233 * finally we'll update the reader values:
234 */
235 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
236 br->words = end / FLAC__BYTES_PER_WORD;
237 br->bytes = end % FLAC__BYTES_PER_WORD;
238
239 return true;
240}
241
242/***********************************************************************
243 *
244 * Class constructor/destructor
245 *
246 ***********************************************************************/
247
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000248FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000249{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000250 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
Josh Coalson423f8042007-01-28 17:40:26 +0000251
252 /* calloc() implies:
253 memset(br, 0, sizeof(FLAC__BitReader));
254 br->buffer = 0;
255 br->capacity = 0;
256 br->words = br->bytes = 0;
257 br->consumed_words = br->consumed_bits = 0;
258 br->read_callback = 0;
259 br->client_data = 0;
260 */
261 return br;
262}
263
264void FLAC__bitreader_delete(FLAC__BitReader *br)
265{
266 FLAC__ASSERT(0 != br);
267
268 FLAC__bitreader_free(br);
269 free(br);
270}
271
272/***********************************************************************
273 *
274 * Public class methods
275 *
276 ***********************************************************************/
277
Josh Coalson65454092007-03-13 16:14:36 +0000278FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000279{
280 FLAC__ASSERT(0 != br);
281
282 br->words = br->bytes = 0;
283 br->consumed_words = br->consumed_bits = 0;
284 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000285 br->buffer = malloc(sizeof(uint32_t) * br->capacity);
Josh Coalson423f8042007-01-28 17:40:26 +0000286 if(br->buffer == 0)
287 return false;
288 br->read_callback = rcb;
289 br->client_data = cd;
Josh Coalson65454092007-03-13 16:14:36 +0000290 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000291
292 return true;
293}
294
295void FLAC__bitreader_free(FLAC__BitReader *br)
296{
297 FLAC__ASSERT(0 != br);
298
299 if(0 != br->buffer)
300 free(br->buffer);
301 br->buffer = 0;
302 br->capacity = 0;
303 br->words = br->bytes = 0;
304 br->consumed_words = br->consumed_bits = 0;
305 br->read_callback = 0;
306 br->client_data = 0;
307}
308
309FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
310{
311 br->words = br->bytes = 0;
312 br->consumed_words = br->consumed_bits = 0;
313 return true;
314}
315
316void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
317{
318 unsigned i, j;
319 if(br == 0) {
320 fprintf(out, "bitreader is NULL\n");
321 }
322 else {
323 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);
324
325 for(i = 0; i < br->words; i++) {
326 fprintf(out, "%08X: ", i);
327 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
328 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
329 fprintf(out, ".");
330 else
331 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
332 fprintf(out, "\n");
333 }
334 if(br->bytes > 0) {
335 fprintf(out, "%08X: ", i);
336 for(j = 0; j < br->bytes*8; j++)
337 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
338 fprintf(out, ".");
339 else
340 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
341 fprintf(out, "\n");
342 }
343 }
344}
345
346void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
347{
348 FLAC__ASSERT(0 != br);
349 FLAC__ASSERT(0 != br->buffer);
350 FLAC__ASSERT((br->consumed_bits & 7) == 0);
351
352 br->read_crc16 = (unsigned)seed;
353 br->crc16_align = br->consumed_bits;
354}
355
356FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
357{
358 FLAC__ASSERT(0 != br);
359 FLAC__ASSERT(0 != br->buffer);
360 FLAC__ASSERT((br->consumed_bits & 7) == 0);
361 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
362
363 /* CRC any tail bytes in a partially-consumed word */
364 if(br->consumed_bits) {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100365 const uint32_t tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000366 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
367 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
368 }
369 return br->read_crc16;
370}
371
Cristian Rodríguez9b7cb222012-04-07 19:24:21 -0300372inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000373{
374 return ((br->consumed_bits & 7) == 0);
375}
376
Cristian Rodríguez9b7cb222012-04-07 19:24:21 -0300377inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000378{
379 return 8 - (br->consumed_bits & 7);
380}
381
Cristian Rodríguez9b7cb222012-04-07 19:24:21 -0300382inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000383{
384 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
385}
386
Erik de Castro Lopo6b3b1372012-02-01 19:49:54 +1100387FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
Josh Coalson423f8042007-01-28 17:40:26 +0000388{
389 FLAC__ASSERT(0 != br);
390 FLAC__ASSERT(0 != br->buffer);
391
392 FLAC__ASSERT(bits <= 32);
393 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
394 FLAC__ASSERT(br->consumed_words <= br->words);
395
396 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
397 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
398
399 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
400 *val = 0;
401 return true;
402 }
403
404 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
405 if(!bitreader_read_from_client_(br))
406 return false;
407 }
408 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
409 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
410 if(br->consumed_bits) {
411 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
412 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100413 const uint32_t word = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000414 if(bits < n) {
415 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
416 br->consumed_bits += bits;
417 return true;
418 }
419 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
420 bits -= n;
421 crc16_update_word_(br, word);
422 br->consumed_words++;
423 br->consumed_bits = 0;
424 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 */
425 *val <<= bits;
426 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
427 br->consumed_bits = bits;
428 }
429 return true;
430 }
431 else {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100432 const uint32_t word = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000433 if(bits < FLAC__BITS_PER_WORD) {
434 *val = word >> (FLAC__BITS_PER_WORD-bits);
435 br->consumed_bits = bits;
436 return true;
437 }
438 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
439 *val = word;
440 crc16_update_word_(br, word);
441 br->consumed_words++;
442 return true;
443 }
444 }
445 else {
446 /* in this case we're starting our read at a partial tail word;
447 * the reader has guaranteed that we have at least 'bits' bits
448 * available to read, which makes this case simpler.
449 */
450 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
451 if(br->consumed_bits) {
452 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
453 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
454 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
455 br->consumed_bits += bits;
456 return true;
457 }
458 else {
459 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
460 br->consumed_bits += bits;
461 return true;
462 }
463 }
464}
465
466FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
467{
468 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
469 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
470 return false;
471 /* sign-extend: */
472 *val <<= (32-bits);
473 *val >>= (32-bits);
474 return true;
475}
476
477FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
478{
479 FLAC__uint32 hi, lo;
480
481 if(bits > 32) {
482 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
483 return false;
484 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
485 return false;
486 *val = hi;
487 *val <<= 32;
488 *val |= lo;
489 }
490 else {
491 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
492 return false;
493 *val = lo;
494 }
495 return true;
496}
497
Cristian Rodríguez9b7cb222012-04-07 19:24:21 -0300498inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
Josh Coalson423f8042007-01-28 17:40:26 +0000499{
500 FLAC__uint32 x8, x32 = 0;
501
502 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
503
504 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
505 return false;
506
507 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
508 return false;
509 x32 |= (x8 << 8);
510
511 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
512 return false;
513 x32 |= (x8 << 16);
514
515 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
516 return false;
517 x32 |= (x8 << 24);
518
519 *val = x32;
520 return true;
521}
522
523FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
524{
525 /*
526 * OPT: a faster implementation is possible but probably not that useful
527 * since this is only called a couple of times in the metadata readers.
528 */
529 FLAC__ASSERT(0 != br);
530 FLAC__ASSERT(0 != br->buffer);
531
532 if(bits > 0) {
533 const unsigned n = br->consumed_bits & 7;
534 unsigned m;
535 FLAC__uint32 x;
536
537 if(n != 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -0300538 m = flac_min(8-n, bits);
Josh Coalson423f8042007-01-28 17:40:26 +0000539 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
540 return false;
541 bits -= m;
542 }
543 m = bits / 8;
544 if(m > 0) {
545 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
546 return false;
547 bits %= 8;
548 }
549 if(bits > 0) {
550 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
551 return false;
552 }
553 }
554
555 return true;
556}
557
558FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
559{
560 FLAC__uint32 x;
561
562 FLAC__ASSERT(0 != br);
563 FLAC__ASSERT(0 != br->buffer);
564 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
565
566 /* step 1: skip over partial head word to get word aligned */
567 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
568 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
569 return false;
570 nvals--;
571 }
572 if(0 == nvals)
573 return true;
574 /* step 2: skip whole words in chunks */
575 while(nvals >= FLAC__BYTES_PER_WORD) {
576 if(br->consumed_words < br->words) {
577 br->consumed_words++;
578 nvals -= FLAC__BYTES_PER_WORD;
579 }
580 else if(!bitreader_read_from_client_(br))
581 return false;
582 }
583 /* step 3: skip any remainder from partial tail bytes */
584 while(nvals) {
585 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
586 return false;
587 nvals--;
588 }
589
590 return true;
591}
592
593FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
594{
595 FLAC__uint32 x;
596
597 FLAC__ASSERT(0 != br);
598 FLAC__ASSERT(0 != br->buffer);
599 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
600
601 /* step 1: read from partial head word to get word aligned */
602 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
603 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
604 return false;
605 *val++ = (FLAC__byte)x;
606 nvals--;
607 }
608 if(0 == nvals)
609 return true;
610 /* step 2: read whole words in chunks */
611 while(nvals >= FLAC__BYTES_PER_WORD) {
612 if(br->consumed_words < br->words) {
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100613 const uint32_t word = br->buffer[br->consumed_words++];
Josh Coalson423f8042007-01-28 17:40:26 +0000614#if FLAC__BYTES_PER_WORD == 4
615 val[0] = (FLAC__byte)(word >> 24);
616 val[1] = (FLAC__byte)(word >> 16);
617 val[2] = (FLAC__byte)(word >> 8);
618 val[3] = (FLAC__byte)word;
619#elif FLAC__BYTES_PER_WORD == 8
620 val[0] = (FLAC__byte)(word >> 56);
621 val[1] = (FLAC__byte)(word >> 48);
622 val[2] = (FLAC__byte)(word >> 40);
623 val[3] = (FLAC__byte)(word >> 32);
624 val[4] = (FLAC__byte)(word >> 24);
625 val[5] = (FLAC__byte)(word >> 16);
626 val[6] = (FLAC__byte)(word >> 8);
627 val[7] = (FLAC__byte)word;
628#else
629 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
630 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
631#endif
632 val += FLAC__BYTES_PER_WORD;
633 nvals -= FLAC__BYTES_PER_WORD;
634 }
635 else if(!bitreader_read_from_client_(br))
636 return false;
637 }
638 /* step 3: read any remainder from partial tail bytes */
639 while(nvals) {
640 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
641 return false;
642 *val++ = (FLAC__byte)x;
643 nvals--;
644 }
645
646 return true;
647}
648
Josh Coalson8e28e432009-01-03 02:10:18 +0000649FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000650#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000651{
652 unsigned bit;
653
654 FLAC__ASSERT(0 != br);
655 FLAC__ASSERT(0 != br->buffer);
656
657 *val = 0;
658 while(1) {
659 if(!FLAC__bitreader_read_bit(br, &bit))
660 return false;
661 if(bit)
662 break;
663 else
664 *val++;
665 }
666 return true;
667}
668#else
669{
670 unsigned i;
671
672 FLAC__ASSERT(0 != br);
673 FLAC__ASSERT(0 != br->buffer);
674
675 *val = 0;
676 while(1) {
677 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 +1100678 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
Josh Coalson423f8042007-01-28 17:40:26 +0000679 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000680 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000681 *val += i;
682 i++;
683 br->consumed_bits += i;
Josh Coalson276d6162007-03-22 03:20:12 +0000684 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 +0000685 crc16_update_word_(br, br->buffer[br->consumed_words]);
686 br->consumed_words++;
687 br->consumed_bits = 0;
688 }
689 return true;
690 }
691 else {
692 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
693 crc16_update_word_(br, br->buffer[br->consumed_words]);
694 br->consumed_words++;
695 br->consumed_bits = 0;
696 /* didn't find stop bit yet, have to keep going... */
697 }
698 }
699 /* at this point we've eaten up all the whole words; have to try
700 * reading through any tail bytes before calling the read callback.
701 * this is a repeat of the above logic adjusted for the fact we
702 * don't have a whole word. note though if the client is feeding
703 * us data a byte at a time (unlikely), br->consumed_bits may not
704 * be zero.
705 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000706 if(br->bytes*8 > br->consumed_bits) {
Josh Coalson423f8042007-01-28 17:40:26 +0000707 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100708 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 +0000709 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000710 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000711 *val += i;
712 i++;
713 br->consumed_bits += i;
714 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
715 return true;
716 }
717 else {
718 *val += end - br->consumed_bits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000719 br->consumed_bits = end;
Josh Coalson423f8042007-01-28 17:40:26 +0000720 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
721 /* didn't find stop bit yet, have to keep going... */
722 }
723 }
724 if(!bitreader_read_from_client_(br))
725 return false;
726 }
727}
728#endif
729
730FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
731{
732 FLAC__uint32 lsbs = 0, msbs = 0;
733 unsigned uval;
734
735 FLAC__ASSERT(0 != br);
736 FLAC__ASSERT(0 != br->buffer);
737 FLAC__ASSERT(parameter <= 31);
738
739 /* read the unary MSBs and end bit */
740 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
741 return false;
742
743 /* read the binary LSBs */
744 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
745 return false;
746
747 /* compose the value */
748 uval = (msbs << parameter) | lsbs;
749 if(uval & 1)
750 *val = -((int)(uval >> 1)) - 1;
751 else
752 *val = (int)(uval >> 1);
753
754 return true;
755}
756
757/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
758/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
759FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
Josh Coalsone289ae52007-07-23 16:14:35 +0000760/* OPT: possibly faster version for use with MSVC */
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000761#ifdef _MSC_VER
762{
763 unsigned i;
764 unsigned uval = 0;
765 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
766
767 /* try and get br->consumed_words and br->consumed_bits into register;
768 * must remember to flush them back to *br before calling other
769 * bitwriter functions that use them, and before returning */
770 register unsigned cwords;
771 register unsigned cbits;
772
773 FLAC__ASSERT(0 != br);
774 FLAC__ASSERT(0 != br->buffer);
775 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
776 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
777 FLAC__ASSERT(parameter < 32);
778 /* 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 */
779
780 if(nvals == 0)
781 return true;
782
783 cbits = br->consumed_bits;
784 cwords = br->consumed_words;
785
786 while(1) {
787
788 /* read unary part */
789 while(1) {
790 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100791 uint32_t b = br->buffer[cwords] << cbits;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000792 if(b) {
793#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
794 __asm {
795 bsr eax, b
796 not eax
797 and eax, 31
798 mov i, eax
799 }
800#else
801 i = COUNT_ZERO_MSBS(b);
802#endif
803 uval += i;
804 bits = parameter;
805 i++;
806 cbits += i;
807 if(cbits == FLAC__BITS_PER_WORD) {
808 crc16_update_word_(br, br->buffer[cwords]);
809 cwords++;
810 cbits = 0;
811 }
812 goto break1;
813 }
814 else {
815 uval += FLAC__BITS_PER_WORD - cbits;
816 crc16_update_word_(br, br->buffer[cwords]);
817 cwords++;
818 cbits = 0;
819 /* didn't find stop bit yet, have to keep going... */
820 }
821 }
822 /* at this point we've eaten up all the whole words; have to try
823 * reading through any tail bytes before calling the read callback.
824 * this is a repeat of the above logic adjusted for the fact we
825 * don't have a whole word. note though if the client is feeding
826 * us data a byte at a time (unlikely), br->consumed_bits may not
827 * be zero.
828 */
Josh Coalsonee51fc02009-01-06 17:14:31 +0000829 if(br->bytes*8 > cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000830 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100831 uint32_t b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000832 if(b) {
833 i = COUNT_ZERO_MSBS(b);
834 uval += i;
835 bits = parameter;
836 i++;
837 cbits += i;
838 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
839 goto break1;
840 }
841 else {
842 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +0000843 cbits = end;
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000844 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
845 /* didn't find stop bit yet, have to keep going... */
846 }
847 }
848 /* flush registers and read; bitreader_read_from_client_() does
849 * not touch br->consumed_bits at all but we still need to set
850 * it in case it fails and we have to return false.
851 */
852 br->consumed_bits = cbits;
853 br->consumed_words = cwords;
854 if(!bitreader_read_from_client_(br))
855 return false;
856 cwords = br->consumed_words;
857 }
858break1:
859 /* read binary part */
860 FLAC__ASSERT(cwords <= br->words);
861
862 if(bits) {
863 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
864 /* flush registers and read; bitreader_read_from_client_() does
865 * not touch br->consumed_bits at all but we still need to set
866 * it in case it fails and we have to return false.
867 */
868 br->consumed_bits = cbits;
869 br->consumed_words = cwords;
870 if(!bitreader_read_from_client_(br))
871 return false;
872 cwords = br->consumed_words;
873 }
874 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
875 if(cbits) {
876 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
877 const unsigned n = FLAC__BITS_PER_WORD - cbits;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100878 const uint32_t word = br->buffer[cwords];
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000879 if(bits < n) {
880 uval <<= bits;
881 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
882 cbits += bits;
883 goto break2;
884 }
885 uval <<= n;
886 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
887 bits -= n;
888 crc16_update_word_(br, word);
889 cwords++;
890 cbits = 0;
891 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 */
892 uval <<= bits;
893 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
894 cbits = bits;
895 }
896 goto break2;
897 }
898 else {
899 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
900 uval <<= bits;
901 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
902 cbits = bits;
903 goto break2;
904 }
905 }
906 else {
907 /* in this case we're starting our read at a partial tail word;
908 * the reader has guaranteed that we have at least 'bits' bits
909 * available to read, which makes this case simpler.
910 */
911 uval <<= bits;
912 if(cbits) {
913 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
914 FLAC__ASSERT(cbits + bits <= br->bytes*8);
915 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
916 cbits += bits;
917 goto break2;
918 }
919 else {
920 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
921 cbits += bits;
922 goto break2;
923 }
924 }
925 }
926break2:
927 /* compose the value */
928 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
929
930 /* are we done? */
931 --nvals;
932 if(nvals == 0) {
933 br->consumed_bits = cbits;
934 br->consumed_words = cwords;
935 return true;
936 }
937
938 uval = 0;
939 ++vals;
940
941 }
942}
943#else
Josh Coalson423f8042007-01-28 17:40:26 +0000944{
945 unsigned i;
946 unsigned uval = 0;
Josh Coalson423f8042007-01-28 17:40:26 +0000947
948 /* try and get br->consumed_words and br->consumed_bits into register;
949 * must remember to flush them back to *br before calling other
950 * bitwriter functions that use them, and before returning */
951 register unsigned cwords;
952 register unsigned cbits;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000953 unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000954
955 FLAC__ASSERT(0 != br);
956 FLAC__ASSERT(0 != br->buffer);
957 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
958 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
959 FLAC__ASSERT(parameter < 32);
Josh Coalsonc63cf412007-03-17 05:21:36 +0000960 /* 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 +0000961
962 if(nvals == 0)
963 return true;
964
965 cbits = br->consumed_bits;
966 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +0000967 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000968
969 while(1) {
970
971 /* read unary part */
972 while(1) {
973 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +1100974 uint32_t b = br->buffer[cwords] << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000975 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000976#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
977 asm volatile (
978 "bsrl %1, %0;"
979 "notl %0;"
980 "andl $31, %0;"
981 : "=r"(i)
982 : "r"(b)
983 );
Josh Coalson423f8042007-01-28 17:40:26 +0000984#else
Josh Coalson9d8fa1e2007-03-23 04:50:54 +0000985 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +0000986#endif
987 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000988 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000989 cbits++; /* skip over stop bit */
Josh Coalson276d6162007-03-22 03:20:12 +0000990 if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
Josh Coalson423f8042007-01-28 17:40:26 +0000991 crc16_update_word_(br, br->buffer[cwords]);
992 cwords++;
993 cbits = 0;
994 }
995 goto break1;
996 }
997 else {
998 uval += FLAC__BITS_PER_WORD - cbits;
999 crc16_update_word_(br, br->buffer[cwords]);
1000 cwords++;
1001 cbits = 0;
1002 /* didn't find stop bit yet, have to keep going... */
1003 }
1004 }
1005 /* at this point we've eaten up all the whole words; have to try
1006 * reading through any tail bytes before calling the read callback.
1007 * this is a repeat of the above logic adjusted for the fact we
1008 * don't have a whole word. note though if the client is feeding
1009 * us data a byte at a time (unlikely), br->consumed_bits may not
1010 * be zero.
1011 */
Josh Coalsonee51fc02009-01-06 17:14:31 +00001012 if(br->bytes*8 > cbits) {
Josh Coalson423f8042007-01-28 17:40:26 +00001013 const unsigned end = br->bytes * 8;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +11001014 uint32_t b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001015 if(b) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001016 i = COUNT_ZERO_MSBS(b);
Josh Coalson423f8042007-01-28 17:40:26 +00001017 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +00001018 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +00001019 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +00001020 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1021 goto break1;
1022 }
1023 else {
1024 uval += end - cbits;
Josh Coalsonee51fc02009-01-06 17:14:31 +00001025 cbits = end;
Josh Coalson423f8042007-01-28 17:40:26 +00001026 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1027 /* didn't find stop bit yet, have to keep going... */
1028 }
1029 }
1030 /* flush registers and read; bitreader_read_from_client_() does
1031 * not touch br->consumed_bits at all but we still need to set
1032 * it in case it fails and we have to return false.
1033 */
1034 br->consumed_bits = cbits;
1035 br->consumed_words = cwords;
1036 if(!bitreader_read_from_client_(br))
1037 return false;
1038 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001039 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
1040 /* + uval to offset our count by the # of unary bits already
1041 * consumed before the read, because we will add these back
1042 * in all at once at break1
1043 */
Josh Coalson423f8042007-01-28 17:40:26 +00001044 }
1045break1:
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001046 ucbits -= uval;
1047 ucbits--; /* account for stop bit */
1048
Josh Coalson423f8042007-01-28 17:40:26 +00001049 /* read binary part */
1050 FLAC__ASSERT(cwords <= br->words);
1051
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001052 if(parameter) {
1053 while(ucbits < parameter) {
Josh Coalson423f8042007-01-28 17:40:26 +00001054 /* flush registers and read; bitreader_read_from_client_() does
1055 * not touch br->consumed_bits at all but we still need to set
1056 * it in case it fails and we have to return false.
1057 */
1058 br->consumed_bits = cbits;
1059 br->consumed_words = cwords;
1060 if(!bitreader_read_from_client_(br))
1061 return false;
1062 cwords = br->consumed_words;
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001063 ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
Josh Coalson423f8042007-01-28 17:40:26 +00001064 }
1065 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1066 if(cbits) {
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001067 /* this also works when consumed_bits==0, it's just slower than necessary for that case */
Josh Coalson423f8042007-01-28 17:40:26 +00001068 const unsigned n = FLAC__BITS_PER_WORD - cbits;
Erik de Castro Lopo66bd44b2012-03-30 22:25:39 +11001069 const uint32_t word = br->buffer[cwords];
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001070 if(parameter < n) {
1071 uval <<= parameter;
1072 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1073 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001074 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001075 else {
1076 uval <<= n;
1077 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1078 crc16_update_word_(br, word);
1079 cwords++;
1080 cbits = parameter - n;
1081 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 */
1082 uval <<= cbits;
1083 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1084 }
Josh Coalson423f8042007-01-28 17:40:26 +00001085 }
Josh Coalson423f8042007-01-28 17:40:26 +00001086 }
1087 else {
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001088 cbits = parameter;
Josh Coalson276d6162007-03-22 03:20:12 +00001089 uval <<= parameter;
1090 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001091 }
1092 }
1093 else {
1094 /* in this case we're starting our read at a partial tail word;
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001095 * the reader has guaranteed that we have at least 'parameter'
1096 * bits available to read, which makes this case simpler.
Josh Coalson423f8042007-01-28 17:40:26 +00001097 */
Josh Coalsonf1dfaeb2007-03-22 03:19:52 +00001098 uval <<= parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001099 if(cbits) {
1100 /* 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 +00001101 FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1102 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1103 cbits += parameter;
Josh Coalson423f8042007-01-28 17:40:26 +00001104 }
1105 else {
Josh Coalson276d6162007-03-22 03:20:12 +00001106 cbits = parameter;
1107 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
Josh Coalson423f8042007-01-28 17:40:26 +00001108 }
1109 }
1110 }
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001111
Josh Coalsonc9212fa2007-03-22 03:19:19 +00001112 ucbits -= parameter;
1113
Josh Coalson423f8042007-01-28 17:40:26 +00001114 /* compose the value */
1115 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1116
1117 /* are we done? */
1118 --nvals;
1119 if(nvals == 0) {
1120 br->consumed_bits = cbits;
1121 br->consumed_words = cwords;
1122 return true;
1123 }
1124
1125 uval = 0;
1126 ++vals;
1127
1128 }
1129}
Josh Coalson9d8fa1e2007-03-23 04:50:54 +00001130#endif
Josh Coalson423f8042007-01-28 17:40:26 +00001131
1132#if 0 /* UNUSED */
1133FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1134{
1135 FLAC__uint32 lsbs = 0, msbs = 0;
1136 unsigned bit, uval, k;
1137
1138 FLAC__ASSERT(0 != br);
1139 FLAC__ASSERT(0 != br->buffer);
1140
1141 k = FLAC__bitmath_ilog2(parameter);
1142
1143 /* read the unary MSBs and end bit */
1144 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1145 return false;
1146
1147 /* read the binary LSBs */
1148 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1149 return false;
1150
1151 if(parameter == 1u<<k) {
1152 /* compose the value */
1153 uval = (msbs << k) | lsbs;
1154 }
1155 else {
1156 unsigned d = (1 << (k+1)) - parameter;
1157 if(lsbs >= d) {
1158 if(!FLAC__bitreader_read_bit(br, &bit))
1159 return false;
1160 lsbs <<= 1;
1161 lsbs |= bit;
1162 lsbs -= d;
1163 }
1164 /* compose the value */
1165 uval = msbs * parameter + lsbs;
1166 }
1167
1168 /* unfold unsigned to signed */
1169 if(uval & 1)
1170 *val = -((int)(uval >> 1)) - 1;
1171 else
1172 *val = (int)(uval >> 1);
1173
1174 return true;
1175}
1176
1177FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1178{
1179 FLAC__uint32 lsbs, msbs = 0;
1180 unsigned bit, k;
1181
1182 FLAC__ASSERT(0 != br);
1183 FLAC__ASSERT(0 != br->buffer);
1184
1185 k = FLAC__bitmath_ilog2(parameter);
1186
1187 /* read the unary MSBs and end bit */
1188 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1189 return false;
1190
1191 /* read the binary LSBs */
1192 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1193 return false;
1194
1195 if(parameter == 1u<<k) {
1196 /* compose the value */
1197 *val = (msbs << k) | lsbs;
1198 }
1199 else {
1200 unsigned d = (1 << (k+1)) - parameter;
1201 if(lsbs >= d) {
1202 if(!FLAC__bitreader_read_bit(br, &bit))
1203 return false;
1204 lsbs <<= 1;
1205 lsbs |= bit;
1206 lsbs -= d;
1207 }
1208 /* compose the value */
1209 *val = msbs * parameter + lsbs;
1210 }
1211
1212 return true;
1213}
1214#endif /* UNUSED */
1215
1216/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1217FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1218{
1219 FLAC__uint32 v = 0;
1220 FLAC__uint32 x;
1221 unsigned i;
1222
1223 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1224 return false;
1225 if(raw)
1226 raw[(*rawlen)++] = (FLAC__byte)x;
1227 if(!(x & 0x80)) { /* 0xxxxxxx */
1228 v = x;
1229 i = 0;
1230 }
1231 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1232 v = x & 0x1F;
1233 i = 1;
1234 }
1235 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1236 v = x & 0x0F;
1237 i = 2;
1238 }
1239 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1240 v = x & 0x07;
1241 i = 3;
1242 }
1243 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1244 v = x & 0x03;
1245 i = 4;
1246 }
1247 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1248 v = x & 0x01;
1249 i = 5;
1250 }
1251 else {
1252 *val = 0xffffffff;
1253 return true;
1254 }
1255 for( ; i; i--) {
1256 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1257 return false;
1258 if(raw)
1259 raw[(*rawlen)++] = (FLAC__byte)x;
1260 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1261 *val = 0xffffffff;
1262 return true;
1263 }
1264 v <<= 6;
1265 v |= (x & 0x3F);
1266 }
1267 *val = v;
1268 return true;
1269}
1270
1271/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1272FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1273{
1274 FLAC__uint64 v = 0;
1275 FLAC__uint32 x;
1276 unsigned i;
1277
1278 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1279 return false;
1280 if(raw)
1281 raw[(*rawlen)++] = (FLAC__byte)x;
1282 if(!(x & 0x80)) { /* 0xxxxxxx */
1283 v = x;
1284 i = 0;
1285 }
1286 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1287 v = x & 0x1F;
1288 i = 1;
1289 }
1290 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1291 v = x & 0x0F;
1292 i = 2;
1293 }
1294 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1295 v = x & 0x07;
1296 i = 3;
1297 }
1298 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1299 v = x & 0x03;
1300 i = 4;
1301 }
1302 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1303 v = x & 0x01;
1304 i = 5;
1305 }
1306 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1307 v = 0;
1308 i = 6;
1309 }
1310 else {
1311 *val = FLAC__U64L(0xffffffffffffffff);
1312 return true;
1313 }
1314 for( ; i; i--) {
1315 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1316 return false;
1317 if(raw)
1318 raw[(*rawlen)++] = (FLAC__byte)x;
1319 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1320 *val = FLAC__U64L(0xffffffffffffffff);
1321 return true;
1322 }
1323 v <<= 6;
1324 v |= (x & 0x3F);
1325 }
1326 *val = v;
1327 return true;
1328}