1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008 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"
53 #include "share/alloc.h"
55 /* Things should be fastest when this matches the machine word size */
56 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
57 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
58 typedef FLAC__uint32 bwword;
59 #define FLAC__BYTES_PER_WORD 4
60 #define FLAC__BITS_PER_WORD 32
61 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
62 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
64 #define SWAP_BE_WORD_TO_HOST(x) (x)
67 #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
69 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
74 * The default capacity here doesn't matter too much. The buffer always grows
75 * to hold whatever is written to it. Usually the encoder will stop adding at
76 * a frame or metadata block, then write that out and clear the buffer for the
79 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
80 /* When growing, increment 4K at a time */
81 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
83 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
84 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
89 #define min(x,y) ((x)<(y)?(x):(y))
91 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
93 #define FLAC__U64L(x) x
95 #define FLAC__U64L(x) x##LLU
102 struct FLAC__BitWriter {
104 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
105 unsigned capacity; /* capacity of buffer in words */
106 unsigned words; /* # of complete words in buffer */
107 unsigned bits; /* # of used bits in accum */
111 /* OPT: an MSVC built-in would be better */
112 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
114 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
115 return (x>>16) | (x<<16);
119 /* * WATCHOUT: The current implementation only grows the buffer. */
120 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
122 unsigned new_capacity;
125 FLAC__ASSERT(0 != bw);
126 FLAC__ASSERT(0 != bw->buffer);
128 /* calculate total words needed to store 'bits_to_add' additional bits */
129 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
131 /* it's possible (due to pessimism in the growth estimation that
132 * leads to this call) that we don't actually need to grow
134 if(bw->capacity >= new_capacity)
137 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
138 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
139 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
140 /* make sure we got everything right */
141 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
142 FLAC__ASSERT(new_capacity > bw->capacity);
143 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
145 new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
148 bw->buffer = new_buffer;
149 bw->capacity = new_capacity;
154 /***********************************************************************
156 * Class constructor/destructor
158 ***********************************************************************/
160 FLAC__BitWriter *FLAC__bitwriter_new(void)
162 FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
163 /* note that calloc() sets all members to 0 for us */
167 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
169 FLAC__ASSERT(0 != bw);
171 FLAC__bitwriter_free(bw);
175 /***********************************************************************
177 * Public class methods
179 ***********************************************************************/
181 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
183 FLAC__ASSERT(0 != bw);
185 bw->words = bw->bits = 0;
186 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
187 bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
194 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
196 FLAC__ASSERT(0 != bw);
202 bw->words = bw->bits = 0;
205 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
207 bw->words = bw->bits = 0;
210 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
214 fprintf(out, "bitwriter is NULL\n");
217 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
219 for(i = 0; i < bw->words; i++) {
220 fprintf(out, "%08X: ", i);
221 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
222 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
226 fprintf(out, "%08X: ", i);
227 for(j = 0; j < bw->bits; j++)
228 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
234 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
236 const FLAC__byte *buffer;
239 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
241 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
244 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
245 FLAC__bitwriter_release_buffer(bw);
249 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
251 const FLAC__byte *buffer;
254 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
256 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
259 *crc = FLAC__crc8(buffer, bytes);
260 FLAC__bitwriter_release_buffer(bw);
264 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
266 return ((bw->bits & 7) == 0);
269 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
271 return FLAC__TOTAL_BITS(bw);
274 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
276 FLAC__ASSERT((bw->bits & 7) == 0);
277 /* double protection */
280 /* if we have bits in the accumulator we have to flush those to the buffer first */
282 FLAC__ASSERT(bw->words <= bw->capacity);
283 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
285 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
286 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
288 /* now we can just return what we have */
289 *buffer = (FLAC__byte*)bw->buffer;
290 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
294 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
296 /* nothing to do. in the future, strict checking of a 'writer-is-in-
297 * get-mode' flag could be added everywhere and then cleared here
302 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
306 FLAC__ASSERT(0 != bw);
307 FLAC__ASSERT(0 != bw->buffer);
311 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
312 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
314 /* first part gets to word alignment */
316 n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
320 if(bw->bits == FLAC__BITS_PER_WORD) {
321 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
328 while(bits >= FLAC__BITS_PER_WORD) {
329 bw->buffer[bw->words++] = 0;
330 bits -= FLAC__BITS_PER_WORD;
332 /* do any leftovers */
340 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
342 register unsigned left;
344 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
345 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
347 FLAC__ASSERT(0 != bw);
348 FLAC__ASSERT(0 != bw->buffer);
350 FLAC__ASSERT(bits <= 32);
354 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
355 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
358 left = FLAC__BITS_PER_WORD - bw->bits;
364 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 */
366 bw->accum |= val >> (bw->bits = bits - left);
367 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
373 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
379 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
381 /* zero-out unused bits */
383 val &= (~(0xffffffff << bits));
385 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
388 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
390 /* this could be a little faster but it's not used for much */
393 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
394 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
397 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
400 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
402 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
404 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
406 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
408 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
410 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
416 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
420 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
421 for(i = 0; i < nvals; i++) {
422 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
429 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
432 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
435 FLAC__bitwriter_write_zeroes(bw, val) &&
436 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
439 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
443 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
445 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
446 uval = (val<<1) ^ (val>>31);
448 return 1 + parameter + (uval >> parameter);
452 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
454 unsigned bits, msbs, uval;
457 FLAC__ASSERT(parameter > 0);
459 /* fold signed to unsigned */
461 uval = (unsigned)(((-(++val)) << 1) + 1);
463 uval = (unsigned)(val << 1);
465 k = FLAC__bitmath_ilog2(parameter);
466 if(parameter == 1u<<k) {
467 FLAC__ASSERT(k <= 30);
475 d = (1 << (k+1)) - parameter;
476 q = uval / parameter;
477 r = uval - (q * parameter);
486 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
491 FLAC__ASSERT(parameter > 0);
493 k = FLAC__bitmath_ilog2(parameter);
494 if(parameter == 1u<<k) {
495 FLAC__ASSERT(k <= 30);
503 d = (1 << (k+1)) - parameter;
504 q = uval / parameter;
505 r = uval - (q * parameter);
515 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
517 unsigned total_bits, interesting_bits, msbs;
518 FLAC__uint32 uval, pattern;
520 FLAC__ASSERT(0 != bw);
521 FLAC__ASSERT(0 != bw->buffer);
522 FLAC__ASSERT(parameter < 8*sizeof(uval));
524 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
525 uval = (val<<1) ^ (val>>31);
527 msbs = uval >> parameter;
528 interesting_bits = 1 + parameter;
529 total_bits = interesting_bits + msbs;
530 pattern = 1 << parameter; /* the unary end bit */
531 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
534 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
537 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
538 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
541 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
543 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
544 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
547 const unsigned lsbits = 1 + parameter;
550 FLAC__ASSERT(0 != bw);
551 FLAC__ASSERT(0 != bw->buffer);
552 FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
553 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
554 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
557 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
558 uval = (*vals<<1) ^ (*vals>>31);
560 msbits = uval >> parameter;
562 #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) */
563 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
564 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
565 bw->bits = bw->bits + msbits + lsbits;
566 uval |= mask1; /* set stop bit */
567 uval &= mask2; /* mask off unused top bits */
568 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
569 bw->accum <<= msbits;
570 bw->accum <<= lsbits;
572 if(bw->bits == FLAC__BITS_PER_WORD) {
573 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
575 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
576 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
577 FLAC__ASSERT(bw->capacity == bw->words);
583 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
584 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
585 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
586 bw->bits = bw->bits + msbits + lsbits;
587 uval |= mask1; /* set stop bit */
588 uval &= mask2; /* mask off unused top bits */
589 bw->accum <<= msbits + lsbits;
594 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
595 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
596 if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
600 /* first part gets to word alignment */
602 left = FLAC__BITS_PER_WORD - bw->bits;
604 bw->accum <<= msbits;
611 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
616 while(msbits >= FLAC__BITS_PER_WORD) {
617 bw->buffer[bw->words++] = 0;
618 msbits -= FLAC__BITS_PER_WORD;
620 /* do any leftovers */
627 uval |= mask1; /* set stop bit */
628 uval &= mask2; /* mask off unused top bits */
630 left = FLAC__BITS_PER_WORD - bw->bits;
632 bw->accum <<= lsbits;
637 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
638 * be > lsbits (because of previous assertions) so it would have
639 * triggered the (lsbits<left) case above.
641 FLAC__ASSERT(bw->bits);
642 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
644 bw->accum |= uval >> (bw->bits = lsbits - left);
645 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
658 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
660 unsigned total_bits, msbs, uval;
663 FLAC__ASSERT(0 != bw);
664 FLAC__ASSERT(0 != bw->buffer);
665 FLAC__ASSERT(parameter > 0);
667 /* fold signed to unsigned */
669 uval = (unsigned)(((-(++val)) << 1) + 1);
671 uval = (unsigned)(val << 1);
673 k = FLAC__bitmath_ilog2(parameter);
674 if(parameter == 1u<<k) {
677 FLAC__ASSERT(k <= 30);
680 total_bits = 1 + k + msbs;
681 pattern = 1 << k; /* the unary end bit */
682 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
684 if(total_bits <= 32) {
685 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
689 /* write the unary MSBs */
690 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
692 /* write the unary end bit and binary LSBs */
693 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
700 d = (1 << (k+1)) - parameter;
701 q = uval / parameter;
702 r = uval - (q * parameter);
703 /* write the unary MSBs */
704 if(!FLAC__bitwriter_write_zeroes(bw, q))
706 /* write the unary end bit */
707 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
709 /* write the binary LSBs */
711 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
715 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
722 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
724 unsigned total_bits, msbs;
727 FLAC__ASSERT(0 != bw);
728 FLAC__ASSERT(0 != bw->buffer);
729 FLAC__ASSERT(parameter > 0);
731 k = FLAC__bitmath_ilog2(parameter);
732 if(parameter == 1u<<k) {
735 FLAC__ASSERT(k <= 30);
738 total_bits = 1 + k + msbs;
739 pattern = 1 << k; /* the unary end bit */
740 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
742 if(total_bits <= 32) {
743 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
747 /* write the unary MSBs */
748 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
750 /* write the unary end bit and binary LSBs */
751 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
758 d = (1 << (k+1)) - parameter;
759 q = uval / parameter;
760 r = uval - (q * parameter);
761 /* write the unary MSBs */
762 if(!FLAC__bitwriter_write_zeroes(bw, q))
764 /* write the unary end bit */
765 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
767 /* write the binary LSBs */
769 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
773 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
781 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
785 FLAC__ASSERT(0 != bw);
786 FLAC__ASSERT(0 != bw->buffer);
788 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
791 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
793 else if(val < 0x800) {
794 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
795 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
797 else if(val < 0x10000) {
798 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
799 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
800 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
802 else if(val < 0x200000) {
803 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
804 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
805 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
806 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
808 else if(val < 0x4000000) {
809 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
810 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
811 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
812 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
813 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
816 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
817 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
820 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
821 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
827 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
831 FLAC__ASSERT(0 != bw);
832 FLAC__ASSERT(0 != bw->buffer);
834 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
837 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
839 else if(val < 0x800) {
840 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
841 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
843 else if(val < 0x10000) {
844 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
845 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
846 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
848 else if(val < 0x200000) {
849 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
850 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
851 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
852 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
854 else if(val < 0x4000000) {
855 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
856 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
857 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
858 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
859 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
861 else if(val < 0x80000000) {
862 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
863 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
864 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
865 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
866 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
867 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
870 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
871 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
872 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
873 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
874 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
875 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
876 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
882 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
884 /* 0-pad to byte boundary */
886 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));