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(0 != bb);
138 FLAC__ASSERT(0 != bb->buffer);
140 if(bb->capacity == new_capacity)
143 new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
146 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
147 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
148 bb->blurbs = new_capacity;
150 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
152 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
153 bb->consumed_blurbs = new_capacity;
154 bb->consumed_bits = 0;
155 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
157 free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
158 bb->buffer = new_buffer;
159 bb->capacity = new_capacity;
163 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
165 unsigned new_capacity;
167 FLAC__ASSERT(min_blurbs_to_add > 0);
169 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
170 return bitbuffer_resize_(bb, new_capacity);
173 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
175 FLAC__ASSERT(0 != bb);
176 FLAC__ASSERT(0 != bb->buffer);
178 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
179 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
184 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)
189 /* first shift the unconsumed buffer data toward the front as much as possible */
190 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
191 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
192 for( ; r < r_end; l++, r++)
193 bb->buffer[l] = bb->buffer[r];
194 for( ; l < r_end; l++)
196 bb->blurbs -= bb->consumed_blurbs;
197 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
198 bb->consumed_blurbs = 0;
199 bb->total_consumed_bits = bb->consumed_bits;
202 /* grow if we need to */
203 if(bb->capacity <= 1) {
204 if(!bitbuffer_resize_(bb, 16))
208 /* set the target for reading, taking into account blurb alignment */
209 #if FLAC__BITS_PER_BLURB == 8
210 /* blurb == byte, so no gyrations necessary: */
211 target = bb->buffer + bb->blurbs;
212 bytes = bb->capacity - bb->blurbs;
213 #elif FLAC__BITS_PER_BLURB == 32
214 /* @@@ WATCHOUT: code currently only works for big-endian: */
215 FLAC__ASSERT((bb->bits & 7) == 0);
216 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
217 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
219 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
222 /* finally, read in some data */
223 if(!read_callback(target, &bytes, client_data))
226 /* now we have to handle partial blurb cases: */
227 #if FLAC__BITS_PER_BLURB == 8
228 /* blurb == byte, so no gyrations necessary: */
230 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
231 #elif FLAC__BITS_PER_BLURB == 32
232 /* @@@ WATCHOUT: code currently only works for big-endian: */
234 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
235 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
236 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
237 bb->total_bits += (bytes << 3);
240 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
245 /***********************************************************************
247 * Class constructor/destructor
249 ***********************************************************************/
251 FLAC__BitBuffer *FLAC__bitbuffer_new()
253 FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
256 memset(bb, 0, sizeof(FLAC__BitBuffer));
259 bb->blurbs = bb->bits = bb->total_bits = 0;
260 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
265 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
267 FLAC__ASSERT(0 != bb);
269 FLAC__bitbuffer_free(bb);
273 /***********************************************************************
275 * Public class methods
277 ***********************************************************************/
279 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
281 FLAC__ASSERT(0 != bb);
285 bb->blurbs = bb->bits = bb->total_bits = 0;
286 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
288 return FLAC__bitbuffer_clear(bb);
291 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
293 FLAC__ASSERT(0 != bb);
294 FLAC__ASSERT(bytes > 0);
296 if(!FLAC__bitbuffer_init(bb))
299 if(!bitbuffer_ensure_size_(bb, bytes << 3))
302 FLAC__ASSERT(0 != buffer);
303 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
304 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
305 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
306 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
307 bb->total_bits = bytes << 3;
311 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
313 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
315 FLAC__ASSERT(0 != dest);
316 FLAC__ASSERT(0 != src);
320 if(dest->bits != src->consumed_bits)
322 if(!bitbuffer_ensure_size_(dest, bits_to_add))
324 if(dest->bits == 0) {
325 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
327 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
328 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
329 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
330 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)));
333 dest->buffer[dest->blurbs] <<= bits_to_add;
334 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
336 dest->bits = src->bits;
337 dest->total_bits += bits_to_add;
338 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
343 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
345 FLAC__ASSERT(0 != bb);
351 bb->blurbs = bb->bits = bb->total_bits = 0;
352 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
355 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
357 if(bb->buffer == 0) {
358 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
359 bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
364 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
366 bb->blurbs = bb->bits = bb->total_bits = 0;
367 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
371 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
373 FLAC__ASSERT(0 != dest);
374 FLAC__ASSERT(0 != dest->buffer);
375 FLAC__ASSERT(0 != src);
376 FLAC__ASSERT(0 != src->buffer);
378 if(dest->capacity < src->capacity)
379 if(!bitbuffer_resize_(dest, src->capacity))
381 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
382 dest->blurbs = src->blurbs;
383 dest->bits = src->bits;
384 dest->total_bits = src->total_bits;
385 dest->consumed_blurbs = src->consumed_blurbs;
386 dest->consumed_bits = src->consumed_bits;
387 dest->total_consumed_bits = src->total_consumed_bits;
388 dest->read_crc16 = src->read_crc16;
392 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
394 FLAC__ASSERT(0 != bb);
395 FLAC__ASSERT(0 != bb->buffer);
396 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
398 bb->read_crc16 = seed;
399 #if FLAC__BITS_PER_BLURB == 8
400 /* no need to do anything */
401 #elif FLAC__BITS_PER_BLURB == 32
402 bb->crc16_align = bb->consumed_bits;
404 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
408 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
410 FLAC__ASSERT(0 != bb);
411 FLAC__ASSERT(0 != bb->buffer);
412 FLAC__ASSERT((bb->bits & 7) == 0);
413 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
415 #if FLAC__BITS_PER_BLURB == 8
416 /* no need to do anything */
417 #elif FLAC__BITS_PER_BLURB == 32
418 /*@@@ 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 */
419 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
420 if(bb->consumed_bits == 8) {
421 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
422 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
424 else if(bb->consumed_bits == 16) {
425 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
426 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
427 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
429 else if(bb->consumed_bits == 24) {
430 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
431 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
432 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
433 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
437 if(bb->consumed_bits == 8) {
438 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
439 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
441 else if(bb->consumed_bits == 16) {
442 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
443 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
444 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
446 else if(bb->consumed_bits == 24) {
447 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
448 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
449 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
450 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
453 bb->crc16_align = bb->consumed_bits;
455 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
457 return bb->read_crc16;
460 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
462 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
464 #if FLAC__BITS_PER_BLURB == 8
465 return FLAC__crc16(bb->buffer, bb->blurbs);
466 #elif FLAC__BITS_PER_BLURB == 32
467 /* @@@ WATCHOUT: code currently only works for big-endian: */
468 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
470 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
474 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
476 FLAC__ASSERT(0 != bb);
477 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
478 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
479 #if FLAC__BITS_PER_BLURB == 8
480 return FLAC__crc8(bb->buffer, bb->blurbs);
481 #elif FLAC__BITS_PER_BLURB == 32
482 /* @@@ WATCHOUT: code currently only works for big-endian: */
483 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
485 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
489 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
491 return ((bb->bits & 7) == 0);
494 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
496 return ((bb->consumed_bits & 7) == 0);
499 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
501 return 8 - (bb->consumed_bits & 7);
504 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
506 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
507 return (bb->total_bits - bb->total_consumed_bits) >> 3;
510 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
512 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
513 #if FLAC__BITS_PER_BLURB == 8
514 *buffer = bb->buffer + bb->consumed_blurbs;
515 *bytes = bb->blurbs - bb->consumed_blurbs;
516 #elif FLAC__BITS_PER_BLURB == 32
517 /* @@@ WATCHOUT: code currently only works for big-endian: */
518 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
519 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
521 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
525 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
527 #if FLAC__BITS_PER_BLURB == 8
529 #elif FLAC__BITS_PER_BLURB == 32
530 /* @@@ WATCHOUT: code currently only works for big-endian: */
533 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
537 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
541 FLAC__ASSERT(0 != bb);
542 FLAC__ASSERT(0 != bb->buffer);
546 if(!bitbuffer_ensure_size_(bb, bits))
548 bb->total_bits += bits;
550 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
551 bb->buffer[bb->blurbs] <<= n;
554 if(bb->bits == FLAC__BITS_PER_BLURB) {
562 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
566 FLAC__ASSERT(0 != bb);
567 FLAC__ASSERT(0 != bb->buffer);
569 FLAC__ASSERT(bits <= 32);
572 /* inline the size check so we don't incure a function call unnecessarily */
573 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
574 if(!bitbuffer_ensure_size_(bb, bits))
578 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
579 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
580 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
582 bb->total_bits += bits;
584 n = FLAC__BITS_PER_BLURB - bb->bits;
585 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
586 if(bits < FLAC__BITS_PER_BLURB) {
587 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
591 else if(bits == FLAC__BITS_PER_BLURB) {
592 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
596 k = bits - FLAC__BITS_PER_BLURB;
597 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
598 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
599 val &= (~(0xffffffff << k));
600 bits -= FLAC__BITS_PER_BLURB;
604 bb->buffer[bb->blurbs] <<= bits;
605 bb->buffer[bb->blurbs] |= val;
616 bb->buffer[bb->blurbs] <<= n;
617 bb->buffer[bb->blurbs] |= (val >> k);
618 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
619 val &= (~(0xffffffff << k));
629 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
631 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
634 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
636 static const FLAC__uint64 mask[] = {
638 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
639 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
640 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
641 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
642 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
643 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
644 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
645 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
646 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
647 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
648 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
649 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
650 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
651 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
652 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
653 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
657 FLAC__ASSERT(0 != bb);
658 FLAC__ASSERT(0 != bb->buffer);
660 FLAC__ASSERT(bits <= 64);
663 if(!bitbuffer_ensure_size_(bb, bits))
666 bb->total_bits += bits;
669 if(bits < FLAC__BITS_PER_BLURB) {
670 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
674 else if(bits == FLAC__BITS_PER_BLURB) {
675 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
679 k = bits - FLAC__BITS_PER_BLURB;
680 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
681 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
682 val &= (~(0xffffffffffffffff << k));
683 bits -= FLAC__BITS_PER_BLURB;
687 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
689 bb->buffer[bb->blurbs] <<= n;
690 bb->buffer[bb->blurbs] |= (val >> k);
691 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
692 val &= (~(0xffffffffffffffff << k));
695 if(bb->bits == FLAC__BITS_PER_BLURB) {
705 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
707 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
710 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
712 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
714 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
715 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
717 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
719 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
721 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
727 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
731 /* this could be faster but currently we don't need it to be */
732 for(i = 0; i < nvals; i++) {
733 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
740 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
743 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
745 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
747 if(!FLAC__bitbuffer_write_zeroes(bb, val))
749 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
753 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
757 /* fold signed to unsigned */
760 * (unsigned)(((--val) << 1) - 1);
761 * but without the overflow problem at MININT
763 uval = (unsigned)(((-(++val)) << 1) + 1);
765 uval = (unsigned)(val << 1);
767 msbs = uval >> parameter;
769 return 1 + parameter + msbs;
773 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
775 unsigned bits, msbs, uval;
778 FLAC__ASSERT(parameter > 0);
780 /* fold signed to unsigned */
783 * (unsigned)(((--val) << 1) - 1);
784 * but without the overflow problem at MININT
786 uval = (unsigned)(((-(++val)) << 1) + 1);
788 uval = (unsigned)(val << 1);
790 k = FLAC__bitmath_ilog2(parameter);
791 if(parameter == 1u<<k) {
792 FLAC__ASSERT(k <= 30);
800 d = (1 << (k+1)) - parameter;
801 q = uval / parameter;
802 r = uval - (q * parameter);
811 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
816 FLAC__ASSERT(parameter > 0);
818 k = FLAC__bitmath_ilog2(parameter);
819 if(parameter == 1u<<k) {
820 FLAC__ASSERT(k <= 30);
828 d = (1 << (k+1)) - parameter;
829 q = uval / parameter;
830 r = uval - (q * parameter);
840 #ifdef FLAC__SYMMETRIC_RICE
841 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
843 unsigned total_bits, interesting_bits, msbs;
844 FLAC__uint32 pattern;
846 FLAC__ASSERT(0 != bb);
847 FLAC__ASSERT(0 != bb->buffer);
848 FLAC__ASSERT(parameter <= 31);
850 /* init pattern with the unary end bit and the sign bit */
858 msbs = val >> parameter;
859 interesting_bits = 2 + parameter;
860 total_bits = interesting_bits + msbs;
861 pattern <<= parameter;
862 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
864 if(total_bits <= 32) {
865 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
869 /* write the unary MSBs */
870 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
872 /* write the unary end bit, the sign bit, and binary LSBs */
873 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
880 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
882 unsigned total_bits, interesting_bits, msbs;
883 FLAC__uint32 pattern;
885 FLAC__ASSERT(0 != bb);
886 FLAC__ASSERT(0 != bb->buffer);
887 FLAC__ASSERT(parameter <= 31);
891 /* init pattern with the unary end bit and the sign bit */
899 msbs = val >> parameter;
900 interesting_bits = 2 + parameter;
901 total_bits = interesting_bits + msbs;
902 pattern <<= parameter;
903 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
905 if(total_bits <= 32) {
906 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
909 else if(total_bits > max_bits) {
914 /* write the unary MSBs */
915 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
917 /* write the unary end bit, the sign bit, and binary LSBs */
918 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
925 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
927 unsigned total_bits, val_bits;
928 FLAC__uint32 pattern;
930 FLAC__ASSERT(0 != bb);
931 FLAC__ASSERT(0 != bb->buffer);
932 FLAC__ASSERT(parameter <= 31);
934 val_bits = FLAC__bitmath_silog2(val);
935 total_bits = 2 + parameter + 5 + val_bits;
937 if(total_bits <= 32) {
939 pattern <<= (parameter + 5);
941 pattern <<= val_bits;
942 pattern |= (val & ((1 << val_bits) - 1));
943 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
947 /* write the '-0' escape code first */
948 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
950 /* write the length */
951 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
953 /* write the value */
954 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
959 #endif /* ifdef FLAC__SYMMETRIC_RICE */
961 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
963 unsigned total_bits, interesting_bits, msbs, uval;
964 FLAC__uint32 pattern;
966 FLAC__ASSERT(0 != bb);
967 FLAC__ASSERT(0 != bb->buffer);
968 FLAC__ASSERT(parameter <= 30);
970 /* fold signed to unsigned */
973 * (unsigned)(((--val) << 1) - 1);
974 * but without the overflow problem at MININT
976 uval = (unsigned)(((-(++val)) << 1) + 1);
978 uval = (unsigned)(val << 1);
980 msbs = uval >> parameter;
981 interesting_bits = 1 + parameter;
982 total_bits = interesting_bits + msbs;
983 pattern = 1 << parameter; /* the unary end bit */
984 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
986 if(total_bits <= 32) {
987 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
991 /* write the unary MSBs */
992 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
994 /* write the unary end bit and binary LSBs */
995 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1002 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1004 unsigned total_bits, interesting_bits, msbs, uval;
1005 FLAC__uint32 pattern;
1007 FLAC__ASSERT(0 != bb);
1008 FLAC__ASSERT(0 != bb->buffer);
1009 FLAC__ASSERT(parameter <= 30);
1013 /* fold signed to unsigned */
1016 * (unsigned)(((--val) << 1) - 1);
1017 * but without the overflow problem at MININT
1019 uval = (unsigned)(((-(++val)) << 1) + 1);
1021 uval = (unsigned)(val << 1);
1023 msbs = uval >> parameter;
1024 interesting_bits = 1 + parameter;
1025 total_bits = interesting_bits + msbs;
1026 pattern = 1 << parameter; /* the unary end bit */
1027 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1029 if(total_bits <= 32) {
1030 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1033 else if(total_bits > max_bits) {
1038 /* write the unary MSBs */
1039 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1041 /* write the unary end bit and binary LSBs */
1042 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1050 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1052 unsigned total_bits, msbs, uval;
1055 FLAC__ASSERT(0 != bb);
1056 FLAC__ASSERT(0 != bb->buffer);
1057 FLAC__ASSERT(parameter > 0);
1059 /* fold signed to unsigned */
1062 * (unsigned)(((--val) << 1) - 1);
1063 * but without the overflow problem at MININT
1065 uval = (unsigned)(((-(++val)) << 1) + 1);
1067 uval = (unsigned)(val << 1);
1069 k = FLAC__bitmath_ilog2(parameter);
1070 if(parameter == 1u<<k) {
1073 FLAC__ASSERT(k <= 30);
1076 total_bits = 1 + k + msbs;
1077 pattern = 1 << k; /* the unary end bit */
1078 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1080 if(total_bits <= 32) {
1081 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1085 /* write the unary MSBs */
1086 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1088 /* write the unary end bit and binary LSBs */
1089 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1096 d = (1 << (k+1)) - parameter;
1097 q = uval / parameter;
1098 r = uval - (q * parameter);
1099 /* write the unary MSBs */
1100 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1102 /* write the unary end bit */
1103 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1105 /* write the binary LSBs */
1107 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1111 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1118 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1120 unsigned total_bits, msbs;
1123 FLAC__ASSERT(0 != bb);
1124 FLAC__ASSERT(0 != bb->buffer);
1125 FLAC__ASSERT(parameter > 0);
1127 k = FLAC__bitmath_ilog2(parameter);
1128 if(parameter == 1u<<k) {
1131 FLAC__ASSERT(k <= 30);
1134 total_bits = 1 + k + msbs;
1135 pattern = 1 << k; /* the unary end bit */
1136 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1138 if(total_bits <= 32) {
1139 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1143 /* write the unary MSBs */
1144 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1146 /* write the unary end bit and binary LSBs */
1147 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1154 d = (1 << (k+1)) - parameter;
1155 q = uval / parameter;
1156 r = uval - (q * parameter);
1157 /* write the unary MSBs */
1158 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1160 /* write the unary end bit */
1161 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1163 /* write the binary LSBs */
1165 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1169 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1177 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1181 FLAC__ASSERT(0 != bb);
1182 FLAC__ASSERT(0 != bb->buffer);
1184 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1187 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1189 else if(val < 0x800) {
1190 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1191 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1193 else if(val < 0x10000) {
1194 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 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 < 0x200000) {
1199 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1200 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1201 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1202 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1204 else if(val < 0x4000000) {
1205 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1206 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1207 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1208 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1209 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1212 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1213 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1214 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1215 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1216 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1217 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1223 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1227 FLAC__ASSERT(0 != bb);
1228 FLAC__ASSERT(0 != bb->buffer);
1230 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1233 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1235 else if(val < 0x800) {
1236 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1237 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1239 else if(val < 0x10000) {
1240 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 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 < 0x200000) {
1245 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1246 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1247 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1248 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1250 else if(val < 0x4000000) {
1251 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1252 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1253 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1254 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1255 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1257 else if(val < 0x80000000) {
1258 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1259 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1263 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1266 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1267 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1268 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1269 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1270 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1271 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1278 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1280 /* 0-pad to byte boundary */
1282 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1287 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)
1289 /* to avoid a drastic speed penalty we don't:
1290 FLAC__ASSERT(0 != bb);
1291 FLAC__ASSERT(0 != bb->buffer);
1292 FLAC__ASSERT(bb->bits == 0);
1296 if(bb->total_consumed_bits < bb->total_bits) {
1297 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1301 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1307 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)
1309 /* to avoid a drastic speed penalty we don't:
1310 FLAC__ASSERT(0 != bb);
1311 FLAC__ASSERT(0 != bb->buffer);
1312 FLAC__ASSERT(bb->bits == 0);
1316 if(bb->total_consumed_bits < bb->total_bits) {
1317 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1318 bb->consumed_bits++;
1319 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1320 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1321 bb->consumed_blurbs++;
1322 bb->consumed_bits = 0;
1324 bb->total_consumed_bits++;
1328 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1334 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)
1336 /* to avoid a drastic speed penalty we don't:
1337 FLAC__ASSERT(0 != bb);
1338 FLAC__ASSERT(0 != bb->buffer);
1339 FLAC__ASSERT(bb->bits == 0);
1343 if(bb->total_consumed_bits < bb->total_bits) {
1345 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1346 bb->consumed_bits++;
1347 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1348 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1349 bb->consumed_blurbs++;
1350 bb->consumed_bits = 0;
1352 bb->total_consumed_bits++;
1356 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1362 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)
1364 /* to avoid a drastic speed penalty we don't:
1365 FLAC__ASSERT(0 != bb);
1366 FLAC__ASSERT(0 != bb->buffer);
1367 FLAC__ASSERT(bb->bits == 0);
1371 if(bb->total_consumed_bits < bb->total_bits) {
1373 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1374 bb->consumed_bits++;
1375 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1376 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1377 bb->consumed_blurbs++;
1378 bb->consumed_bits = 0;
1380 bb->total_consumed_bits++;
1384 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1390 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)
1391 #ifdef FLAC__NO_MANUAL_INLINING
1395 FLAC__ASSERT(0 != bb);
1396 FLAC__ASSERT(0 != bb->buffer);
1398 FLAC__ASSERT(bits <= 32);
1401 for(i = 0; i < bits; i++) {
1402 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1409 unsigned i, bits_ = bits;
1412 FLAC__ASSERT(0 != bb);
1413 FLAC__ASSERT(0 != bb->buffer);
1415 FLAC__ASSERT(bits <= 32);
1416 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1423 while(bb->total_consumed_bits + bits > bb->total_bits) {
1424 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1427 #if FLAC__BITS_PER_BLURB > 8
1428 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1430 if(bb->consumed_bits) {
1431 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1433 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1435 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1436 bb->consumed_blurbs++;
1437 bb->consumed_bits = 0;
1438 /* we hold off updating bb->total_consumed_bits until the end */
1441 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1442 bb->consumed_bits += bits_;
1443 bb->total_consumed_bits += bits_;
1447 #if FLAC__BITS_PER_BLURB == 32
1448 /* note that we know bits_ cannot be > 32 because of previous assertions */
1449 if(bits_ == FLAC__BITS_PER_BLURB) {
1450 v = bb->buffer[bb->consumed_blurbs];
1451 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1452 bb->consumed_blurbs++;
1453 /* bb->consumed_bits is already 0 */
1454 bb->total_consumed_bits += bits;
1459 while(bits_ >= FLAC__BITS_PER_BLURB) {
1460 v <<= FLAC__BITS_PER_BLURB;
1461 v |= bb->buffer[bb->consumed_blurbs];
1462 bits_ -= FLAC__BITS_PER_BLURB;
1463 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1464 bb->consumed_blurbs++;
1465 /* bb->consumed_bits is already 0 */
1466 /* we hold off updating bb->total_consumed_bits until the end */
1471 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1472 bb->consumed_bits = bits_;
1473 /* we hold off updating bb->total_consumed_bits until the end */
1475 bb->total_consumed_bits += bits;
1477 #if FLAC__BITS_PER_BLURB > 8
1481 for(i = 0; i < bits; i++) {
1482 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1491 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)
1492 #ifdef FLAC__NO_MANUAL_INLINING
1497 FLAC__ASSERT(0 != bb);
1498 FLAC__ASSERT(0 != bb->buffer);
1500 FLAC__ASSERT(bits <= 32);
1508 for(i = 0; i < bits; i++) {
1509 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1517 *val = (FLAC__int32)v;
1521 *val = (FLAC__int32)v;
1527 unsigned i, bits_ = bits;
1530 FLAC__ASSERT(0 != bb);
1531 FLAC__ASSERT(0 != bb->buffer);
1533 FLAC__ASSERT(bits <= 32);
1534 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1541 while(bb->total_consumed_bits + bits > bb->total_bits) {
1542 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1545 #if FLAC__BITS_PER_BLURB > 8
1546 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1548 if(bb->consumed_bits) {
1549 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1551 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1553 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1554 bb->consumed_blurbs++;
1555 bb->consumed_bits = 0;
1556 /* we hold off updating bb->total_consumed_bits until the end */
1559 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1560 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1562 *val = (FLAC__int32)v;
1563 *val >>= (32-bits_);
1564 bb->consumed_bits += bits_;
1565 bb->total_consumed_bits += bits_;
1569 #if FLAC__BITS_PER_BLURB == 32
1570 /* note that we know bits_ cannot be > 32 because of previous assertions */
1571 if(bits_ == FLAC__BITS_PER_BLURB) {
1572 v = bb->buffer[bb->consumed_blurbs];
1574 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1575 bb->consumed_blurbs++;
1576 /* bb->consumed_bits is already 0 */
1577 /* we hold off updating bb->total_consumed_bits until the end */
1580 while(bits_ >= FLAC__BITS_PER_BLURB) {
1581 v <<= FLAC__BITS_PER_BLURB;
1582 v |= bb->buffer[bb->consumed_blurbs];
1583 bits_ -= FLAC__BITS_PER_BLURB;
1584 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1585 bb->consumed_blurbs++;
1586 /* bb->consumed_bits is already 0 */
1587 /* we hold off updating bb->total_consumed_bits until the end */
1592 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1593 bb->consumed_bits = bits_;
1594 /* we hold off updating bb->total_consumed_bits until the end */
1596 bb->total_consumed_bits += bits;
1597 #if FLAC__BITS_PER_BLURB > 8
1600 for(i = 0; i < bits; i++) {
1601 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1611 *val = (FLAC__int32)v;
1615 *val = (FLAC__int32)v;
1621 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)
1622 #ifdef FLAC__NO_MANUAL_INLINING
1626 FLAC__ASSERT(0 != bb);
1627 FLAC__ASSERT(0 != bb->buffer);
1629 FLAC__ASSERT(bits <= 64);
1632 for(i = 0; i < bits; i++) {
1633 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1640 unsigned i, bits_ = bits;
1643 FLAC__ASSERT(0 != bb);
1644 FLAC__ASSERT(0 != bb->buffer);
1646 FLAC__ASSERT(bits <= 64);
1647 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1654 while(bb->total_consumed_bits + bits > bb->total_bits) {
1655 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1658 #if FLAC__BITS_PER_BLURB > 8
1659 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1661 if(bb->consumed_bits) {
1662 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1664 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1666 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1667 bb->consumed_blurbs++;
1668 bb->consumed_bits = 0;
1669 /* we hold off updating bb->total_consumed_bits until the end */
1672 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1673 bb->consumed_bits += bits_;
1674 bb->total_consumed_bits += bits_;
1678 while(bits_ >= FLAC__BITS_PER_BLURB) {
1679 v <<= FLAC__BITS_PER_BLURB;
1680 v |= bb->buffer[bb->consumed_blurbs];
1681 bits_ -= FLAC__BITS_PER_BLURB;
1682 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1683 bb->consumed_blurbs++;
1684 /* bb->consumed_bits is already 0 */
1685 /* we hold off updating bb->total_consumed_bits until the end */
1689 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1690 bb->consumed_bits = bits_;
1691 /* we hold off updating bb->total_consumed_bits until the end */
1693 bb->total_consumed_bits += bits;
1695 #if FLAC__BITS_PER_BLURB > 8
1699 for(i = 0; i < bits; i++) {
1700 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1709 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)
1710 #ifdef FLAC__NO_MANUAL_INLINING
1715 FLAC__ASSERT(0 != bb);
1716 FLAC__ASSERT(0 != bb->buffer);
1718 FLAC__ASSERT(bits <= 64);
1721 for(i = 0; i < bits; i++) {
1722 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1729 *val = (FLAC__int64)v;
1733 *val = (FLAC__int64)v;
1739 unsigned i, bits_ = bits;
1742 FLAC__ASSERT(0 != bb);
1743 FLAC__ASSERT(0 != bb->buffer);
1745 FLAC__ASSERT(bits <= 64);
1746 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1753 while(bb->total_consumed_bits + bits > bb->total_bits) {
1754 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1757 #if FLAC__BITS_PER_BLURB > 8
1758 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1760 if(bb->consumed_bits) {
1761 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1763 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1765 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1766 bb->consumed_blurbs++;
1767 bb->consumed_bits = 0;
1768 /* we hold off updating bb->total_consumed_bits until the end */
1771 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1772 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1774 *val = (FLAC__int64)v;
1775 *val >>= (64-bits_);
1776 bb->consumed_bits += bits_;
1777 bb->total_consumed_bits += bits_;
1781 while(bits_ >= FLAC__BITS_PER_BLURB) {
1782 v <<= FLAC__BITS_PER_BLURB;
1783 v |= bb->buffer[bb->consumed_blurbs];
1784 bits_ -= FLAC__BITS_PER_BLURB;
1785 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1786 bb->consumed_blurbs++;
1787 /* bb->consumed_bits is already 0 */
1788 /* we hold off updating bb->total_consumed_bits until the end */
1792 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1793 bb->consumed_bits = bits_;
1794 /* we hold off updating bb->total_consumed_bits until the end */
1796 bb->total_consumed_bits += bits;
1797 #if FLAC__BITS_PER_BLURB > 8
1800 for(i = 0; i < bits; i++) {
1801 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1811 *val = (FLAC__int64)v;
1815 *val = (FLAC__int64)v;
1821 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)
1823 FLAC__uint32 x8, x32 = 0;
1825 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1827 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1830 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1834 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1838 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1846 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)
1848 FLAC__ASSERT(0 != bb);
1849 FLAC__ASSERT(0 != bb->buffer);
1850 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1851 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1852 #if FLAC__BITS_PER_BLURB == 8
1854 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1856 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1861 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1862 val += FLAC__BYTES_PER_BLURB * chunk;
1865 bb->consumed_blurbs += chunk;
1866 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1870 @@@ need to write this still
1877 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)
1878 #ifdef FLAC__NO_MANUAL_INLINING
1880 unsigned bit, val_ = 0;
1882 FLAC__ASSERT(0 != bb);
1883 FLAC__ASSERT(0 != bb->buffer);
1886 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1898 unsigned i, val_ = 0;
1899 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1902 FLAC__ASSERT(0 != bb);
1903 FLAC__ASSERT(0 != bb->buffer);
1905 #if FLAC__BITS_PER_BLURB > 8
1906 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1908 if(bb->consumed_bits) {
1909 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1911 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1915 bb->consumed_bits += i;
1916 bb->total_consumed_bits += i;
1917 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1918 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1919 bb->consumed_blurbs++;
1920 bb->consumed_bits = 0;
1925 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1926 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1927 bb->consumed_blurbs++;
1928 bb->consumed_bits = 0;
1929 bb->total_consumed_bits += val_;
1933 if(bb->consumed_blurbs >= total_blurbs_) {
1934 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1936 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1938 b = bb->buffer[bb->consumed_blurbs];
1940 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1944 bb->consumed_bits = i;
1946 if(i == FLAC__BITS_PER_BLURB) {
1947 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1948 bb->consumed_blurbs++;
1949 bb->consumed_bits = 0;
1951 bb->total_consumed_bits += i;
1955 val_ += FLAC__BITS_PER_BLURB;
1956 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1957 bb->consumed_blurbs++;
1958 /* bb->consumed_bits is already 0 */
1959 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
1962 #if FLAC__BITS_PER_BLURB > 8
1966 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
1980 #ifdef FLAC__SYMMETRIC_RICE
1981 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)
1983 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1985 FLAC__ASSERT(0 != bb);
1986 FLAC__ASSERT(0 != bb->buffer);
1987 FLAC__ASSERT(parameter <= 31);
1989 /* read the unary MSBs and end bit */
1990 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1993 /* read the sign bit */
1994 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1997 /* read the binary LSBs */
1998 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2001 /* compose the value */
2002 *val = (msbs << parameter) | lsbs;
2008 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2010 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)
2012 FLAC__uint32 lsbs = 0, msbs = 0;
2015 FLAC__ASSERT(0 != bb);
2016 FLAC__ASSERT(0 != bb->buffer);
2017 FLAC__ASSERT(parameter <= 31);
2019 /* read the unary MSBs and end bit */
2020 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2023 /* read the binary LSBs */
2024 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2027 /* compose the value */
2028 uval = (msbs << parameter) | lsbs;
2030 *val = -((int)(uval >> 1)) - 1;
2032 *val = (int)(uval >> 1);
2037 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)
2039 const FLAC__blurb *buffer = bb->buffer;
2041 unsigned i, j, val_i = 0;
2042 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2043 FLAC__blurb blurb, save_blurb;
2044 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2046 FLAC__ASSERT(0 != bb);
2047 FLAC__ASSERT(0 != bb->buffer);
2048 FLAC__ASSERT(parameter <= 31);
2053 i = bb->consumed_blurbs;
2055 * We unroll the main loop to take care of partially consumed blurbs here.
2057 if(bb->consumed_bits > 0) {
2058 save_blurb = blurb = buffer[i];
2059 cbits = bb->consumed_bits;
2065 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2069 /* dispose of the unary end bit */
2075 lsbs_left = parameter;
2077 if(cbits == FLAC__BITS_PER_BLURB) {
2079 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2084 msbs += FLAC__BITS_PER_BLURB - cbits;
2086 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2091 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2092 if(lsbs_left >= available_bits) {
2093 uval <<= available_bits;
2094 uval |= (blurb >> cbits);
2096 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2098 if(lsbs_left == available_bits) {
2099 /* compose the value */
2100 uval |= (msbs << parameter);
2102 vals[val_i++] = -((int)(uval >> 1)) - 1;
2104 vals[val_i++] = (int)(uval >> 1);
2112 lsbs_left -= available_bits;
2117 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2118 blurb <<= lsbs_left;
2121 /* compose the value */
2122 uval |= (msbs << parameter);
2124 vals[val_i++] = -((int)(uval >> 1)) - 1;
2126 vals[val_i++] = (int)(uval >> 1);
2127 if(val_i == nvals) {
2128 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2140 bb->consumed_blurbs = i;
2141 bb->consumed_bits = cbits;
2142 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2146 * Now that we are blurb-aligned the logic is slightly simpler
2148 while(val_i < nvals) {
2149 for( ; i < bb->blurbs && val_i < nvals; i++) {
2150 save_blurb = blurb = buffer[i];
2155 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2159 /* dispose of the unary end bit */
2165 lsbs_left = parameter;
2167 if(cbits == FLAC__BITS_PER_BLURB) {
2169 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2174 msbs += FLAC__BITS_PER_BLURB - cbits;
2176 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2181 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2182 if(lsbs_left >= available_bits) {
2183 uval <<= available_bits;
2184 uval |= (blurb >> cbits);
2186 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2188 if(lsbs_left == available_bits) {
2189 /* compose the value */
2190 uval |= (msbs << parameter);
2192 vals[val_i++] = -((int)(uval >> 1)) - 1;
2194 vals[val_i++] = (int)(uval >> 1);
2202 lsbs_left -= available_bits;
2207 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2208 blurb <<= lsbs_left;
2211 /* compose the value */
2212 uval |= (msbs << parameter);
2214 vals[val_i++] = -((int)(uval >> 1)) - 1;
2216 vals[val_i++] = (int)(uval >> 1);
2217 if(val_i == nvals) {
2218 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2229 bb->consumed_blurbs = i;
2230 bb->consumed_bits = cbits;
2231 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2233 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2235 /* these must be zero because we can only get here if we got to the end of the buffer */
2236 FLAC__ASSERT(bb->consumed_blurbs == 0);
2237 FLAC__ASSERT(bb->consumed_bits == 0);
2246 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)
2248 FLAC__uint32 lsbs = 0, msbs = 0;
2249 unsigned bit, uval, k;
2251 FLAC__ASSERT(0 != bb);
2252 FLAC__ASSERT(0 != bb->buffer);
2254 k = FLAC__bitmath_ilog2(parameter);
2256 /* read the unary MSBs and end bit */
2257 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2260 /* read the binary LSBs */
2261 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2264 if(parameter == 1u<<k) {
2265 /* compose the value */
2266 uval = (msbs << k) | lsbs;
2269 unsigned d = (1 << (k+1)) - parameter;
2271 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2277 /* compose the value */
2278 uval = msbs * parameter + lsbs;
2281 /* unfold unsigned to signed */
2283 *val = -((int)(uval >> 1)) - 1;
2285 *val = (int)(uval >> 1);
2290 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)
2292 FLAC__uint32 lsbs, msbs = 0;
2295 FLAC__ASSERT(0 != bb);
2296 FLAC__ASSERT(0 != bb->buffer);
2298 k = FLAC__bitmath_ilog2(parameter);
2300 /* read the unary MSBs and end bit */
2301 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2304 /* read the binary LSBs */
2305 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2308 if(parameter == 1u<<k) {
2309 /* compose the value */
2310 *val = (msbs << k) | lsbs;
2313 unsigned d = (1 << (k+1)) - parameter;
2315 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2321 /* compose the value */
2322 *val = msbs * parameter + lsbs;
2329 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2330 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)
2336 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2339 raw[(*rawlen)++] = (FLAC__byte)x;
2340 if(!(x & 0x80)) { /* 0xxxxxxx */
2344 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2348 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2352 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2356 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2360 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2369 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2372 raw[(*rawlen)++] = (FLAC__byte)x;
2373 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2384 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2385 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)
2391 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2394 raw[(*rawlen)++] = (FLAC__byte)x;
2395 if(!(x & 0x80)) { /* 0xxxxxxx */
2399 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2403 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2407 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2411 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2415 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2419 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2424 *val = 0xffffffffffffffff;
2428 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2431 raw[(*rawlen)++] = (FLAC__byte)x;
2432 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2433 *val = 0xffffffffffffffff;
2443 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2447 fprintf(out, "bitbuffer is NULL\n");
2450 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);
2452 for(i = 0; i < bb->blurbs; i++) {
2453 fprintf(out, "%08X: ", i);
2454 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2455 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2458 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2462 fprintf(out, "%08X: ", i);
2463 for(j = 0; j < bb->bits; j++)
2464 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2467 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);