bitwriter.c revision c74663799493f2b1e6123c18def94295d0afab7
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* libFLAC - Free Lossless Audio Codec library
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Redistribution and use in source and binary forms, with or without
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * modification, are permitted provided that the following conditions
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * are met:
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
89d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * - Redistributions of source code must retain the above copyright
99d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * notice, this list of conditions and the following disclaimer.
102882c57910e0e91fa0be070871a9258548364825bsalomon@google.com *
112882c57910e0e91fa0be070871a9258548364825bsalomon@google.com * - Redistributions in binary form must reproduce the above copyright
122882c57910e0e91fa0be070871a9258548364825bsalomon@google.com * notice, this list of conditions and the following disclaimer in the
139d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * documentation and/or other materials provided with the distribution.
149d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
159d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * - Neither the name of the Xiph.org Foundation nor the names of its
169d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * contributors may be used to endorse or promote products derived from
179d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * this software without specific prior written permission.
189d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
199d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
209d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
219d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
229d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
239d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
249d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
259d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
269d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
279d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
289d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
299d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
309d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com */
319d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
329d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#if HAVE_CONFIG_H
339d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#  include <config.h>
349d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
359d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
369d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <stdlib.h> /* for malloc() */
379d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <string.h> /* for memcpy(), memset() */
389d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifdef _MSC_VER
399d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <winsock.h> /* for ntohl() */
409d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#elif defined FLAC__SYS_DARWIN
419d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <machine/endian.h> /* for ntohl() */
429d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#elif defined __MINGW32__
439d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <winsock.h> /* for ntohl() */
449d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#else
459d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include <netinet/in.h> /* for ntohl() */
469d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
479d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#if 0 /* UNUSED */
489d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include "private/bitmath.h"
499d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
509d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include "private/bitwriter.h"
519d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include "private/crc.h"
529d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include "FLAC/assert.h"
539d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#include "share/alloc.h"
542882c57910e0e91fa0be070871a9258548364825bsalomon@google.com
552882c57910e0e91fa0be070871a9258548364825bsalomon@google.com/* Things should be fastest when this matches the machine word size */
569d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
579d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
589d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comtypedef FLAC__uint32 bwword;
599d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__BYTES_PER_WORD 4
609d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__BITS_PER_WORD 32
619d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
629d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
639d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#if WORDS_BIGENDIAN
649d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define SWAP_BE_WORD_TO_HOST(x) (x)
659d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#else
669d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifdef _MSC_VER
679d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
689d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#else
699d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
709d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
719d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
729d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
739d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/*
749d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * The default capacity here doesn't matter too much.  The buffer always grows
759d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * to hold whatever is written to it.  Usually the encoder will stop adding at
769d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * a frame or metadata block, then write that out and clear the buffer for the
779d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * next one.
789d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com */
799d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comstatic const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
809d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* When growing, increment 4K at a time */
819d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comstatic const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
829d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
839d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
849d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
859d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
869d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifdef min
879d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#undef min
889d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
899d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define min(x,y) ((x)<(y)?(x):(y))
909d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
919d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
929d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifdef _MSC_VER
939d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__U64L(x) x
949d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#else
959d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLAC__U64L(x) x##LLU
969d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
979d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
989d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifndef FLaC__INLINE
999d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#define FLaC__INLINE
1009d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
1019d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1029d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comstruct FLAC__BitWriter {
1039d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bwword *buffer;
1049d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
1059d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	unsigned capacity; /* capacity of buffer in words */
1069d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	unsigned words; /* # of complete words in buffer */
1079d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	unsigned bits; /* # of used bits in accum */
1089d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com};
1099d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1109d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#ifdef _MSC_VER
1119d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* OPT: an MSVC built-in would be better */
1129d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comstatic _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
1139d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1149d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
1159d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	return (x>>16) | (x<<16);
1169d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
1179d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com#endif
1189d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1199d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/* * WATCHOUT: The current implementation only grows the buffer. */
1209d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comstatic FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
1219d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1229d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	unsigned new_capacity;
1239d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bwword *new_buffer;
1249d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1259d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__ASSERT(0 != bw);
1262882c57910e0e91fa0be070871a9258548364825bsalomon@google.com	FLAC__ASSERT(0 != bw->buffer);
1279d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1289d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	/* calculate total words needed to store 'bits_to_add' additional bits */
1299d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
1302882c57910e0e91fa0be070871a9258548364825bsalomon@google.com
1312882c57910e0e91fa0be070871a9258548364825bsalomon@google.com	/* it's possible (due to pessimism in the growth estimation that
1329d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	 * leads to this call) that we don't actually need to grow
1339d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	 */
1349d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	if(bw->capacity >= new_capacity)
1359d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com		return true;
1369d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1379d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	/* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
1389d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
1399d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com		new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
1409d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	/* make sure we got everything right */
141b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com	FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
142b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com	FLAC__ASSERT(new_capacity > bw->capacity);
143b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com	FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
1449d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1459d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
1469d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	if(new_buffer == 0)
1479d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com		return false;
1489d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->buffer = new_buffer;
1499d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->capacity = new_capacity;
1509d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	return true;
1519d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
1529d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1539d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1549d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/***********************************************************************
1559d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
1569d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * Class constructor/destructor
1579d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
1589d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com ***********************************************************************/
1599d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1609d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comFLAC__BitWriter *FLAC__bitwriter_new(void)
1619d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1629d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
1639d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	/* note that calloc() sets all members to 0 for us */
1649d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	return bw;
1659d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
1669d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1679d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comvoid FLAC__bitwriter_delete(FLAC__BitWriter *bw)
1689d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1699d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__ASSERT(0 != bw);
1709d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1719d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__bitwriter_free(bw);
1729d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	free(bw);
1739d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
1749d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1759d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com/***********************************************************************
1769d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
1779d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com * Public class methods
1789d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com *
1799d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com ***********************************************************************/
1809d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1819d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comFLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
1829d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1839d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__ASSERT(0 != bw);
1849d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1859d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->words = bw->bits = 0;
1869d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
1879d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
1889d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	if(bw->buffer == 0)
1899d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com		return false;
1909d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1919d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	return true;
1929d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
1939d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1949d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comvoid FLAC__bitwriter_free(FLAC__BitWriter *bw)
1959d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
1969d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	FLAC__ASSERT(0 != bw);
1979d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
1989d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	if(0 != bw->buffer)
1999d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com		free(bw->buffer);
2009d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->buffer = 0;
2019d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->capacity = 0;
2029d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->words = bw->bits = 0;
2039d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com}
2049d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com
2059d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.comvoid FLAC__bitwriter_clear(FLAC__BitWriter *bw)
2069d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com{
2079d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com	bw->words = bw->bits = 0;
208}
209
210void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
211{
212	unsigned i, j;
213	if(bw == 0) {
214		fprintf(out, "bitwriter is NULL\n");
215	}
216	else {
217		fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
218
219		for(i = 0; i < bw->words; i++) {
220			fprintf(out, "%08X: ", i);
221			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
222				fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
223			fprintf(out, "\n");
224		}
225		if(bw->bits > 0) {
226			fprintf(out, "%08X: ", i);
227			for(j = 0; j < bw->bits; j++)
228				fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
229			fprintf(out, "\n");
230		}
231	}
232}
233
234FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
235{
236	const FLAC__byte *buffer;
237	size_t bytes;
238
239	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
240
241	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
242		return false;
243
244	*crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
245	FLAC__bitwriter_release_buffer(bw);
246	return true;
247}
248
249FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
250{
251	const FLAC__byte *buffer;
252	size_t bytes;
253
254	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
255
256	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
257		return false;
258
259	*crc = FLAC__crc8(buffer, bytes);
260	FLAC__bitwriter_release_buffer(bw);
261	return true;
262}
263
264FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
265{
266	return ((bw->bits & 7) == 0);
267}
268
269unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
270{
271	return FLAC__TOTAL_BITS(bw);
272}
273
274FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
275{
276	FLAC__ASSERT((bw->bits & 7) == 0);
277	/* double protection */
278	if(bw->bits & 7)
279		return false;
280	/* if we have bits in the accumulator we have to flush those to the buffer first */
281	if(bw->bits) {
282		FLAC__ASSERT(bw->words <= bw->capacity);
283		if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
284			return false;
285		/* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
286		bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
287	}
288	/* now we can just return what we have */
289	*buffer = (FLAC__byte*)bw->buffer;
290	*bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
291	return true;
292}
293
294void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
295{
296	/* nothing to do.  in the future, strict checking of a 'writer-is-in-
297	 * get-mode' flag could be added everywhere and then cleared here
298	 */
299	(void)bw;
300}
301
302FLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
303{
304	unsigned n;
305
306	FLAC__ASSERT(0 != bw);
307	FLAC__ASSERT(0 != bw->buffer);
308
309	if(bits == 0)
310		return true;
311	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
312	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
313		return false;
314	/* first part gets to word alignment */
315	if(bw->bits) {
316		n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
317		bw->accum <<= n;
318		bits -= n;
319		bw->bits += n;
320		if(bw->bits == FLAC__BITS_PER_WORD) {
321			bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
322			bw->bits = 0;
323		}
324		else
325			return true;
326	}
327	/* do whole words */
328	while(bits >= FLAC__BITS_PER_WORD) {
329		bw->buffer[bw->words++] = 0;
330		bits -= FLAC__BITS_PER_WORD;
331	}
332	/* do any leftovers */
333	if(bits > 0) {
334		bw->accum = 0;
335		bw->bits = bits;
336	}
337	return true;
338}
339
340FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
341{
342	register unsigned left;
343
344	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
345	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
346
347	FLAC__ASSERT(0 != bw);
348	FLAC__ASSERT(0 != bw->buffer);
349
350	FLAC__ASSERT(bits <= 32);
351	if(bits == 0)
352		return true;
353
354	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
355	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
356		return false;
357
358	left = FLAC__BITS_PER_WORD - bw->bits;
359	if(bits < left) {
360		bw->accum <<= bits;
361		bw->accum |= val;
362		bw->bits += bits;
363	}
364	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 */
365		bw->accum <<= left;
366		bw->accum |= val >> (bw->bits = bits - left);
367		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
368		bw->accum = val;
369	}
370	else {
371		bw->accum = val;
372		bw->bits = 0;
373		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
374	}
375
376	return true;
377}
378
379FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
380{
381	/* zero-out unused bits */
382	if(bits < 32)
383		val &= (~(0xffffffff << bits));
384
385	return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
386}
387
388FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
389{
390	/* this could be a little faster but it's not used for much */
391	if(bits > 32) {
392		return
393			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
394			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
395	}
396	else
397		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
398}
399
400FLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
401{
402	/* this doesn't need to be that fast as currently it is only used for vorbis comments */
403
404	if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
405		return false;
406	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
407		return false;
408	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
409		return false;
410	if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
411		return false;
412
413	return true;
414}
415
416FLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
417{
418	unsigned i;
419
420	/* this could be faster but currently we don't need it to be since it's only used for writing metadata */
421	for(i = 0; i < nvals; i++) {
422		if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
423			return false;
424	}
425
426	return true;
427}
428
429FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
430{
431	if(val < 32)
432		return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
433	else
434		return
435			FLAC__bitwriter_write_zeroes(bw, val) &&
436			FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
437}
438
439unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
440{
441	FLAC__uint32 uval;
442
443	FLAC__ASSERT(parameter < sizeof(unsigned)*8);
444
445	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
446	uval = (val<<1) ^ (val>>31);
447
448	return 1 + parameter + (uval >> parameter);
449}
450
451#if 0 /* UNUSED */
452unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
453{
454	unsigned bits, msbs, uval;
455	unsigned k;
456
457	FLAC__ASSERT(parameter > 0);
458
459	/* fold signed to unsigned */
460	if(val < 0)
461		uval = (unsigned)(((-(++val)) << 1) + 1);
462	else
463		uval = (unsigned)(val << 1);
464
465	k = FLAC__bitmath_ilog2(parameter);
466	if(parameter == 1u<<k) {
467		FLAC__ASSERT(k <= 30);
468
469		msbs = uval >> k;
470		bits = 1 + k + msbs;
471	}
472	else {
473		unsigned q, r, d;
474
475		d = (1 << (k+1)) - parameter;
476		q = uval / parameter;
477		r = uval - (q * parameter);
478
479		bits = 1 + q + k;
480		if(r >= d)
481			bits++;
482	}
483	return bits;
484}
485
486unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
487{
488	unsigned bits, msbs;
489	unsigned k;
490
491	FLAC__ASSERT(parameter > 0);
492
493	k = FLAC__bitmath_ilog2(parameter);
494	if(parameter == 1u<<k) {
495		FLAC__ASSERT(k <= 30);
496
497		msbs = uval >> k;
498		bits = 1 + k + msbs;
499	}
500	else {
501		unsigned q, r, d;
502
503		d = (1 << (k+1)) - parameter;
504		q = uval / parameter;
505		r = uval - (q * parameter);
506
507		bits = 1 + q + k;
508		if(r >= d)
509			bits++;
510	}
511	return bits;
512}
513#endif /* UNUSED */
514
515FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
516{
517	unsigned total_bits, interesting_bits, msbs;
518	FLAC__uint32 uval, pattern;
519
520	FLAC__ASSERT(0 != bw);
521	FLAC__ASSERT(0 != bw->buffer);
522	FLAC__ASSERT(parameter < 8*sizeof(uval));
523
524	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
525	uval = (val<<1) ^ (val>>31);
526
527	msbs = uval >> parameter;
528	interesting_bits = 1 + parameter;
529	total_bits = interesting_bits + msbs;
530	pattern = 1 << parameter; /* the unary end bit */
531	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
532
533	if(total_bits <= 32)
534		return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
535	else
536		return
537			FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
538			FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
539}
540
541FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
542{
543	const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
544	const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
545	FLAC__uint32 uval;
546	unsigned left;
547	const unsigned lsbits = 1 + parameter;
548	unsigned msbits;
549
550	FLAC__ASSERT(0 != bw);
551	FLAC__ASSERT(0 != bw->buffer);
552	FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
553	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
554	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
555
556	while(nvals) {
557		/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
558		uval = (*vals<<1) ^ (*vals>>31);
559
560		msbits = uval >> parameter;
561
562#if 0 /* OPT: can remove this special case if it doesn't make up for the extra compare (doesn't make a statistically significant difference with msvc or gcc/x86) */
563		if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
564			/* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
565			bw->bits = bw->bits + msbits + lsbits;
566			uval |= mask1; /* set stop bit */
567			uval &= mask2; /* mask off unused top bits */
568			/* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
569			bw->accum <<= msbits;
570			bw->accum <<= lsbits;
571			bw->accum |= uval;
572			if(bw->bits == FLAC__BITS_PER_WORD) {
573				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
574				bw->bits = 0;
575				/* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
576				if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
577					FLAC__ASSERT(bw->capacity == bw->words);
578					return false;
579				}
580			}
581		}
582		else {
583#elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
584		if(bw->bits && bw->bits + msbits + lsbits < 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 = bw->bits + msbits + lsbits;
587			uval |= mask1; /* set stop bit */
588			uval &= mask2; /* mask off unused top bits */
589			bw->accum <<= msbits + lsbits;
590			bw->accum |= uval;
591		}
592		else {
593#endif
594			/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
595			/* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
596			if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
597				return false;
598
599			if(msbits) {
600				/* first part gets to word alignment */
601				if(bw->bits) {
602					left = FLAC__BITS_PER_WORD - bw->bits;
603					if(msbits < left) {
604						bw->accum <<= msbits;
605						bw->bits += msbits;
606						goto break1;
607					}
608					else {
609						bw->accum <<= left;
610						msbits -= left;
611						bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
612						bw->bits = 0;
613					}
614				}
615				/* do whole words */
616				while(msbits >= FLAC__BITS_PER_WORD) {
617					bw->buffer[bw->words++] = 0;
618					msbits -= FLAC__BITS_PER_WORD;
619				}
620				/* do any leftovers */
621				if(msbits > 0) {
622					bw->accum = 0;
623					bw->bits = msbits;
624				}
625			}
626break1:
627			uval |= mask1; /* set stop bit */
628			uval &= mask2; /* mask off unused top bits */
629
630			left = FLAC__BITS_PER_WORD - bw->bits;
631			if(lsbits < left) {
632				bw->accum <<= lsbits;
633				bw->accum |= uval;
634				bw->bits += lsbits;
635			}
636			else {
637				/* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
638				 * be > lsbits (because of previous assertions) so it would have
639				 * triggered the (lsbits<left) case above.
640				 */
641				FLAC__ASSERT(bw->bits);
642				FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
643				bw->accum <<= left;
644				bw->accum |= uval >> (bw->bits = lsbits - left);
645				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
646				bw->accum = uval;
647			}
648#if 1
649		}
650#endif
651		vals++;
652		nvals--;
653	}
654	return true;
655}
656
657#if 0 /* UNUSED */
658FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
659{
660	unsigned total_bits, msbs, uval;
661	unsigned k;
662
663	FLAC__ASSERT(0 != bw);
664	FLAC__ASSERT(0 != bw->buffer);
665	FLAC__ASSERT(parameter > 0);
666
667	/* fold signed to unsigned */
668	if(val < 0)
669		uval = (unsigned)(((-(++val)) << 1) + 1);
670	else
671		uval = (unsigned)(val << 1);
672
673	k = FLAC__bitmath_ilog2(parameter);
674	if(parameter == 1u<<k) {
675		unsigned pattern;
676
677		FLAC__ASSERT(k <= 30);
678
679		msbs = uval >> k;
680		total_bits = 1 + k + msbs;
681		pattern = 1 << k; /* the unary end bit */
682		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
683
684		if(total_bits <= 32) {
685			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
686				return false;
687		}
688		else {
689			/* write the unary MSBs */
690			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
691				return false;
692			/* write the unary end bit and binary LSBs */
693			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
694				return false;
695		}
696	}
697	else {
698		unsigned q, r, d;
699
700		d = (1 << (k+1)) - parameter;
701		q = uval / parameter;
702		r = uval - (q * parameter);
703		/* write the unary MSBs */
704		if(!FLAC__bitwriter_write_zeroes(bw, q))
705			return false;
706		/* write the unary end bit */
707		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
708			return false;
709		/* write the binary LSBs */
710		if(r >= d) {
711			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
712				return false;
713		}
714		else {
715			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
716				return false;
717		}
718	}
719	return true;
720}
721
722FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
723{
724	unsigned total_bits, msbs;
725	unsigned k;
726
727	FLAC__ASSERT(0 != bw);
728	FLAC__ASSERT(0 != bw->buffer);
729	FLAC__ASSERT(parameter > 0);
730
731	k = FLAC__bitmath_ilog2(parameter);
732	if(parameter == 1u<<k) {
733		unsigned pattern;
734
735		FLAC__ASSERT(k <= 30);
736
737		msbs = uval >> k;
738		total_bits = 1 + k + msbs;
739		pattern = 1 << k; /* the unary end bit */
740		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
741
742		if(total_bits <= 32) {
743			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
744				return false;
745		}
746		else {
747			/* write the unary MSBs */
748			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
749				return false;
750			/* write the unary end bit and binary LSBs */
751			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
752				return false;
753		}
754	}
755	else {
756		unsigned q, r, d;
757
758		d = (1 << (k+1)) - parameter;
759		q = uval / parameter;
760		r = uval - (q * parameter);
761		/* write the unary MSBs */
762		if(!FLAC__bitwriter_write_zeroes(bw, q))
763			return false;
764		/* write the unary end bit */
765		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
766			return false;
767		/* write the binary LSBs */
768		if(r >= d) {
769			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
770				return false;
771		}
772		else {
773			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
774				return false;
775		}
776	}
777	return true;
778}
779#endif /* UNUSED */
780
781FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
782{
783	FLAC__bool ok = 1;
784
785	FLAC__ASSERT(0 != bw);
786	FLAC__ASSERT(0 != bw->buffer);
787
788	FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
789
790	if(val < 0x80) {
791		return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
792	}
793	else if(val < 0x800) {
794		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
795		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
796	}
797	else if(val < 0x10000) {
798		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
799		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
800		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
801	}
802	else if(val < 0x200000) {
803		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
804		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
805		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
806		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
807	}
808	else if(val < 0x4000000) {
809		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
810		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
811		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
812		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
813		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
814	}
815	else {
816		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
817		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
818		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
819		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
820		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
821		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
822	}
823
824	return ok;
825}
826
827FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
828{
829	FLAC__bool ok = 1;
830
831	FLAC__ASSERT(0 != bw);
832	FLAC__ASSERT(0 != bw->buffer);
833
834	FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
835
836	if(val < 0x80) {
837		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
838	}
839	else if(val < 0x800) {
840		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
841		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
842	}
843	else if(val < 0x10000) {
844		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
845		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
846		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
847	}
848	else if(val < 0x200000) {
849		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
850		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
851		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
852		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
853	}
854	else if(val < 0x4000000) {
855		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
856		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
857		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
858		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
859		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
860	}
861	else if(val < 0x80000000) {
862		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
863		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
864		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
865		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
866		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
867		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
868	}
869	else {
870		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
871		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
872		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
873		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
874		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
875		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
876		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
877	}
878
879	return ok;
880}
881
882FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
883{
884	/* 0-pad to byte boundary */
885	if(bw->bits & 7u)
886		return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
887	else
888		return true;
889}
890