bitbuffer.c 73.1 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
 *
4
5
6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
7
 *
8
9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
30
31
 */

Josh Coalson's avatar
Josh Coalson committed
32
33
34
35
#if HAVE_CONFIG_H
#  include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
36
37
38
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy(), memset() */
#include "private/bitbuffer.h"
39
#include "private/bitmath.h"
40
#include "private/crc.h"
41
#include "FLAC/assert.h"
Josh Coalson's avatar
Josh Coalson committed
42

43
44
45
46
47
48
49
50
/*
 * Along the way you will see two versions of some functions, selected
 * by a FLAC__NO_MANUAL_INLINING macro.  One is the simplified, more
 * readable, and slow version, and the other is the same function
 * where crucial parts have been manually inlined and are much faster.
 *
 */

51
52
53
54
55
56
57
/*
 * Some optimization strategies are slower with older versions of MSVC
 */
#if defined _MSC_VER && _MSC_VER <= 1200
#define FLAC__OLD_MSVC_FLAVOR
#endif

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * This should be at least twice as large as the largest number of blurbs
 * required to represent any 'number' (in any encoding) you are going to
 * read.  With FLAC this is on the order of maybe a few hundred bits.
 * If the buffer is smaller than that, the decoder won't be able to read
 * in a whole number that is in a variable length encoding (e.g. Rice).
 *
 * The number we are actually using here is based on what would be the
 * approximate maximum size of a verbatim frame at the default block size,
 * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
 * 32kbytes sounds reasonable.  For kicks we subtract out 64 bytes for any
 * alignment or malloc overhead.
 *
 * Increase this number to decrease the number of read callbacks, at the
 * expense of using more memory.  Or decrease for the reverse effect,
 * keeping in mind the limit from the first paragraph.
 */
static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */

77
#ifndef FLAC__OLD_MSVC_FLAVOR
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
static const unsigned char byte_to_unary_table[] = {
	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
96
#endif
97

98
99
100
101
102
103
104
#if FLAC__BITS_PER_BLURB == 8
#define FLAC__BITS_PER_BLURB_LOG2 3
#define FLAC__BYTES_PER_BLURB 1
#define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
#define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
#define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
#define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
105
#ifndef FLAC__OLD_MSVC_FLAVOR
106
#define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
107
#endif
108
109
110
111
112
113
114
#elif FLAC__BITS_PER_BLURB == 32
#define FLAC__BITS_PER_BLURB_LOG2 5
#define FLAC__BYTES_PER_BLURB 4
#define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
#define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
#define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
#define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
115
#ifndef FLAC__OLD_MSVC_FLAVOR
116
#define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
117
#endif
118
119
120
#else
/* ERROR, only sizes of 8 and 32 are supported */
#endif
Josh Coalson's avatar
Josh Coalson committed
121

122
#define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
Josh Coalson's avatar
Josh Coalson committed
123

Josh Coalson's avatar
Josh Coalson committed
124
125
126
127
128
129
130
131
132
#ifdef min
#undef min
#endif
#define min(x,y) ((x)<(y)?(x):(y))
#ifdef max
#undef max
#endif
#define max(x,y) ((x)>(y)?(x):(y))

133
134
135
136
137
138
139
/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

140
141
#ifndef FLaC__INLINE
#define FLaC__INLINE
142
143
#endif

144
145
146
147
148
149
150
151
152
153
154
155
156
157
struct FLAC__BitBuffer {
	FLAC__blurb *buffer;
	unsigned capacity; /* in blurbs */
	unsigned blurbs, bits;
	unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
	unsigned consumed_blurbs, consumed_bits;
	unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
	FLAC__uint16 read_crc16;
#if FLAC__BITS_PER_BLURB == 32
	unsigned crc16_align;
#endif
	FLAC__blurb save_head, save_tail;
};

158
#if FLAC__BITS_PER_BLURB == 32
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
{
	if(bb->crc16_align == 0) {
		FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
		FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
		FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
		FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
	}
	else if(bb->crc16_align == 8) {
		FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
		FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
		FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
	}
	else if(bb->crc16_align == 16) {
		FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
		FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
	}
	else if(bb->crc16_align == 24) {
		FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
	}
	bb->crc16_align = 0;
}
181
#endif
182
183
184

/*
 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
Josh Coalson's avatar
Josh Coalson committed
185
 * does not shift out what is consumed, it just chops off the end, whether
186
187
188
189
 * there is unconsumed data there or not.  This is OK because currently we
 * never shrink the buffer, but if this ever changes, we'll have to do some
 * fixups here.
 */
Josh Coalson's avatar
Josh Coalson committed
190
static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
Josh Coalson's avatar
Josh Coalson committed
191
{
192
	FLAC__blurb *new_buffer;
Josh Coalson's avatar
Josh Coalson committed
193

Josh Coalson's avatar
Josh Coalson committed
194
195
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
196
197
198
199

	if(bb->capacity == new_capacity)
		return true;

200
	new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
Josh Coalson's avatar
Josh Coalson committed
201
202
	if(new_buffer == 0)
		return false;
203
204
205
	memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
	if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
		bb->blurbs = new_capacity;
Josh Coalson's avatar
Josh Coalson committed
206
		bb->bits = 0;
207
		bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
Josh Coalson's avatar
Josh Coalson committed
208
	}
209
210
	if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
		bb->consumed_blurbs = new_capacity;
Josh Coalson's avatar
Josh Coalson committed
211
		bb->consumed_bits = 0;
212
		bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
Josh Coalson's avatar
Josh Coalson committed
213
	}
