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 unsigned silog2_(int v)
68 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
73 assert(bb->buffer != 0);
75 if(bb->capacity == new_capacity)
78 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
81 memset(new_buffer, 0, new_capacity);
82 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
83 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
84 bb->bytes = new_capacity;
86 bb->total_bits = (new_capacity<<3);
88 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
89 bb->consumed_bytes = new_capacity;
90 bb->consumed_bits = 0;
91 bb->total_consumed_bits = (new_capacity<<3);
93 bb->buffer = new_buffer;
94 bb->capacity = new_capacity;
98 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
100 unsigned new_capacity;
102 assert(min_bytes_to_add > 0);
104 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
105 return bitbuffer_resize_(bb, new_capacity);
108 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
111 assert(bb->buffer != 0);
112 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
113 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
118 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
122 /* first shift the unconsumed buffer data toward the front as much as possible */
123 if(bb->total_consumed_bits >= 8) {
124 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
125 for( ; r < r_end; l++, r++)
126 bb->buffer[l] = bb->buffer[r];
127 for( ; l < r_end; l++)
129 bb->bytes -= bb->consumed_bytes;
130 bb->total_bits -= (bb->consumed_bytes<<3);
131 bb->consumed_bytes = 0;
132 bb->total_consumed_bits = bb->consumed_bits;
134 /* grow if we need to */
135 if(bb->capacity <= 1) {
136 if(!bitbuffer_resize_(bb, 16))
139 /* finally, read in some data; if OK, go back to read_bit_, else fail */
140 bytes = bb->capacity - bb->bytes;
141 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
144 bb->total_bits += (bytes<<3);
148 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
153 bb->bytes = bb->bits = bb->total_bits = 0;
154 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
157 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
160 FLAC__bitbuffer_init(bb);
165 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
168 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
169 bb->capacity = bb->bytes = bytes;
171 bb->total_bits = (bytes<<3);
172 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
177 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
179 static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
180 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
186 if(dest->bits != src->consumed_bits)
188 if(!bitbuffer_ensure_size_(dest, bits_to_add))
190 if(dest->bits == 0) {
191 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
193 else if(dest->bits + bits_to_add > 8) {
194 dest->buffer[dest->bytes] <<= (8 - dest->bits);
195 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
196 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
199 dest->buffer[dest->bytes] <<= bits_to_add;
200 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
202 dest->bits = src->bits;
203 dest->total_bits += bits_to_add;
204 dest->bytes = dest->total_bits / 8;
209 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
216 bb->bytes = bb->bits = bb->total_bits = 0;
217 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
220 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
222 if(bb->buffer == 0) {
223 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
224 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
227 memset(bb->buffer, 0, bb->capacity);
230 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
232 bb->bytes = bb->bits = bb->total_bits = 0;
233 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
237 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
239 if(dest->capacity < src->capacity)
240 if(!bitbuffer_resize_(dest, src->capacity))
242 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
243 dest->bytes = src->bytes;
244 dest->bits = src->bits;
245 dest->total_bits = src->total_bits;
246 dest->consumed_bytes = src->consumed_bytes;
247 dest->consumed_bits = src->consumed_bits;
248 dest->total_consumed_bits = src->total_consumed_bits;
252 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
257 assert(bb->buffer != 0);
261 if(!bitbuffer_ensure_size_(bb, bits))
263 bb->total_bits += bits;
265 n = min(8 - bb->bits, bits);
267 bb->buffer[bb->bytes] <<= n;
278 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
280 static uint32 mask[] = {
282 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
283 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
284 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
285 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
286 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
287 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
288 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
289 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
294 assert(bb->buffer != 0);
299 if(!bitbuffer_ensure_size_(bb, bits))
302 bb->total_bits += bits;
305 if(n == 8) { /* i.e. bb->bits == 0 */
307 bb->buffer[bb->bytes] = val;
312 bb->buffer[bb->bytes++] = val;
317 bb->buffer[bb->bytes++] = val >> k;
318 val &= (~(0xffffffff << k));
323 bb->buffer[bb->bytes] <<= bits;
324 bb->buffer[bb->bytes] |= val;
335 bb->buffer[bb->bytes] <<= n;
336 bb->buffer[bb->bytes] |= (val>>k);
337 val &= (~(0xffffffff << k));
347 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
349 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
352 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
354 static uint64 mask[] = {
356 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
357 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
358 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
359 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
360 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
361 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
362 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
363 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
364 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
365 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
366 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
367 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
368 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
369 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
370 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
371 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
376 assert(bb->buffer != 0);
381 if(!bitbuffer_ensure_size_(bb, bits))
384 bb->total_bits += bits;
388 bb->buffer[bb->bytes] = val;
393 bb->buffer[bb->bytes++] = val;
398 bb->buffer[bb->bytes++] = val >> k;
399 val &= (~(0xffffffffffffffff << k));
404 n = min(8 - bb->bits, bits);
406 bb->buffer[bb->bytes] <<= n;
407 bb->buffer[bb->bytes] |= (val>>k);
408 val &= (~(0xffffffffffffffff << k));
421 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
423 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
426 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
430 /* convert signed to unsigned */
433 * (unsigned)(((--val) << 1) - 1);
434 * but without the overflow problem at -MAXINT
436 uval = (unsigned)(((-(++val)) << 1) + 1);
438 uval = (unsigned)(val << 1);
440 msbs = uval >> parameter;
442 return 1 + parameter + msbs;
445 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
447 unsigned bits, msbs, uval;
450 assert(parameter > 0);
452 /* convert signed to unsigned */
455 * (unsigned)(((--val) << 1) - 1);
456 * but without the overflow problem at -MAXINT
458 uval = (unsigned)(((-(++val)) << 1) + 1);
460 uval = (unsigned)(val << 1);
462 k = ilog2_(parameter);
463 if(parameter == 1u<<k) {
472 d = (1 << (k+1)) - parameter;
473 q = uval / parameter;
474 r = uval - (q * parameter);
483 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
488 assert(parameter > 0);
490 k = ilog2_(parameter);
491 if(parameter == 1u<<k) {
500 d = (1 << (k+1)) - parameter;
501 q = uval / parameter;
502 r = uval - (q * parameter);
511 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
513 unsigned total_bits, interesting_bits, msbs;
517 assert(bb->buffer != 0);
518 assert(parameter <= 31);
520 /* init pattern with the unary end bit and the sign bit */
528 msbs = val >> parameter;
529 interesting_bits = 2 + parameter;
530 total_bits = interesting_bits + msbs;
531 pattern <<= parameter;
532 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
534 if(total_bits <= 32) {
535 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
539 /* write the unary MSBs */
540 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
542 /* write the unary end bit, the sign bit, and binary LSBs */
543 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
549 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
551 unsigned total_bits, interesting_bits, msbs;
555 assert(bb->buffer != 0);
556 assert(parameter <= 31);
560 /* init pattern with the unary end bit and the sign bit */
568 msbs = val >> parameter;
569 interesting_bits = 2 + parameter;
570 total_bits = interesting_bits + msbs;
571 pattern <<= parameter;
572 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
574 if(total_bits <= 32) {
575 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
578 else if(total_bits > max_bits) {
583 /* write the unary MSBs */
584 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
586 /* write the unary end bit, the sign bit, and binary LSBs */
587 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
593 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
595 unsigned total_bits, val_bits;
599 assert(bb->buffer != 0);
600 assert(parameter <= 31);
602 val_bits = silog2_(val);
603 total_bits = 2 + parameter + 5 + val_bits;
605 if(total_bits <= 32) {
607 pattern <<= (parameter + 5);
609 pattern <<= val_bits;
610 pattern |= (val & ((1 << val_bits) - 1));
611 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
615 /* write the '-0' escape code first */
616 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
618 /* write the length */
619 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
621 /* write the value */
622 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
628 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
630 unsigned total_bits, interesting_bits, msbs, uval;
634 assert(bb->buffer != 0);
635 assert(parameter <= 30);
637 /* convert signed to unsigned */
640 * (unsigned)(((--val) << 1) - 1);
641 * but without the overflow problem at -MAXINT
643 uval = (unsigned)(((-(++val)) << 1) + 1);
645 uval = (unsigned)(val << 1);
647 msbs = uval >> parameter;
648 interesting_bits = 1 + parameter;
649 total_bits = interesting_bits + msbs;
650 pattern = 1 << parameter; /* the unary end bit */
651 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
653 if(total_bits <= 32) {
654 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
658 /* write the unary MSBs */
659 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
661 /* write the unary end bit and binary LSBs */
662 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
668 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
670 unsigned total_bits, interesting_bits, msbs, uval;
674 assert(bb->buffer != 0);
675 assert(parameter <= 30);
679 /* convert signed to unsigned */
682 * (unsigned)(((--val) << 1) - 1);
683 * but without the overflow problem at -MAXINT
685 uval = (unsigned)(((-(++val)) << 1) + 1);
687 uval = (unsigned)(val << 1);
689 msbs = uval >> parameter;
690 interesting_bits = 1 + parameter;
691 total_bits = interesting_bits + msbs;
692 pattern = 1 << parameter; /* the unary end bit */
693 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
695 if(total_bits <= 32) {
696 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
699 else if(total_bits > max_bits) {
704 /* write the unary MSBs */
705 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
707 /* write the unary end bit and binary LSBs */
708 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
714 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
716 unsigned total_bits, msbs, uval;
720 assert(bb->buffer != 0);
721 assert(parameter > 0);
723 /* convert signed to unsigned */
726 * (unsigned)(((--val) << 1) - 1);
727 * but without the overflow problem at -MAXINT
729 uval = (unsigned)(((-(++val)) << 1) + 1);
731 uval = (unsigned)(val << 1);
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_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
784 unsigned total_bits, msbs;
788 assert(bb->buffer != 0);
789 assert(parameter > 0);
791 k = ilog2_(parameter);
792 if(parameter == 1u<<k) {
798 total_bits = 1 + k + msbs;
799 pattern = 1 << k; /* the unary end bit */
800 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
802 if(total_bits <= 32) {
803 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
807 /* write the unary MSBs */
808 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
810 /* write the unary end bit and binary LSBs */
811 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
818 d = (1 << (k+1)) - parameter;
819 q = uval / parameter;
820 r = uval - (q * parameter);
821 /* write the unary MSBs */
822 if(!FLAC__bitbuffer_write_zeroes(bb, q))
824 /* write the unary end bit */
825 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
827 /* write the binary LSBs */
829 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
833 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
840 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
845 assert(bb->buffer != 0);
847 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
850 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
852 else if(val < 0x800) {
853 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
854 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
856 else if(val < 0x10000) {
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
858 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
859 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
861 else if(val < 0x200000) {
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
865 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
867 else if(val < 0x4000000) {
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
869 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
870 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
875 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
876 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
877 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
878 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
879 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
880 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
886 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
891 assert(bb->buffer != 0);
893 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
896 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
898 else if(val < 0x800) {
899 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
900 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
902 else if(val < 0x10000) {
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
904 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
905 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
907 else if(val < 0x200000) {
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
910 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
911 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
913 else if(val < 0x4000000) {
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
915 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
916 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
920 else if(val < 0x80000000) {
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
929 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
930 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
931 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
932 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
933 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
934 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
935 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
941 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
943 /* 0-pad to byte boundary */
945 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
950 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
952 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
954 /* to avoid a drastic speed penalty we don't:
956 assert(bb->buffer != 0);
957 assert(bb->bits == 0);
961 if(bb->total_consumed_bits < bb->total_bits) {
962 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
966 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
972 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
974 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
976 /* to avoid a drastic speed penalty we don't:
978 assert(bb->buffer != 0);
979 assert(bb->bits == 0);
983 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_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1002 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1004 /* to avoid a drastic speed penalty we don't:
1006 assert(bb->buffer != 0);
1007 assert(bb->bits == 0);
1011 if(bb->total_consumed_bits < bb->total_bits) {
1013 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1014 bb->consumed_bits++;
1015 if(bb->consumed_bits == 8) {
1016 bb->consumed_bytes++;
1017 bb->consumed_bits = 0;
1019 bb->total_consumed_bits++;
1023 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1029 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)
1031 static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1033 /* to avoid a drastic speed penalty we don't:
1035 assert(bb->buffer != 0);
1036 assert(bb->bits == 0);
1040 if(bb->total_consumed_bits < bb->total_bits) {
1042 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1043 bb->consumed_bits++;
1044 if(bb->consumed_bits == 8) {
1045 bb->consumed_bytes++;
1046 bb->consumed_bits = 0;
1048 bb->total_consumed_bits++;
1052 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1058 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)
1063 assert(bb->buffer != 0);
1068 for(i = 0; i < bits; i++) {
1069 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1075 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)
1081 assert(bb->buffer != 0);
1086 for(i = 0; i < bits; i++) {
1087 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
1103 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)
1108 assert(bb->buffer != 0);
1113 for(i = 0; i < bits; i++) {
1114 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1120 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)
1126 assert(bb->buffer != 0);
1131 for(i = 0; i < bits; i++) {
1132 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
1148 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)
1150 uint32 sign = 0, lsbs = 0, msbs = 0;
1154 assert(bb->buffer != 0);
1155 assert(parameter <= 31);
1157 /* read the unary MSBs and end bit */
1159 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1166 /* read the sign bit */
1167 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1169 /* read the binary LSBs */
1170 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1173 /* compose the value */
1174 *val = (msbs << parameter) | lsbs;
1181 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)
1183 uint32 lsbs = 0, msbs = 0;
1187 assert(bb->buffer != 0);
1188 assert(parameter <= 31);
1190 /* read the unary MSBs and end bit */
1192 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1199 /* read the binary LSBs */
1200 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1202 /* compose the value */
1203 uval = (msbs << parameter) | lsbs;
1205 *val = -((int)(uval >> 1)) - 1;
1207 *val = (int)(uval >> 1);
1212 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)
1214 uint32 lsbs = 0, msbs = 0;
1215 unsigned bit, uval, k;
1218 assert(bb->buffer != 0);
1220 k = ilog2_(parameter);
1222 /* read the unary MSBs and end bit */
1224 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1231 /* read the binary LSBs */
1232 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1235 if(parameter == 1u<<k) {
1236 /* compose the value */
1237 uval = (msbs << k) | lsbs;
1240 unsigned d = (1 << (k+1)) - parameter;
1242 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1248 /* compose the value */
1249 uval = msbs * parameter + lsbs;
1252 /* unfold unsigned to signed */
1254 *val = -((int)(uval >> 1)) - 1;
1256 *val = (int)(uval >> 1);
1261 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)
1263 uint32 lsbs, msbs = 0;
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 *val = (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 *val = msbs * parameter + lsbs;
1304 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1305 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)
1311 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1314 raw[(*rawlen)++] = (byte)x;
1315 if(!(x & 0x80)) { /* 0xxxxxxx */
1319 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1323 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1327 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1331 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1335 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
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 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1359 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)
1365 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1368 raw[(*rawlen)++] = (byte)x;
1369 if(!(x & 0x80)) { /* 0xxxxxxx */
1373 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1377 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1381 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1385 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1389 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1393 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1400 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1403 raw[(*rawlen)++] = (byte)x;
1404 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
1416 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1420 fprintf(out, "bitbuffer is NULL\n");
1423 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);
1424 for(i = 0; i < bb->bytes; i++) {
1425 fprintf(out, "%08X: ", i);
1426 for(j = 0; j < 8; j++)
1427 if(i*8+j < bb->total_consumed_bits)
1430 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1434 fprintf(out, "%08X: ", i);
1435 for(j = 0; j < bb->bits; j++)
1436 if(i*8+j < bb->total_consumed_bits)
1439 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);