1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy(), memset() */
23 #include "private/bitbuffer.h"
24 #include "private/bitmath.h"
25 #include "private/crc.h"
27 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
32 #define min(x,y) ((x)<(y)?(x):(y))
36 #define max(x,y) ((x)>(y)?(x):(y))
38 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
43 assert(bb->buffer != 0);
45 if(bb->capacity == new_capacity)
48 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
51 memset(new_buffer, 0, new_capacity);
52 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
53 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
54 bb->bytes = new_capacity;
56 bb->total_bits = (new_capacity<<3);
58 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
59 bb->consumed_bytes = new_capacity;
60 bb->consumed_bits = 0;
61 bb->total_consumed_bits = (new_capacity<<3);
63 bb->buffer = new_buffer;
64 bb->capacity = new_capacity;
68 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
70 unsigned new_capacity;
72 assert(min_bytes_to_add > 0);
74 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
75 return bitbuffer_resize_(bb, new_capacity);
78 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
81 assert(bb->buffer != 0);
82 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
83 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
88 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
92 /* first shift the unconsumed buffer data toward the front as much as possible */
93 if(bb->total_consumed_bits >= 8) {
94 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
95 for( ; r < r_end; l++, r++)
96 bb->buffer[l] = bb->buffer[r];
97 for( ; l < r_end; l++)
99 bb->bytes -= bb->consumed_bytes;
100 bb->total_bits -= (bb->consumed_bytes<<3);
101 bb->consumed_bytes = 0;
102 bb->total_consumed_bits = bb->consumed_bits;
104 /* grow if we need to */
105 if(bb->capacity <= 1) {
106 if(!bitbuffer_resize_(bb, 16))
109 /* finally, read in some data; if OK, go back to read_bit_, else fail */
110 bytes = bb->capacity - bb->bytes;
111 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
114 bb->total_bits += (bytes<<3);
118 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
123 bb->bytes = bb->bits = bb->total_bits = 0;
124 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
127 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
130 FLAC__bitbuffer_init(bb);
135 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
138 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
139 bb->capacity = bb->bytes = bytes;
141 bb->total_bits = (bytes<<3);
142 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
147 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
151 bb->read_crc16 = seed;
154 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
156 static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
157 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
164 if(dest->bits != src->consumed_bits)
166 if(!bitbuffer_ensure_size_(dest, bits_to_add))
168 if(dest->bits == 0) {
169 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
171 else if(dest->bits + bits_to_add > 8) {
172 dest->buffer[dest->bytes] <<= (8 - dest->bits);
173 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
174 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
177 dest->buffer[dest->bytes] <<= bits_to_add;
178 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
180 dest->bits = src->bits;
181 dest->total_bits += bits_to_add;
182 dest->bytes = dest->total_bits / 8;
187 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
194 bb->bytes = bb->bits = bb->total_bits = 0;
195 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
198 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
200 if(bb->buffer == 0) {
201 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
202 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
205 memset(bb->buffer, 0, bb->capacity);
208 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
210 bb->bytes = bb->bits = bb->total_bits = 0;
211 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
215 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
217 if(dest->capacity < src->capacity)
218 if(!bitbuffer_resize_(dest, src->capacity))
220 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
221 dest->bytes = src->bytes;
222 dest->bits = src->bits;
223 dest->total_bits = src->total_bits;
224 dest->consumed_bytes = src->consumed_bytes;
225 dest->consumed_bits = src->consumed_bits;
226 dest->total_consumed_bits = src->total_consumed_bits;
227 dest->read_crc16 = src->read_crc16;
231 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
236 assert(bb->buffer != 0);
240 if(!bitbuffer_ensure_size_(bb, bits))
242 bb->total_bits += bits;
244 n = min(8 - bb->bits, bits);
246 bb->buffer[bb->bytes] <<= n;
257 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
259 static const uint32 mask[] = {
261 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
262 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
263 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
264 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
265 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
266 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
267 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
268 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
273 assert(bb->buffer != 0);
278 if(!bitbuffer_ensure_size_(bb, bits))
281 bb->total_bits += bits;
284 if(n == 8) { /* i.e. bb->bits == 0 */
286 bb->buffer[bb->bytes] = val;
291 bb->buffer[bb->bytes++] = val;
296 bb->buffer[bb->bytes++] = val >> k;
297 val &= (~(0xffffffff << k));
302 bb->buffer[bb->bytes] <<= bits;
303 bb->buffer[bb->bytes] |= val;
314 bb->buffer[bb->bytes] <<= n;
315 bb->buffer[bb->bytes] |= (val>>k);
316 val &= (~(0xffffffff << k));
326 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
328 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
331 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
333 static const uint64 mask[] = {
335 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
336 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
337 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
338 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
339 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
340 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
341 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
342 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
343 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
344 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
345 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
346 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
347 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
348 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
349 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
350 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
355 assert(bb->buffer != 0);
360 if(!bitbuffer_ensure_size_(bb, bits))
363 bb->total_bits += bits;
367 bb->buffer[bb->bytes] = val;
372 bb->buffer[bb->bytes++] = val;
377 bb->buffer[bb->bytes++] = val >> k;
378 val &= (~(0xffffffffffffffff << k));
383 n = min(8 - bb->bits, bits);
385 bb->buffer[bb->bytes] <<= n;
386 bb->buffer[bb->bytes] |= (val>>k);
387 val &= (~(0xffffffffffffffff << k));
400 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
402 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
405 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
408 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
410 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
412 if(!FLAC__bitbuffer_write_zeroes(bb, val))
414 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
418 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
422 /* convert signed to unsigned */
425 * (unsigned)(((--val) << 1) - 1);
426 * but without the overflow problem at -MAXINT
428 uval = (unsigned)(((-(++val)) << 1) + 1);
430 uval = (unsigned)(val << 1);
432 msbs = uval >> parameter;
434 return 1 + parameter + msbs;
437 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
439 unsigned bits, msbs, uval;
442 assert(parameter > 0);
444 /* convert signed to unsigned */
447 * (unsigned)(((--val) << 1) - 1);
448 * but without the overflow problem at -MAXINT
450 uval = (unsigned)(((-(++val)) << 1) + 1);
452 uval = (unsigned)(val << 1);
454 k = FLAC__bitmath_ilog2(parameter);
455 if(parameter == 1u<<k) {
464 d = (1 << (k+1)) - parameter;
465 q = uval / parameter;
466 r = uval - (q * parameter);
475 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
480 assert(parameter > 0);
482 k = FLAC__bitmath_ilog2(parameter);
483 if(parameter == 1u<<k) {
492 d = (1 << (k+1)) - parameter;
493 q = uval / parameter;
494 r = uval - (q * parameter);
503 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
505 unsigned total_bits, interesting_bits, msbs;
509 assert(bb->buffer != 0);
510 assert(parameter <= 31);
512 /* init pattern with the unary end bit and the sign bit */
520 msbs = val >> parameter;
521 interesting_bits = 2 + parameter;
522 total_bits = interesting_bits + msbs;
523 pattern <<= parameter;
524 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
526 if(total_bits <= 32) {
527 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
531 /* write the unary MSBs */
532 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
534 /* write the unary end bit, the sign bit, and binary LSBs */
535 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
541 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
543 unsigned total_bits, interesting_bits, msbs;
547 assert(bb->buffer != 0);
548 assert(parameter <= 31);
552 /* init pattern with the unary end bit and the sign bit */
560 msbs = val >> parameter;
561 interesting_bits = 2 + parameter;
562 total_bits = interesting_bits + msbs;
563 pattern <<= parameter;
564 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
566 if(total_bits <= 32) {
567 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
570 else if(total_bits > max_bits) {
575 /* write the unary MSBs */
576 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
578 /* write the unary end bit, the sign bit, and binary LSBs */
579 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
585 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
587 unsigned total_bits, val_bits;
591 assert(bb->buffer != 0);
592 assert(parameter <= 31);
594 val_bits = FLAC__bitmath_silog2(val);
595 total_bits = 2 + parameter + 5 + val_bits;
597 if(total_bits <= 32) {
599 pattern <<= (parameter + 5);
601 pattern <<= val_bits;
602 pattern |= (val & ((1 << val_bits) - 1));
603 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
607 /* write the '-0' escape code first */
608 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
610 /* write the length */
611 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
613 /* write the value */
614 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
620 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
622 unsigned total_bits, interesting_bits, msbs, uval;
626 assert(bb->buffer != 0);
627 assert(parameter <= 30);
629 /* convert signed to unsigned */
632 * (unsigned)(((--val) << 1) - 1);
633 * but without the overflow problem at -MAXINT
635 uval = (unsigned)(((-(++val)) << 1) + 1);
637 uval = (unsigned)(val << 1);
639 msbs = uval >> parameter;
640 interesting_bits = 1 + parameter;
641 total_bits = interesting_bits + msbs;
642 pattern = 1 << parameter; /* the unary end bit */
643 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
645 if(total_bits <= 32) {
646 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
650 /* write the unary MSBs */
651 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
653 /* write the unary end bit and binary LSBs */
654 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
660 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
662 unsigned total_bits, interesting_bits, msbs, uval;
666 assert(bb->buffer != 0);
667 assert(parameter <= 30);
671 /* convert signed to unsigned */
674 * (unsigned)(((--val) << 1) - 1);
675 * but without the overflow problem at -MAXINT
677 uval = (unsigned)(((-(++val)) << 1) + 1);
679 uval = (unsigned)(val << 1);
681 msbs = uval >> parameter;
682 interesting_bits = 1 + parameter;
683 total_bits = interesting_bits + msbs;
684 pattern = 1 << parameter; /* the unary end bit */
685 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
687 if(total_bits <= 32) {
688 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
691 else if(total_bits > max_bits) {
696 /* write the unary MSBs */
697 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
699 /* write the unary end bit and binary LSBs */
700 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
706 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
708 unsigned total_bits, msbs, uval;
712 assert(bb->buffer != 0);
713 assert(parameter > 0);
715 /* convert signed to unsigned */
718 * (unsigned)(((--val) << 1) - 1);
719 * but without the overflow problem at -MAXINT
721 uval = (unsigned)(((-(++val)) << 1) + 1);
723 uval = (unsigned)(val << 1);
725 k = FLAC__bitmath_ilog2(parameter);
726 if(parameter == 1u<<k) {
732 total_bits = 1 + k + msbs;
733 pattern = 1 << k; /* the unary end bit */
734 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
736 if(total_bits <= 32) {
737 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
741 /* write the unary MSBs */
742 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
744 /* write the unary end bit and binary LSBs */
745 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
752 d = (1 << (k+1)) - parameter;
753 q = uval / parameter;
754 r = uval - (q * parameter);
755 /* write the unary MSBs */
756 if(!FLAC__bitbuffer_write_zeroes(bb, q))
758 /* write the unary end bit */
759 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
761 /* write the binary LSBs */
763 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
767 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
774 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
776 unsigned total_bits, msbs;
780 assert(bb->buffer != 0);
781 assert(parameter > 0);
783 k = FLAC__bitmath_ilog2(parameter);
784 if(parameter == 1u<<k) {
790 total_bits = 1 + k + msbs;
791 pattern = 1 << k; /* the unary end bit */
792 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
794 if(total_bits <= 32) {
795 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
799 /* write the unary MSBs */
800 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
802 /* write the unary end bit and binary LSBs */
803 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
810 d = (1 << (k+1)) - parameter;
811 q = uval / parameter;
812 r = uval - (q * parameter);
813 /* write the unary MSBs */
814 if(!FLAC__bitbuffer_write_zeroes(bb, q))
816 /* write the unary end bit */
817 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
819 /* write the binary LSBs */
821 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
825 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
832 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
837 assert(bb->buffer != 0);
839 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
842 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
844 else if(val < 0x800) {
845 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
846 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
848 else if(val < 0x10000) {
849 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
850 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
851 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
853 else if(val < 0x200000) {
854 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
855 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
859 else if(val < 0x4000000) {
860 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 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);
878 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
883 assert(bb->buffer != 0);
885 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
888 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
890 else if(val < 0x800) {
891 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
892 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
894 else if(val < 0x10000) {
895 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
896 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
897 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
899 else if(val < 0x200000) {
900 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
901 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
905 else if(val < 0x4000000) {
906 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
910 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
912 else if(val < 0x80000000) {
913 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 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);
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
933 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
935 /* 0-pad to byte boundary */
937 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
942 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
944 static const 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) {
954 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
958 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
964 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
966 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
968 /* to avoid a drastic speed penalty we don't:
970 assert(bb->buffer != 0);
971 assert(bb->bits == 0);
975 if(bb->total_consumed_bits < bb->total_bits) {
976 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
978 if(bb->consumed_bits == 8) {
979 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
980 bb->consumed_bytes++;
981 bb->consumed_bits = 0;
983 bb->total_consumed_bits++;
987 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
993 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)
995 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
997 /* to avoid a drastic speed penalty we don't:
999 assert(bb->buffer != 0);
1000 assert(bb->bits == 0);
1004 if(bb->total_consumed_bits < bb->total_bits) {
1006 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1007 bb->consumed_bits++;
1008 if(bb->consumed_bits == 8) {
1009 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1010 bb->consumed_bytes++;
1011 bb->consumed_bits = 0;
1013 bb->total_consumed_bits++;
1017 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1023 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)
1025 static const byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
1027 /* to avoid a drastic speed penalty we don't:
1029 assert(bb->buffer != 0);
1030 assert(bb->bits == 0);
1034 if(bb->total_consumed_bits < bb->total_bits) {
1036 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
1037 bb->consumed_bits++;
1038 if(bb->consumed_bits == 8) {
1039 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1040 bb->consumed_bytes++;
1041 bb->consumed_bits = 0;
1043 bb->total_consumed_bits++;
1047 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1053 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)
1058 assert(bb->buffer != 0);
1063 for(i = 0; i < bits; i++) {
1064 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1070 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)
1076 assert(bb->buffer != 0);
1081 for(i = 0; i < bits; i++) {
1082 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
1098 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)
1103 assert(bb->buffer != 0);
1108 for(i = 0; i < bits; i++) {
1109 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1115 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)
1121 assert(bb->buffer != 0);
1126 for(i = 0; i < bits; i++) {
1127 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
1143 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1145 unsigned bit, val_ = 0;
1148 assert(bb->buffer != 0);
1151 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1162 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)
1164 uint32 sign = 0, lsbs = 0, msbs = 0;
1168 assert(bb->buffer != 0);
1169 assert(parameter <= 31);
1171 /* read the unary MSBs and end bit */
1173 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1180 /* read the sign bit */
1181 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1183 /* read the binary LSBs */
1184 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1187 /* compose the value */
1188 *val = (msbs << parameter) | lsbs;
1195 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)
1197 uint32 lsbs = 0, msbs = 0;
1201 assert(bb->buffer != 0);
1202 assert(parameter <= 31);
1204 /* read the unary MSBs and end bit */
1206 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1213 /* read the binary LSBs */
1214 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1216 /* compose the value */
1217 uval = (msbs << parameter) | lsbs;
1219 *val = -((int)(uval >> 1)) - 1;
1221 *val = (int)(uval >> 1);
1226 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)
1228 uint32 lsbs = 0, msbs = 0;
1229 unsigned bit, uval, k;
1232 assert(bb->buffer != 0);
1234 k = FLAC__bitmath_ilog2(parameter);
1236 /* read the unary MSBs and end bit */
1238 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1245 /* read the binary LSBs */
1246 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1249 if(parameter == 1u<<k) {
1250 /* compose the value */
1251 uval = (msbs << k) | lsbs;
1254 unsigned d = (1 << (k+1)) - parameter;
1256 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1262 /* compose the value */
1263 uval = msbs * parameter + lsbs;
1266 /* unfold unsigned to signed */
1268 *val = -((int)(uval >> 1)) - 1;
1270 *val = (int)(uval >> 1);
1275 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)
1277 uint32 lsbs, msbs = 0;
1281 assert(bb->buffer != 0);
1283 k = FLAC__bitmath_ilog2(parameter);
1285 /* read the unary MSBs and end bit */
1287 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1294 /* read the binary LSBs */
1295 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1298 if(parameter == 1u<<k) {
1299 /* compose the value */
1300 *val = (msbs << k) | lsbs;
1303 unsigned d = (1 << (k+1)) - parameter;
1305 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1311 /* compose the value */
1312 *val = msbs * parameter + lsbs;
1318 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1319 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)
1325 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1328 raw[(*rawlen)++] = (byte)x;
1329 if(!(x & 0x80)) { /* 0xxxxxxx */
1333 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1337 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1341 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1345 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1349 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1358 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1361 raw[(*rawlen)++] = (byte)x;
1362 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1373 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1374 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)
1380 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1383 raw[(*rawlen)++] = (byte)x;
1384 if(!(x & 0x80)) { /* 0xxxxxxx */
1388 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1392 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1396 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1400 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1404 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1408 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1413 *val = 0xffffffffffffffff;
1417 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1420 raw[(*rawlen)++] = (byte)x;
1421 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1422 *val = 0xffffffffffffffff;
1432 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1436 fprintf(out, "bitbuffer is NULL\n");
1439 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);
1440 for(i = 0; i < bb->bytes; i++) {
1441 fprintf(out, "%08X: ", i);
1442 for(j = 0; j < 8; j++)
1443 if(i*8+j < bb->total_consumed_bits)
1446 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1450 fprintf(out, "%08X: ", i);
1451 for(j = 0; j < bb->bits; j++)
1452 if(i*8+j < bb->total_consumed_bits)
1455 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);