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