1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include "private/bitwriter.h"
40 #include "private/crc.h"
41 #include "private/macros.h"
42 #include "FLAC/assert.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "share/endswap.h"
47 /* Things should be fastest when this matches the machine word size */
48 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
49 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
50 #define FLAC__BYTES_PER_WORD 4
51 #define FLAC__BITS_PER_WORD 32
52 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
53 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
55 #define SWAP_BE_WORD_TO_HOST(x) (x)
57 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
61 * The default capacity here doesn't matter too much. The buffer always grows
62 * to hold whatever is written to it. Usually the encoder will stop adding at
63 * a frame or metadata block, then write that out and clear the buffer for the
66 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_t); /* size in words */
67 /* When growing, increment 4K at a time */
68 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_t); /* size in words */
70 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
71 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
73 struct FLAC__BitWriter {
75 uint32_t accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
76 unsigned capacity; /* capacity of buffer in words */
77 unsigned words; /* # of complete words in buffer */
78 unsigned bits; /* # of used bits in accum */
81 /* * WATCHOUT: The current implementation only grows the buffer. */
82 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
84 unsigned new_capacity;
87 FLAC__ASSERT(0 != bw);
88 FLAC__ASSERT(0 != bw->buffer);
90 /* calculate total words needed to store 'bits_to_add' additional bits */
91 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
93 /* it's possible (due to pessimism in the growth estimation that
94 * leads to this call) that we don't actually need to grow
96 if(bw->capacity >= new_capacity)
99 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
100 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
101 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
102 /* make sure we got everything right */
103 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
104 FLAC__ASSERT(new_capacity > bw->capacity);
105 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
107 new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times*/new_capacity);
110 bw->buffer = new_buffer;
111 bw->capacity = new_capacity;
116 /***********************************************************************
118 * Class constructor/destructor
120 ***********************************************************************/
122 FLAC__BitWriter *FLAC__bitwriter_new(void)
124 FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
125 /* note that calloc() sets all members to 0 for us */
129 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
131 FLAC__ASSERT(0 != bw);
133 FLAC__bitwriter_free(bw);
137 /***********************************************************************
139 * Public class methods
141 ***********************************************************************/
143 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
145 FLAC__ASSERT(0 != bw);
147 bw->words = bw->bits = 0;
148 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
149 bw->buffer = malloc(sizeof(uint32_t) * bw->capacity);
156 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
158 FLAC__ASSERT(0 != bw);
164 bw->words = bw->bits = 0;
167 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
169 bw->words = bw->bits = 0;
172 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
176 fprintf(out, "bitwriter is NULL\n");
179 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);
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);
196 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
198 const FLAC__byte *buffer;
201 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
203 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
206 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
207 FLAC__bitwriter_release_buffer(bw);
211 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
213 const FLAC__byte *buffer;
216 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
218 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
221 *crc = FLAC__crc8(buffer, bytes);
222 FLAC__bitwriter_release_buffer(bw);
226 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
228 return ((bw->bits & 7) == 0);
231 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
233 return FLAC__TOTAL_BITS(bw);
236 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
238 FLAC__ASSERT((bw->bits & 7) == 0);
239 /* double protection */
242 /* if we have bits in the accumulator we have to flush those to the buffer first */
244 FLAC__ASSERT(bw->words <= bw->capacity);
245 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
247 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
248 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
250 /* now we can just return what we have */
251 *buffer = (FLAC__byte*)bw->buffer;
252 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
256 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
258 /* nothing to do. in the future, strict checking of a 'writer-is-in-
259 * get-mode' flag could be added everywhere and then cleared here
264 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
268 FLAC__ASSERT(0 != bw);
269 FLAC__ASSERT(0 != bw->buffer);
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))
276 /* first part gets to word alignment */
278 n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
282 if(bw->bits == FLAC__BITS_PER_WORD) {
283 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
290 while(bits >= FLAC__BITS_PER_WORD) {
291 bw->buffer[bw->words++] = 0;
292 bits -= FLAC__BITS_PER_WORD;
294 /* do any leftovers */
302 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
304 register unsigned left;
306 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
307 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
309 FLAC__ASSERT(0 != bw);
310 FLAC__ASSERT(0 != bw->buffer);
312 FLAC__ASSERT(bits <= 32);
316 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
317 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
320 left = FLAC__BITS_PER_WORD - bw->bits;
326 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 */
328 bw->accum |= val >> (bw->bits = bits - left);
329 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
335 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
341 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
343 /* zero-out unused bits */
345 val &= (~(0xffffffff << bits));
347 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
350 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
352 /* this could be a little faster but it's not used for much */
355 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
356 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
359 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
362 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
364 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
366 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
368 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
370 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
372 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
378 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
382 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
383 for(i = 0; i < nvals; i++) {
384 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
391 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
394 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
397 FLAC__bitwriter_write_zeroes(bw, val) &&
398 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
401 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
405 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
407 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
408 uval = (val<<1) ^ (val>>31);
410 return 1 + parameter + (uval >> parameter);
414 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
416 unsigned bits, msbs, uval;
419 FLAC__ASSERT(parameter > 0);
421 /* fold signed to unsigned */
423 uval = (unsigned)(((-(++val)) << 1) + 1);
425 uval = (unsigned)(val << 1);
427 k = FLAC__bitmath_ilog2(parameter);
428 if(parameter == 1u<<k) {
429 FLAC__ASSERT(k <= 30);
437 d = (1 << (k+1)) - parameter;
438 q = uval / parameter;
439 r = uval - (q * parameter);
448 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
453 FLAC__ASSERT(parameter > 0);
455 k = FLAC__bitmath_ilog2(parameter);
456 if(parameter == 1u<<k) {
457 FLAC__ASSERT(k <= 30);
465 d = (1 << (k+1)) - parameter;
466 q = uval / parameter;
467 r = uval - (q * parameter);
477 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
479 unsigned total_bits, interesting_bits, msbs;
480 FLAC__uint32 uval, pattern;
482 FLAC__ASSERT(0 != bw);
483 FLAC__ASSERT(0 != bw->buffer);
484 FLAC__ASSERT(parameter < 8*sizeof(uval));
486 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
487 uval = (val<<1) ^ (val>>31);
489 msbs = uval >> parameter;
490 interesting_bits = 1 + parameter;
491 total_bits = interesting_bits + msbs;
492 pattern = 1 << parameter; /* the unary end bit */
493 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
496 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
499 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
500 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
503 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
505 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
506 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
509 const unsigned lsbits = 1 + parameter;
512 FLAC__ASSERT(0 != bw);
513 FLAC__ASSERT(0 != bw->buffer);
514 FLAC__ASSERT(parameter < 8*sizeof(uint32_t)-1);
515 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
516 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
519 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
520 uval = (*vals<<1) ^ (*vals>>31);
522 msbits = uval >> parameter;
524 #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) */
525 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
526 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
527 bw->bits = bw->bits + msbits + lsbits;
528 uval |= mask1; /* set stop bit */
529 uval &= mask2; /* mask off unused top bits */
530 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
531 bw->accum <<= msbits;
532 bw->accum <<= lsbits;
534 if(bw->bits == FLAC__BITS_PER_WORD) {
535 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
537 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
538 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
539 FLAC__ASSERT(bw->capacity == bw->words);
545 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
546 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
547 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
548 bw->bits = bw->bits + msbits + lsbits;
549 uval |= mask1; /* set stop bit */
550 uval &= mask2; /* mask off unused top bits */
551 bw->accum <<= msbits + lsbits;
556 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
557 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
558 if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 uint32_t*/ && !bitwriter_grow_(bw, msbits+lsbits))
562 /* first part gets to word alignment */
564 left = FLAC__BITS_PER_WORD - bw->bits;
566 bw->accum <<= msbits;
573 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
578 while(msbits >= FLAC__BITS_PER_WORD) {
579 bw->buffer[bw->words++] = 0;
580 msbits -= FLAC__BITS_PER_WORD;
582 /* do any leftovers */
589 uval |= mask1; /* set stop bit */
590 uval &= mask2; /* mask off unused top bits */
592 left = FLAC__BITS_PER_WORD - bw->bits;
594 bw->accum <<= lsbits;
599 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
600 * be > lsbits (because of previous assertions) so it would have
601 * triggered the (lsbits<left) case above.
603 FLAC__ASSERT(bw->bits);
604 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
606 bw->accum |= uval >> (bw->bits = lsbits - left);
607 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
620 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
622 unsigned total_bits, msbs, uval;
625 FLAC__ASSERT(0 != bw);
626 FLAC__ASSERT(0 != bw->buffer);
627 FLAC__ASSERT(parameter > 0);
629 /* fold signed to unsigned */
631 uval = (unsigned)(((-(++val)) << 1) + 1);
633 uval = (unsigned)(val << 1);
635 k = FLAC__bitmath_ilog2(parameter);
636 if(parameter == 1u<<k) {
639 FLAC__ASSERT(k <= 30);
642 total_bits = 1 + k + msbs;
643 pattern = 1 << k; /* the unary end bit */
644 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
646 if(total_bits <= 32) {
647 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
651 /* write the unary MSBs */
652 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
654 /* write the unary end bit and binary LSBs */
655 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
662 d = (1 << (k+1)) - parameter;
663 q = uval / parameter;
664 r = uval - (q * parameter);
665 /* write the unary MSBs */
666 if(!FLAC__bitwriter_write_zeroes(bw, q))
668 /* write the unary end bit */
669 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
671 /* write the binary LSBs */
673 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
677 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
684 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
686 unsigned total_bits, msbs;
689 FLAC__ASSERT(0 != bw);
690 FLAC__ASSERT(0 != bw->buffer);
691 FLAC__ASSERT(parameter > 0);
693 k = FLAC__bitmath_ilog2(parameter);
694 if(parameter == 1u<<k) {
697 FLAC__ASSERT(k <= 30);
700 total_bits = 1 + k + msbs;
701 pattern = 1 << k; /* the unary end bit */
702 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
704 if(total_bits <= 32) {
705 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
709 /* write the unary MSBs */
710 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
712 /* write the unary end bit and binary LSBs */
713 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
720 d = (1 << (k+1)) - parameter;
721 q = uval / parameter;
722 r = uval - (q * parameter);
723 /* write the unary MSBs */
724 if(!FLAC__bitwriter_write_zeroes(bw, q))
726 /* write the unary end bit */
727 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
729 /* write the binary LSBs */
731 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
735 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
743 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
747 FLAC__ASSERT(0 != bw);
748 FLAC__ASSERT(0 != bw->buffer);
750 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
753 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
755 else if(val < 0x800) {
756 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
757 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
759 else if(val < 0x10000) {
760 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
761 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
762 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
764 else if(val < 0x200000) {
765 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
766 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
767 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
768 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
770 else if(val < 0x4000000) {
771 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
772 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
773 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
774 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
775 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
778 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
779 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
780 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
781 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
782 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
783 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
789 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
793 FLAC__ASSERT(0 != bw);
794 FLAC__ASSERT(0 != bw->buffer);
796 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
799 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
801 else if(val < 0x800) {
802 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
803 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
805 else if(val < 0x10000) {
806 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
807 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
808 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
810 else if(val < 0x200000) {
811 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
812 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
813 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
814 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
816 else if(val < 0x4000000) {
817 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
820 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
821 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
823 else if(val < 0x80000000) {
824 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
825 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
826 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
827 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
828 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
829 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
832 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
833 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
834 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
835 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
836 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
837 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
838 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
844 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
846 /* 0-pad to byte boundary */
848 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
853 /* These functions a declared inline in this file but are also callable as
854 * externs from elsewhere.
855 * According to the C99 sepc, section 6.7.4, simply providing a function
856 * prototype in a header file without 'inline' and making the function inline
857 * in this file should be sufficient.
858 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
859 * fix that we add extern declarations here.
861 extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits);
862 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
863 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
864 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
865 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);