1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2022 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/format.h"
42 #include "private/macros.h"
43 #include "private/stream_encoder.h"
44 #include "FLAC/assert.h"
45 #include "share/alloc.h"
46 #include "share/compat.h"
47 #include "share/endswap.h"
49 /* Things should be fastest when this matches the machine word size */
50 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
51 /* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
53 #if (ENABLE_64_BIT_WORDS == 0)
55 typedef FLAC__uint32 bwword;
56 #define FLAC__BYTES_PER_WORD 4 /* sizeof bwword */
57 #define FLAC__BITS_PER_WORD 32
58 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
60 #define SWAP_BE_WORD_TO_HOST(x) (x)
62 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
67 typedef FLAC__uint64 bwword;
68 #define FLAC__BYTES_PER_WORD 8 /* sizeof bwword */
69 #define FLAC__BITS_PER_WORD 64
70 /* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
72 #define SWAP_BE_WORD_TO_HOST(x) (x)
74 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(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 uint32_t FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
86 /* When growing, increment 4K at a time */
87 static const uint32_t 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)
92 struct FLAC__BitWriter {
94 bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
95 uint32_t capacity; /* capacity of buffer in words */
96 uint32_t words; /* # of complete words in buffer */
97 uint32_t bits; /* # of used bits in accum */
100 /* * WATCHOUT: The current implementation only grows the buffer. */
104 FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
106 uint32_t new_capacity;
109 FLAC__ASSERT(0 != bw);
110 FLAC__ASSERT(0 != bw->buffer);
112 /* calculate total words needed to store 'bits_to_add' additional bits */
113 new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
115 /* it's possible (due to pessimism in the growth estimation that
116 * leads to this call) that we don't actually need to grow
118 if(bw->capacity >= new_capacity)
121 if(new_capacity * sizeof(bwword) > (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
122 /* Requested new capacity is larger than the largest possible metadata block,
123 * which is also larger than the largest sane framesize. That means something
124 * went very wrong somewhere and previous checks failed.
125 * To prevent chrashing, give up */
128 /* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
129 if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
130 new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
131 /* make sure we got everything right */
132 FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
133 FLAC__ASSERT(new_capacity > bw->capacity);
134 FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
136 new_buffer = safe_realloc_nofree_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
139 bw->buffer = new_buffer;
140 bw->capacity = new_capacity;
145 /***********************************************************************
147 * Class constructor/destructor
149 ***********************************************************************/
151 FLAC__BitWriter *FLAC__bitwriter_new(void)
153 FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
154 /* note that calloc() sets all members to 0 for us */
158 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
160 FLAC__ASSERT(0 != bw);
162 FLAC__bitwriter_free(bw);
166 /***********************************************************************
168 * Public class methods
170 ***********************************************************************/
172 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
174 FLAC__ASSERT(0 != bw);
176 bw->words = bw->bits = 0;
177 bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
178 bw->buffer = malloc(sizeof(bwword) * bw->capacity);
185 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
187 FLAC__ASSERT(0 != bw);
193 bw->words = bw->bits = 0;
196 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
198 bw->words = bw->bits = 0;
201 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
205 fprintf(out, "bitwriter is NULL\n");
208 fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
210 for(i = 0; i < bw->words; i++) {
211 fprintf(out, "%08X: ", i);
212 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
213 fprintf(out, "%01d", bw->buffer[i] & ((bwword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
217 fprintf(out, "%08X: ", i);
218 for(j = 0; j < bw->bits; j++)
219 fprintf(out, "%01d", bw->accum & ((bwword)1 << (bw->bits-j-1)) ? 1:0);
225 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
227 const FLAC__byte *buffer;
230 FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
232 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
235 *crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
236 FLAC__bitwriter_release_buffer(bw);
240 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *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__crc8(buffer, bytes);
251 FLAC__bitwriter_release_buffer(bw);
255 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
257 return ((bw->bits & 7) == 0);
260 uint32_t FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
262 return FLAC__TOTAL_BITS(bw);
265 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
267 FLAC__ASSERT((bw->bits & 7) == 0);
268 /* double protection */
271 /* if we have bits in the accumulator we have to flush those to the buffer first */
273 FLAC__ASSERT(bw->words <= bw->capacity);
274 if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
276 /* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
277 bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
279 /* now we can just return what we have */
280 *buffer = (FLAC__byte*)bw->buffer;
281 *bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
285 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
287 /* nothing to do. in the future, strict checking of a 'writer-is-in-
288 * get-mode' flag could be added everywhere and then cleared here
293 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits)
297 FLAC__ASSERT(0 != bw);
298 FLAC__ASSERT(0 != bw->buffer);
302 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
303 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
305 /* first part gets to word alignment */
307 n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
311 if(bw->bits == FLAC__BITS_PER_WORD) {
312 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
319 while(bits >= FLAC__BITS_PER_WORD) {
320 bw->buffer[bw->words++] = 0;
321 bits -= FLAC__BITS_PER_WORD;
323 /* do any leftovers */
331 static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
333 register uint32_t left;
335 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
336 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
338 if(bw == 0 || bw->buffer == 0)
347 FLAC__ASSERT((bits == 32) || (val>>bits == 0));
349 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
350 if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
353 left = FLAC__BITS_PER_WORD - bw->bits;
359 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 */
361 bw->accum |= val >> (bw->bits = bits - left);
362 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
363 bw->accum = val; /* unused top bits can contain garbage */
365 else { /* at this point bits == FLAC__BITS_PER_WORD == 32 and bw->bits == 0 */
366 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST((bwword)val);
372 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits)
374 /* check that unused bits are unset */
375 if((bits < 32) && (val>>bits != 0))
378 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
381 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits)
383 /* zero-out unused bits */
385 val &= (~(0xffffffff << bits));
387 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
390 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits)
392 /* this could be a little faster but it's not used for much */
395 FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
396 FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 32);
399 return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
402 inline FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits)
404 FLAC__uint64 uval = val;
405 /* zero-out unused bits */
407 uval &= (~(UINT64_MAX << bits));
408 return FLAC__bitwriter_write_raw_uint64(bw, uval, bits);
411 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
413 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
415 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
417 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
419 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
421 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
427 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals)
431 /* grow capacity upfront to prevent constant reallocation during writes */
432 if(bw->capacity <= bw->words + nvals / (FLAC__BITS_PER_WORD / 8) + 1 && !bitwriter_grow_(bw, nvals * 8))
435 /* this could be faster but currently we don't need it to be since it's only used for writing metadata */
436 for(i = 0; i < nvals; i++) {
437 if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)(vals[i]), 8))
444 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, uint32_t val)
447 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
450 FLAC__bitwriter_write_zeroes(bw, val) &&
451 FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
454 uint32_t FLAC__bitwriter_rice_bits(FLAC__int32 val, uint32_t parameter)
458 FLAC__ASSERT(parameter < 32);
460 /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
465 return 1 + parameter + (uval >> parameter);
469 uint32_t FLAC__bitwriter_golomb_bits_signed(int val, uint32_t parameter)
471 uint32_t bits, msbs, uval;
474 FLAC__ASSERT(parameter > 0);
476 /* fold signed to uint32_t */
478 uval = (uint32_t)(((-(++val)) << 1) + 1);
480 uval = (uint32_t)(val << 1);
482 k = FLAC__bitmath_ilog2(parameter);
483 if(parameter == 1u<<k) {
484 FLAC__ASSERT(k <= 30);
492 d = (1 << (k+1)) - parameter;
493 q = uval / parameter;
494 r = uval - (q * parameter);
503 uint32_t FLAC__bitwriter_golomb_bits_unsigned(uint32_t uval, uint32_t parameter)
508 FLAC__ASSERT(parameter > 0);
510 k = FLAC__bitmath_ilog2(parameter);
511 if(parameter == 1u<<k) {
512 FLAC__ASSERT(k <= 30);
520 d = (1 << (k+1)) - parameter;
521 q = uval / parameter;
522 r = uval - (q * parameter);
532 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t parameter)
534 uint32_t total_bits, interesting_bits, msbs;
535 FLAC__uint32 uval, pattern;
537 FLAC__ASSERT(0 != bw);
538 FLAC__ASSERT(0 != bw->buffer);
539 FLAC__ASSERT(parameter < 32);
541 /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
546 msbs = uval >> parameter;
547 interesting_bits = 1 + parameter;
548 total_bits = interesting_bits + msbs;
549 pattern = 1 << parameter; /* the unary end bit */
550 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
553 return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
556 FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
557 FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
560 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, uint32_t nvals, uint32_t parameter)
562 const FLAC__uint32 mask1 = (FLAC__uint32)0xffffffff << parameter; /* we val|=mask1 to set the stop bit above it... */
563 const FLAC__uint32 mask2 = (FLAC__uint32)0xffffffff >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2 */
566 const uint32_t lsbits = 1 + parameter;
567 uint32_t msbits, total_bits;
569 FLAC__ASSERT(0 != bw);
570 FLAC__ASSERT(0 != bw->buffer);
571 FLAC__ASSERT(parameter < 31);
572 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
573 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
576 /* fold signed to uint32_t; actual formula is: negative(v)? -2v-1 : 2v */
581 msbits = uval >> parameter;
582 total_bits = lsbits + msbits;
584 if(bw->bits && bw->bits + total_bits < 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 += total_bits;
587 uval |= mask1; /* set stop bit */
588 uval &= mask2; /* mask off unused top bits */
589 bw->accum <<= total_bits;
593 /* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
594 /* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
595 if(bw->capacity <= bw->words + bw->bits + msbits + 1 /* lsbits always fit in 1 bwword */ && !bitwriter_grow_(bw, total_bits))
599 /* first part gets to word alignment */
601 left = FLAC__BITS_PER_WORD - bw->bits;
603 bw->accum <<= msbits;
610 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
615 while(msbits >= FLAC__BITS_PER_WORD) {
616 bw->buffer[bw->words++] = 0;
617 msbits -= FLAC__BITS_PER_WORD;
619 /* do any leftovers */
626 uval |= mask1; /* set stop bit */
627 uval &= mask2; /* mask off unused top bits */
629 left = FLAC__BITS_PER_WORD - bw->bits;
631 bw->accum <<= lsbits;
636 /* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
637 * be > lsbits (because of previous assertions) so it would have
638 * triggered the (lsbits<left) case above.
640 FLAC__ASSERT(bw->bits);
641 FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
643 bw->accum |= uval >> (bw->bits = lsbits - left);
644 bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
645 bw->accum = uval; /* unused top bits can contain garbage */
655 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, uint32_t parameter)
657 uint32_t total_bits, msbs, uval;
660 FLAC__ASSERT(0 != bw);
661 FLAC__ASSERT(0 != bw->buffer);
662 FLAC__ASSERT(parameter > 0);
664 /* fold signed to uint32_t */
666 uval = (uint32_t)(((-(++val)) << 1) + 1);
668 uval = (uint32_t)(val << 1);
670 k = FLAC__bitmath_ilog2(parameter);
671 if(parameter == 1u<<k) {
674 FLAC__ASSERT(k <= 30);
677 total_bits = 1 + k + msbs;
678 pattern = 1 << k; /* the unary end bit */
679 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
681 if(total_bits <= 32) {
682 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
686 /* write the unary MSBs */
687 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
689 /* write the unary end bit and binary LSBs */
690 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
697 d = (1 << (k+1)) - parameter;
698 q = uval / parameter;
699 r = uval - (q * parameter);
700 /* write the unary MSBs */
701 if(!FLAC__bitwriter_write_zeroes(bw, q))
703 /* write the unary end bit */
704 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
706 /* write the binary LSBs */
708 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
712 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
719 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, uint32_t uval, uint32_t parameter)
721 uint32_t total_bits, msbs;
724 FLAC__ASSERT(0 != bw);
725 FLAC__ASSERT(0 != bw->buffer);
726 FLAC__ASSERT(parameter > 0);
728 k = FLAC__bitmath_ilog2(parameter);
729 if(parameter == 1u<<k) {
732 FLAC__ASSERT(k <= 30);
735 total_bits = 1 + k + msbs;
736 pattern = 1 << k; /* the unary end bit */
737 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
739 if(total_bits <= 32) {
740 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
744 /* write the unary MSBs */
745 if(!FLAC__bitwriter_write_zeroes(bw, msbs))
747 /* write the unary end bit and binary LSBs */
748 if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
755 d = (1 << (k+1)) - parameter;
756 q = uval / parameter;
757 r = uval - (q * parameter);
758 /* write the unary MSBs */
759 if(!FLAC__bitwriter_write_zeroes(bw, q))
761 /* write the unary end bit */
762 if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
764 /* write the binary LSBs */
766 if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
770 if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
778 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
782 FLAC__ASSERT(0 != bw);
783 FLAC__ASSERT(0 != bw->buffer);
785 if((val & 0x80000000) != 0) /* this version only handles 31 bits */
789 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
791 else if(val < 0x800) {
792 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (val>>6), 8);
793 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
795 else if(val < 0x10000) {
796 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (val>>12), 8);
797 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
798 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
800 else if(val < 0x200000) {
801 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (val>>18), 8);
802 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
803 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
804 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
806 else if(val < 0x4000000) {
807 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (val>>24), 8);
808 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
809 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
810 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
811 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
814 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (val>>30), 8);
815 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>24)&0x3F), 8);
816 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>18)&0x3F), 8);
817 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>12)&0x3F), 8);
818 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | ((val>>6)&0x3F), 8);
819 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (val&0x3F), 8);
825 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
829 FLAC__ASSERT(0 != bw);
830 FLAC__ASSERT(0 != bw->buffer);
832 if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
836 return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
838 else if(val < 0x800) {
839 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
840 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
842 else if(val < 0x10000) {
843 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
844 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
845 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
847 else if(val < 0x200000) {
848 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
849 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
850 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
851 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
853 else if(val < 0x4000000) {
854 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
855 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
856 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
857 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
858 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
860 else if(val < 0x80000000) {
861 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
862 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
863 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
864 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
865 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
866 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
869 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0xFE, 8);
870 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
871 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
872 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
873 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
874 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
875 ok &= FLAC__bitwriter_write_raw_uint32_nocheck(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
881 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
883 /* 0-pad to byte boundary */
885 return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
890 /* These functions are declared inline in this file but are also callable as
891 * externs from elsewhere.
892 * According to the C99 spec, section 6.7.4, simply providing a function
893 * prototype in a header file without 'inline' and making the function inline
894 * in this file should be sufficient.
895 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
896 * fix that we add extern declarations here.
898 extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, uint32_t bits);
899 extern FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, uint32_t bits);
900 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, uint32_t bits);
901 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, uint32_t bits);
902 extern FLAC__bool FLAC__bitwriter_write_raw_int64(FLAC__BitWriter *bw, FLAC__int64 val, uint32_t bits);
903 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
904 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], uint32_t nvals);