1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 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))
49 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
51 FLAC__byte *new_buffer;
53 FLAC__ASSERT(bb != 0);
54 FLAC__ASSERT(bb->buffer != 0);
56 if(bb->capacity == new_capacity)
59 new_buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * new_capacity);
62 memset(new_buffer, 0, new_capacity);
63 memcpy(new_buffer, bb->buffer, sizeof(FLAC__byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
64 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
65 bb->bytes = new_capacity;
67 bb->total_bits = (new_capacity<<3);
69 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
70 bb->consumed_bytes = new_capacity;
71 bb->consumed_bits = 0;
72 bb->total_consumed_bits = (new_capacity<<3);
74 bb->buffer = new_buffer;
75 bb->capacity = new_capacity;
79 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
81 unsigned new_capacity;
83 FLAC__ASSERT(min_bytes_to_add > 0);
85 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
86 return bitbuffer_resize_(bb, new_capacity);
89 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
91 FLAC__ASSERT(bb != 0);
92 FLAC__ASSERT(bb->buffer != 0);
93 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
94 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
99 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)
103 /* first shift the unconsumed buffer data toward the front as much as possible */
104 if(bb->total_consumed_bits >= 8) {
105 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
106 for( ; r < r_end; l++, r++)
107 bb->buffer[l] = bb->buffer[r];
108 for( ; l < r_end; l++)
110 bb->bytes -= bb->consumed_bytes;
111 bb->total_bits -= (bb->consumed_bytes<<3);
112 bb->consumed_bytes = 0;
113 bb->total_consumed_bits = bb->consumed_bits;
115 /* grow if we need to */
116 if(bb->capacity <= 1) {
117 if(!bitbuffer_resize_(bb, 16))
120 /* finally, read in some data */
121 bytes = bb->capacity - bb->bytes;
122 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
125 bb->total_bits += (bytes<<3);
129 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
131 FLAC__ASSERT(bb != 0);
135 bb->bytes = bb->bits = bb->total_bits = 0;
136 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
139 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
141 FLAC__ASSERT(bb != 0);
142 FLAC__bitbuffer_init(bb);
147 FLAC__ASSERT(buffer != 0);
148 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte)*bytes);
151 memcpy(bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
152 bb->capacity = bb->bytes = bytes;
154 bb->total_bits = (bytes<<3);
155 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
160 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
162 FLAC__ASSERT(bb != 0);
164 bb->read_crc16 = seed;
167 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
169 static const FLAC__byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
170 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
172 FLAC__ASSERT(dest != 0);
173 FLAC__ASSERT(src != 0);
177 if(dest->bits != src->consumed_bits)
179 if(!bitbuffer_ensure_size_(dest, bits_to_add))
181 if(dest->bits == 0) {
182 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
184 else if(dest->bits + bits_to_add > 8) {
185 dest->buffer[dest->bytes] <<= (8 - dest->bits);
186 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
187 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
190 dest->buffer[dest->bytes] <<= bits_to_add;
191 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
193 dest->bits = src->bits;
194 dest->total_bits += bits_to_add;
195 dest->bytes = dest->total_bits / 8;
200 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
202 FLAC__ASSERT(bb != 0);
208 bb->bytes = bb->bits = bb->total_bits = 0;
209 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
212 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
214 if(bb->buffer == 0) {
215 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
216 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * bb->capacity);
219 memset(bb->buffer, 0, bb->capacity);
222 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
224 bb->bytes = bb->bits = bb->total_bits = 0;
225 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
229 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
231 if(dest->capacity < src->capacity)
232 if(!bitbuffer_resize_(dest, src->capacity))
234 memcpy(dest->buffer, src->buffer, sizeof(FLAC__byte)*min(src->capacity, src->bytes+1));
235 dest->bytes = src->bytes;
236 dest->bits = src->bits;
237 dest->total_bits = src->total_bits;
238 dest->consumed_bytes = src->consumed_bytes;
239 dest->consumed_bits = src->consumed_bits;
240 dest->total_consumed_bits = src->total_consumed_bits;
241 dest->read_crc16 = src->read_crc16;
245 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
249 FLAC__ASSERT(bb != 0);
250 FLAC__ASSERT(bb->buffer != 0);
254 if(!bitbuffer_ensure_size_(bb, bits))
256 bb->total_bits += bits;
258 n = min(8 - bb->bits, bits);
260 bb->buffer[bb->bytes] <<= n;
271 FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
275 FLAC__ASSERT(bb != 0);
276 FLAC__ASSERT(bb->buffer != 0);
278 FLAC__ASSERT(bits <= 32);
281 /* inline the size check so we don't incure a function call unnecessarily */
282 if((bb->capacity<<3) < bb->total_bits + bits) {
283 if(!bitbuffer_ensure_size_(bb, bits))
286 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
287 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
288 bb->total_bits += bits;
291 if(n == 8) { /* i.e. bb->bits == 0 */
293 bb->buffer[bb->bytes] = (FLAC__byte)val;
298 bb->buffer[bb->bytes++] = (FLAC__byte)val;
303 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
304 val &= (~(0xffffffff << k));
309 bb->buffer[bb->bytes] <<= bits;
310 bb->buffer[bb->bytes] |= val;
321 bb->buffer[bb->bytes] <<= n;
322 bb->buffer[bb->bytes] |= (val>>k);
323 val &= (~(0xffffffff << k));
333 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
335 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
338 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
340 static const FLAC__uint64 mask[] = {
342 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
343 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
344 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
345 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
346 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
347 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
348 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
349 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
350 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
351 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
352 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
353 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
354 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
355 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
356 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
357 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
361 FLAC__ASSERT(bb != 0);
362 FLAC__ASSERT(bb->buffer != 0);
364 FLAC__ASSERT(bits <= 64);
367 if(!bitbuffer_ensure_size_(bb, bits))
370 bb->total_bits += bits;
374 bb->buffer[bb->bytes] = (FLAC__byte)val;
379 bb->buffer[bb->bytes++] = (FLAC__byte)val;
384 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
385 val &= (~(0xffffffffffffffff << k));
390 n = min(8 - bb->bits, bits);
392 bb->buffer[bb->bytes] <<= n;
393 bb->buffer[bb->bytes] |= (val>>k);
394 val &= (~(0xffffffffffffffff << k));
407 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
409 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
412 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
415 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
417 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
419 if(!FLAC__bitbuffer_write_zeroes(bb, val))
421 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
425 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
429 /* fold signed to unsigned */
432 * (unsigned)(((--val) << 1) - 1);
433 * but without the overflow problem at MININT
435 uval = (unsigned)(((-(++val)) << 1) + 1);
437 uval = (unsigned)(val << 1);
439 msbs = uval >> parameter;
441 return 1 + parameter + msbs;
444 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
446 unsigned bits, msbs, uval;
449 FLAC__ASSERT(parameter > 0);
451 /* fold signed to unsigned */
454 * (unsigned)(((--val) << 1) - 1);
455 * but without the overflow problem at MININT
457 uval = (unsigned)(((-(++val)) << 1) + 1);
459 uval = (unsigned)(val << 1);
461 k = FLAC__bitmath_ilog2(parameter);
462 if(parameter == 1u<<k) {
463 FLAC__ASSERT(k <= 30);
471 d = (1 << (k+1)) - parameter;
472 q = uval / parameter;
473 r = uval - (q * parameter);
482 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
487 FLAC__ASSERT(parameter > 0);
489 k = FLAC__bitmath_ilog2(parameter);
490 if(parameter == 1u<<k) {
491 FLAC__ASSERT(k <= 30);
499 d = (1 << (k+1)) - parameter;
500 q = uval / parameter;
501 r = uval - (q * parameter);
510 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
512 unsigned total_bits, interesting_bits, msbs;
513 FLAC__uint32 pattern;
515 FLAC__ASSERT(bb != 0);
516 FLAC__ASSERT(bb->buffer != 0);
517 FLAC__ASSERT(parameter <= 31);
519 /* init pattern with the unary end bit and the sign bit */
527 msbs = val >> parameter;
528 interesting_bits = 2 + parameter;
529 total_bits = interesting_bits + msbs;
530 pattern <<= parameter;
531 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
533 if(total_bits <= 32) {
534 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
538 /* write the unary MSBs */
539 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
541 /* write the unary end bit, the sign bit, and binary LSBs */
542 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
548 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
550 unsigned total_bits, interesting_bits, msbs;
551 FLAC__uint32 pattern;
553 FLAC__ASSERT(bb != 0);
554 FLAC__ASSERT(bb->buffer != 0);
555 FLAC__ASSERT(parameter <= 31);
559 /* init pattern with the unary end bit and the sign bit */
567 msbs = val >> parameter;
568 interesting_bits = 2 + parameter;
569 total_bits = interesting_bits + msbs;
570 pattern <<= parameter;
571 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
573 if(total_bits <= 32) {
574 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
577 else if(total_bits > max_bits) {
582 /* write the unary MSBs */
583 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
585 /* write the unary end bit, the sign bit, and binary LSBs */
586 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
592 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
594 unsigned total_bits, val_bits;
595 FLAC__uint32 pattern;
597 FLAC__ASSERT(bb != 0);
598 FLAC__ASSERT(bb->buffer != 0);
599 FLAC__ASSERT(parameter <= 31);
601 val_bits = FLAC__bitmath_silog2(val);
602 total_bits = 2 + parameter + 5 + val_bits;
604 if(total_bits <= 32) {
606 pattern <<= (parameter + 5);
608 pattern <<= val_bits;
609 pattern |= (val & ((1 << val_bits) - 1));
610 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
614 /* write the '-0' escape code first */
615 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
617 /* write the length */
618 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
620 /* write the value */
621 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
627 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
629 unsigned total_bits, interesting_bits, msbs, uval;
630 FLAC__uint32 pattern;
632 FLAC__ASSERT(bb != 0);
633 FLAC__ASSERT(bb->buffer != 0);
634 FLAC__ASSERT(parameter <= 30);
636 /* fold signed to unsigned */
639 * (unsigned)(((--val) << 1) - 1);
640 * but without the overflow problem at MININT
642 uval = (unsigned)(((-(++val)) << 1) + 1);
644 uval = (unsigned)(val << 1);
646 msbs = uval >> parameter;
647 interesting_bits = 1 + parameter;
648 total_bits = interesting_bits + msbs;
649 pattern = 1 << parameter; /* the unary end bit */
650 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
652 if(total_bits <= 32) {
653 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
657 /* write the unary MSBs */
658 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
660 /* write the unary end bit and binary LSBs */
661 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
667 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
669 unsigned total_bits, interesting_bits, msbs, uval;
670 FLAC__uint32 pattern;
672 FLAC__ASSERT(bb != 0);
673 FLAC__ASSERT(bb->buffer != 0);
674 FLAC__ASSERT(parameter <= 30);
678 /* fold signed to unsigned */
681 * (unsigned)(((--val) << 1) - 1);
682 * but without the overflow problem at MININT
684 uval = (unsigned)(((-(++val)) << 1) + 1);
686 uval = (unsigned)(val << 1);
688 msbs = uval >> parameter;
689 interesting_bits = 1 + parameter;
690 total_bits = interesting_bits + msbs;
691 pattern = 1 << parameter; /* the unary end bit */
692 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
694 if(total_bits <= 32) {
695 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
698 else if(total_bits > max_bits) {
703 /* write the unary MSBs */
704 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
706 /* write the unary end bit and binary LSBs */
707 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
713 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
715 unsigned total_bits, msbs, uval;
718 FLAC__ASSERT(bb != 0);
719 FLAC__ASSERT(bb->buffer != 0);
720 FLAC__ASSERT(parameter > 0);
722 /* fold signed to unsigned */
725 * (unsigned)(((--val) << 1) - 1);
726 * but without the overflow problem at MININT
728 uval = (unsigned)(((-(++val)) << 1) + 1);
730 uval = (unsigned)(val << 1);
732 k = FLAC__bitmath_ilog2(parameter);
733 if(parameter == 1u<<k) {
736 FLAC__ASSERT(k <= 30);
739 total_bits = 1 + k + msbs;
740 pattern = 1 << k; /* the unary end bit */
741 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
743 if(total_bits <= 32) {
744 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
748 /* write the unary MSBs */
749 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
751 /* write the unary end bit and binary LSBs */
752 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
759 d = (1 << (k+1)) - parameter;
760 q = uval / parameter;
761 r = uval - (q * parameter);
762 /* write the unary MSBs */
763 if(!FLAC__bitbuffer_write_zeroes(bb, q))
765 /* write the unary end bit */
766 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
768 /* write the binary LSBs */
770 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
774 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
781 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
783 unsigned total_bits, msbs;
786 FLAC__ASSERT(bb != 0);
787 FLAC__ASSERT(bb->buffer != 0);
788 FLAC__ASSERT(parameter > 0);
790 k = FLAC__bitmath_ilog2(parameter);
791 if(parameter == 1u<<k) {
794 FLAC__ASSERT(k <= 30);
797 total_bits = 1 + k + msbs;
798 pattern = 1 << k; /* the unary end bit */
799 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
801 if(total_bits <= 32) {
802 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
806 /* write the unary MSBs */
807 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
809 /* write the unary end bit and binary LSBs */
810 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
817 d = (1 << (k+1)) - parameter;
818 q = uval / parameter;
819 r = uval - (q * parameter);
820 /* write the unary MSBs */
821 if(!FLAC__bitbuffer_write_zeroes(bb, q))
823 /* write the unary end bit */
824 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
826 /* write the binary LSBs */
828 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
832 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
839 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
843 FLAC__ASSERT(bb != 0);
844 FLAC__ASSERT(bb->buffer != 0);
846 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
849 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
851 else if(val < 0x800) {
852 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
853 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
855 else if(val < 0x10000) {
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
858 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
860 else if(val < 0x200000) {
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
866 else if(val < 0x4000000) {
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
869 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
870 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
874 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
875 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
876 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
877 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
885 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
889 FLAC__ASSERT(bb != 0);
890 FLAC__ASSERT(bb->buffer != 0);
892 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
895 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
897 else if(val < 0x800) {
898 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
899 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
901 else if(val < 0x10000) {
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
904 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
906 else if(val < 0x200000) {
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
910 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
912 else if(val < 0x4000000) {
913 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
915 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
916 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
919 else if(val < 0x80000000) {
920 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
929 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
930 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
931 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
940 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
942 /* 0-pad to byte boundary */
944 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
949 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)
951 /* to avoid a drastic speed penalty we don't:
952 FLAC__ASSERT(bb != 0);
953 FLAC__ASSERT(bb->buffer != 0);
954 FLAC__ASSERT(bb->bits == 0);
958 if(bb->total_consumed_bits < bb->total_bits) {
959 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
963 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
969 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)
971 /* to avoid a drastic speed penalty we don't:
972 FLAC__ASSERT(bb != 0);
973 FLAC__ASSERT(bb->buffer != 0);
974 FLAC__ASSERT(bb->bits == 0);
978 if(bb->total_consumed_bits < bb->total_bits) {
979 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
981 if(bb->consumed_bits == 8) {
982 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
983 bb->consumed_bytes++;
984 bb->consumed_bits = 0;
986 bb->total_consumed_bits++;
990 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
996 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)
998 /* to avoid a drastic speed penalty we don't:
999 FLAC__ASSERT(bb != 0);
1000 FLAC__ASSERT(bb->buffer != 0);
1001 FLAC__ASSERT(bb->bits == 0);
1005 if(bb->total_consumed_bits < bb->total_bits) {
1007 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1008 bb->consumed_bits++;
1009 if(bb->consumed_bits == 8) {
1010 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1011 bb->consumed_bytes++;
1012 bb->consumed_bits = 0;
1014 bb->total_consumed_bits++;
1018 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1024 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)
1026 /* to avoid a drastic speed penalty we don't:
1027 FLAC__ASSERT(bb != 0);
1028 FLAC__ASSERT(bb->buffer != 0);
1029 FLAC__ASSERT(bb->bits == 0);
1033 if(bb->total_consumed_bits < bb->total_bits) {
1035 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1036 bb->consumed_bits++;
1037 if(bb->consumed_bits == 8) {
1038 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1039 bb->consumed_bytes++;
1040 bb->consumed_bits = 0;
1042 bb->total_consumed_bits++;
1046 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1052 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)
1053 #ifdef FLAC__NO_MANUAL_INLINING
1057 FLAC__ASSERT(bb != 0);
1058 FLAC__ASSERT(bb->buffer != 0);
1060 FLAC__ASSERT(bits <= 32);
1063 for(i = 0; i < bits; i++) {
1064 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1071 unsigned i, bits_ = bits;
1074 FLAC__ASSERT(bb != 0);
1075 FLAC__ASSERT(bb->buffer != 0);
1077 FLAC__ASSERT(bits <= 32);
1078 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1080 while(bb->total_consumed_bits + bits > bb->total_bits) {
1081 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1084 if(bb->consumed_bits) {
1085 i = 8 - bb->consumed_bits;
1087 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1089 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1090 bb->consumed_bytes++;
1091 bb->consumed_bits = 0;
1092 /* we hold off updating bb->total_consumed_bits until the end */
1095 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1096 bb->consumed_bits += bits_;
1097 bb->total_consumed_bits += bits_;
1103 v |= bb->buffer[bb->consumed_bytes];
1105 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1106 bb->consumed_bytes++;
1107 /* bb->consumed_bits is already 0 */
1108 /* we hold off updating bb->total_consumed_bits until the end */
1112 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1113 bb->consumed_bits = bits_;
1114 /* we hold off updating bb->total_consumed_bits until the end */
1116 bb->total_consumed_bits += bits;
1122 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)
1123 #ifdef FLAC__NO_MANUAL_INLINING
1128 FLAC__ASSERT(bb != 0);
1129 FLAC__ASSERT(bb->buffer != 0);
1131 FLAC__ASSERT(bits <= 32);
1139 for(i = 0; i < bits; i++) {
1140 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1148 *val = (FLAC__int32)v;
1152 *val = (FLAC__int32)v;
1158 unsigned i, bits_ = bits;
1161 FLAC__ASSERT(bb != 0);
1162 FLAC__ASSERT(bb->buffer != 0);
1164 FLAC__ASSERT(bits <= 32);
1165 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1172 while(bb->total_consumed_bits + bits > bb->total_bits) {
1173 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1176 if(bb->consumed_bits) {
1177 i = 8 - bb->consumed_bits;
1179 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1181 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1182 bb->consumed_bytes++;
1183 bb->consumed_bits = 0;
1184 /* we hold off updating bb->total_consumed_bits until the end */
1187 /* bits_ must be < 7 if we get to here */
1188 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1190 *val = (FLAC__int32)v;
1191 *val >>= (32-bits_);
1192 bb->consumed_bits += bits_;
1193 bb->total_consumed_bits += bits_;
1199 v |= bb->buffer[bb->consumed_bytes];
1201 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1202 bb->consumed_bytes++;
1203 /* bb->consumed_bits is already 0 */
1204 /* we hold off updating bb->total_consumed_bits until the end */
1208 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1209 bb->consumed_bits = bits_;
1210 /* we hold off updating bb->total_consumed_bits until the end */
1212 bb->total_consumed_bits += bits;
1218 *val = (FLAC__int32)v;
1222 *val = (FLAC__int32)v;
1228 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)
1229 #ifdef FLAC__NO_MANUAL_INLINING
1233 FLAC__ASSERT(bb != 0);
1234 FLAC__ASSERT(bb->buffer != 0);
1236 FLAC__ASSERT(bits <= 64);
1239 for(i = 0; i < bits; i++) {
1240 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1247 unsigned i, bits_ = bits;
1250 FLAC__ASSERT(bb != 0);
1251 FLAC__ASSERT(bb->buffer != 0);
1253 FLAC__ASSERT(bits <= 64);
1254 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1256 while(bb->total_consumed_bits + bits > bb->total_bits) {
1257 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1260 if(bb->consumed_bits) {
1261 i = 8 - bb->consumed_bits;
1263 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1265 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1266 bb->consumed_bytes++;
1267 bb->consumed_bits = 0;
1268 /* we hold off updating bb->total_consumed_bits until the end */
1271 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1272 bb->consumed_bits += bits_;
1273 bb->total_consumed_bits += bits_;
1279 v |= bb->buffer[bb->consumed_bytes];
1281 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1282 bb->consumed_bytes++;
1283 /* bb->consumed_bits is already 0 */
1284 /* we hold off updating bb->total_consumed_bits until the end */
1288 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1289 bb->consumed_bits = bits_;
1290 /* we hold off updating bb->total_consumed_bits until the end */
1292 bb->total_consumed_bits += bits;
1298 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)
1299 #ifdef FLAC__NO_MANUAL_INLINING
1304 FLAC__ASSERT(bb != 0);
1305 FLAC__ASSERT(bb->buffer != 0);
1307 FLAC__ASSERT(bits <= 64);
1310 for(i = 0; i < bits; i++) {
1311 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1318 *val = (FLAC__int64)v;
1322 *val = (FLAC__int64)v;
1328 unsigned i, bits_ = bits;
1331 FLAC__ASSERT(bb != 0);
1332 FLAC__ASSERT(bb->buffer != 0);
1334 FLAC__ASSERT(bits <= 64);
1335 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1337 while(bb->total_consumed_bits + bits > bb->total_bits) {
1338 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1341 if(bb->consumed_bits) {
1342 i = 8 - bb->consumed_bits;
1344 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1346 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1347 bb->consumed_bytes++;
1348 bb->consumed_bits = 0;
1349 /* we hold off updating bb->total_consumed_bits until the end */
1352 /* bits_ must be < 7 if we get to here */
1353 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1355 *val = (FLAC__int64)v;
1356 *val >>= (64-bits_);
1357 bb->consumed_bits += bits_;
1358 bb->total_consumed_bits += bits_;
1364 v |= bb->buffer[bb->consumed_bytes];
1366 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1367 bb->consumed_bytes++;
1368 /* bb->consumed_bits is already 0 */
1369 /* we hold off updating bb->total_consumed_bits until the end */
1373 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1374 bb->consumed_bits = bits_;
1375 /* we hold off updating bb->total_consumed_bits until the end */
1377 bb->total_consumed_bits += bits;
1383 *val = (FLAC__int64)v;
1387 *val = (FLAC__int64)v;
1393 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)
1394 #ifdef FLAC__NO_MANUAL_INLINING
1396 unsigned bit, val_ = 0;
1398 FLAC__ASSERT(bb != 0);
1399 FLAC__ASSERT(bb->buffer != 0);
1402 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1414 unsigned i, val_ = 0;
1415 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1418 FLAC__ASSERT(bb != 0);
1419 FLAC__ASSERT(bb->buffer != 0);
1421 if(bb->consumed_bits) {
1422 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1424 for(i = 0; !(b & 0x80); i++)
1428 bb->consumed_bits += i;
1429 bb->total_consumed_bits += i;
1430 if(bb->consumed_bits == 8) {
1431 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1432 bb->consumed_bytes++;
1433 bb->consumed_bits = 0;
1438 val_ = 8 - bb->consumed_bits;
1439 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1440 bb->consumed_bytes++;
1441 bb->consumed_bits = 0;
1442 bb->total_consumed_bits += val_;
1446 if(bb->consumed_bytes >= total_bytes_) {
1447 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1449 total_bytes_ = (bb->total_bits + 7) / 8;
1451 b = bb->buffer[bb->consumed_bytes];
1453 for(i = 0; !(b & 0x80); i++)
1457 bb->consumed_bits = i;
1460 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1461 bb->consumed_bytes++;
1462 bb->consumed_bits = 0;
1464 bb->total_consumed_bits += i;
1469 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1470 bb->consumed_bytes++;
1471 /* bb->consumed_bits is already 0 */
1472 /* we hold off updating bb->total_consumed_bits until the end */
1473 bb->total_consumed_bits += 8;
1479 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)
1481 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1483 FLAC__ASSERT(bb != 0);
1484 FLAC__ASSERT(bb->buffer != 0);
1485 FLAC__ASSERT(parameter <= 31);
1487 /* read the unary MSBs and end bit */
1488 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1491 /* read the sign bit */
1492 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1495 /* read the binary LSBs */
1496 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1499 /* compose the value */
1500 *val = (msbs << parameter) | lsbs;
1507 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)
1509 FLAC__uint32 lsbs = 0, msbs = 0;
1512 FLAC__ASSERT(bb != 0);
1513 FLAC__ASSERT(bb->buffer != 0);
1514 FLAC__ASSERT(parameter <= 31);
1516 /* read the unary MSBs and end bit */
1517 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1520 /* read the binary LSBs */
1521 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1524 /* compose the value */
1525 uval = (msbs << parameter) | lsbs;
1527 *val = -((int)(uval >> 1)) - 1;
1529 *val = (int)(uval >> 1);
1534 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)
1536 FLAC__uint32 lsbs = 0, msbs = 0;
1537 unsigned bit, uval, k;
1539 FLAC__ASSERT(bb != 0);
1540 FLAC__ASSERT(bb->buffer != 0);
1542 k = FLAC__bitmath_ilog2(parameter);
1544 /* read the unary MSBs and end bit */
1545 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1548 /* read the binary LSBs */
1549 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1552 if(parameter == 1u<<k) {
1553 /* compose the value */
1554 uval = (msbs << k) | lsbs;
1557 unsigned d = (1 << (k+1)) - parameter;
1559 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1565 /* compose the value */
1566 uval = msbs * parameter + lsbs;
1569 /* unfold unsigned to signed */
1571 *val = -((int)(uval >> 1)) - 1;
1573 *val = (int)(uval >> 1);
1578 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)
1580 FLAC__uint32 lsbs, msbs = 0;
1583 FLAC__ASSERT(bb != 0);
1584 FLAC__ASSERT(bb->buffer != 0);
1586 k = FLAC__bitmath_ilog2(parameter);
1588 /* read the unary MSBs and end bit */
1589 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1592 /* read the binary LSBs */
1593 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1596 if(parameter == 1u<<k) {
1597 /* compose the value */
1598 *val = (msbs << k) | lsbs;
1601 unsigned d = (1 << (k+1)) - parameter;
1603 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1609 /* compose the value */
1610 *val = msbs * parameter + lsbs;
1616 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1617 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)
1623 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1626 raw[(*rawlen)++] = (FLAC__byte)x;
1627 if(!(x & 0x80)) { /* 0xxxxxxx */
1631 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1635 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1639 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1643 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1647 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1656 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1659 raw[(*rawlen)++] = (FLAC__byte)x;
1660 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1671 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1672 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)
1678 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1681 raw[(*rawlen)++] = (FLAC__byte)x;
1682 if(!(x & 0x80)) { /* 0xxxxxxx */
1686 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1690 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1694 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1698 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1702 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1706 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1711 *val = 0xffffffffffffffff;
1715 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1718 raw[(*rawlen)++] = (FLAC__byte)x;
1719 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1720 *val = 0xffffffffffffffff;
1730 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1734 fprintf(out, "bitbuffer is NULL\n");
1737 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);
1738 for(i = 0; i < bb->bytes; i++) {
1739 fprintf(out, "%08X: ", i);
1740 for(j = 0; j < 8; j++)
1741 if(i*8+j < bb->total_consumed_bits)
1744 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1748 fprintf(out, "%08X: ", i);
1749 for(j = 0; j < bb->bits; j++)
1750 if(i*8+j < bb->total_consumed_bits)
1753 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);