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"
53 #include "share/alloc.h"
55 static unsigned long ntohl(unsigned long x) {
56 x = (x >> 16UL) | (x << 16UL);
57 x = ((x & 0x00FF00FFUL) << 8UL) | ((x & 0xFF00FF00UL) >> 8UL);
61 /* Things should be fastest when this matches the machine word size */
62 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
63 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
64 typedef FLAC__uint32 bwword;
65 #define FLAC__BYTES_PER_WORD 4
66 #define FLAC__BITS_PER_WORD 32
67 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
68 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
70 #define SWAP_BE_WORD_TO_HOST(x) (x)
73 #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
75 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
80 * The default capacity here doesn't matter too much. The buffer always grows
81 * to hold whatever is written to it. Usually the encoder will stop adding at
82 * a frame or metadata block, then write that out and clear the buffer for the
85 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
86 /* When growing, increment 4K at a time */
87 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
89 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
90 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
95 #define min(x,y) ((x)<(y)?(x):(y))
97 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
99 #define FLAC__U64L(x) x
101 #define FLAC__U64L(x) x##LLU
108 struct FLAC__BitWriter {
110 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
111 unsigned capacity; /* capacity of buffer in words */
112 unsigned words; /* # of complete words in buffer */
113 unsigned bits; /* # of used bits in accum */
117 /* OPT: an MSVC built-in would be better */
118 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
120 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
121 return (x>>16) | (x<<16);
125 /* * WATCHOUT: The current implementation only grows the buffer. */
126 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
128 unsigned new_capacity;
131 FLAC__ASSERT(0 != bw);
132 FLAC__ASSERT(0 != bw->buffer);
134 /* calculate total words needed to store 'bits_to_add' additional bits */
135 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
137 /* it's possible (due to pessimism in the growth estimation that
138 * leads to this call) that we don't actually need to grow
140 if(bw->capacity >= new_capacity)
143 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
144 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
145 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
146 /* make sure we got everything right */
147 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
148 FLAC__ASSERT(new_capacity > bw->capacity);
149 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
151 new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
154 bw->buffer = new_buffer;
155 bw->capacity = new_capacity;
160 /***********************************************************************
162 * Class constructor/destructor
164 ***********************************************************************/
166 FLAC__BitWriter *FLAC__bitwriter_new(void)
168 FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
169 /* note that calloc() sets all members to 0 for us */
173 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
175 FLAC__ASSERT(0 != bw);
177 FLAC__bitwriter_free(bw);
181 /***********************************************************************
183 * Public class methods
185 ***********************************************************************/
187 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
189 FLAC__ASSERT(0 != bw);
191 bw->words = bw->bits = 0;
192 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
193 bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
200 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
202 FLAC__ASSERT(0 != bw);
208 bw->words = bw->bits = 0;
211 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
213 bw->words = bw->bits = 0;
216 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
220 fprintf(out, "bitwriter is NULL\n");
223 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
225 for(i = 0; i < bw->words; i++) {
226 fprintf(out, "%08X: ", i);
227 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
228 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
232 fprintf(out, "%08X: ", i);
233 for(j = 0; j < bw->bits; j++)
234 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
240 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
242 const FLAC__byte *buffer;
245 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
247 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
250 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
251 FLAC__bitwriter_release_buffer(bw);
255 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
257 const FLAC__byte *buffer;
260 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
262 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
265 *crc = FLAC__crc8(buffer, bytes);
266 FLAC__bitwriter_release_buffer(bw);
270 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
272 return ((bw->bits & 7) == 0);
275 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
277 return FLAC__TOTAL_BITS(bw);
280 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
282 FLAC__ASSERT((bw->bits & 7) == 0);
283 /* double protection */
286 /* if we have bits in the accumulator we have to flush those to the buffer first */
288 FLAC__ASSERT(bw->words <= bw->capacity);
289 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
291 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
292 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
294 /* now we can just return what we have */
295 *buffer = (FLAC__byte*)bw->buffer;
296 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
300 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
302 /* nothing to do. in the future, strict checking of a 'writer-is-in-
303 * get-mode' flag could be added everywhere and then cleared here
308 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
312 FLAC__ASSERT(0 != bw);
313 FLAC__ASSERT(0 != bw->buffer);
317 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
318 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
320 /* first part gets to word alignment */
322 n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
326 if(bw->bits == FLAC__BITS_PER_WORD) {
327 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
334 while(bits >= FLAC__BITS_PER_WORD) {
335 bw->buffer[bw->words++] = 0;
336 bits -= FLAC__BITS_PER_WORD;
338 /* do any leftovers */
346 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
348 register unsigned left;
350 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
351 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
353 FLAC__ASSERT(0 != bw);
354 FLAC__ASSERT(0 != bw->buffer);
356 FLAC__ASSERT(bits <= 32);
360 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
361 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
364 left = FLAC__BITS_PER_WORD - bw->bits;
370 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 */
372 bw->accum |= val >> (bw->bits = bits - left);
373 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
379 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
385 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
387 /* zero-out unused bits */
389 val &= (~(0xffffffff << bits));
391 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
394 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
396 /* this could be a little faster but it's not used for much */
399 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
400 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
403 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
406 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
408 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
410 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
412 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
414 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
416 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
422 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
426 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
427 for(i = 0; i < nvals; i++) {
428 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
435 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
438 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
441 FLAC__bitwriter_write_zeroes(bw, val) &&
442 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
445 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
449 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
451 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
452 uval = (val<<1) ^ (val>>31);
454 return 1 + parameter + (uval >> parameter);
458 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
460 unsigned bits, msbs, uval;
463 FLAC__ASSERT(parameter > 0);
465 /* fold signed to unsigned */
467 uval = (unsigned)(((-(++val)) << 1) + 1);
469 uval = (unsigned)(val << 1);
471 k = FLAC__bitmath_ilog2(parameter);
472 if(parameter == 1u<<k) {
473 FLAC__ASSERT(k <= 30);
481 d = (1 << (k+1)) - parameter;
482 q = uval / parameter;
483 r = uval - (q * parameter);
492 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
497 FLAC__ASSERT(parameter > 0);
499 k = FLAC__bitmath_ilog2(parameter);
500 if(parameter == 1u<<k) {
501 FLAC__ASSERT(k <= 30);
509 d = (1 << (k+1)) - parameter;
510 q = uval / parameter;
511 r = uval - (q * parameter);
521 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
523 unsigned total_bits, interesting_bits, msbs;
524 FLAC__uint32 uval, pattern;
526 FLAC__ASSERT(0 != bw);
527 FLAC__ASSERT(0 != bw->buffer);
528 FLAC__ASSERT(parameter < 8*sizeof(uval));
530 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
531 uval = (val<<1) ^ (val>>31);
533 msbs = uval >> parameter;
534 interesting_bits = 1 + parameter;
535 total_bits = interesting_bits + msbs;
536 pattern = 1 << parameter; /* the unary end bit */
537 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
540 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
543 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
544 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
547 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
549 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
550 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
553 const unsigned lsbits = 1 + parameter;
556 FLAC__ASSERT(0 != bw);
557 FLAC__ASSERT(0 != bw->buffer);
558 FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
559 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
560 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
563 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
564 uval = (*vals<<1) ^ (*vals>>31);
566 msbits = uval >> parameter;
568 #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) */
569 if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
570 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
571 bw->bits = bw->bits + msbits + lsbits;
572 uval |= mask1; /* set stop bit */
573 uval &= mask2; /* mask off unused top bits */
574 /* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
575 bw->accum <<= msbits;
576 bw->accum <<= lsbits;
578 if(bw->bits == FLAC__BITS_PER_WORD) {
579 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
581 /* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
582 if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
583 FLAC__ASSERT(bw->capacity == bw->words);
589 #elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
590 if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
591 /* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
592 bw->bits = bw->bits + msbits + lsbits;
593 uval |= mask1; /* set stop bit */
594 uval &= mask2; /* mask off unused top bits */
595 bw->accum <<= msbits + lsbits;
600 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
601 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
602 if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
606 /* first part gets to word alignment */
608 left = FLAC__BITS_PER_WORD - bw->bits;
610 bw->accum <<= msbits;
617 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
622 while(msbits >= FLAC__BITS_PER_WORD) {
623 bw->buffer[bw->words++] = 0;
624 msbits -= FLAC__BITS_PER_WORD;
626 /* do any leftovers */
633 uval |= mask1; /* set stop bit */
634 uval &= mask2; /* mask off unused top bits */
636 left = FLAC__BITS_PER_WORD - bw->bits;
638 bw->accum <<= lsbits;
643 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
644 * be > lsbits (because of previous assertions) so it would have
645 * triggered the (lsbits<left) case above.
647 FLAC__ASSERT(bw->bits);
648 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
650 bw->accum |= uval >> (bw->bits = lsbits - left);
651 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
664 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
666 unsigned total_bits, msbs, uval;
669 FLAC__ASSERT(0 != bw);
670 FLAC__ASSERT(0 != bw->buffer);
671 FLAC__ASSERT(parameter > 0);
673 /* fold signed to unsigned */
675 uval = (unsigned)(((-(++val)) << 1) + 1);
677 uval = (unsigned)(val << 1);
679 k = FLAC__bitmath_ilog2(parameter);
680 if(parameter == 1u<<k) {
683 FLAC__ASSERT(k <= 30);
686 total_bits = 1 + k + msbs;
687 pattern = 1 << k; /* the unary end bit */
688 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
690 if(total_bits <= 32) {
691 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
695 /* write the unary MSBs */
696 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
698 /* write the unary end bit and binary LSBs */
699 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
706 d = (1 << (k+1)) - parameter;
707 q = uval / parameter;
708 r = uval - (q * parameter);
709 /* write the unary MSBs */
710 if(!FLAC__bitwriter_write_zeroes(bw, q))
712 /* write the unary end bit */
713 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
715 /* write the binary LSBs */
717 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
721 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
728 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
730 unsigned total_bits, msbs;
733 FLAC__ASSERT(0 != bw);
734 FLAC__ASSERT(0 != bw->buffer);
735 FLAC__ASSERT(parameter > 0);
737 k = FLAC__bitmath_ilog2(parameter);
738 if(parameter == 1u<<k) {
741 FLAC__ASSERT(k <= 30);
744 total_bits = 1 + k + msbs;
745 pattern = 1 << k; /* the unary end bit */
746 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
748 if(total_bits <= 32) {
749 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
753 /* write the unary MSBs */
754 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
756 /* write the unary end bit and binary LSBs */
757 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
764 d = (1 << (k+1)) - parameter;
765 q = uval / parameter;
766 r = uval - (q * parameter);
767 /* write the unary MSBs */
768 if(!FLAC__bitwriter_write_zeroes(bw, q))
770 /* write the unary end bit */
771 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
773 /* write the binary LSBs */
775 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
779 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
787 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
791 FLAC__ASSERT(0 != bw);
792 FLAC__ASSERT(0 != bw->buffer);
794 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
797 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
799 else if(val < 0x800) {
800 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
801 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
803 else if(val < 0x10000) {
804 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 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 < 0x200000) {
809 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 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);
814 else if(val < 0x4000000) {
815 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
816 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
817 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
822 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
823 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
824 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
825 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
826 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
827 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
833 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
837 FLAC__ASSERT(0 != bw);
838 FLAC__ASSERT(0 != bw->buffer);
840 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
843 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
845 else if(val < 0x800) {
846 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
847 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
849 else if(val < 0x10000) {
850 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 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 < 0x200000) {
855 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 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 < 0x4000000) {
861 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
862 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
863 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
864 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
865 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
867 else if(val < 0x80000000) {
868 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
869 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
870 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
871 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
872 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
873 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
876 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
877 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
878 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
879 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
880 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
881 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
882 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
888 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
890 /* 0-pad to byte boundary */
892 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));