Josh Coalson's avatar
Josh Coalson committed
214
	free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
Josh Coalson's avatar
Josh Coalson committed
215
216
217
218
219
	bb->buffer = new_buffer;
	bb->capacity = new_capacity;
	return true;
}

220
static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
Josh Coalson's avatar
Josh Coalson committed
221
222
223
{
	unsigned new_capacity;

224
	FLAC__ASSERT(min_blurbs_to_add > 0);
Josh Coalson's avatar
Josh Coalson committed
225

226
	new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
Josh Coalson's avatar
Josh Coalson committed
227
228
229
	return bitbuffer_resize_(bb, new_capacity);
}

Josh Coalson's avatar
Josh Coalson committed
230
static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
Josh Coalson's avatar
Josh Coalson committed
231
{
Josh Coalson's avatar
Josh Coalson committed
232
233
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
234

235
236
	if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
		return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
Josh Coalson's avatar
Josh Coalson committed
237
238
239
240
	else
		return true;
}

Josh Coalson's avatar
Josh Coalson committed
241
static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
Josh Coalson's avatar
Josh Coalson committed
242
243
{
	unsigned bytes;
244
	FLAC__byte *target;
Josh Coalson's avatar
Josh Coalson committed
245
246

	/* first shift the unconsumed buffer data toward the front as much as possible */
247
	if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
248
249
250
251
252
253
254
255
256
257
258
259
260
#if FLAC__BITS_PER_BLURB == 8
		/*
		 * memset and memcpy are usually implemented in assembly language
		 * by the system libc, and they can be much faster
		 */
		const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
		const unsigned r = bb->consumed_blurbs, l = r_end - r;
		memmove(&bb->buffer[0], &bb->buffer[r], l);
		memset(&bb->buffer[l], 0, r);
#elif FLAC__BITS_PER_BLURB == 32
		/* still needs optimization */
		const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
		unsigned l = 0, r = bb->consumed_blurbs;
Josh Coalson's avatar
Josh Coalson committed
261
262
263
264
		for( ; r < r_end; l++, r++)
			bb->buffer[l] = bb->buffer[r];
		for( ; l < r_end; l++)
			bb->buffer[l] = 0;
265
266
267
268
#else
		FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif /* FLAC__BITS_PER_BLURB == 32 or 8 */

269
270
271
		bb->blurbs -= bb->consumed_blurbs;
		bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
		bb->consumed_blurbs = 0;
Josh Coalson's avatar
Josh Coalson committed
272
273
		bb->total_consumed_bits = bb->consumed_bits;
	}
274

Josh Coalson's avatar
Josh Coalson committed
275
276
277
278
279
	/* grow if we need to */
	if(bb->capacity <= 1) {
		if(!bitbuffer_resize_(bb, 16))
			return false;
	}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

	/* set the target for reading, taking into account blurb alignment */
#if FLAC__BITS_PER_BLURB == 8
	/* blurb == byte, so no gyrations necessary: */
	target = bb->buffer + bb->blurbs;
	bytes = bb->capacity - bb->blurbs;
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	FLAC__ASSERT((bb->bits & 7) == 0);
	target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
	bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif

Josh Coalson's avatar
Josh Coalson committed
295
	/* finally, read in some data */
296
	if(!read_callback(target, &bytes, client_data))
Josh Coalson's avatar
Josh Coalson committed
297
		return false;
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

	/* now we have to handle partial blurb cases: */
#if FLAC__BITS_PER_BLURB == 8
	/* blurb == byte, so no gyrations necessary: */
	bb->blurbs += bytes;
	bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	{
		const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
		bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
		bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
		bb->total_bits += (bytes << 3);
	}
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
Josh Coalson's avatar
Josh Coalson committed
315
316
317
	return true;
}

