1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <stdlib.h> /* for malloc() */
21 #include <string.h> /* for memcpy(), memset() */
22 #include "private/bitbuffer.h"
23 #include "private/bitmath.h"
24 #include "private/crc.h"
25 #include "FLAC/assert.h"
28 * Along the way you will see two versions of some functions, selected
29 * by a FLAC__NO_MANUAL_INLINING macro. One is the simplified, more
30 * readable, and slow version, and the other is the same function
31 * where crucial parts have been manually inlined and are much faster.
35 /* This should be at least twice as large as the largest number of bytes required to represent any 'number' (in any encoding) you are going to read. */
36 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
38 #define BYTE_BIT_TO_MASK(b) (((FLAC__byte)'\x80') >> (b))
43 #define min(x,y) ((x)<(y)?(x):(y))
47 #define max(x,y) ((x)>(y)?(x):(y))
53 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
55 FLAC__byte *new_buffer;
57 FLAC__ASSERT(bb != 0);
58 FLAC__ASSERT(bb->buffer != 0);
60 if(bb->capacity == new_capacity)
63 new_buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * new_capacity);
66 memset(new_buffer, 0, new_capacity);
67 memcpy(new_buffer, bb->buffer, sizeof(FLAC__byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
68 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
69 bb->bytes = new_capacity;
71 bb->total_bits = (new_capacity<<3);
73 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
74 bb->consumed_bytes = new_capacity;
75 bb->consumed_bits = 0;
76 bb->total_consumed_bits = (new_capacity<<3);
78 bb->buffer = new_buffer;
79 bb->capacity = new_capacity;
83 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
85 unsigned new_capacity;
87 FLAC__ASSERT(min_bytes_to_add > 0);
89 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
90 return bitbuffer_resize_(bb, new_capacity);
93 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
95 FLAC__ASSERT(bb != 0);
96 FLAC__ASSERT(bb->buffer != 0);
97 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
98 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
103 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)
107 /* first shift the unconsumed buffer data toward the front as much as possible */
108 if(bb->total_consumed_bits >= 8) {
109 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
110 for( ; r < r_end; l++, r++)
111 bb->buffer[l] = bb->buffer[r];
112 for( ; l < r_end; l++)
114 bb->bytes -= bb->consumed_bytes;
115 bb->total_bits -= (bb->consumed_bytes<<3);
116 bb->consumed_bytes = 0;
117 bb->total_consumed_bits = bb->consumed_bits;
119 /* grow if we need to */
120 if(bb->capacity <= 1) {
121 if(!bitbuffer_resize_(bb, 16))
124 /* finally, read in some data */
125 bytes = bb->capacity - bb->bytes;
126 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
129 bb->total_bits += (bytes<<3);
133 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
135 FLAC__ASSERT(bb != 0);
139 bb->bytes = bb->bits = bb->total_bits = 0;
140 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
143 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
145 FLAC__ASSERT(bb != 0);
146 FLAC__bitbuffer_init(bb);
151 FLAC__ASSERT(buffer != 0);
152 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte)*bytes);
155 memcpy(bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
156 bb->capacity = bb->bytes = bytes;
158 bb->total_bits = (bytes<<3);
159 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
164 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
166 FLAC__ASSERT(bb != 0);
168 bb->read_crc16 = seed;
171 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
173 static const FLAC__byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
174 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
176 FLAC__ASSERT(dest != 0);
177 FLAC__ASSERT(src != 0);
181 if(dest->bits != src->consumed_bits)
183 if(!bitbuffer_ensure_size_(dest, bits_to_add))
185 if(dest->bits == 0) {
186 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
188 else if(dest->bits + bits_to_add > 8) {
189 dest->buffer[dest->bytes] <<= (8 - dest->bits);
190 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
191 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
194 dest->buffer[dest->bytes] <<= bits_to_add;
195 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
197 dest->bits = src->bits;
198 dest->total_bits += bits_to_add;
199 dest->bytes = dest->total_bits / 8;
204 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
206 FLAC__ASSERT(bb != 0);
212 bb->bytes = bb->bits = bb->total_bits = 0;
213 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
216 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
218 if(bb->buffer == 0) {
219 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
220 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * bb->capacity);
223 memset(bb->buffer, 0, bb->capacity);
226 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
228 bb->bytes = bb->bits = bb->total_bits = 0;
229 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
233 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
235 if(dest->capacity < src->capacity)
236 if(!bitbuffer_resize_(dest, src->capacity))
238 memcpy(dest->buffer, src->buffer, sizeof(FLAC__byte)*min(src->capacity, src->bytes+1));
239 dest->bytes = src->bytes;
240 dest->bits = src->bits;
241 dest->total_bits = src->total_bits;
242 dest->consumed_bytes = src->consumed_bytes;
243 dest->consumed_bits = src->consumed_bits;
244 dest->total_consumed_bits = src->total_consumed_bits;
245 dest->read_crc16 = src->read_crc16;
249 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
253 FLAC__ASSERT(bb != 0);
254 FLAC__ASSERT(bb->buffer != 0);
258 if(!bitbuffer_ensure_size_(bb, bits))
260 bb->total_bits += bits;
262 n = min(8 - bb->bits, bits);
264 bb->buffer[bb->bytes] <<= n;
275 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
279 FLAC__ASSERT(bb != 0);
280 FLAC__ASSERT(bb->buffer != 0);
282 FLAC__ASSERT(bits <= 32);
285 /* inline the size check so we don't incure a function call unnecessarily */
286 if((bb->capacity<<3) < bb->total_bits + bits) {
287 if(!bitbuffer_ensure_size_(bb, bits))
290 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
291 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
292 bb->total_bits += bits;
295 if(n == 8) { /* i.e. bb->bits == 0 */
297 bb->buffer[bb->bytes] = (FLAC__byte)val;
302 bb->buffer[bb->bytes++] = (FLAC__byte)val;
307 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
308 val &= (~(0xffffffff << k));
313 bb->buffer[bb->bytes] <<= bits;
314 bb->buffer[bb->bytes] |= val;
325 bb->buffer[bb->bytes] <<= n;
326 bb->buffer[bb->bytes] |= (val>>k);
327 val &= (~(0xffffffff << k));
337 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
339 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
342 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
344 static const FLAC__uint64 mask[] = {
346 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
347 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
348 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
349 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
350 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
351 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
352 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
353 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
354 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
355 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
356 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
357 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
358 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
359 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
360 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
361 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
365 FLAC__ASSERT(bb != 0);
366 FLAC__ASSERT(bb->buffer != 0);
368 FLAC__ASSERT(bits <= 64);
371 if(!bitbuffer_ensure_size_(bb, bits))
374 bb->total_bits += bits;
378 bb->buffer[bb->bytes] = (FLAC__byte)val;
383 bb->buffer[bb->bytes++] = (FLAC__byte)val;
388 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
389 val &= (~(0xffffffffffffffff << k));
394 n = min(8 - bb->bits, bits);
396 bb->buffer[bb->bytes] <<= n;
397 bb->buffer[bb->bytes] |= (val>>k);
398 val &= (~(0xffffffffffffffff << k));
411 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
413 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
416 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
419 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
421 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
423 if(!FLAC__bitbuffer_write_zeroes(bb, val))
425 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
429 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
433 /* fold signed to unsigned */
436 * (unsigned)(((--val) << 1) - 1);
437 * but without the overflow problem at MININT
439 uval = (unsigned)(((-(++val)) << 1) + 1);
441 uval = (unsigned)(val << 1);
443 msbs = uval >> parameter;
445 return 1 + parameter + msbs;
448 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
450 unsigned bits, msbs, uval;
453 FLAC__ASSERT(parameter > 0);
455 /* fold signed to unsigned */
458 * (unsigned)(((--val) << 1) - 1);
459 * but without the overflow problem at MININT
461 uval = (unsigned)(((-(++val)) << 1) + 1);
463 uval = (unsigned)(val << 1);
465 k = FLAC__bitmath_ilog2(parameter);
466 if(parameter == 1u<<k) {
467 FLAC__ASSERT(k <= 30);
475 d = (1 << (k+1)) - parameter;
476 q = uval / parameter;
477 r = uval - (q * parameter);
486 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
491 FLAC__ASSERT(parameter > 0);
493 k = FLAC__bitmath_ilog2(parameter);
494 if(parameter == 1u<<k) {
495 FLAC__ASSERT(k <= 30);
503 d = (1 << (k+1)) - parameter;
504 q = uval / parameter;
505 r = uval - (q * parameter);
514 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
516 unsigned total_bits, interesting_bits, msbs;
517 FLAC__uint32 pattern;
519 FLAC__ASSERT(bb != 0);
520 FLAC__ASSERT(bb->buffer != 0);
521 FLAC__ASSERT(parameter <= 31);
523 /* init pattern with the unary end bit and the sign bit */
531 msbs = val >> parameter;
532 interesting_bits = 2 + parameter;
533 total_bits = interesting_bits + msbs;
534 pattern <<= parameter;
535 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
537 if(total_bits <= 32) {
538 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
542 /* write the unary MSBs */
543 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
545 /* write the unary end bit, the sign bit, and binary LSBs */
546 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
552 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
554 unsigned total_bits, interesting_bits, msbs;
555 FLAC__uint32 pattern;
557 FLAC__ASSERT(bb != 0);
558 FLAC__ASSERT(bb->buffer != 0);
559 FLAC__ASSERT(parameter <= 31);
563 /* init pattern with the unary end bit and the sign bit */
571 msbs = val >> parameter;
572 interesting_bits = 2 + parameter;
573 total_bits = interesting_bits + msbs;
574 pattern <<= parameter;
575 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
577 if(total_bits <= 32) {
578 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
581 else if(total_bits > max_bits) {
586 /* write the unary MSBs */
587 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
589 /* write the unary end bit, the sign bit, and binary LSBs */
590 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
596 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
598 unsigned total_bits, val_bits;
599 FLAC__uint32 pattern;
601 FLAC__ASSERT(bb != 0);
602 FLAC__ASSERT(bb->buffer != 0);
603 FLAC__ASSERT(parameter <= 31);
605 val_bits = FLAC__bitmath_silog2(val);
606 total_bits = 2 + parameter + 5 + val_bits;
608 if(total_bits <= 32) {
610 pattern <<= (parameter + 5);
612 pattern <<= val_bits;
613 pattern |= (val & ((1 << val_bits) - 1));
614 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
618 /* write the '-0' escape code first */
619 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
621 /* write the length */
622 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
624 /* write the value */
625 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
631 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
633 unsigned total_bits, interesting_bits, msbs, uval;
634 FLAC__uint32 pattern;
636 FLAC__ASSERT(bb != 0);
637 FLAC__ASSERT(bb->buffer != 0);
638 FLAC__ASSERT(parameter <= 30);
640 /* fold signed to unsigned */
643 * (unsigned)(((--val) << 1) - 1);
644 * but without the overflow problem at MININT
646 uval = (unsigned)(((-(++val)) << 1) + 1);
648 uval = (unsigned)(val << 1);
650 msbs = uval >> parameter;
651 interesting_bits = 1 + parameter;
652 total_bits = interesting_bits + msbs;
653 pattern = 1 << parameter; /* the unary end bit */
654 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
656 if(total_bits <= 32) {
657 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
661 /* write the unary MSBs */
662 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
664 /* write the unary end bit and binary LSBs */
665 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
671 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
673 unsigned total_bits, interesting_bits, msbs, uval;
674 FLAC__uint32 pattern;
676 FLAC__ASSERT(bb != 0);
677 FLAC__ASSERT(bb->buffer != 0);
678 FLAC__ASSERT(parameter <= 30);
682 /* fold signed to unsigned */
685 * (unsigned)(((--val) << 1) - 1);
686 * but without the overflow problem at MININT
688 uval = (unsigned)(((-(++val)) << 1) + 1);
690 uval = (unsigned)(val << 1);
692 msbs = uval >> parameter;
693 interesting_bits = 1 + parameter;
694 total_bits = interesting_bits + msbs;
695 pattern = 1 << parameter; /* the unary end bit */
696 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
698 if(total_bits <= 32) {
699 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
702 else if(total_bits > max_bits) {
707 /* write the unary MSBs */
708 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
710 /* write the unary end bit and binary LSBs */
711 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
717 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
719 unsigned total_bits, msbs, uval;
722 FLAC__ASSERT(bb != 0);
723 FLAC__ASSERT(bb->buffer != 0);
724 FLAC__ASSERT(parameter > 0);
726 /* fold signed to unsigned */
729 * (unsigned)(((--val) << 1) - 1);
730 * but without the overflow problem at MININT
732 uval = (unsigned)(((-(++val)) << 1) + 1);
734 uval = (unsigned)(val << 1);
736 k = FLAC__bitmath_ilog2(parameter);
737 if(parameter == 1u<<k) {
740 FLAC__ASSERT(k <= 30);
743 total_bits = 1 + k + msbs;
744 pattern = 1 << k; /* the unary end bit */
745 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
747 if(total_bits <= 32) {
748 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
752 /* write the unary MSBs */
753 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
755 /* write the unary end bit and binary LSBs */
756 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
763 d = (1 << (k+1)) - parameter;
764 q = uval / parameter;
765 r = uval - (q * parameter);
766 /* write the unary MSBs */
767 if(!FLAC__bitbuffer_write_zeroes(bb, q))
769 /* write the unary end bit */
770 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
772 /* write the binary LSBs */
774 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
778 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
785 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
787 unsigned total_bits, msbs;
790 FLAC__ASSERT(bb != 0);
791 FLAC__ASSERT(bb->buffer != 0);
792 FLAC__ASSERT(parameter > 0);
794 k = FLAC__bitmath_ilog2(parameter);
795 if(parameter == 1u<<k) {
798 FLAC__ASSERT(k <= 30);
801 total_bits = 1 + k + msbs;
802 pattern = 1 << k; /* the unary end bit */
803 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
805 if(total_bits <= 32) {
806 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
810 /* write the unary MSBs */
811 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
813 /* write the unary end bit and binary LSBs */
814 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
821 d = (1 << (k+1)) - parameter;
822 q = uval / parameter;
823 r = uval - (q * parameter);
824 /* write the unary MSBs */
825 if(!FLAC__bitbuffer_write_zeroes(bb, q))
827 /* write the unary end bit */
828 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
830 /* write the binary LSBs */
832 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
836 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
843 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
847 FLAC__ASSERT(bb != 0);
848 FLAC__ASSERT(bb->buffer != 0);
850 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
853 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
855 else if(val < 0x800) {
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
859 else if(val < 0x10000) {
860 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
864 else if(val < 0x200000) {
865 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
866 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
870 else if(val < 0x4000000) {
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
873 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
874 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
875 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
880 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
881 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
882 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
883 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
889 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
893 FLAC__ASSERT(bb != 0);
894 FLAC__ASSERT(bb->buffer != 0);
896 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
899 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
901 else if(val < 0x800) {
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
905 else if(val < 0x10000) {
906 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
910 else if(val < 0x200000) {
911 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
912 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
913 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
916 else if(val < 0x4000000) {
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
919 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
920 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
923 else if(val < 0x80000000) {
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
929 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
935 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
936 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
937 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
938 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
944 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
946 /* 0-pad to byte boundary */
948 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
953 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)
955 /* to avoid a drastic speed penalty we don't:
956 FLAC__ASSERT(bb != 0);
957 FLAC__ASSERT(bb->buffer != 0);
958 FLAC__ASSERT(bb->bits == 0);
962 if(bb->total_consumed_bits < bb->total_bits) {
963 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
967 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
973 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)
975 /* to avoid a drastic speed penalty we don't:
976 FLAC__ASSERT(bb != 0);
977 FLAC__ASSERT(bb->buffer != 0);
978 FLAC__ASSERT(bb->bits == 0);
982 if(bb->total_consumed_bits < bb->total_bits) {
983 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
985 if(bb->consumed_bits == 8) {
986 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
987 bb->consumed_bytes++;
988 bb->consumed_bits = 0;
990 bb->total_consumed_bits++;
994 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1000 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)
1002 /* to avoid a drastic speed penalty we don't:
1003 FLAC__ASSERT(bb != 0);
1004 FLAC__ASSERT(bb->buffer != 0);
1005 FLAC__ASSERT(bb->bits == 0);
1009 if(bb->total_consumed_bits < bb->total_bits) {
1011 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1012 bb->consumed_bits++;
1013 if(bb->consumed_bits == 8) {
1014 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1015 bb->consumed_bytes++;
1016 bb->consumed_bits = 0;
1018 bb->total_consumed_bits++;
1022 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1028 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)
1030 /* to avoid a drastic speed penalty we don't:
1031 FLAC__ASSERT(bb != 0);
1032 FLAC__ASSERT(bb->buffer != 0);
1033 FLAC__ASSERT(bb->bits == 0);
1037 if(bb->total_consumed_bits < bb->total_bits) {
1039 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1040 bb->consumed_bits++;
1041 if(bb->consumed_bits == 8) {
1042 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1043 bb->consumed_bytes++;
1044 bb->consumed_bits = 0;
1046 bb->total_consumed_bits++;
1050 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1056 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)
1057 #ifdef FLAC__NO_MANUAL_INLINING
1061 FLAC__ASSERT(bb != 0);
1062 FLAC__ASSERT(bb->buffer != 0);
1064 FLAC__ASSERT(bits <= 32);
1067 for(i = 0; i < bits; i++) {
1068 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1075 unsigned i, bits_ = bits;
1078 FLAC__ASSERT(bb != 0);
1079 FLAC__ASSERT(bb->buffer != 0);
1081 FLAC__ASSERT(bits <= 32);
1082 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1084 while(bb->total_consumed_bits + bits > bb->total_bits) {
1085 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1088 if(bb->consumed_bits) {
1089 i = 8 - bb->consumed_bits;
1091 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1093 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1094 bb->consumed_bytes++;
1095 bb->consumed_bits = 0;
1096 /* we hold off updating bb->total_consumed_bits until the end */
1099 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1100 bb->consumed_bits += bits_;
1101 bb->total_consumed_bits += bits_;
1107 v |= bb->buffer[bb->consumed_bytes];
1109 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1110 bb->consumed_bytes++;
1111 /* bb->consumed_bits is already 0 */
1112 /* we hold off updating bb->total_consumed_bits until the end */
1116 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1117 bb->consumed_bits = bits_;
1118 /* we hold off updating bb->total_consumed_bits until the end */
1120 bb->total_consumed_bits += bits;
1126 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)
1127 #ifdef FLAC__NO_MANUAL_INLINING
1132 FLAC__ASSERT(bb != 0);
1133 FLAC__ASSERT(bb->buffer != 0);
1135 FLAC__ASSERT(bits <= 32);
1143 for(i = 0; i < bits; i++) {
1144 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1152 *val = (FLAC__int32)v;
1156 *val = (FLAC__int32)v;
1162 unsigned i, bits_ = bits;
1165 FLAC__ASSERT(bb != 0);
1166 FLAC__ASSERT(bb->buffer != 0);
1168 FLAC__ASSERT(bits <= 32);
1169 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1176 while(bb->total_consumed_bits + bits > bb->total_bits) {
1177 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1180 if(bb->consumed_bits) {
1181 i = 8 - bb->consumed_bits;
1183 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1185 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1186 bb->consumed_bytes++;
1187 bb->consumed_bits = 0;
1188 /* we hold off updating bb->total_consumed_bits until the end */
1191 /* bits_ must be < 7 if we get to here */
1192 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1194 *val = (FLAC__int32)v;
1195 *val >>= (32-bits_);
1196 bb->consumed_bits += bits_;
1197 bb->total_consumed_bits += bits_;
1203 v |= bb->buffer[bb->consumed_bytes];
1205 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1206 bb->consumed_bytes++;
1207 /* bb->consumed_bits is already 0 */
1208 /* we hold off updating bb->total_consumed_bits until the end */
1212 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1213 bb->consumed_bits = bits_;
1214 /* we hold off updating bb->total_consumed_bits until the end */
1216 bb->total_consumed_bits += bits;
1222 *val = (FLAC__int32)v;
1226 *val = (FLAC__int32)v;
1232 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)
1233 #ifdef FLAC__NO_MANUAL_INLINING
1237 FLAC__ASSERT(bb != 0);
1238 FLAC__ASSERT(bb->buffer != 0);
1240 FLAC__ASSERT(bits <= 64);
1243 for(i = 0; i < bits; i++) {
1244 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1251 unsigned i, bits_ = bits;
1254 FLAC__ASSERT(bb != 0);
1255 FLAC__ASSERT(bb->buffer != 0);
1257 FLAC__ASSERT(bits <= 64);
1258 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1260 while(bb->total_consumed_bits + bits > bb->total_bits) {
1261 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1264 if(bb->consumed_bits) {
1265 i = 8 - bb->consumed_bits;
1267 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1269 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1270 bb->consumed_bytes++;
1271 bb->consumed_bits = 0;
1272 /* we hold off updating bb->total_consumed_bits until the end */
1275 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1276 bb->consumed_bits += bits_;
1277 bb->total_consumed_bits += bits_;
1283 v |= bb->buffer[bb->consumed_bytes];
1285 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1286 bb->consumed_bytes++;
1287 /* bb->consumed_bits is already 0 */
1288 /* we hold off updating bb->total_consumed_bits until the end */
1292 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1293 bb->consumed_bits = bits_;
1294 /* we hold off updating bb->total_consumed_bits until the end */
1296 bb->total_consumed_bits += bits;
1302 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)
1303 #ifdef FLAC__NO_MANUAL_INLINING
1308 FLAC__ASSERT(bb != 0);
1309 FLAC__ASSERT(bb->buffer != 0);
1311 FLAC__ASSERT(bits <= 64);
1314 for(i = 0; i < bits; i++) {
1315 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1322 *val = (FLAC__int64)v;
1326 *val = (FLAC__int64)v;
1332 unsigned i, bits_ = bits;
1335 FLAC__ASSERT(bb != 0);
1336 FLAC__ASSERT(bb->buffer != 0);
1338 FLAC__ASSERT(bits <= 64);
1339 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1341 while(bb->total_consumed_bits + bits > bb->total_bits) {
1342 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1345 if(bb->consumed_bits) {
1346 i = 8 - bb->consumed_bits;
1348 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1350 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1351 bb->consumed_bytes++;
1352 bb->consumed_bits = 0;
1353 /* we hold off updating bb->total_consumed_bits until the end */
1356 /* bits_ must be < 7 if we get to here */
1357 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1359 *val = (FLAC__int64)v;
1360 *val >>= (64-bits_);
1361 bb->consumed_bits += bits_;
1362 bb->total_consumed_bits += bits_;
1368 v |= bb->buffer[bb->consumed_bytes];
1370 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1371 bb->consumed_bytes++;
1372 /* bb->consumed_bits is already 0 */
1373 /* we hold off updating bb->total_consumed_bits until the end */
1377 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1378 bb->consumed_bits = bits_;
1379 /* we hold off updating bb->total_consumed_bits until the end */
1381 bb->total_consumed_bits += bits;
1387 *val = (FLAC__int64)v;
1391 *val = (FLAC__int64)v;
1397 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)
1398 #ifdef FLAC__NO_MANUAL_INLINING
1400 unsigned bit, val_ = 0;
1402 FLAC__ASSERT(bb != 0);
1403 FLAC__ASSERT(bb->buffer != 0);
1406 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1418 unsigned i, val_ = 0;
1419 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1422 FLAC__ASSERT(bb != 0);
1423 FLAC__ASSERT(bb->buffer != 0);
1425 if(bb->consumed_bits) {
1426 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1428 for(i = 0; !(b & 0x80); i++)
1432 bb->consumed_bits += i;
1433 bb->total_consumed_bits += i;
1434 if(bb->consumed_bits == 8) {
1435 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1436 bb->consumed_bytes++;
1437 bb->consumed_bits = 0;
1442 val_ = 8 - bb->consumed_bits;
1443 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1444 bb->consumed_bytes++;
1445 bb->consumed_bits = 0;
1446 bb->total_consumed_bits += val_;
1450 if(bb->consumed_bytes >= total_bytes_) {
1451 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1453 total_bytes_ = (bb->total_bits + 7) / 8;
1455 b = bb->buffer[bb->consumed_bytes];
1457 for(i = 0; !(b & 0x80); i++)
1461 bb->consumed_bits = i;
1464 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1465 bb->consumed_bytes++;
1466 bb->consumed_bits = 0;
1468 bb->total_consumed_bits += i;
1473 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1474 bb->consumed_bytes++;
1475 /* bb->consumed_bits is already 0 */
1476 /* we hold off updating bb->total_consumed_bits until the end */
1477 bb->total_consumed_bits += 8;
1483 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)
1485 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1487 FLAC__ASSERT(bb != 0);
1488 FLAC__ASSERT(bb->buffer != 0);
1489 FLAC__ASSERT(parameter <= 31);
1491 /* read the unary MSBs and end bit */
1492 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1495 /* read the sign bit */
1496 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1499 /* read the binary LSBs */
1500 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1503 /* compose the value */
1504 *val = (msbs << parameter) | lsbs;
1511 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)
1513 FLAC__uint32 lsbs = 0, msbs = 0;
1516 FLAC__ASSERT(bb != 0);
1517 FLAC__ASSERT(bb->buffer != 0);
1518 FLAC__ASSERT(parameter <= 31);
1520 /* read the unary MSBs and end bit */
1521 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1524 /* read the binary LSBs */
1525 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1528 /* compose the value */
1529 uval = (msbs << parameter) | lsbs;
1531 *val = -((int)(uval >> 1)) - 1;
1533 *val = (int)(uval >> 1);
1538 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)
1540 FLAC__uint32 lsbs = 0, msbs = 0;
1541 unsigned bit, uval, k;
1543 FLAC__ASSERT(bb != 0);
1544 FLAC__ASSERT(bb->buffer != 0);
1546 k = FLAC__bitmath_ilog2(parameter);
1548 /* read the unary MSBs and end bit */
1549 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1552 /* read the binary LSBs */
1553 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1556 if(parameter == 1u<<k) {
1557 /* compose the value */
1558 uval = (msbs << k) | lsbs;
1561 unsigned d = (1 << (k+1)) - parameter;
1563 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1569 /* compose the value */
1570 uval = msbs * parameter + lsbs;
1573 /* unfold unsigned to signed */
1575 *val = -((int)(uval >> 1)) - 1;
1577 *val = (int)(uval >> 1);
1582 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)
1584 FLAC__uint32 lsbs, msbs = 0;
1587 FLAC__ASSERT(bb != 0);
1588 FLAC__ASSERT(bb->buffer != 0);
1590 k = FLAC__bitmath_ilog2(parameter);
1592 /* read the unary MSBs and end bit */
1593 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1596 /* read the binary LSBs */
1597 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1600 if(parameter == 1u<<k) {
1601 /* compose the value */
1602 *val = (msbs << k) | lsbs;
1605 unsigned d = (1 << (k+1)) - parameter;
1607 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1613 /* compose the value */
1614 *val = msbs * parameter + lsbs;
1620 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1621 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)
1627 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1630 raw[(*rawlen)++] = (FLAC__byte)x;
1631 if(!(x & 0x80)) { /* 0xxxxxxx */
1635 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1639 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1643 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1647 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1651 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1660 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1663 raw[(*rawlen)++] = (FLAC__byte)x;
1664 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1675 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1676 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)
1682 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1685 raw[(*rawlen)++] = (FLAC__byte)x;
1686 if(!(x & 0x80)) { /* 0xxxxxxx */
1690 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1694 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1698 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1702 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1706 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1710 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1715 *val = 0xffffffffffffffff;
1719 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1722 raw[(*rawlen)++] = (FLAC__byte)x;
1723 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1724 *val = 0xffffffffffffffff;
1734 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1738 fprintf(out, "bitbuffer is NULL\n");
1741 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);
1742 for(i = 0; i < bb->bytes; i++) {
1743 fprintf(out, "%08X: ", i);
1744 for(j = 0; j < 8; j++)
1745 if(i*8+j < bb->total_consumed_bits)
1748 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1752 fprintf(out, "%08X: ", i);
1753 for(j = 0; j < bb->bits; j++)
1754 if(i*8+j < bb->total_consumed_bits)
1757 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);