1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include "private/bitwriter.h"
39 #include "private/crc.h"
40 #include "private/macros.h"
41 #include "FLAC/assert.h"
42 #include "share/alloc.h"
43 #include "share/compat.h"
44 #include "share/endswap.h"
46 /* Things should be fastest when this matches the machine word size */
47 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
48 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
49 #define FLAC__BYTES_PER_WORD 4
50 #define FLAC__BITS_PER_WORD 32
51 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
52 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
54 #define SWAP_BE_WORD_TO_HOST(x) (x)
56 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
60 * The default capacity here doesn't matter too much. The buffer always grows
61 * to hold whatever is written to it. Usually the encoder will stop adding at
62 * a frame or metadata block, then write that out and clear the buffer for the
65 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_t); /* size in words */
66 /* When growing, increment 4K at a time */
67 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_t); /* size in words */
69 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
70 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
72 struct FLAC__BitWriter {
74 uint32_t accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
75 unsigned capacity; /* capacity of buffer in words */
76 unsigned words; /* # of complete words in buffer */
77 unsigned bits; /* # of used bits in accum */
80 /* * WATCHOUT: The current implementation only grows the buffer. */
81 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
83 unsigned new_capacity;
86 FLAC__ASSERT(0 != bw);
87 FLAC__ASSERT(0 != bw->buffer);
89 /* calculate total words needed to store 'bits_to_add' additional bits */
90 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
92 /* it's possible (due to pessimism in the growth estimation that
93 * leads to this call) that we don't actually need to grow
95 if(bw->capacity >= new_capacity)
98 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
99 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
100 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
101 /* make sure we got everything right */
102 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
103 FLAC__ASSERT(new_capacity > bw->capacity);
104 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
106 new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times*/new_capacity);
109 bw->buffer = new_buffer;
110 bw->capacity = new_capacity;
115 /***********************************************************************
117 * Class constructor/destructor
119 ***********************************************************************/
121 FLAC__BitWriter *FLAC__bitwriter_new(void)
123 FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
124 /* note that calloc() sets all members to 0 for us */
128 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
130 FLAC__ASSERT(0 != bw);
132 FLAC__bitwriter_free(bw);
136 /***********************************************************************
138 * Public class methods
140 ***********************************************************************/
142 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
144 FLAC__ASSERT(0 != bw);
146 bw->words = bw->bits = 0;
147 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
148 bw->buffer = malloc(sizeof(uint32_t) * bw->capacity);
155 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
157 FLAC__ASSERT(0 != bw);
163 bw->words = bw->bits = 0;
166 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
168 bw->words = bw->bits = 0;
171 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
175 fprintf(out, "bitwriter is NULL\n");
178 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
180 for(i = 0; i < bw->words; i++) {
181 fprintf(out, "%08X: ", i);
182 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
183 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
187 fprintf(out, "%08X: ", i);
188 for(j = 0; j < bw->bits; j++)
189 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
195 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
197 const FLAC__byte *buffer;
200 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
202 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
205 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
206 FLAC__bitwriter_release_buffer(bw);
210 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
212 const FLAC__byte *buffer;
215 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
217 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
220 *crc = FLAC__crc8(buffer, bytes);
221 FLAC__bitwriter_release_buffer(bw);
225 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
227 return ((bw->bits & 7) == 0);
230 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
232 return FLAC__TOTAL_BITS(bw);
235 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
237 FLAC__ASSERT((bw->bits & 7) == 0);
238 /* double protection */
241 /* if we have bits in the accumulator we have to flush those to the buffer first */
243 FLAC__ASSERT(bw->words <= bw->capacity);
244 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
246 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
247 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
249 /* now we can just return what we have */
250 *buffer = (FLAC__byte*)bw->buffer;
251 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
255 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
257 /* nothing to do. in the future, strict checking of a 'writer-is-in-
258 * get-mode' flag could be added everywhere and then cleared here
263 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
267 FLAC__ASSERT(0 != bw);
268 FLAC__ASSERT(0 != bw->buffer);
272 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
273 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
275 /* first part gets to word alignment */
277 n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
281 if(bw->bits == FLAC__BITS_PER_WORD) {
282 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
289 while(bits >= FLAC__BITS_PER_WORD) {
290 bw->buffer[bw->words++] = 0;
291 bits -= FLAC__BITS_PER_WORD;
293 /* do any leftovers */
301 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
303 register unsigned left;
305 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
306 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
308 FLAC__ASSERT(0 != bw);
309 FLAC__ASSERT(0 != bw->buffer);
311 FLAC__ASSERT(bits <= 32);
315 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
316 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
319 left = FLAC__BITS_PER_WORD - bw->bits;
325 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 */
327 bw->accum |= val >> (bw->bits = bits - left);
328 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
334 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
340 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
342 /* zero-out unused bits */
344 val &= (~(0xffffffff << bits));
346 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
349 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
351 /* this could be a little faster but it's not used for much */
354 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
355 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
358 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
361 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
363 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
365 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
367 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
369 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
371 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
377 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
381 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
382 for(i = 0; i < nvals; i++) {
383 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
390 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
393 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
396 FLAC__bitwriter_write_zeroes(bw, val) &&
397 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
400 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
404 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
406 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
407 uval = (val<<1) ^ (val>>31);
409 return 1 + parameter + (uval >> parameter);
413 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
415 unsigned bits, msbs, uval;
418 FLAC__ASSERT(parameter > 0);
420 /* fold signed to unsigned */
422 uval = (unsigned)(((-(++val)) << 1) + 1);
424 uval = (unsigned)(val << 1);
426 k = FLAC__bitmath_ilog2(parameter);
427 if(parameter == 1u<<k) {
428 FLAC__ASSERT(k <= 30);
436 d = (1 << (k+1)) - parameter;
437 q = uval / parameter;
438 r = uval - (q * parameter);
447 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
452 FLAC__ASSERT(parameter > 0);
454 k = FLAC__bitmath_ilog2(parameter);
455 if(parameter == 1u<<k) {
456 FLAC__ASSERT(k <= 30);
464 d = (1 << (k+1)) - parameter;
465 q = uval / parameter;
466 r = uval - (q * parameter);
476 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
478 unsigned total_bits, interesting_bits, msbs;
479 FLAC__uint32 uval, pattern;
481 FLAC__ASSERT(0 != bw);
482 FLAC__ASSERT(0 != bw->buffer);
483 FLAC__ASSERT(parameter < 8*sizeof(uval));
485 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
486 uval = (val<<1) ^ (val>>31);
488 msbs = uval >> parameter;
489 interesting_bits = 1 + parameter;
490 total_bits = interesting_bits + msbs;
491 pattern = 1 << parameter; /* the unary end bit */
492 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
495 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
498 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
499 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
502 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
504 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
505 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
508 const unsigned lsbits = 1 + parameter;
511 FLAC__ASSERT(0 != bw);
512 FLAC__ASSERT(0 != bw->buffer);
513 FLAC__ASSERT(parameter < 8*sizeof(uint32_t)-1);
514 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
515 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
518 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
519 uval = (*vals<<1) ^ (*vals>>31);
521 msbits = uval >> parameter;
523 #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) */
524 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
525 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
526 bw->bits = bw->bits + msbits + lsbits;
527 uval |= mask1; /* set stop bit */
528 uval &= mask2; /* mask off unused top bits */
529 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
530 bw->accum <<= msbits;
531 bw->accum <<= lsbits;
533 if(bw->bits == FLAC__BITS_PER_WORD) {
534 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
536 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
537 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
538 FLAC__ASSERT(bw->capacity == bw->words);
544 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
545 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
546 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
547 bw->bits = bw->bits + msbits + lsbits;
548 uval |= mask1; /* set stop bit */
549 uval &= mask2; /* mask off unused top bits */
550 bw->accum <<= msbits + lsbits;
555 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
556 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
557 if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 uint32_t*/ && !bitwriter_grow_(bw, msbits+lsbits))
561 /* first part gets to word alignment */
563 left = FLAC__BITS_PER_WORD - bw->bits;
565 bw->accum <<= msbits;
572 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
577 while(msbits >= FLAC__BITS_PER_WORD) {
578 bw->buffer[bw->words++] = 0;
579 msbits -= FLAC__BITS_PER_WORD;
581 /* do any leftovers */
588 uval |= mask1; /* set stop bit */
589 uval &= mask2; /* mask off unused top bits */
591 left = FLAC__BITS_PER_WORD - bw->bits;
593 bw->accum <<= lsbits;
598 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
599 * be > lsbits (because of previous assertions) so it would have
600 * triggered the (lsbits<left) case above.
602 FLAC__ASSERT(bw->bits);
603 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
605 bw->accum |= uval >> (bw->bits = lsbits - left);
606 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
619 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
621 unsigned total_bits, msbs, uval;
624 FLAC__ASSERT(0 != bw);
625 FLAC__ASSERT(0 != bw->buffer);
626 FLAC__ASSERT(parameter > 0);
628 /* fold signed to unsigned */
630 uval = (unsigned)(((-(++val)) << 1) + 1);
632 uval = (unsigned)(val << 1);
634 k = FLAC__bitmath_ilog2(parameter);
635 if(parameter == 1u<<k) {
638 FLAC__ASSERT(k <= 30);
641 total_bits = 1 + k + msbs;
642 pattern = 1 << k; /* the unary end bit */
643 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
645 if(total_bits <= 32) {
646 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
650 /* write the unary MSBs */
651 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
653 /* write the unary end bit and binary LSBs */
654 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
661 d = (1 << (k+1)) - parameter;
662 q = uval / parameter;
663 r = uval - (q * parameter);
664 /* write the unary MSBs */
665 if(!FLAC__bitwriter_write_zeroes(bw, q))
667 /* write the unary end bit */
668 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
670 /* write the binary LSBs */
672 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
676 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
683 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
685 unsigned total_bits, msbs;
688 FLAC__ASSERT(0 != bw);
689 FLAC__ASSERT(0 != bw->buffer);
690 FLAC__ASSERT(parameter > 0);
692 k = FLAC__bitmath_ilog2(parameter);
693 if(parameter == 1u<<k) {
696 FLAC__ASSERT(k <= 30);
699 total_bits = 1 + k + msbs;
700 pattern = 1 << k; /* the unary end bit */
701 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
703 if(total_bits <= 32) {
704 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
708 /* write the unary MSBs */
709 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
711 /* write the unary end bit and binary LSBs */
712 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
719 d = (1 << (k+1)) - parameter;
720 q = uval / parameter;
721 r = uval - (q * parameter);
722 /* write the unary MSBs */
723 if(!FLAC__bitwriter_write_zeroes(bw, q))
725 /* write the unary end bit */
726 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
728 /* write the binary LSBs */
730 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
734 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
742 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
746 FLAC__ASSERT(0 != bw);
747 FLAC__ASSERT(0 != bw->buffer);
749 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
752 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
754 else if(val < 0x800) {
755 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
756 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
758 else if(val < 0x10000) {
759 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
760 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
761 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
763 else if(val < 0x200000) {
764 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
765 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
766 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
767 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
769 else if(val < 0x4000000) {
770 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
771 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
772 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
773 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
774 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
777 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
778 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
779 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
780 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
781 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
782 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
788 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
792 FLAC__ASSERT(0 != bw);
793 FLAC__ASSERT(0 != bw->buffer);
795 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
798 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
800 else if(val < 0x800) {
801 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
802 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
804 else if(val < 0x10000) {
805 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
806 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
807 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
809 else if(val < 0x200000) {
810 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
811 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
812 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
813 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
815 else if(val < 0x4000000) {
816 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
817 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
820 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
822 else if(val < 0x80000000) {
823 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
824 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
825 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
826 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
827 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
828 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
831 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
832 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
833 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
834 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
835 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
836 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
837 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
843 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
845 /* 0-pad to byte boundary */
847 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
852 /* These functions a declared inline in this file but are also callable as
853 * externs from elsewhere.
854 * According to the C99 sepc, section 6.7.4, simply providing a function
855 * prototype in a header file without 'inline' and making the function inline
856 * in this file should be sufficient.
857 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
858 * fix that we add extern declarations here.
860 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
861 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
862 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
863 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);