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.

956 lines
31KB

  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/bitwriter.h"
  38. #include "include/private/crc.h"
  39. #include "include/private/format.h"
  40. #include "include/private/stream_encoder.h"
  41. #include "../assert.h"
  42. #include "../alloc.h"
  43. #include "../compat.h"
  44. #include "../endswap.h"
  45. /* Things should be fastest when this matches the machine word size */
  46. /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
  47. /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
  48. #if (ENABLE_64_BIT_WORDS == 0)
  49. typedef FLAC__uint32 bwword;
  50. typedef FLAC__uint64 FLAC__bwtemp;
  51. #define FLAC__BYTES_PER_WORD 4 /* sizeof bwword */
  52. #define FLAC__BITS_PER_WORD 32
  53. #define FLAC__TEMP_BITS 64
  54. #define FLAC__HALF_TEMP_BITS 32
  55. /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
  56. #if WORDS_BIGENDIAN
  57. #define SWAP_BE_WORD_TO_HOST(x) (x)
  58. #else
  59. #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
  60. #endif
  61. #else
  62. typedef FLAC__uint64 bwword;
  63. typedef FLAC__uint64 FLAC__bwtemp;
  64. #define FLAC__BYTES_PER_WORD 8 /* sizeof bwword */
  65. #define FLAC__BITS_PER_WORD 64
  66. #define FLAC__TEMP_BITS 64
  67. #define FLAC__HALF_TEMP_BITS 32
  68. /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
  69. #if WORDS_BIGENDIAN
  70. #define SWAP_BE_WORD_TO_HOST(x) (x)
  71. #else
  72. #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x)
  73. #endif
  74. #endif
  75. /*
  76. * The default capacity here doesn't matter too much. The buffer always grows
  77. * to hold whatever is written to it. Usually the encoder will stop adding at
  78. * a frame or metadata block, then write that out and clear the buffer for the
  79. * next one.
  80. */
  81. static const uint32_t FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
  82. /* When growing, increment 4K at a time */
  83. static const uint32_t FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
  84. #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
  85. #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
  86. struct FLAC__BitWriter {
  87. bwword *buffer;
  88. bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
  89. uint32_t capacity; /* capacity of buffer in words */
  90. uint32_t words; /* # of complete words in buffer */
  91. uint32_t bits; /* # of used bits in accum */
  92. };
  93. /* * WATCHOUT: The current implementation only grows the buffer. */
  94. #ifndef __SUNPRO_C
  95. static
  96. #endif
  97. FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
  98. {
  99. uint32_t new_capacity;
  100. bwword *new_buffer;
  101. FLAC__ASSERT(0 != bw);
  102. FLAC__ASSERT(0 != bw->buffer);
  103. /* calculate total words needed to store 'bits_to_add' additional bits */
  104. new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
  105. /* it's possible (due to pessimism in the growth estimation that
  106. * leads to this call) that we don't actually need to grow
  107. */
  108. if(bw->capacity >= new_capacity)
  109. return true;
  110. if(new_capacity * sizeof(bwword) > (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
  111. /* Requested new capacity is larger than the largest possible metadata block,
  112. * which is also larger than the largest sane framesize. That means something
  113. * went very wrong somewhere and previous checks failed.
  114. * To prevent chrashing, give up */
  115. return false;
  116. /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
  117. if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
  118. new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
  119. /* make sure we got everything right */
  120. FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
  121. FLAC__ASSERT(new_capacity > bw->capacity);
  122. FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
  123. new_buffer = (bwword*) safe_realloc_nofree_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
  124. if(new_buffer == 0)
  125. return false;
  126. bw->buffer = new_buffer;
  127. bw->capacity = new_capacity;
  128. return true;
  129. }
  130. /***********************************************************************
  131. *
  132. * Class constructor/destructor
  133. *
  134. ***********************************************************************/
  135. FLAC__BitWriter *FLAC__bitwriter_new(void)
  136. {
  137. FLAC__BitWriter *bw = (FLAC__BitWriter*) calloc(1, sizeof(FLAC__BitWriter));
  138. /* note that calloc() sets all members to 0 for us */
  139. return bw;
  140. }
  141. void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
  142. {
  143. FLAC__ASSERT(0 != bw);
  144. FLAC__bitwriter_free(bw);
  145. free(bw);
  146. }
  147. /***********************************************************************
  148. *
  149. * Public class methods
  150. *
  151. ***********************************************************************/
  152. FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
  153. {
  154. FLAC__ASSERT(0 != bw);
  155. bw->words = bw->bits = 0;
  156. bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
  157. bw->buffer = (bwword*) malloc(sizeof(bwword) * bw->capacity);
  158. if(bw->buffer == 0)
  159. return false;
  160. return true;
  161. }
  162. void FLAC__bitwriter_free(FLAC__BitWriter *bw)
  163. {
  164. FLAC__ASSERT(0 != bw);
  165. if(0 != bw->buffer)
  166. free(bw->buffer);
  167. bw->buffer = 0;
  168. bw->capacity = 0;
  169. bw->words = bw->bits = 0;
  170. }
  171. void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
  172. {
  173. bw->words = bw->bits = 0;
  174. }
  175. FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
  176. {
  177. const FLAC__byte *buffer;
  178. size_t bytes;
  179. FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
  180. if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
  181. return false;
  182. *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
  183. FLAC__bitwriter_release_buffer(bw);
  184. return true;
  185. }
  186. FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
  187. {
  188. const FLAC__byte *buffer;
  189. size_t bytes;
  190. FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
  191. if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
  192. return false;
  193. *crc = FLAC__crc8(buffer, bytes);
  194. FLAC__bitwriter_release_buffer(bw);
  195. return true;
  196. }
  197. FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
  198. {
  199. return ((bw->bits & 7) == 0);
  200. }
  201. uint32_t FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
  202. {
  203. return FLAC__TOTAL_BITS(bw);
  204. }
  205. FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
  206. {
  207. FLAC__ASSERT((bw->bits & 7) == 0);
  208. /* double protection */
  209. if(bw->bits & 7)
  210. return false;
  211. /* if we have bits in the accumulator we have to flush those to the buffer first */
  212. if(bw->bits) {
  213. FLAC__ASSERT(bw->words <= bw->capacity);
  214. if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
  215. return false;
  216. /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
  217. bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
  218. }
  219. /* now we can just return what we have */
  220. *buffer = (FLAC__byte*)bw->buffer;
  221. *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
  222. return true;
  223. }
  224. void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
  225. {
  226. /* nothing to do. in the future, strict checking of a 'writer-is-in-
  227. * get-mode' flag could be added everywhere and then cleared here
  228. */
  229. (void)bw;
  230. }
  231. inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits)
  232. {
  233. uint32_t n;
  234. FLAC__ASSERT(0 != bw);
  235. FLAC__ASSERT(0 != bw->buffer);
  236. if(bits == 0)
  237. return true;
  238. /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
  239. if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
  240. return false;
  241. /* first part gets to word alignment */
  242. if(bw->bits) {
  243. n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
  244. bw->accum <<= n;
  245. bits -= n;
  246. bw->bits += n;
  247. if(bw->bits == FLAC__BITS_PER_WORD) {
  248. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  249. bw->bits = 0;
  250. }
  251. else
  252. return true;
  253. }
  254. /* do whole words */
  255. while(bits >= FLAC__BITS_PER_WORD) {
  256. bw->buffer[bw->words++] = 0;
  257. bits -= FLAC__BITS_PER_WORD;
  258. }
  259. /* do any leftovers */
  260. if(bits > 0) {
  261. bw->accum = 0;
  262. bw->bits = bits;
  263. }
  264. return true;
  265. }
  266. static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
  267. {
  268. uint32_t left;
  269. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  270. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  271. if(bw == 0 || bw->buffer == 0)
  272. return false;
  273. if (bits > 32)
  274. return false;
  275. if(bits == 0)
  276. return true;
  277. FLAC__ASSERT((bits == 32) || (val>>bits == 0));
  278. /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
  279. if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
  280. return false;
  281. left = FLAC__BITS_PER_WORD - bw->bits;
  282. if(bits < left) {
  283. bw->accum <<= bits;
  284. bw->accum |= val;
  285. bw->bits += bits;
  286. }
  287. else if(bw->bits) { /* WATCHOUT: if bw->bits == 0, left==FLAC__BITS_PER_WORD and bw->accum<<=left is a NOP instead of setting to 0 */
  288. bw->accum <<= left;
  289. bw->accum |= val >> (bw->bits = bits - left);
  290. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  291. bw->accum = val; /* unused top bits can contain garbage */
  292. }
  293. else { /* at this point bits == FLAC__BITS_PER_WORD == 32 and bw->bits == 0 */
  294. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val);
  295. }
  296. return true;
  297. }
  298. inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
  299. {
  300. /* check that unused bits are unset */
  301. if((bits < 32) && (val>>bits != 0))
  302. return false;
  303. return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
  304. }
  305. inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits)
  306. {
  307. /* zero-out unused bits */
  308. if(bits < 32)
  309. val &= (~(0xffffffff << bits));
  310. return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
  311. }
  312. inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits)
  313. {
  314. /* this could be a little faster but it's not used for much */
  315. if(bits > 32) {
  316. return
  317. FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
  318. FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32);
  319. }
  320. else
  321. return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
  322. }
  323. inline FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits)
  324. {
  325. FLAC__uint64 uval = val;
  326. /* zero-out unused bits */
  327. if(bits < 64)
  328. uval &= (~(UINT64_MAX << bits));
  329. return FLAC__bitwriter_write_raw_uint64(bw, uval, bits);
  330. }
  331. inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
  332. {
  333. /* this doesn't need to be that fast as currently it is only used for vorbis comments */
  334. if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
  335. return false;
  336. if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
  337. return false;
  338. if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
  339. return false;
  340. if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
  341. return false;
  342. return true;
  343. }
  344. inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals)
  345. {
  346. uint32_t i;
  347. /* grow capacity upfront to prevent constant reallocation during writes */
  348. if(bw->capacity <= bw->words + nvals / (FLAC__BITS_PER_WORD / 8) + 1 && !bitwriter_grow_(bw, nvals * 8))
  349. return false;
  350. /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
  351. for(i = 0; i < nvals; i++) {
  352. if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8))
  353. return false;
  354. }
  355. return true;
  356. }
  357. FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, uint32_t val)
  358. {
  359. if(val < 32)
  360. return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
  361. else
  362. return
  363. FLAC__bitwriter_write_zeroes(bw, val) &&
  364. FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
  365. }
  366. #if 0 /* UNUSED */
  367. uint32_t FLAC__bitwriter_rice_bits(FLAC__int32 val, uint32_t parameter)
  368. {
  369. FLAC__uint32 uval;
  370. FLAC__ASSERT(parameter < 32);
  371. /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
  372. uval = val;
  373. uval <<= 1;
  374. uval ^= (val>>31);
  375. return 1 + parameter + (uval >> parameter);
  376. }
  377. uint32_t FLAC__bitwriter_golomb_bits_signed(int val, uint32_t parameter)
  378. {
  379. uint32_t bits, msbs, uval;
  380. uint32_t k;
  381. FLAC__ASSERT(parameter > 0);
  382. /* fold signed to uint32_t */
  383. if(val < 0)
  384. uval = (uint32_t)(((-(++val)) << 1) + 1);
  385. else
  386. uval = (uint32_t)(val << 1);
  387. k = FLAC__bitmath_ilog2(parameter);
  388. if(parameter == 1u<<k) {
  389. FLAC__ASSERT(k <= 30);
  390. msbs = uval >> k;
  391. bits = 1 + k + msbs;
  392. }
  393. else {
  394. uint32_t q, r, d;
  395. d = (1 << (k+1)) - parameter;
  396. q = uval / parameter;
  397. r = uval - (q * parameter);
  398. bits = 1 + q + k;
  399. if(r >= d)
  400. bits++;
  401. }
  402. return bits;
  403. }
  404. uint32_t FLAC__bitwriter_golomb_bits_unsigned(uint32_t uval, uint32_t parameter)
  405. {
  406. uint32_t bits, msbs;
  407. uint32_t k;
  408. FLAC__ASSERT(parameter > 0);
  409. k = FLAC__bitmath_ilog2(parameter);
  410. if(parameter == 1u<<k) {
  411. FLAC__ASSERT(k <= 30);
  412. msbs = uval >> k;
  413. bits = 1 + k + msbs;
  414. }
  415. else {
  416. uint32_t q, r, d;
  417. d = (1 << (k+1)) - parameter;
  418. q = uval / parameter;
  419. r = uval - (q * parameter);
  420. bits = 1 + q + k;
  421. if(r >= d)
  422. bits++;
  423. }
  424. return bits;
  425. }
  426. FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t parameter)
  427. {
  428. uint32_t total_bits, interesting_bits, msbs;
  429. FLAC__uint32 uval, pattern;
  430. FLAC__ASSERT(0 != bw);
  431. FLAC__ASSERT(0 != bw->buffer);
  432. FLAC__ASSERT(parameter < 32);
  433. /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
  434. uval = val;
  435. uval <<= 1;
  436. uval ^= (val>>31);
  437. msbs = uval >> parameter;
  438. interesting_bits = 1 + parameter;
  439. total_bits = interesting_bits + msbs;
  440. pattern = 1 << parameter; /* the unary end bit */
  441. pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
  442. if(total_bits <= 32)
  443. return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
  444. else
  445. return
  446. FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
  447. FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
  448. }
  449. #endif /* UNUSED */
  450. #if (ENABLE_64_BIT_WORDS == 0)
  451. #define WIDE_ACCUM_TO_BW { \
  452. bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS; \
  453. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \
  454. wide_accum <<= FLAC__HALF_TEMP_BITS; \
  455. bitpointer += FLAC__HALF_TEMP_BITS; \
  456. }
  457. #else
  458. #define WIDE_ACCUM_TO_BW { \
  459. FLAC__ASSERT(bw->bits % FLAC__HALF_TEMP_BITS == 0); \
  460. if(bw->bits == 0) { \
  461. bw->accum = wide_accum >> FLAC__HALF_TEMP_BITS; \
  462. wide_accum <<= FLAC__HALF_TEMP_BITS; \
  463. bw->bits = FLAC__HALF_TEMP_BITS; \
  464. } \
  465. else { \
  466. bw->accum <<= FLAC__HALF_TEMP_BITS; \
  467. bw->accum += wide_accum >> FLAC__HALF_TEMP_BITS; \
  468. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum); \
  469. wide_accum <<= FLAC__HALF_TEMP_BITS; \
  470. bw->bits = 0; \
  471. } \
  472. bitpointer += FLAC__HALF_TEMP_BITS; \
  473. }
  474. #endif
  475. FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, uint32_t nvals, uint32_t parameter)
  476. {
  477. const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */
  478. const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */
  479. FLAC__uint32 uval;
  480. const uint32_t lsbits = 1 + parameter;
  481. uint32_t msbits, total_bits;
  482. FLAC__bwtemp wide_accum = 0;
  483. FLAC__uint32 bitpointer = FLAC__TEMP_BITS;
  484. FLAC__ASSERT(0 != bw);
  485. FLAC__ASSERT(0 != bw->buffer);
  486. FLAC__ASSERT(parameter < 31);
  487. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  488. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  489. #if (ENABLE_64_BIT_WORDS == 0)
  490. if(bw->bits > 0) {
  491. bitpointer -= bw->bits;
  492. wide_accum = (FLAC__bwtemp)(bw->accum) << bitpointer;
  493. bw->bits = 0;
  494. }
  495. #else
  496. if(bw->bits > 0 && bw->bits < FLAC__HALF_TEMP_BITS) {
  497. bitpointer -= bw->bits;
  498. wide_accum = bw->accum << bitpointer;
  499. bw->bits = 0;
  500. }
  501. else if(bw->bits > FLAC__HALF_TEMP_BITS) {
  502. bitpointer -= (bw->bits - FLAC__HALF_TEMP_BITS);
  503. wide_accum = bw->accum << bitpointer;
  504. bw->accum >>= (bw->bits - FLAC__HALF_TEMP_BITS);
  505. bw->bits = FLAC__HALF_TEMP_BITS;
  506. }
  507. #endif
  508. /* Reserve one FLAC__TEMP_BITS per symbol, so checks for space are only necessary when very large symbols are encountered
  509. * this might be considered wasteful, but is only at most 8kB more than necessary for a blocksize of 4096 */
  510. if(bw->capacity * FLAC__BITS_PER_WORD <= bw->words * FLAC__BITS_PER_WORD + nvals * FLAC__TEMP_BITS + bw->bits && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS))
  511. return false;
  512. while(nvals) {
  513. /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
  514. uval = *vals;
  515. uval <<= 1;
  516. uval ^= (*vals>>31);
  517. msbits = uval >> parameter;
  518. total_bits = lsbits + msbits;
  519. uval |= mask1; /* set stop bit */
  520. uval &= mask2; /* mask off unused top bits */
  521. if(total_bits <= bitpointer) {
  522. /* There is room enough to store the symbol whole at once */
  523. wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - total_bits);
  524. bitpointer -= total_bits;
  525. if(bitpointer <= FLAC__HALF_TEMP_BITS) {
  526. /* A word is finished, copy the upper 32 bits of the wide_accum */
  527. WIDE_ACCUM_TO_BW
  528. }
  529. }
  530. else {
  531. /* The symbol needs to be split. This code isn't used often */
  532. /* First check for space in the bitwriter */
  533. if(total_bits > FLAC__TEMP_BITS) {
  534. FLAC__uint32 oversize_in_bits = total_bits - FLAC__TEMP_BITS;
  535. FLAC__uint32 capacity_needed = bw->words * FLAC__BITS_PER_WORD + bw->bits + nvals * FLAC__TEMP_BITS + oversize_in_bits;
  536. if(bw->capacity * FLAC__BITS_PER_WORD <= capacity_needed && !bitwriter_grow_(bw, nvals * FLAC__TEMP_BITS + oversize_in_bits))
  537. return false;
  538. }
  539. if(msbits > bitpointer) {
  540. /* We have a lot of 0 bits to write, first align with bitwriter word */
  541. msbits -= bitpointer - FLAC__HALF_TEMP_BITS;
  542. bitpointer = FLAC__HALF_TEMP_BITS;
  543. WIDE_ACCUM_TO_BW
  544. while(msbits > bitpointer) {
  545. /* As the accumulator is already zero, we only need to
  546. * assign zeroes to the bitbuffer */
  547. WIDE_ACCUM_TO_BW
  548. bitpointer -= FLAC__HALF_TEMP_BITS;
  549. msbits -= FLAC__HALF_TEMP_BITS;
  550. }
  551. /* The remaining bits are zero, and the accumulator already is zero,
  552. * so just subtract the number of bits from bitpointer. When storing,
  553. * we can also just store 0 */
  554. bitpointer -= msbits;
  555. if(bitpointer <= FLAC__HALF_TEMP_BITS)
  556. WIDE_ACCUM_TO_BW
  557. }
  558. else {
  559. bitpointer -= msbits;
  560. if(bitpointer <= FLAC__HALF_TEMP_BITS)
  561. WIDE_ACCUM_TO_BW
  562. }
  563. /* The lsbs + stop bit always fit 32 bit, so this code mirrors the code above */
  564. wide_accum |= (FLAC__bwtemp)(uval) << (bitpointer - lsbits);
  565. bitpointer -= lsbits;
  566. if(bitpointer <= FLAC__HALF_TEMP_BITS) {
  567. /* A word is finished, copy the upper 32 bits of the wide_accum */
  568. WIDE_ACCUM_TO_BW
  569. }
  570. }
  571. vals++;
  572. nvals--;
  573. }
  574. /* Now fixup remainder of wide_accum */
  575. #if (ENABLE_64_BIT_WORDS == 0)
  576. if(bitpointer < FLAC__TEMP_BITS) {
  577. bw->accum = wide_accum >> bitpointer;
  578. bw->bits = FLAC__TEMP_BITS - bitpointer;
  579. }
  580. #else
  581. if(bitpointer < FLAC__TEMP_BITS) {
  582. if(bw->bits == 0) {
  583. bw->accum = wide_accum >> bitpointer;
  584. bw->bits = FLAC__TEMP_BITS - bitpointer;
  585. }
  586. else if (bw->bits == FLAC__HALF_TEMP_BITS) {
  587. bw->accum <<= FLAC__TEMP_BITS - bitpointer;
  588. bw->accum |= (wide_accum >> bitpointer);
  589. bw->bits = FLAC__HALF_TEMP_BITS + FLAC__TEMP_BITS - bitpointer;
  590. }
  591. else {
  592. FLAC__ASSERT(0);
  593. }
  594. }
  595. #endif
  596. return true;
  597. }
  598. #if 0 /* UNUSED */
  599. FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uint32_t parameter)
  600. {
  601. uint32_t total_bits, msbs, uval;
  602. uint32_t k;
  603. FLAC__ASSERT(0 != bw);
  604. FLAC__ASSERT(0 != bw->buffer);
  605. FLAC__ASSERT(parameter > 0);
  606. /* fold signed to uint32_t */
  607. if(val < 0)
  608. uval = (uint32_t)(((-(++val)) << 1) + 1);
  609. else
  610. uval = (uint32_t)(val << 1);
  611. k = FLAC__bitmath_ilog2(parameter);
  612. if(parameter == 1u<<k) {
  613. uint32_t pattern;
  614. FLAC__ASSERT(k <= 30);
  615. msbs = uval >> k;
  616. total_bits = 1 + k + msbs;
  617. pattern = 1 << k; /* the unary end bit */
  618. pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
  619. if(total_bits <= 32) {
  620. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
  621. return false;
  622. }
  623. else {
  624. /* write the unary MSBs */
  625. if(!FLAC__bitwriter_write_zeroes(bw, msbs))
  626. return false;
  627. /* write the unary end bit and binary LSBs */
  628. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
  629. return false;
  630. }
  631. }
  632. else {
  633. uint32_t q, r, d;
  634. d = (1 << (k+1)) - parameter;
  635. q = uval / parameter;
  636. r = uval - (q * parameter);
  637. /* write the unary MSBs */
  638. if(!FLAC__bitwriter_write_zeroes(bw, q))
  639. return false;
  640. /* write the unary end bit */
  641. if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
  642. return false;
  643. /* write the binary LSBs */
  644. if(r >= d) {
  645. if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
  646. return false;
  647. }
  648. else {
  649. if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
  650. return false;
  651. }
  652. }
  653. return true;
  654. }
  655. FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, uint32_t uval, uint32_t parameter)
  656. {
  657. uint32_t total_bits, msbs;
  658. uint32_t k;
  659. FLAC__ASSERT(0 != bw);
  660. FLAC__ASSERT(0 != bw->buffer);
  661. FLAC__ASSERT(parameter > 0);
  662. k = FLAC__bitmath_ilog2(parameter);
  663. if(parameter == 1u<<k) {
  664. uint32_t pattern;
  665. FLAC__ASSERT(k <= 30);
  666. msbs = uval >> k;
  667. total_bits = 1 + k + msbs;
  668. pattern = 1 << k; /* the unary end bit */
  669. pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
  670. if(total_bits <= 32) {
  671. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
  672. return false;
  673. }
  674. else {
  675. /* write the unary MSBs */
  676. if(!FLAC__bitwriter_write_zeroes(bw, msbs))
  677. return false;
  678. /* write the unary end bit and binary LSBs */
  679. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
  680. return false;
  681. }
  682. }
  683. else {
  684. uint32_t q, r, d;
  685. d = (1 << (k+1)) - parameter;
  686. q = uval / parameter;
  687. r = uval - (q * parameter);
  688. /* write the unary MSBs */
  689. if(!FLAC__bitwriter_write_zeroes(bw, q))
  690. return false;
  691. /* write the unary end bit */
  692. if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
  693. return false;
  694. /* write the binary LSBs */
  695. if(r >= d) {
  696. if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
  697. return false;
  698. }
  699. else {
  700. if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
  701. return false;
  702. }
  703. }
  704. return true;
  705. }
  706. #endif /* UNUSED */
  707. FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
  708. {
  709. FLAC__bool ok = 1;
  710. FLAC__ASSERT(0 != bw);
  711. FLAC__ASSERT(0 != bw->buffer);
  712. if((val & 0x80000000) != 0) /* this version only handles 31 bits */
  713. return false;
  714. if(val < 0x80) {
  715. return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
  716. }
  717. else if(val < 0x800) {
  718. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8);
  719. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
  720. }
  721. else if(val < 0x10000) {
  722. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8);
  723. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
  724. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
  725. }
  726. else if(val < 0x200000) {
  727. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8);
  728. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
  729. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
  730. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
  731. }
  732. else if(val < 0x4000000) {
  733. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8);
  734. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
  735. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
  736. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
  737. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
  738. }
  739. else {
  740. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8);
  741. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8);
  742. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
  743. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
  744. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
  745. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
  746. }
  747. return ok;
  748. }
  749. FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
  750. {
  751. FLAC__bool ok = 1;
  752. FLAC__ASSERT(0 != bw);
  753. FLAC__ASSERT(0 != bw->buffer);
  754. if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
  755. return false;
  756. if(val < 0x80) {
  757. return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
  758. }
  759. else if(val < 0x800) {
  760. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
  761. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  762. }
  763. else if(val < 0x10000) {
  764. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
  765. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  766. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  767. }
  768. else if(val < 0x200000) {
  769. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
  770. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  771. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  772. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  773. }
  774. else if(val < 0x4000000) {
  775. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
  776. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  777. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  778. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  779. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  780. }
  781. else if(val < 0x80000000) {
  782. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
  783. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
  784. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  785. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  786. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  787. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  788. }
  789. else {
  790. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8);
  791. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
  792. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
  793. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  794. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  795. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  796. ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  797. }
  798. return ok;
  799. }
  800. FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
  801. {
  802. /* 0-pad to byte boundary */
  803. if(bw->bits & 7u)
  804. return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
  805. else
  806. return true;
  807. }
  808. /* These functions are declared inline in this file but are also callable as
  809. * externs from elsewhere.
  810. * According to the C99 spec, section 6.7.4, simply providing a function
  811. * prototype in a header file without 'inline' and making the function inline
  812. * in this file should be sufficient.
  813. * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
  814. * fix that we add extern declarations here.
  815. */
  816. extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits);
  817. extern FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits);
  818. extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits);
  819. extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits);
  820. extern FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits);
  821. extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
  822. extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals);