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] = val;
298 bb->buffer[bb->bytes++] = val;
303 bb->buffer[bb->bytes++] = 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] = val;
379 bb->buffer[bb->bytes++] = val;
384 bb->buffer[bb->bytes++] = 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);
1134 for(i = 0; i < bits; i++) {
1135 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1143 *val = (FLAC__int32)v;
1147 *val = (FLAC__int32)v;
1153 unsigned i, bits_ = bits;
1156 FLAC__ASSERT(bb != 0);
1157 FLAC__ASSERT(bb->buffer != 0);
1159 FLAC__ASSERT(bits <= 32);
1160 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1162 while(bb->total_consumed_bits + bits > bb->total_bits) {
1163 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1166 if(bb->consumed_bits) {
1167 i = 8 - bb->consumed_bits;
1169 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1171 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1172 bb->consumed_bytes++;
1173 bb->consumed_bits = 0;
1174 /* we hold off updating bb->total_consumed_bits until the end */
1177 /* bits_ must be < 7 if we get to here */
1178 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1180 *val = (FLAC__int32)v;
1181 *val >>= (32-bits_);
1182 bb->consumed_bits += bits_;
1183 bb->total_consumed_bits += bits_;
1189 v |= bb->buffer[bb->consumed_bytes];
1191 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1192 bb->consumed_bytes++;
1193 /* bb->consumed_bits is already 0 */
1194 /* we hold off updating bb->total_consumed_bits until the end */
1198 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1199 bb->consumed_bits = bits_;
1200 /* we hold off updating bb->total_consumed_bits until the end */
1202 bb->total_consumed_bits += bits;
1208 *val = (FLAC__int32)v;
1212 *val = (FLAC__int32)v;
1218 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)
1219 #ifdef FLAC__NO_MANUAL_INLINING
1223 FLAC__ASSERT(bb != 0);
1224 FLAC__ASSERT(bb->buffer != 0);
1226 FLAC__ASSERT(bits <= 64);
1229 for(i = 0; i < bits; i++) {
1230 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1237 unsigned i, bits_ = bits;
1240 FLAC__ASSERT(bb != 0);
1241 FLAC__ASSERT(bb->buffer != 0);
1243 FLAC__ASSERT(bits <= 64);
1244 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1246 while(bb->total_consumed_bits + bits > bb->total_bits) {
1247 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1250 if(bb->consumed_bits) {
1251 i = 8 - bb->consumed_bits;
1253 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1255 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1256 bb->consumed_bytes++;
1257 bb->consumed_bits = 0;
1258 /* we hold off updating bb->total_consumed_bits until the end */
1261 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1262 bb->consumed_bits += bits_;
1263 bb->total_consumed_bits += bits_;
1269 v |= bb->buffer[bb->consumed_bytes];
1271 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1272 bb->consumed_bytes++;
1273 /* bb->consumed_bits is already 0 */
1274 /* we hold off updating bb->total_consumed_bits until the end */
1278 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1279 bb->consumed_bits = bits_;
1280 /* we hold off updating bb->total_consumed_bits until the end */
1282 bb->total_consumed_bits += bits;
1288 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)
1289 #ifdef FLAC__NO_MANUAL_INLINING
1294 FLAC__ASSERT(bb != 0);
1295 FLAC__ASSERT(bb->buffer != 0);
1297 FLAC__ASSERT(bits <= 64);
1300 for(i = 0; i < bits; i++) {
1301 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1308 *val = (FLAC__int64)v;
1312 *val = (FLAC__int64)v;
1318 unsigned i, bits_ = bits;
1321 FLAC__ASSERT(bb != 0);
1322 FLAC__ASSERT(bb->buffer != 0);
1324 FLAC__ASSERT(bits <= 64);
1325 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1327 while(bb->total_consumed_bits + bits > bb->total_bits) {
1328 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1331 if(bb->consumed_bits) {
1332 i = 8 - bb->consumed_bits;
1334 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1336 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1337 bb->consumed_bytes++;
1338 bb->consumed_bits = 0;
1339 /* we hold off updating bb->total_consumed_bits until the end */
1342 /* bits_ must be < 7 if we get to here */
1343 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1345 *val = (FLAC__int64)v;
1346 *val >>= (64-bits_);
1347 bb->consumed_bits += bits_;
1348 bb->total_consumed_bits += bits_;
1354 v |= bb->buffer[bb->consumed_bytes];
1356 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1357 bb->consumed_bytes++;
1358 /* bb->consumed_bits is already 0 */
1359 /* we hold off updating bb->total_consumed_bits until the end */
1363 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1364 bb->consumed_bits = bits_;
1365 /* we hold off updating bb->total_consumed_bits until the end */
1367 bb->total_consumed_bits += bits;
1373 *val = (FLAC__int64)v;
1377 *val = (FLAC__int64)v;
1383 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)
1384 #ifdef FLAC__NO_MANUAL_INLINING
1386 unsigned bit, val_ = 0;
1388 FLAC__ASSERT(bb != 0);
1389 FLAC__ASSERT(bb->buffer != 0);
1392 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1404 unsigned i, val_ = 0;
1405 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1408 FLAC__ASSERT(bb != 0);
1409 FLAC__ASSERT(bb->buffer != 0);
1411 if(bb->consumed_bits) {
1412 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1414 for(i = 0; !(b & 0x80); i++)
1418 bb->consumed_bits += i;
1419 bb->total_consumed_bits += i;
1420 if(bb->consumed_bits == 8) {
1421 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1422 bb->consumed_bytes++;
1423 bb->consumed_bits = 0;
1428 val_ = 8 - bb->consumed_bits;
1429 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1430 bb->consumed_bytes++;
1431 bb->consumed_bits = 0;
1432 bb->total_consumed_bits += val_;
1436 if(bb->consumed_bytes >= total_bytes_) {
1437 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1439 total_bytes_ = (bb->total_bits + 7) / 8;
1441 b = bb->buffer[bb->consumed_bytes];
1443 for(i = 0; !(b & 0x80); i++)
1447 bb->consumed_bits = i;
1450 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1451 bb->consumed_bytes++;
1452 bb->consumed_bits = 0;
1454 bb->total_consumed_bits += i;
1459 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1460 bb->consumed_bytes++;
1461 /* bb->consumed_bits is already 0 */
1462 /* we hold off updating bb->total_consumed_bits until the end */
1463 bb->total_consumed_bits += 8;
1469 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)
1471 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1473 FLAC__ASSERT(bb != 0);
1474 FLAC__ASSERT(bb->buffer != 0);
1475 FLAC__ASSERT(parameter <= 31);
1477 /* read the unary MSBs and end bit */
1478 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1481 /* read the sign bit */
1482 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1485 /* read the binary LSBs */
1486 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1489 /* compose the value */
1490 *val = (msbs << parameter) | lsbs;
1497 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)
1499 FLAC__uint32 lsbs = 0, msbs = 0;
1502 FLAC__ASSERT(bb != 0);
1503 FLAC__ASSERT(bb->buffer != 0);
1504 FLAC__ASSERT(parameter <= 31);
1506 /* read the unary MSBs and end bit */
1507 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1510 /* read the binary LSBs */
1511 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1514 /* compose the value */
1515 uval = (msbs << parameter) | lsbs;
1517 *val = -((int)(uval >> 1)) - 1;
1519 *val = (int)(uval >> 1);
1524 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)
1526 FLAC__uint32 lsbs = 0, msbs = 0;
1527 unsigned bit, uval, k;
1529 FLAC__ASSERT(bb != 0);
1530 FLAC__ASSERT(bb->buffer != 0);
1532 k = FLAC__bitmath_ilog2(parameter);
1534 /* read the unary MSBs and end bit */
1535 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1538 /* read the binary LSBs */
1539 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1542 if(parameter == 1u<<k) {
1543 /* compose the value */
1544 uval = (msbs << k) | lsbs;
1547 unsigned d = (1 << (k+1)) - parameter;
1549 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1555 /* compose the value */
1556 uval = msbs * parameter + lsbs;
1559 /* unfold unsigned to signed */
1561 *val = -((int)(uval >> 1)) - 1;
1563 *val = (int)(uval >> 1);
1568 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)
1570 FLAC__uint32 lsbs, msbs = 0;
1573 FLAC__ASSERT(bb != 0);
1574 FLAC__ASSERT(bb->buffer != 0);
1576 k = FLAC__bitmath_ilog2(parameter);
1578 /* read the unary MSBs and end bit */
1579 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1582 /* read the binary LSBs */
1583 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1586 if(parameter == 1u<<k) {
1587 /* compose the value */
1588 *val = (msbs << k) | lsbs;
1591 unsigned d = (1 << (k+1)) - parameter;
1593 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1599 /* compose the value */
1600 *val = msbs * parameter + lsbs;
1606 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1607 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)
1613 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1616 raw[(*rawlen)++] = (FLAC__byte)x;
1617 if(!(x & 0x80)) { /* 0xxxxxxx */
1621 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1625 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1629 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1633 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1637 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1646 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1649 raw[(*rawlen)++] = (FLAC__byte)x;
1650 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1661 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1662 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)
1668 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1671 raw[(*rawlen)++] = (FLAC__byte)x;
1672 if(!(x & 0x80)) { /* 0xxxxxxx */
1676 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1680 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1684 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1688 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1692 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1696 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1701 *val = 0xffffffffffffffff;
1705 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1708 raw[(*rawlen)++] = (FLAC__byte)x;
1709 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1710 *val = 0xffffffffffffffff;
1720 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1724 fprintf(out, "bitbuffer is NULL\n");
1727 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);
1728 for(i = 0; i < bb->bytes; i++) {
1729 fprintf(out, "%08X: ", i);
1730 for(j = 0; j < 8; j++)
1731 if(i*8+j < bb->total_consumed_bits)
1734 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1738 fprintf(out, "%08X: ", i);
1739 for(j = 0; j < bb->bits; j++)
1740 if(i*8+j < bb->total_consumed_bits)
1743 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);