1 /* libFLAC - Free Lossless Audio Coder library
2 * Copyright (C) 2000 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy(), memset() */
23 #include "private/bitbuffer.h"
25 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
30 #define min(x,y) ((x)<(y)?(x):(y))
34 #define max(x,y) ((x)>(y)?(x):(y))
36 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
41 assert(bb->buffer != 0);
43 if(bb->capacity == new_capacity)
46 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
49 memset(new_buffer, 0, new_capacity);
50 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
51 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
52 bb->bytes = new_capacity;
54 bb->total_bits = (new_capacity<<3);
56 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
57 bb->consumed_bytes = new_capacity;
58 bb->consumed_bits = 0;
59 bb->total_consumed_bits = (new_capacity<<3);
61 bb->buffer = new_buffer;
62 bb->capacity = new_capacity;
66 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
68 unsigned new_capacity;
70 assert(min_bytes_to_add > 0);
72 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
73 return bitbuffer_resize_(bb, new_capacity);
76 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
79 assert(bb->buffer != 0);
80 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
81 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
86 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
90 /* first shift the unconsumed buffer data toward the front as much as possible */
91 if(bb->total_consumed_bits >= 8) {
92 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
93 for( ; r < r_end; l++, r++)
94 bb->buffer[l] = bb->buffer[r];
95 for( ; l < r_end; l++)
97 bb->bytes -= bb->consumed_bytes;
98 bb->total_bits -= (bb->consumed_bytes<<3);
99 bb->consumed_bytes = 0;
100 bb->total_consumed_bits = bb->consumed_bits;
102 /* grow if we need to */
103 if(bb->capacity <= 1) {
104 if(!bitbuffer_resize_(bb, 16))
107 /* finally, read in some data; if OK, go back to read_bit_, else fail */
108 bytes = bb->capacity - bb->bytes;
109 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
112 bb->total_bits += (bytes<<3);
116 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
121 bb->bytes = bb->bits = bb->total_bits = 0;
122 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
125 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
128 FLAC__bitbuffer_init(bb);
133 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
136 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
137 bb->capacity = bb->bytes = bytes;
139 bb->total_bits = (bytes<<3);
140 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
145 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
147 static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
148 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
154 if(dest->bits != src->consumed_bits)
156 if(!bitbuffer_ensure_size_(dest, bits_to_add))
158 if(dest->bits == 0) {
159 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
161 else if(dest->bits + bits_to_add > 8) {
162 dest->buffer[dest->bytes] <<= (8 - dest->bits);
163 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
164 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
167 dest->buffer[dest->bytes] <<= bits_to_add;
168 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
170 dest->bits = src->bits;
171 dest->total_bits += bits_to_add;
172 dest->bytes = dest->total_bits / 8;
177 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
184 bb->bytes = bb->bits = bb->total_bits = 0;
185 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
188 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
190 if(bb->buffer == 0) {
191 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
192 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
195 memset(bb->buffer, 0, bb->capacity);
198 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
200 bb->bytes = bb->bits = bb->total_bits = 0;
201 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
205 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
207 if(dest->capacity < src->capacity)
208 if(!bitbuffer_resize_(dest, src->capacity))
210 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
211 dest->bytes = src->bytes;
212 dest->bits = src->bits;
213 dest->total_bits = src->total_bits;
214 dest->consumed_bytes = src->consumed_bytes;
215 dest->consumed_bits = src->consumed_bits;
216 dest->total_consumed_bits = src->total_consumed_bits;
220 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
225 assert(bb->buffer != 0);
229 if(!bitbuffer_ensure_size_(bb, bits))
231 bb->total_bits += bits;
233 n = min(8 - bb->bits, bits);
235 bb->buffer[bb->bytes] <<= n;
246 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
248 static uint32 mask[] = {
250 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
251 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
252 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
253 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
254 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
255 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
256 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
257 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
262 assert(bb->buffer != 0);
267 if(!bitbuffer_ensure_size_(bb, bits))
270 bb->total_bits += bits;
273 if(n == 8) { /* i.e. bb->bits == 0 */
275 bb->buffer[bb->bytes] = val;
280 bb->buffer[bb->bytes++] = val;
285 bb->buffer[bb->bytes++] = val >> k;
286 val &= (~(0xffffffff << k));
291 bb->buffer[bb->bytes] <<= bits;
292 bb->buffer[bb->bytes] |= val;
303 bb->buffer[bb->bytes] <<= n;
304 bb->buffer[bb->bytes] |= (val>>k);
305 val &= (~(0xffffffff << k));
315 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
317 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
320 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
322 static uint64 mask[] = {
324 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
325 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
326 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
327 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
328 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
329 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
330 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
331 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
332 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
333 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
334 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
335 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
336 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
337 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
338 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
339 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
344 assert(bb->buffer != 0);
349 if(!bitbuffer_ensure_size_(bb, bits))
352 bb->total_bits += bits;
356 bb->buffer[bb->bytes] = val;
361 bb->buffer[bb->bytes++] = val;
366 bb->buffer[bb->bytes++] = val >> k;
367 val &= (~(0xffffffffffffffff << k));
372 n = min(8 - bb->bits, bits);
374 bb->buffer[bb->bytes] <<= n;
375 bb->buffer[bb->bytes] |= (val>>k);
376 val &= (~(0xffffffffffffffff << k));
389 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
391 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
394 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
400 assert(bb->buffer != 0);
402 /* init pattern with sign bit */
410 msbs = val >> parameter;
411 bits = 2 + parameter + msbs;
414 pattern = (pattern << parameter) | (val & ((1<<parameter)-1));
415 pattern = (pattern << (msbs+1)) | 1;
416 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
420 /* write the sign bit */
421 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, 1))
423 /* write the binary LSBs */
424 if(!FLAC__bitbuffer_write_raw_uint32(bb, val & ((1<<parameter)-1), parameter))
426 /* write the unary MSBs */
427 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
429 /* write the end bit */
430 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
436 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
442 assert(bb->buffer != 0);
446 /* init pattern with sign bit */
454 msbs = val >> parameter;
455 bits = 2 + parameter + msbs;
458 pattern = (pattern << parameter) | (val & ((1<<parameter)-1));
459 pattern = (pattern << (msbs+1)) | 1;
460 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
463 else if(bits > max_bits) {
468 /* write the sign bit */
469 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, 1))
471 /* write the binary LSBs */
472 if(!FLAC__bitbuffer_write_raw_uint32(bb, val & ((1<<parameter)-1), parameter))
474 /* write the unary MSBs */
475 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
477 /* write the end bit */
478 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
484 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
489 assert(bb->buffer != 0);
491 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
494 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
496 else if(val < 0x800) {
497 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
498 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
500 else if(val < 0x10000) {
501 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
502 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
503 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
505 else if(val < 0x200000) {
506 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
507 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
508 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
509 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
511 else if(val < 0x4000000) {
512 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
513 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
514 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
515 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
516 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
519 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
520 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
521 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
522 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
523 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
524 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
530 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
535 assert(bb->buffer != 0);
537 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
540 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
542 else if(val < 0x800) {
543 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
544 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
546 else if(val < 0x10000) {
547 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
548 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
549 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
551 else if(val < 0x200000) {
552 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
553 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
554 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
555 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
557 else if(val < 0x4000000) {
558 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
559 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
560 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
561 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
562 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
564 else if(val < 0x80000000) {
565 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
566 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
567 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
568 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
569 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
570 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
573 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
574 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
575 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
576 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
577 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
578 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
579 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
585 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
587 /* 0-pad to byte boundary */
589 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
594 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
596 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
598 /* to avoid a drastic speed penalty we don't:
600 assert(bb->buffer != 0);
601 assert(bb->bits == 0);
605 if(bb->total_consumed_bits < bb->total_bits) {
606 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
610 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
616 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
618 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
620 /* to avoid a drastic speed penalty we don't:
622 assert(bb->buffer != 0);
623 assert(bb->bits == 0);
627 if(bb->total_consumed_bits < bb->total_bits) {
628 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
630 if(bb->consumed_bits == 8) {
631 bb->consumed_bytes++;
632 bb->consumed_bits = 0;
634 bb->total_consumed_bits++;
638 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
644 bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
646 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
648 /* to avoid a drastic speed penalty we don't:
650 assert(bb->buffer != 0);
651 assert(bb->bits == 0);
655 if(bb->total_consumed_bits < bb->total_bits) {
657 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
659 if(bb->consumed_bits == 8) {
660 bb->consumed_bytes++;
661 bb->consumed_bits = 0;
663 bb->total_consumed_bits++;
667 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
673 bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
675 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
677 /* to avoid a drastic speed penalty we don't:
679 assert(bb->buffer != 0);
680 assert(bb->bits == 0);
684 if(bb->total_consumed_bits < bb->total_bits) {
686 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
688 if(bb->consumed_bits == 8) {
689 bb->consumed_bytes++;
690 bb->consumed_bits = 0;
692 bb->total_consumed_bits++;
696 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
702 bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
707 assert(bb->buffer != 0);
712 for(i = 0; i < bits; i++) {
713 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
719 bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
725 assert(bb->buffer != 0);
730 for(i = 0; i < bits; i++) {
731 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
747 bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
752 assert(bb->buffer != 0);
757 for(i = 0; i < bits; i++) {
758 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
764 bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
770 assert(bb->buffer != 0);
775 for(i = 0; i < bits; i++) {
776 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
792 bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
794 uint32 sign = 0, lsbs, msbs = 0;
798 assert(bb->buffer != 0);
800 /* read the sign bit */
801 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
803 /* read the binary LSBs */
804 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
806 /* read the unary MSBs and end bit */
808 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
815 /* compose the value */
816 *val = (msbs << parameter) | lsbs;
823 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
824 bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
830 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
833 raw[(*rawlen)++] = (byte)x;
834 if(!(x & 0x80)) { /* 0xxxxxxx */
838 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
842 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
846 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
850 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
854 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
861 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
864 raw[(*rawlen)++] = (byte)x;
865 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
877 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
878 bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
884 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
887 raw[(*rawlen)++] = (byte)x;
888 if(!(x & 0x80)) { /* 0xxxxxxx */
892 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
896 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
900 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
904 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
908 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
912 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
919 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
922 raw[(*rawlen)++] = (byte)x;
923 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
935 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
939 fprintf(out, "bitbuffer is NULL\n");
942 fprintf(out, "bitbuffer: capacity=%u bytes=%u bits=%u total_bits=%u consumed: bytes=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->bytes, bb->bits, bb->total_bits, bb->consumed_bytes, bb->consumed_bits, bb->total_consumed_bits);
943 for(i = 0; i < bb->bytes; i++) {
944 fprintf(out, "%08X: ", i);
945 for(j = 0; j < 8; j++)
946 if(i*8+j < bb->total_consumed_bits)
949 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
953 fprintf(out, "%08X: ", i);
954 for(j = 0; j < bb->bits; j++)
955 if(i*8+j < bb->total_consumed_bits)
958 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);