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.

1351 lines
42KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <stdlib.h> /* for malloc() */
  35. #include <string.h> /* for memcpy(), memset() */
  36. #ifdef _MSC_VER
  37. #include <winsock.h> /* for ntohl() */
  38. #elif defined FLAC__SYS_DARWIN
  39. #include <machine/endian.h> /* for ntohl() */
  40. #elif defined __MINGW32__
  41. #include <winsock.h> /* for ntohl() */
  42. #else
  43. #include <netinet/in.h> /* for ntohl() */
  44. #endif
  45. #include "include/private/bitmath.h"
  46. #include "include/private/bitreader.h"
  47. #include "include/private/crc.h"
  48. #include "../assert.h"
  49. /* Things should be fastest when this matches the machine word size */
  50. /* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */
  51. /* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
  52. /* also, some sections currently only have fast versions for 4 or 8 bytes per word */
  53. typedef FLAC__uint32 brword;
  54. #define FLAC__BYTES_PER_WORD 4
  55. #define FLAC__BITS_PER_WORD 32
  56. #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
  57. /* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
  58. #if WORDS_BIGENDIAN
  59. #define SWAP_BE_WORD_TO_HOST(x) (x)
  60. #else
  61. #if defined (_MSC_VER) && defined (_X86_)
  62. #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
  63. #else
  64. #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
  65. #endif
  66. #endif
  67. /* counts the # of zero MSBs in a word */
  68. #define COUNT_ZERO_MSBS(word) ( \
  69. (word) <= 0xffff ? \
  70. ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
  71. ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
  72. )
  73. /* this alternate might be slightly faster on some systems/compilers: */
  74. #define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
  75. /*
  76. * This should be at least twice as large as the largest number of words
  77. * required to represent any 'number' (in any encoding) you are going to
  78. * read. With FLAC this is on the order of maybe a few hundred bits.
  79. * If the buffer is smaller than that, the decoder won't be able to read
  80. * in a whole number that is in a variable length encoding (e.g. Rice).
  81. * But to be practical it should be at least 1K bytes.
  82. *
  83. * Increase this number to decrease the number of read callbacks, at the
  84. * expense of using more memory. Or decrease for the reverse effect,
  85. * keeping in mind the limit from the first paragraph. The optimal size
  86. * also depends on the CPU cache size and other factors; some twiddling
  87. * may be necessary to squeeze out the best performance.
  88. */
  89. static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
  90. static const unsigned char byte_to_unary_table[] = {
  91. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  92. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  93. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  94. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  95. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  96. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  97. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  98. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  107. };
  108. #ifdef min
  109. #undef min
  110. #endif
  111. #define min(x,y) ((x)<(y)?(x):(y))
  112. #ifdef max
  113. #undef max
  114. #endif
  115. #define max(x,y) ((x)>(y)?(x):(y))
  116. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  117. #ifdef _MSC_VER
  118. #define FLAC__U64L(x) x
  119. #else
  120. #define FLAC__U64L(x) x##LLU
  121. #endif
  122. #ifndef FLaC__INLINE
  123. #define FLaC__INLINE inline
  124. #endif
  125. /* WATCHOUT: assembly routines rely on the order in which these fields are declared */
  126. struct FLAC__BitReader {
  127. /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
  128. /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
  129. brword *buffer;
  130. unsigned capacity; /* in words */
  131. unsigned words; /* # of completed words in buffer */
  132. unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
  133. unsigned consumed_words; /* #words ... */
  134. unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
  135. unsigned read_crc16; /* the running frame CRC */
  136. unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
  137. FLAC__BitReaderReadCallback read_callback;
  138. void *client_data;
  139. FLAC__CPUInfo cpu_info;
  140. };
  141. static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
  142. {
  143. register unsigned crc = br->read_crc16;
  144. #if FLAC__BYTES_PER_WORD == 4
  145. switch(br->crc16_align) {
  146. case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
  147. case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
  148. case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
  149. case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
  150. }
  151. #elif FLAC__BYTES_PER_WORD == 8
  152. switch(br->crc16_align) {
  153. case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
  154. case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
  155. case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
  156. case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
  157. case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
  158. case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
  159. case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
  160. case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
  161. }
  162. #else
  163. for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
  164. crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
  165. br->read_crc16 = crc;
  166. #endif
  167. br->crc16_align = 0;
  168. }
  169. /* would be static except it needs to be called by asm routines */
  170. FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
  171. {
  172. unsigned start, end;
  173. size_t bytes;
  174. FLAC__byte *target;
  175. /* first shift the unconsumed buffer data toward the front as much as possible */
  176. if(br->consumed_words > 0) {
  177. start = br->consumed_words;
  178. end = br->words + (br->bytes? 1:0);
  179. memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
  180. br->words -= start;
  181. br->consumed_words = 0;
  182. }
  183. /*
  184. * set the target for reading, taking into account word alignment and endianness
  185. */
  186. bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
  187. if(bytes == 0)
  188. return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
  189. target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
  190. /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
  191. * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
  192. * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
  193. * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
  194. * ^^-------target, bytes=3
  195. * on LE machines, have to byteswap the odd tail word so nothing is
  196. * overwritten:
  197. */
  198. #if WORDS_BIGENDIAN
  199. #else
  200. if(br->bytes)
  201. br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
  202. #endif
  203. /* now it looks like:
  204. * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
  205. * buffer[BE]: 11 22 33 44 55 ?? ?? ??
  206. * buffer[LE]: 44 33 22 11 55 ?? ?? ??
  207. * ^^-------target, bytes=3
  208. */
  209. /* read in the data; note that the callback may return a smaller number of bytes */
  210. if(!br->read_callback(target, &bytes, br->client_data))
  211. return false;
  212. /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
  213. * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
  214. * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
  215. * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
  216. * now have to byteswap on LE machines:
  217. */
  218. #if WORDS_BIGENDIAN
  219. #else
  220. end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
  221. # if defined(_MSC_VER) && defined (_X86_) && (FLAC__BYTES_PER_WORD == 4)
  222. if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
  223. start = br->words;
  224. local_swap32_block_(br->buffer + start, end - start);
  225. }
  226. else
  227. # endif
  228. for(start = br->words; start < end; start++)
  229. br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
  230. #endif
  231. /* now it looks like:
  232. * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
  233. * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
  234. * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
  235. * finally we'll update the reader values:
  236. */
  237. end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
  238. br->words = end / FLAC__BYTES_PER_WORD;
  239. br->bytes = end % FLAC__BYTES_PER_WORD;
  240. return true;
  241. }
  242. /***********************************************************************
  243. *
  244. * Class constructor/destructor
  245. *
  246. ***********************************************************************/
  247. FLAC__BitReader *FLAC__bitreader_new(void)
  248. {
  249. FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
  250. /* calloc() implies:
  251. memset(br, 0, sizeof(FLAC__BitReader));
  252. br->buffer = 0;
  253. br->capacity = 0;
  254. br->words = br->bytes = 0;
  255. br->consumed_words = br->consumed_bits = 0;
  256. br->read_callback = 0;
  257. br->client_data = 0;
  258. */
  259. return br;
  260. }
  261. void FLAC__bitreader_delete(FLAC__BitReader *br)
  262. {
  263. FLAC__ASSERT(0 != br);
  264. FLAC__bitreader_free(br);
  265. free(br);
  266. }
  267. /***********************************************************************
  268. *
  269. * Public class methods
  270. *
  271. ***********************************************************************/
  272. FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
  273. {
  274. FLAC__ASSERT(0 != br);
  275. br->words = br->bytes = 0;
  276. br->consumed_words = br->consumed_bits = 0;
  277. br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
  278. br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
  279. if(br->buffer == 0)
  280. return false;
  281. br->read_callback = rcb;
  282. br->client_data = cd;
  283. br->cpu_info = cpu;
  284. return true;
  285. }
  286. void FLAC__bitreader_free(FLAC__BitReader *br)
  287. {
  288. FLAC__ASSERT(0 != br);
  289. if(0 != br->buffer)
  290. free(br->buffer);
  291. br->buffer = 0;
  292. br->capacity = 0;
  293. br->words = br->bytes = 0;
  294. br->consumed_words = br->consumed_bits = 0;
  295. br->read_callback = 0;
  296. br->client_data = 0;
  297. }
  298. FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
  299. {
  300. br->words = br->bytes = 0;
  301. br->consumed_words = br->consumed_bits = 0;
  302. return true;
  303. }
  304. void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
  305. {
  306. unsigned i, j;
  307. if(br == 0) {
  308. fprintf(out, "bitreader is NULL\n");
  309. }
  310. else {
  311. 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);
  312. for(i = 0; i < br->words; i++) {
  313. fprintf(out, "%08X: ", i);
  314. for(j = 0; j < FLAC__BITS_PER_WORD; j++)
  315. if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
  316. fprintf(out, ".");
  317. else
  318. fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
  319. fprintf(out, "\n");
  320. }
  321. if(br->bytes > 0) {
  322. fprintf(out, "%08X: ", i);
  323. for(j = 0; j < br->bytes*8; j++)
  324. if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
  325. fprintf(out, ".");
  326. else
  327. fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
  328. fprintf(out, "\n");
  329. }
  330. }
  331. }
  332. void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
  333. {
  334. FLAC__ASSERT(0 != br);
  335. FLAC__ASSERT(0 != br->buffer);
  336. FLAC__ASSERT((br->consumed_bits & 7) == 0);
  337. br->read_crc16 = (unsigned)seed;
  338. br->crc16_align = br->consumed_bits;
  339. }
  340. FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
  341. {
  342. FLAC__ASSERT(0 != br);
  343. FLAC__ASSERT(0 != br->buffer);
  344. FLAC__ASSERT((br->consumed_bits & 7) == 0);
  345. FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
  346. /* CRC any tail bytes in a partially-consumed word */
  347. if(br->consumed_bits) {
  348. const brword tail = br->buffer[br->consumed_words];
  349. for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
  350. br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
  351. }
  352. return (FLAC__uint16) br->read_crc16;
  353. }
  354. FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
  355. {
  356. return ((br->consumed_bits & 7) == 0);
  357. }
  358. FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
  359. {
  360. return 8 - (br->consumed_bits & 7);
  361. }
  362. FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
  363. {
  364. return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
  365. }
  366. FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
  367. {
  368. FLAC__ASSERT(0 != br);
  369. FLAC__ASSERT(0 != br->buffer);
  370. FLAC__ASSERT(bits <= 32);
  371. FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
  372. FLAC__ASSERT(br->consumed_words <= br->words);
  373. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  374. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  375. if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
  376. *val = 0;
  377. return true;
  378. }
  379. while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
  380. if(!bitreader_read_from_client_(br))
  381. return false;
  382. }
  383. if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
  384. /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
  385. if(br->consumed_bits) {
  386. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  387. const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
  388. const brword word = br->buffer[br->consumed_words];
  389. if(bits < n) {
  390. *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
  391. br->consumed_bits += bits;
  392. return true;
  393. }
  394. *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
  395. bits -= n;
  396. crc16_update_word_(br, word);
  397. br->consumed_words++;
  398. br->consumed_bits = 0;
  399. 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 */
  400. *val <<= bits;
  401. *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
  402. br->consumed_bits = bits;
  403. }
  404. return true;
  405. }
  406. else {
  407. const brword word = br->buffer[br->consumed_words];
  408. if(bits < FLAC__BITS_PER_WORD) {
  409. *val = word >> (FLAC__BITS_PER_WORD-bits);
  410. br->consumed_bits = bits;
  411. return true;
  412. }
  413. /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
  414. *val = word;
  415. crc16_update_word_(br, word);
  416. br->consumed_words++;
  417. return true;
  418. }
  419. }
  420. else {
  421. /* in this case we're starting our read at a partial tail word;
  422. * the reader has guaranteed that we have at least 'bits' bits
  423. * available to read, which makes this case simpler.
  424. */
  425. /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
  426. if(br->consumed_bits) {
  427. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  428. FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
  429. *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
  430. br->consumed_bits += bits;
  431. return true;
  432. }
  433. else {
  434. *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
  435. br->consumed_bits += bits;
  436. return true;
  437. }
  438. }
  439. }
  440. FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
  441. {
  442. /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
  443. if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
  444. return false;
  445. /* sign-extend: */
  446. *val <<= (32-bits);
  447. *val >>= (32-bits);
  448. return true;
  449. }
  450. FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
  451. {
  452. FLAC__uint32 hi, lo;
  453. if(bits > 32) {
  454. if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
  455. return false;
  456. if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
  457. return false;
  458. *val = hi;
  459. *val <<= 32;
  460. *val |= lo;
  461. }
  462. else {
  463. if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
  464. return false;
  465. *val = lo;
  466. }
  467. return true;
  468. }
  469. FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
  470. {
  471. FLAC__uint32 x8, x32 = 0;
  472. /* this doesn't need to be that fast as currently it is only used for vorbis comments */
  473. if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
  474. return false;
  475. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  476. return false;
  477. x32 |= (x8 << 8);
  478. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  479. return false;
  480. x32 |= (x8 << 16);
  481. if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
  482. return false;
  483. x32 |= (x8 << 24);
  484. *val = x32;
  485. return true;
  486. }
  487. FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
  488. {
  489. /*
  490. * OPT: a faster implementation is possible but probably not that useful
  491. * since this is only called a couple of times in the metadata readers.
  492. */
  493. FLAC__ASSERT(0 != br);
  494. FLAC__ASSERT(0 != br->buffer);
  495. if(bits > 0) {
  496. const unsigned n = br->consumed_bits & 7;
  497. unsigned m;
  498. FLAC__uint32 x;
  499. if(n != 0) {
  500. m = min(8-n, bits);
  501. if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
  502. return false;
  503. bits -= m;
  504. }
  505. m = bits / 8;
  506. if(m > 0) {
  507. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
  508. return false;
  509. bits %= 8;
  510. }
  511. if(bits > 0) {
  512. if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
  513. return false;
  514. }
  515. }
  516. return true;
  517. }
  518. FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
  519. {
  520. FLAC__uint32 x;
  521. FLAC__ASSERT(0 != br);
  522. FLAC__ASSERT(0 != br->buffer);
  523. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
  524. /* step 1: skip over partial head word to get word aligned */
  525. while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
  526. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  527. return false;
  528. nvals--;
  529. }
  530. if(0 == nvals)
  531. return true;
  532. /* step 2: skip whole words in chunks */
  533. while(nvals >= FLAC__BYTES_PER_WORD) {
  534. if(br->consumed_words < br->words) {
  535. br->consumed_words++;
  536. nvals -= FLAC__BYTES_PER_WORD;
  537. }
  538. else if(!bitreader_read_from_client_(br))
  539. return false;
  540. }
  541. /* step 3: skip any remainder from partial tail bytes */
  542. while(nvals) {
  543. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  544. return false;
  545. nvals--;
  546. }
  547. return true;
  548. }
  549. FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
  550. {
  551. FLAC__uint32 x;
  552. FLAC__ASSERT(0 != br);
  553. FLAC__ASSERT(0 != br->buffer);
  554. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
  555. /* step 1: read from partial head word to get word aligned */
  556. while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
  557. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  558. return false;
  559. *val++ = (FLAC__byte)x;
  560. nvals--;
  561. }
  562. if(0 == nvals)
  563. return true;
  564. /* step 2: read whole words in chunks */
  565. while(nvals >= FLAC__BYTES_PER_WORD) {
  566. if(br->consumed_words < br->words) {
  567. const brword word = br->buffer[br->consumed_words++];
  568. #if FLAC__BYTES_PER_WORD == 4
  569. val[0] = (FLAC__byte)(word >> 24);
  570. val[1] = (FLAC__byte)(word >> 16);
  571. val[2] = (FLAC__byte)(word >> 8);
  572. val[3] = (FLAC__byte)word;
  573. #elif FLAC__BYTES_PER_WORD == 8
  574. val[0] = (FLAC__byte)(word >> 56);
  575. val[1] = (FLAC__byte)(word >> 48);
  576. val[2] = (FLAC__byte)(word >> 40);
  577. val[3] = (FLAC__byte)(word >> 32);
  578. val[4] = (FLAC__byte)(word >> 24);
  579. val[5] = (FLAC__byte)(word >> 16);
  580. val[6] = (FLAC__byte)(word >> 8);
  581. val[7] = (FLAC__byte)word;
  582. #else
  583. for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
  584. val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
  585. #endif
  586. val += FLAC__BYTES_PER_WORD;
  587. nvals -= FLAC__BYTES_PER_WORD;
  588. }
  589. else if(!bitreader_read_from_client_(br))
  590. return false;
  591. }
  592. /* step 3: read any remainder from partial tail bytes */
  593. while(nvals) {
  594. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  595. return false;
  596. *val++ = (FLAC__byte)x;
  597. nvals--;
  598. }
  599. return true;
  600. }
  601. FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
  602. #if 0 /* slow but readable version */
  603. {
  604. unsigned bit;
  605. FLAC__ASSERT(0 != br);
  606. FLAC__ASSERT(0 != br->buffer);
  607. *val = 0;
  608. while(1) {
  609. if(!FLAC__bitreader_read_bit(br, &bit))
  610. return false;
  611. if(bit)
  612. break;
  613. else
  614. *val++;
  615. }
  616. return true;
  617. }
  618. #else
  619. {
  620. unsigned i;
  621. FLAC__ASSERT(0 != br);
  622. FLAC__ASSERT(0 != br->buffer);
  623. *val = 0;
  624. while(1) {
  625. while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
  626. brword b = br->buffer[br->consumed_words] << br->consumed_bits;
  627. if(b) {
  628. i = COUNT_ZERO_MSBS(b);
  629. *val += i;
  630. i++;
  631. br->consumed_bits += i;
  632. if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
  633. crc16_update_word_(br, br->buffer[br->consumed_words]);
  634. br->consumed_words++;
  635. br->consumed_bits = 0;
  636. }
  637. return true;
  638. }
  639. else {
  640. *val += FLAC__BITS_PER_WORD - br->consumed_bits;
  641. crc16_update_word_(br, br->buffer[br->consumed_words]);
  642. br->consumed_words++;
  643. br->consumed_bits = 0;
  644. /* didn't find stop bit yet, have to keep going... */
  645. }
  646. }
  647. /* at this point we've eaten up all the whole words; have to try
  648. * reading through any tail bytes before calling the read callback.
  649. * this is a repeat of the above logic adjusted for the fact we
  650. * don't have a whole word. note though if the client is feeding
  651. * us data a byte at a time (unlikely), br->consumed_bits may not
  652. * be zero.
  653. */
  654. if(br->bytes) {
  655. const unsigned end = br->bytes * 8;
  656. brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
  657. if(b) {
  658. i = COUNT_ZERO_MSBS(b);
  659. *val += i;
  660. i++;
  661. br->consumed_bits += i;
  662. FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
  663. return true;
  664. }
  665. else {
  666. *val += end - br->consumed_bits;
  667. br->consumed_bits += end;
  668. FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
  669. /* didn't find stop bit yet, have to keep going... */
  670. }
  671. }
  672. if(!bitreader_read_from_client_(br))
  673. return false;
  674. }
  675. }
  676. #endif
  677. FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
  678. {
  679. FLAC__uint32 lsbs = 0, msbs = 0;
  680. unsigned uval;
  681. FLAC__ASSERT(0 != br);
  682. FLAC__ASSERT(0 != br->buffer);
  683. FLAC__ASSERT(parameter <= 31);
  684. /* read the unary MSBs and end bit */
  685. if(!FLAC__bitreader_read_unary_unsigned(br, (unsigned int*) &msbs))
  686. return false;
  687. /* read the binary LSBs */
  688. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
  689. return false;
  690. /* compose the value */
  691. uval = (msbs << parameter) | lsbs;
  692. if(uval & 1)
  693. *val = -((int)(uval >> 1)) - 1;
  694. else
  695. *val = (int)(uval >> 1);
  696. return true;
  697. }
  698. /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
  699. /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
  700. FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
  701. /* OPT: possibly faster version for use with MSVC */
  702. #ifdef _MSC_VER
  703. {
  704. unsigned i;
  705. unsigned uval = 0;
  706. unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
  707. /* try and get br->consumed_words and br->consumed_bits into register;
  708. * must remember to flush them back to *br before calling other
  709. * bitwriter functions that use them, and before returning */
  710. register unsigned cwords;
  711. register unsigned cbits;
  712. FLAC__ASSERT(0 != br);
  713. FLAC__ASSERT(0 != br->buffer);
  714. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  715. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  716. FLAC__ASSERT(parameter < 32);
  717. /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
  718. if(nvals == 0)
  719. return true;
  720. cbits = br->consumed_bits;
  721. cwords = br->consumed_words;
  722. while(1) {
  723. /* read unary part */
  724. while(1) {
  725. while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  726. brword b = br->buffer[cwords] << cbits;
  727. if(b) {
  728. #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
  729. __asm {
  730. bsr eax, b
  731. not eax
  732. and eax, 31
  733. mov i, eax
  734. }
  735. #else
  736. i = COUNT_ZERO_MSBS(b);
  737. #endif
  738. uval += i;
  739. bits = parameter;
  740. i++;
  741. cbits += i;
  742. if(cbits == FLAC__BITS_PER_WORD) {
  743. crc16_update_word_(br, br->buffer[cwords]);
  744. cwords++;
  745. cbits = 0;
  746. }
  747. goto break1;
  748. }
  749. else {
  750. uval += FLAC__BITS_PER_WORD - cbits;
  751. crc16_update_word_(br, br->buffer[cwords]);
  752. cwords++;
  753. cbits = 0;
  754. /* didn't find stop bit yet, have to keep going... */
  755. }
  756. }
  757. /* at this point we've eaten up all the whole words; have to try
  758. * reading through any tail bytes before calling the read callback.
  759. * this is a repeat of the above logic adjusted for the fact we
  760. * don't have a whole word. note though if the client is feeding
  761. * us data a byte at a time (unlikely), br->consumed_bits may not
  762. * be zero.
  763. */
  764. if(br->bytes) {
  765. const unsigned end = br->bytes * 8;
  766. brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
  767. if(b) {
  768. i = COUNT_ZERO_MSBS(b);
  769. uval += i;
  770. bits = parameter;
  771. i++;
  772. cbits += i;
  773. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  774. goto break1;
  775. }
  776. else {
  777. uval += end - cbits;
  778. cbits += end;
  779. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  780. /* didn't find stop bit yet, have to keep going... */
  781. }
  782. }
  783. /* flush registers and read; bitreader_read_from_client_() does
  784. * not touch br->consumed_bits at all but we still need to set
  785. * it in case it fails and we have to return false.
  786. */
  787. br->consumed_bits = cbits;
  788. br->consumed_words = cwords;
  789. if(!bitreader_read_from_client_(br))
  790. return false;
  791. cwords = br->consumed_words;
  792. }
  793. break1:
  794. /* read binary part */
  795. FLAC__ASSERT(cwords <= br->words);
  796. if(bits) {
  797. while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
  798. /* flush registers and read; bitreader_read_from_client_() does
  799. * not touch br->consumed_bits at all but we still need to set
  800. * it in case it fails and we have to return false.
  801. */
  802. br->consumed_bits = cbits;
  803. br->consumed_words = cwords;
  804. if(!bitreader_read_from_client_(br))
  805. return false;
  806. cwords = br->consumed_words;
  807. }
  808. if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  809. if(cbits) {
  810. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  811. const unsigned n = FLAC__BITS_PER_WORD - cbits;
  812. const brword word = br->buffer[cwords];
  813. if(bits < n) {
  814. uval <<= bits;
  815. uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
  816. cbits += bits;
  817. goto break2;
  818. }
  819. uval <<= n;
  820. uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
  821. bits -= n;
  822. crc16_update_word_(br, word);
  823. cwords++;
  824. cbits = 0;
  825. 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 */
  826. uval <<= bits;
  827. uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
  828. cbits = bits;
  829. }
  830. goto break2;
  831. }
  832. else {
  833. FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
  834. uval <<= bits;
  835. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
  836. cbits = bits;
  837. goto break2;
  838. }
  839. }
  840. else {
  841. /* in this case we're starting our read at a partial tail word;
  842. * the reader has guaranteed that we have at least 'bits' bits
  843. * available to read, which makes this case simpler.
  844. */
  845. uval <<= bits;
  846. if(cbits) {
  847. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  848. FLAC__ASSERT(cbits + bits <= br->bytes*8);
  849. uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
  850. cbits += bits;
  851. goto break2;
  852. }
  853. else {
  854. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
  855. cbits += bits;
  856. goto break2;
  857. }
  858. }
  859. }
  860. break2:
  861. /* compose the value */
  862. *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
  863. /* are we done? */
  864. --nvals;
  865. if(nvals == 0) {
  866. br->consumed_bits = cbits;
  867. br->consumed_words = cwords;
  868. return true;
  869. }
  870. uval = 0;
  871. ++vals;
  872. }
  873. }
  874. #else
  875. {
  876. unsigned i;
  877. unsigned uval = 0;
  878. /* try and get br->consumed_words and br->consumed_bits into register;
  879. * must remember to flush them back to *br before calling other
  880. * bitwriter functions that use them, and before returning */
  881. register unsigned cwords;
  882. register unsigned cbits;
  883. unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
  884. FLAC__ASSERT(0 != br);
  885. FLAC__ASSERT(0 != br->buffer);
  886. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  887. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  888. FLAC__ASSERT(parameter < 32);
  889. /* 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 */
  890. if(nvals == 0)
  891. return true;
  892. cbits = br->consumed_bits;
  893. cwords = br->consumed_words;
  894. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
  895. while(1) {
  896. /* read unary part */
  897. while(1) {
  898. while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  899. brword b = br->buffer[cwords] << cbits;
  900. if(b) {
  901. #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
  902. asm volatile (
  903. "bsrl %1, %0;"
  904. "notl %0;"
  905. "andl $31, %0;"
  906. : "=r"(i)
  907. : "r"(b)
  908. );
  909. #else
  910. i = COUNT_ZERO_MSBS(b);
  911. #endif
  912. uval += i;
  913. cbits += i;
  914. cbits++; /* skip over stop bit */
  915. if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
  916. crc16_update_word_(br, br->buffer[cwords]);
  917. cwords++;
  918. cbits = 0;
  919. }
  920. goto break1;
  921. }
  922. else {
  923. uval += FLAC__BITS_PER_WORD - cbits;
  924. crc16_update_word_(br, br->buffer[cwords]);
  925. cwords++;
  926. cbits = 0;
  927. /* didn't find stop bit yet, have to keep going... */
  928. }
  929. }
  930. /* at this point we've eaten up all the whole words; have to try
  931. * reading through any tail bytes before calling the read callback.
  932. * this is a repeat of the above logic adjusted for the fact we
  933. * don't have a whole word. note though if the client is feeding
  934. * us data a byte at a time (unlikely), br->consumed_bits may not
  935. * be zero.
  936. */
  937. if(br->bytes) {
  938. const unsigned end = br->bytes * 8;
  939. brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
  940. if(b) {
  941. i = COUNT_ZERO_MSBS(b);
  942. uval += i;
  943. cbits += i;
  944. cbits++; /* skip over stop bit */
  945. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  946. goto break1;
  947. }
  948. else {
  949. uval += end - cbits;
  950. cbits += end;
  951. FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
  952. /* didn't find stop bit yet, have to keep going... */
  953. }
  954. }
  955. /* flush registers and read; bitreader_read_from_client_() does
  956. * not touch br->consumed_bits at all but we still need to set
  957. * it in case it fails and we have to return false.
  958. */
  959. br->consumed_bits = cbits;
  960. br->consumed_words = cwords;
  961. if(!bitreader_read_from_client_(br))
  962. return false;
  963. cwords = br->consumed_words;
  964. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
  965. /* + uval to offset our count by the # of unary bits already
  966. * consumed before the read, because we will add these back
  967. * in all at once at break1
  968. */
  969. }
  970. break1:
  971. ucbits -= uval;
  972. ucbits--; /* account for stop bit */
  973. /* read binary part */
  974. FLAC__ASSERT(cwords <= br->words);
  975. if(parameter) {
  976. while(ucbits < parameter) {
  977. /* flush registers and read; bitreader_read_from_client_() does
  978. * not touch br->consumed_bits at all but we still need to set
  979. * it in case it fails and we have to return false.
  980. */
  981. br->consumed_bits = cbits;
  982. br->consumed_words = cwords;
  983. if(!bitreader_read_from_client_(br))
  984. return false;
  985. cwords = br->consumed_words;
  986. ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
  987. }
  988. if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
  989. if(cbits) {
  990. /* this also works when consumed_bits==0, it's just slower than necessary for that case */
  991. const unsigned n = FLAC__BITS_PER_WORD - cbits;
  992. const brword word = br->buffer[cwords];
  993. if(parameter < n) {
  994. uval <<= parameter;
  995. uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
  996. cbits += parameter;
  997. }
  998. else {
  999. uval <<= n;
  1000. uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
  1001. crc16_update_word_(br, word);
  1002. cwords++;
  1003. cbits = parameter - n;
  1004. if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
  1005. uval <<= cbits;
  1006. uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
  1007. }
  1008. }
  1009. }
  1010. else {
  1011. cbits = parameter;
  1012. uval <<= parameter;
  1013. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
  1014. }
  1015. }
  1016. else {
  1017. /* in this case we're starting our read at a partial tail word;
  1018. * the reader has guaranteed that we have at least 'parameter'
  1019. * bits available to read, which makes this case simpler.
  1020. */
  1021. uval <<= parameter;
  1022. if(cbits) {
  1023. /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
  1024. FLAC__ASSERT(cbits + parameter <= br->bytes*8);
  1025. uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
  1026. cbits += parameter;
  1027. }
  1028. else {
  1029. cbits = parameter;
  1030. uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
  1031. }
  1032. }
  1033. }
  1034. ucbits -= parameter;
  1035. /* compose the value */
  1036. *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
  1037. /* are we done? */
  1038. --nvals;
  1039. if(nvals == 0) {
  1040. br->consumed_bits = cbits;
  1041. br->consumed_words = cwords;
  1042. return true;
  1043. }
  1044. uval = 0;
  1045. ++vals;
  1046. }
  1047. }
  1048. #endif
  1049. #if 0 /* UNUSED */
  1050. FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
  1051. {
  1052. FLAC__uint32 lsbs = 0, msbs = 0;
  1053. unsigned bit, uval, k;
  1054. FLAC__ASSERT(0 != br);
  1055. FLAC__ASSERT(0 != br->buffer);
  1056. k = FLAC__bitmath_ilog2(parameter);
  1057. /* read the unary MSBs and end bit */
  1058. if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
  1059. return false;
  1060. /* read the binary LSBs */
  1061. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
  1062. return false;
  1063. if(parameter == 1u<<k) {
  1064. /* compose the value */
  1065. uval = (msbs << k) | lsbs;
  1066. }
  1067. else {
  1068. unsigned d = (1 << (k+1)) - parameter;
  1069. if(lsbs >= d) {
  1070. if(!FLAC__bitreader_read_bit(br, &bit))
  1071. return false;
  1072. lsbs <<= 1;
  1073. lsbs |= bit;
  1074. lsbs -= d;
  1075. }
  1076. /* compose the value */
  1077. uval = msbs * parameter + lsbs;
  1078. }
  1079. /* unfold unsigned to signed */
  1080. if(uval & 1)
  1081. *val = -((int)(uval >> 1)) - 1;
  1082. else
  1083. *val = (int)(uval >> 1);
  1084. return true;
  1085. }
  1086. FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
  1087. {
  1088. FLAC__uint32 lsbs, msbs = 0;
  1089. unsigned bit, k;
  1090. FLAC__ASSERT(0 != br);
  1091. FLAC__ASSERT(0 != br->buffer);
  1092. k = FLAC__bitmath_ilog2(parameter);
  1093. /* read the unary MSBs and end bit */
  1094. if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
  1095. return false;
  1096. /* read the binary LSBs */
  1097. if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
  1098. return false;
  1099. if(parameter == 1u<<k) {
  1100. /* compose the value */
  1101. *val = (msbs << k) | lsbs;
  1102. }
  1103. else {
  1104. unsigned d = (1 << (k+1)) - parameter;
  1105. if(lsbs >= d) {
  1106. if(!FLAC__bitreader_read_bit(br, &bit))
  1107. return false;
  1108. lsbs <<= 1;
  1109. lsbs |= bit;
  1110. lsbs -= d;
  1111. }
  1112. /* compose the value */
  1113. *val = msbs * parameter + lsbs;
  1114. }
  1115. return true;
  1116. }
  1117. #endif /* UNUSED */
  1118. /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
  1119. FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
  1120. {
  1121. FLAC__uint32 v = 0;
  1122. FLAC__uint32 x;
  1123. unsigned i;
  1124. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1125. return false;
  1126. if(raw)
  1127. raw[(*rawlen)++] = (FLAC__byte)x;
  1128. if(!(x & 0x80)) { /* 0xxxxxxx */
  1129. v = x;
  1130. i = 0;
  1131. }
  1132. else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
  1133. v = x & 0x1F;
  1134. i = 1;
  1135. }
  1136. else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
  1137. v = x & 0x0F;
  1138. i = 2;
  1139. }
  1140. else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
  1141. v = x & 0x07;
  1142. i = 3;
  1143. }
  1144. else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
  1145. v = x & 0x03;
  1146. i = 4;
  1147. }
  1148. else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
  1149. v = x & 0x01;
  1150. i = 5;
  1151. }
  1152. else {
  1153. *val = 0xffffffff;
  1154. return true;
  1155. }
  1156. for( ; i; i--) {
  1157. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1158. return false;
  1159. if(raw)
  1160. raw[(*rawlen)++] = (FLAC__byte)x;
  1161. if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
  1162. *val = 0xffffffff;
  1163. return true;
  1164. }
  1165. v <<= 6;
  1166. v |= (x & 0x3F);
  1167. }
  1168. *val = v;
  1169. return true;
  1170. }
  1171. /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
  1172. FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
  1173. {
  1174. FLAC__uint64 v = 0;
  1175. FLAC__uint32 x;
  1176. unsigned i;
  1177. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1178. return false;
  1179. if(raw)
  1180. raw[(*rawlen)++] = (FLAC__byte)x;
  1181. if(!(x & 0x80)) { /* 0xxxxxxx */
  1182. v = x;
  1183. i = 0;
  1184. }
  1185. else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
  1186. v = x & 0x1F;
  1187. i = 1;
  1188. }
  1189. else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
  1190. v = x & 0x0F;
  1191. i = 2;
  1192. }
  1193. else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
  1194. v = x & 0x07;
  1195. i = 3;
  1196. }
  1197. else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
  1198. v = x & 0x03;
  1199. i = 4;
  1200. }
  1201. else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
  1202. v = x & 0x01;
  1203. i = 5;
  1204. }
  1205. else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
  1206. v = 0;
  1207. i = 6;
  1208. }
  1209. else {
  1210. *val = FLAC__U64L(0xffffffffffffffff);
  1211. return true;
  1212. }
  1213. for( ; i; i--) {
  1214. if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
  1215. return false;
  1216. if(raw)
  1217. raw[(*rawlen)++] = (FLAC__byte)x;
  1218. if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
  1219. *val = FLAC__U64L(0xffffffffffffffff);
  1220. return true;
  1221. }
  1222. v <<= 6;
  1223. v |= (x & 0x3F);
  1224. }
  1225. *val = v;
  1226. return true;
  1227. }