The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.
Dependencies: GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom
Fork of GR-PEACH_Audio_Playback_Sample by
bitreader.c
00001 /* libFLAC - Free Lossless Audio Codec library 00002 * Copyright (C) 2000-2009 Josh Coalson 00003 * Copyright (C) 2011-2014 Xiph.Org Foundation 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 00012 * - Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * - Neither the name of the Xiph.org Foundation nor the names of its 00017 * contributors may be used to endorse or promote products derived from 00018 * this software without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 00024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifdef HAVE_CONFIG_H 00034 # include <config.h> 00035 #endif 00036 00037 #include <stdlib.h> 00038 #include <string.h> 00039 #include "private/bitmath.h" 00040 #include "private/bitreader.h" 00041 #include "private/crc.h" 00042 #include "private/macros.h" 00043 #include "FLAC/assert.h" 00044 #include "share/compat.h" 00045 #include "share/endswap.h" 00046 00047 /* Things should be fastest when this matches the machine word size */ 00048 /* WATCHOUT: if you change this you must also change the following #defines down to FLAC__clz_uint32 below to match */ 00049 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */ 00050 /* also, some sections currently only have fast versions for 4 or 8 bytes per word */ 00051 #define FLAC__BYTES_PER_WORD 4 /* sizeof uint32_t */ 00052 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD) 00053 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) 00054 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */ 00055 #if WORDS_BIGENDIAN 00056 #define SWAP_BE_WORD_TO_HOST(x) (x) 00057 #else 00058 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x) 00059 #endif 00060 00061 /* 00062 * This should be at least twice as large as the largest number of words 00063 * required to represent any 'number' (in any encoding) you are going to 00064 * read. With FLAC this is on the order of maybe a few hundred bits. 00065 * If the buffer is smaller than that, the decoder won't be able to read 00066 * in a whole number that is in a variable length encoding (e.g. Rice). 00067 * But to be practical it should be at least 1K bytes. 00068 * 00069 * Increase this number to decrease the number of read callbacks, at the 00070 * expense of using more memory. Or decrease for the reverse effect, 00071 * keeping in mind the limit from the first paragraph. The optimal size 00072 * also depends on the CPU cache size and other factors; some twiddling 00073 * may be necessary to squeeze out the best performance. 00074 */ 00075 static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */ 00076 00077 struct FLAC__BitReader { 00078 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */ 00079 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */ 00080 uint32_t *buffer; 00081 unsigned capacity; /* in words */ 00082 unsigned words; /* # of completed words in buffer */ 00083 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */ 00084 unsigned consumed_words; /* #words ... */ 00085 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */ 00086 unsigned read_crc16; /* the running frame CRC */ 00087 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */ 00088 FLAC__BitReaderReadCallback read_callback; 00089 void *client_data; 00090 }; 00091 00092 static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word) 00093 { 00094 register unsigned crc = br->read_crc16; 00095 #if FLAC__BYTES_PER_WORD == 4 00096 switch(br->crc16_align) { 00097 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc); 00098 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); 00099 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); 00100 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); 00101 } 00102 #elif FLAC__BYTES_PER_WORD == 8 00103 switch(br->crc16_align) { 00104 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc); 00105 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc); 00106 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc); 00107 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc); 00108 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc); 00109 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); 00110 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); 00111 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); 00112 } 00113 #else 00114 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8) 00115 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc); 00116 br->read_crc16 = crc; 00117 #endif 00118 br->crc16_align = 0; 00119 } 00120 00121 static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br) 00122 { 00123 unsigned start, end; 00124 size_t bytes; 00125 FLAC__byte *target; 00126 00127 /* first shift the unconsumed buffer data toward the front as much as possible */ 00128 if(br->consumed_words > 0) { 00129 start = br->consumed_words; 00130 end = br->words + (br->bytes? 1:0); 00131 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start)); 00132 00133 br->words -= start; 00134 br->consumed_words = 0; 00135 } 00136 00137 /* 00138 * set the target for reading, taking into account word alignment and endianness 00139 */ 00140 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes; 00141 if(bytes == 0) 00142 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */ 00143 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes; 00144 00145 /* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide) 00146 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified) 00147 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory) 00148 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care) 00149 * ^^-------target, bytes=3 00150 * on LE machines, have to byteswap the odd tail word so nothing is 00151 * overwritten: 00152 */ 00153 #if WORDS_BIGENDIAN 00154 #else 00155 if(br->bytes) 00156 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]); 00157 #endif 00158 00159 /* now it looks like: 00160 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 00161 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? 00162 * buffer[LE]: 44 33 22 11 55 ?? ?? ?? 00163 * ^^-------target, bytes=3 00164 */ 00165 00166 /* read in the data; note that the callback may return a smaller number of bytes */ 00167 if(!br->read_callback(target, &bytes, br->client_data)) 00168 return false; 00169 00170 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client: 00171 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00172 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? 00173 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ?? 00174 * now have to byteswap on LE machines: 00175 */ 00176 #if WORDS_BIGENDIAN 00177 #else 00178 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD; 00179 for(start = br->words; start < end; start++) 00180 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]); 00181 #endif 00182 00183 /* now it looks like: 00184 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00185 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? 00186 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD 00187 * finally we'll update the reader values: 00188 */ 00189 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes; 00190 br->words = end / FLAC__BYTES_PER_WORD; 00191 br->bytes = end % FLAC__BYTES_PER_WORD; 00192 00193 return true; 00194 } 00195 00196 /*********************************************************************** 00197 * 00198 * Class constructor/destructor 00199 * 00200 ***********************************************************************/ 00201 00202 FLAC__BitReader *FLAC__bitreader_new(void) 00203 { 00204 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader)); 00205 00206 /* calloc() implies: 00207 memset(br, 0, sizeof(FLAC__BitReader)); 00208 br->buffer = 0; 00209 br->capacity = 0; 00210 br->words = br->bytes = 0; 00211 br->consumed_words = br->consumed_bits = 0; 00212 br->read_callback = 0; 00213 br->client_data = 0; 00214 */ 00215 return br; 00216 } 00217 00218 void FLAC__bitreader_delete(FLAC__BitReader *br) 00219 { 00220 FLAC__ASSERT(0 != br); 00221 00222 FLAC__bitreader_free(br); 00223 free(br); 00224 } 00225 00226 /*********************************************************************** 00227 * 00228 * Public class methods 00229 * 00230 ***********************************************************************/ 00231 00232 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd) 00233 { 00234 FLAC__ASSERT(0 != br); 00235 00236 br->words = br->bytes = 0; 00237 br->consumed_words = br->consumed_bits = 0; 00238 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY; 00239 br->buffer = malloc(sizeof(uint32_t) * br->capacity); 00240 if(br->buffer == 0) 00241 return false; 00242 br->read_callback = rcb; 00243 br->client_data = cd; 00244 00245 return true; 00246 } 00247 00248 void FLAC__bitreader_free(FLAC__BitReader *br) 00249 { 00250 FLAC__ASSERT(0 != br); 00251 00252 if(0 != br->buffer) 00253 free(br->buffer); 00254 br->buffer = 0; 00255 br->capacity = 0; 00256 br->words = br->bytes = 0; 00257 br->consumed_words = br->consumed_bits = 0; 00258 br->read_callback = 0; 00259 br->client_data = 0; 00260 } 00261 00262 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br) 00263 { 00264 br->words = br->bytes = 0; 00265 br->consumed_words = br->consumed_bits = 0; 00266 return true; 00267 } 00268 00269 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out) 00270 { 00271 unsigned i, j; 00272 if(br == 0) { 00273 fprintf(out, "bitreader is NULL\n"); 00274 } 00275 else { 00276 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); 00277 00278 for(i = 0; i < br->words; i++) { 00279 fprintf(out, "%08X: ", i); 00280 for(j = 0; j < FLAC__BITS_PER_WORD; j++) 00281 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) 00282 fprintf(out, "."); 00283 else 00284 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0); 00285 fprintf(out, "\n"); 00286 } 00287 if(br->bytes > 0) { 00288 fprintf(out, "%08X: ", i); 00289 for(j = 0; j < br->bytes*8; j++) 00290 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) 00291 fprintf(out, "."); 00292 else 00293 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0); 00294 fprintf(out, "\n"); 00295 } 00296 } 00297 } 00298 00299 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed) 00300 { 00301 FLAC__ASSERT(0 != br); 00302 FLAC__ASSERT(0 != br->buffer); 00303 FLAC__ASSERT((br->consumed_bits & 7) == 0); 00304 00305 br->read_crc16 = (unsigned)seed; 00306 br->crc16_align = br->consumed_bits; 00307 } 00308 00309 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br) 00310 { 00311 FLAC__ASSERT(0 != br); 00312 FLAC__ASSERT(0 != br->buffer); 00313 FLAC__ASSERT((br->consumed_bits & 7) == 0); 00314 FLAC__ASSERT(br->crc16_align <= br->consumed_bits); 00315 00316 /* CRC any tail bytes in a partially-consumed word */ 00317 if(br->consumed_bits) { 00318 const uint32_t tail = br->buffer[br->consumed_words]; 00319 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8) 00320 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16); 00321 } 00322 return br->read_crc16; 00323 } 00324 00325 inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br) 00326 { 00327 return ((br->consumed_bits & 7) == 0); 00328 } 00329 00330 inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br) 00331 { 00332 return 8 - (br->consumed_bits & 7); 00333 } 00334 00335 inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br) 00336 { 00337 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits; 00338 } 00339 00340 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits) 00341 { 00342 FLAC__ASSERT(0 != br); 00343 FLAC__ASSERT(0 != br->buffer); 00344 00345 FLAC__ASSERT(bits <= 32); 00346 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits); 00347 FLAC__ASSERT(br->consumed_words <= br->words); 00348 00349 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ 00350 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); 00351 00352 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */ 00353 *val = 0; 00354 return true; 00355 } 00356 00357 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) { 00358 if(!bitreader_read_from_client_(br)) 00359 return false; 00360 } 00361 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ 00362 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ 00363 if(br->consumed_bits) { 00364 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ 00365 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits; 00366 const uint32_t word = br->buffer[br->consumed_words]; 00367 if(bits < n) { 00368 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits); 00369 br->consumed_bits += bits; 00370 return true; 00371 } 00372 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits); 00373 bits -= n; 00374 crc16_update_word_(br, word); 00375 br->consumed_words++; 00376 br->consumed_bits = 0; 00377 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 */ 00378 *val <<= bits; 00379 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits)); 00380 br->consumed_bits = bits; 00381 } 00382 return true; 00383 } 00384 else { 00385 const uint32_t word = br->buffer[br->consumed_words]; 00386 if(bits < FLAC__BITS_PER_WORD) { 00387 *val = word >> (FLAC__BITS_PER_WORD-bits); 00388 br->consumed_bits = bits; 00389 return true; 00390 } 00391 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */ 00392 *val = word; 00393 crc16_update_word_(br, word); 00394 br->consumed_words++; 00395 return true; 00396 } 00397 } 00398 else { 00399 /* in this case we're starting our read at a partial tail word; 00400 * the reader has guaranteed that we have at least 'bits' bits 00401 * available to read, which makes this case simpler. 00402 */ 00403 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ 00404 if(br->consumed_bits) { 00405 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ 00406 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8); 00407 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits); 00408 br->consumed_bits += bits; 00409 return true; 00410 } 00411 else { 00412 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits); 00413 br->consumed_bits += bits; 00414 return true; 00415 } 00416 } 00417 } 00418 00419 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits) 00420 { 00421 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */ 00422 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits)) 00423 return false; 00424 /* sign-extend: */ 00425 *val <<= (32-bits); 00426 *val >>= (32-bits); 00427 return true; 00428 } 00429 00430 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits) 00431 { 00432 FLAC__uint32 hi, lo; 00433 00434 if(bits > 32) { 00435 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32)) 00436 return false; 00437 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32)) 00438 return false; 00439 *val = hi; 00440 *val <<= 32; 00441 *val |= lo; 00442 } 00443 else { 00444 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits)) 00445 return false; 00446 *val = lo; 00447 } 00448 return true; 00449 } 00450 00451 inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val) 00452 { 00453 FLAC__uint32 x8, x32 = 0; 00454 00455 /* this doesn't need to be that fast as currently it is only used for vorbis comments */ 00456 00457 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8)) 00458 return false; 00459 00460 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) 00461 return false; 00462 x32 |= (x8 << 8); 00463 00464 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) 00465 return false; 00466 x32 |= (x8 << 16); 00467 00468 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) 00469 return false; 00470 x32 |= (x8 << 24); 00471 00472 *val = x32; 00473 return true; 00474 } 00475 00476 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits) 00477 { 00478 /* 00479 * OPT: a faster implementation is possible but probably not that useful 00480 * since this is only called a couple of times in the metadata readers. 00481 */ 00482 FLAC__ASSERT(0 != br); 00483 FLAC__ASSERT(0 != br->buffer); 00484 00485 if(bits > 0) { 00486 const unsigned n = br->consumed_bits & 7; 00487 unsigned m; 00488 FLAC__uint32 x; 00489 00490 if(n != 0) { 00491 m = flac_min(8-n, bits); 00492 if(!FLAC__bitreader_read_raw_uint32(br, &x, m)) 00493 return false; 00494 bits -= m; 00495 } 00496 m = bits / 8; 00497 if(m > 0) { 00498 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m)) 00499 return false; 00500 bits %= 8; 00501 } 00502 if(bits > 0) { 00503 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits)) 00504 return false; 00505 } 00506 } 00507 00508 return true; 00509 } 00510 00511 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals) 00512 { 00513 FLAC__uint32 x; 00514 00515 FLAC__ASSERT(0 != br); 00516 FLAC__ASSERT(0 != br->buffer); 00517 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); 00518 00519 /* step 1: skip over partial head word to get word aligned */ 00520 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ 00521 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00522 return false; 00523 nvals--; 00524 } 00525 if(0 == nvals) 00526 return true; 00527 /* step 2: skip whole words in chunks */ 00528 while(nvals >= FLAC__BYTES_PER_WORD) { 00529 if(br->consumed_words < br->words) { 00530 br->consumed_words++; 00531 nvals -= FLAC__BYTES_PER_WORD; 00532 } 00533 else if(!bitreader_read_from_client_(br)) 00534 return false; 00535 } 00536 /* step 3: skip any remainder from partial tail bytes */ 00537 while(nvals) { 00538 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00539 return false; 00540 nvals--; 00541 } 00542 00543 return true; 00544 } 00545 00546 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals) 00547 { 00548 FLAC__uint32 x; 00549 00550 FLAC__ASSERT(0 != br); 00551 FLAC__ASSERT(0 != br->buffer); 00552 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); 00553 00554 /* step 1: read from partial head word to get word aligned */ 00555 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ 00556 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00557 return false; 00558 *val++ = (FLAC__byte)x; 00559 nvals--; 00560 } 00561 if(0 == nvals) 00562 return true; 00563 /* step 2: read whole words in chunks */ 00564 while(nvals >= FLAC__BYTES_PER_WORD) { 00565 if(br->consumed_words < br->words) { 00566 const uint32_t word = br->buffer[br->consumed_words++]; 00567 #if FLAC__BYTES_PER_WORD == 4 00568 val[0] = (FLAC__byte)(word >> 24); 00569 val[1] = (FLAC__byte)(word >> 16); 00570 val[2] = (FLAC__byte)(word >> 8); 00571 val[3] = (FLAC__byte)word; 00572 #elif FLAC__BYTES_PER_WORD == 8 00573 val[0] = (FLAC__byte)(word >> 56); 00574 val[1] = (FLAC__byte)(word >> 48); 00575 val[2] = (FLAC__byte)(word >> 40); 00576 val[3] = (FLAC__byte)(word >> 32); 00577 val[4] = (FLAC__byte)(word >> 24); 00578 val[5] = (FLAC__byte)(word >> 16); 00579 val[6] = (FLAC__byte)(word >> 8); 00580 val[7] = (FLAC__byte)word; 00581 #else 00582 for(x = 0; x < FLAC__BYTES_PER_WORD; x++) 00583 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1))); 00584 #endif 00585 val += FLAC__BYTES_PER_WORD; 00586 nvals -= FLAC__BYTES_PER_WORD; 00587 } 00588 else if(!bitreader_read_from_client_(br)) 00589 return false; 00590 } 00591 /* step 3: read any remainder from partial tail bytes */ 00592 while(nvals) { 00593 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00594 return false; 00595 *val++ = (FLAC__byte)x; 00596 nvals--; 00597 } 00598 00599 return true; 00600 } 00601 00602 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val) 00603 #if 0 /* slow but readable version */ 00604 { 00605 unsigned bit; 00606 00607 FLAC__ASSERT(0 != br); 00608 FLAC__ASSERT(0 != br->buffer); 00609 00610 *val = 0; 00611 while(1) { 00612 if(!FLAC__bitreader_read_bit(br, &bit)) 00613 return false; 00614 if(bit) 00615 break; 00616 else 00617 *val++; 00618 } 00619 return true; 00620 } 00621 #else 00622 { 00623 unsigned i; 00624 00625 FLAC__ASSERT(0 != br); 00626 FLAC__ASSERT(0 != br->buffer); 00627 00628 *val = 0; 00629 while(1) { 00630 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ 00631 uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits; 00632 if(b) { 00633 i = FLAC__clz_uint32(b); 00634 *val += i; 00635 i++; 00636 br->consumed_bits += i; 00637 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */ 00638 crc16_update_word_(br, br->buffer[br->consumed_words]); 00639 br->consumed_words++; 00640 br->consumed_bits = 0; 00641 } 00642 return true; 00643 } 00644 else { 00645 *val += FLAC__BITS_PER_WORD - br->consumed_bits; 00646 crc16_update_word_(br, br->buffer[br->consumed_words]); 00647 br->consumed_words++; 00648 br->consumed_bits = 0; 00649 /* didn't find stop bit yet, have to keep going... */ 00650 } 00651 } 00652 /* at this point we've eaten up all the whole words; have to try 00653 * reading through any tail bytes before calling the read callback. 00654 * this is a repeat of the above logic adjusted for the fact we 00655 * don't have a whole word. note though if the client is feeding 00656 * us data a byte at a time (unlikely), br->consumed_bits may not 00657 * be zero. 00658 */ 00659 if(br->bytes*8 > br->consumed_bits) { 00660 const unsigned end = br->bytes * 8; 00661 uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits; 00662 if(b) { 00663 i = FLAC__clz_uint32(b); 00664 *val += i; 00665 i++; 00666 br->consumed_bits += i; 00667 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); 00668 return true; 00669 } 00670 else { 00671 *val += end - br->consumed_bits; 00672 br->consumed_bits = end; 00673 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); 00674 /* didn't find stop bit yet, have to keep going... */ 00675 } 00676 } 00677 if(!bitreader_read_from_client_(br)) 00678 return false; 00679 } 00680 } 00681 #endif 00682 00683 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter) 00684 { 00685 FLAC__uint32 lsbs = 0, msbs = 0; 00686 unsigned uval; 00687 00688 FLAC__ASSERT(0 != br); 00689 FLAC__ASSERT(0 != br->buffer); 00690 FLAC__ASSERT(parameter <= 31); 00691 00692 /* read the unary MSBs and end bit */ 00693 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) 00694 return false; 00695 00696 /* read the binary LSBs */ 00697 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter)) 00698 return false; 00699 00700 /* compose the value */ 00701 uval = (msbs << parameter) | lsbs; 00702 if(uval & 1) 00703 *val = -((int)(uval >> 1)) - 1; 00704 else 00705 *val = (int)(uval >> 1); 00706 00707 return true; 00708 } 00709 00710 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */ 00711 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter) 00712 { 00713 /* try and get br->consumed_words and br->consumed_bits into register; 00714 * must remember to flush them back to *br before calling other 00715 * bitreader functions that use them, and before returning */ 00716 unsigned cwords, words, lsbs, msbs, x, y; 00717 unsigned ucbits; /* keep track of the number of unconsumed bits in word */ 00718 uint32_t b; 00719 int *val, *end; 00720 00721 FLAC__ASSERT(0 != br); 00722 FLAC__ASSERT(0 != br->buffer); 00723 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ 00724 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); 00725 FLAC__ASSERT(parameter < 32); 00726 /* 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 */ 00727 00728 val = vals; 00729 end = vals + nvals; 00730 00731 if(parameter == 0) { 00732 while(val < end) { 00733 /* read the unary MSBs and end bit */ 00734 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) 00735 return false; 00736 00737 *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1); 00738 } 00739 00740 return true; 00741 } 00742 00743 FLAC__ASSERT(parameter > 0); 00744 00745 cwords = br->consumed_words; 00746 words = br->words; 00747 00748 /* if we've not consumed up to a partial tail word... */ 00749 if(cwords >= words) { 00750 x = 0; 00751 goto process_tail; 00752 } 00753 00754 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits; 00755 b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */ 00756 00757 while(val < end) { 00758 /* read the unary MSBs and end bit */ 00759 x = y = FLAC__clz2_uint32(b); 00760 if(x == FLAC__BITS_PER_WORD) { 00761 x = ucbits; 00762 do { 00763 /* didn't find stop bit yet, have to keep going... */ 00764 crc16_update_word_(br, br->buffer[cwords++]); 00765 if (cwords >= words) 00766 goto incomplete_msbs; 00767 b = br->buffer[cwords]; 00768 y = FLAC__clz2_uint32(b); 00769 x += y; 00770 } while(y == FLAC__BITS_PER_WORD); 00771 } 00772 b <<= y; 00773 b <<= 1; /* account for stop bit */ 00774 ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD; 00775 msbs = x; 00776 00777 /* read the binary LSBs */ 00778 x = b >> (FLAC__BITS_PER_WORD - parameter); 00779 if(parameter <= ucbits) { 00780 ucbits -= parameter; 00781 b <<= parameter; 00782 } else { 00783 /* there are still bits left to read, they will all be in the next word */ 00784 crc16_update_word_(br, br->buffer[cwords++]); 00785 if (cwords >= words) 00786 goto incomplete_lsbs; 00787 b = br->buffer[cwords]; 00788 ucbits += FLAC__BITS_PER_WORD - parameter; 00789 x |= b >> ucbits; 00790 b <<= FLAC__BITS_PER_WORD - ucbits; 00791 } 00792 lsbs = x; 00793 00794 /* compose the value */ 00795 x = (msbs << parameter) | lsbs; 00796 *val++ = (int)(x >> 1) ^ -(int)(x & 1); 00797 00798 continue; 00799 00800 /* at this point we've eaten up all the whole words */ 00801 process_tail: 00802 do { 00803 if(0) { 00804 incomplete_msbs: 00805 br->consumed_bits = 0; 00806 br->consumed_words = cwords; 00807 } 00808 00809 /* read the unary MSBs and end bit */ 00810 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) 00811 return false; 00812 msbs += x; 00813 x = ucbits = 0; 00814 00815 if(0) { 00816 incomplete_lsbs: 00817 br->consumed_bits = 0; 00818 br->consumed_words = cwords; 00819 } 00820 00821 /* read the binary LSBs */ 00822 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits)) 00823 return false; 00824 lsbs = x | lsbs; 00825 00826 /* compose the value */ 00827 x = (msbs << parameter) | lsbs; 00828 *val++ = (int)(x >> 1) ^ -(int)(x & 1); 00829 x = 0; 00830 00831 cwords = br->consumed_words; 00832 words = br->words; 00833 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits; 00834 b = br->buffer[cwords] << br->consumed_bits; 00835 } while(cwords >= words && val < end); 00836 } 00837 00838 if(ucbits == 0 && cwords < words) { 00839 /* don't leave the head word with no unconsumed bits */ 00840 crc16_update_word_(br, br->buffer[cwords++]); 00841 ucbits = FLAC__BITS_PER_WORD; 00842 } 00843 00844 br->consumed_bits = FLAC__BITS_PER_WORD - ucbits; 00845 br->consumed_words = cwords; 00846 00847 return true; 00848 } 00849 00850 #if 0 /* UNUSED */ 00851 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter) 00852 { 00853 FLAC__uint32 lsbs = 0, msbs = 0; 00854 unsigned bit, uval, k; 00855 00856 FLAC__ASSERT(0 != br); 00857 FLAC__ASSERT(0 != br->buffer); 00858 00859 k = FLAC__bitmath_ilog2(parameter); 00860 00861 /* read the unary MSBs and end bit */ 00862 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) 00863 return false; 00864 00865 /* read the binary LSBs */ 00866 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) 00867 return false; 00868 00869 if(parameter == 1u<<k) { 00870 /* compose the value */ 00871 uval = (msbs << k) | lsbs; 00872 } 00873 else { 00874 unsigned d = (1 << (k+1)) - parameter; 00875 if(lsbs >= d) { 00876 if(!FLAC__bitreader_read_bit(br, &bit)) 00877 return false; 00878 lsbs <<= 1; 00879 lsbs |= bit; 00880 lsbs -= d; 00881 } 00882 /* compose the value */ 00883 uval = msbs * parameter + lsbs; 00884 } 00885 00886 /* unfold unsigned to signed */ 00887 if(uval & 1) 00888 *val = -((int)(uval >> 1)) - 1; 00889 else 00890 *val = (int)(uval >> 1); 00891 00892 return true; 00893 } 00894 00895 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter) 00896 { 00897 FLAC__uint32 lsbs, msbs = 0; 00898 unsigned bit, k; 00899 00900 FLAC__ASSERT(0 != br); 00901 FLAC__ASSERT(0 != br->buffer); 00902 00903 k = FLAC__bitmath_ilog2(parameter); 00904 00905 /* read the unary MSBs and end bit */ 00906 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) 00907 return false; 00908 00909 /* read the binary LSBs */ 00910 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) 00911 return false; 00912 00913 if(parameter == 1u<<k) { 00914 /* compose the value */ 00915 *val = (msbs << k) | lsbs; 00916 } 00917 else { 00918 unsigned d = (1 << (k+1)) - parameter; 00919 if(lsbs >= d) { 00920 if(!FLAC__bitreader_read_bit(br, &bit)) 00921 return false; 00922 lsbs <<= 1; 00923 lsbs |= bit; 00924 lsbs -= d; 00925 } 00926 /* compose the value */ 00927 *val = msbs * parameter + lsbs; 00928 } 00929 00930 return true; 00931 } 00932 #endif /* UNUSED */ 00933 00934 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */ 00935 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen) 00936 { 00937 FLAC__uint32 v = 0; 00938 FLAC__uint32 x; 00939 unsigned i; 00940 00941 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00942 return false; 00943 if(raw) 00944 raw[(*rawlen)++] = (FLAC__byte)x; 00945 if(!(x & 0x80)) { /* 0xxxxxxx */ 00946 v = x; 00947 i = 0; 00948 } 00949 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ 00950 v = x & 0x1F; 00951 i = 1; 00952 } 00953 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ 00954 v = x & 0x0F; 00955 i = 2; 00956 } 00957 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ 00958 v = x & 0x07; 00959 i = 3; 00960 } 00961 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ 00962 v = x & 0x03; 00963 i = 4; 00964 } 00965 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ 00966 v = x & 0x01; 00967 i = 5; 00968 } 00969 else { 00970 *val = 0xffffffff; 00971 return true; 00972 } 00973 for( ; i; i--) { 00974 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00975 return false; 00976 if(raw) 00977 raw[(*rawlen)++] = (FLAC__byte)x; 00978 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ 00979 *val = 0xffffffff; 00980 return true; 00981 } 00982 v <<= 6; 00983 v |= (x & 0x3F); 00984 } 00985 *val = v; 00986 return true; 00987 } 00988 00989 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */ 00990 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen) 00991 { 00992 FLAC__uint64 v = 0; 00993 FLAC__uint32 x; 00994 unsigned i; 00995 00996 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 00997 return false; 00998 if(raw) 00999 raw[(*rawlen)++] = (FLAC__byte)x; 01000 if(!(x & 0x80)) { /* 0xxxxxxx */ 01001 v = x; 01002 i = 0; 01003 } 01004 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ 01005 v = x & 0x1F; 01006 i = 1; 01007 } 01008 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ 01009 v = x & 0x0F; 01010 i = 2; 01011 } 01012 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ 01013 v = x & 0x07; 01014 i = 3; 01015 } 01016 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ 01017 v = x & 0x03; 01018 i = 4; 01019 } 01020 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ 01021 v = x & 0x01; 01022 i = 5; 01023 } 01024 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */ 01025 v = 0; 01026 i = 6; 01027 } 01028 else { 01029 *val = FLAC__U64L(0xffffffffffffffff); 01030 return true; 01031 } 01032 for( ; i; i--) { 01033 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) 01034 return false; 01035 if(raw) 01036 raw[(*rawlen)++] = (FLAC__byte)x; 01037 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ 01038 *val = FLAC__U64L(0xffffffffffffffff); 01039 return true; 01040 } 01041 v <<= 6; 01042 v |= (x & 0x3F); 01043 } 01044 *val = v; 01045 return true; 01046 } 01047 01048 /* These functions are declared inline in this file but are also callable as 01049 * externs from elsewhere. 01050 * According to the C99 spec, section 6.7.4, simply providing a function 01051 * prototype in a header file without 'inline' and making the function inline 01052 * in this file should be sufficient. 01053 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To 01054 * fix that we add extern declarations here. 01055 */ 01056 extern FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br); 01057 extern unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br); 01058 extern unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br); 01059 extern FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val);
Generated on Tue Jul 12 2022 19:32:28 by 1.7.2