318
319
320
321
322
323
324
325
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/

FLAC__BitBuffer *FLAC__bitbuffer_new()
{
326
	FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
327

328
	/* calloc() implies:
Josh Coalson's avatar
Josh Coalson committed
329
		memset(bb, 0, sizeof(FLAC__BitBuffer));
330
331
332
333
		bb->buffer = 0;
		bb->capacity = 0;
		bb->blurbs = bb->bits = bb->total_bits = 0;
		bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
334
	*/
335
	return bb;
336
337
338
}

void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
Josh Coalson's avatar
Josh Coalson committed
339
{
Josh Coalson's avatar
Josh Coalson committed
340
	FLAC__ASSERT(0 != bb);
341

342
343
	FLAC__bitbuffer_free(bb);
	free(bb);
Josh Coalson's avatar
Josh Coalson committed
344
345
}

346
347
348
349
350
351
352
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
Josh Coalson's avatar
Josh Coalson committed
353
{
Josh Coalson's avatar
Josh Coalson committed
354
	FLAC__ASSERT(0 != bb);
355

356
357
358
359
360
361
	bb->buffer = 0;
	bb->capacity = 0;
	bb->blurbs = bb->bits = bb->total_bits = 0;
	bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;

	return FLAC__bitbuffer_clear(bb);
Josh Coalson's avatar
Josh Coalson committed
362
363
}

364
FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
365
{
Josh Coalson's avatar
Josh Coalson committed
366
	FLAC__ASSERT(0 != bb);
367
	FLAC__ASSERT(bytes > 0);
368

369
370
371
372
373
374
	if(!FLAC__bitbuffer_init(bb))
		return false;

	if(!bitbuffer_ensure_size_(bb, bytes << 3))
		return false;

Josh Coalson's avatar
Josh Coalson committed
375
	FLAC__ASSERT(0 != buffer);
376
377
378
379
380
381
	/* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
	memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
	bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
	bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
	bb->total_bits = bytes << 3;
	return true;
382
383
}

Josh Coalson's avatar
Josh Coalson committed
384
FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
Josh Coalson's avatar
Josh Coalson committed
385
386
{
	unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
387

Josh Coalson's avatar
Josh Coalson committed
388
389
	FLAC__ASSERT(0 != dest);
	FLAC__ASSERT(0 != src);
Josh Coalson's avatar
Josh Coalson committed
390
391
392
393
394
395
396
397

	if(bits_to_add == 0)
		return true;
	if(dest->bits != src->consumed_bits)
		return false;
	if(!bitbuffer_ensure_size_(dest, bits_to_add))
		return false;
	if(dest->bits == 0) {
398
		memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
Josh Coalson's avatar
Josh Coalson committed
399
	}
400
401
402
403
	else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
		dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
		dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
		memcpy(dest->buffer+dest->blurbs+1, src->buffer+src->consumed_blurbs+1, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs-1 + ((src->bits)? 1:0)));
Josh Coalson's avatar
Josh Coalson committed
404
405
	}
	else {
406
407
		dest->buffer[dest->blurbs] <<= bits_to_add;
		dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
Josh Coalson's avatar
Josh Coalson committed
408
409
410
	}
	dest->bits = src->bits;
	dest->total_bits += bits_to_add;
411
	dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
Josh Coalson's avatar
Josh Coalson committed
412
413
414
415
416
417

	return true;
}

void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
{
Josh Coalson's avatar
Josh Coalson committed
418
	FLAC__ASSERT(0 != bb);
419

Josh Coalson's avatar
Josh Coalson committed
420
	if(0 != bb->buffer)
Josh Coalson's avatar
Josh Coalson committed
421
422
423
		free(bb->buffer);
	bb->buffer = 0;
	bb->capacity = 0;
424
425
	bb->blurbs = bb->bits = bb->total_bits = 0;
	bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
Josh Coalson's avatar
Josh Coalson committed
426
427
}

Josh Coalson's avatar
Josh Coalson committed
428
FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
Josh Coalson's avatar
Josh Coalson committed
429
430
431
{
	if(bb->buffer == 0) {
		bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
432
		bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
Josh Coalson's avatar
Josh Coalson committed
433
434
435
436
		if(bb->buffer == 0)
			return false;
	}
	else {
437
		memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
Josh Coalson's avatar
Josh Coalson committed
438
	}
439
440
	bb->blurbs = bb->bits = bb->total_bits = 0;
	bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
Josh Coalson's avatar
Josh Coalson committed
441
442
443
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
444
FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
Josh Coalson's avatar
Josh Coalson committed
445
{
Josh Coalson's avatar
Josh Coalson committed
446
447
448
449
	FLAC__ASSERT(0 != dest);
	FLAC__ASSERT(0 != dest->buffer);
	FLAC__ASSERT(0 != src);
	FLAC__ASSERT(0 != src->buffer);
450

Josh Coalson's avatar
Josh Coalson committed
451
452
453
	if(dest->capacity < src->capacity)
		if(!bitbuffer_resize_(dest, src->capacity))
			return false;
454
455
	memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
	dest->blurbs = src->blurbs;
Josh Coalson's avatar
Josh Coalson committed
456
457
	dest->bits = src->bits;
	dest->total_bits = src->total_bits;
458
	dest->consumed_blurbs = src->consumed_blurbs;
Josh Coalson's avatar
Josh Coalson committed
459
460
	dest->consumed_bits = src->consumed_bits;
	dest->total_consumed_bits = src->total_consumed_bits;
461
	dest->read_crc16 = src->read_crc16;
Josh Coalson's avatar
Josh Coalson committed
462
463
464
	return true;
}

465
466
void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
{
Josh Coalson's avatar
Josh Coalson committed
467
468
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
	FLAC__ASSERT((bb->consumed_bits & 7) == 0);

	bb->read_crc16 = seed;
#if FLAC__BITS_PER_BLURB == 8
	/* no need to do anything */
#elif FLAC__BITS_PER_BLURB == 32
	bb->crc16_align = bb->consumed_bits;
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
}

FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
{
Josh Coalson's avatar
Josh Coalson committed
483
484
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
	FLAC__ASSERT((bb->bits & 7) == 0);
	FLAC__ASSERT((bb->consumed_bits & 7) == 0);

#if FLAC__BITS_PER_BLURB == 8
	/* no need to do anything */
#elif FLAC__BITS_PER_BLURB == 32
	/*@@@ BUG: even though this probably can't happen with FLAC, need to fix the case where we are called here for the very first blurb and crc16_align is > 0 */
	if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
		if(bb->consumed_bits == 8) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
		}
		else if(bb->consumed_bits == 16) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
		}
		else if(bb->consumed_bits == 24) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
		}
	}
	else {
		if(bb->consumed_bits == 8) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
		}
		else if(bb->consumed_bits == 16) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
		}
		else if(bb->consumed_bits == 24) {
			const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
			FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
			FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
		}
	}
	bb->crc16_align = bb->consumed_bits;
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
	return bb->read_crc16;
}

FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
{
	FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */

#if FLAC__BITS_PER_BLURB == 8
	return FLAC__crc16(bb->buffer, bb->blurbs);
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
}

FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
{
549
	FLAC__ASSERT(0 != bb);
550
	FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
551
	FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#if FLAC__BITS_PER_BLURB == 8
	return FLAC__crc8(bb->buffer, bb->blurbs);
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
}

FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
{
	return ((bb->bits & 7) == 0);
}

FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
{
	return ((bb->consumed_bits & 7) == 0);
}

unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
{
	return 8 - (bb->consumed_bits & 7);
}

unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
{
	FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
	return (bb->total_bits - bb->total_consumed_bits) >> 3;
}

void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
{
	FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
#if FLAC__BITS_PER_BLURB == 8
	*buffer = bb->buffer + bb->consumed_blurbs;
	*bytes = bb->blurbs - bb->consumed_blurbs;
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	*buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
	*bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
}

void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
{
#if FLAC__BITS_PER_BLURB == 8
	(void)bb;
#elif FLAC__BITS_PER_BLURB == 32
	/* @@@ WATCHOUT: code currently only works for big-endian: */
	(void)bb;
#else
	FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
#endif
}

Josh Coalson's avatar
Josh Coalson committed
610
FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
Josh Coalson's avatar
Josh Coalson committed
611
{
612
	unsigned n;
Josh Coalson's avatar
Josh Coalson committed
613

Josh Coalson's avatar
Josh Coalson committed
614
615
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
616
617
618
619
620
621
622

	if(bits == 0)
		return true;
	if(!bitbuffer_ensure_size_(bb, bits))
		return false;
	bb->total_bits += bits;
	while(bits > 0) {
623
624
		n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
		bb->buffer[bb->blurbs] <<= n;
Josh Coalson's avatar
Josh Coalson committed
625
626
		bits -= n;
		bb->bits += n;
627
628
		if(bb->bits == FLAC__BITS_PER_BLURB) {
			bb->blurbs++;
Josh Coalson's avatar
Josh Coalson committed
629
630
631
632
633
634
			bb->bits = 0;
		}
	}
	return true;
}

