Home | History | Annotate | Download | only in libFLAC
      1 /* libFLAC - Free Lossless Audio Codec library
      2  * Copyright (C) 2000-2009  Josh Coalson
      3  * Copyright (C) 2011-2016  Xiph.Org Foundation
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * - Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *
     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.
     15  *
     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.
     19  *
     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.
     31  */
     32 
     33 #ifdef HAVE_CONFIG_H
     34 #  include <config.h>
     35 #endif
     36 
     37 #include <stdlib.h>
     38 #include <string.h>
     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"
     46 
     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 */
     50 
     51 #if (ENABLE_64_BIT_WORDS == 0)
     52 
     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 */
     57 #if WORDS_BIGENDIAN
     58 #define SWAP_BE_WORD_TO_HOST(x) (x)
     59 #else
     60 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
     61 #endif
     62 
     63 #else
     64 
     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 */
     69 #if WORDS_BIGENDIAN
     70 #define SWAP_BE_WORD_TO_HOST(x) (x)
     71 #else
     72 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x)
     73 #endif
     74 
     75 #endif
     76 
     77 /*
     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
     81  * next one.
     82  */
     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 */
     86 
     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)
     89 
     90 struct FLAC__BitWriter {
     91 	bwword *buffer;
     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 */
     96 };
     97 
     98 /* * WATCHOUT: The current implementation only grows the buffer. */
     99 #ifndef __SUNPRO_C
    100 static
    101 #endif
    102 FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
    103 {
    104 	unsigned new_capacity;
    105 	bwword *new_buffer;
    106 
    107 	FLAC__ASSERT(0 != bw);
    108 	FLAC__ASSERT(0 != bw->buffer);
    109 
    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);
    112 
    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
    115 	 */
    116 	if(bw->capacity >= new_capacity)
    117 		return true;
    118 
    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));
    126 
    127 	new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
    128 	if(new_buffer == 0)
    129 		return false;
    130 	bw->buffer = new_buffer;
    131 	bw->capacity = new_capacity;
    132 	return true;
    133 }
    134 
    135 
    136 /***********************************************************************
    137  *
    138  * Class constructor/destructor
    139  *
    140  ***********************************************************************/
    141 
    142 FLAC__BitWriter *FLAC__bitwriter_new(void)
    143 {
    144 	FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
    145 	/* note that calloc() sets all members to 0 for us */
    146 	return bw;
    147 }
    148 
    149 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
    150 {
    151 	FLAC__ASSERT(0 != bw);
    152 
    153 	FLAC__bitwriter_free(bw);
    154 	free(bw);
    155 }
    156 
    157 /***********************************************************************
    158  *
    159  * Public class methods
    160  *
    161  ***********************************************************************/
    162 
    163 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
    164 {
    165 	FLAC__ASSERT(0 != bw);
    166 
    167 	bw->words = bw->bits = 0;
    168 	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
    169 	bw->buffer = malloc(sizeof(bwword) * bw->capacity);
    170 	if(bw->buffer == 0)
    171 		return false;
    172 
    173 	return true;
    174 }
    175 
    176 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
    177 {
    178 	FLAC__ASSERT(0 != bw);
    179 
    180 	if(0 != bw->buffer)
    181 		free(bw->buffer);
    182 	bw->buffer = 0;
    183 	bw->capacity = 0;
    184 	bw->words = bw->bits = 0;
    185 }
    186 
    187 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
    188 {
    189 	bw->words = bw->bits = 0;
    190 }
    191 
    192 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
    193 {
    194 	unsigned i, j;
    195 	if(bw == 0) {
    196 		fprintf(out, "bitwriter is NULL\n");
    197 	}
    198 	else {
    199 		fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
    200 
    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);
    205 			fprintf(out, "\n");
    206 		}
    207 		if(bw->bits > 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);
    211 			fprintf(out, "\n");
    212 		}
    213 	}
    214 }
    215 
    216 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
    217 {
    218 	const FLAC__byte *buffer;
    219 	size_t bytes;
    220 
    221 	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
    222 
    223 	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
    224 		return false;
    225 
    226 	*crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
    227 	FLAC__bitwriter_release_buffer(bw);
    228 	return true;
    229 }
    230 
    231 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
    232 {
    233 	const FLAC__byte *buffer;
    234 	size_t bytes;
    235 
    236 	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
    237 
    238 	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
    239 		return false;
    240 
    241 	*crc = FLAC__crc8(buffer, bytes);
    242 	FLAC__bitwriter_release_buffer(bw);
    243 	return true;
    244 }
    245 
    246 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
    247 {
    248 	return ((bw->bits & 7) == 0);
    249 }
    250 
    251 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
    252 {
    253 	return FLAC__TOTAL_BITS(bw);
    254 }
    255 
    256 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
    257 {
    258 	FLAC__ASSERT((bw->bits & 7) == 0);
    259 	/* double protection */
    260 	if(bw->bits & 7)
    261 		return false;
    262 	/* if we have bits in the accumulator we have to flush those to the buffer first */
    263 	if(bw->bits) {
    264 		FLAC__ASSERT(bw->words <= bw->capacity);
    265 		if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
    266 			return false;
    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));
    269 	}
    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);
    273 	return true;
    274 }
    275 
    276 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
    277 {
    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
    280 	 */
    281 	(void)bw;
    282 }
    283 
    284 inline FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
    285 {
    286 	unsigned n;
    287 
    288 	FLAC__ASSERT(0 != bw);
    289 	FLAC__ASSERT(0 != bw->buffer);
    290 
    291 	if(bits == 0)
    292 		return true;
    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))
    295 		return false;
    296 	/* first part gets to word alignment */
    297 	if(bw->bits) {
    298 		n = flac_min(FLAC__BITS_PER_WORD - bw->bits, bits);
    299 		bw->accum <<= n;
    300 		bits -= n;
    301 		bw->bits += n;
    302 		if(bw->bits == FLAC__BITS_PER_WORD) {
    303 			bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
    304 			bw->bits = 0;
    305 		}
    306 		else
    307 			return true;
    308 	}
    309 	/* do whole words */
    310 	while(bits >= FLAC__BITS_PER_WORD) {
    311 		bw->buffer[bw->words++] = 0;
    312 		bits -= FLAC__BITS_PER_WORD;
    313 	}
    314 	/* do any leftovers */
    315 	if(bits > 0) {
    316 		bw->accum = 0;
    317 		bw->bits = bits;
    318 	}
    319 	return true;
    320 }
    321 
    322 static inline FLAC__bool FLAC__bitwriter_write_raw_uint32_nocheck(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
    323 {
    324 	register unsigned left;
    325 
    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);
    328 
    329 	if(bw == 0 || bw->buffer == 0)
    330 		return false;
    331 
    332 	if (bits > 32)
    333 		return false;
    334 
    335 	if(bits == 0)
    336 		return true;
    337 
    338 	FLAC__ASSERT((bits == 32) || (val>>bits == 0));
    339 
    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))
    342 		return false;
    343 
    344 	left = FLAC__BITS_PER_WORD - bw->bits;
    345 	if(bits < left) {
    346 		bw->accum <<= bits;
    347 		bw->accum |= val;
    348 		bw->bits += bits;
    349 	}
    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 */
    351 		bw->accum <<= left;
    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 */
    355 	}
    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);
    358 	}
    359 
    360 	return true;
    361 }
    362 
    363 inline FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
    364 {
    365 	/* check that unused bits are unset */
    366 	if((bits < 32) && (val>>bits != 0))
    367 		return false;
    368 
    369 	return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, bits);
    370 }
    371 
    372 inline FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
    373 {
    374 	/* zero-out unused bits */
    375 	if(bits < 32)
    376 		val &= (~(0xffffffff << bits));
    377 
    378 	return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, bits);
    379 }
    380 
    381 inline FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
    382 {
    383 	/* this could be a little faster but it's not used for much */
    384 	if(bits > 32) {
    385 		return
    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);
    388 	}
    389 	else
    390 		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
    391 }
    392 
    393 inline FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
    394 {
    395 	/* this doesn't need to be that fast as currently it is only used for vorbis comments */
    396 
    397 	if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val & 0xff, 8))
    398 		return false;
    399 	if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>8) & 0xff, 8))
    400 		return false;
    401 	if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, (val>>16) & 0xff, 8))
    402 		return false;
    403 	if(!FLAC__bitwriter_write_raw_uint32_nocheck(bw, val>>24, 8))
    404 		return false;
    405 
    406 	return true;
    407 }
    408 
    409 inline FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
    410 {
    411 	unsigned i;
    412 
    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))
    416 			return false;
    417 	}
    418 
    419 	return true;
    420 }
    421 
    422 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
    423 {
    424 	if(val < 32)
    425 		return FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, ++val);
    426 	else
    427 		return
    428 			FLAC__bitwriter_write_zeroes(bw, val) &&
    429 			FLAC__bitwriter_write_raw_uint32_nocheck(bw, 1, 1);
    430 }
    431 
    432 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
    433 {
    434 	FLAC__uint32 uval;
    435 
    436 	FLAC__ASSERT(parameter < 32);
    437 
    438 	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
    439 	uval = val;
    440 	uval <<= 1;
    441 	uval ^= (val>>31);
    442 
    443 	return 1 + parameter + (uval >> parameter);
    444 }
    445 
    446 #if 0 /* UNUSED */
    447 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
    448 {
    449 	unsigned bits, msbs, uval;
    450 	unsigned k;
    451 
    452 	FLAC__ASSERT(parameter > 0);
    453 
    454 	/* fold signed to unsigned */
    455 	if(val < 0)
    456 		uval = (unsigned)(((-(++val)) << 1) + 1);
    457 	else
    458 		uval = (unsigned)(val << 1);
    459 
    460 	k = FLAC__bitmath_ilog2(parameter);
    461 	if(parameter == 1u<<k) {
    462 		FLAC__ASSERT(k <= 30);
    463 
    464 		msbs = uval >> k;
    465 		bits = 1 + k + msbs;
    466 	}
    467 	else {
    468 		unsigned q, r, d;
    469 
    470 		d = (1 << (k+1)) - parameter;
    471 		q = uval / parameter;
    472 		r = uval - (q * parameter);
    473 
    474 		bits = 1 + q + k;
    475 		if(r >= d)
    476 			bits++;
    477 	}
    478 	return bits;
    479 }
    480 
    481 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
    482 {
    483 	unsigned bits, msbs;
    484 	unsigned k;
    485 
    486 	FLAC__ASSERT(parameter > 0);
    487 
    488 	k = FLAC__bitmath_ilog2(parameter);
    489 	if(parameter == 1u<<k) {
    490 		FLAC__ASSERT(k <= 30);
    491 
    492 		msbs = uval >> k;
    493 		bits = 1 + k + msbs;
    494 	}
    495 	else {
    496 		unsigned q, r, d;
    497 
    498 		d = (1 << (k+1)) - parameter;
    499 		q = uval / parameter;
    500 		r = uval - (q * parameter);
    501 
    502 		bits = 1 + q + k;
    503 		if(r >= d)
    504 			bits++;
    505 	}
    506 	return bits;
    507 }
    508 #endif /* UNUSED */
    509 
    510 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
    511 {
    512 	unsigned total_bits, interesting_bits, msbs;
    513 	FLAC__uint32 uval, pattern;
    514 
    515 	FLAC__ASSERT(0 != bw);
    516 	FLAC__ASSERT(0 != bw->buffer);
    517 	FLAC__ASSERT(parameter < 32);
    518 
    519 	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
    520 	uval = val;
    521 	uval <<= 1;
    522 	uval ^= (val>>31);
    523 
    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 */
    529 
    530 	if(total_bits <= 32)
    531 		return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
    532 	else
    533 		return
    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 */
    536 }
    537 
    538 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
    539 {
    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 */
    542 	FLAC__uint32 uval;
    543 	unsigned left;
    544 	const unsigned lsbits = 1 + parameter;
    545 	unsigned msbits, total_bits;
    546 
    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);
    552 
    553 	while(nvals) {
    554 		/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
    555 		uval = *vals;
    556 		uval <<= 1;
    557 		uval ^= (*vals>>31);
    558 
    559 		msbits = uval >> parameter;
    560 		total_bits = lsbits + msbits;
    561 
    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;
    568 			bw->accum |= uval;
    569 		}
    570 		else {
    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))
    574 				return false;
    575 
    576 			if(msbits) {
    577 				/* first part gets to word alignment */
    578 				if(bw->bits) {
    579 					left = FLAC__BITS_PER_WORD - bw->bits;
    580 					if(msbits < left) {
    581 						bw->accum <<= msbits;
    582 						bw->bits += msbits;
    583 						goto break1;
    584 					}
    585 					else {
    586 						bw->accum <<= left;
    587 						msbits -= left;
    588 						bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
    589 						bw->bits = 0;
    590 					}
    591 				}
    592 				/* do whole words */
    593 				while(msbits >= FLAC__BITS_PER_WORD) {
    594 					bw->buffer[bw->words++] = 0;
    595 					msbits -= FLAC__BITS_PER_WORD;
    596 				}
    597 				/* do any leftovers */
    598 				if(msbits > 0) {
    599 					bw->accum = 0;
    600 					bw->bits = msbits;
    601 				}
    602 			}
    603 break1:
    604 			uval |= mask1; /* set stop bit */
    605 			uval &= mask2; /* mask off unused top bits */
    606 
    607 			left = FLAC__BITS_PER_WORD - bw->bits;
    608 			if(lsbits < left) {
    609 				bw->accum <<= lsbits;
    610 				bw->accum |= uval;
    611 				bw->bits += lsbits;
    612 			}
    613 			else {
    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.
    617 				 */
    618 				FLAC__ASSERT(bw->bits);
    619 				FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
    620 				bw->accum <<= left;
    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 */
    624 			}
    625 		}
    626 		vals++;
    627 		nvals--;
    628 	}
    629 	return true;
    630 }
    631 
    632 #if 0 /* UNUSED */
    633 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
    634 {
    635 	unsigned total_bits, msbs, uval;
    636 	unsigned k;
    637 
    638 	FLAC__ASSERT(0 != bw);
    639 	FLAC__ASSERT(0 != bw->buffer);
    640 	FLAC__ASSERT(parameter > 0);
    641 
    642 	/* fold signed to unsigned */
    643 	if(val < 0)
    644 		uval = (unsigned)(((-(++val)) << 1) + 1);
    645 	else
    646 		uval = (unsigned)(val << 1);
    647 
    648 	k = FLAC__bitmath_ilog2(parameter);
    649 	if(parameter == 1u<<k) {
    650 		unsigned pattern;
    651 
    652 		FLAC__ASSERT(k <= 30);
    653 
    654 		msbs = uval >> k;
    655 		total_bits = 1 + k + msbs;
    656 		pattern = 1 << k; /* the unary end bit */
    657 		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
    658 
    659 		if(total_bits <= 32) {
    660 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
    661 				return false;
    662 		}
    663 		else {
    664 			/* write the unary MSBs */
    665 			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
    666 				return false;
    667 			/* write the unary end bit and binary LSBs */
    668 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
    669 				return false;
    670 		}
    671 	}
    672 	else {
    673 		unsigned q, r, d;
    674 
    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))
    680 			return false;
    681 		/* write the unary end bit */
    682 		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
    683 			return false;
    684 		/* write the binary LSBs */
    685 		if(r >= d) {
    686 			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
    687 				return false;
    688 		}
    689 		else {
    690 			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
    691 				return false;
    692 		}
    693 	}
    694 	return true;
    695 }
    696 
    697 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
    698 {
    699 	unsigned total_bits, msbs;
    700 	unsigned k;
    701 
    702 	FLAC__ASSERT(0 != bw);
    703 	FLAC__ASSERT(0 != bw->buffer);
    704 	FLAC__ASSERT(parameter > 0);
    705 
    706 	k = FLAC__bitmath_ilog2(parameter);
    707 	if(parameter == 1u<<k) {
    708 		unsigned pattern;
    709 
    710 		FLAC__ASSERT(k <= 30);
    711 
    712 		msbs = uval >> k;
    713 		total_bits = 1 + k + msbs;
    714 		pattern = 1 << k; /* the unary end bit */
    715 		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
    716 
    717 		if(total_bits <= 32) {
    718 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
    719 				return false;
    720 		}
    721 		else {
    722 			/* write the unary MSBs */
    723 			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
    724 				return false;
    725 			/* write the unary end bit and binary LSBs */
    726 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
    727 				return false;
    728 		}
    729 	}
    730 	else {
    731 		unsigned q, r, d;
    732 
    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))
    738 			return false;
    739 		/* write the unary end bit */
    740 		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
    741 			return false;
    742 		/* write the binary LSBs */
    743 		if(r >= d) {
    744 			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
    745 				return false;
    746 		}
    747 		else {
    748 			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
    749 				return false;
    750 		}
    751 	}
    752 	return true;
    753 }
    754 #endif /* UNUSED */
    755 
    756 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
    757 {
    758 	FLAC__bool ok = 1;
    759 
    760 	FLAC__ASSERT(0 != bw);
    761 	FLAC__ASSERT(0 != bw->buffer);
    762 
    763 	if((val & 0x80000000) != 0) /* this version only handles 31 bits */
    764 		return false;
    765 
    766 	if(val < 0x80) {
    767 		return FLAC__bitwriter_write_raw_uint32_nocheck(bw, val, 8);
    768 	}
    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);
    772 	}
    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);
    777 	}
    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);
    783 	}
    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);
    790 	}
    791 	else {
    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);
    798 	}
    799 
    800 	return ok;
    801 }
    802 
    803 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
    804 {
    805 	FLAC__bool ok = 1;
    806 
    807 	FLAC__ASSERT(0 != bw);
    808 	FLAC__ASSERT(0 != bw->buffer);
    809 
    810 	if((val & FLAC__U64L(0xFFFFFFF000000000)) != 0) /* this version only handles 36 bits */
    811 		return false;
    812 
    813 	if(val < 0x80) {
    814 		return FLAC__bitwriter_write_raw_uint32_nocheck(bw, (FLAC__uint32)val, 8);
    815 	}
    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);
    819 	}
    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);
    824 	}
    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);
    830 	}
    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);
    837 	}
    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);
    845 	}
    846 	else {
    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);
    854 	}
    855 
    856 	return ok;
    857 }
    858 
    859 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
    860 {
    861 	/* 0-pad to byte boundary */
    862 	if(bw->bits & 7u)
    863 		return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
    864 	else
    865 		return true;
    866 }
    867 
    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.
    875  */
    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);
    882