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 free(bb->buffer); // we've already asserted above that (bb->buffer != 0)
79 bb->buffer = new_buffer;
80 bb->capacity = new_capacity;
84 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
86 unsigned new_capacity;
88 FLAC__ASSERT(min_bytes_to_add > 0);
90 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
91 return bitbuffer_resize_(bb, new_capacity);
94 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
96 FLAC__ASSERT(bb != 0);
97 FLAC__ASSERT(bb->buffer != 0);
99 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
100 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
105 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)
109 /* first shift the unconsumed buffer data toward the front as much as possible */
110 if(bb->total_consumed_bits >= 8) {
111 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
112 for( ; r < r_end; l++, r++)
113 bb->buffer[l] = bb->buffer[r];
114 for( ; l < r_end; l++)
116 bb->bytes -= bb->consumed_bytes;
117 bb->total_bits -= (bb->consumed_bytes<<3);
118 bb->consumed_bytes = 0;
119 bb->total_consumed_bits = bb->consumed_bits;
121 /* grow if we need to */
122 if(bb->capacity <= 1) {
123 if(!bitbuffer_resize_(bb, 16))
126 /* finally, read in some data */
127 bytes = bb->capacity - bb->bytes;
128 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
131 bb->total_bits += (bytes<<3);
135 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
137 FLAC__ASSERT(bb != 0);
141 bb->bytes = bb->bits = bb->total_bits = 0;
142 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
145 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
147 FLAC__ASSERT(bb != 0);
148 FLAC__bitbuffer_init(bb);
153 FLAC__ASSERT(buffer != 0);
154 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte)*bytes);
157 memcpy(bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
158 bb->capacity = bb->bytes = bytes;
160 bb->total_bits = (bytes<<3);
161 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
166 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
168 FLAC__ASSERT(bb != 0);
170 bb->read_crc16 = seed;
173 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
175 static const FLAC__byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
176 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
178 FLAC__ASSERT(dest != 0);
179 FLAC__ASSERT(src != 0);
183 if(dest->bits != src->consumed_bits)
185 if(!bitbuffer_ensure_size_(dest, bits_to_add))
187 if(dest->bits == 0) {
188 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
190 else if(dest->bits + bits_to_add > 8) {
191 dest->buffer[dest->bytes] <<= (8 - dest->bits);
192 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
193 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
196 dest->buffer[dest->bytes] <<= bits_to_add;
197 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
199 dest->bits = src->bits;
200 dest->total_bits += bits_to_add;
201 dest->bytes = dest->total_bits / 8;
206 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
208 FLAC__ASSERT(bb != 0);
214 bb->bytes = bb->bits = bb->total_bits = 0;
215 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
218 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
220 if(bb->buffer == 0) {
221 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
222 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * bb->capacity);
225 memset(bb->buffer, 0, bb->capacity);
228 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
230 bb->bytes = bb->bits = bb->total_bits = 0;
231 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
235 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
237 FLAC__ASSERT(dest != 0);
238 FLAC__ASSERT(dest->buffer != 0);
239 FLAC__ASSERT(src != 0);
240 FLAC__ASSERT(src->buffer != 0);
242 if(dest->capacity < src->capacity)
243 if(!bitbuffer_resize_(dest, src->capacity))
245 memcpy(dest->buffer, src->buffer, sizeof(FLAC__byte)*min(src->capacity, src->bytes+1));
246 dest->bytes = src->bytes;
247 dest->bits = src->bits;
248 dest->total_bits = src->total_bits;
249 dest->consumed_bytes = src->consumed_bytes;
250 dest->consumed_bits = src->consumed_bits;
251 dest->total_consumed_bits = src->total_consumed_bits;
252 dest->read_crc16 = src->read_crc16;
256 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
260 FLAC__ASSERT(bb != 0);
261 FLAC__ASSERT(bb->buffer != 0);
265 if(!bitbuffer_ensure_size_(bb, bits))
267 bb->total_bits += bits;
269 n = min(8 - bb->bits, bits);
270 bb->buffer[bb->bytes] <<= n;
281 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
285 FLAC__ASSERT(bb != 0);
286 FLAC__ASSERT(bb->buffer != 0);
288 FLAC__ASSERT(bits <= 32);
291 /* inline the size check so we don't incure a function call unnecessarily */
292 if((bb->capacity<<3) < bb->total_bits + bits) {
293 if(!bitbuffer_ensure_size_(bb, bits))
296 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
297 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
298 bb->total_bits += bits;
301 if(n == 8) { /* i.e. bb->bits == 0 */
303 bb->buffer[bb->bytes] = (FLAC__byte)val;
308 bb->buffer[bb->bytes++] = (FLAC__byte)val;
313 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
314 val &= (~(0xffffffff << k));
319 bb->buffer[bb->bytes] <<= bits;
320 bb->buffer[bb->bytes] |= val;
331 bb->buffer[bb->bytes] <<= n;
332 bb->buffer[bb->bytes] |= (val>>k);
333 val &= (~(0xffffffff << k));
343 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
345 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
348 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
350 static const FLAC__uint64 mask[] = {
352 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
353 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
354 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
355 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
356 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
357 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
358 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
359 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
360 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
361 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
362 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
363 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
364 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
365 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
366 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
367 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
371 FLAC__ASSERT(bb != 0);
372 FLAC__ASSERT(bb->buffer != 0);
374 FLAC__ASSERT(bits <= 64);
377 if(!bitbuffer_ensure_size_(bb, bits))
380 bb->total_bits += bits;
384 bb->buffer[bb->bytes] = (FLAC__byte)val;
389 bb->buffer[bb->bytes++] = (FLAC__byte)val;
394 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
395 val &= (~(0xffffffffffffffff << k));
400 n = min(8 - bb->bits, bits);
402 bb->buffer[bb->bytes] <<= n;
403 bb->buffer[bb->bytes] |= (val>>k);
404 val &= (~(0xffffffffffffffff << k));
417 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
419 return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
422 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
425 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
427 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
429 if(!FLAC__bitbuffer_write_zeroes(bb, val))
431 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
435 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
439 /* fold signed to unsigned */
442 * (unsigned)(((--val) << 1) - 1);
443 * but without the overflow problem at MININT
445 uval = (unsigned)(((-(++val)) << 1) + 1);
447 uval = (unsigned)(val << 1);
449 msbs = uval >> parameter;
451 return 1 + parameter + msbs;
454 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
456 unsigned bits, msbs, uval;
459 FLAC__ASSERT(parameter > 0);
461 /* fold signed to unsigned */
464 * (unsigned)(((--val) << 1) - 1);
465 * but without the overflow problem at MININT
467 uval = (unsigned)(((-(++val)) << 1) + 1);
469 uval = (unsigned)(val << 1);
471 k = FLAC__bitmath_ilog2(parameter);
472 if(parameter == 1u<<k) {
473 FLAC__ASSERT(k <= 30);
481 d = (1 << (k+1)) - parameter;
482 q = uval / parameter;
483 r = uval - (q * parameter);
492 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
497 FLAC__ASSERT(parameter > 0);
499 k = FLAC__bitmath_ilog2(parameter);
500 if(parameter == 1u<<k) {
501 FLAC__ASSERT(k <= 30);
509 d = (1 << (k+1)) - parameter;
510 q = uval / parameter;
511 r = uval - (q * parameter);
520 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
522 unsigned total_bits, interesting_bits, msbs;
523 FLAC__uint32 pattern;
525 FLAC__ASSERT(bb != 0);
526 FLAC__ASSERT(bb->buffer != 0);
527 FLAC__ASSERT(parameter <= 31);
529 /* init pattern with the unary end bit and the sign bit */
537 msbs = val >> parameter;
538 interesting_bits = 2 + parameter;
539 total_bits = interesting_bits + msbs;
540 pattern <<= parameter;
541 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
543 if(total_bits <= 32) {
544 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
548 /* write the unary MSBs */
549 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
551 /* write the unary end bit, the sign bit, and binary LSBs */
552 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
558 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
560 unsigned total_bits, interesting_bits, msbs;
561 FLAC__uint32 pattern;
563 FLAC__ASSERT(bb != 0);
564 FLAC__ASSERT(bb->buffer != 0);
565 FLAC__ASSERT(parameter <= 31);
569 /* init pattern with the unary end bit and the sign bit */
577 msbs = val >> parameter;
578 interesting_bits = 2 + parameter;
579 total_bits = interesting_bits + msbs;
580 pattern <<= parameter;
581 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
583 if(total_bits <= 32) {
584 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
587 else if(total_bits > max_bits) {
592 /* write the unary MSBs */
593 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
595 /* write the unary end bit, the sign bit, and binary LSBs */
596 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
602 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
604 unsigned total_bits, val_bits;
605 FLAC__uint32 pattern;
607 FLAC__ASSERT(bb != 0);
608 FLAC__ASSERT(bb->buffer != 0);
609 FLAC__ASSERT(parameter <= 31);
611 val_bits = FLAC__bitmath_silog2(val);
612 total_bits = 2 + parameter + 5 + val_bits;
614 if(total_bits <= 32) {
616 pattern <<= (parameter + 5);
618 pattern <<= val_bits;
619 pattern |= (val & ((1 << val_bits) - 1));
620 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
624 /* write the '-0' escape code first */
625 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
627 /* write the length */
628 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
630 /* write the value */
631 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
637 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
639 unsigned total_bits, interesting_bits, msbs, uval;
640 FLAC__uint32 pattern;
642 FLAC__ASSERT(bb != 0);
643 FLAC__ASSERT(bb->buffer != 0);
644 FLAC__ASSERT(parameter <= 30);
646 /* fold signed to unsigned */
649 * (unsigned)(((--val) << 1) - 1);
650 * but without the overflow problem at MININT
652 uval = (unsigned)(((-(++val)) << 1) + 1);
654 uval = (unsigned)(val << 1);
656 msbs = uval >> parameter;
657 interesting_bits = 1 + parameter;
658 total_bits = interesting_bits + msbs;
659 pattern = 1 << parameter; /* the unary end bit */
660 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
662 if(total_bits <= 32) {
663 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
667 /* write the unary MSBs */
668 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
670 /* write the unary end bit and binary LSBs */
671 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
677 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
679 unsigned total_bits, interesting_bits, msbs, uval;
680 FLAC__uint32 pattern;
682 FLAC__ASSERT(bb != 0);
683 FLAC__ASSERT(bb->buffer != 0);
684 FLAC__ASSERT(parameter <= 30);
688 /* fold signed to unsigned */
691 * (unsigned)(((--val) << 1) - 1);
692 * but without the overflow problem at MININT
694 uval = (unsigned)(((-(++val)) << 1) + 1);
696 uval = (unsigned)(val << 1);
698 msbs = uval >> parameter;
699 interesting_bits = 1 + parameter;
700 total_bits = interesting_bits + msbs;
701 pattern = 1 << parameter; /* the unary end bit */
702 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
704 if(total_bits <= 32) {
705 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
708 else if(total_bits > max_bits) {
713 /* write the unary MSBs */
714 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
716 /* write the unary end bit and binary LSBs */
717 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
723 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
725 unsigned total_bits, msbs, uval;
728 FLAC__ASSERT(bb != 0);
729 FLAC__ASSERT(bb->buffer != 0);
730 FLAC__ASSERT(parameter > 0);
732 /* fold signed to unsigned */
735 * (unsigned)(((--val) << 1) - 1);
736 * but without the overflow problem at MININT
738 uval = (unsigned)(((-(++val)) << 1) + 1);
740 uval = (unsigned)(val << 1);
742 k = FLAC__bitmath_ilog2(parameter);
743 if(parameter == 1u<<k) {
746 FLAC__ASSERT(k <= 30);
749 total_bits = 1 + k + msbs;
750 pattern = 1 << k; /* the unary end bit */
751 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
753 if(total_bits <= 32) {
754 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
758 /* write the unary MSBs */
759 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
761 /* write the unary end bit and binary LSBs */
762 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
769 d = (1 << (k+1)) - parameter;
770 q = uval / parameter;
771 r = uval - (q * parameter);
772 /* write the unary MSBs */
773 if(!FLAC__bitbuffer_write_zeroes(bb, q))
775 /* write the unary end bit */
776 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
778 /* write the binary LSBs */
780 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
784 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
791 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
793 unsigned total_bits, msbs;
796 FLAC__ASSERT(bb != 0);
797 FLAC__ASSERT(bb->buffer != 0);
798 FLAC__ASSERT(parameter > 0);
800 k = FLAC__bitmath_ilog2(parameter);
801 if(parameter == 1u<<k) {
804 FLAC__ASSERT(k <= 30);
807 total_bits = 1 + k + msbs;
808 pattern = 1 << k; /* the unary end bit */
809 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
811 if(total_bits <= 32) {
812 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
816 /* write the unary MSBs */
817 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
819 /* write the unary end bit and binary LSBs */
820 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
827 d = (1 << (k+1)) - parameter;
828 q = uval / parameter;
829 r = uval - (q * parameter);
830 /* write the unary MSBs */
831 if(!FLAC__bitbuffer_write_zeroes(bb, q))
833 /* write the unary end bit */
834 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
836 /* write the binary LSBs */
838 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
842 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
849 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
853 FLAC__ASSERT(bb != 0);
854 FLAC__ASSERT(bb->buffer != 0);
856 FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
859 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
861 else if(val < 0x800) {
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
865 else if(val < 0x10000) {
866 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 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 < 0x200000) {
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
873 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
874 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
876 else if(val < 0x4000000) {
877 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
880 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
881 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
884 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
885 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
886 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
887 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
888 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
889 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
895 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
899 FLAC__ASSERT(bb != 0);
900 FLAC__ASSERT(bb->buffer != 0);
902 FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
905 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
907 else if(val < 0x800) {
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
911 else if(val < 0x10000) {
912 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 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 < 0x200000) {
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
919 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
920 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
922 else if(val < 0x4000000) {
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
929 else if(val < 0x80000000) {
930 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
931 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
935 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
938 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
939 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
940 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
941 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
942 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
943 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
944 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
950 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
952 /* 0-pad to byte boundary */
954 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
959 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)
961 /* to avoid a drastic speed penalty we don't:
962 FLAC__ASSERT(bb != 0);
963 FLAC__ASSERT(bb->buffer != 0);
964 FLAC__ASSERT(bb->bits == 0);
968 if(bb->total_consumed_bits < bb->total_bits) {
969 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
973 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
979 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)
981 /* to avoid a drastic speed penalty we don't:
982 FLAC__ASSERT(bb != 0);
983 FLAC__ASSERT(bb->buffer != 0);
984 FLAC__ASSERT(bb->bits == 0);
988 if(bb->total_consumed_bits < bb->total_bits) {
989 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
991 if(bb->consumed_bits == 8) {
992 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
993 bb->consumed_bytes++;
994 bb->consumed_bits = 0;
996 bb->total_consumed_bits++;
1000 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1006 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)
1008 /* to avoid a drastic speed penalty we don't:
1009 FLAC__ASSERT(bb != 0);
1010 FLAC__ASSERT(bb->buffer != 0);
1011 FLAC__ASSERT(bb->bits == 0);
1015 if(bb->total_consumed_bits < bb->total_bits) {
1017 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1018 bb->consumed_bits++;
1019 if(bb->consumed_bits == 8) {
1020 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1021 bb->consumed_bytes++;
1022 bb->consumed_bits = 0;
1024 bb->total_consumed_bits++;
1028 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1034 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)
1036 /* to avoid a drastic speed penalty we don't:
1037 FLAC__ASSERT(bb != 0);
1038 FLAC__ASSERT(bb->buffer != 0);
1039 FLAC__ASSERT(bb->bits == 0);
1043 if(bb->total_consumed_bits < bb->total_bits) {
1045 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1046 bb->consumed_bits++;
1047 if(bb->consumed_bits == 8) {
1048 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1049 bb->consumed_bytes++;
1050 bb->consumed_bits = 0;
1052 bb->total_consumed_bits++;
1056 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1062 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)
1063 #ifdef FLAC__NO_MANUAL_INLINING
1067 FLAC__ASSERT(bb != 0);
1068 FLAC__ASSERT(bb->buffer != 0);
1070 FLAC__ASSERT(bits <= 32);
1073 for(i = 0; i < bits; i++) {
1074 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1081 unsigned i, bits_ = bits;
1084 FLAC__ASSERT(bb != 0);
1085 FLAC__ASSERT(bb->buffer != 0);
1087 FLAC__ASSERT(bits <= 32);
1088 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1090 while(bb->total_consumed_bits + bits > bb->total_bits) {
1091 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1094 if(bb->consumed_bits) {
1095 i = 8 - bb->consumed_bits;
1097 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1099 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1100 bb->consumed_bytes++;
1101 bb->consumed_bits = 0;
1102 /* we hold off updating bb->total_consumed_bits until the end */
1105 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1106 bb->consumed_bits += bits_;
1107 bb->total_consumed_bits += bits_;
1113 v |= bb->buffer[bb->consumed_bytes];
1115 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1116 bb->consumed_bytes++;
1117 /* bb->consumed_bits is already 0 */
1118 /* we hold off updating bb->total_consumed_bits until the end */
1122 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1123 bb->consumed_bits = bits_;
1124 /* we hold off updating bb->total_consumed_bits until the end */
1126 bb->total_consumed_bits += bits;
1132 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)
1133 #ifdef FLAC__NO_MANUAL_INLINING
1138 FLAC__ASSERT(bb != 0);
1139 FLAC__ASSERT(bb->buffer != 0);
1141 FLAC__ASSERT(bits <= 32);
1149 for(i = 0; i < bits; i++) {
1150 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1158 *val = (FLAC__int32)v;
1162 *val = (FLAC__int32)v;
1168 unsigned i, bits_ = bits;
1171 FLAC__ASSERT(bb != 0);
1172 FLAC__ASSERT(bb->buffer != 0);
1174 FLAC__ASSERT(bits <= 32);
1175 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1182 while(bb->total_consumed_bits + bits > bb->total_bits) {
1183 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1186 if(bb->consumed_bits) {
1187 i = 8 - bb->consumed_bits;
1189 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1191 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1192 bb->consumed_bytes++;
1193 bb->consumed_bits = 0;
1194 /* we hold off updating bb->total_consumed_bits until the end */
1197 /* bits_ must be < 7 if we get to here */
1198 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1200 *val = (FLAC__int32)v;
1201 *val >>= (32-bits_);
1202 bb->consumed_bits += bits_;
1203 bb->total_consumed_bits += bits_;
1209 v |= bb->buffer[bb->consumed_bytes];
1211 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1212 bb->consumed_bytes++;
1213 /* bb->consumed_bits is already 0 */
1214 /* we hold off updating bb->total_consumed_bits until the end */
1218 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1219 bb->consumed_bits = bits_;
1220 /* we hold off updating bb->total_consumed_bits until the end */
1222 bb->total_consumed_bits += bits;
1228 *val = (FLAC__int32)v;
1232 *val = (FLAC__int32)v;
1238 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)
1239 #ifdef FLAC__NO_MANUAL_INLINING
1243 FLAC__ASSERT(bb != 0);
1244 FLAC__ASSERT(bb->buffer != 0);
1246 FLAC__ASSERT(bits <= 64);
1249 for(i = 0; i < bits; i++) {
1250 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1257 unsigned i, bits_ = bits;
1260 FLAC__ASSERT(bb != 0);
1261 FLAC__ASSERT(bb->buffer != 0);
1263 FLAC__ASSERT(bits <= 64);
1264 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1266 while(bb->total_consumed_bits + bits > bb->total_bits) {
1267 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1270 if(bb->consumed_bits) {
1271 i = 8 - bb->consumed_bits;
1273 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1275 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1276 bb->consumed_bytes++;
1277 bb->consumed_bits = 0;
1278 /* we hold off updating bb->total_consumed_bits until the end */
1281 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1282 bb->consumed_bits += bits_;
1283 bb->total_consumed_bits += bits_;
1289 v |= bb->buffer[bb->consumed_bytes];
1291 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1292 bb->consumed_bytes++;
1293 /* bb->consumed_bits is already 0 */
1294 /* we hold off updating bb->total_consumed_bits until the end */
1298 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1299 bb->consumed_bits = bits_;
1300 /* we hold off updating bb->total_consumed_bits until the end */
1302 bb->total_consumed_bits += bits;
1308 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)
1309 #ifdef FLAC__NO_MANUAL_INLINING
1314 FLAC__ASSERT(bb != 0);
1315 FLAC__ASSERT(bb->buffer != 0);
1317 FLAC__ASSERT(bits <= 64);
1320 for(i = 0; i < bits; i++) {
1321 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1328 *val = (FLAC__int64)v;
1332 *val = (FLAC__int64)v;
1338 unsigned i, bits_ = bits;
1341 FLAC__ASSERT(bb != 0);
1342 FLAC__ASSERT(bb->buffer != 0);
1344 FLAC__ASSERT(bits <= 64);
1345 FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1347 while(bb->total_consumed_bits + bits > bb->total_bits) {
1348 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1351 if(bb->consumed_bits) {
1352 i = 8 - bb->consumed_bits;
1354 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1356 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1357 bb->consumed_bytes++;
1358 bb->consumed_bits = 0;
1359 /* we hold off updating bb->total_consumed_bits until the end */
1362 /* bits_ must be < 7 if we get to here */
1363 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1365 *val = (FLAC__int64)v;
1366 *val >>= (64-bits_);
1367 bb->consumed_bits += bits_;
1368 bb->total_consumed_bits += bits_;
1374 v |= bb->buffer[bb->consumed_bytes];
1376 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1377 bb->consumed_bytes++;
1378 /* bb->consumed_bits is already 0 */
1379 /* we hold off updating bb->total_consumed_bits until the end */
1383 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1384 bb->consumed_bits = bits_;
1385 /* we hold off updating bb->total_consumed_bits until the end */
1387 bb->total_consumed_bits += bits;
1393 *val = (FLAC__int64)v;
1397 *val = (FLAC__int64)v;
1403 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)
1404 #ifdef FLAC__NO_MANUAL_INLINING
1406 unsigned bit, val_ = 0;
1408 FLAC__ASSERT(bb != 0);
1409 FLAC__ASSERT(bb->buffer != 0);
1412 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1424 unsigned i, val_ = 0;
1425 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1428 FLAC__ASSERT(bb != 0);
1429 FLAC__ASSERT(bb->buffer != 0);
1431 if(bb->consumed_bits) {
1432 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1434 for(i = 0; !(b & 0x80); i++)
1438 bb->consumed_bits += i;
1439 bb->total_consumed_bits += i;
1440 if(bb->consumed_bits == 8) {
1441 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1442 bb->consumed_bytes++;
1443 bb->consumed_bits = 0;
1448 val_ = 8 - bb->consumed_bits;
1449 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1450 bb->consumed_bytes++;
1451 bb->consumed_bits = 0;
1452 bb->total_consumed_bits += val_;
1456 if(bb->consumed_bytes >= total_bytes_) {
1457 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1459 total_bytes_ = (bb->total_bits + 7) / 8;
1461 b = bb->buffer[bb->consumed_bytes];
1463 for(i = 0; !(b & 0x80); i++)
1467 bb->consumed_bits = i;
1470 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1471 bb->consumed_bytes++;
1472 bb->consumed_bits = 0;
1474 bb->total_consumed_bits += i;
1479 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1480 bb->consumed_bytes++;
1481 /* bb->consumed_bits is already 0 */
1482 /* we hold off updating bb->total_consumed_bits until the end */
1483 bb->total_consumed_bits += 8;
1489 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)
1491 FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1493 FLAC__ASSERT(bb != 0);
1494 FLAC__ASSERT(bb->buffer != 0);
1495 FLAC__ASSERT(parameter <= 31);
1497 /* read the unary MSBs and end bit */
1498 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1501 /* read the sign bit */
1502 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1505 /* read the binary LSBs */
1506 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1509 /* compose the value */
1510 *val = (msbs << parameter) | lsbs;
1517 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)
1519 FLAC__uint32 lsbs = 0, msbs = 0;
1522 FLAC__ASSERT(bb != 0);
1523 FLAC__ASSERT(bb->buffer != 0);
1524 FLAC__ASSERT(parameter <= 31);
1526 /* read the unary MSBs and end bit */
1527 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1530 /* read the binary LSBs */
1531 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1534 /* compose the value */
1535 uval = (msbs << parameter) | lsbs;
1537 *val = -((int)(uval >> 1)) - 1;
1539 *val = (int)(uval >> 1);
1544 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)
1546 FLAC__uint32 lsbs = 0, msbs = 0;
1547 unsigned bit, uval, k;
1549 FLAC__ASSERT(bb != 0);
1550 FLAC__ASSERT(bb->buffer != 0);
1552 k = FLAC__bitmath_ilog2(parameter);
1554 /* read the unary MSBs and end bit */
1555 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1558 /* read the binary LSBs */
1559 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1562 if(parameter == 1u<<k) {
1563 /* compose the value */
1564 uval = (msbs << k) | lsbs;
1567 unsigned d = (1 << (k+1)) - parameter;
1569 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1575 /* compose the value */
1576 uval = msbs * parameter + lsbs;
1579 /* unfold unsigned to signed */
1581 *val = -((int)(uval >> 1)) - 1;
1583 *val = (int)(uval >> 1);
1588 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)
1590 FLAC__uint32 lsbs, msbs = 0;
1593 FLAC__ASSERT(bb != 0);
1594 FLAC__ASSERT(bb->buffer != 0);
1596 k = FLAC__bitmath_ilog2(parameter);
1598 /* read the unary MSBs and end bit */
1599 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1602 /* read the binary LSBs */
1603 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1606 if(parameter == 1u<<k) {
1607 /* compose the value */
1608 *val = (msbs << k) | lsbs;
1611 unsigned d = (1 << (k+1)) - parameter;
1613 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1619 /* compose the value */
1620 *val = msbs * parameter + lsbs;
1626 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1627 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)
1633 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1636 raw[(*rawlen)++] = (FLAC__byte)x;
1637 if(!(x & 0x80)) { /* 0xxxxxxx */
1641 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1645 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1649 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1653 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1657 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1666 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1669 raw[(*rawlen)++] = (FLAC__byte)x;
1670 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1681 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1682 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)
1688 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1691 raw[(*rawlen)++] = (FLAC__byte)x;
1692 if(!(x & 0x80)) { /* 0xxxxxxx */
1696 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1700 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1704 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1708 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1712 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1716 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1721 *val = 0xffffffffffffffff;
1725 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1728 raw[(*rawlen)++] = (FLAC__byte)x;
1729 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1730 *val = 0xffffffffffffffff;
1740 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1744 fprintf(out, "bitbuffer is NULL\n");
1747 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);
1748 for(i = 0; i < bb->bytes; i++) {
1749 fprintf(out, "%08X: ", i);
1750 for(j = 0; j < 8; j++)
1751 if(i*8+j < bb->total_consumed_bits)
1754 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1758 fprintf(out, "%08X: ", i);
1759 for(j = 0; j < bb->bits; j++)
1760 if(i*8+j < bb->total_consumed_bits)
1763 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);