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 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
82 memset(new_buffer, 0, new_capacity);
83 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
84 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
85 bb->bytes = new_capacity;
87 bb->total_bits = (new_capacity<<3);
89 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
90 bb->consumed_bytes = new_capacity;
91 bb->consumed_bits = 0;
92 bb->total_consumed_bits = (new_capacity<<3);
94 bb->buffer = new_buffer;
95 bb->capacity = new_capacity;
99 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
101 unsigned new_capacity;
103 assert(min_bytes_to_add > 0);
105 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
106 return bitbuffer_resize_(bb, new_capacity);
109 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
112 assert(bb->buffer != 0);
113 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
114 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
119 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
123 /* first shift the unconsumed buffer data toward the front as much as possible */
124 if(bb->total_consumed_bits >= 8) {
125 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
126 for( ; r < r_end; l++, r++)
127 bb->buffer[l] = bb->buffer[r];
128 for( ; l < r_end; l++)
130 bb->bytes -= bb->consumed_bytes;
131 bb->total_bits -= (bb->consumed_bytes<<3);
132 bb->consumed_bytes = 0;
133 bb->total_consumed_bits = bb->consumed_bits;
135 /* grow if we need to */
136 if(bb->capacity <= 1) {
137 if(!bitbuffer_resize_(bb, 16))
140 /* finally, read in some data; if OK, go back to read_bit_, else fail */
141 bytes = bb->capacity - bb->bytes;
142 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
145 bb->total_bits += (bytes<<3);
149 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
154 bb->bytes = bb->bits = bb->total_bits = 0;
155 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
158 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
161 FLAC__bitbuffer_init(bb);
166 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
169 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
170 bb->capacity = bb->bytes = bytes;
172 bb->total_bits = (bytes<<3);
173 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
178 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
182 bb->read_crc16 = seed;
185 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
187 static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
188 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
195 if(dest->bits != src->consumed_bits)
197 if(!bitbuffer_ensure_size_(dest, bits_to_add))
199 if(dest->bits == 0) {
200 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
202 else if(dest->bits + bits_to_add > 8) {
203 dest->buffer[dest->bytes] <<= (8 - dest->bits);
204 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
205 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
208 dest->buffer[dest->bytes] <<= bits_to_add;
209 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
211 dest->bits = src->bits;
212 dest->total_bits += bits_to_add;
213 dest->bytes = dest->total_bits / 8;
218 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
225 bb->bytes = bb->bits = bb->total_bits = 0;
226 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
229 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
231 if(bb->buffer == 0) {
232 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
233 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
236 memset(bb->buffer, 0, bb->capacity);
239 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
241 bb->bytes = bb->bits = bb->total_bits = 0;
242 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
246 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
248 if(dest->capacity < src->capacity)
249 if(!bitbuffer_resize_(dest, src->capacity))
251 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
252 dest->bytes = src->bytes;
253 dest->bits = src->bits;
254 dest->total_bits = src->total_bits;
255 dest->consumed_bytes = src->consumed_bytes;
256 dest->consumed_bits = src->consumed_bits;
257 dest->total_consumed_bits = src->total_consumed_bits;
258 dest->read_crc16 = src->read_crc16;
262 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
267 assert(bb->buffer != 0);
271 if(!bitbuffer_ensure_size_(bb, bits))
273 bb->total_bits += bits;
275 n = min(8 - bb->bits, bits);
277 bb->buffer[bb->bytes] <<= n;
288 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
290 static const uint32 mask[] = {
292 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
293 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
294 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
295 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
296 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
297 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
298 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
299 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
304 assert(bb->buffer != 0);
309 if(!bitbuffer_ensure_size_(bb, bits))
312 bb->total_bits += bits;
315 if(n == 8) { /* i.e. bb->bits == 0 */
317 bb->buffer[bb->bytes] = val;
322 bb->buffer[bb->bytes++] = val;
327 bb->buffer[bb->bytes++] = val >> k;
328 val &= (~(0xffffffff << k));
333 bb->buffer[bb->bytes] <<= bits;
334 bb->buffer[bb->bytes] |= val;
345 bb->buffer[bb->bytes] <<= n;
346 bb->buffer[bb->bytes] |= (val>>k);
347 val &= (~(0xffffffff << k));
357 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
359 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
362 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
364 static const uint64 mask[] = {
366 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
367 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
368 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
369 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
370 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
371 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
372 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
373 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
374 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
375 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
376 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
377 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
378 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
379 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
380 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
381 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
386 assert(bb->buffer != 0);
391 if(!bitbuffer_ensure_size_(bb, bits))
394 bb->total_bits += bits;
398 bb->buffer[bb->bytes] = val;
403 bb->buffer[bb->bytes++] = val;
408 bb->buffer[bb->bytes++] = val >> k;
409 val &= (~(0xffffffffffffffff << k));
414 n = min(8 - bb->bits, bits);
416 bb->buffer[bb->bytes] <<= n;
417 bb->buffer[bb->bytes] |= (val>>k);
418 val &= (~(0xffffffffffffffff << k));
431 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
433 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
436 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
439 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
441 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
443 if(!FLAC__bitbuffer_write_zeroes(bb, val))
445 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
449 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
453 /* convert signed to unsigned */
456 * (unsigned)(((--val) << 1) - 1);
457 * but without the overflow problem at -MAXINT
459 uval = (unsigned)(((-(++val)) << 1) + 1);
461 uval = (unsigned)(val << 1);
463 msbs = uval >> parameter;
465 return 1 + parameter + msbs;
468 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
470 unsigned bits, msbs, uval;
473 assert(parameter > 0);
475 /* convert signed to unsigned */
478 * (unsigned)(((--val) << 1) - 1);
479 * but without the overflow problem at -MAXINT
481 uval = (unsigned)(((-(++val)) << 1) + 1);
483 uval = (unsigned)(val << 1);
485 k = ilog2_(parameter);
486 if(parameter == 1u<<k) {
495 d = (1 << (k+1)) - parameter;
496 q = uval / parameter;
497 r = uval - (q * parameter);
506 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
511 assert(parameter > 0);
513 k = ilog2_(parameter);
514 if(parameter == 1u<<k) {
523 d = (1 << (k+1)) - parameter;
524 q = uval / parameter;
525 r = uval - (q * parameter);
534 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
536 unsigned total_bits, interesting_bits, msbs;
540 assert(bb->buffer != 0);
541 assert(parameter <= 31);
543 /* init pattern with the unary end bit and the sign bit */
551 msbs = val >> parameter;
552 interesting_bits = 2 + parameter;
553 total_bits = interesting_bits + msbs;
554 pattern <<= parameter;
555 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
557 if(total_bits <= 32) {
558 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
562 /* write the unary MSBs */
563 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
565 /* write the unary end bit, the sign bit, and binary LSBs */
566 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
572 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
574 unsigned total_bits, interesting_bits, msbs;
578 assert(bb->buffer != 0);
579 assert(parameter <= 31);
583 /* init pattern with the unary end bit and the sign bit */
591 msbs = val >> parameter;
592 interesting_bits = 2 + parameter;
593 total_bits = interesting_bits + msbs;
594 pattern <<= parameter;
595 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
597 if(total_bits <= 32) {
598 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
601 else if(total_bits > max_bits) {
606 /* write the unary MSBs */
607 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
609 /* write the unary end bit, the sign bit, and binary LSBs */
610 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
616 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
618 unsigned total_bits, val_bits;
622 assert(bb->buffer != 0);
623 assert(parameter <= 31);
625 val_bits = silog2_(val);
626 total_bits = 2 + parameter + 5 + val_bits;
628 if(total_bits <= 32) {
630 pattern <<= (parameter + 5);
632 pattern <<= val_bits;
633 pattern |= (val & ((1 << val_bits) - 1));
634 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
638 /* write the '-0' escape code first */
639 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
641 /* write the length */
642 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
644 /* write the value */
645 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
651 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
653 unsigned total_bits, interesting_bits, msbs, uval;
657 assert(bb->buffer != 0);
658 assert(parameter <= 30);
660 /* convert signed to unsigned */
663 * (unsigned)(((--val) << 1) - 1);
664 * but without the overflow problem at -MAXINT
666 uval = (unsigned)(((-(++val)) << 1) + 1);
668 uval = (unsigned)(val << 1);
670 msbs = uval >> parameter;
671 interesting_bits = 1 + parameter;
672 total_bits = interesting_bits + msbs;
673 pattern = 1 << parameter; /* the unary end bit */
674 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
676 if(total_bits <= 32) {
677 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
681 /* write the unary MSBs */
682 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
684 /* write the unary end bit and binary LSBs */
685 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
691 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
693 unsigned total_bits, interesting_bits, msbs, uval;
697 assert(bb->buffer != 0);
698 assert(parameter <= 30);
702 /* convert signed to unsigned */
705 * (unsigned)(((--val) << 1) - 1);
706 * but without the overflow problem at -MAXINT
708 uval = (unsigned)(((-(++val)) << 1) + 1);
710 uval = (unsigned)(val << 1);
712 msbs = uval >> parameter;
713 interesting_bits = 1 + parameter;
714 total_bits = interesting_bits + msbs;
715 pattern = 1 << parameter; /* the unary end bit */
716 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
718 if(total_bits <= 32) {
719 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
722 else if(total_bits > max_bits) {
727 /* write the unary MSBs */
728 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
730 /* write the unary end bit and binary LSBs */
731 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
737 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
739 unsigned total_bits, msbs, uval;
743 assert(bb->buffer != 0);
744 assert(parameter > 0);
746 /* convert signed to unsigned */
749 * (unsigned)(((--val) << 1) - 1);
750 * but without the overflow problem at -MAXINT
752 uval = (unsigned)(((-(++val)) << 1) + 1);
754 uval = (unsigned)(val << 1);
756 k = ilog2_(parameter);
757 if(parameter == 1u<<k) {
763 total_bits = 1 + k + msbs;
764 pattern = 1 << k; /* the unary end bit */
765 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
767 if(total_bits <= 32) {
768 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
772 /* write the unary MSBs */
773 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
775 /* write the unary end bit and binary LSBs */
776 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
783 d = (1 << (k+1)) - parameter;
784 q = uval / parameter;
785 r = uval - (q * parameter);
786 /* write the unary MSBs */
787 if(!FLAC__bitbuffer_write_zeroes(bb, q))
789 /* write the unary end bit */
790 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
792 /* write the binary LSBs */
794 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
798 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
805 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
807 unsigned total_bits, msbs;
811 assert(bb->buffer != 0);
812 assert(parameter > 0);
814 k = ilog2_(parameter);
815 if(parameter == 1u<<k) {
821 total_bits = 1 + k + msbs;
822 pattern = 1 << k; /* the unary end bit */
823 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
825 if(total_bits <= 32) {
826 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
830 /* write the unary MSBs */
831 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
833 /* write the unary end bit and binary LSBs */
834 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
841 d = (1 << (k+1)) - parameter;
842 q = uval / parameter;
843 r = uval - (q * parameter);
844 /* write the unary MSBs */
845 if(!FLAC__bitbuffer_write_zeroes(bb, q))
847 /* write the unary end bit */
848 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
850 /* write the binary LSBs */
852 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
856 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
863 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
868 assert(bb->buffer != 0);
870 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
873 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
875 else if(val < 0x800) {
876 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
877 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
879 else if(val < 0x10000) {
880 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
881 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
882 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
884 else if(val < 0x200000) {
885 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
886 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
887 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
888 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
890 else if(val < 0x4000000) {
891 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
892 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
893 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
894 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
895 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
898 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
899 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
900 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
901 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
909 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
914 assert(bb->buffer != 0);
916 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
919 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
921 else if(val < 0x800) {
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
925 else if(val < 0x10000) {
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
930 else if(val < 0x200000) {
931 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
936 else if(val < 0x4000000) {
937 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
938 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
939 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
940 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
941 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
943 else if(val < 0x80000000) {
944 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
945 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
946 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
947 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
948 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
949 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
952 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
953 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
954 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
955 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
956 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
957 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
958 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
964 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
966 /* 0-pad to byte boundary */
968 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
973 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
975 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
977 /* to avoid a drastic speed penalty we don't:
979 assert(bb->buffer != 0);
980 assert(bb->bits == 0);
984 if(bb->total_consumed_bits < bb->total_bits) {
985 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
989 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
995 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
997 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
999 /* to avoid a drastic speed penalty we don't:
1001 assert(bb->buffer != 0);
1002 assert(bb->bits == 0);
1006 if(bb->total_consumed_bits < bb->total_bits) {
1007 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1008 bb->consumed_bits++;
1009 if(bb->consumed_bits == 8) {
1010 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1011 bb->consumed_bytes++;
1012 bb->consumed_bits = 0;
1014 bb->total_consumed_bits++;
1018 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1024 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)
1026 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1028 /* to avoid a drastic speed penalty we don't:
1030 assert(bb->buffer != 0);
1031 assert(bb->bits == 0);
1035 if(bb->total_consumed_bits < bb->total_bits) {
1037 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1038 bb->consumed_bits++;
1039 if(bb->consumed_bits == 8) {
1040 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1041 bb->consumed_bytes++;
1042 bb->consumed_bits = 0;
1044 bb->total_consumed_bits++;
1048 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1054 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)
1056 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1058 /* to avoid a drastic speed penalty we don't:
1060 assert(bb->buffer != 0);
1061 assert(bb->bits == 0);
1065 if(bb->total_consumed_bits < bb->total_bits) {
1067 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1068 bb->consumed_bits++;
1069 if(bb->consumed_bits == 8) {
1070 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1071 bb->consumed_bytes++;
1072 bb->consumed_bits = 0;
1074 bb->total_consumed_bits++;
1078 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1084 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)
1089 assert(bb->buffer != 0);
1094 for(i = 0; i < bits; i++) {
1095 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1101 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)
1107 assert(bb->buffer != 0);
1112 for(i = 0; i < bits; i++) {
1113 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
1129 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)
1134 assert(bb->buffer != 0);
1139 for(i = 0; i < bits; i++) {
1140 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1146 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)
1152 assert(bb->buffer != 0);
1157 for(i = 0; i < bits; i++) {
1158 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
1174 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1176 unsigned bit, val_ = 0;
1179 assert(bb->buffer != 0);
1182 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1193 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)
1195 uint32 sign = 0, lsbs = 0, msbs = 0;
1199 assert(bb->buffer != 0);
1200 assert(parameter <= 31);
1202 /* read the unary MSBs and end bit */
1204 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1211 /* read the sign bit */
1212 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1214 /* read the binary LSBs */
1215 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1218 /* compose the value */
1219 *val = (msbs << parameter) | lsbs;
1226 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)
1228 uint32 lsbs = 0, msbs = 0;
1232 assert(bb->buffer != 0);
1233 assert(parameter <= 31);
1235 /* read the unary MSBs and end bit */
1237 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1244 /* read the binary LSBs */
1245 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1247 /* compose the value */
1248 uval = (msbs << parameter) | lsbs;
1250 *val = -((int)(uval >> 1)) - 1;
1252 *val = (int)(uval >> 1);
1257 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)
1259 uint32 lsbs = 0, msbs = 0;
1260 unsigned bit, uval, k;
1263 assert(bb->buffer != 0);
1265 k = ilog2_(parameter);
1267 /* read the unary MSBs and end bit */
1269 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1276 /* read the binary LSBs */
1277 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1280 if(parameter == 1u<<k) {
1281 /* compose the value */
1282 uval = (msbs << k) | lsbs;
1285 unsigned d = (1 << (k+1)) - parameter;
1287 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1293 /* compose the value */
1294 uval = msbs * parameter + lsbs;
1297 /* unfold unsigned to signed */
1299 *val = -((int)(uval >> 1)) - 1;
1301 *val = (int)(uval >> 1);
1306 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)
1308 uint32 lsbs, msbs = 0;
1312 assert(bb->buffer != 0);
1314 k = ilog2_(parameter);
1316 /* read the unary MSBs and end bit */
1318 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1325 /* read the binary LSBs */
1326 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1329 if(parameter == 1u<<k) {
1330 /* compose the value */
1331 *val = (msbs << k) | lsbs;
1334 unsigned d = (1 << (k+1)) - parameter;
1336 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1342 /* compose the value */
1343 *val = msbs * parameter + lsbs;
1349 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1350 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)
1356 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1359 raw[(*rawlen)++] = (byte)x;
1360 if(!(x & 0x80)) { /* 0xxxxxxx */
1364 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1368 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1372 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1376 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1380 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1389 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1392 raw[(*rawlen)++] = (byte)x;
1393 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1404 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1405 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)
1411 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1414 raw[(*rawlen)++] = (byte)x;
1415 if(!(x & 0x80)) { /* 0xxxxxxx */
1419 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1423 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1427 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1431 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1435 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1439 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1444 *val = 0xffffffffffffffff;
1448 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1451 raw[(*rawlen)++] = (byte)x;
1452 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1453 *val = 0xffffffffffffffff;
1463 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1467 fprintf(out, "bitbuffer is NULL\n");
1470 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);
1471 for(i = 0; i < bb->bytes; i++) {
1472 fprintf(out, "%08X: ", i);
1473 for(j = 0; j < 8; j++)
1474 if(i*8+j < bb->total_consumed_bits)
1477 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1481 fprintf(out, "%08X: ", i);
1482 for(j = 0; j < bb->bits; j++)
1483 if(i*8+j < bb->total_consumed_bits)
1486 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);