1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "private/bitwriter.h"
40 #include "private/crc.h"
41 #include "private/macros.h"
42 #include "FLAC/assert.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "share/endswap.h"
47 /* Things should be fastest when this matches the machine word size */
48 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
49 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
51 #if (ENABLE_64_BIT_WORDS == 0)
53 typedef FLAC__uint32 bwword;
54 #define FLAC__BYTES_PER_WORD 4 /* sizeof bwword */
55 #define FLAC__BITS_PER_WORD 32
56 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
58 #define SWAP_BE_WORD_TO_HOST(x) (x)
60 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
65 typedef FLAC__uint64 bwword;
66 #define FLAC__BYTES_PER_WORD 8 /* sizeof bwword */
67 #define FLAC__BITS_PER_WORD 64
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)
72 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x)
78 * The default capacity here doesn't matter too much. The buffer always grows
79 * to hold whatever is written to it. Usually the encoder will stop adding at
80 * a frame or metadata block, then write that out and clear the buffer for the
83 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
84 /* When growing, increment 4K at a time */
85 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
87 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
88 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
90 struct FLAC__BitWriter {
92 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
93 unsigned capacity; /* capacity of buffer in words */
94 unsigned words; /* # of complete words in buffer */
95 unsigned bits; /* # of used bits in accum */
98 /* * WATCHOUT: The current implementation only grows the buffer. */
102 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 = safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
130 bw->buffer = new_buffer;
131 bw->capacity = new_capacity;
136 /***********************************************************************
138 * Class constructor/destructor
140 ***********************************************************************/
142 FLAC__BitWriter *FLAC__bitwriter_new(void)
144 FLAC__BitWriter *bw = 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 = 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] & ((bwword)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 & ((bwword)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 */
264 FLAC__ASSERT(bw->words <= bw->capacity);
265 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
267 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
268 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
270 /* now we can just return what we have */
271 *buffer = (FLAC__byte*)bw->buffer;
272 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
276 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
278 /* nothing to do. in the future, strict checking of a 'writer-is-in-
279 * get-mode' flag could be added everywhere and then cleared here
284 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
288 FLAC__ASSERT(0 != bw);
289 FLAC__ASSERT(0 != bw->buffer);
293 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
294 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
296 /* first part gets to word alignment */
298 n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
302 if(bw->bits == FLAC__BITS_PER_WORD) {
303 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
310 while(bits >= FLAC__BITS_PER_WORD) {
311 bw->buffer[bw->words++] = 0;
312 bits -= FLAC__BITS_PER_WORD;
314 /* do any leftovers */
322 static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
324 register unsigned left;
326 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
327 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
329 if(bw == 0 || bw->buffer == 0)
338 FLAC__ASSERT((bits == 32) || (val>>bits == 0));
340 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
341 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
344 left = FLAC__BITS_PER_WORD - bw->bits;
350 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 */
352 bw->accum |= val >> (bw->bits = bits - left);
353 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
354 bw->accum = val; /* unused top bits can contain garbage */
356 else { /* at this point bits == FLAC__BITS_PER_WORD == 32 and bw->bits == 0 */
357 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val);
363 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
365 /* check that unused bits are unset */
366 if((bits < 32) && (val>>bits != 0))
369 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
372 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
374 /* zero-out unused bits */
376 val &= (~(0xffffffff << bits));
378 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
381 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
383 /* this could be a little faster but it's not used for much */
386 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
387 FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32);
390 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
393 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
395 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
397 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
399 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
401 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
403 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
409 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
413 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
414 for(i = 0; i < nvals; i++) {
415 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8))
422 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
425 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
428 FLAC__bitwriter_write_zeroes(bw, val) &&
429 FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
432 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
436 FLAC__ASSERT(parameter < 32);
438 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
443 return 1 + parameter + (uval >> parameter);
447 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
449 unsigned bits, msbs, uval;
452 FLAC__ASSERT(parameter > 0);
454 /* fold signed to unsigned */
456 uval = (unsigned)(((-(++val)) << 1) + 1);
458 uval = (unsigned)(val << 1);
460 k = FLAC__bitmath_ilog2(parameter);
461 if(parameter == 1u<<k) {
462 FLAC__ASSERT(k <= 30);
470 d = (1 << (k+1)) - parameter;
471 q = uval / parameter;
472 r = uval - (q * parameter);
481 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
486 FLAC__ASSERT(parameter > 0);
488 k = FLAC__bitmath_ilog2(parameter);
489 if(parameter == 1u<<k) {
490 FLAC__ASSERT(k <= 30);
498 d = (1 << (k+1)) - parameter;
499 q = uval / parameter;
500 r = uval - (q * parameter);
510 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
512 unsigned total_bits, interesting_bits, msbs;
513 FLAC__uint32 uval, pattern;
515 FLAC__ASSERT(0 != bw);
516 FLAC__ASSERT(0 != bw->buffer);
517 FLAC__ASSERT(parameter < 32);
519 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
524 msbs = uval >> parameter;
525 interesting_bits = 1 + parameter;
526 total_bits = interesting_bits + msbs;
527 pattern = 1 << parameter; /* the unary end bit */
528 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
531 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
534 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
535 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
538 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
540 const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */
541 const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */
544 const unsigned lsbits = 1 + parameter;
545 unsigned msbits, total_bits;
547 FLAC__ASSERT(0 != bw);
548 FLAC__ASSERT(0 != bw->buffer);
549 FLAC__ASSERT(parameter < 31);
550 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
551 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
554 /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
559 msbits = uval >> parameter;
560 total_bits = lsbits + msbits;
562 if(bw->bits && bw->bits + total_bits < 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 += total_bits;
565 uval |= mask1; /* set stop bit */
566 uval &= mask2; /* mask off unused top bits */
567 bw->accum <<= total_bits;
571 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
572 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
573 if(bw->capacity <= bw->words + bw->bits + msbits + 1 /* lsbits always fit in 1 bwword */ && !bitwriter_grow_(bw, total_bits))
577 /* first part gets to word alignment */
579 left = FLAC__BITS_PER_WORD - bw->bits;
581 bw->accum <<= msbits;
588 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
593 while(msbits >= FLAC__BITS_PER_WORD) {
594 bw->buffer[bw->words++] = 0;
595 msbits -= FLAC__BITS_PER_WORD;
597 /* do any leftovers */
604 uval |= mask1; /* set stop bit */
605 uval &= mask2; /* mask off unused top bits */
607 left = FLAC__BITS_PER_WORD - bw->bits;
609 bw->accum <<= lsbits;
614 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
615 * be > lsbits (because of previous assertions) so it would have
616 * triggered the (lsbits<left) case above.
618 FLAC__ASSERT(bw->bits);
619 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
621 bw->accum |= uval >> (bw->bits = lsbits - left);
622 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
623 bw->accum = uval; /* unused top bits can contain garbage */
633 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
635 unsigned total_bits, msbs, uval;
638 FLAC__ASSERT(0 != bw);
639 FLAC__ASSERT(0 != bw->buffer);
640 FLAC__ASSERT(parameter > 0);
642 /* fold signed to unsigned */
644 uval = (unsigned)(((-(++val)) << 1) + 1);
646 uval = (unsigned)(val << 1);
648 k = FLAC__bitmath_ilog2(parameter);
649 if(parameter == 1u<<k) {
652 FLAC__ASSERT(k <= 30);
655 total_bits = 1 + k + msbs;
656 pattern = 1 << k; /* the unary end bit */
657 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
659 if(total_bits <= 32) {
660 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
664 /* write the unary MSBs */
665 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
667 /* write the unary end bit and binary LSBs */
668 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
675 d = (1 << (k+1)) - parameter;
676 q = uval / parameter;
677 r = uval - (q * parameter);
678 /* write the unary MSBs */
679 if(!FLAC__bitwriter_write_zeroes(bw, q))
681 /* write the unary end bit */
682 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
684 /* write the binary LSBs */
686 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
690 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
697 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
699 unsigned total_bits, msbs;
702 FLAC__ASSERT(0 != bw);
703 FLAC__ASSERT(0 != bw->buffer);
704 FLAC__ASSERT(parameter > 0);
706 k = FLAC__bitmath_ilog2(parameter);
707 if(parameter == 1u<<k) {
710 FLAC__ASSERT(k <= 30);
713 total_bits = 1 + k + msbs;
714 pattern = 1 << k; /* the unary end bit */
715 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
717 if(total_bits <= 32) {
718 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
722 /* write the unary MSBs */
723 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
725 /* write the unary end bit and binary LSBs */
726 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
733 d = (1 << (k+1)) - parameter;
734 q = uval / parameter;
735 r = uval - (q * parameter);
736 /* write the unary MSBs */
737 if(!FLAC__bitwriter_write_zeroes(bw, q))
739 /* write the unary end bit */
740 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
742 /* write the binary LSBs */
744 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
748 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
756 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
760 FLAC__ASSERT(0 != bw);
761 FLAC__ASSERT(0 != bw->buffer);
763 if((val & 0x80000000) != 0) /* this version only handles 31 bits */
767 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
769 else if(val < 0x800) {
770 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8);
771 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
773 else if(val < 0x10000) {
774 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8);
775 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
776 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
778 else if(val < 0x200000) {
779 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8);
780 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
781 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
782 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
784 else if(val < 0x4000000) {
785 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8);
786 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
787 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
788 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
789 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
792 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8);
793 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8);
794 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
795 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
796 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
797 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
803 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
807 FLAC__ASSERT(0 != bw);
808 FLAC__ASSERT(0 != bw->buffer);
810 if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
814 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
816 else if(val < 0x800) {
817 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
820 else if(val < 0x10000) {
821 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
822 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
823 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
825 else if(val < 0x200000) {
826 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
827 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
828 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
829 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
831 else if(val < 0x4000000) {
832 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
833 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
834 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
835 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
836 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
838 else if(val < 0x80000000) {
839 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
840 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
841 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
842 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
843 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
844 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
847 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8);
848 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
849 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
850 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
851 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
852 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
853 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
859 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
861 /* 0-pad to byte boundary */
863 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
868 /* These functions are declared inline in this file but are also callable as
869 * externs from elsewhere.
870 * According to the C99 spec, section 6.7.4, simply providing a function
871 * prototype in a header file without 'inline' and making the function inline
872 * in this file should be sufficient.
873 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
874 * fix that we add extern declarations here.
876 extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits);
877 extern FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits);
878 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
879 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
880 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
881 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);