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.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy(), memset() */
23 #include "private/bitbuffer.h"
24 #include "private/bitmath.h"
25 #include "private/crc.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 static const byte byte_bit_to_mask_[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
43 #define min(x,y) ((x)<(y)?(x):(y))
47 #define max(x,y) ((x)>(y)?(x):(y))
49 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
54 assert(bb->buffer != 0);
56 if(bb->capacity == new_capacity)
59 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
62 memset(new_buffer, 0, new_capacity);
63 memcpy(new_buffer, bb->buffer, sizeof(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 bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
81 unsigned new_capacity;
83 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 bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
92 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 bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(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)
134 bb->bytes = bb->bits = bb->total_bits = 0;
135 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
138 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
141 FLAC__bitbuffer_init(bb);
146 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
149 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
150 bb->capacity = bb->bytes = bytes;
152 bb->total_bits = (bytes<<3);
153 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
158 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
162 bb->read_crc16 = seed;
165 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
167 static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
168 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
175 if(dest->bits != src->consumed_bits)
177 if(!bitbuffer_ensure_size_(dest, bits_to_add))
179 if(dest->bits == 0) {
180 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
182 else if(dest->bits + bits_to_add > 8) {
183 dest->buffer[dest->bytes] <<= (8 - dest->bits);
184 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
185 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
188 dest->buffer[dest->bytes] <<= bits_to_add;
189 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
191 dest->bits = src->bits;
192 dest->total_bits += bits_to_add;
193 dest->bytes = dest->total_bits / 8;
198 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
205 bb->bytes = bb->bits = bb->total_bits = 0;
206 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
209 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
211 if(bb->buffer == 0) {
212 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
213 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
216 memset(bb->buffer, 0, bb->capacity);
219 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
221 bb->bytes = bb->bits = bb->total_bits = 0;
222 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
226 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
228 if(dest->capacity < src->capacity)
229 if(!bitbuffer_resize_(dest, src->capacity))
231 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
232 dest->bytes = src->bytes;
233 dest->bits = src->bits;
234 dest->total_bits = src->total_bits;
235 dest->consumed_bytes = src->consumed_bytes;
236 dest->consumed_bits = src->consumed_bits;
237 dest->total_consumed_bits = src->total_consumed_bits;
238 dest->read_crc16 = src->read_crc16;
242 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
247 assert(bb->buffer != 0);
251 if(!bitbuffer_ensure_size_(bb, bits))
253 bb->total_bits += bits;
255 n = min(8 - bb->bits, bits);
257 bb->buffer[bb->bytes] <<= n;
268 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
270 static const uint32 mask[] = {
272 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
273 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
274 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
275 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
276 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
277 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
278 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
279 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
284 assert(bb->buffer != 0);
289 if(!bitbuffer_ensure_size_(bb, bits))
292 bb->total_bits += bits;
295 if(n == 8) { /* i.e. bb->bits == 0 */
297 bb->buffer[bb->bytes] = val;
302 bb->buffer[bb->bytes++] = val;
307 bb->buffer[bb->bytes++] = val >> k;
308 val &= (~(0xffffffff << k));
313 bb->buffer[bb->bytes] <<= bits;
314 bb->buffer[bb->bytes] |= val;
325 bb->buffer[bb->bytes] <<= n;
326 bb->buffer[bb->bytes] |= (val>>k);
327 val &= (~(0xffffffff << k));
337 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
339 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
342 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
344 static const uint64 mask[] = {
346 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
347 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
348 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
349 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
350 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
351 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
352 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
353 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
354 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
355 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
356 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
357 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
358 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
359 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
360 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
361 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
366 assert(bb->buffer != 0);
371 if(!bitbuffer_ensure_size_(bb, bits))
374 bb->total_bits += bits;
378 bb->buffer[bb->bytes] = val;
383 bb->buffer[bb->bytes++] = val;
388 bb->buffer[bb->bytes++] = val >> k;
389 val &= (~(0xffffffffffffffff << k));
394 n = min(8 - bb->bits, bits);
396 bb->buffer[bb->bytes] <<= n;
397 bb->buffer[bb->bytes] |= (val>>k);
398 val &= (~(0xffffffffffffffff << k));
411 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
413 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
416 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
419 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
421 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
423 if(!FLAC__bitbuffer_write_zeroes(bb, val))
425 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
429 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
433 /* convert signed to unsigned */
436 * (unsigned)(((--val) << 1) - 1);
437 * but without the overflow problem at -MAXINT
439 uval = (unsigned)(((-(++val)) << 1) + 1);
441 uval = (unsigned)(val << 1);
443 msbs = uval >> parameter;
445 return 1 + parameter + msbs;
448 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
450 unsigned bits, msbs, uval;
453 assert(parameter > 0);
455 /* convert signed to unsigned */
458 * (unsigned)(((--val) << 1) - 1);
459 * but without the overflow problem at -MAXINT
461 uval = (unsigned)(((-(++val)) << 1) + 1);
463 uval = (unsigned)(val << 1);
465 k = FLAC__bitmath_ilog2(parameter);
466 if(parameter == 1u<<k) {
475 d = (1 << (k+1)) - parameter;
476 q = uval / parameter;
477 r = uval - (q * parameter);
486 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
491 assert(parameter > 0);
493 k = FLAC__bitmath_ilog2(parameter);
494 if(parameter == 1u<<k) {
503 d = (1 << (k+1)) - parameter;
504 q = uval / parameter;
505 r = uval - (q * parameter);
514 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
516 unsigned total_bits, interesting_bits, msbs;
520 assert(bb->buffer != 0);
521 assert(parameter <= 31);
523 /* init pattern with the unary end bit and the sign bit */
531 msbs = val >> parameter;
532 interesting_bits = 2 + parameter;
533 total_bits = interesting_bits + msbs;
534 pattern <<= parameter;
535 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
537 if(total_bits <= 32) {
538 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
542 /* write the unary MSBs */
543 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
545 /* write the unary end bit, the sign bit, and binary LSBs */
546 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
552 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
554 unsigned total_bits, interesting_bits, msbs;
558 assert(bb->buffer != 0);
559 assert(parameter <= 31);
563 /* init pattern with the unary end bit and the sign bit */
571 msbs = val >> parameter;
572 interesting_bits = 2 + parameter;
573 total_bits = interesting_bits + msbs;
574 pattern <<= parameter;
575 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
577 if(total_bits <= 32) {
578 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
581 else if(total_bits > max_bits) {
586 /* write the unary MSBs */
587 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
589 /* write the unary end bit, the sign bit, and binary LSBs */
590 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
596 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
598 unsigned total_bits, val_bits;
602 assert(bb->buffer != 0);
603 assert(parameter <= 31);
605 val_bits = FLAC__bitmath_silog2(val);
606 total_bits = 2 + parameter + 5 + val_bits;
608 if(total_bits <= 32) {
610 pattern <<= (parameter + 5);
612 pattern <<= val_bits;
613 pattern |= (val & ((1 << val_bits) - 1));
614 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
618 /* write the '-0' escape code first */
619 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
621 /* write the length */
622 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
624 /* write the value */
625 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
631 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
633 unsigned total_bits, interesting_bits, msbs, uval;
637 assert(bb->buffer != 0);
638 assert(parameter <= 30);
640 /* convert signed to unsigned */
643 * (unsigned)(((--val) << 1) - 1);
644 * but without the overflow problem at -MAXINT
646 uval = (unsigned)(((-(++val)) << 1) + 1);
648 uval = (unsigned)(val << 1);
650 msbs = uval >> parameter;
651 interesting_bits = 1 + parameter;
652 total_bits = interesting_bits + msbs;
653 pattern = 1 << parameter; /* the unary end bit */
654 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
656 if(total_bits <= 32) {
657 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
661 /* write the unary MSBs */
662 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
664 /* write the unary end bit and binary LSBs */
665 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
671 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
673 unsigned total_bits, interesting_bits, msbs, uval;
677 assert(bb->buffer != 0);
678 assert(parameter <= 30);
682 /* convert signed to unsigned */
685 * (unsigned)(((--val) << 1) - 1);
686 * but without the overflow problem at -MAXINT
688 uval = (unsigned)(((-(++val)) << 1) + 1);
690 uval = (unsigned)(val << 1);
692 msbs = uval >> parameter;
693 interesting_bits = 1 + parameter;
694 total_bits = interesting_bits + msbs;
695 pattern = 1 << parameter; /* the unary end bit */
696 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
698 if(total_bits <= 32) {
699 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
702 else if(total_bits > max_bits) {
707 /* write the unary MSBs */
708 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
710 /* write the unary end bit and binary LSBs */
711 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
717 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
719 unsigned total_bits, msbs, uval;
723 assert(bb->buffer != 0);
724 assert(parameter > 0);
726 /* convert signed to unsigned */
729 * (unsigned)(((--val) << 1) - 1);
730 * but without the overflow problem at -MAXINT
732 uval = (unsigned)(((-(++val)) << 1) + 1);
734 uval = (unsigned)(val << 1);
736 k = FLAC__bitmath_ilog2(parameter);
737 if(parameter == 1u<<k) {
743 total_bits = 1 + k + msbs;
744 pattern = 1 << k; /* the unary end bit */
745 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
747 if(total_bits <= 32) {
748 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
752 /* write the unary MSBs */
753 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
755 /* write the unary end bit and binary LSBs */
756 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
763 d = (1 << (k+1)) - parameter;
764 q = uval / parameter;
765 r = uval - (q * parameter);
766 /* write the unary MSBs */
767 if(!FLAC__bitbuffer_write_zeroes(bb, q))
769 /* write the unary end bit */
770 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
772 /* write the binary LSBs */
774 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
778 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
785 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
787 unsigned total_bits, msbs;
791 assert(bb->buffer != 0);
792 assert(parameter > 0);
794 k = FLAC__bitmath_ilog2(parameter);
795 if(parameter == 1u<<k) {
801 total_bits = 1 + k + msbs;
802 pattern = 1 << k; /* the unary end bit */
803 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
805 if(total_bits <= 32) {
806 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
810 /* write the unary MSBs */
811 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
813 /* write the unary end bit and binary LSBs */
814 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
821 d = (1 << (k+1)) - parameter;
822 q = uval / parameter;
823 r = uval - (q * parameter);
824 /* write the unary MSBs */
825 if(!FLAC__bitbuffer_write_zeroes(bb, q))
827 /* write the unary end bit */
828 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
830 /* write the binary LSBs */
832 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
836 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
843 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
848 assert(bb->buffer != 0);
850 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
853 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
855 else if(val < 0x800) {
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
859 else if(val < 0x10000) {
860 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
864 else if(val < 0x200000) {
865 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
866 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
870 else if(val < 0x4000000) {
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
873 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
874 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
875 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
880 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
881 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
882 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
883 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
889 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
894 assert(bb->buffer != 0);
896 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
899 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
901 else if(val < 0x800) {
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
905 else if(val < 0x10000) {
906 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
910 else if(val < 0x200000) {
911 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
912 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
913 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
916 else if(val < 0x4000000) {
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
919 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
920 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
923 else if(val < 0x80000000) {
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
929 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
935 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
936 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
937 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
938 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
944 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
946 /* 0-pad to byte boundary */
948 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
953 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
955 /* to avoid a drastic speed penalty we don't:
957 assert(bb->buffer != 0);
958 assert(bb->bits == 0);
962 if(bb->total_consumed_bits < bb->total_bits) {
963 *val = (bb->buffer[bb->consumed_bytes] & byte_bit_to_mask_[bb->consumed_bits])? 1 : 0;
967 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
973 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
975 /* to avoid a drastic speed penalty we don't:
977 assert(bb->buffer != 0);
978 assert(bb->bits == 0);
982 if(bb->total_consumed_bits < bb->total_bits) {
983 *val = (bb->buffer[bb->consumed_bytes] & byte_bit_to_mask_[bb->consumed_bits])? 1 : 0;
985 if(bb->consumed_bits == 8) {
986 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
987 bb->consumed_bytes++;
988 bb->consumed_bits = 0;
990 bb->total_consumed_bits++;
994 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1000 bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1002 /* to avoid a drastic speed penalty we don't:
1004 assert(bb->buffer != 0);
1005 assert(bb->bits == 0);
1009 if(bb->total_consumed_bits < bb->total_bits) {
1011 *val |= (bb->buffer[bb->consumed_bytes] & byte_bit_to_mask_[bb->consumed_bits])? 1 : 0;
1012 bb->consumed_bits++;
1013 if(bb->consumed_bits == 8) {
1014 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1015 bb->consumed_bytes++;
1016 bb->consumed_bits = 0;
1018 bb->total_consumed_bits++;
1022 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1028 bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1030 /* to avoid a drastic speed penalty we don't:
1032 assert(bb->buffer != 0);
1033 assert(bb->bits == 0);
1037 if(bb->total_consumed_bits < bb->total_bits) {
1039 *val |= (bb->buffer[bb->consumed_bytes] & byte_bit_to_mask_[bb->consumed_bits])? 1 : 0;
1040 bb->consumed_bits++;
1041 if(bb->consumed_bits == 8) {
1042 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1043 bb->consumed_bytes++;
1044 bb->consumed_bits = 0;
1046 bb->total_consumed_bits++;
1050 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1056 bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1057 #ifdef FLAC__NO_MANUAL_INLINING
1062 assert(bb->buffer != 0);
1067 for(i = 0; i < bits; i++) {
1068 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1075 unsigned i, bits_ = bits;
1079 assert(bb->buffer != 0);
1082 assert((bb->capacity*8) * 2 >= bits);
1084 while(bb->total_consumed_bits + bits > bb->total_bits) {
1085 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1088 if(bb->consumed_bits) {
1089 i = 8 - bb->consumed_bits;
1091 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1093 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1094 bb->consumed_bytes++;
1095 bb->consumed_bits = 0;
1096 /* we hold off updating bb->total_consumed_bits until the end */
1099 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1100 bb->consumed_bits += bits_;
1101 bb->total_consumed_bits += bits_;
1107 v |= bb->buffer[bb->consumed_bytes];
1109 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1110 bb->consumed_bytes++;
1111 /* bb->consumed_bits is already 0 */
1112 /* we hold off updating bb->total_consumed_bits until the end */
1116 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1117 bb->consumed_bits = bits_;
1118 /* we hold off updating bb->total_consumed_bits until the end */
1120 bb->total_consumed_bits += bits;
1126 bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1127 #ifdef FLAC__NO_MANUAL_INLINING
1133 assert(bb->buffer != 0);
1138 for(i = 0; i < bits; i++) {
1139 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1157 unsigned i, bits_ = bits;
1161 assert(bb->buffer != 0);
1164 assert((bb->capacity*8) * 2 >= bits);
1166 while(bb->total_consumed_bits + bits > bb->total_bits) {
1167 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1170 if(bb->consumed_bits) {
1171 i = 8 - bb->consumed_bits;
1173 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1175 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1176 bb->consumed_bytes++;
1177 bb->consumed_bits = 0;
1178 /* we hold off updating bb->total_consumed_bits until the end */
1181 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-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;
1218 bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1219 #ifdef FLAC__NO_MANUAL_INLINING
1224 assert(bb->buffer != 0);
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;
1241 assert(bb->buffer != 0);
1244 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 bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1289 #ifdef FLAC__NO_MANUAL_INLINING
1295 assert(bb->buffer != 0);
1300 for(i = 0; i < bits; i++) {
1301 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1318 unsigned i, bits_ = bits;
1322 assert(bb->buffer != 0);
1325 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 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1343 bb->consumed_bits += bits_;
1344 bb->total_consumed_bits += bits_;
1350 v |= bb->buffer[bb->consumed_bytes];
1352 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1353 bb->consumed_bytes++;
1354 /* bb->consumed_bits is already 0 */
1355 /* we hold off updating bb->total_consumed_bits until the end */
1359 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1360 bb->consumed_bits = bits_;
1361 /* we hold off updating bb->total_consumed_bits until the end */
1363 bb->total_consumed_bits += bits;
1379 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1380 #ifdef FLAC__NO_MANUAL_INLINING
1382 unsigned bit, val_ = 0;
1385 assert(bb->buffer != 0);
1388 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1400 unsigned i, val_ = 0;
1401 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1405 assert(bb->buffer != 0);
1407 if(bb->consumed_bits) {
1408 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1410 for(i = 0; !(b & 0x80); i++)
1414 bb->consumed_bits += i;
1415 bb->total_consumed_bits += i;
1416 if(bb->consumed_bits == 8) {
1417 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1418 bb->consumed_bytes++;
1419 bb->consumed_bits = 0;
1424 val_ = 8 - bb->consumed_bits;
1425 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1426 bb->consumed_bytes++;
1427 bb->consumed_bits = 0;
1428 /* we hold off updating bb->total_consumed_bits until the end */
1432 if(bb->consumed_bytes >= total_bytes_) {
1433 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1435 total_bytes_ = (bb->total_bits + 7) / 8;
1437 b = bb->buffer[bb->consumed_bytes];
1439 for(i = 0; !(b & 0x80); i++)
1443 bb->consumed_bits = i;
1446 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1447 bb->consumed_bytes++;
1448 bb->consumed_bits = 0;
1450 bb->total_consumed_bits += (++val_);
1455 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1456 bb->consumed_bytes++;
1457 /* bb->consumed_bits is already 0 */
1458 /* we hold off updating bb->total_consumed_bits until the end */
1464 bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1466 uint32 sign = 0, lsbs = 0, msbs = 0;
1469 assert(bb->buffer != 0);
1470 assert(parameter <= 31);
1472 /* read the unary MSBs and end bit */
1473 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1476 /* read the sign bit */
1477 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1480 /* read the binary LSBs */
1481 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1484 /* compose the value */
1485 *val = (msbs << parameter) | lsbs;
1492 bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1494 uint32 lsbs = 0, msbs = 0;
1498 assert(bb->buffer != 0);
1499 assert(parameter <= 31);
1501 /* read the unary MSBs and end bit */
1502 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, 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 uval = (msbs << parameter) | lsbs;
1512 *val = -((int)(uval >> 1)) - 1;
1514 *val = (int)(uval >> 1);
1519 bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1521 uint32 lsbs = 0, msbs = 0;
1522 unsigned bit, uval, k;
1525 assert(bb->buffer != 0);
1527 k = FLAC__bitmath_ilog2(parameter);
1529 /* read the unary MSBs and end bit */
1530 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1533 /* read the binary LSBs */
1534 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1537 if(parameter == 1u<<k) {
1538 /* compose the value */
1539 uval = (msbs << k) | lsbs;
1542 unsigned d = (1 << (k+1)) - parameter;
1544 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1550 /* compose the value */
1551 uval = msbs * parameter + lsbs;
1554 /* unfold unsigned to signed */
1556 *val = -((int)(uval >> 1)) - 1;
1558 *val = (int)(uval >> 1);
1563 bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1565 uint32 lsbs, msbs = 0;
1569 assert(bb->buffer != 0);
1571 k = FLAC__bitmath_ilog2(parameter);
1573 /* read the unary MSBs and end bit */
1574 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1577 /* read the binary LSBs */
1578 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1581 if(parameter == 1u<<k) {
1582 /* compose the value */
1583 *val = (msbs << k) | lsbs;
1586 unsigned d = (1 << (k+1)) - parameter;
1588 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1594 /* compose the value */
1595 *val = msbs * parameter + lsbs;
1601 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1602 bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
1608 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1611 raw[(*rawlen)++] = (byte)x;
1612 if(!(x & 0x80)) { /* 0xxxxxxx */
1616 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1620 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1624 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1628 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1632 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1641 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1644 raw[(*rawlen)++] = (byte)x;
1645 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1656 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1657 bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
1663 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1666 raw[(*rawlen)++] = (byte)x;
1667 if(!(x & 0x80)) { /* 0xxxxxxx */
1671 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1675 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1679 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1683 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1687 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1691 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1696 *val = 0xffffffffffffffff;
1700 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1703 raw[(*rawlen)++] = (byte)x;
1704 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1705 *val = 0xffffffffffffffff;
1715 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1719 fprintf(out, "bitbuffer is NULL\n");
1722 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);
1723 for(i = 0; i < bb->bytes; i++) {
1724 fprintf(out, "%08X: ", i);
1725 for(j = 0; j < 8; j++)
1726 if(i*8+j < bb->total_consumed_bits)
1729 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1733 fprintf(out, "%08X: ", i);
1734 for(j = 0; j < bb->bits; j++)
1735 if(i*8+j < bb->total_consumed_bits)
1738 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);