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.

1100 lines
34KB

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