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.

1063 lines
32KB

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