635
FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
Josh Coalson's avatar
Josh Coalson committed
636
637
638
{
	unsigned n, k;

Josh Coalson's avatar
Josh Coalson committed
639
640
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
641

642
	FLAC__ASSERT(bits <= 32);
Josh Coalson's avatar
Josh Coalson committed
643
644
	if(bits == 0)
		return true;
645
	/* inline the size check so we don't incure a function call unnecessarily */
646
	if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
647
648
649
		if(!bitbuffer_ensure_size_(bb, bits))
			return false;
	}
650
651

	/* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
652
653
	if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
		val &= (~(0xffffffff << bits)); /* zero-out unused bits */
654

Josh Coalson's avatar
Josh Coalson committed
655
656
	bb->total_bits += bits;
	while(bits > 0) {
657
658
659
660
		n = FLAC__BITS_PER_BLURB - bb->bits;
		if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
			if(bits < FLAC__BITS_PER_BLURB) {
				bb->buffer[bb->blurbs] = (FLAC__blurb)val;
Josh Coalson's avatar
Josh Coalson committed
661
662
663
				bb->bits = bits;
				break;
			}
664
665
			else if(bits == FLAC__BITS_PER_BLURB) {
				bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
Josh Coalson's avatar
Josh Coalson committed
666
667
668
				break;
			}
			else {
669
670
671
				k = bits - FLAC__BITS_PER_BLURB;
				bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
				/* we know k < 32 so no need to protect against the gcc bug mentioned above */
Josh Coalson's avatar
Josh Coalson committed
672
				val &= (~(0xffffffff << k));
673
				bits -= FLAC__BITS_PER_BLURB;
Josh Coalson's avatar
Josh Coalson committed
674
675
676
			}
		}
		else if(bits <= n) {
677
678
			bb->buffer[bb->blurbs] <<= bits;
			bb->buffer[bb->blurbs] |= val;
Josh Coalson's avatar
Josh Coalson committed
679
			if(bits == n) {
680
				bb->blurbs++;
Josh Coalson's avatar
Josh Coalson committed
681
682
683
684
685
686
687
688
				bb->bits = 0;
			}
			else
				bb->bits += bits;
			break;
		}
		else {
			k = bits - n;
689
690
691
			bb->buffer[bb->blurbs] <<= n;
			bb->buffer[bb->blurbs] |= (val >> k);
			/* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
Josh Coalson's avatar
Josh Coalson committed
692
693
			val &= (~(0xffffffff << k));
			bits -= n;
694
			bb->blurbs++;
Josh Coalson's avatar
Josh Coalson committed
695
696
697
698
699
700
701
			bb->bits = 0;
		}
	}

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
702
FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
Josh Coalson's avatar
Josh Coalson committed
703
{
Josh Coalson's avatar
Josh Coalson committed
704
	return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
Josh Coalson's avatar
Josh Coalson committed
705
706
}

Josh Coalson's avatar
Josh Coalson committed
707
FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
Josh Coalson's avatar
Josh Coalson committed
708
{
Josh Coalson's avatar
Josh Coalson committed
709
	static const FLAC__uint64 mask[] = {
Josh Coalson's avatar
Josh Coalson committed
710
		0,
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
		FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
		FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
		FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
		FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
		FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
		FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
		FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
		FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
		FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
		FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
		FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
		FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
		FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
		FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
		FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
		FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
Josh Coalson's avatar
Josh Coalson committed
727
728
729
	};
	unsigned n, k;

Josh Coalson's avatar
Josh Coalson committed
730
731
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
732

733
	FLAC__ASSERT(bits <= 64);
Josh Coalson's avatar
Josh Coalson committed
734
735
736
737
738
739
740
741
	if(bits == 0)
		return true;
	if(!bitbuffer_ensure_size_(bb, bits))
		return false;
	val &= mask[bits];
	bb->total_bits += bits;
	while(bits > 0) {
		if(bb->bits == 0) {
742
743
			if(bits < FLAC__BITS_PER_BLURB) {
				bb->buffer[bb->blurbs] = (FLAC__blurb)val;
Josh Coalson's avatar
Josh Coalson committed
744
745
746
				bb->bits = bits;
				break;
			}
747
748
			else if(bits == FLAC__BITS_PER_BLURB) {
				bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
Josh Coalson's avatar
Josh Coalson committed
749
750
751
				break;
			}
			else {
752
753
754
				k = bits - FLAC__BITS_PER_BLURB;
				bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
				/* we know k < 64 so no need to protect against the gcc bug mentioned above */
755
				val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
756
				bits -= FLAC__BITS_PER_BLURB;
Josh Coalson's avatar
Josh Coalson committed
757
758
759
			}
		}
		else {
760
			n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
Josh Coalson's avatar
Josh Coalson committed
761
			k = bits - n;
762
763
764
			bb->buffer[bb->blurbs] <<= n;
			bb->buffer[bb->blurbs] |= (val >> k);
			/* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
765
			val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
Josh Coalson's avatar
Josh Coalson committed
766
767
			bits -= n;
			bb->bits += n;
768
769
			if(bb->bits == FLAC__BITS_PER_BLURB) {
				bb->blurbs++;
Josh Coalson's avatar
Josh Coalson committed
770
771
772
773
774
775
776
777
				bb->bits = 0;
			}
		}
	}

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
778
#if 0 /* UNUSED */
Josh Coalson's avatar
Josh Coalson committed
779
FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
Josh Coalson's avatar
Josh Coalson committed
780
{
Josh Coalson's avatar
Josh Coalson committed
781
	return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
Josh Coalson's avatar
Josh Coalson committed
782
}
Josh Coalson's avatar
Josh Coalson committed
783
#endif
Josh Coalson's avatar
Josh Coalson committed
784

785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
{
	/* this doesn't need to be that fast as currently it is only used for vorbis comments */

	/* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
	if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
		return false;
	if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
		return false;
	if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
		return false;
	if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
		return false;

	return true;
}

FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
{
	unsigned i;

	/* this could be faster but currently we don't need it to be */
	for(i = 0; i < nvals; i++) {
		if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
			return false;
	}

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
815
FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
816
817
818
819
820
821
822
823
824
825
826
827
{
	if(val < 32)
		return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
	else if(val < 64)
		return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
	else {
		if(!FLAC__bitbuffer_write_zeroes(bb, val))
			return false;
		return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
	}
}

828
829
830
831
unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
{
	unsigned msbs, uval;

Josh Coalson's avatar
Josh Coalson committed
832
	/* fold signed to unsigned */
833
834
835
	if(val < 0)
		/* equivalent to
		 *     (unsigned)(((--val) << 1) - 1);
Josh Coalson's avatar
Josh Coalson committed
836
		 * but without the overflow problem at MININT
837
838
839
840
841
842
843
844
845
846
		 */
		uval = (unsigned)(((-(++val)) << 1) + 1);
	else
		uval = (unsigned)(val << 1);

	msbs = uval >> parameter;

	return 1 + parameter + msbs;
}

847
#if 0 /* UNUSED */
848
849
850
851
852
unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
{
	unsigned bits, msbs, uval;
	unsigned k;

853
	FLAC__ASSERT(parameter > 0);
854

Josh Coalson's avatar
Josh Coalson committed
855
	/* fold signed to unsigned */
856
857
858
	if(val < 0)
		/* equivalent to
		 *     (unsigned)(((--val) << 1) - 1);
Josh Coalson's avatar
Josh Coalson committed
859
		 * but without the overflow problem at MININT
860
861
862
863
864
		 */
		uval = (unsigned)(((-(++val)) << 1) + 1);
	else
		uval = (unsigned)(val << 1);

865
	k = FLAC__bitmath_ilog2(parameter);
866
	if(parameter == 1u<<k) {
867
		FLAC__ASSERT(k <= 30);
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886

		msbs = uval >> k;
		bits = 1 + k + msbs;
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);

		bits = 1 + q + k;
		if(r >= d)
			bits++;
	}
	return bits;
}

unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
Josh Coalson's avatar
Josh Coalson committed
887
888
{
	unsigned bits, msbs;
889
890
	unsigned k;

891
	FLAC__ASSERT(parameter > 0);
892

893
	k = FLAC__bitmath_ilog2(parameter);
894
	if(parameter == 1u<<k) {
895
		FLAC__ASSERT(k <= 30);
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912

		msbs = uval >> k;
		bits = 1 + k + msbs;
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);

		bits = 1 + q + k;
		if(r >= d)
			bits++;
	}
	return bits;
}
913
#endif /* UNUSED */
914

