1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <stdlib.h> /* for malloc() */
21 #include <string.h> /* for memcpy(), memset() */
22 #include "private/bitbuffer.h"
23 #include "private/bitmath.h"
24 #include "private/crc.h"
25 #include "FLAC/assert.h"
28 * Along the way you will see two versions of some functions, selected
29 * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
30 * readable, and slow version, and the other is the same function
31 * where crucial parts have been manually inlined and are much faster.
36 * This should be at least twice as large as the largest number of blurbs
37 * required to represent any 'number' (in any encoding) you are going to
38 * read. With FLAC this is on the order of maybe a few hundred bits.
39 * If the buffer is smaller than that, the decoder won't be able to read
40 * in a whole number that is in a variable length encoding (e.g. Rice).
42 * The number we are actually using here is based on what would be the
43 * approximate maximum size of a verbatim frame at the default block size,
44 * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
45 * 32kbytes sounds reasonable. For kicks we subtract out 64 bytes for any
46 * alignment or malloc overhead.
48 * Increase this number to decrease the number of read callbacks, at the
49 * expense of using more memory. Or decrease for the reverse effect,
50 * keeping in mind the limit from the first paragraph.
52 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
54 #if FLAC__BITS_PER_BLURB == 8
55 #define FLAC__BITS_PER_BLURB_LOG2 3
56 #define FLAC__BYTES_PER_BLURB 1
57 #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
58 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
59 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
60 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
61 #elif FLAC__BITS_PER_BLURB == 32
62 #define FLAC__BITS_PER_BLURB_LOG2 5
63 #define FLAC__BYTES_PER_BLURB 4
64 #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
65 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
66 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
67 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
69 /* ERROR, only sizes of 8 and 32 are supported */
72 #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
77 #define min(x,y) ((x)<(y)?(x):(y))
81 #define max(x,y) ((x)>(y)?(x):(y))
87 struct FLAC__BitBuffer {
89 unsigned capacity; /* in blurbs */
90 unsigned blurbs, bits;
91 unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
92 unsigned consumed_blurbs, consumed_bits;
93 unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
94 FLAC__uint16 read_crc16;
95 #if FLAC__BITS_PER_BLURB == 32
98 FLAC__blurb save_head, save_tail;
101 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
103 #if FLAC__BITS_PER_BLURB == 32
104 if(bb->crc16_align == 0) {
105 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
106 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
107 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
108 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
110 else if(bb->crc16_align == 8) {
111 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
112 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
113 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
115 else if(bb->crc16_align == 16) {
116 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
117 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
119 else if(bb->crc16_align == 24) {
120 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
124 (void)bb; (void)blurb;
130 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
131 * does not shift left what is consumed, it just chops off the end, whether
132 * there is unconsumed data there or not. This is OK because currently we
133 * never shrink the buffer, but if this ever changes, we'll have to do some
136 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
138 FLAC__blurb *new_buffer;
140 FLAC__ASSERT(bb != 0);
141 FLAC__ASSERT(bb->buffer != 0);
143 if(bb->capacity == new_capacity)
146 new_buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * new_capacity);
149 memset(new_buffer, 0, sizeof(FLAC__blurb) * new_capacity);
150 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
151 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
152 bb->blurbs = new_capacity;
154 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
156 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
157 bb->consumed_blurbs = new_capacity;
158 bb->consumed_bits = 0;
159 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
161 free(bb->buffer); // we've already asserted above that (bb->buffer != 0)
162 bb->buffer = new_buffer;
163 bb->capacity = new_capacity;
167 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
169 unsigned new_capacity;
171 FLAC__ASSERT(min_blurbs_to_add > 0);
173 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
174 return bitbuffer_resize_(bb, new_capacity);
177 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
179 FLAC__ASSERT(bb != 0);
180 FLAC__ASSERT(bb->buffer != 0);
182 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
183 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
188 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)
193 /* first shift the unconsumed buffer data toward the front as much as possible */
194 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
195 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
196 for( ; r < r_end; l++, r++)
197 bb->buffer[l] = bb->buffer[r];
198 for( ; l < r_end; l++)
200 bb->blurbs -= bb->consumed_blurbs;
201 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
202 bb->consumed_blurbs = 0;
203 bb->total_consumed_bits = bb->consumed_bits;
206 /* grow if we need to */
207 if(bb->capacity <= 1) {
208 if(!bitbuffer_resize_(bb, 16))
212 /* set the target for reading, taking into account blurb alignment */
213 #if FLAC__BITS_PER_BLURB == 8
214 /* blurb == byte, so no gyrations necessary: */
215 target = bb->buffer + bb->blurbs;
216 bytes = bb->capacity - bb->blurbs;
217 #elif FLAC__BITS_PER_BLURB == 32
218 /* @@@ WATCHOUT: code currently only works for big-endian: */
219 FLAC__ASSERT((bb->bits & 7) == 0);
220 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
221 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
223 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
226 /* finally, read in some data */
227 if(!read_callback(target, &bytes, client_data))
230 /* now we have to handle partial blurb cases: */
231 #if FLAC__BITS_PER_BLURB == 8
232 /* blurb == byte, so no gyrations necessary: */
234 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
235 #elif FLAC__BITS_PER_BLURB == 32
236 /* @@@ WATCHOUT: code currently only works for big-endian: */
238 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
239 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
240 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
241 bb->total_bits += (bytes << 3);
244 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
249 /***********************************************************************
251 * Class constructor/destructor
253 ***********************************************************************/
255 FLAC__BitBuffer *FLAC__bitbuffer_new()
257 return (FLAC__BitBuffer*)malloc(sizeof(FLAC__BitBuffer));
260 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
262 FLAC__ASSERT(bb != 0);
264 FLAC__bitbuffer_free(bb);
268 /***********************************************************************
270 * Public class methods
272 ***********************************************************************/
274 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
276 FLAC__ASSERT(bb != 0);
280 bb->blurbs = bb->bits = bb->total_bits = 0;
281 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
283 return FLAC__bitbuffer_clear(bb);
286 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
288 FLAC__ASSERT(bb != 0);
289 FLAC__ASSERT(bytes > 0);
291 if(!FLAC__bitbuffer_init(bb))
294 if(!bitbuffer_ensure_size_(bb, bytes << 3))
297 FLAC__ASSERT(buffer != 0);
298 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
299 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
300 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
301 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
302 bb->total_bits = bytes << 3;
306 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
308 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
310 FLAC__ASSERT(dest != 0);
311 FLAC__ASSERT(src != 0);
315 if(dest->bits != src->consumed_bits)
317 if(!bitbuffer_ensure_size_(dest, bits_to_add))
319 if(dest->bits == 0) {
320 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
322 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
323 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
324 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
325 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)));
328 dest->buffer[dest->blurbs] <<= bits_to_add;
329 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
331 dest->bits = src->bits;
332 dest->total_bits += bits_to_add;
333 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
338 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
340 FLAC__ASSERT(bb != 0);
346 bb->blurbs = bb->bits = bb->total_bits = 0;
347 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
350 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
352 if(bb->buffer == 0) {
353 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
354 bb->buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * bb->capacity);
357 memset(bb->buffer, 0, bb->capacity);
360 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
362 bb->blurbs = bb->bits = bb->total_bits = 0;
363 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
367 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
369 FLAC__ASSERT(dest != 0);
370 FLAC__ASSERT(dest->buffer != 0);
371 FLAC__ASSERT(src != 0);
372 FLAC__ASSERT(src->buffer != 0);
374 if(dest->capacity < src->capacity)
375 if(!bitbuffer_resize_(dest, src->capacity))
377 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
378 dest->blurbs = src->blurbs;
379 dest->bits = src->bits;
380 dest->total_bits = src->total_bits;
381 dest->consumed_blurbs = src->consumed_blurbs;
382 dest->consumed_bits = src->consumed_bits;
383 dest->total_consumed_bits = src->total_consumed_bits;
384 dest->read_crc16 = src->read_crc16;
388 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
390 FLAC__ASSERT(bb != 0);
391 FLAC__ASSERT(bb->buffer != 0);
392 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
394 bb->read_crc16 = seed;
395 #if FLAC__BITS_PER_BLURB == 8
396 /* no need to do anything */
397 #elif FLAC__BITS_PER_BLURB == 32
398 bb->crc16_align = bb->consumed_bits;
400 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
404 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
406 FLAC__ASSERT(bb != 0);
407 FLAC__ASSERT(bb->buffer != 0);
408 FLAC__ASSERT((bb->bits & 7) == 0);
409 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
411 #if FLAC__BITS_PER_BLURB == 8
412 /* no need to do anything */
413 #elif FLAC__BITS_PER_BLURB == 32
414 /*@@@ 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 */
415 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
416 if(bb->consumed_bits == 8) {
417 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
418 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
420 else if(bb->consumed_bits == 16) {
421 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
422 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
423 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
425 else if(bb->consumed_bits == 24) {
426 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
427 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
428 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
429 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
433 if(bb->consumed_bits == 8) {
434 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
435 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
437 else if(bb->consumed_bits == 16) {
438 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
439 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
440 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
442 else if(bb->consumed_bits == 24) {
443 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
444 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
445 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
446 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
449 bb->crc16_align = bb->consumed_bits;
451 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
453 return bb->read_crc16;
456 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
458 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
460 #if FLAC__BITS_PER_BLURB == 8
461 return FLAC__crc16(bb->buffer, bb->blurbs);
462 #elif FLAC__BITS_PER_BLURB == 32
463 /* @@@ WATCHOUT: code currently only works for big-endian: */
464 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
466 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
470 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
472 FLAC__ASSERT(bb->blurbs == 0);
473 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
474 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
475 #if FLAC__BITS_PER_BLURB == 8
476 return FLAC__crc8(bb->buffer, bb->blurbs);
477 #elif FLAC__BITS_PER_BLURB == 32
478 /* @@@ WATCHOUT: code currently only works for big-endian: */
479 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
481 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
485 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
487 return ((bb->bits & 7) == 0);
490 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
492 return ((bb->consumed_bits & 7) == 0);
495 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
497 return 8 - (bb->consumed_bits & 7);
500 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
502 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
503 return (bb->total_bits - bb->total_consumed_bits) >> 3;
506 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
508 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
509 #if FLAC__BITS_PER_BLURB == 8
510 *buffer = bb->buffer + bb->consumed_blurbs;
511 *bytes = bb->blurbs - bb->consumed_blurbs;
512 #elif FLAC__BITS_PER_BLURB == 32
513 /* @@@ WATCHOUT: code currently only works for big-endian: */
514 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
515 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
517 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
521 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
523 #if FLAC__BITS_PER_BLURB == 8
525 #elif FLAC__BITS_PER_BLURB == 32
526 /* @@@ WATCHOUT: code currently only works for big-endian: */
529 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
533 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
537 FLAC__ASSERT(bb != 0);
538 FLAC__ASSERT(bb->buffer != 0);
542 if(!bitbuffer_ensure_size_(bb, bits))
544 bb->total_bits += bits;
546 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
547 bb->buffer[bb->blurbs] <<= n;
550 if(bb->bits == FLAC__BITS_PER_BLURB) {
558 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
562 FLAC__ASSERT(bb != 0);
563 FLAC__ASSERT(bb->buffer != 0);
565 FLAC__ASSERT(bits <= 32);
568 /* inline the size check so we don't incure a function call unnecessarily */
569 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
570 if(!bitbuffer_ensure_size_(bb, bits))
573 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
574 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
575 bb->total_bits += bits;
577 n = FLAC__BITS_PER_BLURB - bb->bits;
578 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
579 if(bits < FLAC__BITS_PER_BLURB) {
580 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
584 else if(bits == FLAC__BITS_PER_BLURB) {
585 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
589 k = bits - FLAC__BITS_PER_BLURB;
590 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
591 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
592 val &= (~(0xffffffff << k));
593 bits -= FLAC__BITS_PER_BLURB;
597 bb->buffer[bb->blurbs] <<= bits;
598 bb->buffer[bb->blurbs] |= val;
609 bb->buffer[bb->blurbs] <<= n;
610 bb->buffer[bb->blurbs] |= (val >> k);
611 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
612 val &= (~(0xffffffff << k));
622 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
624 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
627 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
629 static const FLAC__uint64 mask[] = {
631 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
632 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
633 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
634 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
635 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
636 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
637 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
638 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
639 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
640 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
641 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
642 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
643 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
644 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
645 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
646 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
650 FLAC__ASSERT(bb != 0);
651 FLAC__ASSERT(bb->buffer != 0);
653 FLAC__ASSERT(bits <= 64);
656 if(!bitbuffer_ensure_size_(bb, bits))
659 bb->total_bits += bits;
662 if(bits < FLAC__BITS_PER_BLURB) {
663 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
667 else if(bits == FLAC__BITS_PER_BLURB) {
668 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
672 k = bits - FLAC__BITS_PER_BLURB;
673 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
674 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
675 val &= (~(0xffffffffffffffff << k));
676 bits -= FLAC__BITS_PER_BLURB;
680 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
682 bb->buffer[bb->blurbs] <<= n;
683 bb->buffer[bb->blurbs] |= (val >> k);
684 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
685 val &= (~(0xffffffffffffffff << k));
688 if(bb->bits == FLAC__BITS_PER_BLURB) {
698 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
700 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
703 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
706 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
708 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
710 if(!FLAC__bitbuffer_write_zeroes(bb, val))
712 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
716 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
720 /* fold signed to unsigned */
723 * (unsigned)(((--val) << 1) - 1);
724 * but without the overflow problem at MININT
726 uval = (unsigned)(((-(++val)) << 1) + 1);
728 uval = (unsigned)(val << 1);
730 msbs = uval >> parameter;
732 return 1 + parameter + msbs;
736 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
738 unsigned bits, msbs, uval;
741 FLAC__ASSERT(parameter > 0);
743 /* fold signed to unsigned */
746 * (unsigned)(((--val) << 1) - 1);
747 * but without the overflow problem at MININT
749 uval = (unsigned)(((-(++val)) << 1) + 1);
751 uval = (unsigned)(val << 1);
753 k = FLAC__bitmath_ilog2(parameter);
754 if(parameter == 1u<<k) {
755 FLAC__ASSERT(k <= 30);
763 d = (1 << (k+1)) - parameter;
764 q = uval / parameter;
765 r = uval - (q * parameter);
774 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
779 FLAC__ASSERT(parameter > 0);
781 k = FLAC__bitmath_ilog2(parameter);
782 if(parameter == 1u<<k) {
783 FLAC__ASSERT(k <= 30);
791 d = (1 << (k+1)) - parameter;
792 q = uval / parameter;
793 r = uval - (q * parameter);
803 #ifdef FLAC__SYMMETRIC_RICE
804 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
806 unsigned total_bits, interesting_bits, msbs;
807 FLAC__uint32 pattern;
809 FLAC__ASSERT(bb != 0);
810 FLAC__ASSERT(bb->buffer != 0);
811 FLAC__ASSERT(parameter <= 31);
813 /* init pattern with the unary end bit and the sign bit */
821 msbs = val >> parameter;
822 interesting_bits = 2 + parameter;
823 total_bits = interesting_bits + msbs;
824 pattern <<= parameter;
825 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
827 if(total_bits <= 32) {
828 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
832 /* write the unary MSBs */
833 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
835 /* write the unary end bit, the sign bit, and binary LSBs */
836 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
843 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
845 unsigned total_bits, interesting_bits, msbs;
846 FLAC__uint32 pattern;
848 FLAC__ASSERT(bb != 0);
849 FLAC__ASSERT(bb->buffer != 0);
850 FLAC__ASSERT(parameter <= 31);
854 /* init pattern with the unary end bit and the sign bit */
862 msbs = val >> parameter;
863 interesting_bits = 2 + parameter;
864 total_bits = interesting_bits + msbs;
865 pattern <<= parameter;
866 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
868 if(total_bits <= 32) {
869 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
872 else if(total_bits > max_bits) {
877 /* write the unary MSBs */
878 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
880 /* write the unary end bit, the sign bit, and binary LSBs */
881 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
888 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
890 unsigned total_bits, val_bits;
891 FLAC__uint32 pattern;
893 FLAC__ASSERT(bb != 0);
894 FLAC__ASSERT(bb->buffer != 0);
895 FLAC__ASSERT(parameter <= 31);
897 val_bits = FLAC__bitmath_silog2(val);
898 total_bits = 2 + parameter + 5 + val_bits;
900 if(total_bits <= 32) {
902 pattern <<= (parameter + 5);
904 pattern <<= val_bits;
905 pattern |= (val & ((1 << val_bits) - 1));
906 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
910 /* write the '-0' escape code first */
911 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
913 /* write the length */
914 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
916 /* write the value */
917 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
922 #endif /* ifdef FLAC__SYMMETRIC_RICE */
924 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
926 unsigned total_bits, interesting_bits, msbs, uval;
927 FLAC__uint32 pattern;
929 FLAC__ASSERT(bb != 0);
930 FLAC__ASSERT(bb->buffer != 0);
931 FLAC__ASSERT(parameter <= 30);
933 /* fold signed to unsigned */
936 * (unsigned)(((--val) << 1) - 1);
937 * but without the overflow problem at MININT
939 uval = (unsigned)(((-(++val)) << 1) + 1);
941 uval = (unsigned)(val << 1);
943 msbs = uval >> parameter;
944 interesting_bits = 1 + parameter;
945 total_bits = interesting_bits + msbs;
946 pattern = 1 << parameter; /* the unary end bit */
947 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
949 if(total_bits <= 32) {
950 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
954 /* write the unary MSBs */
955 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
957 /* write the unary end bit and binary LSBs */
958 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
965 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
967 unsigned total_bits, interesting_bits, msbs, uval;
968 FLAC__uint32 pattern;
970 FLAC__ASSERT(bb != 0);
971 FLAC__ASSERT(bb->buffer != 0);
972 FLAC__ASSERT(parameter <= 30);
976 /* fold signed to unsigned */
979 * (unsigned)(((--val) << 1) - 1);
980 * but without the overflow problem at MININT
982 uval = (unsigned)(((-(++val)) << 1) + 1);
984 uval = (unsigned)(val << 1);
986 msbs = uval >> parameter;
987 interesting_bits = 1 + parameter;
988 total_bits = interesting_bits + msbs;
989 pattern = 1 << parameter; /* the unary end bit */
990 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
992 if(total_bits <= 32) {
993 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
996 else if(total_bits > max_bits) {
1001 /* write the unary MSBs */
1002 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1004 /* write the unary end bit and binary LSBs */
1005 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1013 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1015 unsigned total_bits, msbs, uval;
1018 FLAC__ASSERT(bb != 0);
1019 FLAC__ASSERT(bb->buffer != 0);
1020 FLAC__ASSERT(parameter > 0);
1022 /* fold signed to unsigned */
1025 * (unsigned)(((--val) << 1) - 1);
1026 * but without the overflow problem at MININT
1028 uval = (unsigned)(((-(++val)) << 1) + 1);
1030 uval = (unsigned)(val << 1);
1032 k = FLAC__bitmath_ilog2(parameter);
1033 if(parameter == 1u<<k) {
1036 FLAC__ASSERT(k <= 30);
1039 total_bits = 1 + k + msbs;
1040 pattern = 1 << k; /* the unary end bit */
1041 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1043 if(total_bits <= 32) {
1044 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1048 /* write the unary MSBs */
1049 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1051 /* write the unary end bit and binary LSBs */
1052 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1059 d = (1 << (k+1)) - parameter;
1060 q = uval / parameter;
1061 r = uval - (q * parameter);
1062 /* write the unary MSBs */
1063 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1065 /* write the unary end bit */
1066 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1068 /* write the binary LSBs */
1070 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1074 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1081 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1083 unsigned total_bits, msbs;
1086 FLAC__ASSERT(bb != 0);
1087 FLAC__ASSERT(bb->buffer != 0);
1088 FLAC__ASSERT(parameter > 0);
1090 k = FLAC__bitmath_ilog2(parameter);
1091 if(parameter == 1u<<k) {
1094 FLAC__ASSERT(k <= 30);
1097 total_bits = 1 + k + msbs;
1098 pattern = 1 << k; /* the unary end bit */
1099 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1101 if(total_bits <= 32) {
1102 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1106 /* write the unary MSBs */
1107 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1109 /* write the unary end bit and binary LSBs */
1110 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1117 d = (1 << (k+1)) - parameter;
1118 q = uval / parameter;
1119 r = uval - (q * parameter);
1120 /* write the unary MSBs */
1121 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1123 /* write the unary end bit */
1124 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1126 /* write the binary LSBs */
1128 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1132 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1140 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1144 FLAC__ASSERT(bb != 0);
1145 FLAC__ASSERT(bb->buffer != 0);
1147 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1150 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1152 else if(val < 0x800) {
1153 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1154 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1156 else if(val < 0x10000) {
1157 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1158 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1159 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1161 else if(val < 0x200000) {
1162 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1163 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1164 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1165 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1167 else if(val < 0x4000000) {
1168 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1169 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1170 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1171 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1172 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1175 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1176 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1177 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1178 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1179 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1180 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1186 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1190 FLAC__ASSERT(bb != 0);
1191 FLAC__ASSERT(bb->buffer != 0);
1193 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1196 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1198 else if(val < 0x800) {
1199 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1200 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1202 else if(val < 0x10000) {
1203 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1204 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1205 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1207 else if(val < 0x200000) {
1208 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1209 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1210 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1211 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1213 else if(val < 0x4000000) {
1214 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1215 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1216 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1217 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1218 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1220 else if(val < 0x80000000) {
1221 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1222 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1223 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1224 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1225 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1226 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1229 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1230 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1231 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1232 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1233 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1234 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1235 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1241 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1243 /* 0-pad to byte boundary */
1245 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1250 FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1252 /* to avoid a drastic speed penalty we don't:
1253 FLAC__ASSERT(bb != 0);
1254 FLAC__ASSERT(bb->buffer != 0);
1255 FLAC__ASSERT(bb->bits == 0);
1259 if(bb->total_consumed_bits < bb->total_bits) {
1260 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1264 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1270 FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1272 /* to avoid a drastic speed penalty we don't:
1273 FLAC__ASSERT(bb != 0);
1274 FLAC__ASSERT(bb->buffer != 0);
1275 FLAC__ASSERT(bb->bits == 0);
1279 if(bb->total_consumed_bits < bb->total_bits) {
1280 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1281 bb->consumed_bits++;
1282 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1283 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1284 bb->consumed_blurbs++;
1285 bb->consumed_bits = 0;
1287 bb->total_consumed_bits++;
1291 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1297 FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1299 /* to avoid a drastic speed penalty we don't:
1300 FLAC__ASSERT(bb != 0);
1301 FLAC__ASSERT(bb->buffer != 0);
1302 FLAC__ASSERT(bb->bits == 0);
1306 if(bb->total_consumed_bits < bb->total_bits) {
1308 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1309 bb->consumed_bits++;
1310 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1311 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1312 bb->consumed_blurbs++;
1313 bb->consumed_bits = 0;
1315 bb->total_consumed_bits++;
1319 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1325 FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1327 /* to avoid a drastic speed penalty we don't:
1328 FLAC__ASSERT(bb != 0);
1329 FLAC__ASSERT(bb->buffer != 0);
1330 FLAC__ASSERT(bb->bits == 0);
1334 if(bb->total_consumed_bits < bb->total_bits) {
1336 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1337 bb->consumed_bits++;
1338 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1339 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1340 bb->consumed_blurbs++;
1341 bb->consumed_bits = 0;
1343 bb->total_consumed_bits++;
1347 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1353 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1354 #ifdef FLAC__NO_MANUAL_INLINING
1358 FLAC__ASSERT(bb != 0);
1359 FLAC__ASSERT(bb->buffer != 0);
1361 FLAC__ASSERT(bits <= 32);
1364 for(i = 0; i < bits; i++) {
1365 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1372 unsigned i, bits_ = bits;
1375 FLAC__ASSERT(bb != 0);
1376 FLAC__ASSERT(bb->buffer != 0);
1378 FLAC__ASSERT(bits <= 32);
1379 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1386 while(bb->total_consumed_bits + bits > bb->total_bits) {
1387 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1390 #if FLAC__BITS_PER_BLURB > 8
1391 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1393 if(bb->consumed_bits) {
1394 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1396 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1398 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1399 bb->consumed_blurbs++;
1400 bb->consumed_bits = 0;
1401 /* we hold off updating bb->total_consumed_bits until the end */
1404 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1405 bb->consumed_bits += bits_;
1406 bb->total_consumed_bits += bits_;
1410 #if FLAC__BITS_PER_BLURB == 32
1411 /* note that we know bits_ cannot be > 32 because of previous assertions */
1412 if(bits_ == FLAC__BITS_PER_BLURB) {
1413 v = bb->buffer[bb->consumed_blurbs];
1414 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1415 bb->consumed_blurbs++;
1416 /* bb->consumed_bits is already 0 */
1417 bb->total_consumed_bits += bits;
1422 while(bits_ >= FLAC__BITS_PER_BLURB) {
1423 v <<= FLAC__BITS_PER_BLURB;
1424 v |= bb->buffer[bb->consumed_blurbs];
1425 bits_ -= FLAC__BITS_PER_BLURB;
1426 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1427 bb->consumed_blurbs++;
1428 /* bb->consumed_bits is already 0 */
1429 /* we hold off updating bb->total_consumed_bits until the end */
1434 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1435 bb->consumed_bits = bits_;
1436 /* we hold off updating bb->total_consumed_bits until the end */
1438 bb->total_consumed_bits += bits;
1440 #if FLAC__BITS_PER_BLURB > 8
1444 for(i = 0; i < bits; i++) {
1445 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1454 FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1455 #ifdef FLAC__NO_MANUAL_INLINING
1460 FLAC__ASSERT(bb != 0);
1461 FLAC__ASSERT(bb->buffer != 0);
1463 FLAC__ASSERT(bits <= 32);
1471 for(i = 0; i < bits; i++) {
1472 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1480 *val = (FLAC__int32)v;
1484 *val = (FLAC__int32)v;
1490 unsigned i, bits_ = bits;
1493 FLAC__ASSERT(bb != 0);
1494 FLAC__ASSERT(bb->buffer != 0);
1496 FLAC__ASSERT(bits <= 32);
1497 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1504 while(bb->total_consumed_bits + bits > bb->total_bits) {
1505 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1508 #if FLAC__BITS_PER_BLURB > 8
1509 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1511 if(bb->consumed_bits) {
1512 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1514 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1516 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1517 bb->consumed_blurbs++;
1518 bb->consumed_bits = 0;
1519 /* we hold off updating bb->total_consumed_bits until the end */
1522 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1523 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1525 *val = (FLAC__int32)v;
1526 *val >>= (32-bits_);
1527 bb->consumed_bits += bits_;
1528 bb->total_consumed_bits += bits_;
1532 #if FLAC__BITS_PER_BLURB == 32
1533 /* note that we know bits_ cannot be > 32 because of previous assertions */
1534 if(bits_ == FLAC__BITS_PER_BLURB) {
1535 v = bb->buffer[bb->consumed_blurbs];
1537 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1538 bb->consumed_blurbs++;
1539 /* bb->consumed_bits is already 0 */
1540 /* we hold off updating bb->total_consumed_bits until the end */
1543 while(bits_ >= FLAC__BITS_PER_BLURB) {
1544 v <<= FLAC__BITS_PER_BLURB;
1545 v |= bb->buffer[bb->consumed_blurbs];
1546 bits_ -= FLAC__BITS_PER_BLURB;
1547 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1548 bb->consumed_blurbs++;
1549 /* bb->consumed_bits is already 0 */
1550 /* we hold off updating bb->total_consumed_bits until the end */
1555 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1556 bb->consumed_bits = bits_;
1557 /* we hold off updating bb->total_consumed_bits until the end */
1559 bb->total_consumed_bits += bits;
1560 #if FLAC__BITS_PER_BLURB > 8
1563 for(i = 0; i < bits; i++) {
1564 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1574 *val = (FLAC__int32)v;
1578 *val = (FLAC__int32)v;
1584 FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1585 #ifdef FLAC__NO_MANUAL_INLINING
1589 FLAC__ASSERT(bb != 0);
1590 FLAC__ASSERT(bb->buffer != 0);
1592 FLAC__ASSERT(bits <= 64);
1595 for(i = 0; i < bits; i++) {
1596 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1603 unsigned i, bits_ = bits;
1606 FLAC__ASSERT(bb != 0);
1607 FLAC__ASSERT(bb->buffer != 0);
1609 FLAC__ASSERT(bits <= 64);
1610 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1617 while(bb->total_consumed_bits + bits > bb->total_bits) {
1618 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1621 #if FLAC__BITS_PER_BLURB > 8
1622 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1624 if(bb->consumed_bits) {
1625 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1627 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1629 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1630 bb->consumed_blurbs++;
1631 bb->consumed_bits = 0;
1632 /* we hold off updating bb->total_consumed_bits until the end */
1635 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1636 bb->consumed_bits += bits_;
1637 bb->total_consumed_bits += bits_;
1641 while(bits_ >= FLAC__BITS_PER_BLURB) {
1642 v <<= FLAC__BITS_PER_BLURB;
1643 v |= bb->buffer[bb->consumed_blurbs];
1644 bits_ -= FLAC__BITS_PER_BLURB;
1645 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1646 bb->consumed_blurbs++;
1647 /* bb->consumed_bits is already 0 */
1648 /* we hold off updating bb->total_consumed_bits until the end */
1652 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1653 bb->consumed_bits = bits_;
1654 /* we hold off updating bb->total_consumed_bits until the end */
1656 bb->total_consumed_bits += bits;
1658 #if FLAC__BITS_PER_BLURB > 8
1662 for(i = 0; i < bits; i++) {
1663 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1672 FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1673 #ifdef FLAC__NO_MANUAL_INLINING
1678 FLAC__ASSERT(bb != 0);
1679 FLAC__ASSERT(bb->buffer != 0);
1681 FLAC__ASSERT(bits <= 64);
1684 for(i = 0; i < bits; i++) {
1685 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1692 *val = (FLAC__int64)v;
1696 *val = (FLAC__int64)v;
1702 unsigned i, bits_ = bits;
1705 FLAC__ASSERT(bb != 0);
1706 FLAC__ASSERT(bb->buffer != 0);
1708 FLAC__ASSERT(bits <= 64);
1709 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1716 while(bb->total_consumed_bits + bits > bb->total_bits) {
1717 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1720 #if FLAC__BITS_PER_BLURB > 8
1721 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1723 if(bb->consumed_bits) {
1724 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1726 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1728 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1729 bb->consumed_blurbs++;
1730 bb->consumed_bits = 0;
1731 /* we hold off updating bb->total_consumed_bits until the end */
1734 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1735 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1737 *val = (FLAC__int64)v;
1738 *val >>= (64-bits_);
1739 bb->consumed_bits += bits_;
1740 bb->total_consumed_bits += bits_;
1744 while(bits_ >= FLAC__BITS_PER_BLURB) {
1745 v <<= FLAC__BITS_PER_BLURB;
1746 v |= bb->buffer[bb->consumed_blurbs];
1747 bits_ -= FLAC__BITS_PER_BLURB;
1748 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1749 bb->consumed_blurbs++;
1750 /* bb->consumed_bits is already 0 */
1751 /* we hold off updating bb->total_consumed_bits until the end */
1755 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1756 bb->consumed_bits = bits_;
1757 /* we hold off updating bb->total_consumed_bits until the end */
1759 bb->total_consumed_bits += bits;
1760 #if FLAC__BITS_PER_BLURB > 8
1763 for(i = 0; i < bits; i++) {
1764 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1774 *val = (FLAC__int64)v;
1778 *val = (FLAC__int64)v;
1784 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1785 #ifdef FLAC__NO_MANUAL_INLINING
1787 unsigned bit, val_ = 0;
1789 FLAC__ASSERT(bb != 0);
1790 FLAC__ASSERT(bb->buffer != 0);
1793 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1805 unsigned i, val_ = 0;
1806 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1809 FLAC__ASSERT(bb != 0);
1810 FLAC__ASSERT(bb->buffer != 0);
1812 #if FLAC__BITS_PER_BLURB > 8
1813 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1815 if(bb->consumed_bits) {
1816 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1818 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1822 bb->consumed_bits += i;
1823 bb->total_consumed_bits += i;
1824 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1825 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1826 bb->consumed_blurbs++;
1827 bb->consumed_bits = 0;
1832 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1833 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1834 bb->consumed_blurbs++;
1835 bb->consumed_bits = 0;
1836 bb->total_consumed_bits += val_;
1840 if(bb->consumed_blurbs >= total_blurbs_) {
1841 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1843 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1845 b = bb->buffer[bb->consumed_blurbs];
1847 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1851 bb->consumed_bits = i;
1853 if(i == FLAC__BITS_PER_BLURB) {
1854 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1855 bb->consumed_blurbs++;
1856 bb->consumed_bits = 0;
1858 bb->total_consumed_bits += i;
1862 val_ += FLAC__BITS_PER_BLURB;
1863 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1864 bb->consumed_blurbs++;
1865 /* bb->consumed_bits is already 0 */
1866 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
1869 #if FLAC__BITS_PER_BLURB > 8
1873 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
1887 #ifdef FLAC__SYMMETRIC_RICE
1888 FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1890 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1892 FLAC__ASSERT(bb != 0);
1893 FLAC__ASSERT(bb->buffer != 0);
1894 FLAC__ASSERT(parameter <= 31);
1896 /* read the unary MSBs and end bit */
1897 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1900 /* read the sign bit */
1901 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1904 /* read the binary LSBs */
1905 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1908 /* compose the value */
1909 *val = (msbs << parameter) | lsbs;
1915 #endif /* ifdef FLAC__SYMMETRIC_RICE */
1917 FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1919 FLAC__uint32 lsbs = 0, msbs = 0;
1922 FLAC__ASSERT(bb != 0);
1923 FLAC__ASSERT(bb->buffer != 0);
1924 FLAC__ASSERT(parameter <= 31);
1926 /* read the unary MSBs and end bit */
1927 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1930 /* read the binary LSBs */
1931 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1934 /* compose the value */
1935 uval = (msbs << parameter) | lsbs;
1937 *val = -((int)(uval >> 1)) - 1;
1939 *val = (int)(uval >> 1);
1944 FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1946 const FLAC__blurb *buffer = bb->buffer;
1948 unsigned i, j, uval, val_i = 0;
1949 unsigned msbs = 0, lsbs_left;
1950 FLAC__blurb blurb, save_blurb, cbits;
1951 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
1953 FLAC__ASSERT(bb != 0);
1954 FLAC__ASSERT(bb->buffer != 0);
1955 FLAC__ASSERT(parameter <= 31);
1960 i = bb->consumed_blurbs;
1962 * We unroll the main loop to take care of partially consumed blurbs here.
1964 if(bb->consumed_bits > 0) {
1965 save_blurb = blurb = buffer[i];
1966 cbits = bb->consumed_bits;
1972 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
1976 /* dispose of the unary end bit */
1982 lsbs_left = parameter;
1984 if(cbits == FLAC__BITS_PER_BLURB) {
1986 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
1991 msbs += FLAC__BITS_PER_BLURB - cbits;
1993 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
1998 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
1999 if(lsbs_left >= available_bits) {
2000 uval <<= available_bits;
2001 uval |= (blurb >> cbits);
2003 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2005 if(lsbs_left == available_bits) {
2006 /* compose the value */
2007 uval |= (msbs << parameter);
2009 vals[val_i++] = -((int)(uval >> 1)) - 1;
2011 vals[val_i++] = (int)(uval >> 1);
2019 lsbs_left -= available_bits;
2024 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2025 blurb <<= lsbs_left;
2028 /* compose the value */
2029 uval |= (msbs << parameter);
2031 vals[val_i++] = -((int)(uval >> 1)) - 1;
2033 vals[val_i++] = (int)(uval >> 1);
2034 if(val_i == nvals) {
2035 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2047 bb->consumed_blurbs = i;
2048 bb->consumed_bits = cbits;
2049 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2053 * Now that we are blurb-aligned the logic is slightly simpler
2055 while(val_i < nvals) {
2056 for( ; i < bb->blurbs && val_i < nvals; i++) {
2057 save_blurb = blurb = buffer[i];
2062 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2066 /* dispose of the unary end bit */
2072 lsbs_left = parameter;
2074 if(cbits == FLAC__BITS_PER_BLURB) {
2076 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2081 msbs += FLAC__BITS_PER_BLURB - cbits;
2083 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2088 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2089 if(lsbs_left >= available_bits) {
2090 uval <<= available_bits;
2091 uval |= (blurb >> cbits);
2093 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2095 if(lsbs_left == available_bits) {
2096 /* compose the value */
2097 uval |= (msbs << parameter);
2099 vals[val_i++] = -((int)(uval >> 1)) - 1;
2101 vals[val_i++] = (int)(uval >> 1);
2109 lsbs_left -= available_bits;
2114 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2115 blurb <<= lsbs_left;
2118 /* compose the value */
2119 uval |= (msbs << parameter);
2121 vals[val_i++] = -((int)(uval >> 1)) - 1;
2123 vals[val_i++] = (int)(uval >> 1);
2124 if(val_i == nvals) {
2125 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2136 bb->consumed_blurbs = i;
2137 bb->consumed_bits = cbits;
2138 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2140 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2142 /* these must be zero because we can only get here if we got to the end of the buffer */
2143 FLAC__ASSERT(bb->consumed_blurbs == 0);
2144 FLAC__ASSERT(bb->consumed_bits == 0);
2153 FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2155 FLAC__uint32 lsbs = 0, msbs = 0;
2156 unsigned bit, uval, k;
2158 FLAC__ASSERT(bb != 0);
2159 FLAC__ASSERT(bb->buffer != 0);
2161 k = FLAC__bitmath_ilog2(parameter);
2163 /* read the unary MSBs and end bit */
2164 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2167 /* read the binary LSBs */
2168 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2171 if(parameter == 1u<<k) {
2172 /* compose the value */
2173 uval = (msbs << k) | lsbs;
2176 unsigned d = (1 << (k+1)) - parameter;
2178 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2184 /* compose the value */
2185 uval = msbs * parameter + lsbs;
2188 /* unfold unsigned to signed */
2190 *val = -((int)(uval >> 1)) - 1;
2192 *val = (int)(uval >> 1);
2197 FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2199 FLAC__uint32 lsbs, msbs = 0;
2202 FLAC__ASSERT(bb != 0);
2203 FLAC__ASSERT(bb->buffer != 0);
2205 k = FLAC__bitmath_ilog2(parameter);
2207 /* read the unary MSBs and end bit */
2208 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2211 /* read the binary LSBs */
2212 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2215 if(parameter == 1u<<k) {
2216 /* compose the value */
2217 *val = (msbs << k) | lsbs;
2220 unsigned d = (1 << (k+1)) - parameter;
2222 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2228 /* compose the value */
2229 *val = msbs * parameter + lsbs;
2236 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2237 FLAC__bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen)
2243 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2246 raw[(*rawlen)++] = (FLAC__byte)x;
2247 if(!(x & 0x80)) { /* 0xxxxxxx */
2251 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2255 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2259 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2263 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2267 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2276 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2279 raw[(*rawlen)++] = (FLAC__byte)x;
2280 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2291 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2292 FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen)
2298 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2301 raw[(*rawlen)++] = (FLAC__byte)x;
2302 if(!(x & 0x80)) { /* 0xxxxxxx */
2306 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2310 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2314 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2318 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2322 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2326 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2331 *val = 0xffffffffffffffff;
2335 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2338 raw[(*rawlen)++] = (FLAC__byte)x;
2339 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2340 *val = 0xffffffffffffffff;
2350 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2354 fprintf(out, "bitbuffer is NULL\n");
2357 fprintf(out, "bitbuffer: capacity=%u blurbs=%u bits=%u total_bits=%u consumed: blurbs=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->blurbs, bb->bits, bb->total_bits, bb->consumed_blurbs, bb->consumed_bits, bb->total_consumed_bits);
2359 for(i = 0; i < bb->blurbs; i++) {
2360 fprintf(out, "%08X: ", i);
2361 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2362 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2365 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2369 fprintf(out, "%08X: ", i);
2370 for(j = 0; j < bb->bits; j++)
2371 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2374 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);