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.

843 lines
27KB

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