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 #if FLAC__BITS_PER_BLURB == 32
102 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
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);
127 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
128 * does not shift left what is consumed, it just chops off the end, whether
129 * there is unconsumed data there or not. This is OK because currently we
130 * never shrink the buffer, but if this ever changes, we'll have to do some
133 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
135 FLAC__blurb *new_buffer;
137 FLAC__ASSERT(bb != 0);
138 FLAC__ASSERT(bb->buffer != 0);
140 if(bb->capacity == new_capacity)
143 new_buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * new_capacity);
146 memset(new_buffer, 0, sizeof(FLAC__blurb) * new_capacity);
147 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
148 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
149 bb->blurbs = new_capacity;
151 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
153 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
154 bb->consumed_blurbs = new_capacity;
155 bb->consumed_bits = 0;
156 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
158 free(bb->buffer); // we've already asserted above that (bb->buffer != 0)
159 bb->buffer = new_buffer;
160 bb->capacity = new_capacity;
164 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
166 unsigned new_capacity;
168 FLAC__ASSERT(min_blurbs_to_add > 0);
170 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
171 return bitbuffer_resize_(bb, new_capacity);
174 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
176 FLAC__ASSERT(bb != 0);
177 FLAC__ASSERT(bb->buffer != 0);
179 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
180 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
185 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)
190 /* first shift the unconsumed buffer data toward the front as much as possible */
191 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
192 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
193 for( ; r < r_end; l++, r++)
194 bb->buffer[l] = bb->buffer[r];
195 for( ; l < r_end; l++)
197 bb->blurbs -= bb->consumed_blurbs;
198 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
199 bb->consumed_blurbs = 0;
200 bb->total_consumed_bits = bb->consumed_bits;
203 /* grow if we need to */
204 if(bb->capacity <= 1) {
205 if(!bitbuffer_resize_(bb, 16))
209 /* set the target for reading, taking into account blurb alignment */
210 #if FLAC__BITS_PER_BLURB == 8
211 /* blurb == byte, so no gyrations necessary: */
212 target = bb->buffer + bb->blurbs;
213 bytes = bb->capacity - bb->blurbs;
214 #elif FLAC__BITS_PER_BLURB == 32
215 /* @@@ WATCHOUT: code currently only works for big-endian: */
216 FLAC__ASSERT((bb->bits & 7) == 0);
217 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
218 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
220 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
223 /* finally, read in some data */
224 if(!read_callback(target, &bytes, client_data))
227 /* now we have to handle partial blurb cases: */
228 #if FLAC__BITS_PER_BLURB == 8
229 /* blurb == byte, so no gyrations necessary: */
231 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
232 #elif FLAC__BITS_PER_BLURB == 32
233 /* @@@ WATCHOUT: code currently only works for big-endian: */
235 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
236 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
237 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
238 bb->total_bits += (bytes << 3);
241 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
246 /***********************************************************************
248 * Class constructor/destructor
250 ***********************************************************************/
252 FLAC__BitBuffer *FLAC__bitbuffer_new()
254 return (FLAC__BitBuffer*)malloc(sizeof(FLAC__BitBuffer));
257 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
259 FLAC__ASSERT(bb != 0);
261 FLAC__bitbuffer_free(bb);
265 /***********************************************************************
267 * Public class methods
269 ***********************************************************************/
271 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
273 FLAC__ASSERT(bb != 0);
277 bb->blurbs = bb->bits = bb->total_bits = 0;
278 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
280 return FLAC__bitbuffer_clear(bb);
283 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
285 FLAC__ASSERT(bb != 0);
286 FLAC__ASSERT(bytes > 0);
288 if(!FLAC__bitbuffer_init(bb))
291 if(!bitbuffer_ensure_size_(bb, bytes << 3))
294 FLAC__ASSERT(buffer != 0);
295 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
296 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
297 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
298 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
299 bb->total_bits = bytes << 3;
303 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
305 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
307 FLAC__ASSERT(dest != 0);
308 FLAC__ASSERT(src != 0);
312 if(dest->bits != src->consumed_bits)
314 if(!bitbuffer_ensure_size_(dest, bits_to_add))
316 if(dest->bits == 0) {
317 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
319 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
320 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
321 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
322 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)));
325 dest->buffer[dest->blurbs] <<= bits_to_add;
326 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
328 dest->bits = src->bits;
329 dest->total_bits += bits_to_add;
330 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
335 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
337 FLAC__ASSERT(bb != 0);
343 bb->blurbs = bb->bits = bb->total_bits = 0;
344 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
347 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
349 if(bb->buffer == 0) {
350 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
351 bb->buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * bb->capacity);
354 memset(bb->buffer, 0, bb->capacity);
357 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
359 bb->blurbs = bb->bits = bb->total_bits = 0;
360 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
364 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
366 FLAC__ASSERT(dest != 0);
367 FLAC__ASSERT(dest->buffer != 0);
368 FLAC__ASSERT(src != 0);
369 FLAC__ASSERT(src->buffer != 0);
371 if(dest->capacity < src->capacity)
372 if(!bitbuffer_resize_(dest, src->capacity))
374 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
375 dest->blurbs = src->blurbs;
376 dest->bits = src->bits;
377 dest->total_bits = src->total_bits;
378 dest->consumed_blurbs = src->consumed_blurbs;
379 dest->consumed_bits = src->consumed_bits;
380 dest->total_consumed_bits = src->total_consumed_bits;
381 dest->read_crc16 = src->read_crc16;
385 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
387 FLAC__ASSERT(bb != 0);
388 FLAC__ASSERT(bb->buffer != 0);
389 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
391 bb->read_crc16 = seed;
392 #if FLAC__BITS_PER_BLURB == 8
393 /* no need to do anything */
394 #elif FLAC__BITS_PER_BLURB == 32
395 bb->crc16_align = bb->consumed_bits;
397 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
401 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
403 FLAC__ASSERT(bb != 0);
404 FLAC__ASSERT(bb->buffer != 0);
405 FLAC__ASSERT((bb->bits & 7) == 0);
406 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
408 #if FLAC__BITS_PER_BLURB == 8
409 /* no need to do anything */
410 #elif FLAC__BITS_PER_BLURB == 32
411 /*@@@ 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 */
412 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
413 if(bb->consumed_bits == 8) {
414 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
415 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
417 else if(bb->consumed_bits == 16) {
418 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
419 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
420 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
422 else if(bb->consumed_bits == 24) {
423 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
424 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
425 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
426 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
430 if(bb->consumed_bits == 8) {
431 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
432 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
434 else if(bb->consumed_bits == 16) {
435 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
436 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
437 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
439 else if(bb->consumed_bits == 24) {
440 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
441 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
442 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
443 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
446 bb->crc16_align = bb->consumed_bits;
448 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
450 return bb->read_crc16;
453 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
455 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
457 #if FLAC__BITS_PER_BLURB == 8
458 return FLAC__crc16(bb->buffer, bb->blurbs);
459 #elif FLAC__BITS_PER_BLURB == 32
460 /* @@@ WATCHOUT: code currently only works for big-endian: */
461 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
463 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
467 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
469 FLAC__ASSERT(0 != bb);
470 //@@@ WHY WAS THIS HERE? FLAC__ASSERT(bb->blurbs == 0);
471 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
472 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
473 #if FLAC__BITS_PER_BLURB == 8
474 return FLAC__crc8(bb->buffer, bb->blurbs);
475 #elif FLAC__BITS_PER_BLURB == 32
476 /* @@@ WATCHOUT: code currently only works for big-endian: */
477 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
479 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
483 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
485 return ((bb->bits & 7) == 0);
488 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
490 return ((bb->consumed_bits & 7) == 0);
493 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
495 return 8 - (bb->consumed_bits & 7);
498 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
500 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
501 return (bb->total_bits - bb->total_consumed_bits) >> 3;
504 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
506 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
507 #if FLAC__BITS_PER_BLURB == 8
508 *buffer = bb->buffer + bb->consumed_blurbs;
509 *bytes = bb->blurbs - bb->consumed_blurbs;
510 #elif FLAC__BITS_PER_BLURB == 32
511 /* @@@ WATCHOUT: code currently only works for big-endian: */
512 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
513 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
515 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
519 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
521 #if FLAC__BITS_PER_BLURB == 8
523 #elif FLAC__BITS_PER_BLURB == 32
524 /* @@@ WATCHOUT: code currently only works for big-endian: */
527 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
531 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
535 FLAC__ASSERT(bb != 0);
536 FLAC__ASSERT(bb->buffer != 0);
540 if(!bitbuffer_ensure_size_(bb, bits))
542 bb->total_bits += bits;
544 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
545 bb->buffer[bb->blurbs] <<= n;
548 if(bb->bits == FLAC__BITS_PER_BLURB) {
556 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
560 FLAC__ASSERT(bb != 0);
561 FLAC__ASSERT(bb->buffer != 0);
563 FLAC__ASSERT(bits <= 32);
566 /* inline the size check so we don't incure a function call unnecessarily */
567 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
568 if(!bitbuffer_ensure_size_(bb, bits))
572 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
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 */
576 bb->total_bits += bits;
578 n = FLAC__BITS_PER_BLURB - bb->bits;
579 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
580 if(bits < FLAC__BITS_PER_BLURB) {
581 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
585 else if(bits == FLAC__BITS_PER_BLURB) {
586 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
590 k = bits - FLAC__BITS_PER_BLURB;
591 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
592 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
593 val &= (~(0xffffffff << k));
594 bits -= FLAC__BITS_PER_BLURB;
598 bb->buffer[bb->blurbs] <<= bits;
599 bb->buffer[bb->blurbs] |= val;
610 bb->buffer[bb->blurbs] <<= n;
611 bb->buffer[bb->blurbs] |= (val >> k);
612 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
613 val &= (~(0xffffffff << k));
623 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
625 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
628 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
630 static const FLAC__uint64 mask[] = {
632 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
633 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
634 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
635 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
636 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
637 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
638 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
639 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
640 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
641 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
642 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
643 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
644 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
645 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
646 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
647 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
651 FLAC__ASSERT(bb != 0);
652 FLAC__ASSERT(bb->buffer != 0);
654 FLAC__ASSERT(bits <= 64);
657 if(!bitbuffer_ensure_size_(bb, bits))
660 bb->total_bits += bits;
663 if(bits < FLAC__BITS_PER_BLURB) {
664 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
668 else if(bits == FLAC__BITS_PER_BLURB) {
669 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
673 k = bits - FLAC__BITS_PER_BLURB;
674 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
675 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
676 val &= (~(0xffffffffffffffff << k));
677 bits -= FLAC__BITS_PER_BLURB;
681 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
683 bb->buffer[bb->blurbs] <<= n;
684 bb->buffer[bb->blurbs] |= (val >> k);
685 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
686 val &= (~(0xffffffffffffffff << k));
689 if(bb->bits == FLAC__BITS_PER_BLURB) {
699 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
701 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
704 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
706 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
708 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
709 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
711 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
713 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
715 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
721 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
725 /* this could be faster but currently we don't need it to be */
726 for(i = 0; i < nvals; i++) {
727 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
734 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
737 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
739 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
741 if(!FLAC__bitbuffer_write_zeroes(bb, val))
743 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
747 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
751 /* fold signed to unsigned */
754 * (unsigned)(((--val) << 1) - 1);
755 * but without the overflow problem at MININT
757 uval = (unsigned)(((-(++val)) << 1) + 1);
759 uval = (unsigned)(val << 1);
761 msbs = uval >> parameter;
763 return 1 + parameter + msbs;
767 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
769 unsigned bits, msbs, uval;
772 FLAC__ASSERT(parameter > 0);
774 /* fold signed to unsigned */
777 * (unsigned)(((--val) << 1) - 1);
778 * but without the overflow problem at MININT
780 uval = (unsigned)(((-(++val)) << 1) + 1);
782 uval = (unsigned)(val << 1);
784 k = FLAC__bitmath_ilog2(parameter);
785 if(parameter == 1u<<k) {
786 FLAC__ASSERT(k <= 30);
794 d = (1 << (k+1)) - parameter;
795 q = uval / parameter;
796 r = uval - (q * parameter);
805 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
810 FLAC__ASSERT(parameter > 0);
812 k = FLAC__bitmath_ilog2(parameter);
813 if(parameter == 1u<<k) {
814 FLAC__ASSERT(k <= 30);
822 d = (1 << (k+1)) - parameter;
823 q = uval / parameter;
824 r = uval - (q * parameter);
834 #ifdef FLAC__SYMMETRIC_RICE
835 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
837 unsigned total_bits, interesting_bits, msbs;
838 FLAC__uint32 pattern;
840 FLAC__ASSERT(bb != 0);
841 FLAC__ASSERT(bb->buffer != 0);
842 FLAC__ASSERT(parameter <= 31);
844 /* init pattern with the unary end bit and the sign bit */
852 msbs = val >> parameter;
853 interesting_bits = 2 + parameter;
854 total_bits = interesting_bits + msbs;
855 pattern <<= parameter;
856 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
858 if(total_bits <= 32) {
859 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
863 /* write the unary MSBs */
864 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
866 /* write the unary end bit, the sign bit, and binary LSBs */
867 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
874 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
876 unsigned total_bits, interesting_bits, msbs;
877 FLAC__uint32 pattern;
879 FLAC__ASSERT(bb != 0);
880 FLAC__ASSERT(bb->buffer != 0);
881 FLAC__ASSERT(parameter <= 31);
885 /* init pattern with the unary end bit and the sign bit */
893 msbs = val >> parameter;
894 interesting_bits = 2 + parameter;
895 total_bits = interesting_bits + msbs;
896 pattern <<= parameter;
897 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
899 if(total_bits <= 32) {
900 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
903 else if(total_bits > max_bits) {
908 /* write the unary MSBs */
909 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
911 /* write the unary end bit, the sign bit, and binary LSBs */
912 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
919 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
921 unsigned total_bits, val_bits;
922 FLAC__uint32 pattern;
924 FLAC__ASSERT(bb != 0);
925 FLAC__ASSERT(bb->buffer != 0);
926 FLAC__ASSERT(parameter <= 31);
928 val_bits = FLAC__bitmath_silog2(val);
929 total_bits = 2 + parameter + 5 + val_bits;
931 if(total_bits <= 32) {
933 pattern <<= (parameter + 5);
935 pattern <<= val_bits;
936 pattern |= (val & ((1 << val_bits) - 1));
937 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
941 /* write the '-0' escape code first */
942 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
944 /* write the length */
945 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
947 /* write the value */
948 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
953 #endif /* ifdef FLAC__SYMMETRIC_RICE */
955 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
957 unsigned total_bits, interesting_bits, msbs, uval;
958 FLAC__uint32 pattern;
960 FLAC__ASSERT(bb != 0);
961 FLAC__ASSERT(bb->buffer != 0);
962 FLAC__ASSERT(parameter <= 30);
964 /* fold signed to unsigned */
967 * (unsigned)(((--val) << 1) - 1);
968 * but without the overflow problem at MININT
970 uval = (unsigned)(((-(++val)) << 1) + 1);
972 uval = (unsigned)(val << 1);
974 msbs = uval >> parameter;
975 interesting_bits = 1 + parameter;
976 total_bits = interesting_bits + msbs;
977 pattern = 1 << parameter; /* the unary end bit */
978 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
980 if(total_bits <= 32) {
981 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
985 /* write the unary MSBs */
986 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
988 /* write the unary end bit and binary LSBs */
989 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
996 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
998 unsigned total_bits, interesting_bits, msbs, uval;
999 FLAC__uint32 pattern;
1001 FLAC__ASSERT(bb != 0);
1002 FLAC__ASSERT(bb->buffer != 0);
1003 FLAC__ASSERT(parameter <= 30);
1007 /* fold signed to unsigned */
1010 * (unsigned)(((--val) << 1) - 1);
1011 * but without the overflow problem at MININT
1013 uval = (unsigned)(((-(++val)) << 1) + 1);
1015 uval = (unsigned)(val << 1);
1017 msbs = uval >> parameter;
1018 interesting_bits = 1 + parameter;
1019 total_bits = interesting_bits + msbs;
1020 pattern = 1 << parameter; /* the unary end bit */
1021 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1023 if(total_bits <= 32) {
1024 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1027 else if(total_bits > max_bits) {
1032 /* write the unary MSBs */
1033 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1035 /* write the unary end bit and binary LSBs */
1036 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1044 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1046 unsigned total_bits, msbs, uval;
1049 FLAC__ASSERT(bb != 0);
1050 FLAC__ASSERT(bb->buffer != 0);
1051 FLAC__ASSERT(parameter > 0);
1053 /* fold signed to unsigned */
1056 * (unsigned)(((--val) << 1) - 1);
1057 * but without the overflow problem at MININT
1059 uval = (unsigned)(((-(++val)) << 1) + 1);
1061 uval = (unsigned)(val << 1);
1063 k = FLAC__bitmath_ilog2(parameter);
1064 if(parameter == 1u<<k) {
1067 FLAC__ASSERT(k <= 30);
1070 total_bits = 1 + k + msbs;
1071 pattern = 1 << k; /* the unary end bit */
1072 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1074 if(total_bits <= 32) {
1075 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1079 /* write the unary MSBs */
1080 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1082 /* write the unary end bit and binary LSBs */
1083 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1090 d = (1 << (k+1)) - parameter;
1091 q = uval / parameter;
1092 r = uval - (q * parameter);
1093 /* write the unary MSBs */
1094 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1096 /* write the unary end bit */
1097 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1099 /* write the binary LSBs */
1101 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1105 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1112 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1114 unsigned total_bits, msbs;
1117 FLAC__ASSERT(bb != 0);
1118 FLAC__ASSERT(bb->buffer != 0);
1119 FLAC__ASSERT(parameter > 0);
1121 k = FLAC__bitmath_ilog2(parameter);
1122 if(parameter == 1u<<k) {
1125 FLAC__ASSERT(k <= 30);
1128 total_bits = 1 + k + msbs;
1129 pattern = 1 << k; /* the unary end bit */
1130 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1132 if(total_bits <= 32) {
1133 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1137 /* write the unary MSBs */
1138 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1140 /* write the unary end bit and binary LSBs */
1141 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1148 d = (1 << (k+1)) - parameter;
1149 q = uval / parameter;
1150 r = uval - (q * parameter);
1151 /* write the unary MSBs */
1152 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1154 /* write the unary end bit */
1155 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1157 /* write the binary LSBs */
1159 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1163 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1171 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1175 FLAC__ASSERT(bb != 0);
1176 FLAC__ASSERT(bb->buffer != 0);
1178 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1181 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1183 else if(val < 0x800) {
1184 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1185 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1187 else if(val < 0x10000) {
1188 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1189 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1190 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1192 else if(val < 0x200000) {
1193 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1194 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1195 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1196 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1198 else if(val < 0x4000000) {
1199 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1200 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1201 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1202 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1203 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1206 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1207 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1208 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1209 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1210 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1211 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1217 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1221 FLAC__ASSERT(bb != 0);
1222 FLAC__ASSERT(bb->buffer != 0);
1224 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1227 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1229 else if(val < 0x800) {
1230 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1231 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1233 else if(val < 0x10000) {
1234 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1235 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1236 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1238 else if(val < 0x200000) {
1239 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1240 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1241 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1242 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1244 else if(val < 0x4000000) {
1245 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1246 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1247 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1248 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1249 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1251 else if(val < 0x80000000) {
1252 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1253 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1254 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1255 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1256 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1257 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1263 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1264 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1265 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1266 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1272 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1274 /* 0-pad to byte boundary */
1276 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1281 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)
1283 /* to avoid a drastic speed penalty we don't:
1284 FLAC__ASSERT(bb != 0);
1285 FLAC__ASSERT(bb->buffer != 0);
1286 FLAC__ASSERT(bb->bits == 0);
1290 if(bb->total_consumed_bits < bb->total_bits) {
1291 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1295 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1301 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)
1303 /* to avoid a drastic speed penalty we don't:
1304 FLAC__ASSERT(bb != 0);
1305 FLAC__ASSERT(bb->buffer != 0);
1306 FLAC__ASSERT(bb->bits == 0);
1310 if(bb->total_consumed_bits < bb->total_bits) {
1311 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1312 bb->consumed_bits++;
1313 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1314 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1315 bb->consumed_blurbs++;
1316 bb->consumed_bits = 0;
1318 bb->total_consumed_bits++;
1322 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1328 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)
1330 /* to avoid a drastic speed penalty we don't:
1331 FLAC__ASSERT(bb != 0);
1332 FLAC__ASSERT(bb->buffer != 0);
1333 FLAC__ASSERT(bb->bits == 0);
1337 if(bb->total_consumed_bits < bb->total_bits) {
1339 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1340 bb->consumed_bits++;
1341 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1342 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1343 bb->consumed_blurbs++;
1344 bb->consumed_bits = 0;
1346 bb->total_consumed_bits++;
1350 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1356 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)
1358 /* to avoid a drastic speed penalty we don't:
1359 FLAC__ASSERT(bb != 0);
1360 FLAC__ASSERT(bb->buffer != 0);
1361 FLAC__ASSERT(bb->bits == 0);
1365 if(bb->total_consumed_bits < bb->total_bits) {
1367 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1368 bb->consumed_bits++;
1369 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1370 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1371 bb->consumed_blurbs++;
1372 bb->consumed_bits = 0;
1374 bb->total_consumed_bits++;
1378 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1384 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)
1385 #ifdef FLAC__NO_MANUAL_INLINING
1389 FLAC__ASSERT(bb != 0);
1390 FLAC__ASSERT(bb->buffer != 0);
1392 FLAC__ASSERT(bits <= 32);
1395 for(i = 0; i < bits; i++) {
1396 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1403 unsigned i, bits_ = bits;
1406 FLAC__ASSERT(bb != 0);
1407 FLAC__ASSERT(bb->buffer != 0);
1409 FLAC__ASSERT(bits <= 32);
1410 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1417 while(bb->total_consumed_bits + bits > bb->total_bits) {
1418 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1421 #if FLAC__BITS_PER_BLURB > 8
1422 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1424 if(bb->consumed_bits) {
1425 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1427 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1429 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1430 bb->consumed_blurbs++;
1431 bb->consumed_bits = 0;
1432 /* we hold off updating bb->total_consumed_bits until the end */
1435 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1436 bb->consumed_bits += bits_;
1437 bb->total_consumed_bits += bits_;
1441 #if FLAC__BITS_PER_BLURB == 32
1442 /* note that we know bits_ cannot be > 32 because of previous assertions */
1443 if(bits_ == FLAC__BITS_PER_BLURB) {
1444 v = bb->buffer[bb->consumed_blurbs];
1445 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1446 bb->consumed_blurbs++;
1447 /* bb->consumed_bits is already 0 */
1448 bb->total_consumed_bits += bits;
1453 while(bits_ >= FLAC__BITS_PER_BLURB) {
1454 v <<= FLAC__BITS_PER_BLURB;
1455 v |= bb->buffer[bb->consumed_blurbs];
1456 bits_ -= FLAC__BITS_PER_BLURB;
1457 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1458 bb->consumed_blurbs++;
1459 /* bb->consumed_bits is already 0 */
1460 /* we hold off updating bb->total_consumed_bits until the end */
1465 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1466 bb->consumed_bits = bits_;
1467 /* we hold off updating bb->total_consumed_bits until the end */
1469 bb->total_consumed_bits += bits;
1471 #if FLAC__BITS_PER_BLURB > 8
1475 for(i = 0; i < bits; i++) {
1476 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1485 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)
1486 #ifdef FLAC__NO_MANUAL_INLINING
1491 FLAC__ASSERT(bb != 0);
1492 FLAC__ASSERT(bb->buffer != 0);
1494 FLAC__ASSERT(bits <= 32);
1502 for(i = 0; i < bits; i++) {
1503 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1511 *val = (FLAC__int32)v;
1515 *val = (FLAC__int32)v;
1521 unsigned i, bits_ = bits;
1524 FLAC__ASSERT(bb != 0);
1525 FLAC__ASSERT(bb->buffer != 0);
1527 FLAC__ASSERT(bits <= 32);
1528 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1535 while(bb->total_consumed_bits + bits > bb->total_bits) {
1536 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1539 #if FLAC__BITS_PER_BLURB > 8
1540 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1542 if(bb->consumed_bits) {
1543 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1545 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1547 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1548 bb->consumed_blurbs++;
1549 bb->consumed_bits = 0;
1550 /* we hold off updating bb->total_consumed_bits until the end */
1553 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1554 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1556 *val = (FLAC__int32)v;
1557 *val >>= (32-bits_);
1558 bb->consumed_bits += bits_;
1559 bb->total_consumed_bits += bits_;
1563 #if FLAC__BITS_PER_BLURB == 32
1564 /* note that we know bits_ cannot be > 32 because of previous assertions */
1565 if(bits_ == FLAC__BITS_PER_BLURB) {
1566 v = bb->buffer[bb->consumed_blurbs];
1568 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1569 bb->consumed_blurbs++;
1570 /* bb->consumed_bits is already 0 */
1571 /* we hold off updating bb->total_consumed_bits until the end */
1574 while(bits_ >= FLAC__BITS_PER_BLURB) {
1575 v <<= FLAC__BITS_PER_BLURB;
1576 v |= bb->buffer[bb->consumed_blurbs];
1577 bits_ -= FLAC__BITS_PER_BLURB;
1578 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1579 bb->consumed_blurbs++;
1580 /* bb->consumed_bits is already 0 */
1581 /* we hold off updating bb->total_consumed_bits until the end */
1586 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1587 bb->consumed_bits = bits_;
1588 /* we hold off updating bb->total_consumed_bits until the end */
1590 bb->total_consumed_bits += bits;
1591 #if FLAC__BITS_PER_BLURB > 8
1594 for(i = 0; i < bits; i++) {
1595 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1605 *val = (FLAC__int32)v;
1609 *val = (FLAC__int32)v;
1615 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)
1616 #ifdef FLAC__NO_MANUAL_INLINING
1620 FLAC__ASSERT(bb != 0);
1621 FLAC__ASSERT(bb->buffer != 0);
1623 FLAC__ASSERT(bits <= 64);
1626 for(i = 0; i < bits; i++) {
1627 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1634 unsigned i, bits_ = bits;
1637 FLAC__ASSERT(bb != 0);
1638 FLAC__ASSERT(bb->buffer != 0);
1640 FLAC__ASSERT(bits <= 64);
1641 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1648 while(bb->total_consumed_bits + bits > bb->total_bits) {
1649 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1652 #if FLAC__BITS_PER_BLURB > 8
1653 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1655 if(bb->consumed_bits) {
1656 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1658 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1660 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1661 bb->consumed_blurbs++;
1662 bb->consumed_bits = 0;
1663 /* we hold off updating bb->total_consumed_bits until the end */
1666 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1667 bb->consumed_bits += bits_;
1668 bb->total_consumed_bits += bits_;
1672 while(bits_ >= FLAC__BITS_PER_BLURB) {
1673 v <<= FLAC__BITS_PER_BLURB;
1674 v |= bb->buffer[bb->consumed_blurbs];
1675 bits_ -= FLAC__BITS_PER_BLURB;
1676 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1677 bb->consumed_blurbs++;
1678 /* bb->consumed_bits is already 0 */
1679 /* we hold off updating bb->total_consumed_bits until the end */
1683 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1684 bb->consumed_bits = bits_;
1685 /* we hold off updating bb->total_consumed_bits until the end */
1687 bb->total_consumed_bits += bits;
1689 #if FLAC__BITS_PER_BLURB > 8
1693 for(i = 0; i < bits; i++) {
1694 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1703 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)
1704 #ifdef FLAC__NO_MANUAL_INLINING
1709 FLAC__ASSERT(bb != 0);
1710 FLAC__ASSERT(bb->buffer != 0);
1712 FLAC__ASSERT(bits <= 64);
1715 for(i = 0; i < bits; i++) {
1716 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1723 *val = (FLAC__int64)v;
1727 *val = (FLAC__int64)v;
1733 unsigned i, bits_ = bits;
1736 FLAC__ASSERT(bb != 0);
1737 FLAC__ASSERT(bb->buffer != 0);
1739 FLAC__ASSERT(bits <= 64);
1740 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1747 while(bb->total_consumed_bits + bits > bb->total_bits) {
1748 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1751 #if FLAC__BITS_PER_BLURB > 8
1752 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1754 if(bb->consumed_bits) {
1755 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1757 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1759 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1760 bb->consumed_blurbs++;
1761 bb->consumed_bits = 0;
1762 /* we hold off updating bb->total_consumed_bits until the end */
1765 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1766 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1768 *val = (FLAC__int64)v;
1769 *val >>= (64-bits_);
1770 bb->consumed_bits += bits_;
1771 bb->total_consumed_bits += bits_;
1775 while(bits_ >= FLAC__BITS_PER_BLURB) {
1776 v <<= FLAC__BITS_PER_BLURB;
1777 v |= bb->buffer[bb->consumed_blurbs];
1778 bits_ -= FLAC__BITS_PER_BLURB;
1779 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1780 bb->consumed_blurbs++;
1781 /* bb->consumed_bits is already 0 */
1782 /* we hold off updating bb->total_consumed_bits until the end */
1786 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1787 bb->consumed_bits = bits_;
1788 /* we hold off updating bb->total_consumed_bits until the end */
1790 bb->total_consumed_bits += bits;
1791 #if FLAC__BITS_PER_BLURB > 8
1794 for(i = 0; i < bits; i++) {
1795 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1805 *val = (FLAC__int64)v;
1809 *val = (FLAC__int64)v;
1815 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1817 FLAC__uint32 x8, x32 = 0;
1819 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1821 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1824 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1828 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1832 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1840 FLAC__bool FLAC__bitbuffer_read_byte_block_aligned(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1842 FLAC__ASSERT(bb != 0);
1843 FLAC__ASSERT(bb->buffer != 0);
1844 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1845 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1846 #if FLAC__BITS_PER_BLURB == 8
1848 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1850 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1855 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1856 val += FLAC__BYTES_PER_BLURB * chunk;
1859 bb->consumed_blurbs += chunk;
1860 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1864 @@@ need to write this still
1871 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)
1872 #ifdef FLAC__NO_MANUAL_INLINING
1874 unsigned bit, val_ = 0;
1876 FLAC__ASSERT(bb != 0);
1877 FLAC__ASSERT(bb->buffer != 0);
1880 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1892 unsigned i, val_ = 0;
1893 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1896 FLAC__ASSERT(bb != 0);
1897 FLAC__ASSERT(bb->buffer != 0);
1899 #if FLAC__BITS_PER_BLURB > 8
1900 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { //@@@ comment on why this is here
1902 if(bb->consumed_bits) {
1903 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1905 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1909 bb->consumed_bits += i;
1910 bb->total_consumed_bits += i;
1911 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1912 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1913 bb->consumed_blurbs++;
1914 bb->consumed_bits = 0;
1919 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1920 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1921 bb->consumed_blurbs++;
1922 bb->consumed_bits = 0;
1923 bb->total_consumed_bits += val_;
1927 if(bb->consumed_blurbs >= total_blurbs_) {
1928 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1930 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1932 b = bb->buffer[bb->consumed_blurbs];
1934 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1938 bb->consumed_bits = i;
1940 if(i == FLAC__BITS_PER_BLURB) {
1941 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1942 bb->consumed_blurbs++;
1943 bb->consumed_bits = 0;
1945 bb->total_consumed_bits += i;
1949 val_ += FLAC__BITS_PER_BLURB;
1950 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1951 bb->consumed_blurbs++;
1952 /* bb->consumed_bits is already 0 */
1953 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
1956 #if FLAC__BITS_PER_BLURB > 8
1960 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
1974 #ifdef FLAC__SYMMETRIC_RICE
1975 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)
1977 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1979 FLAC__ASSERT(bb != 0);
1980 FLAC__ASSERT(bb->buffer != 0);
1981 FLAC__ASSERT(parameter <= 31);
1983 /* read the unary MSBs and end bit */
1984 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1987 /* read the sign bit */
1988 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1991 /* read the binary LSBs */
1992 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1995 /* compose the value */
1996 *val = (msbs << parameter) | lsbs;
2002 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2004 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)
2006 FLAC__uint32 lsbs = 0, msbs = 0;
2009 FLAC__ASSERT(bb != 0);
2010 FLAC__ASSERT(bb->buffer != 0);
2011 FLAC__ASSERT(parameter <= 31);
2013 /* read the unary MSBs and end bit */
2014 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2017 /* read the binary LSBs */
2018 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2021 /* compose the value */
2022 uval = (msbs << parameter) | lsbs;
2024 *val = -((int)(uval >> 1)) - 1;
2026 *val = (int)(uval >> 1);
2031 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)
2033 const FLAC__blurb *buffer = bb->buffer;
2035 unsigned i, j, val_i = 0;
2036 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2037 FLAC__blurb blurb, save_blurb;
2038 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2040 FLAC__ASSERT(bb != 0);
2041 FLAC__ASSERT(bb->buffer != 0);
2042 FLAC__ASSERT(parameter <= 31);
2047 i = bb->consumed_blurbs;
2049 * We unroll the main loop to take care of partially consumed blurbs here.
2051 if(bb->consumed_bits > 0) {
2052 save_blurb = blurb = buffer[i];
2053 cbits = bb->consumed_bits;
2059 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2063 /* dispose of the unary end bit */
2069 lsbs_left = parameter;
2071 if(cbits == FLAC__BITS_PER_BLURB) {
2073 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2078 msbs += FLAC__BITS_PER_BLURB - cbits;
2080 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2085 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2086 if(lsbs_left >= available_bits) {
2087 uval <<= available_bits;
2088 uval |= (blurb >> cbits);
2090 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2092 if(lsbs_left == available_bits) {
2093 /* compose the value */
2094 uval |= (msbs << parameter);
2096 vals[val_i++] = -((int)(uval >> 1)) - 1;
2098 vals[val_i++] = (int)(uval >> 1);
2106 lsbs_left -= available_bits;
2111 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2112 blurb <<= lsbs_left;
2115 /* compose the value */
2116 uval |= (msbs << parameter);
2118 vals[val_i++] = -((int)(uval >> 1)) - 1;
2120 vals[val_i++] = (int)(uval >> 1);
2121 if(val_i == nvals) {
2122 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2134 bb->consumed_blurbs = i;
2135 bb->consumed_bits = cbits;
2136 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2140 * Now that we are blurb-aligned the logic is slightly simpler
2142 while(val_i < nvals) {
2143 for( ; i < bb->blurbs && val_i < nvals; i++) {
2144 save_blurb = blurb = buffer[i];
2149 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2153 /* dispose of the unary end bit */
2159 lsbs_left = parameter;
2161 if(cbits == FLAC__BITS_PER_BLURB) {
2163 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2168 msbs += FLAC__BITS_PER_BLURB - cbits;
2170 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2175 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2176 if(lsbs_left >= available_bits) {
2177 uval <<= available_bits;
2178 uval |= (blurb >> cbits);
2180 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2182 if(lsbs_left == available_bits) {
2183 /* compose the value */
2184 uval |= (msbs << parameter);
2186 vals[val_i++] = -((int)(uval >> 1)) - 1;
2188 vals[val_i++] = (int)(uval >> 1);
2196 lsbs_left -= available_bits;
2201 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2202 blurb <<= lsbs_left;
2205 /* compose the value */
2206 uval |= (msbs << parameter);
2208 vals[val_i++] = -((int)(uval >> 1)) - 1;
2210 vals[val_i++] = (int)(uval >> 1);
2211 if(val_i == nvals) {
2212 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2223 bb->consumed_blurbs = i;
2224 bb->consumed_bits = cbits;
2225 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2227 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2229 /* these must be zero because we can only get here if we got to the end of the buffer */
2230 FLAC__ASSERT(bb->consumed_blurbs == 0);
2231 FLAC__ASSERT(bb->consumed_bits == 0);
2240 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)
2242 FLAC__uint32 lsbs = 0, msbs = 0;
2243 unsigned bit, uval, k;
2245 FLAC__ASSERT(bb != 0);
2246 FLAC__ASSERT(bb->buffer != 0);
2248 k = FLAC__bitmath_ilog2(parameter);
2250 /* read the unary MSBs and end bit */
2251 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2254 /* read the binary LSBs */
2255 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2258 if(parameter == 1u<<k) {
2259 /* compose the value */
2260 uval = (msbs << k) | lsbs;
2263 unsigned d = (1 << (k+1)) - parameter;
2265 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2271 /* compose the value */
2272 uval = msbs * parameter + lsbs;
2275 /* unfold unsigned to signed */
2277 *val = -((int)(uval >> 1)) - 1;
2279 *val = (int)(uval >> 1);
2284 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)
2286 FLAC__uint32 lsbs, msbs = 0;
2289 FLAC__ASSERT(bb != 0);
2290 FLAC__ASSERT(bb->buffer != 0);
2292 k = FLAC__bitmath_ilog2(parameter);
2294 /* read the unary MSBs and end bit */
2295 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2298 /* read the binary LSBs */
2299 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2302 if(parameter == 1u<<k) {
2303 /* compose the value */
2304 *val = (msbs << k) | lsbs;
2307 unsigned d = (1 << (k+1)) - parameter;
2309 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2315 /* compose the value */
2316 *val = msbs * parameter + lsbs;
2323 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2324 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)
2330 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2333 raw[(*rawlen)++] = (FLAC__byte)x;
2334 if(!(x & 0x80)) { /* 0xxxxxxx */
2338 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2342 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2346 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2350 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2354 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2363 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2366 raw[(*rawlen)++] = (FLAC__byte)x;
2367 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2378 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2379 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)
2385 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2388 raw[(*rawlen)++] = (FLAC__byte)x;
2389 if(!(x & 0x80)) { /* 0xxxxxxx */
2393 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2397 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2401 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2405 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2409 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2413 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2418 *val = 0xffffffffffffffff;
2422 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2425 raw[(*rawlen)++] = (FLAC__byte)x;
2426 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2427 *val = 0xffffffffffffffff;
2437 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2441 fprintf(out, "bitbuffer is NULL\n");
2444 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);
2446 for(i = 0; i < bb->blurbs; i++) {
2447 fprintf(out, "%08X: ", i);
2448 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2449 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2452 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2456 fprintf(out, "%08X: ", i);
2457 for(j = 0; j < bb->bits; j++)
2458 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2461 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);