1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 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() */
38 #if defined(_MSC_VER) && _MSC_VER <= 1200
39 #include <winsock.h> /* for ntohl() */
41 #include <netinet/in.h> /* for ntohl() */
44 #include "private/bitmath.h"
46 #include "private/bitwriter.h"
47 #include "private/crc.h"
48 #include "FLAC/assert.h"
50 /* Things should be fastest when this matches the machine word size */
51 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
52 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
53 typedef FLAC__uint32 bwword;
54 #define FLAC__BYTES_PER_WORD 4
55 #define FLAC__BITS_PER_WORD 32
56 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
57 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
59 #define SWAP_BE_WORD_TO_HOST(x) (x)
61 #define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
65 * The default capacity here doesn't matter too much. The buffer always grows
66 * to hold whatever is written to it. Usually the encoder will stop adding at
67 * a frame or metadata block, then write that out and clear the buffer for the
70 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
71 /* When growing, increment 4K at a time */
72 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
74 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
75 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
80 #define min(x,y) ((x)<(y)?(x):(y))
82 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
84 #define FLAC__U64L(x) x
86 #define FLAC__U64L(x) x##LLU
93 struct FLAC__BitWriter {
95 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
96 unsigned capacity; /* capacity of buffer in words */
97 unsigned words; /* # of complete words in buffer */
98 unsigned bits; /* # of used bits in accum */
101 /* * WATCHOUT: The current implementation only grows the buffer. */
102 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
104 unsigned new_capacity;
107 FLAC__ASSERT(0 != bw);
108 FLAC__ASSERT(0 != bw->buffer);
110 /* calculate total words needed to store 'bits_to_add' additional bits */
111 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
113 /* it's possible (due to pessimism in the growth estimation that
114 * leads to this call) that we don't actually need to grow
116 if(bw->capacity >= new_capacity)
119 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
120 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
121 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
122 /* make sure we got everything right */
123 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
124 FLAC__ASSERT(new_capacity > bw->capacity);
125 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
127 new_buffer = (bwword*)realloc(bw->buffer, sizeof(bwword)*new_capacity);
130 bw->buffer = new_buffer;
131 bw->capacity = new_capacity;
136 /***********************************************************************
138 * Class constructor/destructor
140 ***********************************************************************/
142 FLAC__BitWriter *FLAC__bitwriter_new()
144 FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
145 /* note that calloc() sets all members to 0 for us */
149 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
151 FLAC__ASSERT(0 != bw);
153 FLAC__bitwriter_free(bw);
157 /***********************************************************************
159 * Public class methods
161 ***********************************************************************/
163 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
165 FLAC__ASSERT(0 != bw);
167 bw->words = bw->bits = 0;
168 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
169 bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
176 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
178 FLAC__ASSERT(0 != bw);
184 bw->words = bw->bits = 0;
187 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
189 bw->words = bw->bits = 0;
192 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
196 fprintf(out, "bitwriter is NULL\n");
199 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
201 for(i = 0; i < bw->words; i++) {
202 fprintf(out, "%08X: ", i);
203 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
204 fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
208 fprintf(out, "%08X: ", i);
209 for(j = 0; j < bw->bits; j++)
210 fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
216 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
218 const FLAC__byte *buffer;
221 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
223 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
226 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
227 FLAC__bitwriter_release_buffer(bw);
231 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
233 const FLAC__byte *buffer;
236 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
238 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
241 *crc = FLAC__crc8(buffer, bytes);
242 FLAC__bitwriter_release_buffer(bw);
246 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
248 return ((bw->bits & 7) == 0);
251 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
253 return FLAC__TOTAL_BITS(bw);
256 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
258 FLAC__ASSERT((bw->bits & 7) == 0);
259 /* double protection */
262 /* if we have bits in the accumulator we have to flush those to the buffer first */
265 if(bw->words == bw->capacity)fprintf(stderr,"@@@@@@ DEBUG:resizing in FLAC__bitwriter_get_buffer()\n");
267 FLAC__ASSERT(bw->words <= bw->capacity);
268 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
270 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
271 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
273 /* now we can just return what we have */
274 *buffer = (FLAC__byte*)bw->buffer;
275 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
279 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
281 /* nothing to do. in the future, strict checking of a 'writer-is-in-
282 * get-mode' flag could be added everywhere and then cleared here
287 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
291 FLAC__ASSERT(0 != bw);
292 FLAC__ASSERT(0 != bw->buffer);
296 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
297 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
299 /* first part gets to word alignment */
301 n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
305 if(bw->bits == FLAC__BITS_PER_WORD) {
306 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
313 while(bits >= FLAC__BITS_PER_WORD) {
314 bw->buffer[bw->words++] = 0;
315 bits -= FLAC__BITS_PER_WORD;
317 /* do any leftovers */
325 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
327 register unsigned left;
329 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
330 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
332 FLAC__ASSERT(0 != bw);
333 FLAC__ASSERT(0 != bw->buffer);
335 FLAC__ASSERT(bits <= 32);
339 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
340 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
343 left = FLAC__BITS_PER_WORD - bw->bits;
349 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 */
351 if(left>=FLAC__BITS_PER_WORD)fprintf(stderr,"@@@@@@ bitwriter shift error @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
354 bw->accum |= val >> (bw->bits = bits - left);
355 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
360 if(bits!=left)fprintf(stderr,"@@@@@@ bitwriter shift error2 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
364 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
370 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
372 /* zero-out unused bits */
374 val &= (~(0xffffffff << bits));
376 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
379 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
381 /* this could be a little faster but it's not used for much */
384 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
385 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
388 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
391 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
393 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
395 if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
397 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
399 if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
401 if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
407 FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
411 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
412 for(i = 0; i < nvals; i++) {
413 if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
420 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
423 return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
426 FLAC__bitwriter_write_zeroes(bw, val) &&
427 FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
430 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
434 FLAC__ASSERT(parameter < sizeof(unsigned)*8);
436 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
437 uval = (val<<1) ^ (val>>31);
439 return 1 + parameter + (uval >> parameter);
443 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
445 unsigned bits, msbs, uval;
448 FLAC__ASSERT(parameter > 0);
450 /* fold signed to unsigned */
452 uval = (unsigned)(((-(++val)) << 1) + 1);
454 uval = (unsigned)(val << 1);
456 k = FLAC__bitmath_ilog2(parameter);
457 if(parameter == 1u<<k) {
458 FLAC__ASSERT(k <= 30);
466 d = (1 << (k+1)) - parameter;
467 q = uval / parameter;
468 r = uval - (q * parameter);
477 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
482 FLAC__ASSERT(parameter > 0);
484 k = FLAC__bitmath_ilog2(parameter);
485 if(parameter == 1u<<k) {
486 FLAC__ASSERT(k <= 30);
494 d = (1 << (k+1)) - parameter;
495 q = uval / parameter;
496 r = uval - (q * parameter);
506 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
508 unsigned total_bits, interesting_bits, msbs;
509 FLAC__uint32 uval, pattern;
511 FLAC__ASSERT(0 != bw);
512 FLAC__ASSERT(0 != bw->buffer);
513 FLAC__ASSERT(parameter < 8*sizeof(uval));
515 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
516 uval = (val<<1) ^ (val>>31);
518 msbs = uval >> parameter;
519 interesting_bits = 1 + parameter;
520 total_bits = interesting_bits + msbs;
521 pattern = 1 << parameter; /* the unary end bit */
522 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
525 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
528 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
529 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
532 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
534 const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
535 const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
537 register unsigned left;
538 const unsigned lsbits = 1 + parameter;
541 FLAC__ASSERT(0 != bw);
542 FLAC__ASSERT(0 != bw->buffer);
543 FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
544 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
545 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
548 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
549 uval = (*vals<<1) ^ (*vals>>31);
551 msbits = uval >> parameter;
553 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
554 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
555 if(bw->capacity <= bw->words + bw->bits + msbits + lsbits && !bitwriter_grow_(bw, msbits+lsbits))
559 /* first part gets to word alignment */
561 left = min(FLAC__BITS_PER_WORD - bw->bits, msbits);
565 if(bw->bits == FLAC__BITS_PER_WORD) {
566 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
573 while(msbits >= FLAC__BITS_PER_WORD) {
574 bw->buffer[bw->words++] = 0;
575 msbits -= FLAC__BITS_PER_WORD;
577 /* do any leftovers */
584 uval |= mask1; /* set stop bit */
585 uval &= mask2; /* mask off unused top bits */
587 left = FLAC__BITS_PER_WORD - bw->bits;
589 bw->accum <<= lsbits;
594 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
595 * be > lsbits (because of previous assertions) so it would have
596 * triggered the (lsbits<left) case above.
598 FLAC__ASSERT(bw->bits);
600 if(left>=FLAC__BITS_PER_WORD)fprintf(stderr,"@@@@@@ bitwriter shift error @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
603 bw->accum |= uval >> (bw->bits = lsbits - left);
604 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
614 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
616 unsigned total_bits, msbs, uval;
619 FLAC__ASSERT(0 != bw);
620 FLAC__ASSERT(0 != bw->buffer);
621 FLAC__ASSERT(parameter > 0);
623 /* fold signed to unsigned */
625 uval = (unsigned)(((-(++val)) << 1) + 1);
627 uval = (unsigned)(val << 1);
629 k = FLAC__bitmath_ilog2(parameter);
630 if(parameter == 1u<<k) {
633 FLAC__ASSERT(k <= 30);
636 total_bits = 1 + k + msbs;
637 pattern = 1 << k; /* the unary end bit */
638 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
640 if(total_bits <= 32) {
641 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
645 /* write the unary MSBs */
646 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
648 /* write the unary end bit and binary LSBs */
649 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
656 d = (1 << (k+1)) - parameter;
657 q = uval / parameter;
658 r = uval - (q * parameter);
659 /* write the unary MSBs */
660 if(!FLAC__bitwriter_write_zeroes(bw, q))
662 /* write the unary end bit */
663 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
665 /* write the binary LSBs */
667 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
671 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
678 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
680 unsigned total_bits, msbs;
683 FLAC__ASSERT(0 != bw);
684 FLAC__ASSERT(0 != bw->buffer);
685 FLAC__ASSERT(parameter > 0);
687 k = FLAC__bitmath_ilog2(parameter);
688 if(parameter == 1u<<k) {
691 FLAC__ASSERT(k <= 30);
694 total_bits = 1 + k + msbs;
695 pattern = 1 << k; /* the unary end bit */
696 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
698 if(total_bits <= 32) {
699 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
703 /* write the unary MSBs */
704 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
706 /* write the unary end bit and binary LSBs */
707 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
714 d = (1 << (k+1)) - parameter;
715 q = uval / parameter;
716 r = uval - (q * parameter);
717 /* write the unary MSBs */
718 if(!FLAC__bitwriter_write_zeroes(bw, q))
720 /* write the unary end bit */
721 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
723 /* write the binary LSBs */
725 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
729 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
737 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
741 FLAC__ASSERT(0 != bw);
742 FLAC__ASSERT(0 != bw->buffer);
744 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
747 return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
749 else if(val < 0x800) {
750 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
751 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
753 else if(val < 0x10000) {
754 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
755 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
756 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
758 else if(val < 0x200000) {
759 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
760 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 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 < 0x4000000) {
765 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
766 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
767 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
768 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
769 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
772 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
773 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
774 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
775 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
776 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
777 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
783 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
787 FLAC__ASSERT(0 != bw);
788 FLAC__ASSERT(0 != bw->buffer);
790 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
793 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
795 else if(val < 0x800) {
796 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
797 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
799 else if(val < 0x10000) {
800 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
801 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
802 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
804 else if(val < 0x200000) {
805 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
806 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 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 < 0x4000000) {
811 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
812 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
813 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
814 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
815 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
817 else if(val < 0x80000000) {
818 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
820 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
821 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
822 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
823 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
826 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
827 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
828 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
829 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
830 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
831 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
832 ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
838 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
840 /* 0-pad to byte boundary */
842 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));