1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004 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 static const unsigned char byte_to_unary_table[] = {
67 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
68 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
69 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
72 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
73 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
85 #if FLAC__BITS_PER_BLURB == 8
86 #define FLAC__BITS_PER_BLURB_LOG2 3
87 #define FLAC__BYTES_PER_BLURB 1
88 #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
89 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
90 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
91 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
92 #define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
93 #elif FLAC__BITS_PER_BLURB == 32
94 #define FLAC__BITS_PER_BLURB_LOG2 5
95 #define FLAC__BYTES_PER_BLURB 4
96 #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
97 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
98 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
99 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
100 #define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
102 /* ERROR, only sizes of 8 and 32 are supported */
105 #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
110 #define min(x,y) ((x)<(y)?(x):(y))
114 #define max(x,y) ((x)>(y)?(x):(y))
116 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
118 #define FLAC__U64L(x) x
120 #define FLAC__U64L(x) x##LLU
127 struct FLAC__BitBuffer {
129 unsigned capacity; /* in blurbs */
130 unsigned blurbs, bits;
131 unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
132 unsigned consumed_blurbs, consumed_bits;
133 unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
134 FLAC__uint16 read_crc16;
135 #if FLAC__BITS_PER_BLURB == 32
136 unsigned crc16_align;
138 FLAC__blurb save_head, save_tail;
141 #if FLAC__BITS_PER_BLURB == 32
142 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
144 if(bb->crc16_align == 0) {
145 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
146 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
147 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
148 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
150 else if(bb->crc16_align == 8) {
151 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
152 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
153 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
155 else if(bb->crc16_align == 16) {
156 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
157 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
159 else if(bb->crc16_align == 24) {
160 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
167 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
168 * does not shift left what is consumed, it just chops off the end, whether
169 * there is unconsumed data there or not. This is OK because currently we
170 * never shrink the buffer, but if this ever changes, we'll have to do some
173 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
175 FLAC__blurb *new_buffer;
177 FLAC__ASSERT(0 != bb);
178 FLAC__ASSERT(0 != bb->buffer);
180 if(bb->capacity == new_capacity)
183 new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
186 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
187 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
188 bb->blurbs = new_capacity;
190 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
192 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
193 bb->consumed_blurbs = new_capacity;
194 bb->consumed_bits = 0;
195 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
197 free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
198 bb->buffer = new_buffer;
199 bb->capacity = new_capacity;
203 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
205 unsigned new_capacity;
207 FLAC__ASSERT(min_blurbs_to_add > 0);
209 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
210 return bitbuffer_resize_(bb, new_capacity);
213 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
215 FLAC__ASSERT(0 != bb);
216 FLAC__ASSERT(0 != bb->buffer);
218 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
219 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
224 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)
229 /* first shift the unconsumed buffer data toward the front as much as possible */
230 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
231 #if FLAC__BITS_PER_BLURB == 8
233 * memset and memcpy are usually implemented in assembly language
234 * by the system libc, and they can be much faster
236 const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
237 const unsigned r = bb->consumed_blurbs, l = r_end - r;
238 memmove(&bb->buffer[0], &bb->buffer[r], l);
239 memset(&bb->buffer[l], 0, r);
240 #elif FLAC__BITS_PER_BLURB == 32
241 /* still needs optimization */
242 const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
243 unsigned l = 0, r = bb->consumed_blurbs;
244 for( ; r < r_end; l++, r++)
245 bb->buffer[l] = bb->buffer[r];
246 for( ; l < r_end; l++)
249 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
250 #endif /* FLAC__BITS_PER_BLURB == 32 or 8 */
252 bb->blurbs -= bb->consumed_blurbs;
253 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
254 bb->consumed_blurbs = 0;
255 bb->total_consumed_bits = bb->consumed_bits;
258 /* grow if we need to */
259 if(bb->capacity <= 1) {
260 if(!bitbuffer_resize_(bb, 16))
264 /* set the target for reading, taking into account blurb alignment */
265 #if FLAC__BITS_PER_BLURB == 8
266 /* blurb == byte, so no gyrations necessary: */
267 target = bb->buffer + bb->blurbs;
268 bytes = bb->capacity - bb->blurbs;
269 #elif FLAC__BITS_PER_BLURB == 32
270 /* @@@ WATCHOUT: code currently only works for big-endian: */
271 FLAC__ASSERT((bb->bits & 7) == 0);
272 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
273 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
275 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
278 /* finally, read in some data */
279 if(!read_callback(target, &bytes, client_data))
282 /* now we have to handle partial blurb cases: */
283 #if FLAC__BITS_PER_BLURB == 8
284 /* blurb == byte, so no gyrations necessary: */
286 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
287 #elif FLAC__BITS_PER_BLURB == 32
288 /* @@@ WATCHOUT: code currently only works for big-endian: */
290 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
291 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
292 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
293 bb->total_bits += (bytes << 3);
296 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
301 /***********************************************************************
303 * Class constructor/destructor
305 ***********************************************************************/
307 FLAC__BitBuffer *FLAC__bitbuffer_new()
309 FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
312 memset(bb, 0, sizeof(FLAC__BitBuffer));
315 bb->blurbs = bb->bits = bb->total_bits = 0;
316 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
321 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
323 FLAC__ASSERT(0 != bb);
325 FLAC__bitbuffer_free(bb);
329 /***********************************************************************
331 * Public class methods
333 ***********************************************************************/
335 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
337 FLAC__ASSERT(0 != bb);
341 bb->blurbs = bb->bits = bb->total_bits = 0;
342 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
344 return FLAC__bitbuffer_clear(bb);
347 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
349 FLAC__ASSERT(0 != bb);
350 FLAC__ASSERT(bytes > 0);
352 if(!FLAC__bitbuffer_init(bb))
355 if(!bitbuffer_ensure_size_(bb, bytes << 3))
358 FLAC__ASSERT(0 != buffer);
359 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
360 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
361 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
362 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
363 bb->total_bits = bytes << 3;
367 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
369 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
371 FLAC__ASSERT(0 != dest);
372 FLAC__ASSERT(0 != src);
376 if(dest->bits != src->consumed_bits)
378 if(!bitbuffer_ensure_size_(dest, bits_to_add))
380 if(dest->bits == 0) {
381 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
383 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
384 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
385 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
386 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)));
389 dest->buffer[dest->blurbs] <<= bits_to_add;
390 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
392 dest->bits = src->bits;
393 dest->total_bits += bits_to_add;
394 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
399 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
401 FLAC__ASSERT(0 != bb);
407 bb->blurbs = bb->bits = bb->total_bits = 0;
408 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
411 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
413 if(bb->buffer == 0) {
414 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
415 bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
420 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
422 bb->blurbs = bb->bits = bb->total_bits = 0;
423 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
427 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
429 FLAC__ASSERT(0 != dest);
430 FLAC__ASSERT(0 != dest->buffer);
431 FLAC__ASSERT(0 != src);
432 FLAC__ASSERT(0 != src->buffer);
434 if(dest->capacity < src->capacity)
435 if(!bitbuffer_resize_(dest, src->capacity))
437 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
438 dest->blurbs = src->blurbs;
439 dest->bits = src->bits;
440 dest->total_bits = src->total_bits;
441 dest->consumed_blurbs = src->consumed_blurbs;
442 dest->consumed_bits = src->consumed_bits;
443 dest->total_consumed_bits = src->total_consumed_bits;
444 dest->read_crc16 = src->read_crc16;
448 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
450 FLAC__ASSERT(0 != bb);
451 FLAC__ASSERT(0 != bb->buffer);
452 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
454 bb->read_crc16 = seed;
455 #if FLAC__BITS_PER_BLURB == 8
456 /* no need to do anything */
457 #elif FLAC__BITS_PER_BLURB == 32
458 bb->crc16_align = bb->consumed_bits;
460 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
464 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
466 FLAC__ASSERT(0 != bb);
467 FLAC__ASSERT(0 != bb->buffer);
468 FLAC__ASSERT((bb->bits & 7) == 0);
469 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
471 #if FLAC__BITS_PER_BLURB == 8
472 /* no need to do anything */
473 #elif FLAC__BITS_PER_BLURB == 32
474 /*@@@ 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 */
475 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
476 if(bb->consumed_bits == 8) {
477 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
478 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
480 else if(bb->consumed_bits == 16) {
481 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
482 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
483 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
485 else if(bb->consumed_bits == 24) {
486 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
487 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
488 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
489 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
493 if(bb->consumed_bits == 8) {
494 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
495 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
497 else if(bb->consumed_bits == 16) {
498 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
499 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
500 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
502 else if(bb->consumed_bits == 24) {
503 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
504 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
505 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
506 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
509 bb->crc16_align = bb->consumed_bits;
511 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
513 return bb->read_crc16;
516 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
518 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
520 #if FLAC__BITS_PER_BLURB == 8
521 return FLAC__crc16(bb->buffer, bb->blurbs);
522 #elif FLAC__BITS_PER_BLURB == 32
523 /* @@@ WATCHOUT: code currently only works for big-endian: */
524 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
526 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
530 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
532 FLAC__ASSERT(0 != bb);
533 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
534 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
535 #if FLAC__BITS_PER_BLURB == 8
536 return FLAC__crc8(bb->buffer, bb->blurbs);
537 #elif FLAC__BITS_PER_BLURB == 32
538 /* @@@ WATCHOUT: code currently only works for big-endian: */
539 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
541 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
545 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
547 return ((bb->bits & 7) == 0);
550 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
552 return ((bb->consumed_bits & 7) == 0);
555 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
557 return 8 - (bb->consumed_bits & 7);
560 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
562 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
563 return (bb->total_bits - bb->total_consumed_bits) >> 3;
566 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
568 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
569 #if FLAC__BITS_PER_BLURB == 8
570 *buffer = bb->buffer + bb->consumed_blurbs;
571 *bytes = bb->blurbs - bb->consumed_blurbs;
572 #elif FLAC__BITS_PER_BLURB == 32
573 /* @@@ WATCHOUT: code currently only works for big-endian: */
574 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
575 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
577 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
581 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
583 #if FLAC__BITS_PER_BLURB == 8
585 #elif FLAC__BITS_PER_BLURB == 32
586 /* @@@ WATCHOUT: code currently only works for big-endian: */
589 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
593 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
597 FLAC__ASSERT(0 != bb);
598 FLAC__ASSERT(0 != bb->buffer);
602 if(!bitbuffer_ensure_size_(bb, bits))
604 bb->total_bits += bits;
606 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
607 bb->buffer[bb->blurbs] <<= n;
610 if(bb->bits == FLAC__BITS_PER_BLURB) {
618 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
622 FLAC__ASSERT(0 != bb);
623 FLAC__ASSERT(0 != bb->buffer);
625 FLAC__ASSERT(bits <= 32);
628 /* inline the size check so we don't incure a function call unnecessarily */
629 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
630 if(!bitbuffer_ensure_size_(bb, bits))
634 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
635 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
636 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
638 bb->total_bits += bits;
640 n = FLAC__BITS_PER_BLURB - bb->bits;
641 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
642 if(bits < FLAC__BITS_PER_BLURB) {
643 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
647 else if(bits == FLAC__BITS_PER_BLURB) {
648 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
652 k = bits - FLAC__BITS_PER_BLURB;
653 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
654 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
655 val &= (~(0xffffffff << k));
656 bits -= FLAC__BITS_PER_BLURB;
660 bb->buffer[bb->blurbs] <<= bits;
661 bb->buffer[bb->blurbs] |= val;
672 bb->buffer[bb->blurbs] <<= n;
673 bb->buffer[bb->blurbs] |= (val >> k);
674 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
675 val &= (~(0xffffffff << k));
685 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
687 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
690 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
692 static const FLAC__uint64 mask[] = {
694 FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
695 FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
696 FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
697 FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
698 FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
699 FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
700 FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
701 FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
702 FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
703 FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
704 FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
705 FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
706 FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
707 FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
708 FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
709 FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
713 FLAC__ASSERT(0 != bb);
714 FLAC__ASSERT(0 != bb->buffer);
716 FLAC__ASSERT(bits <= 64);
719 if(!bitbuffer_ensure_size_(bb, bits))
722 bb->total_bits += bits;
725 if(bits < FLAC__BITS_PER_BLURB) {
726 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
730 else if(bits == FLAC__BITS_PER_BLURB) {
731 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
735 k = bits - FLAC__BITS_PER_BLURB;
736 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
737 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
738 val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
739 bits -= FLAC__BITS_PER_BLURB;
743 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
745 bb->buffer[bb->blurbs] <<= n;
746 bb->buffer[bb->blurbs] |= (val >> k);
747 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
748 val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
751 if(bb->bits == FLAC__BITS_PER_BLURB) {
762 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
764 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
768 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
770 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
772 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
773 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
775 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
777 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
779 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
785 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
789 /* this could be faster but currently we don't need it to be */
790 for(i = 0; i < nvals; i++) {
791 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
798 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
801 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
803 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
805 if(!FLAC__bitbuffer_write_zeroes(bb, val))
807 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
811 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
815 /* fold signed to unsigned */
818 * (unsigned)(((--val) << 1) - 1);
819 * but without the overflow problem at MININT
821 uval = (unsigned)(((-(++val)) << 1) + 1);
823 uval = (unsigned)(val << 1);
825 msbs = uval >> parameter;
827 return 1 + parameter + msbs;
831 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
833 unsigned bits, msbs, uval;
836 FLAC__ASSERT(parameter > 0);
838 /* fold signed to unsigned */
841 * (unsigned)(((--val) << 1) - 1);
842 * but without the overflow problem at MININT
844 uval = (unsigned)(((-(++val)) << 1) + 1);
846 uval = (unsigned)(val << 1);
848 k = FLAC__bitmath_ilog2(parameter);
849 if(parameter == 1u<<k) {
850 FLAC__ASSERT(k <= 30);
858 d = (1 << (k+1)) - parameter;
859 q = uval / parameter;
860 r = uval - (q * parameter);
869 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
874 FLAC__ASSERT(parameter > 0);
876 k = FLAC__bitmath_ilog2(parameter);
877 if(parameter == 1u<<k) {
878 FLAC__ASSERT(k <= 30);
886 d = (1 << (k+1)) - parameter;
887 q = uval / parameter;
888 r = uval - (q * parameter);
898 #ifdef FLAC__SYMMETRIC_RICE
899 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
901 unsigned total_bits, interesting_bits, msbs;
902 FLAC__uint32 pattern;
904 FLAC__ASSERT(0 != bb);
905 FLAC__ASSERT(0 != bb->buffer);
906 FLAC__ASSERT(parameter <= 31);
908 /* init pattern with the unary end bit and the sign bit */
916 msbs = val >> parameter;
917 interesting_bits = 2 + parameter;
918 total_bits = interesting_bits + msbs;
919 pattern <<= parameter;
920 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
922 if(total_bits <= 32) {
923 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
927 /* write the unary MSBs */
928 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
930 /* write the unary end bit, the sign bit, and binary LSBs */
931 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
938 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
940 unsigned total_bits, interesting_bits, msbs;
941 FLAC__uint32 pattern;
943 FLAC__ASSERT(0 != bb);
944 FLAC__ASSERT(0 != bb->buffer);
945 FLAC__ASSERT(parameter <= 31);
949 /* init pattern with the unary end bit and the sign bit */
957 msbs = val >> parameter;
958 interesting_bits = 2 + parameter;
959 total_bits = interesting_bits + msbs;
960 pattern <<= parameter;
961 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
963 if(total_bits <= 32) {
964 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
967 else if(total_bits > max_bits) {
972 /* write the unary MSBs */
973 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
975 /* write the unary end bit, the sign bit, and binary LSBs */
976 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
983 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
985 unsigned total_bits, val_bits;
986 FLAC__uint32 pattern;
988 FLAC__ASSERT(0 != bb);
989 FLAC__ASSERT(0 != bb->buffer);
990 FLAC__ASSERT(parameter <= 31);
992 val_bits = FLAC__bitmath_silog2(val);
993 total_bits = 2 + parameter + 5 + val_bits;
995 if(total_bits <= 32) {
997 pattern <<= (parameter + 5);
999 pattern <<= val_bits;
1000 pattern |= (val & ((1 << val_bits) - 1));
1001 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1005 /* write the '-0' escape code first */
1006 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
1008 /* write the length */
1009 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
1011 /* write the value */
1012 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
1017 #endif /* ifdef FLAC__SYMMETRIC_RICE */
1019 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1021 unsigned total_bits, interesting_bits, msbs, uval;
1022 FLAC__uint32 pattern;
1024 FLAC__ASSERT(0 != bb);
1025 FLAC__ASSERT(0 != bb->buffer);
1026 FLAC__ASSERT(parameter <= 30);
1028 /* fold signed to unsigned */
1031 * (unsigned)(((--val) << 1) - 1);
1032 * but without the overflow problem at MININT
1034 uval = (unsigned)(((-(++val)) << 1) + 1);
1036 uval = (unsigned)(val << 1);
1038 msbs = uval >> parameter;
1039 interesting_bits = 1 + parameter;
1040 total_bits = interesting_bits + msbs;
1041 pattern = 1 << parameter; /* the unary end bit */
1042 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1044 if(total_bits <= 32) {
1045 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1049 /* write the unary MSBs */
1050 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1052 /* write the unary end bit and binary LSBs */
1053 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1060 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1062 unsigned total_bits, interesting_bits, msbs, uval;
1063 FLAC__uint32 pattern;
1065 FLAC__ASSERT(0 != bb);
1066 FLAC__ASSERT(0 != bb->buffer);
1067 FLAC__ASSERT(parameter <= 30);
1071 /* fold signed to unsigned */
1074 * (unsigned)(((--val) << 1) - 1);
1075 * but without the overflow problem at MININT
1077 uval = (unsigned)(((-(++val)) << 1) + 1);
1079 uval = (unsigned)(val << 1);
1081 msbs = uval >> parameter;
1082 interesting_bits = 1 + parameter;
1083 total_bits = interesting_bits + msbs;
1084 pattern = 1 << parameter; /* the unary end bit */
1085 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1087 if(total_bits <= 32) {
1088 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1091 else if(total_bits > max_bits) {
1096 /* write the unary MSBs */
1097 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1099 /* write the unary end bit and binary LSBs */
1100 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1108 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1110 unsigned total_bits, msbs, uval;
1113 FLAC__ASSERT(0 != bb);
1114 FLAC__ASSERT(0 != bb->buffer);
1115 FLAC__ASSERT(parameter > 0);
1117 /* fold signed to unsigned */
1120 * (unsigned)(((--val) << 1) - 1);
1121 * but without the overflow problem at MININT
1123 uval = (unsigned)(((-(++val)) << 1) + 1);
1125 uval = (unsigned)(val << 1);
1127 k = FLAC__bitmath_ilog2(parameter);
1128 if(parameter == 1u<<k) {
1131 FLAC__ASSERT(k <= 30);
1134 total_bits = 1 + k + msbs;
1135 pattern = 1 << k; /* the unary end bit */
1136 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1138 if(total_bits <= 32) {
1139 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1143 /* write the unary MSBs */
1144 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1146 /* write the unary end bit and binary LSBs */
1147 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1154 d = (1 << (k+1)) - parameter;
1155 q = uval / parameter;
1156 r = uval - (q * parameter);
1157 /* write the unary MSBs */
1158 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1160 /* write the unary end bit */
1161 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1163 /* write the binary LSBs */
1165 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1169 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1176 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1178 unsigned total_bits, msbs;
1181 FLAC__ASSERT(0 != bb);
1182 FLAC__ASSERT(0 != bb->buffer);
1183 FLAC__ASSERT(parameter > 0);
1185 k = FLAC__bitmath_ilog2(parameter);
1186 if(parameter == 1u<<k) {
1189 FLAC__ASSERT(k <= 30);
1192 total_bits = 1 + k + msbs;
1193 pattern = 1 << k; /* the unary end bit */
1194 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1196 if(total_bits <= 32) {
1197 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1201 /* write the unary MSBs */
1202 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1204 /* write the unary end bit and binary LSBs */
1205 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1212 d = (1 << (k+1)) - parameter;
1213 q = uval / parameter;
1214 r = uval - (q * parameter);
1215 /* write the unary MSBs */
1216 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1218 /* write the unary end bit */
1219 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1221 /* write the binary LSBs */
1223 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1227 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1235 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1239 FLAC__ASSERT(0 != bb);
1240 FLAC__ASSERT(0 != bb->buffer);
1242 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1245 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1247 else if(val < 0x800) {
1248 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1249 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1251 else if(val < 0x10000) {
1252 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1253 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1254 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1256 else if(val < 0x200000) {
1257 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1258 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1259 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1260 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1262 else if(val < 0x4000000) {
1263 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1264 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1265 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1266 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1267 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1270 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1271 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1273 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1274 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1275 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1281 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1285 FLAC__ASSERT(0 != bb);
1286 FLAC__ASSERT(0 != bb->buffer);
1288 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
1291 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1293 else if(val < 0x800) {
1294 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1295 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1297 else if(val < 0x10000) {
1298 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1299 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1300 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1302 else if(val < 0x200000) {
1303 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1304 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1305 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1306 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1308 else if(val < 0x4000000) {
1309 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1310 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1311 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1312 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1313 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1315 else if(val < 0x80000000) {
1316 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1317 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1318 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1319 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1320 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1321 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1324 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1325 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1326 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1327 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1328 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1329 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1330 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1336 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1338 /* 0-pad to byte boundary */
1340 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1345 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)
1347 /* to avoid a drastic speed penalty we don't:
1348 FLAC__ASSERT(0 != bb);
1349 FLAC__ASSERT(0 != bb->buffer);
1350 FLAC__ASSERT(bb->bits == 0);
1354 if(bb->total_consumed_bits < bb->total_bits) {
1355 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1359 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1365 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)
1367 /* to avoid a drastic speed penalty we don't:
1368 FLAC__ASSERT(0 != bb);
1369 FLAC__ASSERT(0 != bb->buffer);
1370 FLAC__ASSERT(bb->bits == 0);
1374 if(bb->total_consumed_bits < bb->total_bits) {
1375 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1376 bb->consumed_bits++;
1377 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1378 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1379 bb->consumed_blurbs++;
1380 bb->consumed_bits = 0;
1382 bb->total_consumed_bits++;
1386 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1392 FLAC__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)
1394 /* to avoid a drastic speed penalty we don't:
1395 FLAC__ASSERT(0 != bb);
1396 FLAC__ASSERT(0 != bb->buffer);
1397 FLAC__ASSERT(bb->bits == 0);
1401 if(bb->total_consumed_bits < bb->total_bits) {
1403 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1404 bb->consumed_bits++;
1405 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1406 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1407 bb->consumed_blurbs++;
1408 bb->consumed_bits = 0;
1410 bb->total_consumed_bits++;
1414 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1420 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)
1422 /* to avoid a drastic speed penalty we don't:
1423 FLAC__ASSERT(0 != bb);
1424 FLAC__ASSERT(0 != bb->buffer);
1425 FLAC__ASSERT(bb->bits == 0);
1429 if(bb->total_consumed_bits < bb->total_bits) {
1431 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1432 bb->consumed_bits++;
1433 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1434 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1435 bb->consumed_blurbs++;
1436 bb->consumed_bits = 0;
1438 bb->total_consumed_bits++;
1442 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1448 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)
1449 #ifdef FLAC__NO_MANUAL_INLINING
1453 FLAC__ASSERT(0 != bb);
1454 FLAC__ASSERT(0 != bb->buffer);
1456 FLAC__ASSERT(bits <= 32);
1459 for(i = 0; i < bits; i++) {
1460 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1467 unsigned i, bits_ = bits;
1470 FLAC__ASSERT(0 != bb);
1471 FLAC__ASSERT(0 != bb->buffer);
1473 FLAC__ASSERT(bits <= 32);
1474 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1481 while(bb->total_consumed_bits + bits > bb->total_bits) {
1482 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1485 #if FLAC__BITS_PER_BLURB > 8
1486 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1488 if(bb->consumed_bits) {
1489 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1491 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1493 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1494 bb->consumed_blurbs++;
1495 bb->consumed_bits = 0;
1496 /* we hold off updating bb->total_consumed_bits until the end */
1499 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1500 bb->consumed_bits += bits_;
1501 bb->total_consumed_bits += bits_;
1505 #if FLAC__BITS_PER_BLURB == 32
1506 /* note that we know bits_ cannot be > 32 because of previous assertions */
1507 if(bits_ == FLAC__BITS_PER_BLURB) {
1508 v = bb->buffer[bb->consumed_blurbs];
1509 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1510 bb->consumed_blurbs++;
1511 /* bb->consumed_bits is already 0 */
1512 bb->total_consumed_bits += bits;
1517 while(bits_ >= FLAC__BITS_PER_BLURB) {
1518 v <<= FLAC__BITS_PER_BLURB;
1519 v |= bb->buffer[bb->consumed_blurbs];
1520 bits_ -= FLAC__BITS_PER_BLURB;
1521 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1522 bb->consumed_blurbs++;
1523 /* bb->consumed_bits is already 0 */
1524 /* we hold off updating bb->total_consumed_bits until the end */
1529 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1530 bb->consumed_bits = bits_;
1531 /* we hold off updating bb->total_consumed_bits until the end */
1533 bb->total_consumed_bits += bits;
1535 #if FLAC__BITS_PER_BLURB > 8
1539 for(i = 0; i < bits; i++) {
1540 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1549 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)
1550 #ifdef FLAC__NO_MANUAL_INLINING
1555 FLAC__ASSERT(0 != bb);
1556 FLAC__ASSERT(0 != bb->buffer);
1558 FLAC__ASSERT(bits <= 32);
1566 for(i = 0; i < bits; i++) {
1567 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1575 *val = (FLAC__int32)v;
1579 *val = (FLAC__int32)v;
1585 unsigned i, bits_ = bits;
1588 FLAC__ASSERT(0 != bb);
1589 FLAC__ASSERT(0 != bb->buffer);
1591 FLAC__ASSERT(bits <= 32);
1592 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1599 while(bb->total_consumed_bits + bits > bb->total_bits) {
1600 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1603 #if FLAC__BITS_PER_BLURB > 8
1604 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1606 if(bb->consumed_bits) {
1607 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1609 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1611 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1612 bb->consumed_blurbs++;
1613 bb->consumed_bits = 0;
1614 /* we hold off updating bb->total_consumed_bits until the end */
1617 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1618 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1620 *val = (FLAC__int32)v;
1621 *val >>= (32-bits_);
1622 bb->consumed_bits += bits_;
1623 bb->total_consumed_bits += bits_;
1627 #if FLAC__BITS_PER_BLURB == 32
1628 /* note that we know bits_ cannot be > 32 because of previous assertions */
1629 if(bits_ == FLAC__BITS_PER_BLURB) {
1630 v = bb->buffer[bb->consumed_blurbs];
1632 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1633 bb->consumed_blurbs++;
1634 /* bb->consumed_bits is already 0 */
1635 /* we hold off updating bb->total_consumed_bits until the end */
1638 while(bits_ >= FLAC__BITS_PER_BLURB) {
1639 v <<= FLAC__BITS_PER_BLURB;
1640 v |= bb->buffer[bb->consumed_blurbs];
1641 bits_ -= FLAC__BITS_PER_BLURB;
1642 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1643 bb->consumed_blurbs++;
1644 /* bb->consumed_bits is already 0 */
1645 /* we hold off updating bb->total_consumed_bits until the end */
1650 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1651 bb->consumed_bits = bits_;
1652 /* we hold off updating bb->total_consumed_bits until the end */
1654 bb->total_consumed_bits += bits;
1655 #if FLAC__BITS_PER_BLURB > 8
1658 for(i = 0; i < bits; i++) {
1659 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1669 *val = (FLAC__int32)v;
1673 *val = (FLAC__int32)v;
1679 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)
1680 #ifdef FLAC__NO_MANUAL_INLINING
1684 FLAC__ASSERT(0 != bb);
1685 FLAC__ASSERT(0 != bb->buffer);
1687 FLAC__ASSERT(bits <= 64);
1690 for(i = 0; i < bits; i++) {
1691 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1698 unsigned i, bits_ = bits;
1701 FLAC__ASSERT(0 != bb);
1702 FLAC__ASSERT(0 != bb->buffer);
1704 FLAC__ASSERT(bits <= 64);
1705 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1712 while(bb->total_consumed_bits + bits > bb->total_bits) {
1713 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1716 #if FLAC__BITS_PER_BLURB > 8
1717 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1719 if(bb->consumed_bits) {
1720 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1722 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1724 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1725 bb->consumed_blurbs++;
1726 bb->consumed_bits = 0;
1727 /* we hold off updating bb->total_consumed_bits until the end */
1730 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1731 bb->consumed_bits += bits_;
1732 bb->total_consumed_bits += bits_;
1736 while(bits_ >= FLAC__BITS_PER_BLURB) {
1737 v <<= FLAC__BITS_PER_BLURB;
1738 v |= bb->buffer[bb->consumed_blurbs];
1739 bits_ -= FLAC__BITS_PER_BLURB;
1740 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1741 bb->consumed_blurbs++;
1742 /* bb->consumed_bits is already 0 */
1743 /* we hold off updating bb->total_consumed_bits until the end */
1747 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1748 bb->consumed_bits = bits_;
1749 /* we hold off updating bb->total_consumed_bits until the end */
1751 bb->total_consumed_bits += bits;
1753 #if FLAC__BITS_PER_BLURB > 8
1757 for(i = 0; i < bits; i++) {
1758 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1768 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)
1769 #ifdef FLAC__NO_MANUAL_INLINING
1774 FLAC__ASSERT(0 != bb);
1775 FLAC__ASSERT(0 != bb->buffer);
1777 FLAC__ASSERT(bits <= 64);
1780 for(i = 0; i < bits; i++) {
1781 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1788 *val = (FLAC__int64)v;
1792 *val = (FLAC__int64)v;
1798 unsigned i, bits_ = bits;
1801 FLAC__ASSERT(0 != bb);
1802 FLAC__ASSERT(0 != bb->buffer);
1804 FLAC__ASSERT(bits <= 64);
1805 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1812 while(bb->total_consumed_bits + bits > bb->total_bits) {
1813 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1816 #if FLAC__BITS_PER_BLURB > 8
1817 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1819 if(bb->consumed_bits) {
1820 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1822 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1824 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1825 bb->consumed_blurbs++;
1826 bb->consumed_bits = 0;
1827 /* we hold off updating bb->total_consumed_bits until the end */
1830 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1831 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1833 *val = (FLAC__int64)v;
1834 *val >>= (64-bits_);
1835 bb->consumed_bits += bits_;
1836 bb->total_consumed_bits += bits_;
1840 while(bits_ >= FLAC__BITS_PER_BLURB) {
1841 v <<= FLAC__BITS_PER_BLURB;
1842 v |= bb->buffer[bb->consumed_blurbs];
1843 bits_ -= FLAC__BITS_PER_BLURB;
1844 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1845 bb->consumed_blurbs++;
1846 /* bb->consumed_bits is already 0 */
1847 /* we hold off updating bb->total_consumed_bits until the end */
1851 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1852 bb->consumed_bits = bits_;
1853 /* we hold off updating bb->total_consumed_bits until the end */
1855 bb->total_consumed_bits += bits;
1856 #if FLAC__BITS_PER_BLURB > 8
1859 for(i = 0; i < bits; i++) {
1860 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1870 *val = (FLAC__int64)v;
1874 *val = (FLAC__int64)v;
1881 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)
1883 FLAC__uint32 x8, x32 = 0;
1885 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1887 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1890 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1894 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1898 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1906 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)
1909 * @@@ a slightly faster implementation is possible but
1910 * probably not that useful since this is only called a
1911 * couple of times in the metadata readers.
1913 FLAC__ASSERT(0 != bb);
1914 FLAC__ASSERT(0 != bb->buffer);
1917 const unsigned n = bb->consumed_bits & 7;
1923 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
1929 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
1934 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
1942 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)
1944 FLAC__ASSERT(0 != bb);
1945 FLAC__ASSERT(0 != bb->buffer);
1946 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1947 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1948 #if FLAC__BITS_PER_BLURB == 8
1950 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1952 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1957 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1958 val += FLAC__BYTES_PER_BLURB * chunk;
1961 bb->consumed_blurbs += chunk;
1962 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1966 @@@ need to write this still
1973 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)
1974 #ifdef FLAC__NO_MANUAL_INLINING
1976 unsigned bit, val_ = 0;
1978 FLAC__ASSERT(0 != bb);
1979 FLAC__ASSERT(0 != bb->buffer);
1982 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1994 unsigned i, val_ = 0;
1995 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1998 FLAC__ASSERT(0 != bb);
1999 FLAC__ASSERT(0 != bb->buffer);
2001 #if FLAC__BITS_PER_BLURB > 8
2002 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
2004 if(bb->consumed_bits) {
2005 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
2007 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
2011 bb->consumed_bits += i;
2012 bb->total_consumed_bits += i;
2013 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
2014 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2015 bb->consumed_blurbs++;
2016 bb->consumed_bits = 0;
2021 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
2022 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2023 bb->consumed_blurbs++;
2024 bb->consumed_bits = 0;
2025 bb->total_consumed_bits += val_;
2029 if(bb->consumed_blurbs >= total_blurbs_) {
2030 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2032 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
2034 b = bb->buffer[bb->consumed_blurbs];
2036 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
2040 bb->consumed_bits = i;
2042 if(i == FLAC__BITS_PER_BLURB) {
2043 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2044 bb->consumed_blurbs++;
2045 bb->consumed_bits = 0;
2047 bb->total_consumed_bits += i;
2051 val_ += FLAC__BITS_PER_BLURB;
2052 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
2053 bb->consumed_blurbs++;
2054 /* bb->consumed_bits is already 0 */
2055 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
2058 #if FLAC__BITS_PER_BLURB > 8
2062 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
2076 #ifdef FLAC__SYMMETRIC_RICE
2077 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)
2079 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
2081 FLAC__ASSERT(0 != bb);
2082 FLAC__ASSERT(0 != bb->buffer);
2083 FLAC__ASSERT(parameter <= 31);
2085 /* read the unary MSBs and end bit */
2086 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2089 /* read the sign bit */
2090 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
2093 /* read the binary LSBs */
2094 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2097 /* compose the value */
2098 *val = (msbs << parameter) | lsbs;
2104 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2106 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)
2108 FLAC__uint32 lsbs = 0, msbs = 0;
2111 FLAC__ASSERT(0 != bb);
2112 FLAC__ASSERT(0 != bb->buffer);
2113 FLAC__ASSERT(parameter <= 31);
2115 /* read the unary MSBs and end bit */
2116 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2119 /* read the binary LSBs */
2120 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2123 /* compose the value */
2124 uval = (msbs << parameter) | lsbs;
2126 *val = -((int)(uval >> 1)) - 1;
2128 *val = (int)(uval >> 1);
2133 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)
2135 const FLAC__blurb *buffer = bb->buffer;
2137 unsigned i, j, val_i = nvals;
2138 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2139 FLAC__blurb blurb, save_blurb;
2140 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2142 FLAC__ASSERT(0 != bb);
2143 FLAC__ASSERT(0 != bb->buffer);
2144 FLAC__ASSERT(parameter <= 31);
2149 cbits = bb->consumed_bits;
2150 i = bb->consumed_blurbs;
2152 for( ; i < bb->blurbs; i++) {
2153 blurb = (save_blurb = buffer[i]) << cbits;
2157 j = FLAC__ALIGNED_BLURB_UNARY(blurb);
2163 lsbs_left = parameter;
2165 if(cbits == FLAC__BITS_PER_BLURB) {
2167 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2173 msbs += FLAC__BITS_PER_BLURB - cbits;
2175 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2180 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2181 if(lsbs_left >= available_bits) {
2182 uval <<= available_bits;
2183 uval |= (blurb >> cbits);
2185 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2187 if(lsbs_left == available_bits) {
2188 /* compose the value */
2189 uval |= (msbs << parameter);
2190 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2202 lsbs_left -= available_bits;
2208 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2209 blurb <<= lsbs_left;
2211 /* compose the value */
2212 uval |= (msbs << parameter);
2213 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2226 bb->consumed_blurbs = i;
2227 bb->consumed_bits = cbits;
2228 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2230 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2232 /* these must be zero because we can only get here if we got to the end of the buffer */
2233 FLAC__ASSERT(bb->consumed_blurbs == 0);
2234 FLAC__ASSERT(bb->consumed_bits == 0);
2243 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)
2245 FLAC__uint32 lsbs = 0, msbs = 0;
2246 unsigned bit, uval, k;
2248 FLAC__ASSERT(0 != bb);
2249 FLAC__ASSERT(0 != bb->buffer);
2251 k = FLAC__bitmath_ilog2(parameter);
2253 /* read the unary MSBs and end bit */
2254 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2257 /* read the binary LSBs */
2258 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2261 if(parameter == 1u<<k) {
2262 /* compose the value */
2263 uval = (msbs << k) | lsbs;
2266 unsigned d = (1 << (k+1)) - parameter;
2268 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2274 /* compose the value */
2275 uval = msbs * parameter + lsbs;
2278 /* unfold unsigned to signed */
2280 *val = -((int)(uval >> 1)) - 1;
2282 *val = (int)(uval >> 1);
2287 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)
2289 FLAC__uint32 lsbs, msbs = 0;
2292 FLAC__ASSERT(0 != bb);
2293 FLAC__ASSERT(0 != bb->buffer);
2295 k = FLAC__bitmath_ilog2(parameter);
2297 /* read the unary MSBs and end bit */
2298 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2301 /* read the binary LSBs */
2302 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2305 if(parameter == 1u<<k) {
2306 /* compose the value */
2307 *val = (msbs << k) | lsbs;
2310 unsigned d = (1 << (k+1)) - parameter;
2312 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2318 /* compose the value */
2319 *val = msbs * parameter + lsbs;
2326 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2327 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)
2333 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2336 raw[(*rawlen)++] = (FLAC__byte)x;
2337 if(!(x & 0x80)) { /* 0xxxxxxx */
2341 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2345 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2349 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2353 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2357 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2366 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2369 raw[(*rawlen)++] = (FLAC__byte)x;
2370 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2381 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2382 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)
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 */
2416 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2421 *val = FLAC__U64L(0xffffffffffffffff);
2425 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2428 raw[(*rawlen)++] = (FLAC__byte)x;
2429 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2430 *val = FLAC__U64L(0xffffffffffffffff);
2440 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2444 fprintf(out, "bitbuffer is NULL\n");
2447 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);
2449 for(i = 0; i < bb->blurbs; i++) {
2450 fprintf(out, "%08X: ", i);
2451 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2452 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2455 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2459 fprintf(out, "%08X: ", i);
2460 for(j = 0; j < bb->bits; j++)
2461 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2464 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);