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.

881 lines
28KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <stdlib.h> /* for malloc() */
  35. #include <string.h> /* for memcpy(), memset() */
  36. #ifdef _MSC_VER
  37. #include <winsock.h> /* for ntohl() */
  38. #elif defined FLAC__SYS_DARWIN
  39. #include <machine/endian.h> /* for ntohl() */
  40. #elif defined __MINGW32__
  41. #include <winsock.h> /* for ntohl() */
  42. #else
  43. #include <netinet/in.h> /* for ntohl() */
  44. #endif
  45. #if 0 /* UNUSED */
  46. #include "include/private/bitmath.h"
  47. #endif
  48. #include "include/private/bitwriter.h"
  49. #include "include/private/crc.h"
  50. #include "../assert.h"
  51. #include "../alloc.h"
  52. /* Things should be fastest when this matches the machine word size */
  53. /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
  54. /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
  55. typedef FLAC__uint32 bwword;
  56. #define FLAC__BYTES_PER_WORD 4
  57. #define FLAC__BITS_PER_WORD 32
  58. #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
  59. /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
  60. #if WORDS_BIGENDIAN
  61. #define SWAP_BE_WORD_TO_HOST(x) (x)
  62. #else
  63. #ifdef _MSC_VER
  64. #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
  65. #else
  66. #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
  67. #endif
  68. #endif
  69. /*
  70. * The default capacity here doesn't matter too much. The buffer always grows
  71. * to hold whatever is written to it. Usually the encoder will stop adding at
  72. * a frame or metadata block, then write that out and clear the buffer for the
  73. * next one.
  74. */
  75. static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
  76. /* When growing, increment 4K at a time */
  77. static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
  78. #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
  79. #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
  80. #ifdef min
  81. #undef min
  82. #endif
  83. #define min(x,y) ((x)<(y)?(x):(y))
  84. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  85. #ifdef _MSC_VER
  86. #define FLAC__U64L(x) x
  87. #else
  88. #define FLAC__U64L(x) x##LLU
  89. #endif
  90. #ifndef FLaC__INLINE
  91. #define FLaC__INLINE
  92. #endif
  93. struct FLAC__BitWriter {
  94. bwword *buffer;
  95. bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
  96. unsigned capacity; /* capacity of buffer in words */
  97. unsigned words; /* # of complete words in buffer */
  98. unsigned bits; /* # of used bits in accum */
  99. };
  100. /* * WATCHOUT: The current implementation only grows the buffer. */
  101. static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
  102. {
  103. unsigned new_capacity;
  104. bwword *new_buffer;
  105. FLAC__ASSERT(0 != bw);
  106. FLAC__ASSERT(0 != bw->buffer);
  107. /* calculate total words needed to store 'bits_to_add' additional bits */
  108. new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
  109. /* it's possible (due to pessimism in the growth estimation that
  110. * leads to this call) that we don't actually need to grow
  111. */
  112. if(bw->capacity >= new_capacity)
  113. return true;
  114. /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
  115. if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
  116. new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
  117. /* make sure we got everything right */
  118. FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
  119. FLAC__ASSERT(new_capacity > bw->capacity);
  120. FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
  121. new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
  122. if(new_buffer == 0)
  123. return false;
  124. bw->buffer = new_buffer;
  125. bw->capacity = new_capacity;
  126. return true;
  127. }
  128. /***********************************************************************
  129. *
  130. * Class constructor/destructor
  131. *
  132. ***********************************************************************/
  133. FLAC__BitWriter *FLAC__bitwriter_new(void)
  134. {
  135. FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
  136. /* note that calloc() sets all members to 0 for us */
  137. return bw;
  138. }
  139. void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
  140. {
  141. FLAC__ASSERT(0 != bw);
  142. FLAC__bitwriter_free(bw);
  143. free(bw);
  144. }
  145. /***********************************************************************
  146. *
  147. * Public class methods
  148. *
  149. ***********************************************************************/
  150. FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
  151. {
  152. FLAC__ASSERT(0 != bw);
  153. bw->words = bw->bits = 0;
  154. bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
  155. bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
  156. if(bw->buffer == 0)
  157. return false;
  158. return true;
  159. }
  160. void FLAC__bitwriter_free(FLAC__BitWriter *bw)
  161. {
  162. FLAC__ASSERT(0 != bw);
  163. if(0 != bw->buffer)
  164. free(bw->buffer);
  165. bw->buffer = 0;
  166. bw->capacity = 0;
  167. bw->words = bw->bits = 0;
  168. }
  169. void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
  170. {
  171. bw->words = bw->bits = 0;
  172. }
  173. void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
  174. {
  175. unsigned i, j;
  176. if(bw == 0) {
  177. fprintf(out, "bitwriter is NULL\n");
  178. }
  179. else {
  180. fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
  181. for(i = 0; i < bw->words; i++) {
  182. fprintf(out, "%08X: ", i);
  183. for(j = 0; j < FLAC__BITS_PER_WORD; j++)
  184. fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
  185. fprintf(out, "\n");
  186. }
  187. if(bw->bits > 0) {
  188. fprintf(out, "%08X: ", i);
  189. for(j = 0; j < bw->bits; j++)
  190. fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
  191. fprintf(out, "\n");
  192. }
  193. }
  194. }
  195. FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
  196. {
  197. const FLAC__byte *buffer;
  198. size_t bytes;
  199. FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
  200. if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
  201. return false;
  202. *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
  203. FLAC__bitwriter_release_buffer(bw);
  204. return true;
  205. }
  206. FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
  207. {
  208. const FLAC__byte *buffer;
  209. size_t bytes;
  210. FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
  211. if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
  212. return false;
  213. *crc = FLAC__crc8(buffer, bytes);
  214. FLAC__bitwriter_release_buffer(bw);
  215. return true;
  216. }
  217. FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
  218. {
  219. return ((bw->bits & 7) == 0);
  220. }
  221. unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
  222. {
  223. return FLAC__TOTAL_BITS(bw);
  224. }
  225. FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
  226. {
  227. FLAC__ASSERT((bw->bits & 7) == 0);
  228. /* double protection */
  229. if(bw->bits & 7)
  230. return false;
  231. /* if we have bits in the accumulator we have to flush those to the buffer first */
  232. if(bw->bits) {
  233. FLAC__ASSERT(bw->words <= bw->capacity);
  234. if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
  235. return false;
  236. /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
  237. bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
  238. }
  239. /* now we can just return what we have */
  240. *buffer = (FLAC__byte*)bw->buffer;
  241. *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
  242. return true;
  243. }
  244. void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
  245. {
  246. /* nothing to do. in the future, strict checking of a 'writer-is-in-
  247. * get-mode' flag could be added everywhere and then cleared here
  248. */
  249. (void)bw;
  250. }
  251. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
  252. {
  253. unsigned n;
  254. FLAC__ASSERT(0 != bw);
  255. FLAC__ASSERT(0 != bw->buffer);
  256. if(bits == 0)
  257. return true;
  258. /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
  259. if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
  260. return false;
  261. /* first part gets to word alignment */
  262. if(bw->bits) {
  263. n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
  264. bw->accum <<= n;
  265. bits -= n;
  266. bw->bits += n;
  267. if(bw->bits == FLAC__BITS_PER_WORD) {
  268. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  269. bw->bits = 0;
  270. }
  271. else
  272. return true;
  273. }
  274. /* do whole words */
  275. while(bits >= FLAC__BITS_PER_WORD) {
  276. bw->buffer[bw->words++] = 0;
  277. bits -= FLAC__BITS_PER_WORD;
  278. }
  279. /* do any leftovers */
  280. if(bits > 0) {
  281. bw->accum = 0;
  282. bw->bits = bits;
  283. }
  284. return true;
  285. }
  286. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
  287. {
  288. register unsigned left;
  289. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  290. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  291. FLAC__ASSERT(0 != bw);
  292. FLAC__ASSERT(0 != bw->buffer);
  293. FLAC__ASSERT(bits <= 32);
  294. if(bits == 0)
  295. return true;
  296. /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
  297. if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
  298. return false;
  299. left = FLAC__BITS_PER_WORD - bw->bits;
  300. if(bits < left) {
  301. bw->accum <<= bits;
  302. bw->accum |= val;
  303. bw->bits += bits;
  304. }
  305. 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 */
  306. bw->accum <<= left;
  307. bw->accum |= val >> (bw->bits = bits - left);
  308. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  309. bw->accum = val;
  310. }
  311. else {
  312. bw->accum = val;
  313. bw->bits = 0;
  314. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
  315. }
  316. return true;
  317. }
  318. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
  319. {
  320. /* zero-out unused bits */
  321. if(bits < 32)
  322. val &= (~(0xffffffff << bits));
  323. return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
  324. }
  325. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
  326. {
  327. /* this could be a little faster but it's not used for much */
  328. if(bits > 32) {
  329. return
  330. FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
  331. FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
  332. }
  333. else
  334. return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
  335. }
  336. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
  337. {
  338. /* this doesn't need to be that fast as currently it is only used for vorbis comments */
  339. if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
  340. return false;
  341. if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
  342. return false;
  343. if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
  344. return false;
  345. if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
  346. return false;
  347. return true;
  348. }
  349. FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
  350. {
  351. unsigned i;
  352. /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
  353. for(i = 0; i < nvals; i++) {
  354. if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
  355. return false;
  356. }
  357. return true;
  358. }
  359. FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
  360. {
  361. if(val < 32)
  362. return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
  363. else
  364. return
  365. FLAC__bitwriter_write_zeroes(bw, val) &&
  366. FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
  367. }
  368. unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
  369. {
  370. FLAC__uint32 uval;
  371. FLAC__ASSERT(parameter < sizeof(unsigned)*8);
  372. /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
  373. uval = (val<<1) ^ (val>>31);
  374. return 1 + parameter + (uval >> parameter);
  375. }
  376. #if 0 /* UNUSED */
  377. unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
  378. {
  379. unsigned bits, msbs, uval;
  380. unsigned k;
  381. FLAC__ASSERT(parameter > 0);
  382. /* fold signed to unsigned */
  383. if(val < 0)
  384. uval = (unsigned)(((-(++val)) << 1) + 1);
  385. else
  386. uval = (unsigned)(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. unsigned 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. unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
  405. {
  406. unsigned bits, msbs;
  407. unsigned 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. unsigned 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. #endif /* UNUSED */
  427. FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
  428. {
  429. unsigned total_bits, interesting_bits, msbs;
  430. FLAC__uint32 uval, pattern;
  431. FLAC__ASSERT(0 != bw);
  432. FLAC__ASSERT(0 != bw->buffer);
  433. FLAC__ASSERT(parameter < 8*sizeof(uval));
  434. /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
  435. uval = (val<<1) ^ (val>>31);
  436. msbs = uval >> parameter;
  437. interesting_bits = 1 + parameter;
  438. total_bits = interesting_bits + msbs;
  439. pattern = 1 << parameter; /* the unary end bit */
  440. pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
  441. if(total_bits <= 32)
  442. return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
  443. else
  444. return
  445. FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
  446. FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
  447. }
  448. FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
  449. {
  450. const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
  451. const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
  452. FLAC__uint32 uval;
  453. unsigned left;
  454. const unsigned lsbits = 1 + parameter;
  455. unsigned msbits;
  456. FLAC__ASSERT(0 != bw);
  457. FLAC__ASSERT(0 != bw->buffer);
  458. FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
  459. /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
  460. FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
  461. while(nvals) {
  462. /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
  463. uval = (*vals<<1) ^ (*vals>>31);
  464. msbits = uval >> parameter;
  465. #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) */
  466. if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
  467. /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
  468. bw->bits = bw->bits + msbits + lsbits;
  469. uval |= mask1; /* set stop bit */
  470. uval &= mask2; /* mask off unused top bits */
  471. /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
  472. bw->accum <<= msbits;
  473. bw->accum <<= lsbits;
  474. bw->accum |= uval;
  475. if(bw->bits == FLAC__BITS_PER_WORD) {
  476. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  477. bw->bits = 0;
  478. /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
  479. if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
  480. FLAC__ASSERT(bw->capacity == bw->words);
  481. return false;
  482. }
  483. }
  484. }
  485. else {
  486. #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
  487. if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
  488. /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
  489. bw->bits = bw->bits + msbits + lsbits;
  490. uval |= mask1; /* set stop bit */
  491. uval &= mask2; /* mask off unused top bits */
  492. bw->accum <<= msbits + lsbits;
  493. bw->accum |= uval;
  494. }
  495. else {
  496. #endif
  497. /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
  498. /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
  499. if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
  500. return false;
  501. if(msbits) {
  502. /* first part gets to word alignment */
  503. if(bw->bits) {
  504. left = FLAC__BITS_PER_WORD - bw->bits;
  505. if(msbits < left) {
  506. bw->accum <<= msbits;
  507. bw->bits += msbits;
  508. goto break1;
  509. }
  510. else {
  511. bw->accum <<= left;
  512. msbits -= left;
  513. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  514. bw->bits = 0;
  515. }
  516. }
  517. /* do whole words */
  518. while(msbits >= FLAC__BITS_PER_WORD) {
  519. bw->buffer[bw->words++] = 0;
  520. msbits -= FLAC__BITS_PER_WORD;
  521. }
  522. /* do any leftovers */
  523. if(msbits > 0) {
  524. bw->accum = 0;
  525. bw->bits = msbits;
  526. }
  527. }
  528. break1:
  529. uval |= mask1; /* set stop bit */
  530. uval &= mask2; /* mask off unused top bits */
  531. left = FLAC__BITS_PER_WORD - bw->bits;
  532. if(lsbits < left) {
  533. bw->accum <<= lsbits;
  534. bw->accum |= uval;
  535. bw->bits += lsbits;
  536. }
  537. else {
  538. /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
  539. * be > lsbits (because of previous assertions) so it would have
  540. * triggered the (lsbits<left) case above.
  541. */
  542. FLAC__ASSERT(bw->bits);
  543. FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
  544. bw->accum <<= left;
  545. bw->accum |= uval >> (bw->bits = lsbits - left);
  546. bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
  547. bw->accum = uval;
  548. }
  549. #if 1
  550. }
  551. #endif
  552. vals++;
  553. nvals--;
  554. }
  555. return true;
  556. }
  557. #if 0 /* UNUSED */
  558. FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
  559. {
  560. unsigned total_bits, msbs, uval;
  561. unsigned k;
  562. FLAC__ASSERT(0 != bw);
  563. FLAC__ASSERT(0 != bw->buffer);
  564. FLAC__ASSERT(parameter > 0);
  565. /* fold signed to unsigned */
  566. if(val < 0)
  567. uval = (unsigned)(((-(++val)) << 1) + 1);
  568. else
  569. uval = (unsigned)(val << 1);
  570. k = FLAC__bitmath_ilog2(parameter);
  571. if(parameter == 1u<<k) {
  572. unsigned pattern;
  573. FLAC__ASSERT(k <= 30);
  574. msbs = uval >> k;
  575. total_bits = 1 + k + msbs;
  576. pattern = 1 << k; /* the unary end bit */
  577. pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
  578. if(total_bits <= 32) {
  579. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
  580. return false;
  581. }
  582. else {
  583. /* write the unary MSBs */
  584. if(!FLAC__bitwriter_write_zeroes(bw, msbs))
  585. return false;
  586. /* write the unary end bit and binary LSBs */
  587. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
  588. return false;
  589. }
  590. }
  591. else {
  592. unsigned q, r, d;
  593. d = (1 << (k+1)) - parameter;
  594. q = uval / parameter;
  595. r = uval - (q * parameter);
  596. /* write the unary MSBs */
  597. if(!FLAC__bitwriter_write_zeroes(bw, q))
  598. return false;
  599. /* write the unary end bit */
  600. if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
  601. return false;
  602. /* write the binary LSBs */
  603. if(r >= d) {
  604. if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
  605. return false;
  606. }
  607. else {
  608. if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
  615. {
  616. unsigned total_bits, msbs;
  617. unsigned k;
  618. FLAC__ASSERT(0 != bw);
  619. FLAC__ASSERT(0 != bw->buffer);
  620. FLAC__ASSERT(parameter > 0);
  621. k = FLAC__bitmath_ilog2(parameter);
  622. if(parameter == 1u<<k) {
  623. unsigned pattern;
  624. FLAC__ASSERT(k <= 30);
  625. msbs = uval >> k;
  626. total_bits = 1 + k + msbs;
  627. pattern = 1 << k; /* the unary end bit */
  628. pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
  629. if(total_bits <= 32) {
  630. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
  631. return false;
  632. }
  633. else {
  634. /* write the unary MSBs */
  635. if(!FLAC__bitwriter_write_zeroes(bw, msbs))
  636. return false;
  637. /* write the unary end bit and binary LSBs */
  638. if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
  639. return false;
  640. }
  641. }
  642. else {
  643. unsigned q, r, d;
  644. d = (1 << (k+1)) - parameter;
  645. q = uval / parameter;
  646. r = uval - (q * parameter);
  647. /* write the unary MSBs */
  648. if(!FLAC__bitwriter_write_zeroes(bw, q))
  649. return false;
  650. /* write the unary end bit */
  651. if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
  652. return false;
  653. /* write the binary LSBs */
  654. if(r >= d) {
  655. if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
  656. return false;
  657. }
  658. else {
  659. if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
  660. return false;
  661. }
  662. }
  663. return true;
  664. }
  665. #endif /* UNUSED */
  666. FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
  667. {
  668. FLAC__bool ok = 1;
  669. FLAC__ASSERT(0 != bw);
  670. FLAC__ASSERT(0 != bw->buffer);
  671. FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
  672. if(val < 0x80) {
  673. return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
  674. }
  675. else if(val < 0x800) {
  676. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
  677. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
  678. }
  679. else if(val < 0x10000) {
  680. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
  681. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
  682. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
  683. }
  684. else if(val < 0x200000) {
  685. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
  686. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
  687. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
  688. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
  689. }
  690. else if(val < 0x4000000) {
  691. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
  692. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
  693. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
  694. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
  695. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
  696. }
  697. else {
  698. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
  699. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
  700. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
  701. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
  702. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
  703. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
  704. }
  705. return ok;
  706. }
  707. FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
  708. {
  709. FLAC__bool ok = 1;
  710. FLAC__ASSERT(0 != bw);
  711. FLAC__ASSERT(0 != bw->buffer);
  712. FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
  713. if(val < 0x80) {
  714. return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
  715. }
  716. else if(val < 0x800) {
  717. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
  718. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  719. }
  720. else if(val < 0x10000) {
  721. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
  722. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  723. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  724. }
  725. else if(val < 0x200000) {
  726. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
  727. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  728. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  729. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  730. }
  731. else if(val < 0x4000000) {
  732. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
  733. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  734. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  735. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  736. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  737. }
  738. else if(val < 0x80000000) {
  739. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
  740. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
  741. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  742. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  743. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  744. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  745. }
  746. else {
  747. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
  748. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
  749. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
  750. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
  751. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
  752. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
  753. ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
  754. }
  755. return ok;
  756. }
  757. FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
  758. {
  759. /* 0-pad to byte boundary */
  760. if(bw->bits & 7u)
  761. return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
  762. else
  763. return true;
  764. }