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