1c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* libFLAC - Free Lossless Audio Codec library
2c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
4c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Redistribution and use in source and binary forms, with or without
5c74663799493f2b1e6123c18def94295d0afab7Kenny Root * modification, are permitted provided that the following conditions
6c74663799493f2b1e6123c18def94295d0afab7Kenny Root * are met:
7c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
8c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions of source code must retain the above copyright
9c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer.
10c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
11c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions in binary form must reproduce the above copyright
12c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer in the
13c74663799493f2b1e6123c18def94295d0afab7Kenny Root * documentation and/or other materials provided with the distribution.
14c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
15c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Neither the name of the Xiph.org Foundation nor the names of its
16c74663799493f2b1e6123c18def94295d0afab7Kenny Root * contributors may be used to endorse or promote products derived from
17c74663799493f2b1e6123c18def94295d0afab7Kenny Root * this software without specific prior written permission.
18c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
19c74663799493f2b1e6123c18def94295d0afab7Kenny Root * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20c74663799493f2b1e6123c18def94295d0afab7Kenny Root * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22c74663799493f2b1e6123c18def94295d0afab7Kenny Root * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23c74663799493f2b1e6123c18def94295d0afab7Kenny Root * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24c74663799493f2b1e6123c18def94295d0afab7Kenny Root * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28c74663799493f2b1e6123c18def94295d0afab7Kenny Root * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29c74663799493f2b1e6123c18def94295d0afab7Kenny Root * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30c74663799493f2b1e6123c18def94295d0afab7Kenny Root */
31c74663799493f2b1e6123c18def94295d0afab7Kenny Root
32c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if HAVE_CONFIG_H
33c74663799493f2b1e6123c18def94295d0afab7Kenny Root#  include <config.h>
34c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
35c74663799493f2b1e6123c18def94295d0afab7Kenny Root
36c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <stdlib.h> /* for malloc() */
37c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <string.h> /* for memcpy(), memset() */
38c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
39c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <winsock.h> /* for ntohl() */
40c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif defined FLAC__SYS_DARWIN
41c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <machine/endian.h> /* for ntohl() */
42c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif defined __MINGW32__
43c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <winsock.h> /* for ntohl() */
44c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
45c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <netinet/in.h> /* for ntohl() */
46c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
47c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* UNUSED */
48c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/bitmath.h"
49c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
50c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/bitwriter.h"
51c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/crc.h"
52c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "FLAC/assert.h"
53c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "share/alloc.h"
54c74663799493f2b1e6123c18def94295d0afab7Kenny Root
55c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* Things should be fastest when this matches the machine word size */
56c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
57c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* WATCHOUT: there are a few places where the code will not work unless bwword is >= 32 bits wide */
58c74663799493f2b1e6123c18def94295d0afab7Kenny Roottypedef FLAC__uint32 bwword;
59c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__BYTES_PER_WORD 4
60c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__BITS_PER_WORD 32
61c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
62c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* SWAP_BE_WORD_TO_HOST swaps bytes in a bwword (which is always big-endian) if necessary to match host byte order */
63c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if WORDS_BIGENDIAN
64c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) (x)
65c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
66c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
67c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
68c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
69c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
70c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
71c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
72c74663799493f2b1e6123c18def94295d0afab7Kenny Root
73c74663799493f2b1e6123c18def94295d0afab7Kenny Root/*
74c74663799493f2b1e6123c18def94295d0afab7Kenny Root * The default capacity here doesn't matter too much.  The buffer always grows
75c74663799493f2b1e6123c18def94295d0afab7Kenny Root * to hold whatever is written to it.  Usually the encoder will stop adding at
76c74663799493f2b1e6123c18def94295d0afab7Kenny Root * a frame or metadata block, then write that out and clear the buffer for the
77c74663799493f2b1e6123c18def94295d0afab7Kenny Root * next one.
78c74663799493f2b1e6123c18def94295d0afab7Kenny Root */
79c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(bwword); /* size in words */
80c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* When growing, increment 4K at a time */
81c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(bwword); /* size in words */
82c74663799493f2b1e6123c18def94295d0afab7Kenny Root
83c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
84c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
85c74663799493f2b1e6123c18def94295d0afab7Kenny Root
86c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef min
87c74663799493f2b1e6123c18def94295d0afab7Kenny Root#undef min
88c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
89c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define min(x,y) ((x)<(y)?(x):(y))
90c74663799493f2b1e6123c18def94295d0afab7Kenny Root
91c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
92c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
93c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x
94c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
95c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x##LLU
96c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
97c74663799493f2b1e6123c18def94295d0afab7Kenny Root
98c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifndef FLaC__INLINE
99c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLaC__INLINE
100c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
101c74663799493f2b1e6123c18def94295d0afab7Kenny Root
102c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstruct FLAC__BitWriter {
103c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bwword *buffer;
104c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
105c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned capacity; /* capacity of buffer in words */
106c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned words; /* # of complete words in buffer */
107c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bits; /* # of used bits in accum */
108c74663799493f2b1e6123c18def94295d0afab7Kenny Root};
109c74663799493f2b1e6123c18def94295d0afab7Kenny Root
110c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
111c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* OPT: an MSVC built-in would be better */
112c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
113c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
114c74663799493f2b1e6123c18def94295d0afab7Kenny Root	x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
115c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return (x>>16) | (x<<16);
116c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
117c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
118c74663799493f2b1e6123c18def94295d0afab7Kenny Root
119c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* * WATCHOUT: The current implementation only grows the buffer. */
120c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
121c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
122c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned new_capacity;
123c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bwword *new_buffer;
124c74663799493f2b1e6123c18def94295d0afab7Kenny Root
125c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
126c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
127c74663799493f2b1e6123c18def94295d0afab7Kenny Root
128c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* calculate total words needed to store 'bits_to_add' additional bits */
129c74663799493f2b1e6123c18def94295d0afab7Kenny Root	new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
130c74663799493f2b1e6123c18def94295d0afab7Kenny Root
131c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* it's possible (due to pessimism in the growth estimation that
132c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * leads to this call) that we don't actually need to grow
133c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
134c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->capacity >= new_capacity)
135c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
136c74663799493f2b1e6123c18def94295d0afab7Kenny Root
137c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
138c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
139c74663799493f2b1e6123c18def94295d0afab7Kenny Root		new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
140c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* make sure we got everything right */
141c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
142c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(new_capacity > bw->capacity);
143c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
144c74663799493f2b1e6123c18def94295d0afab7Kenny Root
145c74663799493f2b1e6123c18def94295d0afab7Kenny Root	new_buffer = (bwword*)safe_realloc_mul_2op_(bw->buffer, sizeof(bwword), /*times*/new_capacity);
146c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(new_buffer == 0)
147c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
148c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->buffer = new_buffer;
149c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->capacity = new_capacity;
150c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
151c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
152c74663799493f2b1e6123c18def94295d0afab7Kenny Root
153c74663799493f2b1e6123c18def94295d0afab7Kenny Root
154c74663799493f2b1e6123c18def94295d0afab7Kenny Root/***********************************************************************
155c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
156c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Class constructor/destructor
157c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
158c74663799493f2b1e6123c18def94295d0afab7Kenny Root ***********************************************************************/
159c74663799493f2b1e6123c18def94295d0afab7Kenny Root
160c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__BitWriter *FLAC__bitwriter_new(void)
161c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
162c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
163c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* note that calloc() sets all members to 0 for us */
164c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return bw;
165c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
166c74663799493f2b1e6123c18def94295d0afab7Kenny Root
167c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitwriter_delete(FLAC__BitWriter *bw)
168c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
169c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
170c74663799493f2b1e6123c18def94295d0afab7Kenny Root
171c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bitwriter_free(bw);
172c74663799493f2b1e6123c18def94295d0afab7Kenny Root	free(bw);
173c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
174c74663799493f2b1e6123c18def94295d0afab7Kenny Root
175c74663799493f2b1e6123c18def94295d0afab7Kenny Root/***********************************************************************
176c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
177c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Public class methods
178c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
179c74663799493f2b1e6123c18def94295d0afab7Kenny Root ***********************************************************************/
180c74663799493f2b1e6123c18def94295d0afab7Kenny Root
181c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
182c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
183c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
184c74663799493f2b1e6123c18def94295d0afab7Kenny Root
185c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->words = bw->bits = 0;
186c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
187c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->buffer = (bwword*)malloc(sizeof(bwword) * bw->capacity);
188c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->buffer == 0)
189c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
190c74663799493f2b1e6123c18def94295d0afab7Kenny Root
191c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
192c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
193c74663799493f2b1e6123c18def94295d0afab7Kenny Root
194c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitwriter_free(FLAC__BitWriter *bw)
195c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
196c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
197c74663799493f2b1e6123c18def94295d0afab7Kenny Root
198c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(0 != bw->buffer)
199c74663799493f2b1e6123c18def94295d0afab7Kenny Root		free(bw->buffer);
200c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->buffer = 0;
201c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->capacity = 0;
202c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->words = bw->bits = 0;
203c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
204c74663799493f2b1e6123c18def94295d0afab7Kenny Root
205c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitwriter_clear(FLAC__BitWriter *bw)
206c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
207c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bw->words = bw->bits = 0;
208c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
209c74663799493f2b1e6123c18def94295d0afab7Kenny Root
210c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
211c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
212c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i, j;
213c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw == 0) {
214c74663799493f2b1e6123c18def94295d0afab7Kenny Root		fprintf(out, "bitwriter is NULL\n");
215c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
216c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
217c74663799493f2b1e6123c18def94295d0afab7Kenny Root		fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
218c74663799493f2b1e6123c18def94295d0afab7Kenny Root
219c74663799493f2b1e6123c18def94295d0afab7Kenny Root		for(i = 0; i < bw->words; i++) {
220c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "%08X: ", i);
221c74663799493f2b1e6123c18def94295d0afab7Kenny Root			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
222c74663799493f2b1e6123c18def94295d0afab7Kenny Root				fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
223c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "\n");
224c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
225c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bw->bits > 0) {
226c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "%08X: ", i);
227c74663799493f2b1e6123c18def94295d0afab7Kenny Root			for(j = 0; j < bw->bits; j++)
228c74663799493f2b1e6123c18def94295d0afab7Kenny Root				fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
229c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "\n");
230c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
231c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
232c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
233c74663799493f2b1e6123c18def94295d0afab7Kenny Root
234c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
235c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
236c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__byte *buffer;
237c74663799493f2b1e6123c18def94295d0afab7Kenny Root	size_t bytes;
238c74663799493f2b1e6123c18def94295d0afab7Kenny Root
239c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
240c74663799493f2b1e6123c18def94295d0afab7Kenny Root
241c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
242c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
243c74663799493f2b1e6123c18def94295d0afab7Kenny Root
244c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
245c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bitwriter_release_buffer(bw);
246c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
247c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
248c74663799493f2b1e6123c18def94295d0afab7Kenny Root
249c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
250c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
251c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__byte *buffer;
252c74663799493f2b1e6123c18def94295d0afab7Kenny Root	size_t bytes;
253c74663799493f2b1e6123c18def94295d0afab7Kenny Root
254c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
255c74663799493f2b1e6123c18def94295d0afab7Kenny Root
256c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
257c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
258c74663799493f2b1e6123c18def94295d0afab7Kenny Root
259c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*crc = FLAC__crc8(buffer, bytes);
260c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bitwriter_release_buffer(bw);
261c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
262c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
263c74663799493f2b1e6123c18def94295d0afab7Kenny Root
264c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
265c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
266c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return ((bw->bits & 7) == 0);
267c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
268c74663799493f2b1e6123c18def94295d0afab7Kenny Root
269c74663799493f2b1e6123c18def94295d0afab7Kenny Rootunsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
270c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
271c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return FLAC__TOTAL_BITS(bw);
272c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
273c74663799493f2b1e6123c18def94295d0afab7Kenny Root
274c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
275c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
276c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((bw->bits & 7) == 0);
277c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* double protection */
278c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->bits & 7)
279c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
280c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* if we have bits in the accumulator we have to flush those to the buffer first */
281c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->bits) {
282c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(bw->words <= bw->capacity);
283c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
284c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
285c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
286c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
287c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
288c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* now we can just return what we have */
289c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*buffer = (FLAC__byte*)bw->buffer;
290c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
291c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
292c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
293c74663799493f2b1e6123c18def94295d0afab7Kenny Root
294c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
295c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
296c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* nothing to do.  in the future, strict checking of a 'writer-is-in-
297c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * get-mode' flag could be added everywhere and then cleared here
298c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
299c74663799493f2b1e6123c18def94295d0afab7Kenny Root	(void)bw;
300c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
301c74663799493f2b1e6123c18def94295d0afab7Kenny Root
302c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
303c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
304c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned n;
305c74663799493f2b1e6123c18def94295d0afab7Kenny Root
306c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
307c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
308c74663799493f2b1e6123c18def94295d0afab7Kenny Root
309c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits == 0)
310c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
311c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
312c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
313c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
314c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* first part gets to word alignment */
315c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->bits) {
316c74663799493f2b1e6123c18def94295d0afab7Kenny Root		n = min(FLAC__BITS_PER_WORD - bw->bits, bits);
317c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum <<= n;
318c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits -= n;
319c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->bits += n;
320c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bw->bits == FLAC__BITS_PER_WORD) {
321c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
322c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->bits = 0;
323c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
324c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else
325c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
326c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
327c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* do whole words */
328c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(bits >= FLAC__BITS_PER_WORD) {
329c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->buffer[bw->words++] = 0;
330c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits -= FLAC__BITS_PER_WORD;
331c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
332c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* do any leftovers */
333c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits > 0) {
334c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum = 0;
335c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->bits = bits;
336c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
337c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
338c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
339c74663799493f2b1e6123c18def94295d0afab7Kenny Root
340c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
341c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
342c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned left;
343c74663799493f2b1e6123c18def94295d0afab7Kenny Root
344c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
345c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
346c74663799493f2b1e6123c18def94295d0afab7Kenny Root
347c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
348c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
349c74663799493f2b1e6123c18def94295d0afab7Kenny Root
350c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(bits <= 32);
351c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits == 0)
352c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
353c74663799493f2b1e6123c18def94295d0afab7Kenny Root
354c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
355c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
356c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
357c74663799493f2b1e6123c18def94295d0afab7Kenny Root
358c74663799493f2b1e6123c18def94295d0afab7Kenny Root	left = FLAC__BITS_PER_WORD - bw->bits;
359c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits < left) {
360c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum <<= bits;
361c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum |= val;
362c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->bits += bits;
363c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
364c74663799493f2b1e6123c18def94295d0afab7Kenny Root	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 */
365c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum <<= left;
366c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum |= val >> (bw->bits = bits - left);
367c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
368c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum = val;
369c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
370c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
371c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->accum = val;
372c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->bits = 0;
373c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
374c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
375c74663799493f2b1e6123c18def94295d0afab7Kenny Root
376c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
377c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
378c74663799493f2b1e6123c18def94295d0afab7Kenny Root
379c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
380c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
381c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* zero-out unused bits */
382c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits < 32)
383c74663799493f2b1e6123c18def94295d0afab7Kenny Root		val &= (~(0xffffffff << bits));
384c74663799493f2b1e6123c18def94295d0afab7Kenny Root
385c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
386c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
387c74663799493f2b1e6123c18def94295d0afab7Kenny Root
388c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
389c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
390c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* this could be a little faster but it's not used for much */
391c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits > 32) {
392c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return
393c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
394c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
395c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
396c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
397c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
398c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
399c74663799493f2b1e6123c18def94295d0afab7Kenny Root
400c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
401c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
402c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* this doesn't need to be that fast as currently it is only used for vorbis comments */
403c74663799493f2b1e6123c18def94295d0afab7Kenny Root
404c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
405c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
406c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
407c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
408c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
409c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
410c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
411c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
412c74663799493f2b1e6123c18def94295d0afab7Kenny Root
413c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
414c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
415c74663799493f2b1e6123c18def94295d0afab7Kenny Root
416c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
417c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
418c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
419c74663799493f2b1e6123c18def94295d0afab7Kenny Root
420c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* this could be faster but currently we don't need it to be since it's only used for writing metadata */
421c74663799493f2b1e6123c18def94295d0afab7Kenny Root	for(i = 0; i < nvals; i++) {
422c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
423c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
424c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
425c74663799493f2b1e6123c18def94295d0afab7Kenny Root
426c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
427c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
428c74663799493f2b1e6123c18def94295d0afab7Kenny Root
429c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
430c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
431c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(val < 32)
432c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
433c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
434c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return
435c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_zeroes(bw, val) &&
436c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
437c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
438c74663799493f2b1e6123c18def94295d0afab7Kenny Root
439c74663799493f2b1e6123c18def94295d0afab7Kenny Rootunsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
440c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
441c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 uval;
442c74663799493f2b1e6123c18def94295d0afab7Kenny Root
443c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter < sizeof(unsigned)*8);
444c74663799493f2b1e6123c18def94295d0afab7Kenny Root
445c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
446c74663799493f2b1e6123c18def94295d0afab7Kenny Root	uval = (val<<1) ^ (val>>31);
447c74663799493f2b1e6123c18def94295d0afab7Kenny Root
448c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return 1 + parameter + (uval >> parameter);
449c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
450c74663799493f2b1e6123c18def94295d0afab7Kenny Root
451c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* UNUSED */
452c74663799493f2b1e6123c18def94295d0afab7Kenny Rootunsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
453c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
454c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bits, msbs, uval;
455c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned k;
456c74663799493f2b1e6123c18def94295d0afab7Kenny Root
457c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter > 0);
458c74663799493f2b1e6123c18def94295d0afab7Kenny Root
459c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* fold signed to unsigned */
460c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(val < 0)
461c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (unsigned)(((-(++val)) << 1) + 1);
462c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
463c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (unsigned)(val << 1);
464c74663799493f2b1e6123c18def94295d0afab7Kenny Root
465c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
466c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
467c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(k <= 30);
468c74663799493f2b1e6123c18def94295d0afab7Kenny Root
469c74663799493f2b1e6123c18def94295d0afab7Kenny Root		msbs = uval >> k;
470c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits = 1 + k + msbs;
471c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
472c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
473c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned q, r, d;
474c74663799493f2b1e6123c18def94295d0afab7Kenny Root
475c74663799493f2b1e6123c18def94295d0afab7Kenny Root		d = (1 << (k+1)) - parameter;
476c74663799493f2b1e6123c18def94295d0afab7Kenny Root		q = uval / parameter;
477c74663799493f2b1e6123c18def94295d0afab7Kenny Root		r = uval - (q * parameter);
478c74663799493f2b1e6123c18def94295d0afab7Kenny Root
479c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits = 1 + q + k;
480c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(r >= d)
481c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bits++;
482c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
483c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return bits;
484c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
485c74663799493f2b1e6123c18def94295d0afab7Kenny Root
486c74663799493f2b1e6123c18def94295d0afab7Kenny Rootunsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
487c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
488c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bits, msbs;
489c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned k;
490c74663799493f2b1e6123c18def94295d0afab7Kenny Root
491c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter > 0);
492c74663799493f2b1e6123c18def94295d0afab7Kenny Root
493c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
494c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
495c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(k <= 30);
496c74663799493f2b1e6123c18def94295d0afab7Kenny Root
497c74663799493f2b1e6123c18def94295d0afab7Kenny Root		msbs = uval >> k;
498c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits = 1 + k + msbs;
499c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
500c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
501c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned q, r, d;
502c74663799493f2b1e6123c18def94295d0afab7Kenny Root
503c74663799493f2b1e6123c18def94295d0afab7Kenny Root		d = (1 << (k+1)) - parameter;
504c74663799493f2b1e6123c18def94295d0afab7Kenny Root		q = uval / parameter;
505c74663799493f2b1e6123c18def94295d0afab7Kenny Root		r = uval - (q * parameter);
506c74663799493f2b1e6123c18def94295d0afab7Kenny Root
507c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bits = 1 + q + k;
508c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(r >= d)
509c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bits++;
510c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
511c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return bits;
512c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
513c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif /* UNUSED */
514c74663799493f2b1e6123c18def94295d0afab7Kenny Root
515c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
516c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
517c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned total_bits, interesting_bits, msbs;
518c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 uval, pattern;
519c74663799493f2b1e6123c18def94295d0afab7Kenny Root
520c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
521c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
522c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter < 8*sizeof(uval));
523c74663799493f2b1e6123c18def94295d0afab7Kenny Root
524c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
525c74663799493f2b1e6123c18def94295d0afab7Kenny Root	uval = (val<<1) ^ (val>>31);
526c74663799493f2b1e6123c18def94295d0afab7Kenny Root
527c74663799493f2b1e6123c18def94295d0afab7Kenny Root	msbs = uval >> parameter;
528c74663799493f2b1e6123c18def94295d0afab7Kenny Root	interesting_bits = 1 + parameter;
529c74663799493f2b1e6123c18def94295d0afab7Kenny Root	total_bits = interesting_bits + msbs;
530c74663799493f2b1e6123c18def94295d0afab7Kenny Root	pattern = 1 << parameter; /* the unary end bit */
531c74663799493f2b1e6123c18def94295d0afab7Kenny Root	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
532c74663799493f2b1e6123c18def94295d0afab7Kenny Root
533c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(total_bits <= 32)
534c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
535c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
536c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return
537c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
538c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
539c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
540c74663799493f2b1e6123c18def94295d0afab7Kenny Root
541c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
542c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
543c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
544c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
545c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 uval;
546c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned left;
547c74663799493f2b1e6123c18def94295d0afab7Kenny Root	const unsigned lsbits = 1 + parameter;
548c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned msbits;
549c74663799493f2b1e6123c18def94295d0afab7Kenny Root
550c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
551c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
552c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter < 8*sizeof(bwword)-1);
553c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
554c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
555c74663799493f2b1e6123c18def94295d0afab7Kenny Root
556c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals) {
557c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
558c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (*vals<<1) ^ (*vals>>31);
559c74663799493f2b1e6123c18def94295d0afab7Kenny Root
560c74663799493f2b1e6123c18def94295d0afab7Kenny Root		msbits = uval >> parameter;
561c74663799493f2b1e6123c18def94295d0afab7Kenny Root
562c74663799493f2b1e6123c18def94295d0afab7Kenny Root#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) */
563c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bw->bits && bw->bits + msbits + lsbits <= FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
564c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
565c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->bits = bw->bits + msbits + lsbits;
566c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval |= mask1; /* set stop bit */
567c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval &= mask2; /* mask off unused top bits */
568c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* NOT: bw->accum <<= msbits + lsbits because msbits+lsbits could be 32, then the shift would be a NOP */
569c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->accum <<= msbits;
570c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->accum <<= lsbits;
571c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->accum |= uval;
572c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(bw->bits == FLAC__BITS_PER_WORD) {
573c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
574c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->bits = 0;
575c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* burying the capacity check down here means we have to grow the buffer a little if there are more vals to do */
576c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(bw->capacity <= bw->words && nvals > 1 && !bitwriter_grow_(bw, 1)) {
577c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(bw->capacity == bw->words);
578c74663799493f2b1e6123c18def94295d0afab7Kenny Root					return false;
579c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
580c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
581c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
582c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
583c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif 1 /*@@@@@@ OPT: try this version with MSVC6 to see if better, not much difference for gcc-4 */
584c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current bwword */
585c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free bwword to work in */
586c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->bits = bw->bits + msbits + lsbits;
587c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval |= mask1; /* set stop bit */
588c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval &= mask2; /* mask off unused top bits */
589c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->accum <<= msbits + lsbits;
590c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bw->accum |= uval;
591c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
592c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
593c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
594c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
595c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
596c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 bwword*/ && !bitwriter_grow_(bw, msbits+lsbits))
597c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
598c74663799493f2b1e6123c18def94295d0afab7Kenny Root
599c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(msbits) {
600c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* first part gets to word alignment */
601c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(bw->bits) {
602c74663799493f2b1e6123c18def94295d0afab7Kenny Root					left = FLAC__BITS_PER_WORD - bw->bits;
603c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(msbits < left) {
604c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bw->accum <<= msbits;
605c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bw->bits += msbits;
606c74663799493f2b1e6123c18def94295d0afab7Kenny Root						goto break1;
607c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
608c74663799493f2b1e6123c18def94295d0afab7Kenny Root					else {
609c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bw->accum <<= left;
610c74663799493f2b1e6123c18def94295d0afab7Kenny Root						msbits -= left;
611c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
612c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bw->bits = 0;
613c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
614c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
615c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* do whole words */
616c74663799493f2b1e6123c18def94295d0afab7Kenny Root				while(msbits >= FLAC__BITS_PER_WORD) {
617c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bw->buffer[bw->words++] = 0;
618c74663799493f2b1e6123c18def94295d0afab7Kenny Root					msbits -= FLAC__BITS_PER_WORD;
619c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
620c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* do any leftovers */
621c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(msbits > 0) {
622c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bw->accum = 0;
623c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bw->bits = msbits;
624c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
625c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
626c74663799493f2b1e6123c18def94295d0afab7Kenny Rootbreak1:
627c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval |= mask1; /* set stop bit */
628c74663799493f2b1e6123c18def94295d0afab7Kenny Root			uval &= mask2; /* mask off unused top bits */
629c74663799493f2b1e6123c18def94295d0afab7Kenny Root
630c74663799493f2b1e6123c18def94295d0afab7Kenny Root			left = FLAC__BITS_PER_WORD - bw->bits;
631c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(lsbits < left) {
632c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->accum <<= lsbits;
633c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->accum |= uval;
634c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->bits += lsbits;
635c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
636c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
637c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
638c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * be > lsbits (because of previous assertions) so it would have
639c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * triggered the (lsbits<left) case above.
640c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 */
641c74663799493f2b1e6123c18def94295d0afab7Kenny Root				FLAC__ASSERT(bw->bits);
642c74663799493f2b1e6123c18def94295d0afab7Kenny Root				FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
643c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->accum <<= left;
644c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->accum |= uval >> (bw->bits = lsbits - left);
645c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
646c74663799493f2b1e6123c18def94295d0afab7Kenny Root				bw->accum = uval;
647c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
648c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 1
649c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
650c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
651c74663799493f2b1e6123c18def94295d0afab7Kenny Root		vals++;
652c74663799493f2b1e6123c18def94295d0afab7Kenny Root		nvals--;
653c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
654c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
655c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
656c74663799493f2b1e6123c18def94295d0afab7Kenny Root
657c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* UNUSED */
658c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
659c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
660c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned total_bits, msbs, uval;
661c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned k;
662c74663799493f2b1e6123c18def94295d0afab7Kenny Root
663c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
664c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
665c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter > 0);
666c74663799493f2b1e6123c18def94295d0afab7Kenny Root
667c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* fold signed to unsigned */
668c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(val < 0)
669c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (unsigned)(((-(++val)) << 1) + 1);
670c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
671c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (unsigned)(val << 1);
672c74663799493f2b1e6123c18def94295d0afab7Kenny Root
673c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
674c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
675c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned pattern;
676c74663799493f2b1e6123c18def94295d0afab7Kenny Root
677c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(k <= 30);
678c74663799493f2b1e6123c18def94295d0afab7Kenny Root
679c74663799493f2b1e6123c18def94295d0afab7Kenny Root		msbs = uval >> k;
680c74663799493f2b1e6123c18def94295d0afab7Kenny Root		total_bits = 1 + k + msbs;
681c74663799493f2b1e6123c18def94295d0afab7Kenny Root		pattern = 1 << k; /* the unary end bit */
682c74663799493f2b1e6123c18def94295d0afab7Kenny Root		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
683c74663799493f2b1e6123c18def94295d0afab7Kenny Root
684c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(total_bits <= 32) {
685c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
686c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
687c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
688c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
689c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* write the unary MSBs */
690c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
691c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
692c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* write the unary end bit and binary LSBs */
693c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
694c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
695c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
696c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
697c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
698c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned q, r, d;
699c74663799493f2b1e6123c18def94295d0afab7Kenny Root
700c74663799493f2b1e6123c18def94295d0afab7Kenny Root		d = (1 << (k+1)) - parameter;
701c74663799493f2b1e6123c18def94295d0afab7Kenny Root		q = uval / parameter;
702c74663799493f2b1e6123c18def94295d0afab7Kenny Root		r = uval - (q * parameter);
703c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the unary MSBs */
704c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitwriter_write_zeroes(bw, q))
705c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
706c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the unary end bit */
707c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
708c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
709c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the binary LSBs */
710c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(r >= d) {
711c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
712c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
713c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
714c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
715c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
716c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
717c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
718c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
719c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
720c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
721c74663799493f2b1e6123c18def94295d0afab7Kenny Root
722c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
723c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
724c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned total_bits, msbs;
725c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned k;
726c74663799493f2b1e6123c18def94295d0afab7Kenny Root
727c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
728c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
729c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter > 0);
730c74663799493f2b1e6123c18def94295d0afab7Kenny Root
731c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
732c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
733c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned pattern;
734c74663799493f2b1e6123c18def94295d0afab7Kenny Root
735c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(k <= 30);
736c74663799493f2b1e6123c18def94295d0afab7Kenny Root
737c74663799493f2b1e6123c18def94295d0afab7Kenny Root		msbs = uval >> k;
738c74663799493f2b1e6123c18def94295d0afab7Kenny Root		total_bits = 1 + k + msbs;
739c74663799493f2b1e6123c18def94295d0afab7Kenny Root		pattern = 1 << k; /* the unary end bit */
740c74663799493f2b1e6123c18def94295d0afab7Kenny Root		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
741c74663799493f2b1e6123c18def94295d0afab7Kenny Root
742c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(total_bits <= 32) {
743c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
744c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
745c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
746c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
747c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* write the unary MSBs */
748c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
749c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
750c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* write the unary end bit and binary LSBs */
751c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
752c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
753c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
754c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
755c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
756c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned q, r, d;
757c74663799493f2b1e6123c18def94295d0afab7Kenny Root
758c74663799493f2b1e6123c18def94295d0afab7Kenny Root		d = (1 << (k+1)) - parameter;
759c74663799493f2b1e6123c18def94295d0afab7Kenny Root		q = uval / parameter;
760c74663799493f2b1e6123c18def94295d0afab7Kenny Root		r = uval - (q * parameter);
761c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the unary MSBs */
762c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitwriter_write_zeroes(bw, q))
763c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
764c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the unary end bit */
765c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
766c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
767c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* write the binary LSBs */
768c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(r >= d) {
769c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
770c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
771c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
772c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
773c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
774c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
775c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
776c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
777c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
778c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
779c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif /* UNUSED */
780c74663799493f2b1e6123c18def94295d0afab7Kenny Root
781c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
782c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
783c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bool ok = 1;
784c74663799493f2b1e6123c18def94295d0afab7Kenny Root
785c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
786c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
787c74663799493f2b1e6123c18def94295d0afab7Kenny Root
788c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
789c74663799493f2b1e6123c18def94295d0afab7Kenny Root
790c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(val < 0x80) {
791c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
792c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
793c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x800) {
794c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
795c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
796c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
797c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x10000) {
798c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
799c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
800c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
801c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
802c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x200000) {
803c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
804c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
805c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
806c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
807c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
808c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x4000000) {
809c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
810c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
811c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
812c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
813c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
814c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
815c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
816c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
817c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
818c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
819c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
820c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
821c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
822c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
823c74663799493f2b1e6123c18def94295d0afab7Kenny Root
824c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return ok;
825c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
826c74663799493f2b1e6123c18def94295d0afab7Kenny Root
827c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
828c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
829c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bool ok = 1;
830c74663799493f2b1e6123c18def94295d0afab7Kenny Root
831c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw);
832c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != bw->buffer);
833c74663799493f2b1e6123c18def94295d0afab7Kenny Root
834c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
835c74663799493f2b1e6123c18def94295d0afab7Kenny Root
836c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(val < 0x80) {
837c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
838c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
839c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x800) {
840c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
841c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
842c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
843c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x10000) {
844c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
845c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
846c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
847c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
848c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x200000) {
849c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
850c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
851c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
852c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
853c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
854c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x4000000) {
855c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
856c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
857c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
858c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
859c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
860c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
861c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(val < 0x80000000) {
862c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
863c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
864c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
865c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
866c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
867c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
868c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
869c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
870c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
871c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
872c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
873c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
874c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
875c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
876c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
877c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
878c74663799493f2b1e6123c18def94295d0afab7Kenny Root
879c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return ok;
880c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
881c74663799493f2b1e6123c18def94295d0afab7Kenny Root
882c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
883c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
884c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* 0-pad to byte boundary */
885c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bw->bits & 7u)
886c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
887c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
888c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
889c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
890