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/crc.h"
26 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
31 #define min(x,y) ((x)<(y)?(x):(y))
35 #define max(x,y) ((x)>(y)?(x):(y))
37 static unsigned ilog2_(unsigned v)
46 static unsigned silog2_(int v)
69 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
74 assert(bb->buffer != 0);
76 if(bb->capacity == new_capacity)
79 fprintf(stderr,"@@@ bitbuffer_resize_(bb=%p, capacity=%u, new_capactity=%u)\n",bb,bb->capacity,new_capacity);
80 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
83 memset(new_buffer, 0, new_capacity);
84 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
85 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
86 bb->bytes = new_capacity;
88 bb->total_bits = (new_capacity<<3);
90 //@@@ boy oh boy, is this a bug? maybe should drop the 'if' statement
91 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
92 bb->consumed_bytes = new_capacity;
93 bb->consumed_bits = 0;
94 bb->total_consumed_bits = (new_capacity<<3);
96 bb->buffer = new_buffer;
97 bb->capacity = new_capacity;
101 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
103 unsigned new_capacity;
105 assert(min_bytes_to_add > 0);
107 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
108 return bitbuffer_resize_(bb, new_capacity);
111 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
114 assert(bb->buffer != 0);
115 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
116 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
121 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
125 /* first shift the unconsumed buffer data toward the front as much as possible */
126 if(bb->total_consumed_bits >= 8) {
127 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
128 for( ; r < r_end; l++, r++)
129 bb->buffer[l] = bb->buffer[r];
130 for( ; l < r_end; l++)
132 bb->bytes -= bb->consumed_bytes;
133 bb->total_bits -= (bb->consumed_bytes<<3);
134 bb->consumed_bytes = 0;
135 bb->total_consumed_bits = bb->consumed_bits;
137 /* grow if we need to */
138 if(bb->capacity <= 1) {
139 if(!bitbuffer_resize_(bb, 16))
142 /* finally, read in some data; if OK, go back to read_bit_, else fail */
143 bytes = bb->capacity - bb->bytes;
144 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
147 bb->total_bits += (bytes<<3);
151 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
156 bb->bytes = bb->bits = bb->total_bits = 0;
157 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
160 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
163 FLAC__bitbuffer_init(bb);
168 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
171 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
172 bb->capacity = bb->bytes = bytes;
174 bb->total_bits = (bytes<<3);
175 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
180 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
184 bb->read_crc16 = seed;
187 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
189 static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
190 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
197 if(dest->bits != src->consumed_bits)
199 if(!bitbuffer_ensure_size_(dest, bits_to_add))
201 if(dest->bits == 0) {
202 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
204 else if(dest->bits + bits_to_add > 8) {
205 dest->buffer[dest->bytes] <<= (8 - dest->bits);
206 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
207 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
210 dest->buffer[dest->bytes] <<= bits_to_add;
211 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
213 dest->bits = src->bits;
214 dest->total_bits += bits_to_add;
215 dest->bytes = dest->total_bits / 8;
220 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
227 bb->bytes = bb->bits = bb->total_bits = 0;
228 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
231 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
233 if(bb->buffer == 0) {
234 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
235 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
238 memset(bb->buffer, 0, bb->capacity);
241 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
243 bb->bytes = bb->bits = bb->total_bits = 0;
244 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
248 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
250 if(dest->capacity < src->capacity)
251 if(!bitbuffer_resize_(dest, src->capacity))
253 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
254 dest->bytes = src->bytes;
255 dest->bits = src->bits;
256 dest->total_bits = src->total_bits;
257 dest->consumed_bytes = src->consumed_bytes;
258 dest->consumed_bits = src->consumed_bits;
259 dest->total_consumed_bits = src->total_consumed_bits;
260 dest->read_crc16 = src->read_crc16;
264 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
269 assert(bb->buffer != 0);
273 if(!bitbuffer_ensure_size_(bb, bits))
275 bb->total_bits += bits;
277 n = min(8 - bb->bits, bits);
279 bb->buffer[bb->bytes] <<= n;
290 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
292 static const uint32 mask[] = {
294 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
295 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
296 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
297 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
298 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
299 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
300 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
301 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
306 assert(bb->buffer != 0);
311 if(!bitbuffer_ensure_size_(bb, bits))
314 bb->total_bits += bits;
317 if(n == 8) { /* i.e. bb->bits == 0 */
319 bb->buffer[bb->bytes] = val;
324 bb->buffer[bb->bytes++] = val;
329 bb->buffer[bb->bytes++] = val >> k;
330 val &= (~(0xffffffff << k));
335 bb->buffer[bb->bytes] <<= bits;
336 bb->buffer[bb->bytes] |= val;
347 bb->buffer[bb->bytes] <<= n;
348 bb->buffer[bb->bytes] |= (val>>k);
349 val &= (~(0xffffffff << k));
359 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
361 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
364 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
366 static const uint64 mask[] = {
368 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
369 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
370 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
371 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
372 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
373 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
374 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
375 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
376 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
377 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
378 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
379 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
380 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
381 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
382 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
383 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
388 assert(bb->buffer != 0);
393 if(!bitbuffer_ensure_size_(bb, bits))
396 bb->total_bits += bits;
400 bb->buffer[bb->bytes] = val;
405 bb->buffer[bb->bytes++] = val;
410 bb->buffer[bb->bytes++] = val >> k;
411 val &= (~(0xffffffffffffffff << k));
416 n = min(8 - bb->bits, bits);
418 bb->buffer[bb->bytes] <<= n;
419 bb->buffer[bb->bytes] |= (val>>k);
420 val &= (~(0xffffffffffffffff << k));
433 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
435 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
438 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
441 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
443 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
445 if(!FLAC__bitbuffer_write_zeroes(bb, val))
447 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
451 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
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 msbs = uval >> parameter;
467 return 1 + parameter + msbs;
470 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
472 unsigned bits, msbs, uval;
475 assert(parameter > 0);
477 /* convert signed to unsigned */
480 * (unsigned)(((--val) << 1) - 1);
481 * but without the overflow problem at -MAXINT
483 uval = (unsigned)(((-(++val)) << 1) + 1);
485 uval = (unsigned)(val << 1);
487 k = ilog2_(parameter);
488 if(parameter == 1u<<k) {
497 d = (1 << (k+1)) - parameter;
498 q = uval / parameter;
499 r = uval - (q * parameter);
508 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
513 assert(parameter > 0);
515 k = ilog2_(parameter);
516 if(parameter == 1u<<k) {
525 d = (1 << (k+1)) - parameter;
526 q = uval / parameter;
527 r = uval - (q * parameter);
536 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
538 unsigned total_bits, interesting_bits, msbs;
542 assert(bb->buffer != 0);
543 assert(parameter <= 31);
545 /* init pattern with the unary end bit and the sign bit */
553 msbs = val >> parameter;
554 interesting_bits = 2 + parameter;
555 total_bits = interesting_bits + msbs;
556 pattern <<= parameter;
557 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
559 if(total_bits <= 32) {
560 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
564 /* write the unary MSBs */
565 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
567 /* write the unary end bit, the sign bit, and binary LSBs */
568 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
574 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
576 unsigned total_bits, interesting_bits, msbs;
580 assert(bb->buffer != 0);
581 assert(parameter <= 31);
585 /* init pattern with the unary end bit and the sign bit */
593 msbs = val >> parameter;
594 interesting_bits = 2 + parameter;
595 total_bits = interesting_bits + msbs;
596 pattern <<= parameter;
597 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
599 if(total_bits <= 32) {
600 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
603 else if(total_bits > max_bits) {
608 /* write the unary MSBs */
609 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
611 /* write the unary end bit, the sign bit, and binary LSBs */
612 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
618 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
620 unsigned total_bits, val_bits;
624 assert(bb->buffer != 0);
625 assert(parameter <= 31);
627 val_bits = silog2_(val);
628 total_bits = 2 + parameter + 5 + val_bits;
630 if(total_bits <= 32) {
632 pattern <<= (parameter + 5);
634 pattern <<= val_bits;
635 pattern |= (val & ((1 << val_bits) - 1));
636 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
640 /* write the '-0' escape code first */
641 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
643 /* write the length */
644 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
646 /* write the value */
647 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
653 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
655 unsigned total_bits, interesting_bits, msbs, uval;
659 assert(bb->buffer != 0);
660 assert(parameter <= 30);
662 /* convert signed to unsigned */
665 * (unsigned)(((--val) << 1) - 1);
666 * but without the overflow problem at -MAXINT
668 uval = (unsigned)(((-(++val)) << 1) + 1);
670 uval = (unsigned)(val << 1);
672 msbs = uval >> parameter;
673 interesting_bits = 1 + parameter;
674 total_bits = interesting_bits + msbs;
675 pattern = 1 << parameter; /* the unary end bit */
676 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
678 if(total_bits <= 32) {
679 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
683 /* write the unary MSBs */
684 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
686 /* write the unary end bit and binary LSBs */
687 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
693 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
695 unsigned total_bits, interesting_bits, msbs, uval;
699 assert(bb->buffer != 0);
700 assert(parameter <= 30);
704 /* convert signed to unsigned */
707 * (unsigned)(((--val) << 1) - 1);
708 * but without the overflow problem at -MAXINT
710 uval = (unsigned)(((-(++val)) << 1) + 1);
712 uval = (unsigned)(val << 1);
714 msbs = uval >> parameter;
715 interesting_bits = 1 + parameter;
716 total_bits = interesting_bits + msbs;
717 pattern = 1 << parameter; /* the unary end bit */
718 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
720 if(total_bits <= 32) {
721 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
724 else if(total_bits > max_bits) {
729 /* write the unary MSBs */
730 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
732 /* write the unary end bit and binary LSBs */
733 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
739 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
741 unsigned total_bits, msbs, uval;
745 assert(bb->buffer != 0);
746 assert(parameter > 0);
748 /* convert signed to unsigned */
751 * (unsigned)(((--val) << 1) - 1);
752 * but without the overflow problem at -MAXINT
754 uval = (unsigned)(((-(++val)) << 1) + 1);
756 uval = (unsigned)(val << 1);
758 k = ilog2_(parameter);
759 if(parameter == 1u<<k) {
765 total_bits = 1 + k + msbs;
766 pattern = 1 << k; /* the unary end bit */
767 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
769 if(total_bits <= 32) {
770 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
774 /* write the unary MSBs */
775 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
777 /* write the unary end bit and binary LSBs */
778 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
785 d = (1 << (k+1)) - parameter;
786 q = uval / parameter;
787 r = uval - (q * parameter);
788 /* write the unary MSBs */
789 if(!FLAC__bitbuffer_write_zeroes(bb, q))
791 /* write the unary end bit */
792 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
794 /* write the binary LSBs */
796 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
800 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
807 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
809 unsigned total_bits, msbs;
813 assert(bb->buffer != 0);
814 assert(parameter > 0);
816 k = ilog2_(parameter);
817 if(parameter == 1u<<k) {
823 total_bits = 1 + k + msbs;
824 pattern = 1 << k; /* the unary end bit */
825 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
827 if(total_bits <= 32) {
828 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
832 /* write the unary MSBs */
833 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
835 /* write the unary end bit and binary LSBs */
836 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
843 d = (1 << (k+1)) - parameter;
844 q = uval / parameter;
845 r = uval - (q * parameter);
846 /* write the unary MSBs */
847 if(!FLAC__bitbuffer_write_zeroes(bb, q))
849 /* write the unary end bit */
850 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
852 /* write the binary LSBs */
854 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
858 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
865 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
870 assert(bb->buffer != 0);
872 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
875 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
877 else if(val < 0x800) {
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
881 else if(val < 0x10000) {
882 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
883 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
884 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
886 else if(val < 0x200000) {
887 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
888 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
889 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
890 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
892 else if(val < 0x4000000) {
893 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
894 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
895 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
896 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
897 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
900 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
901 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
904 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
905 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
911 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
916 assert(bb->buffer != 0);
918 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
921 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
923 else if(val < 0x800) {
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
927 else if(val < 0x10000) {
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
929 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
930 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
932 else if(val < 0x200000) {
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
935 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
936 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
938 else if(val < 0x4000000) {
939 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
940 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
941 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
942 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
943 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
945 else if(val < 0x80000000) {
946 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
947 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
948 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
949 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
950 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
951 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
954 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
955 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
956 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
957 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
958 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
959 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
960 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
966 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
968 /* 0-pad to byte boundary */
970 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
975 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
977 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
979 /* to avoid a drastic speed penalty we don't:
981 assert(bb->buffer != 0);
982 assert(bb->bits == 0);
986 if(bb->total_consumed_bits < bb->total_bits) {
987 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
991 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
997 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
999 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1001 /* to avoid a drastic speed penalty we don't:
1003 assert(bb->buffer != 0);
1004 assert(bb->bits == 0);
1008 if(bb->total_consumed_bits < bb->total_bits) {
1009 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1010 bb->consumed_bits++;
1011 if(bb->consumed_bits == 8) {
1012 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1013 bb->consumed_bytes++;
1014 bb->consumed_bits = 0;
1016 bb->total_consumed_bits++;
1020 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1026 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)
1028 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
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] & 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_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1058 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1060 /* to avoid a drastic speed penalty we don't:
1062 assert(bb->buffer != 0);
1063 assert(bb->bits == 0);
1067 if(bb->total_consumed_bits < bb->total_bits) {
1069 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1070 bb->consumed_bits++;
1071 if(bb->consumed_bits == 8) {
1072 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1073 bb->consumed_bytes++;
1074 bb->consumed_bits = 0;
1076 bb->total_consumed_bits++;
1080 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1086 bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1091 assert(bb->buffer != 0);
1096 for(i = 0; i < bits; i++) {
1097 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1103 bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1109 assert(bb->buffer != 0);
1114 for(i = 0; i < bits; i++) {
1115 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
1131 bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1136 assert(bb->buffer != 0);
1141 for(i = 0; i < bits; i++) {
1142 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1148 bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1154 assert(bb->buffer != 0);
1159 for(i = 0; i < bits; i++) {
1160 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
1176 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1178 unsigned bit, val_ = 0;
1181 assert(bb->buffer != 0);
1186 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1197 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)
1199 uint32 sign = 0, lsbs = 0, msbs = 0;
1203 assert(bb->buffer != 0);
1204 assert(parameter <= 31);
1206 /* read the unary MSBs and end bit */
1208 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1215 /* read the sign bit */
1216 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1218 /* read the binary LSBs */
1219 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1222 /* compose the value */
1223 *val = (msbs << parameter) | lsbs;
1230 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)
1232 uint32 lsbs = 0, msbs = 0;
1236 assert(bb->buffer != 0);
1237 assert(parameter <= 31);
1239 /* read the unary MSBs and end bit */
1241 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1248 /* read the binary LSBs */
1249 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1251 /* compose the value */
1252 uval = (msbs << parameter) | lsbs;
1254 *val = -((int)(uval >> 1)) - 1;
1256 *val = (int)(uval >> 1);
1261 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)
1263 uint32 lsbs = 0, msbs = 0;
1264 unsigned bit, uval, k;
1267 assert(bb->buffer != 0);
1269 k = ilog2_(parameter);
1271 /* read the unary MSBs and end bit */
1273 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1280 /* read the binary LSBs */
1281 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1284 if(parameter == 1u<<k) {
1285 /* compose the value */
1286 uval = (msbs << k) | lsbs;
1289 unsigned d = (1 << (k+1)) - parameter;
1291 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1297 /* compose the value */
1298 uval = msbs * parameter + lsbs;
1301 /* unfold unsigned to signed */
1303 *val = -((int)(uval >> 1)) - 1;
1305 *val = (int)(uval >> 1);
1310 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)
1312 uint32 lsbs, msbs = 0;
1316 assert(bb->buffer != 0);
1318 k = ilog2_(parameter);
1320 /* read the unary MSBs and end bit */
1322 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1329 /* read the binary LSBs */
1330 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1333 if(parameter == 1u<<k) {
1334 /* compose the value */
1335 *val = (msbs << k) | lsbs;
1338 unsigned d = (1 << (k+1)) - parameter;
1340 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1346 /* compose the value */
1347 *val = msbs * parameter + lsbs;
1353 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1354 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)
1360 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1363 raw[(*rawlen)++] = (byte)x;
1364 if(!(x & 0x80)) { /* 0xxxxxxx */
1368 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1372 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1376 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1380 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1384 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1393 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1396 raw[(*rawlen)++] = (byte)x;
1397 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1408 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1409 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)
1415 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1418 raw[(*rawlen)++] = (byte)x;
1419 if(!(x & 0x80)) { /* 0xxxxxxx */
1423 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1427 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1431 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1435 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1439 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1443 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1448 *val = 0xffffffffffffffff;
1452 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1455 raw[(*rawlen)++] = (byte)x;
1456 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1457 *val = 0xffffffffffffffff;
1467 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1471 fprintf(out, "bitbuffer is NULL\n");
1474 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);
1475 for(i = 0; i < bb->bytes; i++) {
1476 fprintf(out, "%08X: ", i);
1477 for(j = 0; j < 8; j++)
1478 if(i*8+j < bb->total_consumed_bits)
1481 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1485 fprintf(out, "%08X: ", i);
1486 for(j = 0; j < bb->bits; j++)
1487 if(i*8+j < bb->total_consumed_bits)
1490 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);