flac_bitwriter.c 27 KB

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