1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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.
36 #include <stdlib.h> /* for malloc() */
37 #include <string.h> /* for memcpy(), memset() */
39 #include <winsock.h> /* for ntohl() */
40 #elif defined FLAC__SYS_DARWIN
41 #include <machine/endian.h> /* for ntohl() */
42 #elif defined __MINGW32__
43 #include <winsock.h> /* for ntohl() */
45 #include <netinet/in.h> /* for ntohl() */
48 #include "private/bitmath.h"
50 #include "private/bitwriter.h"
51 #include "private/crc.h"
52 #include "FLAC/assert.h"
54 /* Things should be fastest when this matches the machine word size */
55 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
56 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
57 typedef FLAC__uint32 bwword;
58 #define FLAC__BYTES_PER_WORD 4
59 #define FLAC__BITS_PER_WORD 32
60 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
61 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
63 #define SWAP_BE_WORD_TO_HOST(x) (x)
66 #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
68 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
73 * The default capacity here doesn't matter too much. The buffer always grows
74 * to hold whatever is written to it. Usually the encoder will stop adding at
75 * a frame or metadata block, then write that out and clear the buffer for the
78 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
79 /* When growing, increment 4K at a time */
80 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
82 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
83 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
88 #define min(x,y) ((x)<(y)?(x):(y))
90 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
92 #define FLAC__U64L(x) x
94 #define FLAC__U64L(x) x##LLU
101 struct FLAC__BitWriter {
103 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
104 unsigned capacity; /* capacity of buffer in words */
105 unsigned words; /* # of complete words in buffer */
106 unsigned bits; /* # of used bits in accum */
110 /* OPT: an MSVC built-in would be better */
111 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
113 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
114 return (x>>16) | (x<<16);
118 /* * WATCHOUT: The current implementation only grows the buffer. */
119 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
121 unsigned new_capacity;
124 FLAC__ASSERT(0 != bw);
125 FLAC__ASSERT(0 != bw->buffer);
127 /* calculate total words needed to store 'bits_to_add' additional bits */
128 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
130 /* it's possible (due to pessimism in the growth estimation that
131 * leads to this call) that we don't actually need to grow
133 if(bw->capacity >= new_capacity)
136 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
137 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
138 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
139 /* make sure we got everything right */
140 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
141 FLAC__ASSERT(new_capacity > bw->capacity);
142 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
144 new_buffer = (bwword*)realloc(bw->buffer, sizeof(bwword)*new_capacity);
147 bw->buffer = new_buffer;
148 bw->capacity = new_capacity;
153 /***********************************************************************
155 * Class constructor/destructor
157 ***********************************************************************/
159 FLAC__BitWriter *FLAC__bitwriter_new(void)
161 FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
162 /* note that calloc() sets all members to 0 for us */
166 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
168 FLAC__ASSERT(0 != bw);
170 FLAC__bitwriter_free(bw);
174 /***********************************************************************
176 * Public class methods
178 ***********************************************************************/
180 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
182 FLAC__ASSERT(0 != bw);
184 bw->words = bw->bits = 0;
185 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
186 bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
193 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
195 FLAC__ASSERT(0 != bw);
201 bw->words = bw->bits = 0;
204 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
206 bw->words = bw->bits = 0;
209 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
213 fprintf(out, "bitwriter is NULL\n");
216 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
218 for(i = 0; i < bw->words; i++) {
219 fprintf(out, "%08X: ", i);
220 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
221 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
225 fprintf(out, "%08X: ", i);
226 for(j = 0; j < bw->bits; j++)
227 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
233 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
235 const FLAC__byte *buffer;
238 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
240 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
243 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
244 FLAC__bitwriter_release_buffer(bw);
248 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
250 const FLAC__byte *buffer;
253 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
255 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
258 *crc = FLAC__crc8(buffer, bytes);
259 FLAC__bitwriter_release_buffer(bw);
263 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
265 return ((bw->bits & 7) == 0);
268 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
270 return FLAC__TOTAL_BITS(bw);
273 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
275 FLAC__ASSERT((bw->bits & 7) == 0);
276 /* double protection */
279 /* if we have bits in the accumulator we have to flush those to the buffer first */
281 FLAC__ASSERT(bw->words <= bw->capacity);
282 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
284 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
285 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
287 /* now we can just return what we have */
288 *buffer = (FLAC__byte*)bw->buffer;
289 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
293 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
295 /* nothing to do. in the future, strict checking of a 'writer-is-in-
296 * get-mode' flag could be added everywhere and then cleared here
301 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
305 FLAC__ASSERT(0 != bw);
306 FLAC__ASSERT(0 != bw->buffer);
310 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
311 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
313 /* first part gets to word alignment */
315 n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
319 if(bw->bits == FLAC__BITS_PER_WORD) {
320 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
327 while(bits >= FLAC__BITS_PER_WORD) {
328 bw->buffer[bw->words++] = 0;
329 bits -= FLAC__BITS_PER_WORD;
331 /* do any leftovers */
339 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
341 register unsigned left;
343 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
344 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
346 FLAC__ASSERT(0 != bw);
347 FLAC__ASSERT(0 != bw->buffer);
349 FLAC__ASSERT(bits <= 32);
353 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
354 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
357 left = FLAC__BITS_PER_WORD - bw->bits;
363 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 */
365 bw->accum |= val >> (bw->bits = bits - left);
366 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
372 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
378 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
380 /* zero-out unused bits */
382 val &= (~(0xffffffff << bits));
384 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
387 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
389 /* this could be a little faster but it's not used for much */
392 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
393 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
396 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
399 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
401 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
403 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
405 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
407 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
409 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
415 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
419 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
420 for(i = 0; i < nvals; i++) {
421 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
428 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
431 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
434 FLAC__bitwriter_write_zeroes(bw, val) &&
435 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
438 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
442 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
444 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
445 uval = (val<<1) ^ (val>>31);
447 return 1 + parameter + (uval >> parameter);
451 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
453 unsigned bits, msbs, uval;
456 FLAC__ASSERT(parameter > 0);
458 /* fold signed to unsigned */
460 uval = (unsigned)(((-(++val)) << 1) + 1);
462 uval = (unsigned)(val << 1);
464 k = FLAC__bitmath_ilog2(parameter);
465 if(parameter == 1u<<k) {
466 FLAC__ASSERT(k <= 30);
474 d = (1 << (k+1)) - parameter;
475 q = uval / parameter;
476 r = uval - (q * parameter);
485 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
490 FLAC__ASSERT(parameter > 0);
492 k = FLAC__bitmath_ilog2(parameter);
493 if(parameter == 1u<<k) {
494 FLAC__ASSERT(k <= 30);
502 d = (1 << (k+1)) - parameter;
503 q = uval / parameter;
504 r = uval - (q * parameter);
514 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
516 unsigned total_bits, interesting_bits, msbs;
517 FLAC__uint32 uval, pattern;
519 FLAC__ASSERT(0 != bw);
520 FLAC__ASSERT(0 != bw->buffer);
521 FLAC__ASSERT(parameter < 8*sizeof(uval));
523 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
524 uval = (val<<1) ^ (val>>31);
526 msbs = uval >> parameter;
527 interesting_bits = 1 + parameter;
528 total_bits = interesting_bits + msbs;
529 pattern = 1 << parameter; /* the unary end bit */
530 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
533 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
536 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
537 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
540 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
542 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
543 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
546 const unsigned lsbits = 1 + parameter;
549 FLAC__ASSERT(0 != bw);
550 FLAC__ASSERT(0 != bw->buffer);
551 FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
552 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
553 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
556 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
557 uval = (*vals<<1) ^ (*vals>>31);
559 msbits = uval >> parameter;
561 #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) */
562 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
563 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
564 bw->bits = bw->bits + msbits + lsbits;
565 uval |= mask1; /* set stop bit */
566 uval &= mask2; /* mask off unused top bits */
567 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
568 bw->accum <<= msbits;
569 bw->accum <<= lsbits;
571 if(bw->bits == FLAC__BITS_PER_WORD) {
572 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
574 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
575 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
576 FLAC__ASSERT(bw->capacity == bw->words);
582 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
583 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
584 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
585 bw->bits = bw->bits + msbits + lsbits;
586 uval |= mask1; /* set stop bit */
587 uval &= mask2; /* mask off unused top bits */
588 bw->accum <<= msbits + lsbits;
593 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
594 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
595 if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
599 /* first part gets to word alignment */
601 left = FLAC__BITS_PER_WORD - bw->bits;
603 bw->accum <<= msbits;
610 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
615 while(msbits >= FLAC__BITS_PER_WORD) {
616 bw->buffer[bw->words++] = 0;
617 msbits -= FLAC__BITS_PER_WORD;
619 /* do any leftovers */
626 uval |= mask1; /* set stop bit */
627 uval &= mask2; /* mask off unused top bits */
629 left = FLAC__BITS_PER_WORD - bw->bits;
631 bw->accum <<= lsbits;
636 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
637 * be > lsbits (because of previous assertions) so it would have
638 * triggered the (lsbits<left) case above.
640 FLAC__ASSERT(bw->bits);
641 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
643 bw->accum |= uval >> (bw->bits = lsbits - left);
644 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
657 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
659 unsigned total_bits, msbs, uval;
662 FLAC__ASSERT(0 != bw);
663 FLAC__ASSERT(0 != bw->buffer);
664 FLAC__ASSERT(parameter > 0);
666 /* fold signed to unsigned */
668 uval = (unsigned)(((-(++val)) << 1) + 1);
670 uval = (unsigned)(val << 1);
672 k = FLAC__bitmath_ilog2(parameter);
673 if(parameter == 1u<<k) {
676 FLAC__ASSERT(k <= 30);
679 total_bits = 1 + k + msbs;
680 pattern = 1 << k; /* the unary end bit */
681 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
683 if(total_bits <= 32) {
684 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
688 /* write the unary MSBs */
689 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
691 /* write the unary end bit and binary LSBs */
692 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
699 d = (1 << (k+1)) - parameter;
700 q = uval / parameter;
701 r = uval - (q * parameter);
702 /* write the unary MSBs */
703 if(!FLAC__bitwriter_write_zeroes(bw, q))
705 /* write the unary end bit */
706 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
708 /* write the binary LSBs */
710 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
714 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
721 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
723 unsigned total_bits, msbs;
726 FLAC__ASSERT(0 != bw);
727 FLAC__ASSERT(0 != bw->buffer);
728 FLAC__ASSERT(parameter > 0);
730 k = FLAC__bitmath_ilog2(parameter);
731 if(parameter == 1u<<k) {
734 FLAC__ASSERT(k <= 30);
737 total_bits = 1 + k + msbs;
738 pattern = 1 << k; /* the unary end bit */
739 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
741 if(total_bits <= 32) {
742 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
746 /* write the unary MSBs */
747 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
749 /* write the unary end bit and binary LSBs */
750 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
757 d = (1 << (k+1)) - parameter;
758 q = uval / parameter;
759 r = uval - (q * parameter);
760 /* write the unary MSBs */
761 if(!FLAC__bitwriter_write_zeroes(bw, q))
763 /* write the unary end bit */
764 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
766 /* write the binary LSBs */
768 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
772 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
780 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
784 FLAC__ASSERT(0 != bw);
785 FLAC__ASSERT(0 != bw->buffer);
787 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
790 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
792 else if(val < 0x800) {
793 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
794 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
796 else if(val < 0x10000) {
797 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
798 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
799 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
801 else if(val < 0x200000) {
802 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
803 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
804 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
805 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
807 else if(val < 0x4000000) {
808 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
809 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
810 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
811 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
812 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
815 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
816 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
817 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
820 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
826 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
830 FLAC__ASSERT(0 != bw);
831 FLAC__ASSERT(0 != bw->buffer);
833 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
836 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
838 else if(val < 0x800) {
839 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
840 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
842 else if(val < 0x10000) {
843 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
844 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
845 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
847 else if(val < 0x200000) {
848 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
849 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
850 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
851 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
853 else if(val < 0x4000000) {
854 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
855 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
856 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
857 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
858 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
860 else if(val < 0x80000000) {
861 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
862 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
863 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
864 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
865 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
866 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
869 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
870 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
871 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
872 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
873 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
874 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
875 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
881 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
883 /* 0-pad to byte boundary */
885 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));