Josh Coalson's avatar
Josh Coalson committed
915
FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
916
{
917
	unsigned total_bits, interesting_bits, msbs, uval;
Josh Coalson's avatar
Josh Coalson committed
918
	FLAC__uint32 pattern;
Josh Coalson's avatar
Josh Coalson committed
919

Josh Coalson's avatar
Josh Coalson committed
920
921
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
922
	FLAC__ASSERT(parameter <= 30);
923

Josh Coalson's avatar
Josh Coalson committed
924
	/* fold signed to unsigned */
925
926
927
	if(val < 0)
		/* equivalent to
		 *     (unsigned)(((--val) << 1) - 1);
Josh Coalson's avatar
Josh Coalson committed
928
		 * but without the overflow problem at MININT
929
930
		 */
		uval = (unsigned)(((-(++val)) << 1) + 1);
Josh Coalson's avatar
Josh Coalson committed
931
	else
932
		uval = (unsigned)(val << 1);
Josh Coalson's avatar
Josh Coalson committed
933

934
	msbs = uval >> parameter;
935
936
	interesting_bits = 1 + parameter;
	total_bits = interesting_bits + msbs;
937
938
	pattern = 1 << parameter; /* the unary end bit */
	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
Josh Coalson's avatar
Josh Coalson committed
939

940
941
	if(total_bits <= 32) {
		if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
Josh Coalson's avatar
Josh Coalson committed
942
943
944
945
946
947
			return false;
	}
	else {
		/* write the unary MSBs */
		if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
			return false;
948
		/* write the unary end bit and binary LSBs */
949
		if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
Josh Coalson's avatar
Josh Coalson committed
950
951
952
953
954
			return false;
	}
	return true;
}

