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.

869 lines
28KB

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