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.

1053 lines
33KB

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