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));
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*)malloc(sizeof(FLAC__blurb) * bb->capacity);
362 memset(bb->buffer, 0, bb->capacity);
365 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
367 bb->blurbs = bb->bits = bb->total_bits = 0;
368 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
372 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
374 FLAC__ASSERT(0 != dest);
375 FLAC__ASSERT(0 != dest->buffer);
376 FLAC__ASSERT(0 != src);
377 FLAC__ASSERT(0 != src->buffer);
379 if(dest->capacity < src->capacity)
380 if(!bitbuffer_resize_(dest, src->capacity))
382 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
383 dest->blurbs = src->blurbs;
384 dest->bits = src->bits;
385 dest->total_bits = src->total_bits;
386 dest->consumed_blurbs = src->consumed_blurbs;
387 dest->consumed_bits = src->consumed_bits;
388 dest->total_consumed_bits = src->total_consumed_bits;
389 dest->read_crc16 = src->read_crc16;
393 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
395 FLAC__ASSERT(0 != bb);
396 FLAC__ASSERT(0 != bb->buffer);
397 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
399 bb->read_crc16 = seed;
400 #if FLAC__BITS_PER_BLURB == 8
401 /* no need to do anything */
402 #elif FLAC__BITS_PER_BLURB == 32
403 bb->crc16_align = bb->consumed_bits;
405 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
409 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
411 FLAC__ASSERT(0 != bb);
412 FLAC__ASSERT(0 != bb->buffer);
413 FLAC__ASSERT((bb->bits & 7) == 0);
414 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
416 #if FLAC__BITS_PER_BLURB == 8
417 /* no need to do anything */
418 #elif FLAC__BITS_PER_BLURB == 32
419 /*@@@ 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 */
420 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
421 if(bb->consumed_bits == 8) {
422 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
423 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
425 else if(bb->consumed_bits == 16) {
426 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
427 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
428 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
430 else if(bb->consumed_bits == 24) {
431 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
432 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
433 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
434 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
438 if(bb->consumed_bits == 8) {
439 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
440 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
442 else if(bb->consumed_bits == 16) {
443 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
444 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
445 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
447 else if(bb->consumed_bits == 24) {
448 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
449 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
450 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
451 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
454 bb->crc16_align = bb->consumed_bits;
456 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
458 return bb->read_crc16;
461 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
463 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
465 #if FLAC__BITS_PER_BLURB == 8
466 return FLAC__crc16(bb->buffer, bb->blurbs);
467 #elif FLAC__BITS_PER_BLURB == 32
468 /* @@@ WATCHOUT: code currently only works for big-endian: */
469 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
471 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
475 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
477 FLAC__ASSERT(0 != bb);
478 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
479 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
480 #if FLAC__BITS_PER_BLURB == 8
481 return FLAC__crc8(bb->buffer, bb->blurbs);
482 #elif FLAC__BITS_PER_BLURB == 32
483 /* @@@ WATCHOUT: code currently only works for big-endian: */
484 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
486 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
490 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
492 return ((bb->bits & 7) == 0);
495 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
497 return ((bb->consumed_bits & 7) == 0);
500 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
502 return 8 - (bb->consumed_bits & 7);
505 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
507 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
508 return (bb->total_bits - bb->total_consumed_bits) >> 3;
511 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
513 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
514 #if FLAC__BITS_PER_BLURB == 8
515 *buffer = bb->buffer + bb->consumed_blurbs;
516 *bytes = bb->blurbs - bb->consumed_blurbs;
517 #elif FLAC__BITS_PER_BLURB == 32
518 /* @@@ WATCHOUT: code currently only works for big-endian: */
519 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
520 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
522 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
526 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
528 #if FLAC__BITS_PER_BLURB == 8
530 #elif FLAC__BITS_PER_BLURB == 32
531 /* @@@ WATCHOUT: code currently only works for big-endian: */
534 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
538 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
542 FLAC__ASSERT(0 != bb);
543 FLAC__ASSERT(0 != bb->buffer);
547 if(!bitbuffer_ensure_size_(bb, bits))
549 bb->total_bits += bits;
551 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
552 bb->buffer[bb->blurbs] <<= n;
555 if(bb->bits == FLAC__BITS_PER_BLURB) {
563 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
567 FLAC__ASSERT(0 != bb);
568 FLAC__ASSERT(0 != bb->buffer);
570 FLAC__ASSERT(bits <= 32);
573 /* inline the size check so we don't incure a function call unnecessarily */
574 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
575 if(!bitbuffer_ensure_size_(bb, bits))
579 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
580 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
581 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
583 bb->total_bits += bits;
585 n = FLAC__BITS_PER_BLURB - bb->bits;
586 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
587 if(bits < FLAC__BITS_PER_BLURB) {
588 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
592 else if(bits == FLAC__BITS_PER_BLURB) {
593 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
597 k = bits - FLAC__BITS_PER_BLURB;
598 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
599 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
600 val &= (~(0xffffffff << k));
601 bits -= FLAC__BITS_PER_BLURB;
605 bb->buffer[bb->blurbs] <<= bits;
606 bb->buffer[bb->blurbs] |= val;
617 bb->buffer[bb->blurbs] <<= n;
618 bb->buffer[bb->blurbs] |= (val >> k);
619 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
620 val &= (~(0xffffffff << k));
630 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
632 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
635 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
637 static const FLAC__uint64 mask[] = {
639 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
640 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
641 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
642 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
643 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
644 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
645 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
646 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
647 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
648 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
649 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
650 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
651 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
652 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
653 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
654 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
658 FLAC__ASSERT(0 != bb);
659 FLAC__ASSERT(0 != bb->buffer);
661 FLAC__ASSERT(bits <= 64);
664 if(!bitbuffer_ensure_size_(bb, bits))
667 bb->total_bits += bits;
670 if(bits < FLAC__BITS_PER_BLURB) {
671 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
675 else if(bits == FLAC__BITS_PER_BLURB) {
676 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
680 k = bits - FLAC__BITS_PER_BLURB;
681 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
682 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
683 val &= (~(0xffffffffffffffff << k));
684 bits -= FLAC__BITS_PER_BLURB;
688 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
690 bb->buffer[bb->blurbs] <<= n;
691 bb->buffer[bb->blurbs] |= (val >> k);
692 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
693 val &= (~(0xffffffffffffffff << k));
696 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);
711 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
713 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
715 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
716 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
718 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
720 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
722 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
728 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
732 /* this could be faster but currently we don't need it to be */
733 for(i = 0; i < nvals; i++) {
734 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
741 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
744 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
746 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
748 if(!FLAC__bitbuffer_write_zeroes(bb, val))
750 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
754 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
758 /* fold signed to unsigned */
761 * (unsigned)(((--val) << 1) - 1);
762 * but without the overflow problem at MININT
764 uval = (unsigned)(((-(++val)) << 1) + 1);
766 uval = (unsigned)(val << 1);
768 msbs = uval >> parameter;
770 return 1 + parameter + msbs;
774 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
776 unsigned bits, msbs, uval;
779 FLAC__ASSERT(parameter > 0);
781 /* fold signed to unsigned */
784 * (unsigned)(((--val) << 1) - 1);
785 * but without the overflow problem at MININT
787 uval = (unsigned)(((-(++val)) << 1) + 1);
789 uval = (unsigned)(val << 1);
791 k = FLAC__bitmath_ilog2(parameter);
792 if(parameter == 1u<<k) {
793 FLAC__ASSERT(k <= 30);
801 d = (1 << (k+1)) - parameter;
802 q = uval / parameter;
803 r = uval - (q * parameter);
812 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
817 FLAC__ASSERT(parameter > 0);
819 k = FLAC__bitmath_ilog2(parameter);
820 if(parameter == 1u<<k) {
821 FLAC__ASSERT(k <= 30);
829 d = (1 << (k+1)) - parameter;
830 q = uval / parameter;
831 r = uval - (q * parameter);
841 #ifdef FLAC__SYMMETRIC_RICE
842 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
844 unsigned total_bits, interesting_bits, msbs;
845 FLAC__uint32 pattern;
847 FLAC__ASSERT(0 != bb);
848 FLAC__ASSERT(0 != bb->buffer);
849 FLAC__ASSERT(parameter <= 31);
851 /* init pattern with the unary end bit and the sign bit */
859 msbs = val >> parameter;
860 interesting_bits = 2 + parameter;
861 total_bits = interesting_bits + msbs;
862 pattern <<= parameter;
863 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
865 if(total_bits <= 32) {
866 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
870 /* write the unary MSBs */
871 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
873 /* write the unary end bit, the sign bit, and binary LSBs */
874 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
881 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
883 unsigned total_bits, interesting_bits, msbs;
884 FLAC__uint32 pattern;
886 FLAC__ASSERT(0 != bb);
887 FLAC__ASSERT(0 != bb->buffer);
888 FLAC__ASSERT(parameter <= 31);
892 /* init pattern with the unary end bit and the sign bit */
900 msbs = val >> parameter;
901 interesting_bits = 2 + parameter;
902 total_bits = interesting_bits + msbs;
903 pattern <<= parameter;
904 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
906 if(total_bits <= 32) {
907 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
910 else if(total_bits > max_bits) {
915 /* write the unary MSBs */
916 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
918 /* write the unary end bit, the sign bit, and binary LSBs */
919 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
926 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
928 unsigned total_bits, val_bits;
929 FLAC__uint32 pattern;
931 FLAC__ASSERT(0 != bb);
932 FLAC__ASSERT(0 != bb->buffer);
933 FLAC__ASSERT(parameter <= 31);
935 val_bits = FLAC__bitmath_silog2(val);
936 total_bits = 2 + parameter + 5 + val_bits;
938 if(total_bits <= 32) {
940 pattern <<= (parameter + 5);
942 pattern <<= val_bits;
943 pattern |= (val & ((1 << val_bits) - 1));
944 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
948 /* write the '-0' escape code first */
949 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
951 /* write the length */
952 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
954 /* write the value */
955 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
960 #endif /* ifdef FLAC__SYMMETRIC_RICE */
962 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
964 unsigned total_bits, interesting_bits, msbs, uval;
965 FLAC__uint32 pattern;
967 FLAC__ASSERT(0 != bb);
968 FLAC__ASSERT(0 != bb->buffer);
969 FLAC__ASSERT(parameter <= 30);
971 /* fold signed to unsigned */
974 * (unsigned)(((--val) << 1) - 1);
975 * but without the overflow problem at MININT
977 uval = (unsigned)(((-(++val)) << 1) + 1);
979 uval = (unsigned)(val << 1);
981 msbs = uval >> parameter;
982 interesting_bits = 1 + parameter;
983 total_bits = interesting_bits + msbs;
984 pattern = 1 << parameter; /* the unary end bit */
985 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
987 if(total_bits <= 32) {
988 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
992 /* write the unary MSBs */
993 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
995 /* write the unary end bit and binary LSBs */
996 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1003 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1005 unsigned total_bits, interesting_bits, msbs, uval;
1006 FLAC__uint32 pattern;
1008 FLAC__ASSERT(0 != bb);
1009 FLAC__ASSERT(0 != bb->buffer);
1010 FLAC__ASSERT(parameter <= 30);
1014 /* fold signed to unsigned */
1017 * (unsigned)(((--val) << 1) - 1);
1018 * but without the overflow problem at MININT
1020 uval = (unsigned)(((-(++val)) << 1) + 1);
1022 uval = (unsigned)(val << 1);
1024 msbs = uval >> parameter;
1025 interesting_bits = 1 + parameter;
1026 total_bits = interesting_bits + msbs;
1027 pattern = 1 << parameter; /* the unary end bit */
1028 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1030 if(total_bits <= 32) {
1031 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1034 else if(total_bits > max_bits) {
1039 /* write the unary MSBs */
1040 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1042 /* write the unary end bit and binary LSBs */
1043 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1051 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1053 unsigned total_bits, msbs, uval;
1056 FLAC__ASSERT(0 != bb);
1057 FLAC__ASSERT(0 != bb->buffer);
1058 FLAC__ASSERT(parameter > 0);
1060 /* fold signed to unsigned */
1063 * (unsigned)(((--val) << 1) - 1);
1064 * but without the overflow problem at MININT
1066 uval = (unsigned)(((-(++val)) << 1) + 1);
1068 uval = (unsigned)(val << 1);
1070 k = FLAC__bitmath_ilog2(parameter);
1071 if(parameter == 1u<<k) {
1074 FLAC__ASSERT(k <= 30);
1077 total_bits = 1 + k + msbs;
1078 pattern = 1 << k; /* the unary end bit */
1079 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1081 if(total_bits <= 32) {
1082 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1086 /* write the unary MSBs */
1087 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1089 /* write the unary end bit and binary LSBs */
1090 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1097 d = (1 << (k+1)) - parameter;
1098 q = uval / parameter;
1099 r = uval - (q * parameter);
1100 /* write the unary MSBs */
1101 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1103 /* write the unary end bit */
1104 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1106 /* write the binary LSBs */
1108 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1112 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1119 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1121 unsigned total_bits, msbs;
1124 FLAC__ASSERT(0 != bb);
1125 FLAC__ASSERT(0 != bb->buffer);
1126 FLAC__ASSERT(parameter > 0);
1128 k = FLAC__bitmath_ilog2(parameter);
1129 if(parameter == 1u<<k) {
1132 FLAC__ASSERT(k <= 30);
1135 total_bits = 1 + k + msbs;
1136 pattern = 1 << k; /* the unary end bit */
1137 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1139 if(total_bits <= 32) {
1140 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1144 /* write the unary MSBs */
1145 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1147 /* write the unary end bit and binary LSBs */
1148 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1155 d = (1 << (k+1)) - parameter;
1156 q = uval / parameter;
1157 r = uval - (q * parameter);
1158 /* write the unary MSBs */
1159 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1161 /* write the unary end bit */
1162 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1164 /* write the binary LSBs */
1166 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1170 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1178 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1182 FLAC__ASSERT(0 != bb);
1183 FLAC__ASSERT(0 != bb->buffer);
1185 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1188 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1190 else if(val < 0x800) {
1191 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1192 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1194 else if(val < 0x10000) {
1195 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1196 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1197 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1199 else if(val < 0x200000) {
1200 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1201 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1202 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1203 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1205 else if(val < 0x4000000) {
1206 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1207 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1208 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1209 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1210 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1213 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1214 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1215 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1216 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1217 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1218 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1224 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1228 FLAC__ASSERT(0 != bb);
1229 FLAC__ASSERT(0 != bb->buffer);
1231 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1234 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1236 else if(val < 0x800) {
1237 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1238 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1240 else if(val < 0x10000) {
1241 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1242 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1243 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1245 else if(val < 0x200000) {
1246 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1247 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1248 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1249 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1251 else if(val < 0x4000000) {
1252 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1253 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1254 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1255 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1256 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1258 else if(val < 0x80000000) {
1259 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1263 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1264 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1267 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1268 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1269 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1270 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1271 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1273 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1279 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1281 /* 0-pad to byte boundary */
1283 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1288 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)
1290 /* to avoid a drastic speed penalty we don't:
1291 FLAC__ASSERT(0 != bb);
1292 FLAC__ASSERT(0 != bb->buffer);
1293 FLAC__ASSERT(bb->bits == 0);
1297 if(bb->total_consumed_bits < bb->total_bits) {
1298 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1302 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1308 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)
1310 /* to avoid a drastic speed penalty we don't:
1311 FLAC__ASSERT(0 != bb);
1312 FLAC__ASSERT(0 != bb->buffer);
1313 FLAC__ASSERT(bb->bits == 0);
1317 if(bb->total_consumed_bits < bb->total_bits) {
1318 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1319 bb->consumed_bits++;
1320 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1321 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1322 bb->consumed_blurbs++;
1323 bb->consumed_bits = 0;
1325 bb->total_consumed_bits++;
1329 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1335 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)
1337 /* to avoid a drastic speed penalty we don't:
1338 FLAC__ASSERT(0 != bb);
1339 FLAC__ASSERT(0 != bb->buffer);
1340 FLAC__ASSERT(bb->bits == 0);
1344 if(bb->total_consumed_bits < bb->total_bits) {
1346 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1347 bb->consumed_bits++;
1348 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1349 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1350 bb->consumed_blurbs++;
1351 bb->consumed_bits = 0;
1353 bb->total_consumed_bits++;
1357 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1363 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)
1365 /* to avoid a drastic speed penalty we don't:
1366 FLAC__ASSERT(0 != bb);
1367 FLAC__ASSERT(0 != bb->buffer);
1368 FLAC__ASSERT(bb->bits == 0);
1372 if(bb->total_consumed_bits < bb->total_bits) {
1374 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1375 bb->consumed_bits++;
1376 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1377 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1378 bb->consumed_blurbs++;
1379 bb->consumed_bits = 0;
1381 bb->total_consumed_bits++;
1385 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1391 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)
1392 #ifdef FLAC__NO_MANUAL_INLINING
1396 FLAC__ASSERT(0 != bb);
1397 FLAC__ASSERT(0 != bb->buffer);
1399 FLAC__ASSERT(bits <= 32);
1402 for(i = 0; i < bits; i++) {
1403 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1410 unsigned i, bits_ = bits;
1413 FLAC__ASSERT(0 != bb);
1414 FLAC__ASSERT(0 != bb->buffer);
1416 FLAC__ASSERT(bits <= 32);
1417 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1424 while(bb->total_consumed_bits + bits > bb->total_bits) {
1425 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1428 #if FLAC__BITS_PER_BLURB > 8
1429 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1431 if(bb->consumed_bits) {
1432 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1434 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1436 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1437 bb->consumed_blurbs++;
1438 bb->consumed_bits = 0;
1439 /* we hold off updating bb->total_consumed_bits until the end */
1442 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1443 bb->consumed_bits += bits_;
1444 bb->total_consumed_bits += bits_;
1448 #if FLAC__BITS_PER_BLURB == 32
1449 /* note that we know bits_ cannot be > 32 because of previous assertions */
1450 if(bits_ == FLAC__BITS_PER_BLURB) {
1451 v = bb->buffer[bb->consumed_blurbs];
1452 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1453 bb->consumed_blurbs++;
1454 /* bb->consumed_bits is already 0 */
1455 bb->total_consumed_bits += bits;
1460 while(bits_ >= FLAC__BITS_PER_BLURB) {
1461 v <<= FLAC__BITS_PER_BLURB;
1462 v |= bb->buffer[bb->consumed_blurbs];
1463 bits_ -= FLAC__BITS_PER_BLURB;
1464 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1465 bb->consumed_blurbs++;
1466 /* bb->consumed_bits is already 0 */
1467 /* we hold off updating bb->total_consumed_bits until the end */
1472 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1473 bb->consumed_bits = bits_;
1474 /* we hold off updating bb->total_consumed_bits until the end */
1476 bb->total_consumed_bits += bits;
1478 #if FLAC__BITS_PER_BLURB > 8
1482 for(i = 0; i < bits; i++) {
1483 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1492 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)
1493 #ifdef FLAC__NO_MANUAL_INLINING
1498 FLAC__ASSERT(0 != bb);
1499 FLAC__ASSERT(0 != bb->buffer);
1501 FLAC__ASSERT(bits <= 32);
1509 for(i = 0; i < bits; i++) {
1510 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1518 *val = (FLAC__int32)v;
1522 *val = (FLAC__int32)v;
1528 unsigned i, bits_ = bits;
1531 FLAC__ASSERT(0 != bb);
1532 FLAC__ASSERT(0 != bb->buffer);
1534 FLAC__ASSERT(bits <= 32);
1535 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1542 while(bb->total_consumed_bits + bits > bb->total_bits) {
1543 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1546 #if FLAC__BITS_PER_BLURB > 8
1547 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1549 if(bb->consumed_bits) {
1550 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1552 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1554 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1555 bb->consumed_blurbs++;
1556 bb->consumed_bits = 0;
1557 /* we hold off updating bb->total_consumed_bits until the end */
1560 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1561 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1563 *val = (FLAC__int32)v;
1564 *val >>= (32-bits_);
1565 bb->consumed_bits += bits_;
1566 bb->total_consumed_bits += bits_;
1570 #if FLAC__BITS_PER_BLURB == 32
1571 /* note that we know bits_ cannot be > 32 because of previous assertions */
1572 if(bits_ == FLAC__BITS_PER_BLURB) {
1573 v = bb->buffer[bb->consumed_blurbs];
1575 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1576 bb->consumed_blurbs++;
1577 /* bb->consumed_bits is already 0 */
1578 /* we hold off updating bb->total_consumed_bits until the end */
1581 while(bits_ >= FLAC__BITS_PER_BLURB) {
1582 v <<= FLAC__BITS_PER_BLURB;
1583 v |= bb->buffer[bb->consumed_blurbs];
1584 bits_ -= FLAC__BITS_PER_BLURB;
1585 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1586 bb->consumed_blurbs++;
1587 /* bb->consumed_bits is already 0 */
1588 /* we hold off updating bb->total_consumed_bits until the end */
1593 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1594 bb->consumed_bits = bits_;
1595 /* we hold off updating bb->total_consumed_bits until the end */
1597 bb->total_consumed_bits += bits;
1598 #if FLAC__BITS_PER_BLURB > 8
1601 for(i = 0; i < bits; i++) {
1602 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1612 *val = (FLAC__int32)v;
1616 *val = (FLAC__int32)v;
1622 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)
1623 #ifdef FLAC__NO_MANUAL_INLINING
1627 FLAC__ASSERT(0 != bb);
1628 FLAC__ASSERT(0 != bb->buffer);
1630 FLAC__ASSERT(bits <= 64);
1633 for(i = 0; i < bits; i++) {
1634 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1641 unsigned i, bits_ = bits;
1644 FLAC__ASSERT(0 != bb);
1645 FLAC__ASSERT(0 != bb->buffer);
1647 FLAC__ASSERT(bits <= 64);
1648 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1655 while(bb->total_consumed_bits + bits > bb->total_bits) {
1656 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1659 #if FLAC__BITS_PER_BLURB > 8
1660 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1662 if(bb->consumed_bits) {
1663 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1665 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1667 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1668 bb->consumed_blurbs++;
1669 bb->consumed_bits = 0;
1670 /* we hold off updating bb->total_consumed_bits until the end */
1673 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1674 bb->consumed_bits += bits_;
1675 bb->total_consumed_bits += bits_;
1679 while(bits_ >= FLAC__BITS_PER_BLURB) {
1680 v <<= FLAC__BITS_PER_BLURB;
1681 v |= bb->buffer[bb->consumed_blurbs];
1682 bits_ -= FLAC__BITS_PER_BLURB;
1683 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1684 bb->consumed_blurbs++;
1685 /* bb->consumed_bits is already 0 */
1686 /* we hold off updating bb->total_consumed_bits until the end */
1690 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1691 bb->consumed_bits = bits_;
1692 /* we hold off updating bb->total_consumed_bits until the end */
1694 bb->total_consumed_bits += bits;
1696 #if FLAC__BITS_PER_BLURB > 8
1700 for(i = 0; i < bits; i++) {
1701 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1710 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)
1711 #ifdef FLAC__NO_MANUAL_INLINING
1716 FLAC__ASSERT(0 != bb);
1717 FLAC__ASSERT(0 != bb->buffer);
1719 FLAC__ASSERT(bits <= 64);
1722 for(i = 0; i < bits; i++) {
1723 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1730 *val = (FLAC__int64)v;
1734 *val = (FLAC__int64)v;
1740 unsigned i, bits_ = bits;
1743 FLAC__ASSERT(0 != bb);
1744 FLAC__ASSERT(0 != bb->buffer);
1746 FLAC__ASSERT(bits <= 64);
1747 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1754 while(bb->total_consumed_bits + bits > bb->total_bits) {
1755 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1758 #if FLAC__BITS_PER_BLURB > 8
1759 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1761 if(bb->consumed_bits) {
1762 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1764 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1766 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1767 bb->consumed_blurbs++;
1768 bb->consumed_bits = 0;
1769 /* we hold off updating bb->total_consumed_bits until the end */
1772 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1773 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1775 *val = (FLAC__int64)v;
1776 *val >>= (64-bits_);
1777 bb->consumed_bits += bits_;
1778 bb->total_consumed_bits += bits_;
1782 while(bits_ >= FLAC__BITS_PER_BLURB) {
1783 v <<= FLAC__BITS_PER_BLURB;
1784 v |= bb->buffer[bb->consumed_blurbs];
1785 bits_ -= FLAC__BITS_PER_BLURB;
1786 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1787 bb->consumed_blurbs++;
1788 /* bb->consumed_bits is already 0 */
1789 /* we hold off updating bb->total_consumed_bits until the end */
1793 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1794 bb->consumed_bits = bits_;
1795 /* we hold off updating bb->total_consumed_bits until the end */
1797 bb->total_consumed_bits += bits;
1798 #if FLAC__BITS_PER_BLURB > 8
1801 for(i = 0; i < bits; i++) {
1802 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1812 *val = (FLAC__int64)v;
1816 *val = (FLAC__int64)v;
1822 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)
1824 FLAC__uint32 x8, x32 = 0;
1826 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1828 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1831 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1835 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1839 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1847 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)
1849 FLAC__ASSERT(0 != bb);
1850 FLAC__ASSERT(0 != bb->buffer);
1851 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1852 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1853 #if FLAC__BITS_PER_BLURB == 8
1855 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1857 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1862 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1863 val += FLAC__BYTES_PER_BLURB * chunk;
1866 bb->consumed_blurbs += chunk;
1867 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1871 @@@ need to write this still
1878 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)
1879 #ifdef FLAC__NO_MANUAL_INLINING
1881 unsigned bit, val_ = 0;
1883 FLAC__ASSERT(0 != bb);
1884 FLAC__ASSERT(0 != bb->buffer);
1887 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1899 unsigned i, val_ = 0;
1900 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1903 FLAC__ASSERT(0 != bb);
1904 FLAC__ASSERT(0 != bb->buffer);
1906 #if FLAC__BITS_PER_BLURB > 8
1907 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1909 if(bb->consumed_bits) {
1910 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1912 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1916 bb->consumed_bits += i;
1917 bb->total_consumed_bits += i;
1918 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1919 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1920 bb->consumed_blurbs++;
1921 bb->consumed_bits = 0;
1926 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1927 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1928 bb->consumed_blurbs++;
1929 bb->consumed_bits = 0;
1930 bb->total_consumed_bits += val_;
1934 if(bb->consumed_blurbs >= total_blurbs_) {
1935 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1937 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1939 b = bb->buffer[bb->consumed_blurbs];
1941 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1945 bb->consumed_bits = i;
1947 if(i == FLAC__BITS_PER_BLURB) {
1948 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1949 bb->consumed_blurbs++;
1950 bb->consumed_bits = 0;
1952 bb->total_consumed_bits += i;
1956 val_ += FLAC__BITS_PER_BLURB;
1957 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
1958 bb->consumed_blurbs++;
1959 /* bb->consumed_bits is already 0 */
1960 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
1963 #if FLAC__BITS_PER_BLURB > 8
1967 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
1981 #ifdef FLAC__SYMMETRIC_RICE
1982 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)
1984 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1986 FLAC__ASSERT(0 != bb);
1987 FLAC__ASSERT(0 != bb->buffer);
1988 FLAC__ASSERT(parameter <= 31);
1990 /* read the unary MSBs and end bit */
1991 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1994 /* read the sign bit */
1995 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1998 /* read the binary LSBs */
1999 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2002 /* compose the value */
2003 *val = (msbs << parameter) | lsbs;
2009 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2011 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)
2013 FLAC__uint32 lsbs = 0, msbs = 0;
2016 FLAC__ASSERT(0 != bb);
2017 FLAC__ASSERT(0 != bb->buffer);
2018 FLAC__ASSERT(parameter <= 31);
2020 /* read the unary MSBs and end bit */
2021 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2024 /* read the binary LSBs */
2025 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2028 /* compose the value */
2029 uval = (msbs << parameter) | lsbs;
2031 *val = -((int)(uval >> 1)) - 1;
2033 *val = (int)(uval >> 1);
2038 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)
2040 const FLAC__blurb *buffer = bb->buffer;
2042 unsigned i, j, val_i = 0;
2043 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2044 FLAC__blurb blurb, save_blurb;
2045 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2047 FLAC__ASSERT(0 != bb);
2048 FLAC__ASSERT(0 != bb->buffer);
2049 FLAC__ASSERT(parameter <= 31);
2054 i = bb->consumed_blurbs;
2056 * We unroll the main loop to take care of partially consumed blurbs here.
2058 if(bb->consumed_bits > 0) {
2059 save_blurb = blurb = buffer[i];
2060 cbits = bb->consumed_bits;
2066 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2070 /* dispose of the unary end bit */
2076 lsbs_left = parameter;
2078 if(cbits == FLAC__BITS_PER_BLURB) {
2080 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2085 msbs += FLAC__BITS_PER_BLURB - cbits;
2087 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2092 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2093 if(lsbs_left >= available_bits) {
2094 uval <<= available_bits;
2095 uval |= (blurb >> cbits);
2097 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2099 if(lsbs_left == available_bits) {
2100 /* compose the value */
2101 uval |= (msbs << parameter);
2103 vals[val_i++] = -((int)(uval >> 1)) - 1;
2105 vals[val_i++] = (int)(uval >> 1);
2113 lsbs_left -= available_bits;
2118 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2119 blurb <<= lsbs_left;
2122 /* compose the value */
2123 uval |= (msbs << parameter);
2125 vals[val_i++] = -((int)(uval >> 1)) - 1;
2127 vals[val_i++] = (int)(uval >> 1);
2128 if(val_i == nvals) {
2129 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2141 bb->consumed_blurbs = i;
2142 bb->consumed_bits = cbits;
2143 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2147 * Now that we are blurb-aligned the logic is slightly simpler
2149 while(val_i < nvals) {
2150 for( ; i < bb->blurbs && val_i < nvals; i++) {
2151 save_blurb = blurb = buffer[i];
2156 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2160 /* dispose of the unary end bit */
2166 lsbs_left = parameter;
2168 if(cbits == FLAC__BITS_PER_BLURB) {
2170 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2175 msbs += FLAC__BITS_PER_BLURB - cbits;
2177 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2182 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2183 if(lsbs_left >= available_bits) {
2184 uval <<= available_bits;
2185 uval |= (blurb >> cbits);
2187 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2189 if(lsbs_left == available_bits) {
2190 /* compose the value */
2191 uval |= (msbs << parameter);
2193 vals[val_i++] = -((int)(uval >> 1)) - 1;
2195 vals[val_i++] = (int)(uval >> 1);
2203 lsbs_left -= available_bits;
2208 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2209 blurb <<= lsbs_left;
2212 /* compose the value */
2213 uval |= (msbs << parameter);
2215 vals[val_i++] = -((int)(uval >> 1)) - 1;
2217 vals[val_i++] = (int)(uval >> 1);
2218 if(val_i == nvals) {
2219 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2230 bb->consumed_blurbs = i;
2231 bb->consumed_bits = cbits;
2232 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2234 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2236 /* these must be zero because we can only get here if we got to the end of the buffer */
2237 FLAC__ASSERT(bb->consumed_blurbs == 0);
2238 FLAC__ASSERT(bb->consumed_bits == 0);
2247 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)
2249 FLAC__uint32 lsbs = 0, msbs = 0;
2250 unsigned bit, uval, k;
2252 FLAC__ASSERT(0 != bb);
2253 FLAC__ASSERT(0 != bb->buffer);
2255 k = FLAC__bitmath_ilog2(parameter);
2257 /* read the unary MSBs and end bit */
2258 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2261 /* read the binary LSBs */
2262 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2265 if(parameter == 1u<<k) {
2266 /* compose the value */
2267 uval = (msbs << k) | lsbs;
2270 unsigned d = (1 << (k+1)) - parameter;
2272 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2278 /* compose the value */
2279 uval = msbs * parameter + lsbs;
2282 /* unfold unsigned to signed */
2284 *val = -((int)(uval >> 1)) - 1;
2286 *val = (int)(uval >> 1);
2291 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)
2293 FLAC__uint32 lsbs, msbs = 0;
2296 FLAC__ASSERT(0 != bb);
2297 FLAC__ASSERT(0 != bb->buffer);
2299 k = FLAC__bitmath_ilog2(parameter);
2301 /* read the unary MSBs and end bit */
2302 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2305 /* read the binary LSBs */
2306 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2309 if(parameter == 1u<<k) {
2310 /* compose the value */
2311 *val = (msbs << k) | lsbs;
2314 unsigned d = (1 << (k+1)) - parameter;
2316 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2322 /* compose the value */
2323 *val = msbs * parameter + lsbs;
2330 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2331 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)
2337 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2340 raw[(*rawlen)++] = (FLAC__byte)x;
2341 if(!(x & 0x80)) { /* 0xxxxxxx */
2345 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2349 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2353 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2357 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2361 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2370 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2373 raw[(*rawlen)++] = (FLAC__byte)x;
2374 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2385 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2386 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)
2392 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2395 raw[(*rawlen)++] = (FLAC__byte)x;
2396 if(!(x & 0x80)) { /* 0xxxxxxx */
2400 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2404 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2408 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2412 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2416 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2420 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2425 *val = 0xffffffffffffffff;
2429 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2432 raw[(*rawlen)++] = (FLAC__byte)x;
2433 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2434 *val = 0xffffffffffffffff;
2444 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2448 fprintf(out, "bitbuffer is NULL\n");
2451 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);
2453 for(i = 0; i < bb->blurbs; i++) {
2454 fprintf(out, "%08X: ", i);
2455 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2456 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2459 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2463 fprintf(out, "%08X: ", i);
2464 for(j = 0; j < bb->bits; j++)
2465 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2468 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);