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"
25 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
30 #define min(x,y) ((x)<(y)?(x):(y))
34 #define max(x,y) ((x)>(y)?(x):(y))
36 static unsigned ilog2_(unsigned v)
45 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
50 assert(bb->buffer != 0);
52 if(bb->capacity == new_capacity)
55 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
58 memset(new_buffer, 0, new_capacity);
59 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
60 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
61 bb->bytes = new_capacity;
63 bb->total_bits = (new_capacity<<3);
65 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
66 bb->consumed_bytes = new_capacity;
67 bb->consumed_bits = 0;
68 bb->total_consumed_bits = (new_capacity<<3);
70 bb->buffer = new_buffer;
71 bb->capacity = new_capacity;
75 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
77 unsigned new_capacity;
79 assert(min_bytes_to_add > 0);
81 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
82 return bitbuffer_resize_(bb, new_capacity);
85 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
88 assert(bb->buffer != 0);
89 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
90 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
95 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
99 /* first shift the unconsumed buffer data toward the front as much as possible */
100 if(bb->total_consumed_bits >= 8) {
101 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
102 for( ; r < r_end; l++, r++)
103 bb->buffer[l] = bb->buffer[r];
104 for( ; l < r_end; l++)
106 bb->bytes -= bb->consumed_bytes;
107 bb->total_bits -= (bb->consumed_bytes<<3);
108 bb->consumed_bytes = 0;
109 bb->total_consumed_bits = bb->consumed_bits;
111 /* grow if we need to */
112 if(bb->capacity <= 1) {
113 if(!bitbuffer_resize_(bb, 16))
116 /* finally, read in some data; if OK, go back to read_bit_, else fail */
117 bytes = bb->capacity - bb->bytes;
118 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
121 bb->total_bits += (bytes<<3);
125 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
130 bb->bytes = bb->bits = bb->total_bits = 0;
131 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
134 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
137 FLAC__bitbuffer_init(bb);
142 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
145 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
146 bb->capacity = bb->bytes = bytes;
148 bb->total_bits = (bytes<<3);
149 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
154 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
156 static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
157 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
163 if(dest->bits != src->consumed_bits)
165 if(!bitbuffer_ensure_size_(dest, bits_to_add))
167 if(dest->bits == 0) {
168 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
170 else if(dest->bits + bits_to_add > 8) {
171 dest->buffer[dest->bytes] <<= (8 - dest->bits);
172 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
173 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
176 dest->buffer[dest->bytes] <<= bits_to_add;
177 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
179 dest->bits = src->bits;
180 dest->total_bits += bits_to_add;
181 dest->bytes = dest->total_bits / 8;
186 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
193 bb->bytes = bb->bits = bb->total_bits = 0;
194 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
197 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
199 if(bb->buffer == 0) {
200 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
201 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
204 memset(bb->buffer, 0, bb->capacity);
207 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
209 bb->bytes = bb->bits = bb->total_bits = 0;
210 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
214 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
216 if(dest->capacity < src->capacity)
217 if(!bitbuffer_resize_(dest, src->capacity))
219 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
220 dest->bytes = src->bytes;
221 dest->bits = src->bits;
222 dest->total_bits = src->total_bits;
223 dest->consumed_bytes = src->consumed_bytes;
224 dest->consumed_bits = src->consumed_bits;
225 dest->total_consumed_bits = src->total_consumed_bits;
229 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
234 assert(bb->buffer != 0);
238 if(!bitbuffer_ensure_size_(bb, bits))
240 bb->total_bits += bits;
242 n = min(8 - bb->bits, bits);
244 bb->buffer[bb->bytes] <<= n;
255 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
257 static uint32 mask[] = {
259 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
260 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
261 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
262 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
263 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
264 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
265 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
266 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
271 assert(bb->buffer != 0);
276 if(!bitbuffer_ensure_size_(bb, bits))
279 bb->total_bits += bits;
282 if(n == 8) { /* i.e. bb->bits == 0 */
284 bb->buffer[bb->bytes] = val;
289 bb->buffer[bb->bytes++] = val;
294 bb->buffer[bb->bytes++] = val >> k;
295 val &= (~(0xffffffff << k));
300 bb->buffer[bb->bytes] <<= bits;
301 bb->buffer[bb->bytes] |= val;
312 bb->buffer[bb->bytes] <<= n;
313 bb->buffer[bb->bytes] |= (val>>k);
314 val &= (~(0xffffffff << k));
324 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
326 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
329 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
331 static uint64 mask[] = {
333 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
334 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
335 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
336 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
337 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
338 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
339 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
340 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
341 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
342 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
343 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
344 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
345 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
346 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
347 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
348 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
353 assert(bb->buffer != 0);
358 if(!bitbuffer_ensure_size_(bb, bits))
361 bb->total_bits += bits;
365 bb->buffer[bb->bytes] = val;
370 bb->buffer[bb->bytes++] = val;
375 bb->buffer[bb->bytes++] = val >> k;
376 val &= (~(0xffffffffffffffff << k));
381 n = min(8 - bb->bits, bits);
383 bb->buffer[bb->bytes] <<= n;
384 bb->buffer[bb->bytes] |= (val>>k);
385 val &= (~(0xffffffffffffffff << k));
398 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
400 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
403 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
407 /* convert signed to unsigned */
410 * (unsigned)(((--val) << 1) - 1);
411 * but without the overflow problem at -MAXINT
413 uval = (unsigned)(((-(++val)) << 1) + 1);
415 uval = (unsigned)(val << 1);
417 msbs = uval >> parameter;
419 return 1 + parameter + msbs;
422 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
424 unsigned bits, msbs, uval;
427 assert(parameter > 0);
429 /* convert signed to unsigned */
432 * (unsigned)(((--val) << 1) - 1);
433 * but without the overflow problem at -MAXINT
435 uval = (unsigned)(((-(++val)) << 1) + 1);
437 uval = (unsigned)(val << 1);
439 k = ilog2_(parameter);
440 if(parameter == 1u<<k) {
449 d = (1 << (k+1)) - parameter;
450 q = uval / parameter;
451 r = uval - (q * parameter);
460 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
465 assert(parameter > 0);
467 k = ilog2_(parameter);
468 if(parameter == 1u<<k) {
477 d = (1 << (k+1)) - parameter;
478 q = uval / parameter;
479 r = uval - (q * parameter);
488 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
490 unsigned total_bits, interesting_bits, msbs;
494 assert(bb->buffer != 0);
495 assert(parameter <= 31);
497 /* init pattern with the unary end bit and the sign bit */
505 msbs = val >> parameter;
506 interesting_bits = 2 + parameter;
507 total_bits = interesting_bits + msbs;
508 pattern <<= parameter;
509 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
511 if(total_bits <= 32) {
512 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
516 /* write the unary MSBs */
517 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
519 /* write the unary end bit, the sign bit, and binary LSBs */
520 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
526 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
528 unsigned total_bits, interesting_bits, msbs;
532 assert(bb->buffer != 0);
533 assert(parameter <= 31);
537 /* init pattern with the unary end bit and the sign bit */
545 msbs = val >> parameter;
546 interesting_bits = 2 + parameter;
547 total_bits = interesting_bits + msbs;
548 pattern <<= parameter;
549 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
551 if(total_bits <= 32) {
552 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
555 else if(total_bits > max_bits) {
560 /* write the unary MSBs */
561 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
563 /* write the unary end bit, the sign bit, and binary LSBs */
564 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
570 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
572 unsigned total_bits, interesting_bits, msbs, uval;
576 assert(bb->buffer != 0);
577 assert(parameter <= 30);
579 /* convert signed to unsigned */
582 * (unsigned)(((--val) << 1) - 1);
583 * but without the overflow problem at -MAXINT
585 uval = (unsigned)(((-(++val)) << 1) + 1);
587 uval = (unsigned)(val << 1);
589 msbs = uval >> parameter;
590 interesting_bits = 1 + parameter;
591 total_bits = interesting_bits + msbs;
592 pattern = 1 << parameter; /* the unary end bit */
593 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
595 if(total_bits <= 32) {
596 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
600 /* write the unary MSBs */
601 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
603 /* write the unary end bit and binary LSBs */
604 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
610 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
612 unsigned total_bits, interesting_bits, msbs, uval;
616 assert(bb->buffer != 0);
617 assert(parameter <= 30);
621 /* convert signed to unsigned */
624 * (unsigned)(((--val) << 1) - 1);
625 * but without the overflow problem at -MAXINT
627 uval = (unsigned)(((-(++val)) << 1) + 1);
629 uval = (unsigned)(val << 1);
631 msbs = uval >> parameter;
632 interesting_bits = 1 + parameter;
633 total_bits = interesting_bits + msbs;
634 pattern = 1 << parameter; /* the unary end bit */
635 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
637 if(total_bits <= 32) {
638 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
641 else if(total_bits > max_bits) {
646 /* write the unary MSBs */
647 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
649 /* write the unary end bit and binary LSBs */
650 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
656 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
658 unsigned total_bits, msbs, uval;
662 assert(bb->buffer != 0);
663 assert(parameter > 0);
665 /* convert signed to unsigned */
668 * (unsigned)(((--val) << 1) - 1);
669 * but without the overflow problem at -MAXINT
671 uval = (unsigned)(((-(++val)) << 1) + 1);
673 uval = (unsigned)(val << 1);
675 k = ilog2_(parameter);
676 if(parameter == 1u<<k) {
682 total_bits = 1 + k + msbs;
683 pattern = 1 << k; /* the unary end bit */
684 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
686 if(total_bits <= 32) {
687 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
691 /* write the unary MSBs */
692 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
694 /* write the unary end bit and binary LSBs */
695 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
702 d = (1 << (k+1)) - parameter;
703 q = uval / parameter;
704 r = uval - (q * parameter);
705 /* write the unary MSBs */
706 if(!FLAC__bitbuffer_write_zeroes(bb, q))
708 /* write the unary end bit */
709 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
711 /* write the binary LSBs */
713 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
717 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
724 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
726 unsigned total_bits, msbs;
730 assert(bb->buffer != 0);
731 assert(parameter > 0);
733 k = ilog2_(parameter);
734 if(parameter == 1u<<k) {
740 total_bits = 1 + k + msbs;
741 pattern = 1 << k; /* the unary end bit */
742 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
744 if(total_bits <= 32) {
745 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
749 /* write the unary MSBs */
750 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
752 /* write the unary end bit and binary LSBs */
753 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
760 d = (1 << (k+1)) - parameter;
761 q = uval / parameter;
762 r = uval - (q * parameter);
763 /* write the unary MSBs */
764 if(!FLAC__bitbuffer_write_zeroes(bb, q))
766 /* write the unary end bit */
767 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
769 /* write the binary LSBs */
771 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
775 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
782 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
787 assert(bb->buffer != 0);
789 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
792 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
794 else if(val < 0x800) {
795 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
796 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
798 else if(val < 0x10000) {
799 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
800 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
801 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
803 else if(val < 0x200000) {
804 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
805 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
806 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
807 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
809 else if(val < 0x4000000) {
810 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
811 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
812 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
813 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
814 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
817 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
818 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
819 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
820 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
821 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
822 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
828 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
833 assert(bb->buffer != 0);
835 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
838 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
840 else if(val < 0x800) {
841 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
842 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
844 else if(val < 0x10000) {
845 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
846 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
847 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
849 else if(val < 0x200000) {
850 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
851 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
852 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
853 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
855 else if(val < 0x4000000) {
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
858 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
859 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
860 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
862 else if(val < 0x80000000) {
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
865 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
866 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
873 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
874 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
875 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
876 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
877 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
883 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
885 /* 0-pad to byte boundary */
887 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
892 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
894 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
896 /* to avoid a drastic speed penalty we don't:
898 assert(bb->buffer != 0);
899 assert(bb->bits == 0);
903 if(bb->total_consumed_bits < bb->total_bits) {
904 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
908 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
914 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
916 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
918 /* to avoid a drastic speed penalty we don't:
920 assert(bb->buffer != 0);
921 assert(bb->bits == 0);
925 if(bb->total_consumed_bits < bb->total_bits) {
926 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
928 if(bb->consumed_bits == 8) {
929 bb->consumed_bytes++;
930 bb->consumed_bits = 0;
932 bb->total_consumed_bits++;
936 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
942 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)
944 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
946 /* to avoid a drastic speed penalty we don't:
948 assert(bb->buffer != 0);
949 assert(bb->bits == 0);
953 if(bb->total_consumed_bits < bb->total_bits) {
955 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
957 if(bb->consumed_bits == 8) {
958 bb->consumed_bytes++;
959 bb->consumed_bits = 0;
961 bb->total_consumed_bits++;
965 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
971 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)
973 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
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) {
984 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
986 if(bb->consumed_bits == 8) {
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_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1005 assert(bb->buffer != 0);
1010 for(i = 0; i < bits; i++) {
1011 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1017 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)
1023 assert(bb->buffer != 0);
1028 for(i = 0; i < bits; i++) {
1029 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
1045 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)
1050 assert(bb->buffer != 0);
1055 for(i = 0; i < bits; i++) {
1056 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1062 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)
1068 assert(bb->buffer != 0);
1073 for(i = 0; i < bits; i++) {
1074 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
1090 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)
1092 uint32 sign = 0, lsbs = 0, msbs = 0;
1096 assert(bb->buffer != 0);
1097 assert(parameter <= 31);
1099 /* read the unary MSBs and end bit */
1101 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1108 /* read the sign bit */
1109 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1111 /* read the binary LSBs */
1112 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1115 /* compose the value */
1116 *val = (msbs << parameter) | lsbs;
1123 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)
1125 uint32 lsbs = 0, msbs = 0;
1129 assert(bb->buffer != 0);
1130 assert(parameter <= 31);
1132 /* read the unary MSBs and end bit */
1134 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1141 /* read the binary LSBs */
1142 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1144 /* compose the value */
1145 uval = (msbs << parameter) | lsbs;
1147 *val = -((int)(uval >> 1)) - 1;
1149 *val = (int)(uval >> 1);
1154 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)
1156 uint32 lsbs = 0, msbs = 0;
1157 unsigned bit, uval, k;
1160 assert(bb->buffer != 0);
1162 k = ilog2_(parameter);
1164 /* read the unary MSBs and end bit */
1166 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1173 /* read the binary LSBs */
1174 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1177 if(parameter == 1u<<k) {
1178 /* compose the value */
1179 uval = (msbs << k) | lsbs;
1182 unsigned d = (1 << (k+1)) - parameter;
1184 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1190 /* compose the value */
1191 uval = msbs * parameter + lsbs;
1194 /* unfold unsigned to signed */
1196 *val = -((int)(uval >> 1)) - 1;
1198 *val = (int)(uval >> 1);
1203 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)
1205 uint32 lsbs, msbs = 0;
1209 assert(bb->buffer != 0);
1211 k = ilog2_(parameter);
1213 /* read the unary MSBs and end bit */
1215 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1222 /* read the binary LSBs */
1223 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1226 if(parameter == 1u<<k) {
1227 /* compose the value */
1228 *val = (msbs << k) | lsbs;
1231 unsigned d = (1 << (k+1)) - parameter;
1233 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1239 /* compose the value */
1240 *val = msbs * parameter + lsbs;
1246 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1247 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)
1253 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1256 raw[(*rawlen)++] = (byte)x;
1257 if(!(x & 0x80)) { /* 0xxxxxxx */
1261 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1265 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1269 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1273 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1277 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1284 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1287 raw[(*rawlen)++] = (byte)x;
1288 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
1300 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1301 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)
1307 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1310 raw[(*rawlen)++] = (byte)x;
1311 if(!(x & 0x80)) { /* 0xxxxxxx */
1315 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1319 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1323 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1327 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1331 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1335 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1342 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1345 raw[(*rawlen)++] = (byte)x;
1346 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
1358 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1362 fprintf(out, "bitbuffer is NULL\n");
1365 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);
1366 for(i = 0; i < bb->bytes; i++) {
1367 fprintf(out, "%08X: ", i);
1368 for(j = 0; j < 8; j++)
1369 if(i*8+j < bb->total_consumed_bits)
1372 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1376 fprintf(out, "%08X: ", i);
1377 for(j = 0; j < bb->bits; j++)
1378 if(i*8+j < bb->total_consumed_bits)
1381 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);