The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1059 lines
32KB

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