1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 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) {
706 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
708 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
712 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
714 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
716 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
717 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
719 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
721 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
723 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
729 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
733 /* this could be faster but currently we don't need it to be */
734 for(i = 0; i < nvals; i++) {
735 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
742 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
745 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
747 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
749 if(!FLAC__bitbuffer_write_zeroes(bb, val))
751 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
755 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
759 /* fold signed to unsigned */
762 * (unsigned)(((--val) << 1) - 1);
763 * but without the overflow problem at MININT
765 uval = (unsigned)(((-(++val)) << 1) + 1);
767 uval = (unsigned)(val << 1);
769 msbs = uval >> parameter;
771 return 1 + parameter + msbs;
775 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
777 unsigned bits, msbs, uval;
780 FLAC__ASSERT(parameter > 0);
782 /* fold signed to unsigned */
785 * (unsigned)(((--val) << 1) - 1);
786 * but without the overflow problem at MININT
788 uval = (unsigned)(((-(++val)) << 1) + 1);
790 uval = (unsigned)(val << 1);
792 k = FLAC__bitmath_ilog2(parameter);
793 if(parameter == 1u<<k) {
794 FLAC__ASSERT(k <= 30);
802 d = (1 << (k+1)) - parameter;
803 q = uval / parameter;
804 r = uval - (q * parameter);
813 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
818 FLAC__ASSERT(parameter > 0);
820 k = FLAC__bitmath_ilog2(parameter);
821 if(parameter == 1u<<k) {
822 FLAC__ASSERT(k <= 30);
830 d = (1 << (k+1)) - parameter;
831 q = uval / parameter;
832 r = uval - (q * parameter);
842 #ifdef FLAC__SYMMETRIC_RICE
843 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
845 unsigned total_bits, interesting_bits, msbs;
846 FLAC__uint32 pattern;
848 FLAC__ASSERT(0 != bb);
849 FLAC__ASSERT(0 != bb->buffer);
850 FLAC__ASSERT(parameter <= 31);
852 /* init pattern with the unary end bit and the sign bit */
860 msbs = val >> parameter;
861 interesting_bits = 2 + parameter;
862 total_bits = interesting_bits + msbs;
863 pattern <<= parameter;
864 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
866 if(total_bits <= 32) {
867 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
871 /* write the unary MSBs */
872 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
874 /* write the unary end bit, the sign bit, and binary LSBs */
875 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
882 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
884 unsigned total_bits, interesting_bits, msbs;
885 FLAC__uint32 pattern;
887 FLAC__ASSERT(0 != bb);
888 FLAC__ASSERT(0 != bb->buffer);
889 FLAC__ASSERT(parameter <= 31);
893 /* init pattern with the unary end bit and the sign bit */
901 msbs = val >> parameter;
902 interesting_bits = 2 + parameter;
903 total_bits = interesting_bits + msbs;
904 pattern <<= parameter;
905 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
907 if(total_bits <= 32) {
908 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
911 else if(total_bits > max_bits) {
916 /* write the unary MSBs */
917 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
919 /* write the unary end bit, the sign bit, and binary LSBs */
920 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
927 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
929 unsigned total_bits, val_bits;
930 FLAC__uint32 pattern;
932 FLAC__ASSERT(0 != bb);
933 FLAC__ASSERT(0 != bb->buffer);
934 FLAC__ASSERT(parameter <= 31);
936 val_bits = FLAC__bitmath_silog2(val);
937 total_bits = 2 + parameter + 5 + val_bits;
939 if(total_bits <= 32) {
941 pattern <<= (parameter + 5);
943 pattern <<= val_bits;
944 pattern |= (val & ((1 << val_bits) - 1));
945 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
949 /* write the '-0' escape code first */
950 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
952 /* write the length */
953 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
955 /* write the value */
956 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
961 #endif /* ifdef FLAC__SYMMETRIC_RICE */
963 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
965 unsigned total_bits, interesting_bits, msbs, uval;
966 FLAC__uint32 pattern;
968 FLAC__ASSERT(0 != bb);
969 FLAC__ASSERT(0 != bb->buffer);
970 FLAC__ASSERT(parameter <= 30);
972 /* fold signed to unsigned */
975 * (unsigned)(((--val) << 1) - 1);
976 * but without the overflow problem at MININT
978 uval = (unsigned)(((-(++val)) << 1) + 1);
980 uval = (unsigned)(val << 1);
982 msbs = uval >> parameter;
983 interesting_bits = 1 + parameter;
984 total_bits = interesting_bits + msbs;
985 pattern = 1 << parameter; /* the unary end bit */
986 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
988 if(total_bits <= 32) {
989 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
993 /* write the unary MSBs */
994 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
996 /* write the unary end bit and binary LSBs */
997 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1004 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1006 unsigned total_bits, interesting_bits, msbs, uval;
1007 FLAC__uint32 pattern;
1009 FLAC__ASSERT(0 != bb);
1010 FLAC__ASSERT(0 != bb->buffer);
1011 FLAC__ASSERT(parameter <= 30);
1015 /* fold signed to unsigned */
1018 * (unsigned)(((--val) << 1) - 1);
1019 * but without the overflow problem at MININT
1021 uval = (unsigned)(((-(++val)) << 1) + 1);
1023 uval = (unsigned)(val << 1);
1025 msbs = uval >> parameter;
1026 interesting_bits = 1 + parameter;
1027 total_bits = interesting_bits + msbs;
1028 pattern = 1 << parameter; /* the unary end bit */
1029 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1031 if(total_bits <= 32) {
1032 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1035 else if(total_bits > max_bits) {
1040 /* write the unary MSBs */
1041 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1043 /* write the unary end bit and binary LSBs */
1044 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1052 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1054 unsigned total_bits, msbs, uval;
1057 FLAC__ASSERT(0 != bb);
1058 FLAC__ASSERT(0 != bb->buffer);
1059 FLAC__ASSERT(parameter > 0);
1061 /* fold signed to unsigned */
1064 * (unsigned)(((--val) << 1) - 1);
1065 * but without the overflow problem at MININT
1067 uval = (unsigned)(((-(++val)) << 1) + 1);
1069 uval = (unsigned)(val << 1);
1071 k = FLAC__bitmath_ilog2(parameter);
1072 if(parameter == 1u<<k) {
1075 FLAC__ASSERT(k <= 30);
1078 total_bits = 1 + k + msbs;
1079 pattern = 1 << k; /* the unary end bit */
1080 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1082 if(total_bits <= 32) {
1083 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1087 /* write the unary MSBs */
1088 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1090 /* write the unary end bit and binary LSBs */
1091 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1098 d = (1 << (k+1)) - parameter;
1099 q = uval / parameter;
1100 r = uval - (q * parameter);
1101 /* write the unary MSBs */
1102 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1104 /* write the unary end bit */
1105 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1107 /* write the binary LSBs */
1109 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1113 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1120 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1122 unsigned total_bits, msbs;
1125 FLAC__ASSERT(0 != bb);
1126 FLAC__ASSERT(0 != bb->buffer);
1127 FLAC__ASSERT(parameter > 0);
1129 k = FLAC__bitmath_ilog2(parameter);
1130 if(parameter == 1u<<k) {
1133 FLAC__ASSERT(k <= 30);
1136 total_bits = 1 + k + msbs;
1137 pattern = 1 << k; /* the unary end bit */
1138 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1140 if(total_bits <= 32) {
1141 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1145 /* write the unary MSBs */
1146 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1148 /* write the unary end bit and binary LSBs */
1149 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1156 d = (1 << (k+1)) - parameter;
1157 q = uval / parameter;
1158 r = uval - (q * parameter);
1159 /* write the unary MSBs */
1160 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1162 /* write the unary end bit */
1163 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1165 /* write the binary LSBs */
1167 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1171 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1179 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1183 FLAC__ASSERT(0 != bb);
1184 FLAC__ASSERT(0 != bb->buffer);
1186 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1189 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1191 else if(val < 0x800) {
1192 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1193 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1195 else if(val < 0x10000) {
1196 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1197 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1198 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1200 else if(val < 0x200000) {
1201 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1202 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1203 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1204 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1206 else if(val < 0x4000000) {
1207 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 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);
1214 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1215 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1216 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1217 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1218 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1219 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1225 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1229 FLAC__ASSERT(0 != bb);
1230 FLAC__ASSERT(0 != bb->buffer);
1232 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1235 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1237 else if(val < 0x800) {
1238 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1239 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1241 else if(val < 0x10000) {
1242 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1243 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1244 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1246 else if(val < 0x200000) {
1247 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1248 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1249 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1250 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1252 else if(val < 0x4000000) {
1253 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 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);
1259 else if(val < 0x80000000) {
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1263 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1264 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1265 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1268 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1269 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1270 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1271 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1273 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1274 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1280 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1282 /* 0-pad to byte boundary */
1284 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1289 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)
1291 /* to avoid a drastic speed penalty we don't:
1292 FLAC__ASSERT(0 != bb);
1293 FLAC__ASSERT(0 != bb->buffer);
1294 FLAC__ASSERT(bb->bits == 0);
1298 if(bb->total_consumed_bits < bb->total_bits) {
1299 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1303 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1309 FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1312 * @@@ a slightly faster implementation is possible but
1313 * probably not that useful since this is only called a
1314 * couple of times in the metadata readers.
1316 FLAC__ASSERT(0 != bb);
1317 FLAC__ASSERT(0 != bb->buffer);
1320 const unsigned n = bb->consumed_bits & 7;
1326 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
1332 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
1337 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
1345 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)
1347 /* to avoid a drastic speed penalty we don't:
1348 FLAC__ASSERT(0 != bb);
1349 FLAC__ASSERT(0 != bb->buffer);
1350 FLAC__ASSERT(bb->bits == 0);
1354 if(bb->total_consumed_bits < bb->total_bits) {
1355 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1356 bb->consumed_bits++;
1357 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1358 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1359 bb->consumed_blurbs++;
1360 bb->consumed_bits = 0;
1362 bb->total_consumed_bits++;
1366 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1372 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)
1374 /* to avoid a drastic speed penalty we don't:
1375 FLAC__ASSERT(0 != bb);
1376 FLAC__ASSERT(0 != bb->buffer);
1377 FLAC__ASSERT(bb->bits == 0);
1381 if(bb->total_consumed_bits < bb->total_bits) {
1383 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1384 bb->consumed_bits++;
1385 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1386 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1387 bb->consumed_blurbs++;
1388 bb->consumed_bits = 0;
1390 bb->total_consumed_bits++;
1394 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1400 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)
1402 /* to avoid a drastic speed penalty we don't:
1403 FLAC__ASSERT(0 != bb);
1404 FLAC__ASSERT(0 != bb->buffer);
1405 FLAC__ASSERT(bb->bits == 0);
1409 if(bb->total_consumed_bits < bb->total_bits) {
1411 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1412 bb->consumed_bits++;
1413 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1414 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1415 bb->consumed_blurbs++;
1416 bb->consumed_bits = 0;
1418 bb->total_consumed_bits++;
1422 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1428 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)
1429 #ifdef FLAC__NO_MANUAL_INLINING
1433 FLAC__ASSERT(0 != bb);
1434 FLAC__ASSERT(0 != bb->buffer);
1436 FLAC__ASSERT(bits <= 32);
1439 for(i = 0; i < bits; i++) {
1440 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1447 unsigned i, bits_ = bits;
1450 FLAC__ASSERT(0 != bb);
1451 FLAC__ASSERT(0 != bb->buffer);
1453 FLAC__ASSERT(bits <= 32);
1454 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1461 while(bb->total_consumed_bits + bits > bb->total_bits) {
1462 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1465 #if FLAC__BITS_PER_BLURB > 8
1466 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1468 if(bb->consumed_bits) {
1469 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1471 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1473 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1474 bb->consumed_blurbs++;
1475 bb->consumed_bits = 0;
1476 /* we hold off updating bb->total_consumed_bits until the end */
1479 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1480 bb->consumed_bits += bits_;
1481 bb->total_consumed_bits += bits_;
1485 #if FLAC__BITS_PER_BLURB == 32
1486 /* note that we know bits_ cannot be > 32 because of previous assertions */
1487 if(bits_ == FLAC__BITS_PER_BLURB) {
1488 v = bb->buffer[bb->consumed_blurbs];
1489 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1490 bb->consumed_blurbs++;
1491 /* bb->consumed_bits is already 0 */
1492 bb->total_consumed_bits += bits;
1497 while(bits_ >= FLAC__BITS_PER_BLURB) {
1498 v <<= FLAC__BITS_PER_BLURB;
1499 v |= bb->buffer[bb->consumed_blurbs];
1500 bits_ -= FLAC__BITS_PER_BLURB;
1501 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1502 bb->consumed_blurbs++;
1503 /* bb->consumed_bits is already 0 */
1504 /* we hold off updating bb->total_consumed_bits until the end */
1509 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1510 bb->consumed_bits = bits_;
1511 /* we hold off updating bb->total_consumed_bits until the end */
1513 bb->total_consumed_bits += bits;
1515 #if FLAC__BITS_PER_BLURB > 8
1519 for(i = 0; i < bits; i++) {
1520 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1529 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)
1530 #ifdef FLAC__NO_MANUAL_INLINING
1535 FLAC__ASSERT(0 != bb);
1536 FLAC__ASSERT(0 != bb->buffer);
1538 FLAC__ASSERT(bits <= 32);
1546 for(i = 0; i < bits; i++) {
1547 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1555 *val = (FLAC__int32)v;
1559 *val = (FLAC__int32)v;
1565 unsigned i, bits_ = bits;
1568 FLAC__ASSERT(0 != bb);
1569 FLAC__ASSERT(0 != bb->buffer);
1571 FLAC__ASSERT(bits <= 32);
1572 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1579 while(bb->total_consumed_bits + bits > bb->total_bits) {
1580 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1583 #if FLAC__BITS_PER_BLURB > 8
1584 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1586 if(bb->consumed_bits) {
1587 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1589 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1591 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1592 bb->consumed_blurbs++;
1593 bb->consumed_bits = 0;
1594 /* we hold off updating bb->total_consumed_bits until the end */
1597 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1598 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1600 *val = (FLAC__int32)v;
1601 *val >>= (32-bits_);
1602 bb->consumed_bits += bits_;
1603 bb->total_consumed_bits += bits_;
1607 #if FLAC__BITS_PER_BLURB == 32
1608 /* note that we know bits_ cannot be > 32 because of previous assertions */
1609 if(bits_ == FLAC__BITS_PER_BLURB) {
1610 v = bb->buffer[bb->consumed_blurbs];
1612 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1613 bb->consumed_blurbs++;
1614 /* bb->consumed_bits is already 0 */
1615 /* we hold off updating bb->total_consumed_bits until the end */
1618 while(bits_ >= FLAC__BITS_PER_BLURB) {
1619 v <<= FLAC__BITS_PER_BLURB;
1620 v |= bb->buffer[bb->consumed_blurbs];
1621 bits_ -= FLAC__BITS_PER_BLURB;
1622 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1623 bb->consumed_blurbs++;
1624 /* bb->consumed_bits is already 0 */
1625 /* we hold off updating bb->total_consumed_bits until the end */
1630 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1631 bb->consumed_bits = bits_;
1632 /* we hold off updating bb->total_consumed_bits until the end */
1634 bb->total_consumed_bits += bits;
1635 #if FLAC__BITS_PER_BLURB > 8
1638 for(i = 0; i < bits; i++) {
1639 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1649 *val = (FLAC__int32)v;
1653 *val = (FLAC__int32)v;
1659 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)
1660 #ifdef FLAC__NO_MANUAL_INLINING
1664 FLAC__ASSERT(0 != bb);
1665 FLAC__ASSERT(0 != bb->buffer);
1667 FLAC__ASSERT(bits <= 64);
1670 for(i = 0; i < bits; i++) {
1671 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1678 unsigned i, bits_ = bits;
1681 FLAC__ASSERT(0 != bb);
1682 FLAC__ASSERT(0 != bb->buffer);
1684 FLAC__ASSERT(bits <= 64);
1685 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1692 while(bb->total_consumed_bits + bits > bb->total_bits) {
1693 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1696 #if FLAC__BITS_PER_BLURB > 8
1697 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1699 if(bb->consumed_bits) {
1700 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1702 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1704 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1705 bb->consumed_blurbs++;
1706 bb->consumed_bits = 0;
1707 /* we hold off updating bb->total_consumed_bits until the end */
1710 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1711 bb->consumed_bits += bits_;
1712 bb->total_consumed_bits += bits_;
1716 while(bits_ >= FLAC__BITS_PER_BLURB) {
1717 v <<= FLAC__BITS_PER_BLURB;
1718 v |= bb->buffer[bb->consumed_blurbs];
1719 bits_ -= FLAC__BITS_PER_BLURB;
1720 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1721 bb->consumed_blurbs++;
1722 /* bb->consumed_bits is already 0 */
1723 /* we hold off updating bb->total_consumed_bits until the end */
1727 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1728 bb->consumed_bits = bits_;
1729 /* we hold off updating bb->total_consumed_bits until the end */
1731 bb->total_consumed_bits += bits;
1733 #if FLAC__BITS_PER_BLURB > 8
1737 for(i = 0; i < bits; i++) {
1738 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1748 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)
1749 #ifdef FLAC__NO_MANUAL_INLINING
1754 FLAC__ASSERT(0 != bb);
1755 FLAC__ASSERT(0 != bb->buffer);
1757 FLAC__ASSERT(bits <= 64);
1760 for(i = 0; i < bits; i++) {
1761 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1768 *val = (FLAC__int64)v;
1772 *val = (FLAC__int64)v;
1778 unsigned i, bits_ = bits;
1781 FLAC__ASSERT(0 != bb);
1782 FLAC__ASSERT(0 != bb->buffer);
1784 FLAC__ASSERT(bits <= 64);
1785 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1792 while(bb->total_consumed_bits + bits > bb->total_bits) {
1793 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1796 #if FLAC__BITS_PER_BLURB > 8
1797 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1799 if(bb->consumed_bits) {
1800 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1802 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1804 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1805 bb->consumed_blurbs++;
1806 bb->consumed_bits = 0;
1807 /* we hold off updating bb->total_consumed_bits until the end */
1810 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1811 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1813 *val = (FLAC__int64)v;
1814 *val >>= (64-bits_);
1815 bb->consumed_bits += bits_;
1816 bb->total_consumed_bits += bits_;
1820 while(bits_ >= FLAC__BITS_PER_BLURB) {
1821 v <<= FLAC__BITS_PER_BLURB;
1822 v |= bb->buffer[bb->consumed_blurbs];
1823 bits_ -= FLAC__BITS_PER_BLURB;
1824 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1825 bb->consumed_blurbs++;
1826 /* bb->consumed_bits is already 0 */
1827 /* we hold off updating bb->total_consumed_bits until the end */
1831 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1832 bb->consumed_bits = bits_;
1833 /* we hold off updating bb->total_consumed_bits until the end */
1835 bb->total_consumed_bits += bits;
1836 #if FLAC__BITS_PER_BLURB > 8
1839 for(i = 0; i < bits; i++) {
1840 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1850 *val = (FLAC__int64)v;
1854 *val = (FLAC__int64)v;
1861 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)
1863 FLAC__uint32 x8, x32 = 0;
1865 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1867 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1870 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1874 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1878 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1886 FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1888 FLAC__ASSERT(0 != bb);
1889 FLAC__ASSERT(0 != bb->buffer);
1890 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1891 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1892 #if FLAC__BITS_PER_BLURB == 8
1894 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1896 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1901 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1902 val += FLAC__BYTES_PER_BLURB * chunk;
1905 bb->consumed_blurbs += chunk;
1906 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1910 @@@ need to write this still
1917 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)
1918 #ifdef FLAC__NO_MANUAL_INLINING
1920 unsigned bit, val_ = 0;
1922 FLAC__ASSERT(0 != bb);
1923 FLAC__ASSERT(0 != bb->buffer);
1926 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1938 unsigned i, val_ = 0;
1939 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1942 FLAC__ASSERT(0 != bb);
1943 FLAC__ASSERT(0 != bb->buffer);
1945 #if FLAC__BITS_PER_BLURB > 8
1946 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1948 if(bb->consumed_bits) {
1949 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1951 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1955 bb->consumed_bits += i;
1956 bb->total_consumed_bits += i;
1957 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1958 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1959 bb->consumed_blurbs++;
1960 bb->consumed_bits = 0;
1965 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1966 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1967 bb->consumed_blurbs++;
1968 bb->consumed_bits = 0;
1969 bb->total_consumed_bits += val_;
1973 if(bb->consumed_blurbs >= total_blurbs_) {
1974 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1976 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1978 b = bb->buffer[bb->consumed_blurbs];
1980 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1984 bb->consumed_bits = i;
1986 if(i == FLAC__BITS_PER_BLURB) {
1987 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1988 bb->consumed_blurbs++;
1989 bb->consumed_bits = 0;
1991 bb->total_consumed_bits += i;
1995 val_ += FLAC__BITS_PER_BLURB;
1996 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1997 bb->consumed_blurbs++;
1998 /* bb->consumed_bits is already 0 */
1999 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
2002 #if FLAC__BITS_PER_BLURB > 8
2006 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
2020 #ifdef FLAC__SYMMETRIC_RICE
2021 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)
2023 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
2025 FLAC__ASSERT(0 != bb);
2026 FLAC__ASSERT(0 != bb->buffer);
2027 FLAC__ASSERT(parameter <= 31);
2029 /* read the unary MSBs and end bit */
2030 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2033 /* read the sign bit */
2034 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
2037 /* read the binary LSBs */
2038 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2041 /* compose the value */
2042 *val = (msbs << parameter) | lsbs;
2048 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2050 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)
2052 FLAC__uint32 lsbs = 0, msbs = 0;
2055 FLAC__ASSERT(0 != bb);
2056 FLAC__ASSERT(0 != bb->buffer);
2057 FLAC__ASSERT(parameter <= 31);
2059 /* read the unary MSBs and end bit */
2060 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2063 /* read the binary LSBs */
2064 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2067 /* compose the value */
2068 uval = (msbs << parameter) | lsbs;
2070 *val = -((int)(uval >> 1)) - 1;
2072 *val = (int)(uval >> 1);
2077 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)
2079 const FLAC__blurb *buffer = bb->buffer;
2081 unsigned i, j, val_i = 0;
2082 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2083 FLAC__blurb blurb, save_blurb;
2084 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2086 FLAC__ASSERT(0 != bb);
2087 FLAC__ASSERT(0 != bb->buffer);
2088 FLAC__ASSERT(parameter <= 31);
2093 i = bb->consumed_blurbs;
2095 * We unroll the main loop to take care of partially consumed blurbs here.
2097 if(bb->consumed_bits > 0) {
2098 save_blurb = blurb = buffer[i];
2099 cbits = bb->consumed_bits;
2105 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2109 /* dispose of the unary end bit */
2115 lsbs_left = parameter;
2117 if(cbits == FLAC__BITS_PER_BLURB) {
2119 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2124 msbs += FLAC__BITS_PER_BLURB - cbits;
2126 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2131 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2132 if(lsbs_left >= available_bits) {
2133 uval <<= available_bits;
2134 uval |= (blurb >> cbits);
2136 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2138 if(lsbs_left == available_bits) {
2139 /* compose the value */
2140 uval |= (msbs << parameter);
2142 vals[val_i++] = -((int)(uval >> 1)) - 1;
2144 vals[val_i++] = (int)(uval >> 1);
2152 lsbs_left -= available_bits;
2157 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2158 blurb <<= lsbs_left;
2161 /* compose the value */
2162 uval |= (msbs << parameter);
2164 vals[val_i++] = -((int)(uval >> 1)) - 1;
2166 vals[val_i++] = (int)(uval >> 1);
2167 if(val_i == nvals) {
2168 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2180 bb->consumed_blurbs = i;
2181 bb->consumed_bits = cbits;
2182 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2186 * Now that we are blurb-aligned the logic is slightly simpler
2188 while(val_i < nvals) {
2189 for( ; i < bb->blurbs && val_i < nvals; i++) {
2190 save_blurb = blurb = buffer[i];
2195 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2199 /* dispose of the unary end bit */
2205 lsbs_left = parameter;
2207 if(cbits == FLAC__BITS_PER_BLURB) {
2209 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2214 msbs += FLAC__BITS_PER_BLURB - cbits;
2216 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2221 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2222 if(lsbs_left >= available_bits) {
2223 uval <<= available_bits;
2224 uval |= (blurb >> cbits);
2226 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2228 if(lsbs_left == available_bits) {
2229 /* compose the value */
2230 uval |= (msbs << parameter);
2232 vals[val_i++] = -((int)(uval >> 1)) - 1;
2234 vals[val_i++] = (int)(uval >> 1);
2242 lsbs_left -= available_bits;
2247 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2248 blurb <<= lsbs_left;
2251 /* compose the value */
2252 uval |= (msbs << parameter);
2254 vals[val_i++] = -((int)(uval >> 1)) - 1;
2256 vals[val_i++] = (int)(uval >> 1);
2257 if(val_i == nvals) {
2258 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2269 bb->consumed_blurbs = i;
2270 bb->consumed_bits = cbits;
2271 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2273 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2275 /* these must be zero because we can only get here if we got to the end of the buffer */
2276 FLAC__ASSERT(bb->consumed_blurbs == 0);
2277 FLAC__ASSERT(bb->consumed_bits == 0);
2286 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)
2288 FLAC__uint32 lsbs = 0, msbs = 0;
2289 unsigned bit, uval, k;
2291 FLAC__ASSERT(0 != bb);
2292 FLAC__ASSERT(0 != bb->buffer);
2294 k = FLAC__bitmath_ilog2(parameter);
2296 /* read the unary MSBs and end bit */
2297 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2300 /* read the binary LSBs */
2301 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2304 if(parameter == 1u<<k) {
2305 /* compose the value */
2306 uval = (msbs << k) | lsbs;
2309 unsigned d = (1 << (k+1)) - parameter;
2311 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2317 /* compose the value */
2318 uval = msbs * parameter + lsbs;
2321 /* unfold unsigned to signed */
2323 *val = -((int)(uval >> 1)) - 1;
2325 *val = (int)(uval >> 1);
2330 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)
2332 FLAC__uint32 lsbs, msbs = 0;
2335 FLAC__ASSERT(0 != bb);
2336 FLAC__ASSERT(0 != bb->buffer);
2338 k = FLAC__bitmath_ilog2(parameter);
2340 /* read the unary MSBs and end bit */
2341 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2344 /* read the binary LSBs */
2345 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2348 if(parameter == 1u<<k) {
2349 /* compose the value */
2350 *val = (msbs << k) | lsbs;
2353 unsigned d = (1 << (k+1)) - parameter;
2355 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2361 /* compose the value */
2362 *val = msbs * parameter + lsbs;
2369 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2370 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)
2376 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2379 raw[(*rawlen)++] = (FLAC__byte)x;
2380 if(!(x & 0x80)) { /* 0xxxxxxx */
2384 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2388 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2392 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2396 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2400 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2409 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2412 raw[(*rawlen)++] = (FLAC__byte)x;
2413 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2424 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2425 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)
2431 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2434 raw[(*rawlen)++] = (FLAC__byte)x;
2435 if(!(x & 0x80)) { /* 0xxxxxxx */
2439 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2443 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2447 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2451 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2455 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2459 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2464 *val = 0xffffffffffffffff;
2468 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2471 raw[(*rawlen)++] = (FLAC__byte)x;
2472 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2473 *val = 0xffffffffffffffff;
2483 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2487 fprintf(out, "bitbuffer is NULL\n");
2490 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);
2492 for(i = 0; i < bb->blurbs; i++) {
2493 fprintf(out, "%08X: ", i);
2494 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2495 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2498 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2502 fprintf(out, "%08X: ", i);
2503 for(j = 0; j < bb->bits; j++)
2504 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2507 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);