1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005 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 * Some optimization strategies are slower with older versions of MSVC
50 #if defined _MSC_VER && _MSC_VER <= 1200
51 #define FLAC__OLD_MSVC_FLAVOR
55 * This should be at least twice as large as the largest number of blurbs
56 * required to represent any 'number' (in any encoding) you are going to
57 * read. With FLAC this is on the order of maybe a few hundred bits.
58 * If the buffer is smaller than that, the decoder won't be able to read
59 * in a whole number that is in a variable length encoding (e.g. Rice).
61 * The number we are actually using here is based on what would be the
62 * approximate maximum size of a verbatim frame at the default block size,
63 * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
64 * 32kbytes sounds reasonable. For kicks we subtract out 64 bytes for any
65 * alignment or malloc overhead.
67 * Increase this number to decrease the number of read callbacks, at the
68 * expense of using more memory. Or decrease for the reverse effect,
69 * keeping in mind the limit from the first paragraph.
71 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
73 #ifndef FLAC__OLD_MSVC_FLAVOR
74 static const unsigned char byte_to_unary_table[] = {
75 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
76 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
77 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
78 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
82 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94 #if FLAC__BITS_PER_BLURB == 8
95 #define FLAC__BITS_PER_BLURB_LOG2 3
96 #define FLAC__BYTES_PER_BLURB 1
97 #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
98 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
99 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
100 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
101 #ifndef FLAC__OLD_MSVC_FLAVOR
102 #define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
104 #elif FLAC__BITS_PER_BLURB == 32
105 #define FLAC__BITS_PER_BLURB_LOG2 5
106 #define FLAC__BYTES_PER_BLURB 4
107 #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
108 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
109 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
110 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
111 #ifndef FLAC__OLD_MSVC_FLAVOR
112 #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])))
115 /* ERROR, only sizes of 8 and 32 are supported */
118 #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
123 #define min(x,y) ((x)<(y)?(x):(y))
127 #define max(x,y) ((x)>(y)?(x):(y))
129 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
131 #define FLAC__U64L(x) x
133 #define FLAC__U64L(x) x##LLU
140 struct FLAC__BitBuffer {
142 unsigned capacity; /* in blurbs */
143 unsigned blurbs, bits;
144 unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
145 unsigned consumed_blurbs, consumed_bits;
146 unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
147 FLAC__uint16 read_crc16;
148 #if FLAC__BITS_PER_BLURB == 32
149 unsigned crc16_align;
151 FLAC__blurb save_head, save_tail;
154 #if FLAC__BITS_PER_BLURB == 32
155 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
157 if(bb->crc16_align == 0) {
158 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
159 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
160 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
161 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
163 else if(bb->crc16_align == 8) {
164 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
165 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
166 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
168 else if(bb->crc16_align == 16) {
169 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
170 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
172 else if(bb->crc16_align == 24) {
173 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
180 * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
181 * does not shift left what is consumed, it just chops off the end, whether
182 * there is unconsumed data there or not. This is OK because currently we
183 * never shrink the buffer, but if this ever changes, we'll have to do some
186 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
188 FLAC__blurb *new_buffer;
190 FLAC__ASSERT(0 != bb);
191 FLAC__ASSERT(0 != bb->buffer);
193 if(bb->capacity == new_capacity)
196 new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
199 memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
200 if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
201 bb->blurbs = new_capacity;
203 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
205 if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
206 bb->consumed_blurbs = new_capacity;
207 bb->consumed_bits = 0;
208 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
210 free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
211 bb->buffer = new_buffer;
212 bb->capacity = new_capacity;
216 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
218 unsigned new_capacity;
220 FLAC__ASSERT(min_blurbs_to_add > 0);
222 new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
223 return bitbuffer_resize_(bb, new_capacity);
226 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
228 FLAC__ASSERT(0 != bb);
229 FLAC__ASSERT(0 != bb->buffer);
231 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
232 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
237 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)
242 /* first shift the unconsumed buffer data toward the front as much as possible */
243 if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
244 #if FLAC__BITS_PER_BLURB == 8
246 * memset and memcpy are usually implemented in assembly language
247 * by the system libc, and they can be much faster
249 const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
250 const unsigned r = bb->consumed_blurbs, l = r_end - r;
251 memmove(&bb->buffer[0], &bb->buffer[r], l);
252 memset(&bb->buffer[l], 0, r);
253 #elif FLAC__BITS_PER_BLURB == 32
254 /* still needs optimization */
255 const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
256 unsigned l = 0, r = bb->consumed_blurbs;
257 for( ; r < r_end; l++, r++)
258 bb->buffer[l] = bb->buffer[r];
259 for( ; l < r_end; l++)
262 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
263 #endif /* FLAC__BITS_PER_BLURB == 32 or 8 */
265 bb->blurbs -= bb->consumed_blurbs;
266 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
267 bb->consumed_blurbs = 0;
268 bb->total_consumed_bits = bb->consumed_bits;
271 /* grow if we need to */
272 if(bb->capacity <= 1) {
273 if(!bitbuffer_resize_(bb, 16))
277 /* set the target for reading, taking into account blurb alignment */
278 #if FLAC__BITS_PER_BLURB == 8
279 /* blurb == byte, so no gyrations necessary: */
280 target = bb->buffer + bb->blurbs;
281 bytes = bb->capacity - bb->blurbs;
282 #elif FLAC__BITS_PER_BLURB == 32
283 /* @@@ WATCHOUT: code currently only works for big-endian: */
284 FLAC__ASSERT((bb->bits & 7) == 0);
285 target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
286 bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
288 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
291 /* finally, read in some data */
292 if(!read_callback(target, &bytes, client_data))
295 /* now we have to handle partial blurb cases: */
296 #if FLAC__BITS_PER_BLURB == 8
297 /* blurb == byte, so no gyrations necessary: */
299 bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
300 #elif FLAC__BITS_PER_BLURB == 32
301 /* @@@ WATCHOUT: code currently only works for big-endian: */
303 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
304 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
305 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
306 bb->total_bits += (bytes << 3);
309 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
314 /***********************************************************************
316 * Class constructor/destructor
318 ***********************************************************************/
320 FLAC__BitBuffer *FLAC__bitbuffer_new()
322 FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
325 memset(bb, 0, sizeof(FLAC__BitBuffer));
328 bb->blurbs = bb->bits = bb->total_bits = 0;
329 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
334 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
336 FLAC__ASSERT(0 != bb);
338 FLAC__bitbuffer_free(bb);
342 /***********************************************************************
344 * Public class methods
346 ***********************************************************************/
348 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
350 FLAC__ASSERT(0 != bb);
354 bb->blurbs = bb->bits = bb->total_bits = 0;
355 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
357 return FLAC__bitbuffer_clear(bb);
360 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
362 FLAC__ASSERT(0 != bb);
363 FLAC__ASSERT(bytes > 0);
365 if(!FLAC__bitbuffer_init(bb))
368 if(!bitbuffer_ensure_size_(bb, bytes << 3))
371 FLAC__ASSERT(0 != buffer);
372 /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
373 memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
374 bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
375 bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
376 bb->total_bits = bytes << 3;
380 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
382 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
384 FLAC__ASSERT(0 != dest);
385 FLAC__ASSERT(0 != src);
389 if(dest->bits != src->consumed_bits)
391 if(!bitbuffer_ensure_size_(dest, bits_to_add))
393 if(dest->bits == 0) {
394 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
396 else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
397 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
398 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
399 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)));
402 dest->buffer[dest->blurbs] <<= bits_to_add;
403 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
405 dest->bits = src->bits;
406 dest->total_bits += bits_to_add;
407 dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
412 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
414 FLAC__ASSERT(0 != bb);
420 bb->blurbs = bb->bits = bb->total_bits = 0;
421 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
424 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
426 if(bb->buffer == 0) {
427 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
428 bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
433 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
435 bb->blurbs = bb->bits = bb->total_bits = 0;
436 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
440 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
442 FLAC__ASSERT(0 != dest);
443 FLAC__ASSERT(0 != dest->buffer);
444 FLAC__ASSERT(0 != src);
445 FLAC__ASSERT(0 != src->buffer);
447 if(dest->capacity < src->capacity)
448 if(!bitbuffer_resize_(dest, src->capacity))
450 memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
451 dest->blurbs = src->blurbs;
452 dest->bits = src->bits;
453 dest->total_bits = src->total_bits;
454 dest->consumed_blurbs = src->consumed_blurbs;
455 dest->consumed_bits = src->consumed_bits;
456 dest->total_consumed_bits = src->total_consumed_bits;
457 dest->read_crc16 = src->read_crc16;
461 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
463 FLAC__ASSERT(0 != bb);
464 FLAC__ASSERT(0 != bb->buffer);
465 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
467 bb->read_crc16 = seed;
468 #if FLAC__BITS_PER_BLURB == 8
469 /* no need to do anything */
470 #elif FLAC__BITS_PER_BLURB == 32
471 bb->crc16_align = bb->consumed_bits;
473 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
477 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
479 FLAC__ASSERT(0 != bb);
480 FLAC__ASSERT(0 != bb->buffer);
481 FLAC__ASSERT((bb->bits & 7) == 0);
482 FLAC__ASSERT((bb->consumed_bits & 7) == 0);
484 #if FLAC__BITS_PER_BLURB == 8
485 /* no need to do anything */
486 #elif FLAC__BITS_PER_BLURB == 32
487 /*@@@ 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 */
488 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
489 if(bb->consumed_bits == 8) {
490 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
491 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
493 else if(bb->consumed_bits == 16) {
494 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
495 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
496 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
498 else if(bb->consumed_bits == 24) {
499 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
500 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
501 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
502 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
506 if(bb->consumed_bits == 8) {
507 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
508 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
510 else if(bb->consumed_bits == 16) {
511 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
512 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
513 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
515 else if(bb->consumed_bits == 24) {
516 const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
517 FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
518 FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
519 FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
522 bb->crc16_align = bb->consumed_bits;
524 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
526 return bb->read_crc16;
529 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
531 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
533 #if FLAC__BITS_PER_BLURB == 8
534 return FLAC__crc16(bb->buffer, bb->blurbs);
535 #elif FLAC__BITS_PER_BLURB == 32
536 /* @@@ WATCHOUT: code currently only works for big-endian: */
537 return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
539 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
543 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
545 FLAC__ASSERT(0 != bb);
546 FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
547 FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
548 #if FLAC__BITS_PER_BLURB == 8
549 return FLAC__crc8(bb->buffer, bb->blurbs);
550 #elif FLAC__BITS_PER_BLURB == 32
551 /* @@@ WATCHOUT: code currently only works for big-endian: */
552 return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
554 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
558 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
560 return ((bb->bits & 7) == 0);
563 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
565 return ((bb->consumed_bits & 7) == 0);
568 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
570 return 8 - (bb->consumed_bits & 7);
573 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
575 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
576 return (bb->total_bits - bb->total_consumed_bits) >> 3;
579 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
581 FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
582 #if FLAC__BITS_PER_BLURB == 8
583 *buffer = bb->buffer + bb->consumed_blurbs;
584 *bytes = bb->blurbs - bb->consumed_blurbs;
585 #elif FLAC__BITS_PER_BLURB == 32
586 /* @@@ WATCHOUT: code currently only works for big-endian: */
587 *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
588 *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
590 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
594 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
596 #if FLAC__BITS_PER_BLURB == 8
598 #elif FLAC__BITS_PER_BLURB == 32
599 /* @@@ WATCHOUT: code currently only works for big-endian: */
602 FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
606 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
610 FLAC__ASSERT(0 != bb);
611 FLAC__ASSERT(0 != bb->buffer);
615 if(!bitbuffer_ensure_size_(bb, bits))
617 bb->total_bits += bits;
619 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
620 bb->buffer[bb->blurbs] <<= n;
623 if(bb->bits == FLAC__BITS_PER_BLURB) {
631 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
635 FLAC__ASSERT(0 != bb);
636 FLAC__ASSERT(0 != bb->buffer);
638 FLAC__ASSERT(bits <= 32);
641 /* inline the size check so we don't incure a function call unnecessarily */
642 if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
643 if(!bitbuffer_ensure_size_(bb, bits))
647 /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
648 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
649 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
651 bb->total_bits += bits;
653 n = FLAC__BITS_PER_BLURB - bb->bits;
654 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
655 if(bits < FLAC__BITS_PER_BLURB) {
656 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
660 else if(bits == FLAC__BITS_PER_BLURB) {
661 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
665 k = bits - FLAC__BITS_PER_BLURB;
666 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
667 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
668 val &= (~(0xffffffff << k));
669 bits -= FLAC__BITS_PER_BLURB;
673 bb->buffer[bb->blurbs] <<= bits;
674 bb->buffer[bb->blurbs] |= val;
685 bb->buffer[bb->blurbs] <<= n;
686 bb->buffer[bb->blurbs] |= (val >> k);
687 /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
688 val &= (~(0xffffffff << k));
698 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
700 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
703 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
705 static const FLAC__uint64 mask[] = {
707 FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
708 FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
709 FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
710 FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
711 FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
712 FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
713 FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
714 FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
715 FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
716 FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
717 FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
718 FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
719 FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
720 FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
721 FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
722 FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
726 FLAC__ASSERT(0 != bb);
727 FLAC__ASSERT(0 != bb->buffer);
729 FLAC__ASSERT(bits <= 64);
732 if(!bitbuffer_ensure_size_(bb, bits))
735 bb->total_bits += bits;
738 if(bits < FLAC__BITS_PER_BLURB) {
739 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
743 else if(bits == FLAC__BITS_PER_BLURB) {
744 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
748 k = bits - FLAC__BITS_PER_BLURB;
749 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
750 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
751 val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
752 bits -= FLAC__BITS_PER_BLURB;
756 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
758 bb->buffer[bb->blurbs] <<= n;
759 bb->buffer[bb->blurbs] |= (val >> k);
760 /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
761 val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
764 if(bb->bits == FLAC__BITS_PER_BLURB) {
775 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
777 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
781 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
783 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
785 /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
786 if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
788 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
790 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
792 if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
798 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
802 /* this could be faster but currently we don't need it to be */
803 for(i = 0; i < nvals; i++) {
804 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
811 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
814 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
816 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
818 if(!FLAC__bitbuffer_write_zeroes(bb, val))
820 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
824 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
828 /* fold signed to unsigned */
831 * (unsigned)(((--val) << 1) - 1);
832 * but without the overflow problem at MININT
834 uval = (unsigned)(((-(++val)) << 1) + 1);
836 uval = (unsigned)(val << 1);
838 msbs = uval >> parameter;
840 return 1 + parameter + msbs;
844 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
846 unsigned bits, msbs, uval;
849 FLAC__ASSERT(parameter > 0);
851 /* fold signed to unsigned */
854 * (unsigned)(((--val) << 1) - 1);
855 * but without the overflow problem at MININT
857 uval = (unsigned)(((-(++val)) << 1) + 1);
859 uval = (unsigned)(val << 1);
861 k = FLAC__bitmath_ilog2(parameter);
862 if(parameter == 1u<<k) {
863 FLAC__ASSERT(k <= 30);
871 d = (1 << (k+1)) - parameter;
872 q = uval / parameter;
873 r = uval - (q * parameter);
882 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
887 FLAC__ASSERT(parameter > 0);
889 k = FLAC__bitmath_ilog2(parameter);
890 if(parameter == 1u<<k) {
891 FLAC__ASSERT(k <= 30);
899 d = (1 << (k+1)) - parameter;
900 q = uval / parameter;
901 r = uval - (q * parameter);
911 #ifdef FLAC__SYMMETRIC_RICE
912 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
914 unsigned total_bits, interesting_bits, msbs;
915 FLAC__uint32 pattern;
917 FLAC__ASSERT(0 != bb);
918 FLAC__ASSERT(0 != bb->buffer);
919 FLAC__ASSERT(parameter <= 31);
921 /* init pattern with the unary end bit and the sign bit */
929 msbs = val >> parameter;
930 interesting_bits = 2 + parameter;
931 total_bits = interesting_bits + msbs;
932 pattern <<= parameter;
933 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
935 if(total_bits <= 32) {
936 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
940 /* write the unary MSBs */
941 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
943 /* write the unary end bit, the sign bit, and binary LSBs */
944 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
951 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
953 unsigned total_bits, interesting_bits, msbs;
954 FLAC__uint32 pattern;
956 FLAC__ASSERT(0 != bb);
957 FLAC__ASSERT(0 != bb->buffer);
958 FLAC__ASSERT(parameter <= 31);
962 /* init pattern with the unary end bit and the sign bit */
970 msbs = val >> parameter;
971 interesting_bits = 2 + parameter;
972 total_bits = interesting_bits + msbs;
973 pattern <<= parameter;
974 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
976 if(total_bits <= 32) {
977 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
980 else if(total_bits > max_bits) {
985 /* write the unary MSBs */
986 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
988 /* write the unary end bit, the sign bit, and binary LSBs */
989 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
996 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
998 unsigned total_bits, val_bits;
999 FLAC__uint32 pattern;
1001 FLAC__ASSERT(0 != bb);
1002 FLAC__ASSERT(0 != bb->buffer);
1003 FLAC__ASSERT(parameter <= 31);
1005 val_bits = FLAC__bitmath_silog2(val);
1006 total_bits = 2 + parameter + 5 + val_bits;
1008 if(total_bits <= 32) {
1010 pattern <<= (parameter + 5);
1011 pattern |= val_bits;
1012 pattern <<= val_bits;
1013 pattern |= (val & ((1 << val_bits) - 1));
1014 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1018 /* write the '-0' escape code first */
1019 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
1021 /* write the length */
1022 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
1024 /* write the value */
1025 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
1030 #endif /* ifdef FLAC__SYMMETRIC_RICE */
1032 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1034 unsigned total_bits, interesting_bits, msbs, uval;
1035 FLAC__uint32 pattern;
1037 FLAC__ASSERT(0 != bb);
1038 FLAC__ASSERT(0 != bb->buffer);
1039 FLAC__ASSERT(parameter <= 30);
1041 /* fold signed to unsigned */
1044 * (unsigned)(((--val) << 1) - 1);
1045 * but without the overflow problem at MININT
1047 uval = (unsigned)(((-(++val)) << 1) + 1);
1049 uval = (unsigned)(val << 1);
1051 msbs = uval >> parameter;
1052 interesting_bits = 1 + parameter;
1053 total_bits = interesting_bits + msbs;
1054 pattern = 1 << parameter; /* the unary end bit */
1055 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1057 if(total_bits <= 32) {
1058 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1062 /* write the unary MSBs */
1063 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1065 /* write the unary end bit and binary LSBs */
1066 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1073 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1075 unsigned total_bits, interesting_bits, msbs, uval;
1076 FLAC__uint32 pattern;
1078 FLAC__ASSERT(0 != bb);
1079 FLAC__ASSERT(0 != bb->buffer);
1080 FLAC__ASSERT(parameter <= 30);
1084 /* fold signed to unsigned */
1087 * (unsigned)(((--val) << 1) - 1);
1088 * but without the overflow problem at MININT
1090 uval = (unsigned)(((-(++val)) << 1) + 1);
1092 uval = (unsigned)(val << 1);
1094 msbs = uval >> parameter;
1095 interesting_bits = 1 + parameter;
1096 total_bits = interesting_bits + msbs;
1097 pattern = 1 << parameter; /* the unary end bit */
1098 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1100 if(total_bits <= 32) {
1101 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1104 else if(total_bits > max_bits) {
1109 /* write the unary MSBs */
1110 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1112 /* write the unary end bit and binary LSBs */
1113 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1121 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1123 unsigned total_bits, msbs, uval;
1126 FLAC__ASSERT(0 != bb);
1127 FLAC__ASSERT(0 != bb->buffer);
1128 FLAC__ASSERT(parameter > 0);
1130 /* fold signed to unsigned */
1133 * (unsigned)(((--val) << 1) - 1);
1134 * but without the overflow problem at MININT
1136 uval = (unsigned)(((-(++val)) << 1) + 1);
1138 uval = (unsigned)(val << 1);
1140 k = FLAC__bitmath_ilog2(parameter);
1141 if(parameter == 1u<<k) {
1144 FLAC__ASSERT(k <= 30);
1147 total_bits = 1 + k + msbs;
1148 pattern = 1 << k; /* the unary end bit */
1149 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1151 if(total_bits <= 32) {
1152 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1156 /* write the unary MSBs */
1157 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1159 /* write the unary end bit and binary LSBs */
1160 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1167 d = (1 << (k+1)) - parameter;
1168 q = uval / parameter;
1169 r = uval - (q * parameter);
1170 /* write the unary MSBs */
1171 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1173 /* write the unary end bit */
1174 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1176 /* write the binary LSBs */
1178 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1182 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1189 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1191 unsigned total_bits, msbs;
1194 FLAC__ASSERT(0 != bb);
1195 FLAC__ASSERT(0 != bb->buffer);
1196 FLAC__ASSERT(parameter > 0);
1198 k = FLAC__bitmath_ilog2(parameter);
1199 if(parameter == 1u<<k) {
1202 FLAC__ASSERT(k <= 30);
1205 total_bits = 1 + k + msbs;
1206 pattern = 1 << k; /* the unary end bit */
1207 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1209 if(total_bits <= 32) {
1210 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1214 /* write the unary MSBs */
1215 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1217 /* write the unary end bit and binary LSBs */
1218 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1225 d = (1 << (k+1)) - parameter;
1226 q = uval / parameter;
1227 r = uval - (q * parameter);
1228 /* write the unary MSBs */
1229 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1231 /* write the unary end bit */
1232 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1234 /* write the binary LSBs */
1236 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1240 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1248 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1252 FLAC__ASSERT(0 != bb);
1253 FLAC__ASSERT(0 != bb->buffer);
1255 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1258 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1260 else if(val < 0x800) {
1261 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1262 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1264 else if(val < 0x10000) {
1265 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 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);
1269 else if(val < 0x200000) {
1270 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1271 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1272 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1273 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1275 else if(val < 0x4000000) {
1276 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1277 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1278 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1279 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1280 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1283 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1284 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1285 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1286 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1287 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1288 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1294 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1298 FLAC__ASSERT(0 != bb);
1299 FLAC__ASSERT(0 != bb->buffer);
1301 FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
1304 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1306 else if(val < 0x800) {
1307 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1308 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1310 else if(val < 0x10000) {
1311 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 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 < 0x200000) {
1316 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1317 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1318 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1319 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1321 else if(val < 0x4000000) {
1322 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1323 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1324 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1325 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1326 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1328 else if(val < 0x80000000) {
1329 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1330 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1331 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1332 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1333 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1334 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1337 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1338 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1339 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1340 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1341 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1342 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1343 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1349 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1351 /* 0-pad to byte boundary */
1353 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1358 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)
1360 /* to avoid a drastic speed penalty we don't:
1361 FLAC__ASSERT(0 != bb);
1362 FLAC__ASSERT(0 != bb->buffer);
1363 FLAC__ASSERT(bb->bits == 0);
1367 if(bb->total_consumed_bits < bb->total_bits) {
1368 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1372 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1378 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)
1380 /* to avoid a drastic speed penalty we don't:
1381 FLAC__ASSERT(0 != bb);
1382 FLAC__ASSERT(0 != bb->buffer);
1383 FLAC__ASSERT(bb->bits == 0);
1387 if(bb->total_consumed_bits < bb->total_bits) {
1388 *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1389 bb->consumed_bits++;
1390 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1391 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1392 bb->consumed_blurbs++;
1393 bb->consumed_bits = 0;
1395 bb->total_consumed_bits++;
1399 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1405 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)
1407 /* to avoid a drastic speed penalty we don't:
1408 FLAC__ASSERT(0 != bb);
1409 FLAC__ASSERT(0 != bb->buffer);
1410 FLAC__ASSERT(bb->bits == 0);
1414 if(bb->total_consumed_bits < bb->total_bits) {
1416 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1417 bb->consumed_bits++;
1418 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1419 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1420 bb->consumed_blurbs++;
1421 bb->consumed_bits = 0;
1423 bb->total_consumed_bits++;
1427 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1433 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)
1435 /* to avoid a drastic speed penalty we don't:
1436 FLAC__ASSERT(0 != bb);
1437 FLAC__ASSERT(0 != bb->buffer);
1438 FLAC__ASSERT(bb->bits == 0);
1442 if(bb->total_consumed_bits < bb->total_bits) {
1444 *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1445 bb->consumed_bits++;
1446 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1447 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1448 bb->consumed_blurbs++;
1449 bb->consumed_bits = 0;
1451 bb->total_consumed_bits++;
1455 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1461 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)
1462 #ifdef FLAC__NO_MANUAL_INLINING
1466 FLAC__ASSERT(0 != bb);
1467 FLAC__ASSERT(0 != bb->buffer);
1469 FLAC__ASSERT(bits <= 32);
1472 for(i = 0; i < bits; i++) {
1473 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1480 unsigned i, bits_ = bits;
1483 FLAC__ASSERT(0 != bb);
1484 FLAC__ASSERT(0 != bb->buffer);
1486 FLAC__ASSERT(bits <= 32);
1487 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1494 while(bb->total_consumed_bits + bits > bb->total_bits) {
1495 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1498 #if FLAC__BITS_PER_BLURB > 8
1499 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1501 if(bb->consumed_bits) {
1502 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1504 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1506 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1507 bb->consumed_blurbs++;
1508 bb->consumed_bits = 0;
1509 /* we hold off updating bb->total_consumed_bits until the end */
1512 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1513 bb->consumed_bits += bits_;
1514 bb->total_consumed_bits += bits_;
1518 #if FLAC__BITS_PER_BLURB == 32
1519 /* note that we know bits_ cannot be > 32 because of previous assertions */
1520 if(bits_ == FLAC__BITS_PER_BLURB) {
1521 v = bb->buffer[bb->consumed_blurbs];
1522 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1523 bb->consumed_blurbs++;
1524 /* bb->consumed_bits is already 0 */
1525 bb->total_consumed_bits += bits;
1530 while(bits_ >= FLAC__BITS_PER_BLURB) {
1531 v <<= FLAC__BITS_PER_BLURB;
1532 v |= bb->buffer[bb->consumed_blurbs];
1533 bits_ -= FLAC__BITS_PER_BLURB;
1534 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1535 bb->consumed_blurbs++;
1536 /* bb->consumed_bits is already 0 */
1537 /* we hold off updating bb->total_consumed_bits until the end */
1542 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1543 bb->consumed_bits = bits_;
1544 /* we hold off updating bb->total_consumed_bits until the end */
1546 bb->total_consumed_bits += bits;
1548 #if FLAC__BITS_PER_BLURB > 8
1552 for(i = 0; i < bits; i++) {
1553 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1562 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)
1563 #ifdef FLAC__NO_MANUAL_INLINING
1568 FLAC__ASSERT(0 != bb);
1569 FLAC__ASSERT(0 != bb->buffer);
1571 FLAC__ASSERT(bits <= 32);
1579 for(i = 0; i < bits; i++) {
1580 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1588 *val = (FLAC__int32)v;
1592 *val = (FLAC__int32)v;
1598 unsigned i, bits_ = bits;
1601 FLAC__ASSERT(0 != bb);
1602 FLAC__ASSERT(0 != bb->buffer);
1604 FLAC__ASSERT(bits <= 32);
1605 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1612 while(bb->total_consumed_bits + bits > bb->total_bits) {
1613 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1616 #if FLAC__BITS_PER_BLURB > 8
1617 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1619 if(bb->consumed_bits) {
1620 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1622 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1624 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1625 bb->consumed_blurbs++;
1626 bb->consumed_bits = 0;
1627 /* we hold off updating bb->total_consumed_bits until the end */
1630 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1631 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1633 *val = (FLAC__int32)v;
1634 *val >>= (32-bits_);
1635 bb->consumed_bits += bits_;
1636 bb->total_consumed_bits += bits_;
1640 #if FLAC__BITS_PER_BLURB == 32
1641 /* note that we know bits_ cannot be > 32 because of previous assertions */
1642 if(bits_ == FLAC__BITS_PER_BLURB) {
1643 v = bb->buffer[bb->consumed_blurbs];
1645 CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1646 bb->consumed_blurbs++;
1647 /* bb->consumed_bits is already 0 */
1648 /* we hold off updating bb->total_consumed_bits until the end */
1651 while(bits_ >= FLAC__BITS_PER_BLURB) {
1652 v <<= FLAC__BITS_PER_BLURB;
1653 v |= bb->buffer[bb->consumed_blurbs];
1654 bits_ -= FLAC__BITS_PER_BLURB;
1655 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1656 bb->consumed_blurbs++;
1657 /* bb->consumed_bits is already 0 */
1658 /* we hold off updating bb->total_consumed_bits until the end */
1663 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1664 bb->consumed_bits = bits_;
1665 /* we hold off updating bb->total_consumed_bits until the end */
1667 bb->total_consumed_bits += bits;
1668 #if FLAC__BITS_PER_BLURB > 8
1671 for(i = 0; i < bits; i++) {
1672 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1682 *val = (FLAC__int32)v;
1686 *val = (FLAC__int32)v;
1692 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)
1693 #ifdef FLAC__NO_MANUAL_INLINING
1697 FLAC__ASSERT(0 != bb);
1698 FLAC__ASSERT(0 != bb->buffer);
1700 FLAC__ASSERT(bits <= 64);
1703 for(i = 0; i < bits; i++) {
1704 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1711 unsigned i, bits_ = bits;
1714 FLAC__ASSERT(0 != bb);
1715 FLAC__ASSERT(0 != bb->buffer);
1717 FLAC__ASSERT(bits <= 64);
1718 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1725 while(bb->total_consumed_bits + bits > bb->total_bits) {
1726 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1729 #if FLAC__BITS_PER_BLURB > 8
1730 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1732 if(bb->consumed_bits) {
1733 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1735 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1737 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1738 bb->consumed_blurbs++;
1739 bb->consumed_bits = 0;
1740 /* we hold off updating bb->total_consumed_bits until the end */
1743 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1744 bb->consumed_bits += bits_;
1745 bb->total_consumed_bits += bits_;
1749 while(bits_ >= FLAC__BITS_PER_BLURB) {
1750 v <<= FLAC__BITS_PER_BLURB;
1751 v |= bb->buffer[bb->consumed_blurbs];
1752 bits_ -= FLAC__BITS_PER_BLURB;
1753 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1754 bb->consumed_blurbs++;
1755 /* bb->consumed_bits is already 0 */
1756 /* we hold off updating bb->total_consumed_bits until the end */
1760 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1761 bb->consumed_bits = bits_;
1762 /* we hold off updating bb->total_consumed_bits until the end */
1764 bb->total_consumed_bits += bits;
1766 #if FLAC__BITS_PER_BLURB > 8
1770 for(i = 0; i < bits; i++) {
1771 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1781 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)
1782 #ifdef FLAC__NO_MANUAL_INLINING
1787 FLAC__ASSERT(0 != bb);
1788 FLAC__ASSERT(0 != bb->buffer);
1790 FLAC__ASSERT(bits <= 64);
1793 for(i = 0; i < bits; i++) {
1794 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1801 *val = (FLAC__int64)v;
1805 *val = (FLAC__int64)v;
1811 unsigned i, bits_ = bits;
1814 FLAC__ASSERT(0 != bb);
1815 FLAC__ASSERT(0 != bb->buffer);
1817 FLAC__ASSERT(bits <= 64);
1818 FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1825 while(bb->total_consumed_bits + bits > bb->total_bits) {
1826 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1829 #if FLAC__BITS_PER_BLURB > 8
1830 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1832 if(bb->consumed_bits) {
1833 i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1835 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1837 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1838 bb->consumed_blurbs++;
1839 bb->consumed_bits = 0;
1840 /* we hold off updating bb->total_consumed_bits until the end */
1843 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1844 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1846 *val = (FLAC__int64)v;
1847 *val >>= (64-bits_);
1848 bb->consumed_bits += bits_;
1849 bb->total_consumed_bits += bits_;
1853 while(bits_ >= FLAC__BITS_PER_BLURB) {
1854 v <<= FLAC__BITS_PER_BLURB;
1855 v |= bb->buffer[bb->consumed_blurbs];
1856 bits_ -= FLAC__BITS_PER_BLURB;
1857 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1858 bb->consumed_blurbs++;
1859 /* bb->consumed_bits is already 0 */
1860 /* we hold off updating bb->total_consumed_bits until the end */
1864 v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1865 bb->consumed_bits = bits_;
1866 /* we hold off updating bb->total_consumed_bits until the end */
1868 bb->total_consumed_bits += bits;
1869 #if FLAC__BITS_PER_BLURB > 8
1872 for(i = 0; i < bits; i++) {
1873 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1883 *val = (FLAC__int64)v;
1887 *val = (FLAC__int64)v;
1894 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)
1896 FLAC__uint32 x8, x32 = 0;
1898 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1900 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1903 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1907 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1911 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1919 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)
1922 * @@@ a slightly faster implementation is possible but
1923 * probably not that useful since this is only called a
1924 * couple of times in the metadata readers.
1926 FLAC__ASSERT(0 != bb);
1927 FLAC__ASSERT(0 != bb->buffer);
1930 const unsigned n = bb->consumed_bits & 7;
1936 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
1942 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
1947 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
1955 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)
1957 FLAC__ASSERT(0 != bb);
1958 FLAC__ASSERT(0 != bb->buffer);
1959 FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1960 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1961 #if FLAC__BITS_PER_BLURB == 8
1963 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1965 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1970 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1971 val += FLAC__BYTES_PER_BLURB * chunk;
1974 bb->consumed_blurbs += chunk;
1975 bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1979 @@@ need to write this still
1986 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)
1987 #ifdef FLAC__NO_MANUAL_INLINING
1989 unsigned bit, val_ = 0;
1991 FLAC__ASSERT(0 != bb);
1992 FLAC__ASSERT(0 != bb->buffer);
1995 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2007 unsigned i, val_ = 0;
2008 unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
2011 FLAC__ASSERT(0 != bb);
2012 FLAC__ASSERT(0 != bb->buffer);
2014 #if FLAC__BITS_PER_BLURB > 8
2015 if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
2017 if(bb->consumed_bits) {
2018 b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
2020 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
2024 bb->consumed_bits += i;
2025 bb->total_consumed_bits += i;
2026 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
2027 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2028 bb->consumed_blurbs++;
2029 bb->consumed_bits = 0;
2034 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
2035 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2036 bb->consumed_blurbs++;
2037 bb->consumed_bits = 0;
2038 bb->total_consumed_bits += val_;
2042 if(bb->consumed_blurbs >= total_blurbs_) {
2043 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2045 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
2047 b = bb->buffer[bb->consumed_blurbs];
2049 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
2053 bb->consumed_bits = i;
2055 if(i == FLAC__BITS_PER_BLURB) {
2056 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2057 bb->consumed_blurbs++;
2058 bb->consumed_bits = 0;
2060 bb->total_consumed_bits += i;
2064 val_ += FLAC__BITS_PER_BLURB;
2065 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
2066 bb->consumed_blurbs++;
2067 /* bb->consumed_bits is already 0 */
2068 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
2071 #if FLAC__BITS_PER_BLURB > 8
2075 if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
2089 #ifdef FLAC__SYMMETRIC_RICE
2090 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)
2092 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
2094 FLAC__ASSERT(0 != bb);
2095 FLAC__ASSERT(0 != bb->buffer);
2096 FLAC__ASSERT(parameter <= 31);
2098 /* read the unary MSBs and end bit */
2099 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2102 /* read the sign bit */
2103 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
2106 /* read the binary LSBs */
2107 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2110 /* compose the value */
2111 *val = (msbs << parameter) | lsbs;
2117 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2119 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)
2121 FLAC__uint32 lsbs = 0, msbs = 0;
2124 FLAC__ASSERT(0 != bb);
2125 FLAC__ASSERT(0 != bb->buffer);
2126 FLAC__ASSERT(parameter <= 31);
2128 /* read the unary MSBs and end bit */
2129 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2132 /* read the binary LSBs */
2133 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2136 /* compose the value */
2137 uval = (msbs << parameter) | lsbs;
2139 *val = -((int)(uval >> 1)) - 1;
2141 *val = (int)(uval >> 1);
2146 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)
2147 #ifdef FLAC__OLD_MSVC_FLAVOR
2149 const FLAC__blurb *buffer = bb->buffer;
2151 unsigned i, j, val_i = 0;
2152 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2153 FLAC__blurb blurb, save_blurb;
2154 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2156 FLAC__ASSERT(0 != bb);
2157 FLAC__ASSERT(0 != bb->buffer);
2158 FLAC__ASSERT(parameter <= 31);
2163 i = bb->consumed_blurbs;
2165 * We unroll the main loop to take care of partially consumed blurbs here.
2167 if(bb->consumed_bits > 0) {
2168 save_blurb = blurb = buffer[i];
2169 cbits = bb->consumed_bits;
2175 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2179 /* dispose of the unary end bit */
2185 lsbs_left = parameter;
2187 if(cbits == FLAC__BITS_PER_BLURB) {
2189 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2194 msbs += FLAC__BITS_PER_BLURB - cbits;
2196 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2201 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2202 if(lsbs_left >= available_bits) {
2203 uval <<= available_bits;
2204 uval |= (blurb >> cbits);
2206 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2208 if(lsbs_left == available_bits) {
2209 /* compose the value */
2210 uval |= (msbs << parameter);
2212 vals[val_i++] = -((int)(uval >> 1)) - 1;
2214 vals[val_i++] = (int)(uval >> 1);
2222 lsbs_left -= available_bits;
2227 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2228 blurb <<= lsbs_left;
2231 /* compose the value */
2232 uval |= (msbs << parameter);
2234 vals[val_i++] = -((int)(uval >> 1)) - 1;
2236 vals[val_i++] = (int)(uval >> 1);
2237 if(val_i == nvals) {
2238 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2250 bb->consumed_blurbs = i;
2251 bb->consumed_bits = cbits;
2252 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2256 * Now that we are blurb-aligned the logic is slightly simpler
2258 while(val_i < nvals) {
2259 for( ; i < bb->blurbs && val_i < nvals; i++) {
2260 save_blurb = blurb = buffer[i];
2265 for(j = 0; !(blurb & FLAC__BLURB_TOP_BIT_ONE); j++)
2269 /* dispose of the unary end bit */
2275 lsbs_left = parameter;
2277 if(cbits == FLAC__BITS_PER_BLURB) {
2279 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2284 msbs += FLAC__BITS_PER_BLURB - cbits;
2286 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2291 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2292 if(lsbs_left >= available_bits) {
2293 uval <<= available_bits;
2294 uval |= (blurb >> cbits);
2296 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2298 if(lsbs_left == available_bits) {
2299 /* compose the value */
2300 uval |= (msbs << parameter);
2302 vals[val_i++] = -((int)(uval >> 1)) - 1;
2304 vals[val_i++] = (int)(uval >> 1);
2312 lsbs_left -= available_bits;
2317 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2318 blurb <<= lsbs_left;
2321 /* compose the value */
2322 uval |= (msbs << parameter);
2324 vals[val_i++] = -((int)(uval >> 1)) - 1;
2326 vals[val_i++] = (int)(uval >> 1);
2327 if(val_i == nvals) {
2328 /* back up one if we exited the for loop because we read all nvals but the end came in the middle of a blurb */
2339 bb->consumed_blurbs = i;
2340 bb->consumed_bits = cbits;
2341 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2343 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2345 /* these must be zero because we can only get here if we got to the end of the buffer */
2346 FLAC__ASSERT(bb->consumed_blurbs == 0);
2347 FLAC__ASSERT(bb->consumed_bits == 0);
2356 const FLAC__blurb *buffer = bb->buffer;
2358 unsigned i, j, val_i = nvals;
2359 unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2360 FLAC__blurb blurb, save_blurb;
2361 unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2363 FLAC__ASSERT(0 != bb);
2364 FLAC__ASSERT(0 != bb->buffer);
2365 FLAC__ASSERT(parameter <= 31);
2370 cbits = bb->consumed_bits;
2371 i = bb->consumed_blurbs;
2373 for( ; i < bb->blurbs; i++) {
2374 blurb = (save_blurb = buffer[i]) << cbits;
2378 j = FLAC__ALIGNED_BLURB_UNARY(blurb);
2384 lsbs_left = parameter;
2386 if(cbits == FLAC__BITS_PER_BLURB) {
2388 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2394 msbs += FLAC__BITS_PER_BLURB - cbits;
2396 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2401 const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2402 if(lsbs_left >= available_bits) {
2403 uval <<= available_bits;
2404 uval |= (blurb >> cbits);
2406 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2408 if(lsbs_left == available_bits) {
2409 /* compose the value */
2410 uval |= (msbs << parameter);
2411 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2423 lsbs_left -= available_bits;
2429 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2430 blurb <<= lsbs_left;
2432 /* compose the value */
2433 uval |= (msbs << parameter);
2434 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2447 bb->consumed_blurbs = i;
2448 bb->consumed_bits = cbits;
2449 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2451 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2453 /* these must be zero because we can only get here if we got to the end of the buffer */
2454 FLAC__ASSERT(bb->consumed_blurbs == 0);
2455 FLAC__ASSERT(bb->consumed_bits == 0);
2465 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)
2467 FLAC__uint32 lsbs = 0, msbs = 0;
2468 unsigned bit, uval, k;
2470 FLAC__ASSERT(0 != bb);
2471 FLAC__ASSERT(0 != bb->buffer);
2473 k = FLAC__bitmath_ilog2(parameter);
2475 /* read the unary MSBs and end bit */
2476 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2479 /* read the binary LSBs */
2480 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2483 if(parameter == 1u<<k) {
2484 /* compose the value */
2485 uval = (msbs << k) | lsbs;
2488 unsigned d = (1 << (k+1)) - parameter;
2490 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2496 /* compose the value */
2497 uval = msbs * parameter + lsbs;
2500 /* unfold unsigned to signed */
2502 *val = -((int)(uval >> 1)) - 1;
2504 *val = (int)(uval >> 1);
2509 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)
2511 FLAC__uint32 lsbs, msbs = 0;
2514 FLAC__ASSERT(0 != bb);
2515 FLAC__ASSERT(0 != bb->buffer);
2517 k = FLAC__bitmath_ilog2(parameter);
2519 /* read the unary MSBs and end bit */
2520 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2523 /* read the binary LSBs */
2524 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2527 if(parameter == 1u<<k) {
2528 /* compose the value */
2529 *val = (msbs << k) | lsbs;
2532 unsigned d = (1 << (k+1)) - parameter;
2534 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2540 /* compose the value */
2541 *val = msbs * parameter + lsbs;
2548 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2549 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)
2555 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2558 raw[(*rawlen)++] = (FLAC__byte)x;
2559 if(!(x & 0x80)) { /* 0xxxxxxx */
2563 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2567 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2571 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2575 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2579 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2588 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2591 raw[(*rawlen)++] = (FLAC__byte)x;
2592 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2603 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2604 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)
2610 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2613 raw[(*rawlen)++] = (FLAC__byte)x;
2614 if(!(x & 0x80)) { /* 0xxxxxxx */
2618 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2622 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2626 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2630 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2634 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2638 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2643 *val = FLAC__U64L(0xffffffffffffffff);
2647 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2650 raw[(*rawlen)++] = (FLAC__byte)x;
2651 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2652 *val = FLAC__U64L(0xffffffffffffffff);
2662 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2666 fprintf(out, "bitbuffer is NULL\n");
2669 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);
2671 for(i = 0; i < bb->blurbs; i++) {
2672 fprintf(out, "%08X: ", i);
2673 for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2674 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2677 fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2681 fprintf(out, "%08X: ", i);
2682 for(j = 0; j < bb->bits; j++)
2683 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2686 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);