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*)malloc(sizeof(FLAC__blurb) * new_capacity);
146 memset(new_buffer, 0, sizeof(FLAC__blurb) * new_capacity);
147 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
148 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
149 bb->blurbs = new_capacity;
151 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
153 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
154 bb->consumed_blurbs = new_capacity;
155 bb->consumed_bits = 0;
156 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
158 free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
159 bb->buffer = new_buffer;
160 bb->capacity = new_capacity;
164 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
166 unsigned new_capacity;
168 FLAC__ASSERT(min_blurbs_to_add > 0);
170 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
171 return bitbuffer_resize_(bb, new_capacity);
174 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
176 FLAC__ASSERT(0 != bb);
177 FLAC__ASSERT(0 != bb->buffer);
179 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
180 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
185 static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
190 /* first shift the unconsumed buffer data toward the front as much as possible */
191 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
192 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
193 for( ; r < r_end; l++, r++)
194 bb->buffer[l] = bb->buffer[r];
195 for( ; l < r_end; l++)
197 bb->blurbs -= bb->consumed_blurbs;
198 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
199 bb->consumed_blurbs = 0;
200 bb->total_consumed_bits = bb->consumed_bits;
203 /* grow if we need to */
204 if(bb->capacity <= 1) {
205 if(!bitbuffer_resize_(bb, 16))
209 /* set the target for reading, taking into account blurb alignment */
210 #if FLAC__BITS_PER_BLURB == 8
211 /* blurb == byte, so no gyrations necessary: */
212 target = bb->buffer + bb->blurbs;
213 bytes = bb->capacity - bb->blurbs;
214 #elif FLAC__BITS_PER_BLURB == 32
215 /* @@@ WATCHOUT: code currently only works for big-endian: */
216 FLAC__ASSERT((bb->bits & 7) == 0);
217 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
218 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
220 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
223 /* finally, read in some data */
224 if(!read_callback(target, &bytes, client_data))
227 /* now we have to handle partial blurb cases: */
228 #if FLAC__BITS_PER_BLURB == 8
229 /* blurb == byte, so no gyrations necessary: */
231 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
232 #elif FLAC__BITS_PER_BLURB == 32
233 /* @@@ WATCHOUT: code currently only works for big-endian: */
235 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
236 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
237 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
238 bb->total_bits += (bytes << 3);
241 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
246 /***********************************************************************
248 * Class constructor/destructor
250 ***********************************************************************/
252 FLAC__BitBuffer *FLAC__bitbuffer_new()
254 FLAC__BitBuffer *bb = (FLAC__BitBuffer*)malloc(sizeof(FLAC__BitBuffer));
257 memset(bb, 0, sizeof(FLAC__BitBuffer));
260 bb->blurbs = bb->bits = bb->total_bits = 0;
261 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
266 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
268 FLAC__ASSERT(0 != bb);
270 FLAC__bitbuffer_free(bb);
274 /***********************************************************************
276 * Public class methods
278 ***********************************************************************/
280 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
282 FLAC__ASSERT(0 != bb);
286 bb->blurbs = bb->bits = bb->total_bits = 0;
287 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
289 return FLAC__bitbuffer_clear(bb);
292 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
294 FLAC__ASSERT(0 != bb);
295 FLAC__ASSERT(bytes > 0);
297 if(!FLAC__bitbuffer_init(bb))
300 if(!bitbuffer_ensure_size_(bb, bytes << 3))
303 FLAC__ASSERT(0 != buffer);
304 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
305 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
306 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
307 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
308 bb->total_bits = bytes << 3;
312 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
314 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
316 FLAC__ASSERT(0 != dest);
317 FLAC__ASSERT(0 != src);
321 if(dest->bits != src->consumed_bits)
323 if(!bitbuffer_ensure_size_(dest, bits_to_add))
325 if(dest->bits == 0) {
326 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
328 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
329 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
330 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
331 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)));
334 dest->buffer[dest->blurbs] <<= bits_to_add;
335 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
337 dest->bits = src->bits;
338 dest->total_bits += bits_to_add;
339 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
344 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
346 FLAC__ASSERT(0 != bb);
352 bb->blurbs = bb->bits = bb->total_bits = 0;
353 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
356 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
358 if(bb->buffer == 0) {
359 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
360 bb->buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * bb->capacity);
363 memset(bb->buffer, 0, bb->capacity);
366 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
368 bb->blurbs = bb->bits = bb->total_bits = 0;
369 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
373 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
375 FLAC__ASSERT(0 != dest);
376 FLAC__ASSERT(0 != dest->buffer);
377 FLAC__ASSERT(0 != src);
378 FLAC__ASSERT(0 != src->buffer);
380 if(dest->capacity < src->capacity)
381 if(!bitbuffer_resize_(dest, src->capacity))
383 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
384 dest->blurbs = src->blurbs;
385 dest->bits = src->bits;
386 dest->total_bits = src->total_bits;
387 dest->consumed_blurbs = src->consumed_blurbs;
388 dest->consumed_bits = src->consumed_bits;
389 dest->total_consumed_bits = src->total_consumed_bits;
390 dest->read_crc16 = src->read_crc16;
394 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
396 FLAC__ASSERT(0 != bb);
397 FLAC__ASSERT(0 != bb->buffer);
398 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
400 bb->read_crc16 = seed;
401 #if FLAC__BITS_PER_BLURB == 8
402 /* no need to do anything */
403 #elif FLAC__BITS_PER_BLURB == 32
404 bb->crc16_align = bb->consumed_bits;
406 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
410 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
412 FLAC__ASSERT(0 != bb);
413 FLAC__ASSERT(0 != bb->buffer);
414 FLAC__ASSERT((bb->bits & 7) == 0);
415 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
417 #if FLAC__BITS_PER_BLURB == 8
418 /* no need to do anything */
419 #elif FLAC__BITS_PER_BLURB == 32
420 /*@@@ 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 */
421 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
422 if(bb->consumed_bits == 8) {
423 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
424 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
426 else if(bb->consumed_bits == 16) {
427 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
428 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
429 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
431 else if(bb->consumed_bits == 24) {
432 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
433 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
434 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
435 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
439 if(bb->consumed_bits == 8) {
440 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
441 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
443 else if(bb->consumed_bits == 16) {
444 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
445 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
446 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
448 else if(bb->consumed_bits == 24) {
449 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
450 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
451 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
452 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
455 bb->crc16_align = bb->consumed_bits;
457 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
459 return bb->read_crc16;
462 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
464 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
466 #if FLAC__BITS_PER_BLURB == 8
467 return FLAC__crc16(bb->buffer, bb->blurbs);
468 #elif FLAC__BITS_PER_BLURB == 32
469 /* @@@ WATCHOUT: code currently only works for big-endian: */
470 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
472 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
476 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
478 FLAC__ASSERT(0 != bb);
479 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
480 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
481 #if FLAC__BITS_PER_BLURB == 8
482 return FLAC__crc8(bb->buffer, bb->blurbs);
483 #elif FLAC__BITS_PER_BLURB == 32
484 /* @@@ WATCHOUT: code currently only works for big-endian: */
485 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
487 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
491 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
493 return ((bb->bits & 7) == 0);
496 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
498 return ((bb->consumed_bits & 7) == 0);
501 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
503 return 8 - (bb->consumed_bits & 7);
506 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
508 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
509 return (bb->total_bits - bb->total_consumed_bits) >> 3;
512 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
514 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
515 #if FLAC__BITS_PER_BLURB == 8
516 *buffer = bb->buffer + bb->consumed_blurbs;
517 *bytes = bb->blurbs - bb->consumed_blurbs;
518 #elif FLAC__BITS_PER_BLURB == 32
519 /* @@@ WATCHOUT: code currently only works for big-endian: */
520 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
521 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
523 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
527 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
529 #if FLAC__BITS_PER_BLURB == 8
531 #elif FLAC__BITS_PER_BLURB == 32
532 /* @@@ WATCHOUT: code currently only works for big-endian: */
535 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
539 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
543 FLAC__ASSERT(0 != bb);
544 FLAC__ASSERT(0 != bb->buffer);
548 if(!bitbuffer_ensure_size_(bb, bits))
550 bb->total_bits += bits;
552 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
553 bb->buffer[bb->blurbs] <<= n;
556 if(bb->bits == FLAC__BITS_PER_BLURB) {
564 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
568 FLAC__ASSERT(0 != bb);
569 FLAC__ASSERT(0 != bb->buffer);
571 FLAC__ASSERT(bits <= 32);
574 /* inline the size check so we don't incure a function call unnecessarily */
575 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
576 if(!bitbuffer_ensure_size_(bb, bits))
580 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
581 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
582 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
584 bb->total_bits += bits;
586 n = FLAC__BITS_PER_BLURB - bb->bits;
587 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
588 if(bits < FLAC__BITS_PER_BLURB) {
589 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
593 else if(bits == FLAC__BITS_PER_BLURB) {
594 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
598 k = bits - FLAC__BITS_PER_BLURB;
599 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
600 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
601 val &= (~(0xffffffff << k));
602 bits -= FLAC__BITS_PER_BLURB;
606 bb->buffer[bb->blurbs] <<= bits;
607 bb->buffer[bb->blurbs] |= val;
618 bb->buffer[bb->blurbs] <<= n;
619 bb->buffer[bb->blurbs] |= (val >> k);
620 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
621 val &= (~(0xffffffff << k));
631 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
633 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
636 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
638 static const FLAC__uint64 mask[] = {
640 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
641 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
642 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
643 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
644 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
645 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
646 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
647 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
648 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
649 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
650 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
651 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
652 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
653 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
654 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
655 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
659 FLAC__ASSERT(0 != bb);
660 FLAC__ASSERT(0 != bb->buffer);
662 FLAC__ASSERT(bits <= 64);
665 if(!bitbuffer_ensure_size_(bb, bits))
668 bb->total_bits += bits;
671 if(bits < FLAC__BITS_PER_BLURB) {
672 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
676 else if(bits == FLAC__BITS_PER_BLURB) {
677 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
681 k = bits - FLAC__BITS_PER_BLURB;
682 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
683 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
684 val &= (~(0xffffffffffffffff << k));
685 bits -= FLAC__BITS_PER_BLURB;
689 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
691 bb->buffer[bb->blurbs] <<= n;
692 bb->buffer[bb->blurbs] |= (val >> k);
693 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
694 val &= (~(0xffffffffffffffff << k));
697 if(bb->bits == FLAC__BITS_PER_BLURB) {
707 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
709 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_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1311 /* to avoid a drastic speed penalty we don't:
1312 FLAC__ASSERT(0 != bb);
1313 FLAC__ASSERT(0 != bb->buffer);
1314 FLAC__ASSERT(bb->bits == 0);
1318 if(bb->total_consumed_bits < bb->total_bits) {
1319 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1320 bb->consumed_bits++;
1321 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1322 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1323 bb->consumed_blurbs++;
1324 bb->consumed_bits = 0;
1326 bb->total_consumed_bits++;
1330 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1336 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)
1338 /* to avoid a drastic speed penalty we don't:
1339 FLAC__ASSERT(0 != bb);
1340 FLAC__ASSERT(0 != bb->buffer);
1341 FLAC__ASSERT(bb->bits == 0);
1345 if(bb->total_consumed_bits < bb->total_bits) {
1347 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1348 bb->consumed_bits++;
1349 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1350 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1351 bb->consumed_blurbs++;
1352 bb->consumed_bits = 0;
1354 bb->total_consumed_bits++;
1358 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1364 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)
1366 /* to avoid a drastic speed penalty we don't:
1367 FLAC__ASSERT(0 != bb);
1368 FLAC__ASSERT(0 != bb->buffer);
1369 FLAC__ASSERT(bb->bits == 0);
1373 if(bb->total_consumed_bits < bb->total_bits) {
1375 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1376 bb->consumed_bits++;
1377 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1378 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1379 bb->consumed_blurbs++;
1380 bb->consumed_bits = 0;
1382 bb->total_consumed_bits++;
1386 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1392 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)
1393 #ifdef FLAC__NO_MANUAL_INLINING
1397 FLAC__ASSERT(0 != bb);
1398 FLAC__ASSERT(0 != bb->buffer);
1400 FLAC__ASSERT(bits <= 32);
1403 for(i = 0; i < bits; i++) {
1404 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1411 unsigned i, bits_ = bits;
1414 FLAC__ASSERT(0 != bb);
1415 FLAC__ASSERT(0 != bb->buffer);
1417 FLAC__ASSERT(bits <= 32);
1418 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1425 while(bb->total_consumed_bits + bits > bb->total_bits) {
1426 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1429 #if FLAC__BITS_PER_BLURB > 8
1430 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1432 if(bb->consumed_bits) {
1433 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1435 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1437 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1438 bb->consumed_blurbs++;
1439 bb->consumed_bits = 0;
1440 /* we hold off updating bb->total_consumed_bits until the end */
1443 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1444 bb->consumed_bits += bits_;
1445 bb->total_consumed_bits += bits_;
1449 #if FLAC__BITS_PER_BLURB == 32
1450 /* note that we know bits_ cannot be > 32 because of previous assertions */
1451 if(bits_ == FLAC__BITS_PER_BLURB) {
1452 v = bb->buffer[bb->consumed_blurbs];
1453 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1454 bb->consumed_blurbs++;
1455 /* bb->consumed_bits is already 0 */
1456 bb->total_consumed_bits += bits;
1461 while(bits_ >= FLAC__BITS_PER_BLURB) {
1462 v <<= FLAC__BITS_PER_BLURB;
1463 v |= bb->buffer[bb->consumed_blurbs];
1464 bits_ -= FLAC__BITS_PER_BLURB;
1465 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1466 bb->consumed_blurbs++;
1467 /* bb->consumed_bits is already 0 */
1468 /* we hold off updating bb->total_consumed_bits until the end */
1473 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1474 bb->consumed_bits = bits_;
1475 /* we hold off updating bb->total_consumed_bits until the end */
1477 bb->total_consumed_bits += bits;
1479 #if FLAC__BITS_PER_BLURB > 8
1483 for(i = 0; i < bits; i++) {
1484 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1493 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)
1494 #ifdef FLAC__NO_MANUAL_INLINING
1499 FLAC__ASSERT(0 != bb);
1500 FLAC__ASSERT(0 != bb->buffer);
1502 FLAC__ASSERT(bits <= 32);
1510 for(i = 0; i < bits; i++) {
1511 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1519 *val = (FLAC__int32)v;
1523 *val = (FLAC__int32)v;
1529 unsigned i, bits_ = bits;
1532 FLAC__ASSERT(0 != bb);
1533 FLAC__ASSERT(0 != bb->buffer);
1535 FLAC__ASSERT(bits <= 32);
1536 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1543 while(bb->total_consumed_bits + bits > bb->total_bits) {
1544 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1547 #if FLAC__BITS_PER_BLURB > 8
1548 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1550 if(bb->consumed_bits) {
1551 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1553 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1555 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1556 bb->consumed_blurbs++;
1557 bb->consumed_bits = 0;
1558 /* we hold off updating bb->total_consumed_bits until the end */
1561 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1562 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1564 *val = (FLAC__int32)v;
1565 *val >>= (32-bits_);
1566 bb->consumed_bits += bits_;
1567 bb->total_consumed_bits += bits_;
1571 #if FLAC__BITS_PER_BLURB == 32
1572 /* note that we know bits_ cannot be > 32 because of previous assertions */
1573 if(bits_ == FLAC__BITS_PER_BLURB) {
1574 v = bb->buffer[bb->consumed_blurbs];
1576 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1577 bb->consumed_blurbs++;
1578 /* bb->consumed_bits is already 0 */
1579 /* we hold off updating bb->total_consumed_bits until the end */
1582 while(bits_ >= FLAC__BITS_PER_BLURB) {
1583 v <<= FLAC__BITS_PER_BLURB;
1584 v |= bb->buffer[bb->consumed_blurbs];
1585 bits_ -= FLAC__BITS_PER_BLURB;
1586 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1587 bb->consumed_blurbs++;
1588 /* bb->consumed_bits is already 0 */
1589 /* we hold off updating bb->total_consumed_bits until the end */
1594 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1595 bb->consumed_bits = bits_;
1596 /* we hold off updating bb->total_consumed_bits until the end */
1598 bb->total_consumed_bits += bits;
1599 #if FLAC__BITS_PER_BLURB > 8
1602 for(i = 0; i < bits; i++) {
1603 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1613 *val = (FLAC__int32)v;
1617 *val = (FLAC__int32)v;
1623 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)
1624 #ifdef FLAC__NO_MANUAL_INLINING
1628 FLAC__ASSERT(0 != bb);
1629 FLAC__ASSERT(0 != bb->buffer);
1631 FLAC__ASSERT(bits <= 64);
1634 for(i = 0; i < bits; i++) {
1635 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1642 unsigned i, bits_ = bits;
1645 FLAC__ASSERT(0 != bb);
1646 FLAC__ASSERT(0 != bb->buffer);
1648 FLAC__ASSERT(bits <= 64);
1649 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1656 while(bb->total_consumed_bits + bits > bb->total_bits) {
1657 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1660 #if FLAC__BITS_PER_BLURB > 8
1661 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1663 if(bb->consumed_bits) {
1664 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1666 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1668 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1669 bb->consumed_blurbs++;
1670 bb->consumed_bits = 0;
1671 /* we hold off updating bb->total_consumed_bits until the end */
1674 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1675 bb->consumed_bits += bits_;
1676 bb->total_consumed_bits += bits_;
1680 while(bits_ >= FLAC__BITS_PER_BLURB) {
1681 v <<= FLAC__BITS_PER_BLURB;
1682 v |= bb->buffer[bb->consumed_blurbs];
1683 bits_ -= FLAC__BITS_PER_BLURB;
1684 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1685 bb->consumed_blurbs++;
1686 /* bb->consumed_bits is already 0 */
1687 /* we hold off updating bb->total_consumed_bits until the end */
1691 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1692 bb->consumed_bits = bits_;
1693 /* we hold off updating bb->total_consumed_bits until the end */
1695 bb->total_consumed_bits += bits;
1697 #if FLAC__BITS_PER_BLURB > 8
1701 for(i = 0; i < bits; i++) {
1702 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1711 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)
1712 #ifdef FLAC__NO_MANUAL_INLINING
1717 FLAC__ASSERT(0 != bb);
1718 FLAC__ASSERT(0 != bb->buffer);
1720 FLAC__ASSERT(bits <= 64);
1723 for(i = 0; i < bits; i++) {
1724 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1731 *val = (FLAC__int64)v;
1735 *val = (FLAC__int64)v;
1741 unsigned i, bits_ = bits;
1744 FLAC__ASSERT(0 != bb);
1745 FLAC__ASSERT(0 != bb->buffer);
1747 FLAC__ASSERT(bits <= 64);
1748 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1755 while(bb->total_consumed_bits + bits > bb->total_bits) {
1756 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1759 #if FLAC__BITS_PER_BLURB > 8
1760 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1762 if(bb->consumed_bits) {
1763 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1765 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1767 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1768 bb->consumed_blurbs++;
1769 bb->consumed_bits = 0;
1770 /* we hold off updating bb->total_consumed_bits until the end */
1773 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1774 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1776 *val = (FLAC__int64)v;
1777 *val >>= (64-bits_);
1778 bb->consumed_bits += bits_;
1779 bb->total_consumed_bits += bits_;
1783 while(bits_ >= FLAC__BITS_PER_BLURB) {
1784 v <<= FLAC__BITS_PER_BLURB;
1785 v |= bb->buffer[bb->consumed_blurbs];
1786 bits_ -= FLAC__BITS_PER_BLURB;
1787 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1788 bb->consumed_blurbs++;
1789 /* bb->consumed_bits is already 0 */
1790 /* we hold off updating bb->total_consumed_bits until the end */
1794 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1795 bb->consumed_bits = bits_;
1796 /* we hold off updating bb->total_consumed_bits until the end */
1798 bb->total_consumed_bits += bits;
1799 #if FLAC__BITS_PER_BLURB > 8
1802 for(i = 0; i < bits; i++) {
1803 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1813 *val = (FLAC__int64)v;
1817 *val = (FLAC__int64)v;
1823 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)
1825 FLAC__uint32 x8, x32 = 0;
1827 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1829 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1832 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1836 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1840 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1848 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)
1850 FLAC__ASSERT(0 != bb);
1851 FLAC__ASSERT(0 != bb->buffer);
1852 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1853 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1854 #if FLAC__BITS_PER_BLURB == 8
1856 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1858 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1863 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1864 val += FLAC__BYTES_PER_BLURB * chunk;
1867 bb->consumed_blurbs += chunk;
1868 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1872 @@@ need to write this still
1879 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)
1880 #ifdef FLAC__NO_MANUAL_INLINING
1882 unsigned bit, val_ = 0;
1884 FLAC__ASSERT(0 != bb);
1885 FLAC__ASSERT(0 != bb->buffer);
1888 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1900 unsigned i, val_ = 0;
1901 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1904 FLAC__ASSERT(0 != bb);
1905 FLAC__ASSERT(0 != bb->buffer);
1907 #if FLAC__BITS_PER_BLURB > 8
1908 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1910 if(bb->consumed_bits) {
1911 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1913 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1917 bb->consumed_bits += i;
1918 bb->total_consumed_bits += i;
1919 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1920 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1921 bb->consumed_blurbs++;
1922 bb->consumed_bits = 0;
1927 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1928 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1929 bb->consumed_blurbs++;
1930 bb->consumed_bits = 0;
1931 bb->total_consumed_bits += val_;
1935 if(bb->consumed_blurbs >= total_blurbs_) {
1936 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1938 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1940 b = bb->buffer[bb->consumed_blurbs];
1942 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1946 bb->consumed_bits = i;
1948 if(i == FLAC__BITS_PER_BLURB) {
1949 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1950 bb->consumed_blurbs++;
1951 bb->consumed_bits = 0;
1953 bb->total_consumed_bits += i;
1957 val_ += FLAC__BITS_PER_BLURB;
1958 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1959 bb->consumed_blurbs++;
1960 /* bb->consumed_bits is already 0 */
1961 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
1964 #if FLAC__BITS_PER_BLURB > 8
1968 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
1982 #ifdef FLAC__SYMMETRIC_RICE
1983 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)
1985 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1987 FLAC__ASSERT(0 != bb);
1988 FLAC__ASSERT(0 != bb->buffer);
1989 FLAC__ASSERT(parameter <= 31);
1991 /* read the unary MSBs and end bit */
1992 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1995 /* read the sign bit */
1996 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1999 /* read the binary LSBs */
2000 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2003 /* compose the value */
2004 *val = (msbs << parameter) | lsbs;
2010 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2012 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)
2014 FLAC__uint32 lsbs = 0, msbs = 0;
2017 FLAC__ASSERT(0 != bb);
2018 FLAC__ASSERT(0 != bb->buffer);
2019 FLAC__ASSERT(parameter <= 31);
2021 /* read the unary MSBs and end bit */
2022 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2025 /* read the binary LSBs */
2026 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2029 /* compose the value */
2030 uval = (msbs << parameter) | lsbs;
2032 *val = -((int)(uval >> 1)) - 1;
2034 *val = (int)(uval >> 1);
2039 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)
2041 const FLAC__blurb *buffer = bb->buffer;
2043 unsigned i, j, val_i = 0;
2044 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2045 FLAC__blurb blurb, save_blurb;
2046 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2048 FLAC__ASSERT(0 != bb);
2049 FLAC__ASSERT(0 != bb->buffer);
2050 FLAC__ASSERT(parameter <= 31);
2055 i = bb->consumed_blurbs;
2057 * We unroll the main loop to take care of partially consumed blurbs here.
2059 if(bb->consumed_bits > 0) {
2060 save_blurb = blurb = buffer[i];
2061 cbits = bb->consumed_bits;
2067 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2071 /* dispose of the unary end bit */
2077 lsbs_left = parameter;
2079 if(cbits == FLAC__BITS_PER_BLURB) {
2081 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2086 msbs += FLAC__BITS_PER_BLURB - cbits;
2088 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2093 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2094 if(lsbs_left >= available_bits) {
2095 uval <<= available_bits;
2096 uval |= (blurb >> cbits);
2098 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2100 if(lsbs_left == available_bits) {
2101 /* compose the value */
2102 uval |= (msbs << parameter);
2104 vals[val_i++] = -((int)(uval >> 1)) - 1;
2106 vals[val_i++] = (int)(uval >> 1);
2114 lsbs_left -= available_bits;
2119 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2120 blurb <<= lsbs_left;
2123 /* compose the value */
2124 uval |= (msbs << parameter);
2126 vals[val_i++] = -((int)(uval >> 1)) - 1;
2128 vals[val_i++] = (int)(uval >> 1);
2129 if(val_i == nvals) {
2130 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2142 bb->consumed_blurbs = i;
2143 bb->consumed_bits = cbits;
2144 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2148 * Now that we are blurb-aligned the logic is slightly simpler
2150 while(val_i < nvals) {
2151 for( ; i < bb->blurbs && val_i < nvals; i++) {
2152 save_blurb = blurb = buffer[i];
2157 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2161 /* dispose of the unary end bit */
2167 lsbs_left = parameter;
2169 if(cbits == FLAC__BITS_PER_BLURB) {
2171 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2176 msbs += FLAC__BITS_PER_BLURB - cbits;
2178 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2183 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2184 if(lsbs_left >= available_bits) {
2185 uval <<= available_bits;
2186 uval |= (blurb >> cbits);
2188 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2190 if(lsbs_left == available_bits) {
2191 /* compose the value */
2192 uval |= (msbs << parameter);
2194 vals[val_i++] = -((int)(uval >> 1)) - 1;
2196 vals[val_i++] = (int)(uval >> 1);
2204 lsbs_left -= available_bits;
2209 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2210 blurb <<= lsbs_left;
2213 /* compose the value */
2214 uval |= (msbs << parameter);
2216 vals[val_i++] = -((int)(uval >> 1)) - 1;
2218 vals[val_i++] = (int)(uval >> 1);
2219 if(val_i == nvals) {
2220 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2231 bb->consumed_blurbs = i;
2232 bb->consumed_bits = cbits;
2233 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2235 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2237 /* these must be zero because we can only get here if we got to the end of the buffer */
2238 FLAC__ASSERT(bb->consumed_blurbs == 0);
2239 FLAC__ASSERT(bb->consumed_bits == 0);
2248 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)
2250 FLAC__uint32 lsbs = 0, msbs = 0;
2251 unsigned bit, uval, k;
2253 FLAC__ASSERT(0 != bb);
2254 FLAC__ASSERT(0 != bb->buffer);
2256 k = FLAC__bitmath_ilog2(parameter);
2258 /* read the unary MSBs and end bit */
2259 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2262 /* read the binary LSBs */
2263 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2266 if(parameter == 1u<<k) {
2267 /* compose the value */
2268 uval = (msbs << k) | lsbs;
2271 unsigned d = (1 << (k+1)) - parameter;
2273 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2279 /* compose the value */
2280 uval = msbs * parameter + lsbs;
2283 /* unfold unsigned to signed */
2285 *val = -((int)(uval >> 1)) - 1;
2287 *val = (int)(uval >> 1);
2292 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)
2294 FLAC__uint32 lsbs, msbs = 0;
2297 FLAC__ASSERT(0 != bb);
2298 FLAC__ASSERT(0 != bb->buffer);
2300 k = FLAC__bitmath_ilog2(parameter);
2302 /* read the unary MSBs and end bit */
2303 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2306 /* read the binary LSBs */
2307 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2310 if(parameter == 1u<<k) {
2311 /* compose the value */
2312 *val = (msbs << k) | lsbs;
2315 unsigned d = (1 << (k+1)) - parameter;
2317 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2323 /* compose the value */
2324 *val = msbs * parameter + lsbs;
2331 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2332 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)
2338 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2341 raw[(*rawlen)++] = (FLAC__byte)x;
2342 if(!(x & 0x80)) { /* 0xxxxxxx */
2346 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2350 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2354 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2358 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2362 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2371 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2374 raw[(*rawlen)++] = (FLAC__byte)x;
2375 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2386 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2387 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)
2393 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2396 raw[(*rawlen)++] = (FLAC__byte)x;
2397 if(!(x & 0x80)) { /* 0xxxxxxx */
2401 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2405 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2409 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2413 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2417 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2421 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2426 *val = 0xffffffffffffffff;
2430 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2433 raw[(*rawlen)++] = (FLAC__byte)x;
2434 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2435 *val = 0xffffffffffffffff;
2445 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2449 fprintf(out, "bitbuffer is NULL\n");
2452 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);
2454 for(i = 0; i < bb->blurbs; i++) {
2455 fprintf(out, "%08X: ", i);
2456 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2457 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2460 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2464 fprintf(out, "%08X: ", i);
2465 for(j = 0; j < bb->bits; j++)
2466 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2469 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);