955
#if 0 /* UNUSED */
Josh Coalson's avatar
Josh Coalson committed
956
FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
Josh Coalson's avatar
Josh Coalson committed
957
{
958
	unsigned total_bits, interesting_bits, msbs, uval;
Josh Coalson's avatar
Josh Coalson committed
959
	FLAC__uint32 pattern;
Josh Coalson's avatar
Josh Coalson committed
960

Josh Coalson's avatar
Josh Coalson committed
961
962
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
963
	FLAC__ASSERT(parameter <= 30);
Josh Coalson's avatar
Josh Coalson committed
964
965
966

	*overflow = false;

Josh Coalson's avatar
Josh Coalson committed
967
	/* fold signed to unsigned */
968
969
970
	if(val < 0)
		/* equivalent to
		 *     (unsigned)(((--val) << 1) - 1);
Josh Coalson's avatar
Josh Coalson committed
971
		 * but without the overflow problem at MININT
972
973
		 */
		uval = (unsigned)(((-(++val)) << 1) + 1);
Josh Coalson's avatar
Josh Coalson committed
974
	else
975
		uval = (unsigned)(val << 1);
Josh Coalson's avatar
Josh Coalson committed
976

977
	msbs = uval >> parameter;
978
979
	interesting_bits = 1 + parameter;
	total_bits = interesting_bits + msbs;
980
981
	pattern = 1 << parameter; /* the unary end bit */
	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
Josh Coalson's avatar
Josh Coalson committed
982

983
984
	if(total_bits <= 32) {
		if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
Josh Coalson's avatar
Josh Coalson committed
985
986
			return false;
	}
987
	else if(total_bits > max_bits) {
Josh Coalson's avatar
Josh Coalson committed
988
989
990
991
		*overflow = true;
		return true;
	}
	else {
992
993
		/* write the unary MSBs */
		if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
Josh Coalson's avatar
Josh Coalson committed
994
			return false;
995
		/* write the unary end bit and binary LSBs */
996
		if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
Josh Coalson's avatar
Josh Coalson committed
997
			return false;
998
999
1000
	}
	return true;
}
1001
#endif /* UNUSED */
1002

