1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <retro_inline.h>
41
42 #include "private/bitmath.h"
43 #include "private/bitreader.h"
44 #include "private/crc.h"
45 #include "private/macros.h"
46 #include "FLAC/assert.h"
47 #include "share/compat.h"
48 #include "share/endswap.h"
49
50 #include <retro_miscellaneous.h>
51
52 /* Things should be fastest when this matches the machine word size */
53 /* WATCHOUT: if you change this you must also change the following #defines down to FLAC__clz_uint32 below to match */
54 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
55 /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
56 #define FLAC__BYTES_PER_WORD 4 /* sizeof uint32_t */
57 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD)
58 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
59 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
60 #ifdef MSB_FIRST
61 #define SWAP_BE_WORD_TO_HOST(x) (x)
62 #else
63 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
64 #endif
65
66 /*
67 * This should be at least twice as large as the largest number of words
68 * required to represent any 'number' (in any encoding) you are going to
69 * read. With FLAC this is on the order of maybe a few hundred bits.
70 * If the buffer is smaller than that, the decoder won't be able to read
71 * in a whole number that is in a variable length encoding (e.g. Rice).
72 * But to be practical it should be at least 1K bytes.
73 *
74 * Increase this number to decrease the number of read callbacks, at the
75 * expense of using more memory. Or decrease for the reverse effect,
76 * keeping in mind the limit from the first paragraph. The optimal size
77 * also depends on the CPU cache size and other factors; some twiddling
78 * may be necessary to squeeze out the best performance.
79 */
80 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
81
82 /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
83 struct FLAC__BitReader {
84 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
85 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
86 uint32_t *buffer;
87 unsigned capacity; /* in words */
88 unsigned words; /* # of completed words in buffer */
89 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
90 unsigned consumed_words; /* #words ... */
91 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
92 unsigned read_crc16; /* the running frame CRC */
93 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
94 FLAC__BitReaderReadCallback read_callback;
95 void *client_data;
96 FLAC__CPUInfo cpu_info;
97 };
98
crc16_update_word_(FLAC__BitReader * br,uint32_t word)99 static INLINE void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
100 {
101 register unsigned crc = br->read_crc16;
102 #if FLAC__BYTES_PER_WORD == 4
103 switch(br->crc16_align) {
104 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
105 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
106 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
107 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
108 }
109 #elif FLAC__BYTES_PER_WORD == 8
110 switch(br->crc16_align) {
111 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
112 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
113 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
114 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
115 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
116 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
117 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
118 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
119 }
120 #else
121 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
122 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
123 br->read_crc16 = crc;
124 #endif
125 br->crc16_align = 0;
126 }
127
128 /* would be static except it needs to be called by asm routines */
bitreader_read_from_client_(FLAC__BitReader * br)129 FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
130 {
131 unsigned start, end;
132 size_t bytes;
133 FLAC__byte *target;
134
135 /* first shift the unconsumed buffer data toward the front as much as possible */
136 if(br->consumed_words > 0) {
137 start = br->consumed_words;
138 end = br->words + (br->bytes? 1:0);
139 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
140
141 br->words -= start;
142 br->consumed_words = 0;
143 }
144
145 /*
146 * set the target for reading, taking into account word alignment and endianness
147 */
148 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
149 if(bytes == 0)
150 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
151 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
152
153 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
154 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
155 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
156 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
157 * ^^-------target, bytes=3
158 * on LE machines, have to byteswap the odd tail word so nothing is
159 * overwritten:
160 */
161 #ifndef MSB_FIRST
162 if(br->bytes)
163 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
164 #endif
165
166 /* now it looks like:
167 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
168 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
169 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
170 * ^^-------target, bytes=3
171 */
172
173 /* read in the data; note that the callback may return a smaller number of bytes */
174 if(!br->read_callback(target, &bytes, br->client_data))
175 return false;
176
177 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
178 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
179 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
180 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
181 * now have to byteswap on LE machines:
182 */
183 #ifndef MSB_FIRST
184 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
185 for(start = br->words; start < end; start++)
186 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
187 #endif
188
189 /* now it looks like:
190 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
191 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
192 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
193 * finally we'll update the reader values:
194 */
195 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
196 br->words = end / FLAC__BYTES_PER_WORD;
197 br->bytes = end % FLAC__BYTES_PER_WORD;
198
199 return true;
200 }
201
202 /***********************************************************************
203 *
204 * Class constructor/destructor
205 *
206 ***********************************************************************/
207
FLAC__bitreader_new(void)208 FLAC__BitReader *FLAC__bitreader_new(void)
209 {
210 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
211
212 /* calloc() implies:
213 memset(br, 0, sizeof(FLAC__BitReader));
214 br->buffer = 0;
215 br->capacity = 0;
216 br->words = br->bytes = 0;
217 br->consumed_words = br->consumed_bits = 0;
218 br->read_callback = 0;
219 br->client_data = 0;
220 */
221 return br;
222 }
223
FLAC__bitreader_delete(FLAC__BitReader * br)224 void FLAC__bitreader_delete(FLAC__BitReader *br)
225 {
226 FLAC__ASSERT(0 != br);
227
228 FLAC__bitreader_free(br);
229 free(br);
230 }
231
232 /***********************************************************************
233 *
234 * Public class methods
235 *
236 ***********************************************************************/
237
FLAC__bitreader_init(FLAC__BitReader * br,FLAC__CPUInfo cpu,FLAC__BitReaderReadCallback rcb,void * cd)238 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
239 {
240 FLAC__ASSERT(0 != br);
241
242 br->words = br->bytes = 0;
243 br->consumed_words = br->consumed_bits = 0;
244 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
245 br->buffer = (uint32_t*)malloc(sizeof(uint32_t) * br->capacity);
246 if(br->buffer == 0)
247 return false;
248 br->read_callback = rcb;
249 br->client_data = cd;
250 br->cpu_info = cpu;
251
252 return true;
253 }
254
FLAC__bitreader_free(FLAC__BitReader * br)255 void FLAC__bitreader_free(FLAC__BitReader *br)
256 {
257 FLAC__ASSERT(0 != br);
258
259 if(0 != br->buffer)
260 free(br->buffer);
261 br->buffer = 0;
262 br->capacity = 0;
263 br->words = br->bytes = 0;
264 br->consumed_words = br->consumed_bits = 0;
265 br->read_callback = 0;
266 br->client_data = 0;
267 }
268
FLAC__bitreader_clear(FLAC__BitReader * br)269 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
270 {
271 br->words = br->bytes = 0;
272 br->consumed_words = br->consumed_bits = 0;
273 return true;
274 }
275
FLAC__bitreader_dump(const FLAC__BitReader * br,FILE * out)276 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
277 {
278 unsigned i, j;
279 if(br == 0) {
280 fprintf(out, "bitreader is NULL\n");
281 }
282 else {
283 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);
284
285 for(i = 0; i < br->words; i++) {
286 fprintf(out, "%08X: ", i);
287 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
288 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
289 fprintf(out, ".");
290 else
291 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
292 fprintf(out, "\n");
293 }
294 if(br->bytes > 0) {
295 fprintf(out, "%08X: ", i);
296 for(j = 0; j < br->bytes*8; j++)
297 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
298 fprintf(out, ".");
299 else
300 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
301 fprintf(out, "\n");
302 }
303 }
304 }
305
FLAC__bitreader_reset_read_crc16(FLAC__BitReader * br,FLAC__uint16 seed)306 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
307 {
308 FLAC__ASSERT(0 != br);
309 FLAC__ASSERT(0 != br->buffer);
310 FLAC__ASSERT((br->consumed_bits & 7) == 0);
311
312 br->read_crc16 = (unsigned)seed;
313 br->crc16_align = br->consumed_bits;
314 }
315
FLAC__bitreader_get_read_crc16(FLAC__BitReader * br)316 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
317 {
318 FLAC__ASSERT(0 != br);
319 FLAC__ASSERT(0 != br->buffer);
320 FLAC__ASSERT((br->consumed_bits & 7) == 0);
321 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
322
323 /* CRC any tail bytes in a partially-consumed word */
324 if(br->consumed_bits) {
325 const uint32_t tail = br->buffer[br->consumed_words];
326 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
327 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
328 }
329 return br->read_crc16;
330 }
331
FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader * br)332 FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
333 {
334 return ((br->consumed_bits & 7) == 0);
335 }
336
FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader * br)337 unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
338 {
339 return 8 - (br->consumed_bits & 7);
340 }
341
FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader * br)342 unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
343 {
344 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
345 }
346
FLAC__bitreader_read_raw_uint32(FLAC__BitReader * br,FLAC__uint32 * val,unsigned bits)347 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
348 {
349 FLAC__ASSERT(0 != br);
350 FLAC__ASSERT(0 != br->buffer);
351
352 FLAC__ASSERT(bits <= 32);
353 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
354 FLAC__ASSERT(br->consumed_words <= br->words);
355
356 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
357 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
358
359 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
360 *val = 0;
361 return true;
362 }
363
364 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
365 if(!bitreader_read_from_client_(br))
366 return false;
367 }
368 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
369 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
370 if(br->consumed_bits) {
371 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
372 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
373 const uint32_t word = br->buffer[br->consumed_words];
374 if(bits < n) {
375 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
376 br->consumed_bits += bits;
377 return true;
378 }
379 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
380 bits -= n;
381 crc16_update_word_(br, word);
382 br->consumed_words++;
383 br->consumed_bits = 0;
384 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 */
385 *val <<= bits;
386 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
387 br->consumed_bits = bits;
388 }
389 return true;
390 }
391 else {
392 const uint32_t word = br->buffer[br->consumed_words];
393 if(bits < FLAC__BITS_PER_WORD) {
394 *val = word >> (FLAC__BITS_PER_WORD-bits);
395 br->consumed_bits = bits;
396 return true;
397 }
398 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
399 *val = word;
400 crc16_update_word_(br, word);
401 br->consumed_words++;
402 return true;
403 }
404 }
405 else {
406 /* in this case we're starting our read at a partial tail word;
407 * the reader has guaranteed that we have at least 'bits' bits
408 * available to read, which makes this case simpler.
409 */
410 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
411 if(br->consumed_bits) {
412 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
413 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
414 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
415 br->consumed_bits += bits;
416 return true;
417 }
418 else {
419 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
420 br->consumed_bits += bits;
421 return true;
422 }
423 }
424 }
425
FLAC__bitreader_read_raw_int32(FLAC__BitReader * br,FLAC__int32 * val,unsigned bits)426 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
427 {
428 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
429 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
430 return false;
431 /* sign-extend: */
432 *val <<= (32-bits);
433 *val >>= (32-bits);
434 return true;
435 }
436
FLAC__bitreader_read_raw_uint64(FLAC__BitReader * br,FLAC__uint64 * val,unsigned bits)437 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
438 {
439 FLAC__uint32 hi, lo;
440
441 if(bits > 32) {
442 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
443 return false;
444 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
445 return false;
446 *val = hi;
447 *val <<= 32;
448 *val |= lo;
449 }
450 else {
451 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
452 return false;
453 *val = lo;
454 }
455 return true;
456 }
457
FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader * br,FLAC__uint32 * val)458 FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
459 {
460 FLAC__uint32 x8, x32 = 0;
461
462 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
463
464 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
465 return false;
466
467 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
468 return false;
469 x32 |= (x8 << 8);
470
471 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
472 return false;
473 x32 |= (x8 << 16);
474
475 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
476 return false;
477 x32 |= (x8 << 24);
478
479 *val = x32;
480 return true;
481 }
482
FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader * br,unsigned bits)483 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
484 {
485 /*
486 * OPT: a faster implementation is possible but probably not that useful
487 * since this is only called a couple of times in the metadata readers.
488 */
489 FLAC__ASSERT(0 != br);
490 FLAC__ASSERT(0 != br->buffer);
491
492 if(bits > 0) {
493 const unsigned n = br->consumed_bits & 7;
494 unsigned m;
495 FLAC__uint32 x;
496
497 if(n != 0) {
498 m = MIN(8-n, bits);
499 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
500 return false;
501 bits -= m;
502 }
503 m = bits / 8;
504 if(m > 0) {
505 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
506 return false;
507 bits %= 8;
508 }
509 if(bits > 0) {
510 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
511 return false;
512 }
513 }
514
515 return true;
516 }
517
FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader * br,unsigned nvals)518 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
519 {
520 FLAC__uint32 x;
521
522 FLAC__ASSERT(0 != br);
523 FLAC__ASSERT(0 != br->buffer);
524 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
525
526 /* step 1: skip over partial head word to get word aligned */
527 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
528 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
529 return false;
530 nvals--;
531 }
532 if(0 == nvals)
533 return true;
534 /* step 2: skip whole words in chunks */
535 while(nvals >= FLAC__BYTES_PER_WORD) {
536 if(br->consumed_words < br->words) {
537 br->consumed_words++;
538 nvals -= FLAC__BYTES_PER_WORD;
539 }
540 else if(!bitreader_read_from_client_(br))
541 return false;
542 }
543 /* step 3: skip any remainder from partial tail bytes */
544 while(nvals) {
545 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
546 return false;
547 nvals--;
548 }
549
550 return true;
551 }
552
FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader * br,FLAC__byte * val,unsigned nvals)553 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
554 {
555 FLAC__uint32 x;
556
557 FLAC__ASSERT(0 != br);
558 FLAC__ASSERT(0 != br->buffer);
559 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
560
561 /* step 1: read from partial head word to get word aligned */
562 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
563 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
564 return false;
565 *val++ = (FLAC__byte)x;
566 nvals--;
567 }
568 if(0 == nvals)
569 return true;
570 /* step 2: read whole words in chunks */
571 while(nvals >= FLAC__BYTES_PER_WORD) {
572 if(br->consumed_words < br->words) {
573 const uint32_t word = br->buffer[br->consumed_words++];
574 #if FLAC__BYTES_PER_WORD == 4
575 val[0] = (FLAC__byte)(word >> 24);
576 val[1] = (FLAC__byte)(word >> 16);
577 val[2] = (FLAC__byte)(word >> 8);
578 val[3] = (FLAC__byte)word;
579 #elif FLAC__BYTES_PER_WORD == 8
580 val[0] = (FLAC__byte)(word >> 56);
581 val[1] = (FLAC__byte)(word >> 48);
582 val[2] = (FLAC__byte)(word >> 40);
583 val[3] = (FLAC__byte)(word >> 32);
584 val[4] = (FLAC__byte)(word >> 24);
585 val[5] = (FLAC__byte)(word >> 16);
586 val[6] = (FLAC__byte)(word >> 8);
587 val[7] = (FLAC__byte)word;
588 #else
589 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
590 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
591 #endif
592 val += FLAC__BYTES_PER_WORD;
593 nvals -= FLAC__BYTES_PER_WORD;
594 }
595 else if(!bitreader_read_from_client_(br))
596 return false;
597 }
598 /* step 3: read any remainder from partial tail bytes */
599 while(nvals) {
600 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
601 return false;
602 *val++ = (FLAC__byte)x;
603 nvals--;
604 }
605
606 return true;
607 }
608
FLAC__bitreader_read_unary_unsigned(FLAC__BitReader * br,unsigned * val)609 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
610 #if 0 /* slow but readable version */
611 {
612 unsigned bit;
613
614 FLAC__ASSERT(0 != br);
615 FLAC__ASSERT(0 != br->buffer);
616
617 *val = 0;
618 while(1) {
619 if(!FLAC__bitreader_read_bit(br, &bit))
620 return false;
621 if(bit)
622 break;
623 else
624 *val++;
625 }
626 return true;
627 }
628 #else
629 {
630 unsigned i;
631
632 FLAC__ASSERT(0 != br);
633 FLAC__ASSERT(0 != br->buffer);
634
635 *val = 0;
636 while(1) {
637 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
638 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
639 if(b) {
640 i = FLAC__clz_uint32(b);
641 *val += i;
642 i++;
643 br->consumed_bits += i;
644 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
645 crc16_update_word_(br, br->buffer[br->consumed_words]);
646 br->consumed_words++;
647 br->consumed_bits = 0;
648 }
649 return true;
650 }
651 else {
652 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
653 crc16_update_word_(br, br->buffer[br->consumed_words]);
654 br->consumed_words++;
655 br->consumed_bits = 0;
656 /* didn't find stop bit yet, have to keep going... */
657 }
658 }
659 /* at this point we've eaten up all the whole words; have to try
660 * reading through any tail bytes before calling the read callback.
661 * this is a repeat of the above logic adjusted for the fact we
662 * don't have a whole word. note though if the client is feeding
663 * us data a byte at a time (unlikely), br->consumed_bits may not
664 * be zero.
665 */
666 if(br->bytes*8 > br->consumed_bits) {
667 const unsigned end = br->bytes * 8;
668 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
669 if(b) {
670 i = FLAC__clz_uint32(b);
671 *val += i;
672 i++;
673 br->consumed_bits += i;
674 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
675 return true;
676 }
677 else {
678 *val += end - br->consumed_bits;
679 br->consumed_bits = end;
680 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
681 /* didn't find stop bit yet, have to keep going... */
682 }
683 }
684 if(!bitreader_read_from_client_(br))
685 return false;
686 }
687 }
688 #endif
689
FLAC__bitreader_read_rice_signed(FLAC__BitReader * br,int * val,unsigned parameter)690 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
691 {
692 FLAC__uint32 lsbs = 0, msbs = 0;
693 unsigned uval;
694
695 FLAC__ASSERT(0 != br);
696 FLAC__ASSERT(0 != br->buffer);
697 FLAC__ASSERT(parameter <= 31);
698
699 /* read the unary MSBs and end bit */
700 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
701 return false;
702
703 /* read the binary LSBs */
704 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
705 return false;
706
707 /* compose the value */
708 uval = (msbs << parameter) | lsbs;
709 if(uval & 1)
710 *val = -((int)(uval >> 1)) - 1;
711 else
712 *val = (int)(uval >> 1);
713
714 return true;
715 }
716
717 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
FLAC__bitreader_read_rice_signed_block(FLAC__BitReader * br,int vals[],unsigned nvals,unsigned parameter)718 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
719 {
720 /* try and get br->consumed_words and br->consumed_bits into register;
721 * must remember to flush them back to *br before calling other
722 * bitreader functions that use them, and before returning */
723 unsigned cwords, words, lsbs, msbs, x, y;
724 unsigned ucbits; /* keep track of the number of unconsumed bits in word */
725 uint32_t b;
726 int *val, *end;
727
728 FLAC__ASSERT(0 != br);
729 FLAC__ASSERT(0 != br->buffer);
730 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
731 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
732 FLAC__ASSERT(parameter < 32);
733 /* 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 */
734
735 val = vals;
736 end = vals + nvals;
737
738 if(parameter == 0) {
739 while(val < end) {
740 /* read the unary MSBs and end bit */
741 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
742 return false;
743
744 *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1);
745 }
746
747 return true;
748 }
749
750 FLAC__ASSERT(parameter > 0);
751
752 cwords = br->consumed_words;
753 words = br->words;
754
755 /* if we've not consumed up to a partial tail word... */
756 if(cwords >= words) {
757 x = 0;
758 goto process_tail;
759 }
760
761 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
762 b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */
763
764 while(val < end) {
765 /* read the unary MSBs and end bit */
766 x = y = FLAC__clz2_uint32(b);
767 if(x == FLAC__BITS_PER_WORD) {
768 x = ucbits;
769 do {
770 /* didn't find stop bit yet, have to keep going... */
771 crc16_update_word_(br, br->buffer[cwords++]);
772 if (cwords >= words)
773 goto incomplete_msbs;
774 b = br->buffer[cwords];
775 y = FLAC__clz2_uint32(b);
776 x += y;
777 } while(y == FLAC__BITS_PER_WORD);
778 }
779 b <<= y;
780 b <<= 1; /* account for stop bit */
781 ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD;
782 msbs = x;
783
784 /* read the binary LSBs */
785 x = b >> (FLAC__BITS_PER_WORD - parameter);
786 if(parameter <= ucbits) {
787 ucbits -= parameter;
788 b <<= parameter;
789 } else {
790 /* there are still bits left to read, they will all be in the next word */
791 crc16_update_word_(br, br->buffer[cwords++]);
792 if (cwords >= words)
793 goto incomplete_lsbs;
794 b = br->buffer[cwords];
795 ucbits += FLAC__BITS_PER_WORD - parameter;
796 x |= b >> ucbits;
797 b <<= FLAC__BITS_PER_WORD - ucbits;
798 }
799 lsbs = x;
800
801 /* compose the value */
802 x = (msbs << parameter) | lsbs;
803 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
804
805 continue;
806
807 /* at this point we've eaten up all the whole words */
808 process_tail:
809 do {
810 if(0) {
811 incomplete_msbs:
812 br->consumed_bits = 0;
813 br->consumed_words = cwords;
814 }
815
816 /* read the unary MSBs and end bit */
817 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
818 return false;
819 msbs += x;
820 x = ucbits = 0;
821
822 if(0) {
823 incomplete_lsbs:
824 br->consumed_bits = 0;
825 br->consumed_words = cwords;
826 }
827
828 /* read the binary LSBs */
829 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits))
830 return false;
831 lsbs = x | lsbs;
832
833 /* compose the value */
834 x = (msbs << parameter) | lsbs;
835 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
836 x = 0;
837
838 cwords = br->consumed_words;
839 words = br->words;
840 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
841 b = br->buffer[cwords] << br->consumed_bits;
842 } while(cwords >= words && val < end);
843 }
844
845 if(ucbits == 0 && cwords < words) {
846 /* don't leave the head word with no unconsumed bits */
847 crc16_update_word_(br, br->buffer[cwords++]);
848 ucbits = FLAC__BITS_PER_WORD;
849 }
850
851 br->consumed_bits = FLAC__BITS_PER_WORD - ucbits;
852 br->consumed_words = cwords;
853
854 return true;
855 }
856
857 #if 0 /* UNUSED */
858 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
859 {
860 FLAC__uint32 lsbs = 0, msbs = 0;
861 unsigned bit, uval, k;
862
863 FLAC__ASSERT(0 != br);
864 FLAC__ASSERT(0 != br->buffer);
865
866 k = FLAC__bitmath_ilog2(parameter);
867
868 /* read the unary MSBs and end bit */
869 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
870 return false;
871
872 /* read the binary LSBs */
873 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
874 return false;
875
876 if(parameter == 1u<<k) {
877 /* compose the value */
878 uval = (msbs << k) | lsbs;
879 }
880 else {
881 unsigned d = (1 << (k+1)) - parameter;
882 if(lsbs >= d) {
883 if(!FLAC__bitreader_read_bit(br, &bit))
884 return false;
885 lsbs <<= 1;
886 lsbs |= bit;
887 lsbs -= d;
888 }
889 /* compose the value */
890 uval = msbs * parameter + lsbs;
891 }
892
893 /* unfold unsigned to signed */
894 if(uval & 1)
895 *val = -((int)(uval >> 1)) - 1;
896 else
897 *val = (int)(uval >> 1);
898
899 return true;
900 }
901
902 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
903 {
904 FLAC__uint32 lsbs, msbs = 0;
905 unsigned bit, k;
906
907 FLAC__ASSERT(0 != br);
908 FLAC__ASSERT(0 != br->buffer);
909
910 k = FLAC__bitmath_ilog2(parameter);
911
912 /* read the unary MSBs and end bit */
913 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
914 return false;
915
916 /* read the binary LSBs */
917 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
918 return false;
919
920 if(parameter == 1u<<k) {
921 /* compose the value */
922 *val = (msbs << k) | lsbs;
923 }
924 else {
925 unsigned d = (1 << (k+1)) - parameter;
926 if(lsbs >= d) {
927 if(!FLAC__bitreader_read_bit(br, &bit))
928 return false;
929 lsbs <<= 1;
930 lsbs |= bit;
931 lsbs -= d;
932 }
933 /* compose the value */
934 *val = msbs * parameter + lsbs;
935 }
936
937 return true;
938 }
939 #endif /* UNUSED */
940
941 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
FLAC__bitreader_read_utf8_uint32(FLAC__BitReader * br,FLAC__uint32 * val,FLAC__byte * raw,unsigned * rawlen)942 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
943 {
944 FLAC__uint32 v = 0;
945 FLAC__uint32 x;
946 unsigned i;
947
948 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
949 return false;
950 if(raw)
951 raw[(*rawlen)++] = (FLAC__byte)x;
952 if(!(x & 0x80)) { /* 0xxxxxxx */
953 v = x;
954 i = 0;
955 }
956 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
957 v = x & 0x1F;
958 i = 1;
959 }
960 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
961 v = x & 0x0F;
962 i = 2;
963 }
964 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
965 v = x & 0x07;
966 i = 3;
967 }
968 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
969 v = x & 0x03;
970 i = 4;
971 }
972 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
973 v = x & 0x01;
974 i = 5;
975 }
976 else {
977 *val = 0xffffffff;
978 return true;
979 }
980 for( ; i; i--) {
981 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
982 return false;
983 if(raw)
984 raw[(*rawlen)++] = (FLAC__byte)x;
985 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
986 *val = 0xffffffff;
987 return true;
988 }
989 v <<= 6;
990 v |= (x & 0x3F);
991 }
992 *val = v;
993 return true;
994 }
995
996 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
FLAC__bitreader_read_utf8_uint64(FLAC__BitReader * br,FLAC__uint64 * val,FLAC__byte * raw,unsigned * rawlen)997 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
998 {
999 FLAC__uint64 v = 0;
1000 FLAC__uint32 x;
1001 unsigned i;
1002
1003 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1004 return false;
1005 if(raw)
1006 raw[(*rawlen)++] = (FLAC__byte)x;
1007 if(!(x & 0x80)) { /* 0xxxxxxx */
1008 v = x;
1009 i = 0;
1010 }
1011 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1012 v = x & 0x1F;
1013 i = 1;
1014 }
1015 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1016 v = x & 0x0F;
1017 i = 2;
1018 }
1019 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1020 v = x & 0x07;
1021 i = 3;
1022 }
1023 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1024 v = x & 0x03;
1025 i = 4;
1026 }
1027 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1028 v = x & 0x01;
1029 i = 5;
1030 }
1031 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1032 v = 0;
1033 i = 6;
1034 }
1035 else {
1036 *val = FLAC__U64L(0xffffffffffffffff);
1037 return true;
1038 }
1039 for( ; i; i--) {
1040 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1041 return false;
1042 if(raw)
1043 raw[(*rawlen)++] = (FLAC__byte)x;
1044 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1045 *val = FLAC__U64L(0xffffffffffffffff);
1046 return true;
1047 }
1048 v <<= 6;
1049 v |= (x & 0x3F);
1050 }
1051 *val = v;
1052 return true;
1053 }
1054
1055 /* These functions are declared inline in this file but are also callable as
1056 * externs from elsewhere.
1057 * According to the C99 spec, section 6.7.4, simply providing a function
1058 * prototype in a header file without 'inline' and making the function inline
1059 * in this file should be sufficient.
1060 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
1061 * fix that we add extern declarations here.
1062 */
1063 extern FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br);
1064 extern unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br);
1065 extern unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br);
1066 extern FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val);
1067