1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <stdlib.h> /* for malloc() */
33 #include <string.h> /* for memcpy(), memset() */
34 #include "private/bitbuffer.h"
35 #include "private/bitmath.h"
36 #include "private/crc.h"
37 #include "FLAC/assert.h"
40 * Along the way you will see two versions of some functions, selected
41 * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
42 * readable, and slow version, and the other is the same function
43 * where crucial parts have been manually inlined and are much faster.
48 * This should be at least twice as large as the largest number of blurbs
49 * required to represent any 'number' (in any encoding) you are going to
50 * read. With FLAC this is on the order of maybe a few hundred bits.
51 * If the buffer is smaller than that, the decoder won't be able to read
52 * in a whole number that is in a variable length encoding (e.g. Rice).
54 * The number we are actually using here is based on what would be the
55 * approximate maximum size of a verbatim frame at the default block size,
56 * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
57 * 32kbytes sounds reasonable. For kicks we subtract out 64 bytes for any
58 * alignment or malloc overhead.
60 * Increase this number to decrease the number of read callbacks, at the
61 * expense of using more memory. Or decrease for the reverse effect,
62 * keeping in mind the limit from the first paragraph.
64 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
66 #if FLAC__BITS_PER_BLURB == 8
67 #define FLAC__BITS_PER_BLURB_LOG2 3
68 #define FLAC__BYTES_PER_BLURB 1
69 #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
70 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
71 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
72 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
73 #elif FLAC__BITS_PER_BLURB == 32
74 #define FLAC__BITS_PER_BLURB_LOG2 5
75 #define FLAC__BYTES_PER_BLURB 4
76 #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
77 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
78 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
79 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
81 /* ERROR, only sizes of 8 and 32 are supported */
84 #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
89 #define min(x,y) ((x)<(y)?(x):(y))
93 #define max(x,y) ((x)>(y)?(x):(y))
99 struct FLAC__BitBuffer {
101 unsigned capacity; /* in blurbs */
102 unsigned blurbs, bits;
103 unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
104 unsigned consumed_blurbs, consumed_bits;
105 unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
106 FLAC__uint16 read_crc16;
107 #if FLAC__BITS_PER_BLURB == 32
108 unsigned crc16_align;
110 FLAC__blurb save_head, save_tail;
113 #if FLAC__BITS_PER_BLURB == 32
114 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
116 if(bb->crc16_align == 0) {
117 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
118 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
119 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
120 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
122 else if(bb->crc16_align == 8) {
123 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
124 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
125 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
127 else if(bb->crc16_align == 16) {
128 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
129 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
131 else if(bb->crc16_align == 24) {
132 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
139 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
140 * does not shift left what is consumed, it just chops off the end, whether
141 * there is unconsumed data there or not. This is OK because currently we
142 * never shrink the buffer, but if this ever changes, we'll have to do some
145 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
147 FLAC__blurb *new_buffer;
149 FLAC__ASSERT(0 != bb);
150 FLAC__ASSERT(0 != bb->buffer);
152 if(bb->capacity == new_capacity)
155 new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
158 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
159 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
160 bb->blurbs = new_capacity;
162 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
164 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
165 bb->consumed_blurbs = new_capacity;
166 bb->consumed_bits = 0;
167 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
169 free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
170 bb->buffer = new_buffer;
171 bb->capacity = new_capacity;
175 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
177 unsigned new_capacity;
179 FLAC__ASSERT(min_blurbs_to_add > 0);
181 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
182 return bitbuffer_resize_(bb, new_capacity);
185 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
187 FLAC__ASSERT(0 != bb);
188 FLAC__ASSERT(0 != bb->buffer);
190 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
191 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
196 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)
201 /* first shift the unconsumed buffer data toward the front as much as possible */
202 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
203 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
204 for( ; r < r_end; l++, r++)
205 bb->buffer[l] = bb->buffer[r];
206 for( ; l < r_end; l++)
208 bb->blurbs -= bb->consumed_blurbs;
209 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
210 bb->consumed_blurbs = 0;
211 bb->total_consumed_bits = bb->consumed_bits;
214 /* grow if we need to */
215 if(bb->capacity <= 1) {
216 if(!bitbuffer_resize_(bb, 16))
220 /* set the target for reading, taking into account blurb alignment */
221 #if FLAC__BITS_PER_BLURB == 8
222 /* blurb == byte, so no gyrations necessary: */
223 target = bb->buffer + bb->blurbs;
224 bytes = bb->capacity - bb->blurbs;
225 #elif FLAC__BITS_PER_BLURB == 32
226 /* @@@ WATCHOUT: code currently only works for big-endian: */
227 FLAC__ASSERT((bb->bits & 7) == 0);
228 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
229 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
231 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
234 /* finally, read in some data */
235 if(!read_callback(target, &bytes, client_data))
238 /* now we have to handle partial blurb cases: */
239 #if FLAC__BITS_PER_BLURB == 8
240 /* blurb == byte, so no gyrations necessary: */
242 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
243 #elif FLAC__BITS_PER_BLURB == 32
244 /* @@@ WATCHOUT: code currently only works for big-endian: */
246 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
247 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
248 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
249 bb->total_bits += (bytes << 3);
252 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
257 /***********************************************************************
259 * Class constructor/destructor
261 ***********************************************************************/
263 FLAC__BitBuffer *FLAC__bitbuffer_new()
265 FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
268 memset(bb, 0, sizeof(FLAC__BitBuffer));
271 bb->blurbs = bb->bits = bb->total_bits = 0;
272 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
277 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
279 FLAC__ASSERT(0 != bb);
281 FLAC__bitbuffer_free(bb);
285 /***********************************************************************
287 * Public class methods
289 ***********************************************************************/
291 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
293 FLAC__ASSERT(0 != bb);
297 bb->blurbs = bb->bits = bb->total_bits = 0;
298 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
300 return FLAC__bitbuffer_clear(bb);
303 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
305 FLAC__ASSERT(0 != bb);
306 FLAC__ASSERT(bytes > 0);
308 if(!FLAC__bitbuffer_init(bb))
311 if(!bitbuffer_ensure_size_(bb, bytes << 3))
314 FLAC__ASSERT(0 != buffer);
315 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
316 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
317 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
318 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
319 bb->total_bits = bytes << 3;
323 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
325 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
327 FLAC__ASSERT(0 != dest);
328 FLAC__ASSERT(0 != src);
332 if(dest->bits != src->consumed_bits)
334 if(!bitbuffer_ensure_size_(dest, bits_to_add))
336 if(dest->bits == 0) {
337 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
339 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
340 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
341 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
342 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)));
345 dest->buffer[dest->blurbs] <<= bits_to_add;
346 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
348 dest->bits = src->bits;
349 dest->total_bits += bits_to_add;
350 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
355 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
357 FLAC__ASSERT(0 != bb);
363 bb->blurbs = bb->bits = bb->total_bits = 0;
364 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
367 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
369 if(bb->buffer == 0) {
370 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
371 bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
376 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
378 bb->blurbs = bb->bits = bb->total_bits = 0;
379 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
383 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
385 FLAC__ASSERT(0 != dest);
386 FLAC__ASSERT(0 != dest->buffer);
387 FLAC__ASSERT(0 != src);
388 FLAC__ASSERT(0 != src->buffer);
390 if(dest->capacity < src->capacity)
391 if(!bitbuffer_resize_(dest, src->capacity))
393 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
394 dest->blurbs = src->blurbs;
395 dest->bits = src->bits;
396 dest->total_bits = src->total_bits;
397 dest->consumed_blurbs = src->consumed_blurbs;
398 dest->consumed_bits = src->consumed_bits;
399 dest->total_consumed_bits = src->total_consumed_bits;
400 dest->read_crc16 = src->read_crc16;
404 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
406 FLAC__ASSERT(0 != bb);
407 FLAC__ASSERT(0 != bb->buffer);
408 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
410 bb->read_crc16 = seed;
411 #if FLAC__BITS_PER_BLURB == 8
412 /* no need to do anything */
413 #elif FLAC__BITS_PER_BLURB == 32
414 bb->crc16_align = bb->consumed_bits;
416 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
420 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
422 FLAC__ASSERT(0 != bb);
423 FLAC__ASSERT(0 != bb->buffer);
424 FLAC__ASSERT((bb->bits & 7) == 0);
425 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
427 #if FLAC__BITS_PER_BLURB == 8
428 /* no need to do anything */
429 #elif FLAC__BITS_PER_BLURB == 32
430 /*@@@ 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 */
431 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
432 if(bb->consumed_bits == 8) {
433 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
434 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
436 else if(bb->consumed_bits == 16) {
437 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
438 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
439 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
441 else if(bb->consumed_bits == 24) {
442 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
443 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
444 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
445 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
449 if(bb->consumed_bits == 8) {
450 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
451 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
453 else if(bb->consumed_bits == 16) {
454 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
455 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
456 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
458 else if(bb->consumed_bits == 24) {
459 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
460 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
461 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
462 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
465 bb->crc16_align = bb->consumed_bits;
467 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
469 return bb->read_crc16;
472 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
474 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
476 #if FLAC__BITS_PER_BLURB == 8
477 return FLAC__crc16(bb->buffer, bb->blurbs);
478 #elif FLAC__BITS_PER_BLURB == 32
479 /* @@@ WATCHOUT: code currently only works for big-endian: */
480 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
482 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
486 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
488 FLAC__ASSERT(0 != bb);
489 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
490 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
491 #if FLAC__BITS_PER_BLURB == 8
492 return FLAC__crc8(bb->buffer, bb->blurbs);
493 #elif FLAC__BITS_PER_BLURB == 32
494 /* @@@ WATCHOUT: code currently only works for big-endian: */
495 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
497 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
501 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
503 return ((bb->bits & 7) == 0);
506 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
508 return ((bb->consumed_bits & 7) == 0);
511 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
513 return 8 - (bb->consumed_bits & 7);
516 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
518 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
519 return (bb->total_bits - bb->total_consumed_bits) >> 3;
522 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
524 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
525 #if FLAC__BITS_PER_BLURB == 8
526 *buffer = bb->buffer + bb->consumed_blurbs;
527 *bytes = bb->blurbs - bb->consumed_blurbs;
528 #elif FLAC__BITS_PER_BLURB == 32
529 /* @@@ WATCHOUT: code currently only works for big-endian: */
530 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
531 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
533 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
537 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
539 #if FLAC__BITS_PER_BLURB == 8
541 #elif FLAC__BITS_PER_BLURB == 32
542 /* @@@ WATCHOUT: code currently only works for big-endian: */
545 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
549 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
553 FLAC__ASSERT(0 != bb);
554 FLAC__ASSERT(0 != bb->buffer);
558 if(!bitbuffer_ensure_size_(bb, bits))
560 bb->total_bits += bits;
562 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
563 bb->buffer[bb->blurbs] <<= n;
566 if(bb->bits == FLAC__BITS_PER_BLURB) {
574 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
578 FLAC__ASSERT(0 != bb);
579 FLAC__ASSERT(0 != bb->buffer);
581 FLAC__ASSERT(bits <= 32);
584 /* inline the size check so we don't incure a function call unnecessarily */
585 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
586 if(!bitbuffer_ensure_size_(bb, bits))
590 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
591 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
592 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
594 bb->total_bits += bits;
596 n = FLAC__BITS_PER_BLURB - bb->bits;
597 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
598 if(bits < FLAC__BITS_PER_BLURB) {
599 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
603 else if(bits == FLAC__BITS_PER_BLURB) {
604 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
608 k = bits - FLAC__BITS_PER_BLURB;
609 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
610 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
611 val &= (~(0xffffffff << k));
612 bits -= FLAC__BITS_PER_BLURB;
616 bb->buffer[bb->blurbs] <<= bits;
617 bb->buffer[bb->blurbs] |= val;
628 bb->buffer[bb->blurbs] <<= n;
629 bb->buffer[bb->blurbs] |= (val >> k);
630 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
631 val &= (~(0xffffffff << k));
641 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
643 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
646 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
648 static const FLAC__uint64 mask[] = {
650 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
651 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
652 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
653 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
654 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
655 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
656 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
657 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
658 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
659 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
660 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
661 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
662 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
663 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
664 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
665 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
669 FLAC__ASSERT(0 != bb);
670 FLAC__ASSERT(0 != bb->buffer);
672 FLAC__ASSERT(bits <= 64);
675 if(!bitbuffer_ensure_size_(bb, bits))
678 bb->total_bits += bits;
681 if(bits < FLAC__BITS_PER_BLURB) {
682 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
686 else if(bits == FLAC__BITS_PER_BLURB) {
687 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
691 k = bits - FLAC__BITS_PER_BLURB;
692 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
693 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
694 val &= (~(0xffffffffffffffff << k));
695 bits -= FLAC__BITS_PER_BLURB;
699 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
701 bb->buffer[bb->blurbs] <<= n;
702 bb->buffer[bb->blurbs] |= (val >> k);
703 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
704 val &= (~(0xffffffffffffffff << k));
707 if(bb->bits == FLAC__BITS_PER_BLURB) {
718 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
720 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
724 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
726 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
728 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
729 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
731 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
733 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
735 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
741 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
745 /* this could be faster but currently we don't need it to be */
746 for(i = 0; i < nvals; i++) {
747 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
754 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
757 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
759 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
761 if(!FLAC__bitbuffer_write_zeroes(bb, val))
763 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
767 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
771 /* fold signed to unsigned */
774 * (unsigned)(((--val) << 1) - 1);
775 * but without the overflow problem at MININT
777 uval = (unsigned)(((-(++val)) << 1) + 1);
779 uval = (unsigned)(val << 1);
781 msbs = uval >> parameter;
783 return 1 + parameter + msbs;
787 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
789 unsigned bits, msbs, uval;
792 FLAC__ASSERT(parameter > 0);
794 /* fold signed to unsigned */
797 * (unsigned)(((--val) << 1) - 1);
798 * but without the overflow problem at MININT
800 uval = (unsigned)(((-(++val)) << 1) + 1);
802 uval = (unsigned)(val << 1);
804 k = FLAC__bitmath_ilog2(parameter);
805 if(parameter == 1u<<k) {
806 FLAC__ASSERT(k <= 30);
814 d = (1 << (k+1)) - parameter;
815 q = uval / parameter;
816 r = uval - (q * parameter);
825 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
830 FLAC__ASSERT(parameter > 0);
832 k = FLAC__bitmath_ilog2(parameter);
833 if(parameter == 1u<<k) {
834 FLAC__ASSERT(k <= 30);
842 d = (1 << (k+1)) - parameter;
843 q = uval / parameter;
844 r = uval - (q * parameter);
854 #ifdef FLAC__SYMMETRIC_RICE
855 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
857 unsigned total_bits, interesting_bits, msbs;
858 FLAC__uint32 pattern;
860 FLAC__ASSERT(0 != bb);
861 FLAC__ASSERT(0 != bb->buffer);
862 FLAC__ASSERT(parameter <= 31);
864 /* init pattern with the unary end bit and the sign bit */
872 msbs = val >> parameter;
873 interesting_bits = 2 + parameter;
874 total_bits = interesting_bits + msbs;
875 pattern <<= parameter;
876 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
878 if(total_bits <= 32) {
879 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
883 /* write the unary MSBs */
884 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
886 /* write the unary end bit, the sign bit, and binary LSBs */
887 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
894 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
896 unsigned total_bits, interesting_bits, msbs;
897 FLAC__uint32 pattern;
899 FLAC__ASSERT(0 != bb);
900 FLAC__ASSERT(0 != bb->buffer);
901 FLAC__ASSERT(parameter <= 31);
905 /* init pattern with the unary end bit and the sign bit */
913 msbs = val >> parameter;
914 interesting_bits = 2 + parameter;
915 total_bits = interesting_bits + msbs;
916 pattern <<= parameter;
917 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
919 if(total_bits <= 32) {
920 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
923 else if(total_bits > max_bits) {
928 /* write the unary MSBs */
929 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
931 /* write the unary end bit, the sign bit, and binary LSBs */
932 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
939 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
941 unsigned total_bits, val_bits;
942 FLAC__uint32 pattern;
944 FLAC__ASSERT(0 != bb);
945 FLAC__ASSERT(0 != bb->buffer);
946 FLAC__ASSERT(parameter <= 31);
948 val_bits = FLAC__bitmath_silog2(val);
949 total_bits = 2 + parameter + 5 + val_bits;
951 if(total_bits <= 32) {
953 pattern <<= (parameter + 5);
955 pattern <<= val_bits;
956 pattern |= (val & ((1 << val_bits) - 1));
957 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
961 /* write the '-0' escape code first */
962 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
964 /* write the length */
965 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
967 /* write the value */
968 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
973 #endif /* ifdef FLAC__SYMMETRIC_RICE */
975 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
977 unsigned total_bits, interesting_bits, msbs, uval;
978 FLAC__uint32 pattern;
980 FLAC__ASSERT(0 != bb);
981 FLAC__ASSERT(0 != bb->buffer);
982 FLAC__ASSERT(parameter <= 30);
984 /* fold signed to unsigned */
987 * (unsigned)(((--val) << 1) - 1);
988 * but without the overflow problem at MININT
990 uval = (unsigned)(((-(++val)) << 1) + 1);
992 uval = (unsigned)(val << 1);
994 msbs = uval >> parameter;
995 interesting_bits = 1 + parameter;
996 total_bits = interesting_bits + msbs;
997 pattern = 1 << parameter; /* the unary end bit */
998 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1000 if(total_bits <= 32) {
1001 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1005 /* write the unary MSBs */
1006 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1008 /* write the unary end bit and binary LSBs */
1009 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1016 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1018 unsigned total_bits, interesting_bits, msbs, uval;
1019 FLAC__uint32 pattern;
1021 FLAC__ASSERT(0 != bb);
1022 FLAC__ASSERT(0 != bb->buffer);
1023 FLAC__ASSERT(parameter <= 30);
1027 /* fold signed to unsigned */
1030 * (unsigned)(((--val) << 1) - 1);
1031 * but without the overflow problem at MININT
1033 uval = (unsigned)(((-(++val)) << 1) + 1);
1035 uval = (unsigned)(val << 1);
1037 msbs = uval >> parameter;
1038 interesting_bits = 1 + parameter;
1039 total_bits = interesting_bits + msbs;
1040 pattern = 1 << parameter; /* the unary end bit */
1041 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1043 if(total_bits <= 32) {
1044 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1047 else if(total_bits > max_bits) {
1052 /* write the unary MSBs */
1053 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1055 /* write the unary end bit and binary LSBs */
1056 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1064 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1066 unsigned total_bits, msbs, uval;
1069 FLAC__ASSERT(0 != bb);
1070 FLAC__ASSERT(0 != bb->buffer);
1071 FLAC__ASSERT(parameter > 0);
1073 /* fold signed to unsigned */
1076 * (unsigned)(((--val) << 1) - 1);
1077 * but without the overflow problem at MININT
1079 uval = (unsigned)(((-(++val)) << 1) + 1);
1081 uval = (unsigned)(val << 1);
1083 k = FLAC__bitmath_ilog2(parameter);
1084 if(parameter == 1u<<k) {
1087 FLAC__ASSERT(k <= 30);
1090 total_bits = 1 + k + msbs;
1091 pattern = 1 << k; /* the unary end bit */
1092 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1094 if(total_bits <= 32) {
1095 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1099 /* write the unary MSBs */
1100 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1102 /* write the unary end bit and binary LSBs */
1103 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1110 d = (1 << (k+1)) - parameter;
1111 q = uval / parameter;
1112 r = uval - (q * parameter);
1113 /* write the unary MSBs */
1114 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1116 /* write the unary end bit */
1117 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1119 /* write the binary LSBs */
1121 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1125 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1132 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1134 unsigned total_bits, msbs;
1137 FLAC__ASSERT(0 != bb);
1138 FLAC__ASSERT(0 != bb->buffer);
1139 FLAC__ASSERT(parameter > 0);
1141 k = FLAC__bitmath_ilog2(parameter);
1142 if(parameter == 1u<<k) {
1145 FLAC__ASSERT(k <= 30);
1148 total_bits = 1 + k + msbs;
1149 pattern = 1 << k; /* the unary end bit */
1150 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1152 if(total_bits <= 32) {
1153 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1157 /* write the unary MSBs */
1158 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1160 /* write the unary end bit and binary LSBs */
1161 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1168 d = (1 << (k+1)) - parameter;
1169 q = uval / parameter;
1170 r = uval - (q * parameter);
1171 /* write the unary MSBs */
1172 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1174 /* write the unary end bit */
1175 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1177 /* write the binary LSBs */
1179 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1183 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1191 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1195 FLAC__ASSERT(0 != bb);
1196 FLAC__ASSERT(0 != bb->buffer);
1198 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1201 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1203 else if(val < 0x800) {
1204 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1205 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1207 else if(val < 0x10000) {
1208 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 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);
1212 else if(val < 0x200000) {
1213 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1214 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1215 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1216 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1218 else if(val < 0x4000000) {
1219 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1220 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1221 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1222 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1223 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1226 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1227 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1228 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1229 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1230 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1231 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1237 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1241 FLAC__ASSERT(0 != bb);
1242 FLAC__ASSERT(0 != bb->buffer);
1244 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
1247 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1249 else if(val < 0x800) {
1250 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1251 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1253 else if(val < 0x10000) {
1254 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 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 < 0x200000) {
1259 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1264 else if(val < 0x4000000) {
1265 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1266 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1267 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1268 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1269 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1271 else if(val < 0x80000000) {
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1273 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1274 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1275 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1276 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1277 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1280 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1281 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1282 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1283 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1284 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1285 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1286 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1292 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1294 /* 0-pad to byte boundary */
1296 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1301 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)
1303 /* to avoid a drastic speed penalty we don't:
1304 FLAC__ASSERT(0 != bb);
1305 FLAC__ASSERT(0 != bb->buffer);
1306 FLAC__ASSERT(bb->bits == 0);
1310 if(bb->total_consumed_bits < bb->total_bits) {
1311 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1315 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1321 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)
1323 /* to avoid a drastic speed penalty we don't:
1324 FLAC__ASSERT(0 != bb);
1325 FLAC__ASSERT(0 != bb->buffer);
1326 FLAC__ASSERT(bb->bits == 0);
1330 if(bb->total_consumed_bits < bb->total_bits) {
1331 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1332 bb->consumed_bits++;
1333 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1334 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1335 bb->consumed_blurbs++;
1336 bb->consumed_bits = 0;
1338 bb->total_consumed_bits++;
1342 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1348 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)
1350 /* to avoid a drastic speed penalty we don't:
1351 FLAC__ASSERT(0 != bb);
1352 FLAC__ASSERT(0 != bb->buffer);
1353 FLAC__ASSERT(bb->bits == 0);
1357 if(bb->total_consumed_bits < bb->total_bits) {
1359 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1360 bb->consumed_bits++;
1361 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1362 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1363 bb->consumed_blurbs++;
1364 bb->consumed_bits = 0;
1366 bb->total_consumed_bits++;
1370 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1376 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)
1378 /* to avoid a drastic speed penalty we don't:
1379 FLAC__ASSERT(0 != bb);
1380 FLAC__ASSERT(0 != bb->buffer);
1381 FLAC__ASSERT(bb->bits == 0);
1385 if(bb->total_consumed_bits < bb->total_bits) {
1387 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1388 bb->consumed_bits++;
1389 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1390 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1391 bb->consumed_blurbs++;
1392 bb->consumed_bits = 0;
1394 bb->total_consumed_bits++;
1398 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1404 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)
1405 #ifdef FLAC__NO_MANUAL_INLINING
1409 FLAC__ASSERT(0 != bb);
1410 FLAC__ASSERT(0 != bb->buffer);
1412 FLAC__ASSERT(bits <= 32);
1415 for(i = 0; i < bits; i++) {
1416 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1423 unsigned i, bits_ = bits;
1426 FLAC__ASSERT(0 != bb);
1427 FLAC__ASSERT(0 != bb->buffer);
1429 FLAC__ASSERT(bits <= 32);
1430 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1437 while(bb->total_consumed_bits + bits > bb->total_bits) {
1438 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1441 #if FLAC__BITS_PER_BLURB > 8
1442 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1444 if(bb->consumed_bits) {
1445 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1447 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1449 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1450 bb->consumed_blurbs++;
1451 bb->consumed_bits = 0;
1452 /* we hold off updating bb->total_consumed_bits until the end */
1455 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1456 bb->consumed_bits += bits_;
1457 bb->total_consumed_bits += bits_;
1461 #if FLAC__BITS_PER_BLURB == 32
1462 /* note that we know bits_ cannot be > 32 because of previous assertions */
1463 if(bits_ == FLAC__BITS_PER_BLURB) {
1464 v = bb->buffer[bb->consumed_blurbs];
1465 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1466 bb->consumed_blurbs++;
1467 /* bb->consumed_bits is already 0 */
1468 bb->total_consumed_bits += bits;
1473 while(bits_ >= FLAC__BITS_PER_BLURB) {
1474 v <<= FLAC__BITS_PER_BLURB;
1475 v |= bb->buffer[bb->consumed_blurbs];
1476 bits_ -= FLAC__BITS_PER_BLURB;
1477 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1478 bb->consumed_blurbs++;
1479 /* bb->consumed_bits is already 0 */
1480 /* we hold off updating bb->total_consumed_bits until the end */
1485 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1486 bb->consumed_bits = bits_;
1487 /* we hold off updating bb->total_consumed_bits until the end */
1489 bb->total_consumed_bits += bits;
1491 #if FLAC__BITS_PER_BLURB > 8
1495 for(i = 0; i < bits; i++) {
1496 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1505 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)
1506 #ifdef FLAC__NO_MANUAL_INLINING
1511 FLAC__ASSERT(0 != bb);
1512 FLAC__ASSERT(0 != bb->buffer);
1514 FLAC__ASSERT(bits <= 32);
1522 for(i = 0; i < bits; i++) {
1523 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1531 *val = (FLAC__int32)v;
1535 *val = (FLAC__int32)v;
1541 unsigned i, bits_ = bits;
1544 FLAC__ASSERT(0 != bb);
1545 FLAC__ASSERT(0 != bb->buffer);
1547 FLAC__ASSERT(bits <= 32);
1548 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1555 while(bb->total_consumed_bits + bits > bb->total_bits) {
1556 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1559 #if FLAC__BITS_PER_BLURB > 8
1560 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1562 if(bb->consumed_bits) {
1563 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1565 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1567 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1568 bb->consumed_blurbs++;
1569 bb->consumed_bits = 0;
1570 /* we hold off updating bb->total_consumed_bits until the end */
1573 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1574 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1576 *val = (FLAC__int32)v;
1577 *val >>= (32-bits_);
1578 bb->consumed_bits += bits_;
1579 bb->total_consumed_bits += bits_;
1583 #if FLAC__BITS_PER_BLURB == 32
1584 /* note that we know bits_ cannot be > 32 because of previous assertions */
1585 if(bits_ == FLAC__BITS_PER_BLURB) {
1586 v = bb->buffer[bb->consumed_blurbs];
1588 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1589 bb->consumed_blurbs++;
1590 /* bb->consumed_bits is already 0 */
1591 /* we hold off updating bb->total_consumed_bits until the end */
1594 while(bits_ >= FLAC__BITS_PER_BLURB) {
1595 v <<= FLAC__BITS_PER_BLURB;
1596 v |= bb->buffer[bb->consumed_blurbs];
1597 bits_ -= FLAC__BITS_PER_BLURB;
1598 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1599 bb->consumed_blurbs++;
1600 /* bb->consumed_bits is already 0 */
1601 /* we hold off updating bb->total_consumed_bits until the end */
1606 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1607 bb->consumed_bits = bits_;
1608 /* we hold off updating bb->total_consumed_bits until the end */
1610 bb->total_consumed_bits += bits;
1611 #if FLAC__BITS_PER_BLURB > 8
1614 for(i = 0; i < bits; i++) {
1615 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1625 *val = (FLAC__int32)v;
1629 *val = (FLAC__int32)v;
1635 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)
1636 #ifdef FLAC__NO_MANUAL_INLINING
1640 FLAC__ASSERT(0 != bb);
1641 FLAC__ASSERT(0 != bb->buffer);
1643 FLAC__ASSERT(bits <= 64);
1646 for(i = 0; i < bits; i++) {
1647 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1654 unsigned i, bits_ = bits;
1657 FLAC__ASSERT(0 != bb);
1658 FLAC__ASSERT(0 != bb->buffer);
1660 FLAC__ASSERT(bits <= 64);
1661 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1668 while(bb->total_consumed_bits + bits > bb->total_bits) {
1669 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1672 #if FLAC__BITS_PER_BLURB > 8
1673 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1675 if(bb->consumed_bits) {
1676 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1678 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1680 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1681 bb->consumed_blurbs++;
1682 bb->consumed_bits = 0;
1683 /* we hold off updating bb->total_consumed_bits until the end */
1686 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1687 bb->consumed_bits += bits_;
1688 bb->total_consumed_bits += bits_;
1692 while(bits_ >= FLAC__BITS_PER_BLURB) {
1693 v <<= FLAC__BITS_PER_BLURB;
1694 v |= bb->buffer[bb->consumed_blurbs];
1695 bits_ -= FLAC__BITS_PER_BLURB;
1696 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1697 bb->consumed_blurbs++;
1698 /* bb->consumed_bits is already 0 */
1699 /* we hold off updating bb->total_consumed_bits until the end */
1703 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1704 bb->consumed_bits = bits_;
1705 /* we hold off updating bb->total_consumed_bits until the end */
1707 bb->total_consumed_bits += bits;
1709 #if FLAC__BITS_PER_BLURB > 8
1713 for(i = 0; i < bits; i++) {
1714 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1724 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)
1725 #ifdef FLAC__NO_MANUAL_INLINING
1730 FLAC__ASSERT(0 != bb);
1731 FLAC__ASSERT(0 != bb->buffer);
1733 FLAC__ASSERT(bits <= 64);
1736 for(i = 0; i < bits; i++) {
1737 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1744 *val = (FLAC__int64)v;
1748 *val = (FLAC__int64)v;
1754 unsigned i, bits_ = bits;
1757 FLAC__ASSERT(0 != bb);
1758 FLAC__ASSERT(0 != bb->buffer);
1760 FLAC__ASSERT(bits <= 64);
1761 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1768 while(bb->total_consumed_bits + bits > bb->total_bits) {
1769 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1772 #if FLAC__BITS_PER_BLURB > 8
1773 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1775 if(bb->consumed_bits) {
1776 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1778 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1780 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1781 bb->consumed_blurbs++;
1782 bb->consumed_bits = 0;
1783 /* we hold off updating bb->total_consumed_bits until the end */
1786 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1787 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1789 *val = (FLAC__int64)v;
1790 *val >>= (64-bits_);
1791 bb->consumed_bits += bits_;
1792 bb->total_consumed_bits += bits_;
1796 while(bits_ >= FLAC__BITS_PER_BLURB) {
1797 v <<= FLAC__BITS_PER_BLURB;
1798 v |= bb->buffer[bb->consumed_blurbs];
1799 bits_ -= FLAC__BITS_PER_BLURB;
1800 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1801 bb->consumed_blurbs++;
1802 /* bb->consumed_bits is already 0 */
1803 /* we hold off updating bb->total_consumed_bits until the end */
1807 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1808 bb->consumed_bits = bits_;
1809 /* we hold off updating bb->total_consumed_bits until the end */
1811 bb->total_consumed_bits += bits;
1812 #if FLAC__BITS_PER_BLURB > 8
1815 for(i = 0; i < bits; i++) {
1816 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1826 *val = (FLAC__int64)v;
1830 *val = (FLAC__int64)v;
1837 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)
1839 FLAC__uint32 x8, x32 = 0;
1841 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1843 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1846 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1850 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1854 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1862 FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1865 * @@@ a slightly faster implementation is possible but
1866 * probably not that useful since this is only called a
1867 * couple of times in the metadata readers.
1869 FLAC__ASSERT(0 != bb);
1870 FLAC__ASSERT(0 != bb->buffer);
1873 const unsigned n = bb->consumed_bits & 7;
1879 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
1885 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
1890 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
1898 FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1900 FLAC__ASSERT(0 != bb);
1901 FLAC__ASSERT(0 != bb->buffer);
1902 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1903 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1904 #if FLAC__BITS_PER_BLURB == 8
1906 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1908 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1913 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1914 val += FLAC__BYTES_PER_BLURB * chunk;
1917 bb->consumed_blurbs += chunk;
1918 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1922 @@@ need to write this still
1929 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)
1930 #ifdef FLAC__NO_MANUAL_INLINING
1932 unsigned bit, val_ = 0;
1934 FLAC__ASSERT(0 != bb);
1935 FLAC__ASSERT(0 != bb->buffer);
1938 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1950 unsigned i, val_ = 0;
1951 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1954 FLAC__ASSERT(0 != bb);
1955 FLAC__ASSERT(0 != bb->buffer);
1957 #if FLAC__BITS_PER_BLURB > 8
1958 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1960 if(bb->consumed_bits) {
1961 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1963 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1967 bb->consumed_bits += i;
1968 bb->total_consumed_bits += i;
1969 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1970 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1971 bb->consumed_blurbs++;
1972 bb->consumed_bits = 0;
1977 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1978 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1979 bb->consumed_blurbs++;
1980 bb->consumed_bits = 0;
1981 bb->total_consumed_bits += val_;
1985 if(bb->consumed_blurbs >= total_blurbs_) {
1986 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1988 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1990 b = bb->buffer[bb->consumed_blurbs];
1992 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1996 bb->consumed_bits = i;
1998 if(i == FLAC__BITS_PER_BLURB) {
1999 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2000 bb->consumed_blurbs++;
2001 bb->consumed_bits = 0;
2003 bb->total_consumed_bits += i;
2007 val_ += FLAC__BITS_PER_BLURB;
2008 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
2009 bb->consumed_blurbs++;
2010 /* bb->consumed_bits is already 0 */
2011 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
2014 #if FLAC__BITS_PER_BLURB > 8
2018 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
2032 #ifdef FLAC__SYMMETRIC_RICE
2033 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)
2035 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
2037 FLAC__ASSERT(0 != bb);
2038 FLAC__ASSERT(0 != bb->buffer);
2039 FLAC__ASSERT(parameter <= 31);
2041 /* read the unary MSBs and end bit */
2042 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2045 /* read the sign bit */
2046 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
2049 /* read the binary LSBs */
2050 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2053 /* compose the value */
2054 *val = (msbs << parameter) | lsbs;
2060 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2062 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)
2064 FLAC__uint32 lsbs = 0, msbs = 0;
2067 FLAC__ASSERT(0 != bb);
2068 FLAC__ASSERT(0 != bb->buffer);
2069 FLAC__ASSERT(parameter <= 31);
2071 /* read the unary MSBs and end bit */
2072 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2075 /* read the binary LSBs */
2076 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2079 /* compose the value */
2080 uval = (msbs << parameter) | lsbs;
2082 *val = -((int)(uval >> 1)) - 1;
2084 *val = (int)(uval >> 1);
2089 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)
2091 const FLAC__blurb *buffer = bb->buffer;
2093 unsigned i, j, val_i = 0;
2094 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2095 FLAC__blurb blurb, save_blurb;
2096 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2098 FLAC__ASSERT(0 != bb);
2099 FLAC__ASSERT(0 != bb->buffer);
2100 FLAC__ASSERT(parameter <= 31);
2105 i = bb->consumed_blurbs;
2107 * We unroll the main loop to take care of partially consumed blurbs here.
2109 if(bb->consumed_bits > 0) {
2110 save_blurb = blurb = buffer[i];
2111 cbits = bb->consumed_bits;
2117 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2121 /* dispose of the unary end bit */
2127 lsbs_left = parameter;
2129 if(cbits == FLAC__BITS_PER_BLURB) {
2131 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2136 msbs += FLAC__BITS_PER_BLURB - cbits;
2138 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2143 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2144 if(lsbs_left >= available_bits) {
2145 uval <<= available_bits;
2146 uval |= (blurb >> cbits);
2148 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2150 if(lsbs_left == available_bits) {
2151 /* compose the value */
2152 uval |= (msbs << parameter);
2154 vals[val_i++] = -((int)(uval >> 1)) - 1;
2156 vals[val_i++] = (int)(uval >> 1);
2164 lsbs_left -= available_bits;
2169 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2170 blurb <<= lsbs_left;
2173 /* compose the value */
2174 uval |= (msbs << parameter);
2176 vals[val_i++] = -((int)(uval >> 1)) - 1;
2178 vals[val_i++] = (int)(uval >> 1);
2179 if(val_i == nvals) {
2180 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2192 bb->consumed_blurbs = i;
2193 bb->consumed_bits = cbits;
2194 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2198 * Now that we are blurb-aligned the logic is slightly simpler
2200 while(val_i < nvals) {
2201 for( ; i < bb->blurbs && val_i < nvals; i++) {
2202 save_blurb = blurb = buffer[i];
2207 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2211 /* dispose of the unary end bit */
2217 lsbs_left = parameter;
2219 if(cbits == FLAC__BITS_PER_BLURB) {
2221 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2226 msbs += FLAC__BITS_PER_BLURB - cbits;
2228 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2233 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2234 if(lsbs_left >= available_bits) {
2235 uval <<= available_bits;
2236 uval |= (blurb >> cbits);
2238 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2240 if(lsbs_left == available_bits) {
2241 /* compose the value */
2242 uval |= (msbs << parameter);
2244 vals[val_i++] = -((int)(uval >> 1)) - 1;
2246 vals[val_i++] = (int)(uval >> 1);
2254 lsbs_left -= available_bits;
2259 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2260 blurb <<= lsbs_left;
2263 /* compose the value */
2264 uval |= (msbs << parameter);
2266 vals[val_i++] = -((int)(uval >> 1)) - 1;
2268 vals[val_i++] = (int)(uval >> 1);
2269 if(val_i == nvals) {
2270 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2281 bb->consumed_blurbs = i;
2282 bb->consumed_bits = cbits;
2283 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2285 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2287 /* these must be zero because we can only get here if we got to the end of the buffer */
2288 FLAC__ASSERT(bb->consumed_blurbs == 0);
2289 FLAC__ASSERT(bb->consumed_bits == 0);
2298 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)
2300 FLAC__uint32 lsbs = 0, msbs = 0;
2301 unsigned bit, uval, k;
2303 FLAC__ASSERT(0 != bb);
2304 FLAC__ASSERT(0 != bb->buffer);
2306 k = FLAC__bitmath_ilog2(parameter);
2308 /* read the unary MSBs and end bit */
2309 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2312 /* read the binary LSBs */
2313 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2316 if(parameter == 1u<<k) {
2317 /* compose the value */
2318 uval = (msbs << k) | lsbs;
2321 unsigned d = (1 << (k+1)) - parameter;
2323 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2329 /* compose the value */
2330 uval = msbs * parameter + lsbs;
2333 /* unfold unsigned to signed */
2335 *val = -((int)(uval >> 1)) - 1;
2337 *val = (int)(uval >> 1);
2342 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)
2344 FLAC__uint32 lsbs, msbs = 0;
2347 FLAC__ASSERT(0 != bb);
2348 FLAC__ASSERT(0 != bb->buffer);
2350 k = FLAC__bitmath_ilog2(parameter);
2352 /* read the unary MSBs and end bit */
2353 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2356 /* read the binary LSBs */
2357 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2360 if(parameter == 1u<<k) {
2361 /* compose the value */
2362 *val = (msbs << k) | lsbs;
2365 unsigned d = (1 << (k+1)) - parameter;
2367 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2373 /* compose the value */
2374 *val = msbs * parameter + lsbs;
2381 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2382 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)
2388 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2391 raw[(*rawlen)++] = (FLAC__byte)x;
2392 if(!(x & 0x80)) { /* 0xxxxxxx */
2396 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2400 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2404 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2408 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2412 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2421 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2424 raw[(*rawlen)++] = (FLAC__byte)x;
2425 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2436 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2437 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)
2443 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2446 raw[(*rawlen)++] = (FLAC__byte)x;
2447 if(!(x & 0x80)) { /* 0xxxxxxx */
2451 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2455 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2459 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2463 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2467 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2471 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2476 *val = 0xffffffffffffffff;
2480 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2483 raw[(*rawlen)++] = (FLAC__byte)x;
2484 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2485 *val = 0xffffffffffffffff;
2495 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2499 fprintf(out, "bitbuffer is NULL\n");
2502 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);
2504 for(i = 0; i < bb->blurbs; i++) {
2505 fprintf(out, "%08X: ", i);
2506 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2507 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2510 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2514 fprintf(out, "%08X: ", i);
2515 for(j = 0; j < bb->bits; j++)
2516 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2519 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);