1003
#if 0 /* UNUSED */
Josh Coalson's avatar
Josh Coalson committed
1004
FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1005
{
1006
	unsigned total_bits, msbs, uval;
1007
1008
	unsigned k;

Josh Coalson's avatar
Josh Coalson committed
1009
1010
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
1011
	FLAC__ASSERT(parameter > 0);
1012

Josh Coalson's avatar
Josh Coalson committed
1013
	/* fold signed to unsigned */
1014
1015
1016
	if(val < 0)
		/* equivalent to
		 *     (unsigned)(((--val) << 1) - 1);
Josh Coalson's avatar
Josh Coalson committed
1017
		 * but without the overflow problem at MININT
1018
1019
1020
1021
1022
		 */
		uval = (unsigned)(((-(++val)) << 1) + 1);
	else
		uval = (unsigned)(val << 1);

1023
	k = FLAC__bitmath_ilog2(parameter);
1024
1025
1026
	if(parameter == 1u<<k) {
		unsigned pattern;

1027
		FLAC__ASSERT(k <= 30);
1028
1029

		msbs = uval >> k;
1030
		total_bits = 1 + k + msbs;
1031
1032
1033
		pattern = 1 << k; /* the unary end bit */
		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */

1034
1035
		if(total_bits <= 32) {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
				return false;
		}
		else {
			/* write the unary MSBs */
			if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
				return false;
			/* write the unary end bit and binary LSBs */
			if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
				return false;
		}
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);
Josh Coalson's avatar
Josh Coalson committed
1053
		/* write the unary MSBs */
1054
		if(!FLAC__bitbuffer_write_zeroes(bb, q))
Josh Coalson's avatar
Josh Coalson committed
1055
			return false;
1056
		/* write the unary end bit */
Josh Coalson's avatar
Josh Coalson committed
1057
1058
		if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
			return false;
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
		/* write the binary LSBs */
		if(r >= d) {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
				return false;
		}
		else {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
				return false;
		}
	}
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
1072
FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1073
{
1074
	unsigned total_bits, msbs;
1075
1076
	unsigned k;

Josh Coalson's avatar
Josh Coalson committed
1077
1078
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
1079
	FLAC__ASSERT(parameter > 0);
1080

1081
	k = FLAC__bitmath_ilog2(parameter);
1082
1083
1084
	if(parameter == 1u<<k) {
		unsigned pattern;

1085
		FLAC__ASSERT(k <= 30);
1086
1087

		msbs = uval >> k;
1088
		total_bits = 1 + k + msbs;
1089
1090
1091
		pattern = 1 << k; /* the unary end bit */
		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */

1092
1093
		if(total_bits <= 32) {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
				return false;
		}
		else {
			/* write the unary MSBs */
			if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
				return false;
			/* write the unary end bit and binary LSBs */
			if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
				return false;
		}
	}
	else {
		unsigned q, r, d;

		d = (1 << (k+1)) - parameter;
		q = uval / parameter;
		r = uval - (q * parameter);
		/* write the unary MSBs */
		if(!FLAC__bitbuffer_write_zeroes(bb, q))
			return false;
		/* write the unary end bit */
		if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
			return false;
		/* write the binary LSBs */
		if(r >= d) {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
				return false;
		}
		else {
			if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
				return false;
		}
Josh Coalson's avatar
Josh Coalson committed
1126
1127
1128
	}
	return true;
}
1129
#endif /* UNUSED */
Josh Coalson's avatar
Josh Coalson committed
1130

Josh Coalson's avatar
Josh Coalson committed
1131
FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
Josh Coalson's avatar
Josh Coalson committed
1132
{
Josh Coalson's avatar
Josh Coalson committed
1133
	FLAC__bool ok = 1;
Josh Coalson's avatar
Josh Coalson committed
1134

Josh Coalson's avatar
Josh Coalson committed
1135
1136
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
1137

1138
	FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
Josh Coalson's avatar
Josh Coalson committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176

	if(val < 0x80) {
		return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
	}
	else if(val < 0x800) {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x10000) {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x200000) {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
	}
	else if(val < 0x4000000) {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
	}
	else {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
	}

	return ok;
}

Josh Coalson's avatar
Josh Coalson committed
1177
FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
Josh Coalson's avatar
Josh Coalson committed
1178
{
Josh Coalson's avatar
Josh Coalson committed
1179
	FLAC__bool ok = 1;
Josh Coalson's avatar
Josh Coalson committed
1180

Josh Coalson's avatar
Josh Coalson committed
1181
1182
	FLAC__ASSERT(0 != bb);
	FLAC__ASSERT(0 != bb->buffer);
Josh Coalson's avatar
Josh Coalson committed
1183

1184
	FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
Josh Coalson's avatar
Josh Coalson committed
1185
1186

	if(val < 0x80) {
Josh Coalson's avatar
Josh Coalson committed
1187
		return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
Josh Coalson's avatar
Josh Coalson committed
1188
1189
	}
	else if(val < 0x800) {
Josh Coalson's avatar
Josh Coalson committed
1190
1191
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1192
1193
	}
	else if(val < 0x10000) {
Josh Coalson's avatar
Josh Coalson committed
1194
1195
1196
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1197
1198
	}
	else if(val < 0x200000) {
Josh Coalson's avatar
Josh Coalson committed
1199
1200
1201
1202
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1203
1204
	}
	else if(val < 0x4000000) {
Josh Coalson's avatar
Josh Coalson committed
1205
1206
1207
1208
1209
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1210
1211
	}
	else if(val < 0x80000000) {
Josh Coalson's avatar
Josh Coalson committed
1212
1213
1214
1215
1216
1217
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1218
1219
1220
	}
	else {
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
Josh Coalson's avatar
Josh Coalson committed
1221
1222
1223
1224
1225
1226
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
		ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
Josh Coalson's avatar
Josh Coalson committed
1227
1228
1229
1230
1231
	}

	return ok;
}

Josh Coalson's avatar
Josh Coalson committed
1232
FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)